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 answer = text 98 for x in ["\n", "\r"] : 99 answer = answer.replace(x, " ") 100 return answer.replace(" ", " ")
101
102 -class SequentialAlignmentWriter(AlignmentWriter) :
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
109 - def __init__(self, handle) :
110 self.handle = handle
111
112 - def write_file(self, alignments) :
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
126 - def write_header(self) :
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
140 - def write_alignment(self, alignment) :
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 # You SHOULD subclass this, to write the alignment # 148 # objecta to the file handle # 149 ##################################################### 150