core.moltype.MolType

core.moltype.MolType(
    name,
    monomers,
    make_seq,
    gap=IUPAC_gap,
    missing=IUPAC_missing,
    complements=None,
    ambiguities=None,
    colors=None,
    pairing_rules=None,
    mw_calculator=None,
    coerce_to=None,
)

MolType handles operations that depend on the sequence type.

Notes

The only way to create sequences is via a MolType instance. The instance defines different alphabets that are used for data conversions. Create a moltype using the get_moltype() function.

Attributes

Name Description
alphabet monomers
degen_alphabet monomers + ambiguous characters
degen_gapped_alphabet monomers + gap + ambiguous characters
gapped_alphabet monomers + gap
gapped_missing_alphabet monomers + gap
is_nucleic is a nucleic acid moltype
label synonym for name

Methods

Name Description
can_match Returns True if every pos in 1st could match same pos in 2nd.
can_mispair Returns True if any position in self could mispair with other.
complement converts a string or bytes into it’s nucleic acid complement
count_degenerate returns the number of degenerate characters in a sequence
count_gaps returns the number of gap characters in a sequence
count_variants Counts number of possible sequences matching the sequence, given
degap removes all gap and missing characters from a sequence
degenerate_from_seq Returns least degenerate symbol that encompasses a set of characters
disambiguate Returns a non-degenerate sequence from a degenerate one.
get_css_style returns string of CSS classes and {character: , …}
get_degenerate_positions Return list of position indexs of degenerate characters in the sequence.
has_ambiguity whether sequence has an ambiguity character
is_ambiguity Return True if querymotif is an amibiguity character in alphabet.
is_compatible_alphabet checks that characters in alphabet are equal to a bound alphabet
is_degenerate checks if a sequence contains degenerate characters
is_gapped checks if a sequence contains gaps
is_valid checks against most degenerate alphabet
iter_alphabets yield alphabets in order of most to least degenerate
make_seq creates a Sequence object corresponding to the molecular type of
most_degen_alphabet returns the most degenerate alphabet for this instance
mw Returns the molecular weight of the sequence. If the sequence is
random_disambiguate disambiguates a sequence by randomly selecting a non-degenerate character
rc reverse reverse complement of a sequence
resolve_ambiguity Returns tuple of all possible canonical characters corresponding
strand_symmetric_motifs returns ordered pairs of strand complementary motifs
strip_bad Removes any symbols not in the alphabet.
strip_bad_and_gaps Removes any symbols not in the alphabet, and any gaps.
strip_degenerate removes degenerate characters
to_json returns result of json formatted string
to_regex returns a regex pattern with ambiguities expanded to a character set
to_rich_dict returns dict suitable for serialisation

can_match

core.moltype.MolType.can_match(first, second)

Returns True if every pos in 1st could match same pos in 2nd.

Notes

Truncates at length of shorter sequence. gaps are only allowed to match other gaps.

can_mispair

core.moltype.MolType.can_mispair(first, second)

Returns True if any position in self could mispair with other.

Notes

Pairing occurs in reverse order, i.e. last position of other with first position of self, etc.

Truncates at length of shorter sequence.

Gaps are always counted as possible mispairs, as are weak pairs like GU.

complement

core.moltype.MolType.complement(seq, validate=True)

converts a string or bytes into it’s nucleic acid complement

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType sequence to be complemented required
validate bool if True, checks the sequence is validated against the most degenerate alphabet True

count_degenerate

core.moltype.MolType.count_degenerate(seq, validate=True)

returns the number of degenerate characters in a sequence

Parameters

Name Type Description Default
seq NumpyIntArrayType | str | bytes sequence to degapped required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Raises

Name Type Description
TypeError if seq is not a supported type
c3_alphabet.AlphabetError if invalid characters present

count_gaps

core.moltype.MolType.count_gaps(seq)

returns the number of gap characters in a sequence

count_variants

core.moltype.MolType.count_variants(seq)

Counts number of possible sequences matching the sequence, given any ambiguous characters in the sequence.

Notes

Uses self.ambiguitues to decide how many possibilities there are at each position in the sequence and calculates the permutations.

degap

core.moltype.MolType.degap(seq, validate=True)

removes all gap and missing characters from a sequence

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType sequence to degapped required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Raises

Name Type Description
TypeError if seq is not a supported type
AlphabetError if invalid characters present

degenerate_from_seq

core.moltype.MolType.degenerate_from_seq(seq)

Returns least degenerate symbol that encompasses a set of characters

disambiguate

core.moltype.MolType.disambiguate(seq, method='strip')

Returns a non-degenerate sequence from a degenerate one.

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType the sequence to be disambiguated required
method str how to disambiguate the sequence, one of “strip”, “random” strip: removes degenerate characters random: randomly selects a non-degenerate character 'strip'

get_css_style

core.moltype.MolType.get_css_style(
    colors=None,
    font_size=12,
    font_family='Lucida Console',
)

returns string of CSS classes and {character: , …}

Parameters

Name Type Description Default
colors Mapping[str, str] | None A dictionary mapping characters to CSS color values. None
font_size int Font size in points. 12
font_family str Name of a monospace font. 'Lucida Console'

get_degenerate_positions

core.moltype.MolType.get_degenerate_positions(
    seq,
    include_gap=True,
    validate=True,
)

