core.seq_storage.AlignedSeqsData

core.seq_storage.AlignedSeqsData(
    gapped_seqs,
    names,
    alphabet,
    ungapped_seqs=None,
    gaps=None,
    offset=None,
    align_len=None,
    check=True,
    reversed_seqs=None,
)

The builtin cogent3 implementation of aligned sequences storage underlying an Alignment. Indexing this object returns an AlignedDataView which can realise the corresponding slice as a string, bytes, or numpy array, gapped or ungapped.

Notes

Methods on this object only accepts plust strand start, stop and step indices for selecting segments of data. It can return the gap coordinates for a sequence as used by IndelMap.

Attributes

Name Description
align_len Return the length of the alignment.
alphabet the character alphabet for validating, encoding, decoding sequences
names returns the names of the sequences in the storage
offset returns the offset of each sequence in the Alignment
reversed_seqs names of sequences that are reverse complemented

Methods

Name Description
add_seqs Returns a new AlignedSeqsData object with added sequences.
copy shallow copy of self
from_names_and_array Construct an AlignedSeqsData object from a list of names and a numpy
from_seqs Construct an AlignedSeqsData object from a dict of aligned sequences
from_seqs_and_gaps Construct an AlignedSeqsData object from a dict of ungapped sequences
get_gapped_seq_array Return sequence data corresponding to seqid as an array of indices.
get_gapped_seq_bytes Return sequence corresponding to seqid as a bytes string.
get_gapped_seq_str Return sequence corresponding to seqid as a string.
get_gaps returns the gap data for seqid
get_hash returns hash of seqid
get_pos_range returns an array of the selected positions for names.
get_positions returns alignment positions for names
get_seq_array Return ungapped sequence corresponding to seqid as an array of indices.
get_seq_bytes Return ungapped sequence corresponding to seqid as a bytes string.
get_seq_length return length of the unaligned seq for seqid
get_seq_str Return ungapped sequence corresponding to seqid as a string.
get_view reurns view of aligned sequence data for seqid
to_alphabet Returns a new AlignedSeqsData object with the same underlying data
variable_positions returns absolute indices of positions that have more than one state

add_seqs

core.seq_storage.AlignedSeqsData.add_seqs(
    seqs,
    force_unique_keys=True,
    offset=None,
)

Returns a new AlignedSeqsData object with added sequences.

Parameters

Name Type Description Default
seqs Mapping[str, str | bytes | NumpyIntArrayType] dict of sequences to add {name: seq, …} required
force_unique_keys bool if True, raises ValueError if any sequence names already exist in the collection True
offset dict[str, int] | None dict of offsets relative to for the new sequences. None

copy

core.seq_storage.AlignedSeqsData.copy(**kwargs)

shallow copy of self

Notes

kwargs are passed to constructor and will over-ride existing values

from_names_and_array

core.seq_storage.AlignedSeqsData.from_names_and_array(names, data, alphabet)

Construct an AlignedSeqsData object from a list of names and a numpy array of aligned sequence data.

Parameters

Name Type Description Default
names Collection[str] list of sequence names required
data NumpyIntArrayType numpy array of aligned sequence data required
alphabet c3_alphabet.CharAlphabet[Any] alphabet object for the sequences required

from_seqs

core.seq_storage.AlignedSeqsData.from_seqs(data, alphabet, **kwargs)

Construct an AlignedSeqsData object from a dict of aligned sequences

Parameters

Name Type Description Default
data Mapping[str, str | bytes | NumpyIntArrayType] dict of gapped sequences {name: seq, …}. sequences must all be the same length required
alphabet c3_alphabet.CharAlphabet[Any] alphabet object for the sequences required

from_seqs_and_gaps

core.seq_storage.AlignedSeqsData.from_seqs_and_gaps(
    seqs,
    gaps,
    alphabet,
    **kwargs,
)

Construct an AlignedSeqsData object from a dict of ungapped sequences and a corresponding dict of gap data.

Parameters

