ScoringMatrix#

class scoring_matrices.ScoringMatrix#

A scoring matrix to use for biological sequence alignments.

const float *data_ptr()#

Get a pointer to the scoring matrix as a C-contiguous array.

const float **matrix_ptr()#

Get a pointer to the scoring matrix as an array of pointers to matrix rows.

const char *alphabet_ptr()#

Get a pointer to the scoring matrix alphabet as a C-string.

size_t size()#

Get the size of the scoring matrix.

__init__(matrix, alphabet='ARNDCQEGHILKMFPSTWYVBZX', name=None)#

Create a new scoring matrix.

Parameters:
  • matrix (array-like of float) – A square matrix with dimensions equal to the alphabet length, storing the scores for each pair of characters.

  • alphabet (str) – The alphabet used to index the rows and columns of the scoring matrix.

  • name (str or None) – The name of the scoring matrix, if any.

Example

>>> matrix = ScoringMatrix(
...     [[91, -114,  -31, -123],
...      [-114, 100, -125, -31],
...      [-31, -125,  100, -114],
...      [-123, -31, -114,  91]],
...     alphabet="ACGT",
...     name="HOXD70",
... )
Raises:
  • ValueError – When the matrix is not a valid matrix or does not match the given alphabet.

  • MemoryError – When memory for storing the scores could not be allocated successfully.

__len__()#

Return len(self).

__getitem__(key, /)#

Return self[key].

__copy__()#
__eq__(value, /)#

Return self==value.

__reduce_ex__(protocol)#

Helper for pickle.

copy()#

Get a copy of the matrix.

classmethod from_diagonal(diagonal, mismatch_score=0.0, alphabet='ARNDCQEGHILKMFPSTWYVBZX*', name=None)#

Create a scoring matrix from a diagonal vector.

Parameters:
  • diagonal (sequence of float) – The diagonal of the scoring matrix, used to score character matches.

  • mismatch_score (float) – The mismatch score to use for every mismatches.

  • alphabet (str) – The alphabet to use with the scoring matrix.

  • name (str or None) – A name for the scoring matrix, if any.

Example

>>> matrix = ScoringMatrix.from_diagonal(
...     diagonal=[2, 2, 3, 3],
...     mismatch_score=-3.0,
...     alphabet="ATGC",
... )
>>> for row in matrix:
...     print(row)
[2.0, -3.0, -3.0, -3.0]
[-3.0, 2.0, -3.0, -3.0]
[-3.0, -3.0, 3.0, -3.0]
[-3.0, -3.0, -3.0, 3.0]

Added in version 0.2.0.

classmethod from_file(file, name=None)#

Load a scoring matrix from a file-like object.

classmethod from_match_mismatch(match_score=1.0, mismatch_score=-1.0, alphabet='ARNDCQEGHILKMFPSTWYVBZX*', name=None)#

Create a scoring matrix from two match/mismatch scores.

Added in version 0.2.0.

classmethod from_name(name='BLOSUM62')#

Load a built-in scoring matrix by name.

Parameters:

name (str) – The name of the scoring matrix.

Example

>>> blosum62 = ScoringMatrix.from_name("BLOSUM62")
Raises:

ValueError – When no scoring matrix with the given name can be found in the embedded matrix data.

classmethod from_str(text, name=None)#

Load a scoring matrix from a string.

is_integer()#

Test whether the scoring matrix is an integer matrix.

Returns:

boolTrue if the matrix only contains integer scores.

Example

>>> blosum62 = ScoringMatrix.from_name("BLOSUM62")
>>> blosum62.is_integer()
True
>>> benner6 = ScoringMatrix.from_name("BENNER6")
>>> benner6.is_integer()
False
is_symmetric()#

Test whether the scoring matrix is symmetric.

Returns:

boolTrue if the matrix is a symmetric matrix.

Added in version 0.2.0.

max()#

Get the maximum score of the scoring matrix.

Example

>>> blosum62 = ScoringMatrix.from_name("BLOSUM62")
>>> blosum62.max()
11.0
min()#

Get the minimum score of the scoring matrix.

Example

>>> blosum62 = ScoringMatrix.from_name("BLOSUM62")
>>> blosum62.min()
-4.0
shuffle(alphabet)#

Shuffle the matrix using the new given alphabet.

The matrix name is retained only when the provided alphabet is a permutation of the current alphabet, e.g. there is no loss of data.

Parameters:

alphabet (str) – The new alphabet to use for the columns. It must be a subset of self.alphabet.

Raises:

KeyError – When some required alphabet letters are missing from the source matrix alphabet.

Example

>>> m1 = ScoringMatrix.from_name("BLOSUM62")
>>> m1[1, 1]
5.0
>>> m1['R', 'R']
5.0
>>> m2 = m1.shuffle("ABCDEFGHIKLMNPQRSTVWXYZ*")
>>> m2[1, 1]
4.0
>>> m2['R', 'R']
5.0