Back to index

python-biopython  1.60
Interfaces.py
Go to the documentation of this file.
00001 # Copyright 2008-2010 by Peter Cock.  All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 """
00006 AlignIO support module (not for general use).
00007 
00008 Unless you are writing a new parser or writer for Bio.AlignIO, you should not
00009 use this module.  It provides base classes to try and simplify things.
00010 """
00011 
00012 from Bio.Alphabet import single_letter_alphabet, Gapped
00013 
00014 class AlignmentIterator(object):
00015     """Base class for building MultipleSeqAlignment iterators.
00016 
00017     You should write a next() method to return Aligment
00018     objects.  You may wish to redefine the __init__
00019     method as well.
00020     """
00021     #TODO - Should the default be Gapped(single_letter_alphabet) instead?
00022     def __init__(self, handle, seq_count=None,
00023                  alphabet = single_letter_alphabet):
00024         """Create an AlignmentIterator object.
00025 
00026         handle   - input file
00027         count    - optional, expected number of records per alignment
00028                    Recommend for fasta file format.
00029         alphabet - optional, e.g. Bio.Alphabet.generic_protein
00030 
00031         Note when subclassing:
00032         - there should be a single non-optional argument, the handle,
00033           and optional count and alphabet IN THAT ORDER.
00034         - you do not have to require an alphabet (?).
00035         - you can add additional optional arguments."""
00036         self.handle = handle
00037         self.records_per_alignment = seq_count
00038         self.alphabet = alphabet
00039         #####################################################
00040         # You may want to subclass this, for example        #
00041         # to read through the file to find the first record,#
00042         # or if additional arguments are required.          #
00043         #####################################################
00044 
00045     def next(self):
00046         """Return the next alignment in the file.
00047         
00048         This method should be replaced by any derived class to do something
00049         useful."""
00050         raise NotImplementedError("This object should be subclassed")
00051         #####################################################
00052         # You SHOULD subclass this, to split the file up    #
00053         # into your individual alignments and convert these #
00054         # into MultipleSeqAlignment objects.                #
00055         #####################################################
00056 
00057     def __iter__(self):
00058         """Iterate over the entries as MultipleSeqAlignment objects.
00059 
00060         Example usage for (concatenated) PHYLIP files:
00061 
00062         myFile = open("many.phy","r")
00063         for alignment in PhylipIterator(myFile):
00064             print "New alignment:"
00065             for record in alignment:
00066                 print record.id
00067                 print record.seq
00068         myFile.close()"""
00069         return iter(self.next, None)
00070 
00071 class AlignmentWriter(object):
00072     """Base class for building MultipleSeqAlignment writers.
00073     
00074     You should write a write_alignment() method.
00075     You may wish to redefine the __init__ method as well"""
00076 
00077     def __init__(self, handle):
00078         self.handle = handle
00079        
00080     def write_file(self, alignments):
00081         """Use this to write an entire file containing the given alignments.
00082 
00083         alignments - A list or iterator returning MultipleSeqAlignment objects
00084 
00085         In general, this method can only be called once per file.
00086         
00087         This method should be replaced by any derived class to do something
00088         useful.  It should return the number of alignments"""
00089         raise NotImplementedError("This object should be subclassed")
00090         #####################################################
00091         # You SHOULD subclass this, to write the alignment  #
00092         # objecta to the file handle                        #
00093         #####################################################
00094 
00095     def clean(self, text):
00096         """Use this to avoid getting newlines in the output."""
00097         return text.replace("\n", " ").replace("\r", " ").replace("  ", " ")
00098     
00099 class SequentialAlignmentWriter(AlignmentWriter):
00100     """Base class for building MultipleSeqAlignment writers.
00101     
00102     This assumes each alignment can be simply appended to the file.
00103     You should write a write_alignment() method.
00104     You may wish to redefine the __init__ method as well"""
00105 
00106     def __init__(self, handle):
00107         self.handle = handle
00108        
00109     def write_file(self, alignments):
00110         """Use this to write an entire file containing the given alignments.
00111 
00112         alignments - A list or iterator returning MultipleSeqAlignment objects
00113 
00114         In general, this method can only be called once per file."""
00115         self.write_header()
00116         count = 0
00117         for alignment in alignments:
00118             self.write_alignment(alignment)
00119             count += 1
00120         self.write_footer()
00121         return count
00122         
00123     def write_header(self):
00124         """Use this to write any header.
00125         
00126         This method should be replaced by any derived class to do something
00127         useful."""
00128         pass
00129     
00130     def write_footer(self):
00131         """Use this to write any footer.
00132         
00133         This method should be replaced by any derived class to do something
00134         useful."""
00135         pass
00136 
00137     def write_alignment(self, alignment):
00138         """Use this to write a single alignment.
00139         
00140         This method should be replaced by any derived class to do something
00141         useful."""
00142         raise NotImplementedError("This object should be subclassed")
00143         #####################################################
00144         # You SHOULD subclass this, to write the alignment  #
00145         # objecta to the file handle                        #
00146         #####################################################
00147