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

Source Code for Module Bio.AlignIO.Interfaces

  1  # Copyright 2008 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   
  6  from Bio.Alphabet import single_letter_alphabet, Gapped 
  7      
8 -class AlignmentIterator :
9 """Base class for building Alignment iterators. 10 11 You should write a next() method to return Aligment 12 objects. You may wish to redefine the __init__ 13 method as well. 14 """ 15 #TODO - Should the default be Gapped(single_letter_alphabet) instead?
16 - def __init__(self, handle, seq_count=None, 17 alphabet = single_letter_alphabet) :
18 """Create an AlignmentIterator object. 19 20 handle - input file 21 count - optional, expected number of records per alignment 22 Recommend for fasta file format. 23 alphabet - optional, e.g. Bio.Alphabet.generic_protein 24 25 Note when subclassing: 26 - there should be a single non-optional argument, the handle, 27 and optional count and alphabet IN THAT ORDER. 28 - you do not have to require an alphabet (?). 29 - you can add additional optional arguments.""" 30 self.handle = handle 31 self.records_per_alignment = seq_count 32 self.alphabet = alphabet
33 ##################################################### 34 # You may want to subclass this, for example # 35 # to read through the file to find the first record,# 36 # or if additional arguments are required. # 37 ##################################################### 38
39 - def next(self) :
40 """Return the next alignment in the file. 41 42 This method should be replaced by any derived class to do something 43 useful.""" 44 raise NotImplementedError, "This object should be subclassed"
45 ##################################################### 46 # You SHOULD subclass this, to split the file up # 47 # into your individual alignments and convert these # 48 # into Alignment objects. # 49 ##################################################### 50
51 - def __iter__(self):
52 """Iterate over the entries as Alignment objects. 53 54 Example usage for (concatenated) PHYLIP files: 55 56 myFile = open("many.phy","r") 57 for alignment in PhylipIterator(myFile) : 58 print "New alignment:" 59 for record in alignment : 60 print record.id 61 print record.seq 62 myFile.close()""" 63 return iter(self.next, None)
64
65 -class AlignmentWriter :
66 """Base class for building Alignment writers. 67 68 You should write a write_alignment() method. 69 You may wish to redefine the __init__ method as well""" 70
71 - def __init__(self, handle) :
72 self.handle = handle
73
74 - def write_file(self, alignments) :
75 """Use this to write an entire file containing the given alignments. 76 77 alignments - A list or iterator returning Alignment objects 78 79 In general, this method can only be called once per file. 80 81 This method should be replaced by any derived class to do something 82 useful.""" 83 raise NotImplementedError, "This object should be subclassed"
84 ##################################################### 85 # You SHOULD subclass this, to write the alignment # 86 # objecta to the file handle # 87 ##################################################### 88
89 - def clean(self, text) :
90 """Use this to avoid getting newlines in the output.""" 91 answer = text 92 for x in ["\n", "\r"] : 93 answer = answer.replace(x, " ") 94 return answer.replace(" ", " ")
95
96 -class SequentialAlignmentWriter(AlignmentWriter) :
97 """Base class for building Alignment writers. 98 99 This assumes each alignment can be simply appended to the file. 100 You should write a write_alignment() method. 101 You may wish to redefine the __init__ method as well""" 102
103 - def __init__(self, handle) :
104 self.handle = handle
105
106 - def write_file(self, alignments) :
107 """Use this to write an entire file containing the given alignments. 108 109 alignments - A list or iterator returning Alignment objects 110 111 In general, this method can only be called once per file.""" 112 self.write_header() 113 for alignment in alignments : 114 self.write_alignment(alignment) 115 self.write_footer()
116
117 - def write_header(self) :
118 """Use this to write any header. 119 120 This method should be replaced by any derived class to do something 121 useful.""" 122 pass
123 130
131 - def write_alignment(self, alignment) :
132 """Use this to write a single alignment. 133 134 This method should be replaced by any derived class to do something 135 useful.""" 136 raise NotImplementedError, "This object should be subclassed"
137 ##################################################### 138 # You SHOULD subclass this, to write the alignment # 139 # objecta to the file handle # 140 ##################################################### 141