core.alphabet.KmerAlphabet

core.alphabet.KmerAlphabet(words, monomers, k, gap=None, missing=None)

k-mer alphabet represents complete non-monomer alphabets

Notes

Differs from SenseCodonAlphabet case by representing all possible permutations of k-length of the provided monomer alphabet. More efficient mapping between integers and k-length strings

Methods

Name Description
from_index decodes an integer into a k-mer
from_indices converts array of k-mer indices into an array of monomer indices
from_rich_dict returns an instance from a serialised dictionary
is_valid seq is valid for alphabet
to_index encodes a k-mer as a single integer
to_indices returns a sequence of k-mer indices
to_json returns a serialisable string
to_rich_dict returns a serialisable dictionary
with_gap_motif returns a new KmerAlphabet with the gap motif added

from_index

core.alphabet.KmerAlphabet.from_index(kmer_index)

decodes an integer into a k-mer

from_indices

core.alphabet.KmerAlphabet.from_indices(kmer_indices, independent_kmer=True)

converts array of k-mer indices into an array of monomer indices

Parameters

Name Type Description Default
kmer_indices NumpyIntArrayType a sequence of k-mer indices required
independent_kmer bool whether the k-mers are overlapping or not description True

from_rich_dict

core.alphabet.KmerAlphabet.from_rich_dict(data)

returns an instance from a serialised dictionary

is_valid

core.alphabet.KmerAlphabet.is_valid(seq)

seq is valid for alphabet

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType a numpy array of integers required

Notes

This will raise a TypeError for string or bytes. Using to_indices() to convert those ensures a valid result.

to_index

core.alphabet.KmerAlphabet.to_index(seq, validate=True)

encodes a k-mer as a single integer

Parameters

Name Type Description Default
seq str | bytes | NumpyIntArrayType sequence to be encoded, can be either a string or numpy array required
overlapping if False, performs operation on sequential k-mers, e.g. codons required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True

Notes

If self.gap_char is defined, then the following rules apply: returns num_statesk if a k-mer contains a gap character, otherwise returns num_statesk + 1 if a k-mer contains a non-canonical character. If self.gap_char is not defined, returns num_states**k for both cases.

to_indices

core.alphabet.KmerAlphabet.to_indices(seq, validate=True, independent_kmer=True)

returns a sequence of k-mer indices

Parameters

Name Type Description Default
seq str | bytes | tuple[str | bytes, …] | list[str | bytes] | NumpyIntArrayType a sequence of monomers required
validate bool raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() True
independent_kmer bool if True, returns non-overlapping k-mers True

Notes

If self.gap_char is not None, then the following rules apply: If a sequence k-mer contains a gap character it is assigned an index of (num. monomer statesk). If a k-mer contains a non-canonical and non-gap character, it is assigned an index of (num. monomer statesk) + 1. If self.gap_char is None, then both of the above cases are defined as (num. monomer states**k).

to_json

core.alphabet.KmerAlphabet.to_json()

returns a serialisable string

to_rich_dict

core.alphabet.KmerAlphabet.to_rich_dict(for_pickle=False)

returns a serialisable dictionary

with_gap_motif

core.alphabet.KmerAlphabet.with_gap_motif(include_missing=False, **kwargs)

returns a new KmerAlphabet with the gap motif added

Notes

Adds gap state to monomers and recreates k-mer alphabet for self

kwargs is for compatibility with the CharAlphabet method