SubstitutionMatrix#
- class biotite.sequence.align.SubstitutionMatrix(alphabet1: Alphabet[S1], alphabet2: Alphabet[S2], score_matrix: ndarray[tuple[M, N], dtype[integer]] | dict[tuple[S1, S2], int] | str)[source]#
Bases:
Generic[S1,S2]A
SubstitutionMatrixis the foundation for scoring in sequence alignments. ASubstitutionMatrixmaps each possible pairing of a symbol of a first alphabet with a symbol of a second alphabet to a score (integer).The class uses a 2-D (m x n)
ndarray(dtype=:attr:numpy.int32), where each element stores the score for a symbol pairing, indexed by the symbol codes of the respective symbols in an m-length alphabet 1 and an n-length alphabet 2.There are 3 ways to creates instances:
At first a 2-D
ndarraycontaining the scores can be directly provided.Secondly a dictionary can be provided, where the keys are pairing tuples and values are the corresponding scores. The pairing tuples consist of a symbol of alphabet 1 as first element and a symbol of alphabet 2 as second element. Parings have to be provided for each possible combination.
At last a valid matrix name can be given, which is loaded from the internal matrix database. The following matrices are available:
Name
Alphabet
Description
Ref.
NUC
Nucleotide
NCBI NUC.4.4 matrix supporting ambiguous nucleotide symbols
PAM<n>
Protein
Point accepted mutation matrices
BLOSUM<n>
Protein
BLOCKS substitution matrices for the available clustering thresholds; BLOSUMN is also available
MATCH
Protein
Simple matrix assigning 1 to matches and -1 to mismatches
IDENTITY
Protein
Identity matrix assigning 1 to matches and -10000 to mismatches
GONNET
Protein
Gonnet PAM250 matrix
DAYHOFF
Protein
Original Dayhoff PAM250 matrix
VTML<n>
Protein
Variable Time Maximum Likelihood matrices for the available PAM distances
BLOSUM<n>_<BLOCKS>
Protein
Matrices computed with the original BLOSUM implementation from BLOCKS database version
<BLOCKS>RBLOSUM<n>_<BLOCKS>
Protein
Matrices computed with the first correction to the BLOSUM implementation from BLOCKS database version
<BLOCKS>CorBLOSUM<n>_<BLOCKS>
Protein
Matrices computed with the further corrected BLOSUM implementation from BLOCKS database version
<BLOCKS>3Di
3Di
Foldseek substitution matrix for the 3Di structural alphabet
PB
PB
PBxplore substitution matrix for the Protein Blocks alphabet
CLESUM
CLePAPS
Conformation letter substitution matrix for the CLePAPS alphabet
A list of all available matrix names is returned by
list_db(). Scores for ambiguous amino acids that are missing in the original source of a matrix are derived from the scores of the respective unambiguous amino acids.Since this class can handle two different alphabets, it is possible to align two different types of sequences.
Objects of this class are immutable.
- Parameters:
- alphabet1Alphabet, length=m
The first alphabet of the substitution matrix.
- alphabet2Alphabet, length=n
The second alphabet of the substitution matrix.
- score_matrixndarray, shape=(m,n) or dict or str
Either a symbol code indexed
ndarraycontaining the scores, or a dictionary mapping the symbol pairing to scores, or a string referencing a matrix in the internal database.
- Attributes:
shapetupleGet the shape (i.e.
- Raises:
- KeyError
If the matrix dictionary misses a symbol given in the alphabet.
Notes
The type annotation of this class is generic on the types of the symbols its alphabets use.
References
Examples
Creating a matrix for two different (nonsense) alphabets via a matrix dictionary:
>>> alph1 = Alphabet(["foo","bar"]) >>> alph2 = Alphabet([1,2,3]) >>> matrix_dict = {("foo",1):5, ("foo",2):10, ("foo",3):15, ... ("bar",1):42, ("bar",2):42, ("bar",3):42} >>> matrix = SubstitutionMatrix(alph1, alph2, matrix_dict) >>> print(matrix.score_matrix()) [[ 5 10 15] [42 42 42]] >>> print(matrix.get_score("foo", 2)) 10 >>> print(matrix.get_score_by_code(0, 1)) 10
Creating an identity substitution matrix via the score matrix:
>>> alph = NucleotideSequence.alphabet_unambiguous >>> matrix = SubstitutionMatrix(alph, alph, np.identity(len(alph), dtype=int)) >>> print(matrix) A C G T A 1 0 0 0 C 0 1 0 0 G 0 0 1 0 T 0 0 0 1
Creating a matrix via database name:
>>> alph = ProteinSequence.alphabet_ambiguous >>> matrix = SubstitutionMatrix(alph, alph, "BLOSUM50")
- as_positional(sequence1: Sequence[Any], sequence2: Sequence[Any]) tuple[SubstitutionMatrix[Any, Any], PositionalSequence, PositionalSequence]#
Transform this substitution matrix and two sequences into positional equivalents.
This means the new substitution matrix is position-specific: It has the lengths of the sequences instead of the lengths of their alphabets. Its scores represent the same scores as the original matrix, but now mapped onto the positions of the sequences.
- Parameters:
- sequence1, sequence2seq.Sequence, length=n
The sequences to create the positional equivalents from.
- Returns:
- pos_matrixalign.SubstitutionMatrix, shape=(n, n)
The position-specific substitution matrix.
- pos_sequence1, pos_sequence2PositionalSequence, length=n
The positional sequences.
Notes
After the transformation the substitution scores remain the same, i.e. substitution_matrix.get_score(sequence1[i], sequence2[j]) is equal to pos_matrix.get_score(pos_sequence1[i], pos_sequence2[j]).
Examples
Run an alignment with the usual substitution matrix:
>>> seq1 = ProteinSequence("BIQTITE") >>> seq2 = ProteinSequence("IQLITE") >>> matrix = SubstitutionMatrix.std_protein_matrix() >>> print(matrix) A C D E F G H I K L M N P Q R S T V W Y B Z J X * A 4 0 -2 -1 -2 0 -2 -1 -1 -1 -1 -2 -1 -1 -1 1 0 0 -3 -2 -2 -1 -1 0 -4 C 0 9 -3 -4 -2 -3 -3 -1 -3 -1 -1 -3 -3 -3 -3 -1 -1 -1 -2 -2 -3 -3 -1 -2 -4 D -2 -3 6 2 -3 -1 -1 -3 -1 -4 -3 1 -1 0 -2 0 -1 -3 -4 -3 4 1 -3 -1 -4 E -1 -4 2 5 -3 -2 0 -3 1 -3 -2 0 -1 2 0 0 -1 -2 -3 -2 1 4 -3 -1 -4 F -2 -2 -3 -3 6 -3 -1 0 -3 0 0 -3 -4 -3 -3 -2 -2 -1 1 3 -3 -3 0 -1 -4 G 0 -3 -1 -2 -3 6 -2 -4 -2 -4 -3 0 -2 -2 -2 0 -2 -3 -2 -3 -1 -2 -4 -1 -4 H -2 -3 -1 0 -1 -2 8 -3 -1 -3 -2 1 -2 0 0 -1 -2 -3 -2 2 0 0 -3 -1 -4 I -1 -1 -3 -3 0 -4 -3 4 -3 2 1 -3 -3 -3 -3 -2 -1 3 -3 -1 -3 -3 3 -1 -4 K -1 -3 -1 1 -3 -2 -1 -3 5 -2 -1 0 -1 1 2 0 -1 -2 -3 -2 0 1 -2 -1 -4 L -1 -1 -4 -3 0 -4 -3 2 -2 4 2 -3 -3 -2 -2 -2 -1 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4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 -4 1 >>> alignment = align_optimal(seq1, seq2, matrix, gap_penalty=-10)[0] >>> print(alignment) BIQTITE -IQLITE
Running the alignment with positional equivalents gives the same result:
>>> pos_matrix, pos_seq1, pos_seq2 = matrix.as_positional(seq1, seq2) >>> print(pos_matrix) I Q L I T E B -3 0 -4 -3 -1 1 I 4 -3 2 4 -1 -3 Q -3 5 -2 -3 -1 2 T -1 -1 -1 -1 5 -1 I 4 -3 2 4 -1 -3 T -1 -1 -1 -1 5 -1 E -3 2 -3 -3 -1 5 >>> pos_alignment = align_optimal(pos_seq1, pos_seq2, pos_matrix, gap_penalty=-10)[0] >>> print(pos_alignment) BIQTITE -IQLITE
Increase the substitution score for the first symbols in both sequences to align to each other:
>>> score_matrix = pos_matrix.score_matrix().copy() >>> score_matrix[0, 0] = 100 >>> biased_matrix = SubstitutionMatrix( ... pos_matrix.get_alphabet1(), pos_matrix.get_alphabet2(), score_matrix ... ) >>> print(biased_matrix) I Q L I T E B 100 0 -4 -3 -1 1 I 4 -3 2 4 -1 -3 Q -3 5 -2 -3 -1 2 T -1 -1 -1 -1 5 -1 I 4 -3 2 4 -1 -3 T -1 -1 -1 -1 5 -1 E -3 2 -3 -3 -1 5 >>> biased_alignment = align_optimal(pos_seq1, pos_seq2, biased_matrix, gap_penalty=-10)[0] >>> print(biased_alignment) BIQTITE I-QLITE
- static dict_from_db(matrix_name: str) dict[tuple[str, str], int]#
Create a matrix dictionary from a valid matrix name in the internal matrix database.
The keys of the dictionary consist of tuples containing the aligned symbols and the values are the corresponding scores.
- Parameters:
- matrix_namestr
The name of the matrix in the internal database.
- Returns:
- matrix_dictdict
A dictionary representing the substitution matrix.
- static dict_from_str(string: str) dict[tuple[str, str], int]#
Create a matrix dictionary from a string in NCBI matrix format.
Symbols of the first alphabet are taken from the left column, symbols of the second alphabet are taken from the top row.
The keys of the dictionary consist of tuples containing the aligned symbols and the values are the corresponding scores.
- Parameters:
- stringstr
The string containing the substitution matrix in NCBI format.
- Returns:
- matrix_dictdict
A dictionary representing the substitution matrix.
- get_alphabet1() Alphabet[S1]#
Get the first alphabet.
- Returns:
- alphabetAlphabet
The first alphabet.
- get_alphabet2() Alphabet[S2]#
Get the second alphabet.
- Returns:
- alphabetAlphabet
The second alphabet.
- get_score(symbol1: S1, symbol2: S2) int#
Get the substitution score of two symbols.
- Parameters:
- symbol1, symbol2object
Symbols to be aligned.
- Returns:
- scoreint
The substitution / alignment score.
- get_score_by_code(code1: int, code2: int) int#
Get the substitution score of two symbols, represented by their code.
- Parameters:
- code1, code2int
Symbol codes of the two symbols to be aligned.
- Returns:
- scoreint
The substitution / alignment score.
- is_symmetric() bool#
Check whether the substitution matrix is symmetric, i.e. both alphabets are identical and the score matrix is symmetric.
- Returns:
- is_symmetricbool
True, if both alphabets are identical and the score matrix is symmetric, false otherwise.
- static list_db() list[str]#
List all matrix names in the internal database.
- Returns:
- db_listlist
List of matrix names in the internal database.
- score_matrix() ndarray[tuple[M, N], dtype[int32]]#
Get the 2-D
ndarraycontaining the score values.- Returns:
- matrixndarray, shape=(m,n), dtype=np.int32
The symbol code indexed score matrix. The array is read-only.
- static std_3di_matrix() SubstitutionMatrix[str, str]#
Get the default
SubstitutionMatrixfor 3Di sequence alignments. [6]- Returns:
- matrixSubstitutionMatrix
Default matrix.
- static std_clepaps_matrix(unknown_match: int = 200, unknown_mismatch: int = -200) SubstitutionMatrix[str, str]#
Get the CLESUM substitution matrix for CLePAPS sequence.
- Parameters:
- unknown_match, unknown_mismatchint, optional
The match and mismatch score for the unknown symbol. The default values were chosen arbitrarily, but are in the order of magnitude of the other score values.
- Returns:
- matrixSubstitutionMatrix
Default matrix.
References
- static std_nucleotide_matrix() SubstitutionMatrix[str, str]#
Get the default
SubstitutionMatrixfor DNA sequence alignments.- Returns:
- matrixSubstitutionMatrix
Default matrix.
- static std_protein_blocks_matrix(undefined_match: int = 200, undefined_mismatch: int = -200) SubstitutionMatrix[str, str]#
Get the default
SubstitutionMatrixfor Protein Blocks sequences.The matrix is adapted from PBxplore [7].
- Parameters:
- undefined_match, undefined_mismatchint, optional
The match and mismatch score for undefined symbols. The default values were chosen arbitrarily, but are in the order of magnitude of the other score values.
- Returns:
- matrixSubstitutionMatrix
Default matrix.
References
- static std_protein_matrix() SubstitutionMatrix[str, str]#
Get the default
SubstitutionMatrixfor protein sequence alignments, which is BLOSUM62.- Returns:
- matrixSubstitutionMatrix
Default matrix.
- transpose() SubstitutionMatrix[S2, S1]#
Get a copy of this instance, where the alphabets are interchanged.
- Returns:
- transposedSubstitutionMatrix
The transposed substitution matrix.
Gallery#
Customized visualization of a multiple sequence alignment
Fetching and aligning a protein from different species
Plot epitope mapping data onto protein sequence alignments
Multiple Structural alignment of orthologous proteins
Searching for structural homologs in a protein structure database