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

Source Code for Module Bio.AlignIO.Interfaces

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