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