Bio.Align.interfaces module
Bio.Align support module (not for general use).
Unless you are writing a new parser or writer for Bio.Align, you should not use this module. It provides base classes to try and simplify things.
- class Bio.Align.interfaces.AlignmentIterator(source)
Bases:
AlignmentsAbstractBaseClass
Base class for building Alignment iterators.
You should write a parse method that returns an Alignment generator. You may wish to redefine the __init__ method as well.
Subclasses may define the following class attributes: - mode - ‘t’ or ‘b’ for text or binary files, respectively - fmt - a human-readable name for the file format.
- mode = 't'
- fmt: str | None = None
- __init__(source)
Create an AlignmentIterator object.
Arguments: - source - input file stream, or path to input file
This method MAY be overridden by any subclass.
Note when subclassing: - there should be a single non-optional argument, the source. - you can add additional optional arguments.
- __next__()
Return the next alignment.
- __len__()
Return the number of alignments.
The number of alignments is cached. If not yet calculated, the iterator is rewound to the beginning, and the number of alignments is calculated by iterating over the alignments. The iterator is then returned to its original position in the file.
- __enter__()
- __exit__(exc_type, exc_value, exc_traceback)
- __getitem__(index)
Return the alignments as an Alignments object (which inherits from list).
Only an index of the form [:] (i.e., a full slice) is supported. The file stream is returned to its zero position, and the file header is read and stored in an Alignments object. Next, we iterate over the alignments and store them in the Alignments object. The iterator is then returned to its original position in the file, and the Alignments object is returned. The Alignments object contains the exact same information as the Alignment iterator self, but stores the alignments in a list instead of as an iterator, allowing indexing.
Typical usage is
>>> from Bio import Align >>> alignments = Align.parse("Blat/dna_rna.psl", "psl") >>> alignments.metadata {'psLayout version': '3'}
As alignments is an iterator and not a list, we cannot retrieve an alignment by its index:
>>> alignment = alignments[2] Traceback (most recent call last): ... KeyError: 'only [:] (a full slice) can be used as the index'
So we use the iterator to create a list-like Alignments object:
>>> alignments = alignments[:]
While alignments is a list-like object, it has the same metadata attribute representing the information stored in the file header:
>>> alignments.metadata {'psLayout version': '3'}
Now we can index individual alignments:
>>> len(alignments) 4 >>> alignment = alignments[2] >>> alignment.target.id, alignment.query.id ('chr3', 'NR_111921.1')
- rewind()
Rewind the iterator to let it loop over the alignments from the beginning.
- __abstractmethods__ = frozenset({'_read_next_alignment'})
- __annotations__ = {'fmt': typing.Optional[str]}
- class Bio.Align.interfaces.AlignmentWriter(target)
Bases:
ABC
Base class for alignment writers. This class should be subclassed.
It is intended for alignment file formats with an (optional) header, one or more alignments, and an (optional) footer.
The user may call the write_file() method to write a complete file containing the alignments.
Alternatively, users may call the write_header(), followed by multiple calls to format_alignment() and/or write_alignments(), followed finally by write_footer().
Subclasses may define the following class attributes: - mode - ‘t’ or ‘b’ for text or binary files, respectively - fmt - a human-readable name for the file format.
- mode = 't'
- fmt: str | None = None
- __init__(target)
Create the writer object.
Arguments: - target - output file stream, or path to output file
This method MAY be overridden by any subclass.
Note when subclassing: - there should be a single non-optional argument, the target. - you can add additional optional arguments.
- write_header(stream, alignments)
Write the file header to the output file.
Write the file footer to the output file.
- format_alignment(alignment)
Format a single alignment as a string.
alignment - an Alignment object
- write_single_alignment(stream, alignments)
Write a single alignment to the output file, and return 1.
alignments - A list or iterator returning Alignment objects stream - Output file stream.
- write_multiple_alignments(stream, alignments)
Write alignments to the output file, and return the number of alignments.
alignments - A list or iterator returning Alignment objects stream - Output file stream.
- write_alignments(stream, alignments)
Write alignments to the output file, and return the number of alignments.
alignments - A list or iterator returning Alignment objects stream - Output file stream.
- write_file(stream, alignments)
Write the alignments to the file stream, and return the number of alignments.
alignments - A list or iterator returning Alignment objects stream - Output file stream.
- write(alignments)
Write a file with the alignments, and return the number of alignments.
alignments - A list or iterator returning Alignment objects
- __abstractmethods__ = frozenset({})
- __annotations__ = {'fmt': typing.Optional[str]}