1
2
3
4
5
6 from Bio.Alphabet import single_letter_alphabet, Gapped
7
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
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
35
36
37
38
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
47
48
49
50
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
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
73
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
86
87
88
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
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
105
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
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
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
139
140
141