scirpy.ir_dist.sequence_dist(seqs, seqs2=None, *, metric='identity', cutoff=None, n_jobs=None, **kwargs)

Calculate a sequence x sequence distance matrix.

Calculates the full pairwise distance matrix.


  • Distances are offset by 1 to allow efficient use of sparse matrices (\(d' = d+1\)).

  • That means, a distance > cutoff is represented as 0, a distance == 0 is represented as 1, a distance == 1 is represented as 2 and so on.

  • Only returns distances <= cutoff. Larger distances are eliminated from the sparse matrix.

  • Distances are non-negative.

When seqs or seqs2 includes non-unique values, the function internally uses only unique sequences to calculate the distances. Note that, if the input arrays contain large numbers of duplicated values (i.e. hundreds each), this will lead to large “dense” blocks in the sparse matrix. This will result in slow processing and high memory usage.

seqs : Sequence[str]

Numpy array of nucleotide or amino acid sequences. Note that not all distance metrics support nucleotide sequences.

seqs2 : Sequence[str] | NoneOptional[Sequence[str]] (default: None)

Second array sequences. When omitted, sequence_dist computes the square matrix of unique_seqs.

metric : {‘alignment’, ‘identity’, ‘levenshtein’, ‘hamming’} | DistanceCalculatorUnion[Literal[‘alignment’, ‘identity’, ‘levenshtein’, ‘hamming’], DistanceCalculator] (default: 'identity')

You can choose one of the following metrics:

cutoff : int | NoneOptional[int] (default: None)

All distances > cutoff will be replaced by 0 and eliminated from the sparse matrix. A sensible cutoff depends on the distance metric, you can find information in the corresponding docs. If set to None, the cutoff will be 10 for the alignment metric, and 2 for levenshtein and hamming. For the identity metric, the cutoff is ignored and always set to 0.

n_jobs : int | NoneOptional[int] (default: None)

Number of CPU cores to use when running a DistanceCalculator that supports paralellization.

A cutoff of 0 implies the identity metric.


Additional parameters passed to the DistanceCalculator.

Return type



Symmetrical, sparse pairwise distance matrix.