LetterAlphabet#

class biotite.sequence.LetterAlphabet(symbols: Iterable[str | bytes] | str | bytes)[source]#

Bases: Alphabet[str]

LetterAlphabet is a an Alphabet subclass specialized for letter based alphabets, like DNA or protein sequence alphabets. The alphabet size is limited to the 94 printable, non-whitespace characters. Internally the symbols are saved as bytes objects. The encoding and decoding process is a lot faster than for a normal Alphabet.

The performance gain comes through the use of NumPy and Cython for encoding and decoding, without the need of a dictionary.

Parameters:
symbolsiterable object or str or bytes

The symbols, that are allowed in this alphabet. The corresponding code for a symbol, is the index of that symbol in this list.

decode(code: int, as_bytes: bool = False) str#

Use the alphabet to decode a symbol code.

Parameters:
codeint

The symbol code to be decoded.

Returns:
symbolobject

The symbol corresponding to code.

Raises:
AlphabetError

If code is not a valid code in the alphabet.

decode_into_byte(code: int) bytes#

Decode a code into a single-byte symbol.

Parameters:
codeint

The code to decode.

Returns:
symbolbytes

The decoded symbol as a single-byte object.

decode_multiple(code: ndarray[tuple[K], dtype[integer]] | Iterable[int], as_bytes: bool = False) ndarray[tuple[K], dtype[character]]#

Decode a sequence code into a list of symbols.

Parameters:
codendarray, dtype=uint8

The sequence code to decode. Works fastest if a ndarray is provided.

as_bytesbool, optional

DEPRECATED: Use decode_multiple_into_bytes() instead. If true, the output array will contain bytes (dtype ‘S1’). Otherwise, the the output array will contain str (dtype ‘U1’).

Returns:
symbolsndarray, dtype=’U1’ or dtype=’S1’

The decoded list of symbols.

decode_multiple_into_bytes(code: ndarray[tuple[K], dtype[integer]] | Iterable[int]) ndarray[tuple[K], dtype[bytes_]]#

Decode a sequence code into a single-byte symbol array.

This is a faster alternative to decode_multiple() when single-byte symbols are desired, as it avoids the conversion to Unicode characters.

Parameters:
codendarray, dtype=uint8

The sequence code to decode. Works fastest if a ndarray is provided.

Returns:
symbolsndarray, dtype=’S1’

The decoded list of symbols.

encode(symbol: str | bytes) int#

Use the alphabet to encode a symbol.

Parameters:
symbolobject

The object to encode into a symbol code.

Returns:
codeint

The symbol code of symbol.

Raises:
AlphabetError

If symbol is not in the alphabet.

encode_multiple(symbols: Iterable[str | bytes] | str | bytes | ndarray[tuple[K], dtype[character]] | ndarray[tuple[K], dtype[unsignedinteger]], dtype: Any = None) ndarray[tuple[K], dtype[integer]]#

Encode multiple symbols.

Parameters:
symbolsiterable object or str or bytes

The symbols to encode. The method is fastest when a ndarray, str or bytes object containing the symbols is provided, instead of e.g. a list.

dtypedtype, optional

For compatibility with superclass. The value is ignored.

Returns:
codendarray

The sequence code.

extends(alphabet: Alphabet[Any]) bool#

Check, if this alphabet extends another alphabet.

Parameters:
alphabetAlphabet

The potential parent alphabet.

Returns:
resultbool

True, if this object extends alphabet, false otherwise.

get_symbols() tuple[str, ...]#

Get the symbols in the alphabet.

Returns:
symbolstuple

The symbols.

is_letter_alphabet() bool#

Check whether the symbols in this alphabet are single printable letters. If so, the alphabet could be expressed by a LetterAlphabet.

Returns:
is_letter_alphabetbool

True, if all symbols in the alphabet are ‘str’ or ‘bytes’, have length 1 and are printable.