Name Type Description Default
seqs Mapping[str, str | bytes | NumpyIntArrayType] dict of ungapped sequences {name: seq, …} required
gaps Mapping[str, NumpyIntArrayType] gap data {name: [[seq gap position, cumulative gap length], …], …} required
alphabet c3_alphabet.CharAlphabet[Any] alphabet object for the sequences required

get_gapped_seq_array

core.seq_storage.AlignedSeqsData.get_gapped_seq_array(
    seqid,
    start=None,
    stop=None,
    step=None,
)

Return sequence data corresponding to seqid as an array of indices. start/stop are in alignment coordinates. Includes gaps.

get_gapped_seq_bytes

core.seq_storage.AlignedSeqsData.get_gapped_seq_bytes(
    seqid,
    start=None,
    stop=None,
    step=None,
)

Return sequence corresponding to seqid as a bytes string. start/stop are in alignment coordinates. Includes gaps.

get_gapped_seq_str

core.seq_storage.AlignedSeqsData.get_gapped_seq_str(
    seqid,
    start=None,
    stop=None,
    step=None,
)

Return sequence corresponding to seqid as a string. start/stop are in alignment coordinates. Includes gaps.

get_gaps

core.seq_storage.AlignedSeqsData.get_gaps(seqid)

returns the gap data for seqid

get_hash

core.seq_storage.AlignedSeqsData.get_hash(seqid)

returns hash of seqid

get_pos_range

core.seq_storage.AlignedSeqsData.get_pos_range(
    names,
    start=None,
    stop=None,
    step=None,
)

returns an array of the selected positions for names.

get_positions

core.seq_storage.AlignedSeqsData.get_positions(names, positions)

returns alignment positions for names

Parameters

Name Type Description Default
names PySeq[str] series of sequence names required
positions PySeq[int] | NumpyIntArrayType indices lying within self required

Returns

Name Type Description
2D numpy.array, oriented by sequence

Raises

Name Type Description
IndexError if a provided position is negative or
greater then alignment length.

get_seq_array

core.seq_storage.AlignedSeqsData.get_seq_array(
    seqid,
    start=None,
    stop=None,
    step=None,
)

Return ungapped sequence corresponding to seqid as an array of indices.

Notes

Assumes start/stop are in sequence coordinates. If seqid is in reversed_seqs, that sequence will be in plus strand orientation. It is client codes responsibility to ensure the coordinates are consistent with that.

get_seq_bytes

core.seq_storage.AlignedSeqsData.get_seq_bytes(
    seqid,
    start=None,
    stop=None,
    step=None,
)

Return ungapped sequence corresponding to seqid as a bytes string. start/stop are in sequence coordinates. Excludes gaps.

get_seq_length

core.seq_storage.AlignedSeqsData.get_seq_length(seqid)

return length of the unaligned seq for seqid

get_seq_str

core.seq_storage.AlignedSeqsData.get_seq_str(
    seqid,
    start=None,
    stop=None,
    step=None,
)

Return ungapped sequence corresponding to seqid as a string. start/stop are in sequence coordinates. Excludes gaps.

get_view

core.seq_storage.AlignedSeqsData.get_view(seqid, slice_record=None)

reurns view of aligned sequence data for seqid

Parameters

Name Type Description Default
seqid str | int sequence name required
slice_record SliceRecord | None slice record to use for slicing the data. If None, uses the default slice record for the entire sequence. None

to_alphabet

core.seq_storage.AlignedSeqsData.to_alphabet(alphabet, check_valid=True)

Returns a new AlignedSeqsData object with the same underlying data with a new alphabet.

variable_positions

core.seq_storage.AlignedSeqsData.variable_positions(
    names,
    start=None,
    stop=None,
    step=None,
)

returns absolute indices of positions that have more than one state

Parameters

Name Type Description Default
names PySeq[str] selected seqids required
start int | None absolute start None
stop int | None absolute stop None
step int | None step None

Returns

Name Type Description
Absolute indices (as distinct from an index relative to start) of
variable positions.