Sequence IO

Sequence file reading and writing

Biological sequence data is stored and transmitted using a wide variety of different file formats. This package provides convenient methods to read and write several of these file fomats.

WebLogo is often capable of guessing the correct file type, either from the file extension or the structure of the file:

>>> import weblogo.seq_io
>>> afile = open("test_weblogo/data/cap.fa")
>>> seqs = weblogo.seq_io.read(afile)

Alternatively, each sequence file type has a separate module named FILETYPE_io (e.g. fasta_io, clustal_io):

>>> import weblogo.seq_io.fasta_io
>>> afile = open("test_weblogo/data/cap.fa")
>>> seqs = weblogo.seq_io.fasta_io.read(afile)

Sequence data can also be written back to files:

>>> fout = open("out.fa", "w")
>>> weblogo.seq_io.fasta_io.write(fout, seqs)

Supported File Formats

Module              Name            Extension  read write features
---------------------------------------------------------------------------
array_io            array, flatfile             yes  yes    none
clustal_io          clustalw        aln         yes  yes
fasta_io            fasta, Pearson  fa          yes  yes    none
genbank_io          genbank         gb          yes
intelligenetics_io  intelligenetics ig          yes  yes
msf_io              msf             msf         yes
nbrf_io             nbrf, pir       pir         yes
nexus_io            nexus           nexus       yes
phylip_io           phylip          phy         yes
plain_io            plain, raw                  yes  yes    none
table_io            table           tbl         yes  yes    none

Each IO module defines one or more of the following functions and variables:

read(afile, alphabet=None)
Read a file of sequence data and return a SeqList, a collection of Seq’s (Alphabetic strings) and features.
read_seq(afile, alphabet=None)
Read a single sequence from a file.
iter_seq(afile, alphabet =None)
Iterate over the sequences in a file.
index(afile, alphabet = None)
Instead of loading all of the sequences into memory, scan the file and return an index map that will load sequences on demand. Typically not implemented for formats with interleaved sequences.
write(afile, seqlist)
Write a collection of sequences to the specifed file.
write_seq(afile, seq)
Write one sequence to the file. Only implemented for non-interleaved, headerless formats, such as fasta and plain.
example
A string containing a short example of the file format
names
A list of synonyms for the file format. E.g. for fasta_io, ( ‘fasta’, ‘pearson’, ‘fa’). The first entry is the preferred format name.
extensions
A list of file name extensions used for this file format. e.g. fasta_io.extensions is (‘fa’, ‘fasta’, ‘fast’, ‘seq’, ‘fsa’, ‘fst’, ‘nt’, ‘aa’,’fna’,’mpfa’). The preferred or standard extension is first in the list.
Attributes :
  • formats: Available seq_io format parsers
  • format_names: A map between format names and format parsers.
  • format_extensions: A map between filename extensions and parsers.
weblogo.seq_io.read(fin: TextIO, alphabet: weblogo.seq.Alphabet = None) → weblogo.seq.SeqList

Read a sequence file and attempt to guess its format. First the filename extension (if available) is used to infer the format. If that fails, then we attempt to parse the file using several common formats.

Note, fin cannot be unseekable stream such as sys.stdin

returns :
SeqList
Raises :
ValueError: If the file cannot be parsed. ValueError: Sequence do not conform to the alphabet.
weblogo.seq_io.format_names()

Return a map between format names and format modules

weblogo.seq_io.format_extensions()

Return a map between filename extensions and sequence file types