core.alphabet.CharAlphabet
core.alphabet.CharAlphabet(chars, gap=None, missing=None)representing fundamental monomer character sets.
Notes
Provides methods for efficient conversion between characters and integers from fundamental types of strings, bytes and numpy arrays.
Methods
| Name | Description |
|---|---|
| array_to_bytes | returns seq as a byte string |
| as_bytes | returns self as a byte string |
| convert_seq_array_to | converts a numpy array with indices from self to other |
| from_indices | returns a string from a sequence of indices |
| from_rich_dict | returns an instance from a serialised dictionary |
| get_kmer_alphabet | returns kmer alphabet with words of size k |
| get_subset | Returns a new Alphabet object containing a subset of motifs in self. |
| is_valid | seq is valid for alphabet |
| to_indices | returns a sequence of indices for the characters in seq |
| to_json | returns a serialisable string |
| to_rich_dict | returns a serialisable dictionary |
| with_gap_motif | returns new monomer alphabet with gap and missing characters added |
array_to_bytes
core.alphabet.CharAlphabet.array_to_bytes(seq)returns seq as a byte string
as_bytes
core.alphabet.CharAlphabet.as_bytes()returns self as a byte string
convert_seq_array_to
core.alphabet.CharAlphabet.convert_seq_array_to(alphabet, seq, check_valid=True)converts a numpy array with indices from self to other
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| alphabet | CharAlphabet[Any] | alphabet to convert to | required |
| seq | NumpyIntArrayType | ndarray of uint8 integers | required |
| check_valid | bool | validates both input and out sequences are valid for self and other respectively. Validation failure raises an AlphabetError. | True |
Returns
| Name | Type | Description |
|---|---|---|
| the indices of characters in common between self and other | ||
| are swapped |
from_indices
core.alphabet.CharAlphabet.from_indices(seq)returns a string from a sequence of indices
from_rich_dict
core.alphabet.CharAlphabet.from_rich_dict(data)returns an instance from a serialised dictionary
get_kmer_alphabet
core.alphabet.CharAlphabet.get_kmer_alphabet(k, include_gap=True)returns kmer alphabet with words of size k
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| k | int | word size | required |
| include_gap | bool | if True, and self.gap_char, we set KmerAlphabet.gap_char = self.gap_char * k | True |
Notes
If self.missing_char is present, it is included in the new alphabet as missing_char * k
get_subset
core.alphabet.CharAlphabet.get_subset(motif_subset, excluded=False)Returns a new Alphabet object containing a subset of motifs in self.
Raises an exception if any of the items in the subset are not already in self.
is_valid
core.alphabet.CharAlphabet.is_valid(seq)seq is valid for alphabet
to_indices
core.alphabet.CharAlphabet.to_indices(seq, validate=True)returns a sequence of indices for the characters in seq
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| seq | str | bytes | tuple[str | bytes, …] | list[str | bytes] | NumpyIntArrayType | sequence to convert to a numpy array | required |
| validate | bool | raises an AlphabetError if the resulting sequence does not satisfy self.is_valid() | True |
to_json
core.alphabet.CharAlphabet.to_json()returns a serialisable string
to_rich_dict
core.alphabet.CharAlphabet.to_rich_dict(for_pickle=False)returns a serialisable dictionary
with_gap_motif
core.alphabet.CharAlphabet.with_gap_motif(
gap_char='-',
missing_char='?',
include_missing=False,
gap_as_state=False,
)returns new monomer alphabet with gap and missing characters added
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| gap_char | str | bytes | the IUPAC gap character “-” | '-' |
| missing_char | str | bytes | None | the IUPAC missing character “?” | '?' |
| include_missing | bool | if True, and self.missing_char, it is included in the new alphabet | False |
| gap_as_state | bool | include the gap character as a state in the alphabet, drops gap_char attribute in resulting KmerAlphabet | False |