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  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      
14 -class AlignmentIterator :
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 #TODO - Should the default be Gapped(single_letter_alphabet) instead?
22 - def __init__(self, handle, seq_count=None, 23 alphabet = single_letter_alphabet) :
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 # You may want to subclass this, for example # 41 # to read through the file to find the first record,# 42 # or if additional arguments are required. # 43 ##################################################### 44
45 - def next(self) :
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 # You SHOULD subclass this, to split the file up # 53 # into your individual alignments and convert these # 54 # into Alignment objects. # 55 ##################################################### 56
57 - def __iter__(self):
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
71 -class AlignmentWriter :
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
77 - def __init__(self, handle) :
78 self.handle = handle
79
80 - def write_file(self, alignments) :
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 # You SHOULD subclass this, to write the alignment # 92 # objecta to the file handle # 93 ##################################################### 94
95 - def clean(self, text) :
96 """Use this to avoid getting newlines in the output.""" 97 return text.replace("\n", " ").replace("\r", " ").replace(" ", " ")
98
99 -class SequentialAlignmentWriter(AlignmentWriter) :
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
106 - def __init__(self, handle) :
107 self.handle = handle
108
109 - def write_file(self, alignments) :
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
123 - def write_header(self) :
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
137 - def write_alignment(self, alignment) :
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 # You SHOULD subclass this, to write the alignment # 145 # objecta to the file handle # 146 ##################################################### 147