kaptive.core.pairwise¶
Pairwise sequence alignment algorithms and containers.
This module provides high-performance pairwise sequence alignment capabilities
using a parallelized, banded Smith-Waterman-Gotoh dynamic programming algorithm
with affine gap penalties. Alignment results are stored in memory-efficient Structure-of-Arrays
(SoA) containers (PairwiseAlignments) or
scalar records (PairwiseAlignment).
Key Classes
PairwiseAlignment: Immutable container for a single result.PairwiseAlignments: Batched SoA container for results.PairwiseAligner: Banded Smith-Waterman-Gotoh alignment engine.
Classes:
-
PairwiseAligner–A high-performance, batched pairwise sequence aligner.
-
PairwiseAlignment–A lightweight, immutable container for the results of a single pairwise sequence alignment.
-
PairwiseAlignments–A high-performance SoA container for the results of multiple pairwise alignments.
PairwiseAligner
dataclass
¶
A high-performance, batched pairwise sequence aligner.
Uses a parallelized, banded Smith-Waterman-Gotoh algorithm to align pairs of sequences.
Attributes:
-
gap_open(int) –Penalty for opening a new gap. Defaults to 11.
-
gap_extend(int) –Penalty for extending an existing gap. Defaults to 1.
-
k(int) –Bandwidth parameter (
2*k+1diagonals). Defaults to 20.
Methods:
-
__call__–Perform pairwise alignment of query and target sequences.
-
align_seeds–Convenience method to extract and align specific sequence pairs mapped by seeds.
__call__
¶
Perform pairwise alignment of query and target sequences.
Parameters:
-
(queries¶Sequences) –Collection of query sequences.
-
(targets¶Sequences) –Collection of target sequences (must match query batch size).
-
(seeds¶Seeds | None, default:None) –Optional alignment seeds guiding diagonal alignment.
Returns:
-
PairwiseAlignments(PairwiseAlignments) –Alignment scores, statistics, and coordinates for each sequence pair.
Raises:
-
ValueError–If query and target batches have different numbers of sequences.
Source code in src/kaptive/core/pairwise.py
align_seeds
¶
Convenience method to extract and align specific sequence pairs mapped by seeds.
Parameters:
-
(queries¶Sequences) –Full collection of query sequences.
-
(targets¶Sequences) –Full collection of target sequences.
-
(seeds¶Seeds) –Seed collection mapping specific query sequences to target sequences.
Returns:
-
PairwiseAlignments(PairwiseAlignments) –Alignment results parallel to the provided seeds.
Source code in src/kaptive/core/pairwise.py
PairwiseAlignment
dataclass
¶
PairwiseAlignment(score: int, matches: int, mismatches: int, gaps: int, q_start: int, q_end: int, t_start: int, t_end: int)
A lightweight, immutable container for the results of a single pairwise sequence alignment.
This class holds summary statistics and coordinates for an alignment between a single query
and target sequence. It is typically produced by indexing into a
PairwiseAlignments collection.
Attributes:
-
score(int) –Final alignment score calculated using BLOSUM62 and gap penalties.
-
matches(int) –Total number of matching bases.
-
mismatches(int) –Total number of mismatched bases.
-
gaps(int) –Total number of gap characters (insertions or deletions).
-
q_start(int) –0-based start coordinate on query sequence (inclusive).
-
q_end(int) –0-based end coordinate on query sequence (exclusive).
-
t_start(int) –0-based start coordinate on target sequence (inclusive).
-
t_end(int) –0-based end coordinate on target sequence (exclusive).
PairwiseAlignments
dataclass
¶
PairwiseAlignments(scores: NDArray[int32], matches: NDArray[int32], mismatches: NDArray[int32], gaps: NDArray[int32], q_starts: NDArray[int32], q_ends: NDArray[int32], t_starts: NDArray[int32], t_ends: NDArray[int32])
flowchart TD
kaptive.core.pairwise.PairwiseAlignments[PairwiseAlignments]
kaptive.core.collections.BatchedContainer[BatchedContainer]
kaptive.core.collections.BatchedContainer --> kaptive.core.pairwise.PairwiseAlignments
click kaptive.core.pairwise.PairwiseAlignments href "" "kaptive.core.pairwise.PairwiseAlignments"
click kaptive.core.collections.BatchedContainer href "" "kaptive.core.collections.BatchedContainer"
A high-performance SoA container for the results of multiple pairwise alignments.
This class stores alignment statistics in a Structure-of-Arrays (SoA) layout using 1D NumPy arrays.
Attributes:
-
scores(NDArray[int32]) –1D array of alignment scores.
-
matches(NDArray[int32]) –1D array of match counts.
-
mismatches(NDArray[int32]) –1D array of mismatch counts.
-
gaps(NDArray[int32]) –1D array of gap counts.
-
q_starts(NDArray[int32]) –1D array of query start coordinates.
-
q_ends(NDArray[int32]) –1D array of query end coordinates.
-
t_starts(NDArray[int32]) –1D array of target start coordinates.
-
t_ends(NDArray[int32]) –1D array of target end coordinates.
Methods:
-
__getitem__–Access alignment results by index, slice, or boolean array mask.
-
__len__–Return the number of alignments in the batch.
-
concat–Concatenate multiple PairwiseAlignments collections into a single batch.
-
empty–Create an empty PairwiseAlignments collection with zero-length int32 arrays.
-
from_dict–Deserialize a PairwiseAlignments batch from a dictionary of array-like data.
-
to_dict–Convert the alignment batch to a dictionary of NumPy arrays for serialization.
pidents
property
¶
Calculate percent identity for all alignments in the batch in a vectorized manner.
Returns:
-
NDArray[float64]–npt.NDArray[np.float64]: 1D array of percent identity values.
__getitem__
¶
__getitem__(item: Any) -> PairwiseAlignment | PairwiseAlignments
Access alignment results by index, slice, or boolean array mask.
Parameters:
Returns:
-
PairwiseAlignment | PairwiseAlignments–PairwiseAlignment | PairwiseAlignments: Scalar record or sliced batch collection.
Raises:
-
IndexError–If integer index is out of range.
Source code in src/kaptive/core/pairwise.py
__len__
¶
__len__() -> int
concat
classmethod
¶
Concatenate multiple PairwiseAlignments collections into a single batch.
Parameters:
-
(batches¶Iterable[PairwiseAlignments]) –Iterable of alignment collections.
Returns:
-
PairwiseAlignments(Self) –Single concatenated alignment collection.
Source code in src/kaptive/core/pairwise.py
empty
classmethod
¶
empty() -> PairwiseAlignments
Create an empty PairwiseAlignments collection with zero-length int32 arrays.
Returns:
-
PairwiseAlignments(PairwiseAlignments) –Empty alignment collection.
Source code in src/kaptive/core/pairwise.py
from_dict
classmethod
¶
from_dict(d: dict[str, Any]) -> PairwiseAlignments
Deserialize a PairwiseAlignments batch from a dictionary of array-like data.
Parameters:
Returns:
-
PairwiseAlignments(PairwiseAlignments) –Deserialized pairwise alignments container.
Source code in src/kaptive/core/pairwise.py
to_dict
¶
Convert the alignment batch to a dictionary of NumPy arrays for serialization.
Returns:
-
dict[str, NDArray[int32]]–dict[str, npt.NDArray[np.int32]]: Dictionary mapping attribute names to arrays.