- class biotite.sequence.Sequence(sequence=())¶
The abstract base class for all sequence types.
Sequencecan be seen as a succession of symbols, that are elements in the allowed set of symbols, the
Alphabet. Internally, a
Sequenceobject uses a NumPy
ndarrayof integers, where each integer represents a symbol. The
Sequenceobject 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
Alphabetcontains 256 symbols or less, one byte is used per array element; if the
Alphabetcontains between 257 and 65536 symbols, two bytes are used, and so on.
Sequencecan be indexed by any 1-D index a
ndarrayaccepts. 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
ndarraycontaining a sequence code or another
Sequenceinstance. Concatenation of two sequences is achieved with the ‘+’ operator.
- sequenceiterable object, optional
The symbol sequence, the
Sequenceis initialized with. For alphabets containing single letter strings, this parameter may also be a :class`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) ACGTA >>> 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']
>>> print(dna_seq[1:3]) CG >>> print(dna_seq[[0,2,4]]) AGA >>> print(dna_seq[np.array([False,False,True,True,True])]) GTA
>>> dna_copy = dna_seq.copy() >>> dna_copy = "C" >>> print(dna_copy) ACCTA >>> dna_copy = dna_seq.copy() >>> dna_copy[0:2] = dna_copy[3:5] >>> print(dna_copy) TAGTA >>> dna_copy = dna_seq.copy() >>> dna_copy[np.array([True,False,False,False,True])] = "T" >>> print(dna_copy) TCGTT >>> dna_copy = dna_seq.copy() >>> dna_copy[1:4] = np.array([0,1,2]) >>> print(dna_copy) AACGA
>>> dna_seq_rev = dna_seq.reverse() >>> print(dna_seq_rev) ATGCA
Concatenate the two sequences:
>>> dna_seq_concat = dna_seq + dna_seq_rev >>> print(dna_seq_concat) ACGTAATGCA
The sequence code.
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.
The alphabet of this sequence. Cannot be set. Equal to get_alphabet().
Copy the object.
- new_seq_codendarray, 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.
- static dtype(alphabet_size)¶
Get the sequence code dtype required for the given size of the alphabet.
- abstract get_alphabet()¶
This method must be overwritten, when subclassing
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.
A dictionary containing the symbols as keys and the corresponding number of occurences in the sequence as values.
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.
True, if the sequence is valid, false otherwise.
- copybool, optional
If set to False, the code
ndarrayof the returned sequence is an array view to the sequence code of this object. In this case, manipulations on the returned sequence would also affect this object. Otherwise, the sequence code is copied.
>>> dna_seq = NucleotideSequence("ACGTA") >>> dna_seq_rev = dna_seq.reverse() >>> print(dna_seq_rev) ATGCA