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, Gapped
13
15 """Base class for building Alignment iterators.
16
17 You should write a next() method to return Aligment
18 objects. You may wish to redefine the __init__
19 method as well.
20 """
21
24 """Create an AlignmentIterator object.
25
26 handle - input file
27 count - optional, expected number of records per alignment
28 Recommend for fasta file format.
29 alphabet - optional, e.g. Bio.Alphabet.generic_protein
30
31 Note when subclassing:
32 - there should be a single non-optional argument, the handle,
33 and optional count and alphabet IN THAT ORDER.
34 - you do not have to require an alphabet (?).
35 - you can add additional optional arguments."""
36 self.handle = handle
37 self.records_per_alignment = seq_count
38 self.alphabet = alphabet
39
40
41
42
43
44
46 """Return the next alignment in the file.
47
48 This method should be replaced by any derived class to do something
49 useful."""
50 raise NotImplementedError("This object should be subclassed")
51
52
53
54
55
56
58 """Iterate over the entries as Alignment objects.
59
60 Example usage for (concatenated) PHYLIP files:
61
62 myFile = open("many.phy","r")
63 for alignment in PhylipIterator(myFile) :
64 print "New alignment:"
65 for record in alignment :
66 print record.id
67 print record.seq
68 myFile.close()"""
69 return iter(self.next, None)
70
72 """Base class for building Alignment writers.
73
74 You should write a write_alignment() method.
75 You may wish to redefine the __init__ method as well"""
76
79
81 """Use this to write an entire file containing the given alignments.
82
83 alignments - A list or iterator returning Alignment objects
84
85 In general, this method can only be called once per file.
86
87 This method should be replaced by any derived class to do something
88 useful. It should return the number of alignments"""
89 raise NotImplementedError("This object should be subclassed")
90
91
92
93
94
96 """Use this to avoid getting newlines in the output."""
97 answer = text
98 for x in ["\n", "\r"] :
99 answer = answer.replace(x, " ")
100 return answer.replace(" ", " ")
101
103 """Base class for building Alignment 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 Alignment 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