1
2
3
4
5 """
6 AlignIO support module (not for general use).
7
8 Unless you are writing a new parser or writer for Bio.AlignIO, you should not
9 use this module. It provides base classes to try and simplify things.
10 """
11
12 from Bio.Alphabet import single_letter_alphabet
13
14
16 """Base class for building MultipleSeqAlignment iterators.
17
18 You should write a next() method to return Aligment
19 objects. You may wish to redefine the __init__
20 method as well.
21 """
22
25 """Create an AlignmentIterator object.
26
27 handle - input file
28 count - optional, expected number of records per alignment
29 Recommend for fasta file format.
30 alphabet - optional, e.g. Bio.Alphabet.generic_protein
31
32 Note when subclassing:
33 - there should be a single non-optional argument, the handle,
34 and optional count and alphabet IN THAT ORDER.
35 - you do not have to require an alphabet (?).
36 - you can add additional optional arguments."""
37 self.handle = handle
38 self.records_per_alignment = seq_count
39 self.alphabet = alphabet
40
41
42
43
44
45
47 """Return the next alignment in the file.
48
49 This method should be replaced by any derived class to do something
50 useful."""
51 raise NotImplementedError("This object should be subclassed")
52
53
54
55
56
57
59 """Iterate over the entries as MultipleSeqAlignment objects.
60
61 Example usage for (concatenated) PHYLIP files:
62
63 myFile = open("many.phy","r")
64 for alignment in PhylipIterator(myFile):
65 print "New alignment:"
66 for record in alignment:
67 print record.id
68 print record.seq
69 myFile.close()"""
70 return iter(self.next, None)
71
72
74 """Base class for building MultipleSeqAlignment writers.
75
76 You should write a write_alignment() method.
77 You may wish to redefine the __init__ method as well"""
78
81
83 """Use this to write an entire file containing the given alignments.
84
85 alignments - A list or iterator returning MultipleSeqAlignment objects
86
87 In general, this method can only be called once per file.
88
89 This method should be replaced by any derived class to do something
90 useful. It should return the number of alignments"""
91 raise NotImplementedError("This object should be subclassed")
92
93
94
95
96
98 """Use this to avoid getting newlines in the output."""
99 return text.replace("\n", " ").replace("\r", " ").replace(" ", " ")
100
101
103 """Base class for building MultipleSeqAlignment writers.
104
105 This assumes each alignment can be simply appended to the file.
106 You should write a write_alignment() method.
107 You may wish to redefine the __init__ method as well"""
108
111
113 """Use this to write an entire file containing the given alignments.
114
115 alignments - A list or iterator returning MultipleSeqAlignment objects
116
117 In general, this method can only be called once per file."""
118 self.write_header()
119 count = 0
120 for alignment in alignments:
121 self.write_alignment(alignment)
122 count += 1
123 self.write_footer()
124 return count
125
127 """Use this to write any header.
128
129 This method should be replaced by any derived class to do something
130 useful."""
131 pass
132
139
141 """Use this to write a single alignment.
142
143 This method should be replaced by any derived class to do something
144 useful."""
145 raise NotImplementedError("This object should be subclassed")
146
147
148
149
150