Return list of position indexs of degenerate characters in the sequence.

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType the sequence to be used for getting degenerate positions required
include_gap bool if True, then the gap state together with ‘canonical’ sates (A,C,G,T for DNA) will be considered non-ambiguous. True
validate bool if True, checks the sequence is validated for the alphabet True

has_ambiguity

core.moltype.MolType.has_ambiguity(seq, validate=True)

whether sequence has an ambiguity character

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType input sequence to be converted to a numpy array of uint required
validate bool raises an AlphabetError if the resulting sequence does not satisfy alphabet.is_valid() True

is_ambiguity

core.moltype.MolType.is_ambiguity(query_motif, validate=True)

Return True if querymotif is an amibiguity character in alphabet.

Parameters

Name Type Description Default
query_motif str the motif being queried. required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Raises

Name Type Description
AlphabetError if invalid characters present

is_compatible_alphabet

core.moltype.MolType.is_compatible_alphabet(alphabet, strict=True)

checks that characters in alphabet are equal to a bound alphabet

Parameters

Name Type Description Default
alphabet c3_alphabet.CharAlphabet[Any] an Alphabet instance required
strict bool the order of elements must match True

is_degenerate

core.moltype.MolType.is_degenerate(seq, validate=True)

checks if a sequence contains degenerate characters

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType sequence to be evaluated required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Raises

Name Type Description
TypeError if seq is not a supported type
AlphabetError if invalid characters present

is_gapped

core.moltype.MolType.is_gapped(seq, validate=True)

checks if a sequence contains gaps

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType sequence to be evaluated required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Raises

Name Type Description
TypeError if seq is not a supported type
AlphabetError if invalid characters present

is_valid

core.moltype.MolType.is_valid(seq)

checks against most degenerate alphabet

iter_alphabets

core.moltype.MolType.iter_alphabets()

yield alphabets in order of most to least degenerate

make_seq

core.moltype.MolType.make_seq(seq, name=None, check_seq=True, **kwargs)

creates a Sequence object corresponding to the molecular type of this instance.

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType | c3_sequence.Sequence | SeqViewABC the raw sequence data required
name str | None the name of the sequence None
check_seq bool whether to validate the sequence data against the molecular type if True, performs validation checks; set to False if the sequence data is already known to be valid. True
**kwargs Any additional keyword arguments that may be required for creating the Sequence object {}

Notes

If seq is a string, and the moltype has a coerce_to attribute, the string will be converted via that callable into a character set compatible with the moltype. Only applies to nucleic acid moltypes.

most_degen_alphabet

core.moltype.MolType.most_degen_alphabet()

returns the most degenerate alphabet for this instance

mw

core.moltype.MolType.mw(seq, method='random', delta=None)

Returns the molecular weight of the sequence. If the sequence is ambiguous, uses method to disambiguate the sequence.

Parameters

Name Type Description Default
seq str the sequence whose molecular weight is to be calculated. required
method str the method provided to .disambiguate() to disambiguate the sequence. either “random” (a random choice of states encoded by the ambiguity) or “strip” (delete positions with ambiguous characters). 'random'
delta float | None if delta is present, uses it instead of the standard weight adjustment. None

random_disambiguate

core.moltype.MolType.random_disambiguate(seq)

disambiguates a sequence by randomly selecting a non-degenerate character

rc

core.moltype.MolType.rc(seq, validate=True)

reverse reverse complement of a sequence

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType sequence to be reversed required
validate bool if True, checks the sequence is validated against the most degenerate alphabet True

Raises

Name Type Description
AlphabetError if invalid characters present

resolve_ambiguity

core.moltype.MolType.resolve_ambiguity(
    ambig_motif,
    alphabet=None,
    allow_gap=False,
    validate=True,
)

Returns tuple of all possible canonical characters corresponding to ambig_motif

Parameters

Name Type Description Default
ambig_motif str the string to be expanded required
alphabet c3_alphabet.CharAlphabet[str] | c3_alphabet.KmerAlphabet[str] | None optional, disambiguated motifs not present in alphabet will be excluded. This could be a codon alphabet where stop codons are not present. None
allow_gap bool whether the gap character is allowed in output. Only applied when alphabet is None. False
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Raises

Name Type Description
AlphabetError if invalid characters present

Notes

If ambig_motif is > 1 character long and alphabet is None, we construct a word alphabet with the same length.

strand_symmetric_motifs

core.moltype.MolType.strand_symmetric_motifs(motif_length=1)

returns ordered pairs of strand complementary motifs

Parameters

Name Type Description Default
motif_length int size of motifs 1

Raises

Name Type Description
TypeError description

strip_bad

core.moltype.MolType.strip_bad(seq)

Removes any symbols not in the alphabet.

strip_bad_and_gaps

core.moltype.MolType.strip_bad_and_gaps(seq)

Removes any symbols not in the alphabet, and any gaps. Since missing could be a gap, it is also removed.

strip_degenerate

core.moltype.MolType.strip_degenerate(seq)

removes degenerate characters

to_json

core.moltype.MolType.to_json()

returns result of json formatted string

to_regex

core.moltype.MolType.to_regex(seq)

returns a regex pattern with ambiguities expanded to a character set

to_rich_dict

core.moltype.MolType.to_rich_dict(**kwargs)

returns dict suitable for serialisation