Bio.SeqIO.Interfaces module¶
Bio.SeqIO support module (not for general use).
Unless you are writing a new parser or writer for Bio.SeqIO, you should not use this module. It provides base classes to try and simplify things.
-
class
Bio.SeqIO.Interfaces.
SequenceIterator
(handle, alphabet=Alphabet())¶ Bases:
object
Base class for building SeqRecord iterators.
You should write a __next__ method to return SeqRecord objects. You may wish to redefine the __init__ method as well.
-
__init__
(self, handle, alphabet=Alphabet())¶ Create a SequenceIterator object.
Arguments: - handle - input file - alphabet - optional, e.g. Bio.Alphabet.generic_protein
This method MAY be overridden by any subclass, for example if you need to process a header or accept additional arguments.
Note when subclassing: - there should be a single non-optional argument, the handle. - you do not have to require an alphabet. - you can add additional optional arguments.
-
__next__
(self)¶ Return the next record in the file.
This method’s stub-implementation MUST be overridden by any subclass to actually parse the file and return the next entry as a SeqRecord object.
-
__iter__
(self)¶ Iterate over the entries as a SeqRecord objects.
Example usage for Fasta files:
with open("example.fasta","r") as myFile: myFastaReader = FastaIterator(myFile) for record in myFastaReader: print(record.id) print(record.seq)
This method SHOULD NOT be overridden by any subclass. It should be left as is, which will call the subclass implementation of __next__ to actually parse the file.
-
-
class
Bio.SeqIO.Interfaces.
SequenceWriter
(handle)¶ Bases:
object
Base class for building SeqRecord writers.
Interlaced file formats (e.g. Clustal) should subclass directly.
Sequential file formats (e.g. Fasta, GenBank) should subclass the SequentialSequenceWriter class instead.
-
__init__
(self, handle)¶ Create the writer object.
Use the method write_file() to actually record your sequence records.
-
clean
(self, text)¶ Use this to avoid getting newlines in the output.
-
write_file
(self, records)¶ Use this to write an entire file containing the given records.
records - A list or iterator returning SeqRecord objects
Should return the number of records (as an integer).
This method can only be called once.
-
-
class
Bio.SeqIO.Interfaces.
SequentialSequenceWriter
(handle)¶ Bases:
Bio.SeqIO.Interfaces.SequenceWriter
Base class for sequence writers. This class should be subclassed.
It is intended for sequential file formats with an (optional) header, repeated records, and an (optional) footer.
In this case (as with interlaced file formats), the user may simply call the write_file() method and be done.
However, they may also call the write_header(), followed by multiple calls to write_record() and/or write_records() followed finally by write_footer().
Users must call write_header() and write_footer() even when the file format concerned doesn’t have a header or footer. This is to try and make life as easy as possible when switching the output format.
Note that write_header() cannot require any assumptions about the number of records.
-
__init__
(self, handle)¶ Initialize the class.
-
write_header
(self)¶ Write the file header.
If your file format defines a header, you should implement this method in order to write the header before any of the records.
The default implementation checks the private attribute ._header_written to ensure the header is only written once.
Write the file footer.
If your file format defines a footer, you should implement this method in order to write the footer after all the records.
The default implementation checks the private attribute ._footer_written to ensure the footer is only written once.
-
write_record
(self, record)¶ Write a single record to the output file.
record - a SeqRecord object
Once you have called write_header() you can call write_record() and/or write_records() as many times as needed. Then call write_footer() and close().
-
write_records
(self, records)¶ Write multiple record to the output file.
records - A list or iterator returning SeqRecord objects
Once you have called write_header() you can call write_record() and/or write_records() as many times as needed. Then call write_footer() and close().
Returns the number of records written.
-
write_file
(self, records)¶ Use this to write an entire file containing the given records.
records - A list or iterator returning SeqRecord objects
This method can only be called once. Returns the number of records written.
-