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 return text.replace("\n", " ").replace("\r", " ").replace(" ", " ")
98
100 """Base class for building Alignment writers.
101
102 This assumes each alignment can be simply appended to the file.
103 You should write a write_alignment() method.
104 You may wish to redefine the __init__ method as well"""
105
108
110 """Use this to write an entire file containing the given alignments.
111
112 alignments - A list or iterator returning Alignment objects
113
114 In general, this method can only be called once per file."""
115 self.write_header()
116 count = 0
117 for alignment in alignments :
118 self.write_alignment(alignment)
119 count += 1
120 self.write_footer()
121 return count
122
124 """Use this to write any header.
125
126 This method should be replaced by any derived class to do something
127 useful."""
128 pass
129
136
138 """Use this to write a single alignment.
139
140 This method should be replaced by any derived class to do something
141 useful."""
142 raise NotImplementedError("This object should be subclassed")
143
144
145
146
147