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 26 # TODO - Should the default be Gapped(single_letter_alphabet) instead?
27 - def __init__(self, handle, seq_count=None, 28 alphabet=single_letter_alphabet):
29 """Create an AlignmentIterator object. 30 31 Arguments: 32 - handle - input file 33 - count - optional, expected number of records per alignment 34 Recommend for fasta file format. 35 - alphabet - optional, e.g. Bio.Alphabet.generic_protein 36 37 Note when subclassing: 38 - there should be a single non-optional argument, the handle, 39 and optional count and alphabet IN THAT ORDER. 40 - you do not have to require an alphabet (?). 41 - you can add additional optional arguments. 42 43 """ 44 self.handle = handle 45 self.records_per_alignment = seq_count 46 self.alphabet = alphabet
47 ##################################################### 48 # You may want to subclass this, for example # 49 # to read through the file to find the first record,# 50 # or if additional arguments are required. # 51 ##################################################### 52
53 - def __next__(self):
54 """Return the next alignment in the file. 55 56 This method should be replaced by any derived class to do something 57 useful. 58 """ 59 raise NotImplementedError("This object should be subclassed")
60 ##################################################### 61 # You SHOULD subclass this, to split the file up # 62 # into your individual alignments and convert these # 63 # into MultipleSeqAlignment objects. # 64 ##################################################### 65 66 if sys.version_info[0] < 3:
67 - def next(self):
68 """Python 2 style alias for Python 3 style __next__ method.""" 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 """ 84 return iter(self.__next__, None)
85 86
87 -class AlignmentWriter(object):
88 """Base class for building MultipleSeqAlignment writers. 89 90 You should write a write_alignment() method. 91 You may wish to redefine the __init__ method as well. 92 """ 93
94 - def __init__(self, handle):
95 self.handle = handle
96
97 - def write_file(self, alignments):
98 """Use this to write an entire file containing the given alignments. 99 100 Arguments: 101 - alignments - A list or iterator returning MultipleSeqAlignment objects 102 103 In general, this method can only be called once per file. 104 105 This method should be replaced by any derived class to do something 106 useful. It should return the number of alignments.. 107 """ 108 raise NotImplementedError("This object should be subclassed")
109 ##################################################### 110 # You SHOULD subclass this, to write the alignment # 111 # objecta to the file handle # 112 ##################################################### 113
114 - def clean(self, text):
115 """Use this to avoid getting newlines in the output.""" 116 return text.replace("\n", " ").replace("\r", " ").replace(" ", " ")
117 118
119 -class SequentialAlignmentWriter(AlignmentWriter):
120 """Base class for building MultipleSeqAlignment writers. 121 122 This assumes each alignment can be simply appended to the file. 123 You should write a write_alignment() method. 124 You may wish to redefine the __init__ method as well. 125 """ 126
127 - def __init__(self, handle):
128 self.handle = handle
129
130 - def write_file(self, alignments):
131 """Use this to write an entire file containing the given alignments. 132 133 Arguments: 134 - alignments - A list or iterator returning MultipleSeqAlignment objects 135 136 In general, this method can only be called once per file. 137 """ 138 self.write_header() 139 count = 0 140 for alignment in alignments: 141 self.write_alignment(alignment) 142 count += 1 143 self.write_footer() 144 return count
145
146 - def write_header(self):
147 """Use this to write any header. 148 149 This method should be replaced by any derived class to do something 150 useful. 151 """ 152 pass
153 161
162 - def write_alignment(self, alignment):
163 """Use this to write a single alignment. 164 165 This method should be replaced by any derived class to do something 166 useful. 167 """ 168 raise NotImplementedError("This object should be subclassed")
169 ##################################################### 170 # You SHOULD subclass this, to write the alignment # 171 # objecta to the file handle # 172 ##################################################### 173