Package Bio :: Package AlignIO :: Module Interfaces
[hide private]
[frames] | no frames]

Source Code for Module Bio.AlignIO.Interfaces

  1  # Copyright 2008-2013 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  """AlignIO support module (not for general use). 
  6   
  7  Unless you are writing a new parser or writer for Bio.AlignIO, you should not 
  8  use this module.  It provides base classes to try and simplify things. 
  9  """ 
 10   
 11  from __future__ import print_function 
 12   
 13  import sys  # for checking if Python 2 
 14   
 15  from Bio.Alphabet import single_letter_alphabet 
 16   
 17  __docformat__ = "restructuredtext en" 
 18   
 19   
20 -class AlignmentIterator(object):
21 """Base class for building MultipleSeqAlignment iterators. 22 23 You should write a next() method to return Aligment 24 objects. You may wish to redefine the __init__ 25 method as well. 26 """ 27 # TODO - Should the default be Gapped(single_letter_alphabet) instead?
28 - def __init__(self, handle, seq_count=None, 29 alphabet=single_letter_alphabet):
30 """Create an AlignmentIterator object. 31 32 - handle - input file 33 - count - optional, expected number of records per alignment 34 Recommend for fasta file format. 35 - alphabet - optional, e.g. Bio.Alphabet.generic_protein 36 37 Note when subclassing: 38 - there should be a single non-optional argument, the handle, 39 and optional count and alphabet IN THAT ORDER. 40 - you do not have to require an alphabet (?). 41 - you can add additional optional arguments. 42 43 """ 44 self.handle = handle 45 self.records_per_alignment = seq_count 46 self.alphabet = alphabet
47 ##################################################### 48 # You may want to subclass this, for example # 49 # to read through the file to find the first record,# 50 # or if additional arguments are required. # 51 ##################################################### 52
53 - def __next__(self):
54 """Return the next alignment in the file. 55 56 This method should be replaced by any derived class to do something 57 useful.""" 58 raise NotImplementedError("This object should be subclassed")
59 ##################################################### 60 # You SHOULD subclass this, to split the file up # 61 # into your individual alignments and convert these # 62 # into MultipleSeqAlignment objects. # 63 ##################################################### 64 65 if sys.version_info[0] < 3:
66 - def next(self):
67 """Python 2 style alias for Python 3 style __next__ method.""" 68 return self.__next__()
69
70 - def __iter__(self):
71 """Iterate over the entries as MultipleSeqAlignment objects. 72 73 Example usage for (concatenated) PHYLIP files:: 74 75 with open("many.phy","r") as myFile: 76 for alignment in PhylipIterator(myFile): 77 print "New alignment:" 78 for record in alignment: 79 print record.id 80 print record.seq 81 """ 82 return iter(self.__next__, None)
83 84
85 -class AlignmentWriter(object):
86 """Base class for building MultipleSeqAlignment writers. 87 88 You should write a write_alignment() method. 89 You may wish to redefine the __init__ method as well""" 90
91 - def __init__(self, handle):
92 self.handle = handle
93
94 - def write_file(self, alignments):
95 """Use this to write an entire file containing the given alignments. 96 97 alignments - A list or iterator returning MultipleSeqAlignment objects 98 99 In general, this method can only be called once per file. 100 101 This method should be replaced by any derived class to do something 102 useful. It should return the number of alignments""" 103 raise NotImplementedError("This object should be subclassed")
104 ##################################################### 105 # You SHOULD subclass this, to write the alignment # 106 # objecta to the file handle # 107 ##################################################### 108
109 - def clean(self, text):
110 """Use this to avoid getting newlines in the output.""" 111 return text.replace("\n", " ").replace("\r", " ").replace(" ", " ")
112 113
114 -class SequentialAlignmentWriter(AlignmentWriter):
115 """Base class for building MultipleSeqAlignment writers. 116 117 This assumes each alignment can be simply appended to the file. 118 You should write a write_alignment() method. 119 You may wish to redefine the __init__ method as well""" 120
121 - def __init__(self, handle):
122 self.handle = handle
123
124 - def write_file(self, alignments):
125 """Use this to write an entire file containing the given alignments. 126 127 alignments - A list or iterator returning MultipleSeqAlignment objects 128 129 In general, this method can only be called once per file.""" 130 self.write_header() 131 count = 0 132 for alignment in alignments: 133 self.write_alignment(alignment) 134 count += 1 135 self.write_footer() 136 return count
137
138 - def write_header(self):
139 """Use this to write any header. 140 141 This method should be replaced by any derived class to do something 142 useful.""" 143 pass
144 151
152 - def write_alignment(self, alignment):
153 """Use this to write a single alignment. 154 155 This method should be replaced by any derived class to do something 156 useful.""" 157 raise NotImplementedError("This object should be subclassed")
158 ##################################################### 159 # You SHOULD subclass this, to write the alignment # 160 # objecta to the file handle # 161 ##################################################### 162