Package AlignIO
source code
Alignment input/output designed to look similar to Bio.SeqIO.
Input
=====
For the typical special case when your file or handle contains one and only
one alignment, use the function Bio.AlignIO.read(). This takes an input file
handle, format string and optional number of sequences per alignment. It will
return a single Alignment object (or raise an exception if there isn't just
one alignment):
from Bio import AlignIO
handle = open("example.aln", "rU")
align = AlignIO.read(handle, "clustal")
handle.close()
print align
For the general case, when the handle could contain any number of alignments,
use the function Bio.AlignIO.parse(...) which takes the same arguments, but
returns an iterator giving Alignment objects. For example, using the output
from the EMBOSS water or needle pairwise alignment prorams:
from Bio import AlignIO
handle = open("example.txt", "rU")
for alignment in AlignIO.parse(handle, "emboss") :
print alignment
If you want random access to the alignments by number, turn this into a list:
from Bio import AlignIO
handle = open("example.aln", "rU")
alignments = list(AlignIO.parse(handle, "clustal"))
print alignments[0]
Most alignment file formats can be concatenated so as to hold as many
different multiple sequence alignments as possible. One common example
is the output of the tool seqboot in the PHLYIP suite. Sometimes there
can be a file header and footer, as seen in the EMBOSS alignment output.
There is an optional argument for the number of sequences per alignment which
is usually only needed with the alignments stored in the FASTA format.
Without this information, there is no clear way to tell if you have say a
single alignment of 20 sequences, or four alignments of 5 sequences. e.g.
from Bio import AlignIO
handle = open("example.faa", "rU")
for alignment in AlignIO.parse(handle, "fasta", seq_count=5) :
print alignment
The above code would split up the FASTA files, and try and batch every five
sequences into an alignment.
Output
======
Use the function Bio.AlignIO.write(...), which takes a complete set of
Alignment objects (either as a list, or an iterator), an output file handle
and of course the file format.
from Bio import AlignIO
alignments = ...
handle = open("example.faa", "w")
alignment = SeqIO.write(alignments, handle, "fasta")
handle.close()
In general, you are expected to call this function once (with all your
alignments) and then close the file handle. However, for file formats
like PHYLIP where multiple alignments are stored sequentially (with no file
header and footer), then multiple calls to the write function should work as
expected.
File Formats
============
When specifying the file format, use lowercase strings. The same format
names are also used in Bio.SeqIO and include the following:
clustal - Ouput from Clustal W or X, see also the module Bio.Clustalw
which can be used to run the command line tool from Biopython.
emboss - The "pairs" and "simple" alignment format from the EMBOSS tools.
fasta - The generic sequence file format where each record starts with a
identifer line starting with a ">" character, followed by lines
of sequence.
fasta-m10 - For the pairswise alignments output by Bill Pearson's FASTA
tools when used with the -m 10 command line option for machine
readable output.
nexus - Output from NEXUS, see also the module Bio.Nexus which can also
read any phylogenetic trees in these files.
phylip - Used by the PHLIP tools.
stockholm - A richly annotated alignment file format used by PFAM.
Further Information
===================
See the wiki page biopython.org/wiki/AlignIO and also the Bio.AlignIO chapter
in the Biopython Tutorial and Cookbook which is also available online:
http://biopython.org/DIST/docs/tutorial/Tutorial.html
http://biopython.org/DIST/docs/tutorial/Tutorial.pdf
|
write(alignments,
handle,
format)
Write complete set of alignments to a file. |
source code
|
|
|
|
|
parse(handle,
format,
seq_count=None)
Turns a sequence file into an iterator returning Alignment objects. |
source code
|
|
|
read(handle,
format,
seq_count=None)
Turns an alignment file into a single Alignment object. |
source code
|
|
Write complete set of alignments to a file.
sequences - A list (or iterator) of Alignment objects handle - File
handle object to write to format - What format to use.
You should close the handle after calling this function.
There is no return value.
|
_SeqIO_to_alignment_iterator(handle,
format,
seq_count=None)
| source code
|
Private function, uses Bio.SeqIO to create an Alignment iterator.
handle - handle to the file.
format - string describing the file format.
seq_count- Optional integer, number of sequences expected in
each alignment. Recommended for fasta format files.
If count is omitted (default) then all the sequences in
the file are combined into a single Alignment.
|
Turns a sequence file into an iterator returning Alignment objects.
handle - handle to the file.
format - string describing the file format.
seq_count- Optional integer, number of sequences expected in
each alignment. Recommended for fasta format files.
If you have the file name in a string 'filename', use:
from Bio import AlignIO
my_iterator = AlignIO.parse(open(filename,"rU"), format)
If you have a string 'data' containing the file contents, use:
from Bio import AlignIO
from StringIO import StringIO
my_iterator = AlignIO.parse(StringIO(data), format)
Use the Bio.AlignIO.read(handle, format[, seq_count]) function when
you expect a single record only.
|
Turns an alignment file into a single Alignment object.
handle - handle to the file.
format - string describing the file format.
seq_count- Optional interger, number of sequences expected in
the alignment to check you got what you expected.
If the handle contains no alignments, or more than one alignment,
an exception is raised. For example, using a PFAM/Stockholm file
containing one alignment:
from Bio import AlignIO
align = AlignIO.read(open("example.sth"), "stockholm")
If however you want the first alignment from a file containing
multiple alignments this function would raise an exception.
Instead use:
from Bio import AlignIO
align = AlignIO.parse(open("example.sth"), "stockholm").next()
Use the Bio.AlignIO.parse() function if you want to read multiple
records from the handle.
|
_FormatToIterator
- Value:
{"clustal": ClustalIO.ClustalIterator, "emboss": EmbossIO.EmbossIterat
or, "fasta-m10": FastaIO.FastaM10Iterator, "nexus": NexusIO.NexusItera
tor, "phylip": PhylipIO.PhylipIterator, "stockholm": StockholmIO.Stock
holmIterator,}
|
|
_FormatToWriter
- Value:
{"phylip": PhylipIO.PhylipWriter, "stockholm": StockholmIO.StockholmWr
iter, "clustal": ClustalIO.ClustalWriter,}
|
|