Package Bio :: Package AlignIO :: Module Interfaces
[hide private]
[frames] | no frames]

Source Code for Module Bio.AlignIO.Interfaces

  1  # Copyright 2008-2013 by Peter Cock.  All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5  """AlignIO support module (not for general use). 
  6   
  7  Unless you are writing a new parser or writer for Bio.AlignIO, you should not 
  8  use this module.  It provides base classes to try and simplify things. 
  9  """ 
 10   
 11  from __future__ import print_function 
 12   
 13  import sys # for checking if Python 2 
 14   
 15  from Bio.Alphabet import single_letter_alphabet 
 16   
 17   
18 -class AlignmentIterator(object):
19 """Base class for building MultipleSeqAlignment iterators. 20 21 You should write a next() method to return Aligment 22 objects. You may wish to redefine the __init__ 23 method as well. 24 """ 25 #TODO - Should the default be Gapped(single_letter_alphabet) instead?
26 - def __init__(self, handle, seq_count=None, 27 alphabet = single_letter_alphabet):
28 """Create an AlignmentIterator object. 29 30 handle - input file 31 count - optional, expected number of records per alignment 32 Recommend for fasta file format. 33 alphabet - optional, e.g. Bio.Alphabet.generic_protein 34 35 Note when subclassing: 36 - there should be a single non-optional argument, the handle, 37 and optional count and alphabet IN THAT ORDER. 38 - you do not have to require an alphabet (?). 39 - you can add additional optional arguments.""" 40 self.handle = handle 41 self.records_per_alignment = seq_count 42 self.alphabet = alphabet
43 ##################################################### 44 # You may want to subclass this, for example # 45 # to read through the file to find the first record,# 46 # or if additional arguments are required. # 47 ##################################################### 48
49 - def __next__(self):
50 """Return the next alignment in the file. 51 52 This method should be replaced by any derived class to do something 53 useful.""" 54 raise NotImplementedError("This object should be subclassed")
55 ##################################################### 56 # You SHOULD subclass this, to split the file up # 57 # into your individual alignments and convert these # 58 # into MultipleSeqAlignment objects. # 59 ##################################################### 60 61 if sys.version_info[0] < 3:
62 - def next(self):
63 """Deprecated Python 2 style alias for Python 3 style __next__ method.""" 64 import warnings 65 from Bio import BiopythonDeprecationWarning 66 warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), " 67 "the .next() method is deprecated and will be removed in a " 68 "future release of Biopython.", BiopythonDeprecationWarning) 69 return self.__next__()
70
71 - def __iter__(self):
72 """Iterate over the entries as MultipleSeqAlignment objects. 73 74 Example usage for (concatenated) PHYLIP files: 75 76 with open("many.phy","r") as myFile: 77 for alignment in PhylipIterator(myFile): 78 print "New alignment:" 79 for record in alignment: 80 print record.id 81 print record.seq 82 """ 83 return iter(self.__next__, None)
84 85
86 -class AlignmentWriter(object):
87 """Base class for building MultipleSeqAlignment writers. 88 89 You should write a write_alignment() method. 90 You may wish to redefine the __init__ method as well""" 91
92 - def __init__(self, handle):
93 self.handle = handle
94
95 - def write_file(self, alignments):
96 """Use this to write an entire file containing the given alignments. 97 98 alignments - A list or iterator returning MultipleSeqAlignment objects 99 100 In general, this method can only be called once per file. 101 102 This method should be replaced by any derived class to do something 103 useful. It should return the number of alignments""" 104 raise NotImplementedError("This object should be subclassed")
105 ##################################################### 106 # You SHOULD subclass this, to write the alignment # 107 # objecta to the file handle # 108 ##################################################### 109
110 - def clean(self, text):
111 """Use this to avoid getting newlines in the output.""" 112 return text.replace("\n", " ").replace("\r", " ").replace(" ", " ")
113 114
115 -class SequentialAlignmentWriter(AlignmentWriter):
116 """Base class for building MultipleSeqAlignment writers. 117 118 This assumes each alignment can be simply appended to the file. 119 You should write a write_alignment() method. 120 You may wish to redefine the __init__ method as well""" 121
122 - def __init__(self, handle):
123 self.handle = handle
124
125 - def write_file(self, alignments):
126 """Use this to write an entire file containing the given alignments. 127 128 alignments - A list or iterator returning MultipleSeqAlignment objects 129 130 In general, this method can only be called once per file.""" 131 self.write_header() 132 count = 0 133 for alignment in alignments: 134 self.write_alignment(alignment) 135 count += 1 136 self.write_footer() 137 return count
138
139 - def write_header(self):
140 """Use this to write any header. 141 142 This method should be replaced by any derived class to do something 143 useful.""" 144 pass
145 152
153 - def write_alignment(self, alignment):
154 """Use this to write a single alignment. 155 156 This method should be replaced by any derived class to do something 157 useful.""" 158 raise NotImplementedError("This object should be subclassed")
159 ##################################################### 160 # You SHOULD subclass this, to write the alignment # 161 # objecta to the file handle # 162 ##################################################### 163