Loading an alignment from a file or url¶
Loading aligned sequences¶
The load functions record the origin of the data in the info
attribute under a “source” key.
Note
The function load_aligned_seqs()
returns an ArrayAlignment
by default. If you set the argument array_align=False
, you will get an Alignment
. (That class can be annotated.)
Loading unaligned sequences¶
The load_unaligned_seqs()
function returns a sequence collection.
Loading from a url¶
The cogent3
load functions support loading from a url. We load the above fasta file directly from GitHub.
Specifying the file format¶
The loading functions use the filename suffix to infer the file format. This can be overridden using the format
argument.
Specifying the sequence molecular type¶
Simple case of loading a list
of aligned amino acid sequences in FASTA format, with and without moltype
specification. When moltype
is not specified it defaults to BYTES
for the ArrayAlignment
class, ASCII
for the Alignment
class.
Note
This applies to both the load_*
or make_*
functions.
Making an alignment from standard python objects¶
From a dict of strings¶
From a series of strings¶
Stripping label characters on loading¶
Load a list of aligned nucleotide sequences, while specifying the DNA molecule type and stripping the comments from the label. In this example, stripping is accomplished by passing a function that removes everything after the first whitespace to the label_to_name
parameter.
Making a sequence collection from standard python objects¶
This is done using make_unaligned_seqs()
, which returns a SequenceCollection
instance. The function arguments match those of make_aligned_seqs()
. We demonstrate only for the case where the input data is a dict
.
Loading sequences using format parsers¶
load_aligned_seqs()
and load_unaligned_seqs()
are just convenience interfaces to format parsers. It can sometimes be more effective to use the parsers directly, say when you don’t want to load everything into memory.
Loading FASTA sequences from an open file or list of lines¶
To load FASTA formatted sequences directly, you can use the MinimalFastaParser
.
Note
This returns the sequences as strings.
Handling overloaded FASTA sequence labels¶
The FASTA label field is frequently overloaded, with different information fields present in the field and separated by some delimiter. This can be flexibly addressed using the LabelParser
. By creating a custom label parser, we can decide which part we use as the sequence name. We show how to convert a field into something specific.
RichLabel
objects have an Info
object as an attribute, allowing specific reference to all the specified label fields.