class biotite.sequence.Sequence(sequence=())[source]

Bases: biotite.copyable.Copyable

The abstract base class for all sequence types.

A Sequence can be seen as a succession of symbols, that are elements in the allowed set of symbols, the Alphabet. Internally, a Sequence object uses a NumPy ndarray of integers, where each integer represents a symbol. The Alphabet of a Sequence object is used to encode each symbol, that is used to create the Sequence, into an integer. These integer values are called symbol code, the encoding of an entire sequence of symbols is called sequence code.

The size of the symbol code type in the array is determined by the size of the Alphabet: If the Alphabet contains 256 symbols or less, one byte is used per array element; if the Alphabet contains between 257 and 65536 symbols, two bytes are used, and so on.

Two Sequence objects are equal if they are instances of the same class, have the same Alphabet and have equal sequence codes. Comparison with a string or list of symbols evaluates always to false.

A Sequence can be indexed by any 1-D index a ndarray accepts. If the index is a single integer, the decoded symbol at that position is returned, otherwise a subsequence is returned.

Individual symbols of the sequence can also be exchanged in indexed form: If the an integer is used as index, the item is treated as a symbol. Any other index (slice, index list, boolean mask) expects multiple symbols, either as list of symbols, as ndarray containing a sequence code or another Sequence instance. Concatenation of two sequences is achieved with the ‘+’ operator.

Each subclass of Sequence needs to overwrite the abstract method get_alphabet(), which specifies the alphabet the Sequence uses.

sequence : iterable object, optional

The symbol sequence, the Sequence is initialized with. For alphabets containing single letter strings, this parameter may also be a str object. By default the sequence is empty.


Creating a DNA sequence from string and print the symbols and the code:

>>> dna_seq = NucleotideSequence("ACGTA")
>>> print(dna_seq)
>>> print(dna_seq.code)
[0 1 2 3 0]
>>> print(dna_seq.symbols)
['A' 'C' 'G' 'T' 'A']
>>> print(list(dna_seq))
['A', 'C', 'G', 'T', 'A']

Sequence indexing:

>>> print(dna_seq[1:3])
>>> print(dna_seq[[0,2,4]])
>>> print(dna_seq[np.array([False,False,True,True,True])])

Sequence manipulation:

>>> dna_copy = dna_seq.copy()
>>> dna_copy[2] = "C"
>>> print(dna_copy)
>>> dna_copy = dna_seq.copy()
>>> dna_copy[0:2] = dna_copy[3:5]
>>> print(dna_copy)
>>> dna_copy = dna_seq.copy()
>>> dna_copy[np.array([True,False,False,False,True])] = "T"
>>> print(dna_copy)
>>> dna_copy = dna_seq.copy()
>>> dna_copy[1:4] = np.array([0,1,2])
>>> print(dna_copy)

Reverse sequence:

>>> dna_seq_rev = dna_seq.reverse()
>>> print(dna_seq_rev)

Concatenate the two sequences:

>>> dna_seq_concat = dna_seq + dna_seq_rev
>>> print(dna_seq_concat)
code : ndarray

The sequence code of this Sequence

symbols : list

The list of symbols, represented by the Sequence. The list is generated by decoding the sequence code, when this attribute is accessed. When this attribute is modified, the new list of symbols is encoded into the sequence code.


Copy the object.

new_seq_code : ndarray, optional

If this parameter is set, the sequence code is set to this value, rather than the original sequence code.


A copy of this object.


Get the Alphabet of the Sequence.

This method must be overwritten, when subclassing Sequence.

alphabet : Alphabet

Sequence alphabet.


Reverse the Sequence.

reversed : Sequence

The reversed Sequence.


>>> dna_seq = NucleotideSequence("ACGTA")
>>> dna_seq_rev = dna_seq.reverse()
>>> print(dna_seq_rev)

Check, if the sequence contains a valid sequence code.

A sequence code is valid, if at each sequence position the code is smaller than the size of the alphabet.

Invalid code means that the code cannot be decoded into symbols. Furthermore invalid code can lead to serious errors in alignments, since the substitution matrix is indexed with an invalid index.

valid : bool

True, if the sequence is valid, false otherwise.


Get the number of occurences of each symbol in the sequence.

If a symbol does not occur in the sequence, but it is in the alphabet, its number of occurences is 0.

frequency : dict

A dictionary containing the symbols as keys and the corresponding number of occurences in the sequence as values.