kaptive.core.kmers¶
K-mer indexing, FracMinHash, Randstrobe sketch construction, and alignment seed management.
This module provides sequence sketch indices (FracMinHashIndex,
RandstrobeIndex), alignment seed batch containers
(Seeds), and Numba JIT parallel search kernels.
Classes:
-
BaseKmerIndex–Abstract base class for high-performance k-mer based sequence indices using Array of Structs (AoS).
-
FracMinHashIndex–Specialized index for fast nucleotide sequence comparisons using FracMinHash.
-
RandstrobeIndex–Specialized index for fast amino-acid sequence comparisons using syncmer-linked randstrobes.
-
Seed–Alignment seed representing a potential matching region between query and target.
-
Seeds–Structure-of-Arrays (SoA) batch container for alignment seeds.
BaseKmerIndex
dataclass
¶
Abstract base class for high-performance k-mer based sequence indices using Array of Structs (AoS).
Attributes:
-
records(NDArray) –Structured NumPy array holding index records.
-
n_seqs(int) –Total number of indexed sequences.
-
is_sorted(bool) –True if records are sorted by hash.
-
k(int) –K-mer length parameter.
Methods:
-
__len__–Return the number of records in the index.
-
build–Build a k-mer index from a sequence collection.
-
empty–Create an empty BaseKmerIndex.
-
top_hits–Find the single best-matching target sequence for each query sequence.
build
classmethod
¶
build(batch: Sequences, **kwargs: Any) -> BaseKmerIndex
Build a k-mer index from a sequence collection.
Parameters:
-
(batch¶Sequences) –Sequence collection to index.
-
(**kwargs¶Any, default:{}) –Subclass-specific options.
Raises:
-
NotImplementedError–Must be implemented by subclasses.
Source code in src/kaptive/core/kmers.py
empty
classmethod
¶
empty() -> BaseKmerIndex
top_hits
¶
Find the single best-matching target sequence for each query sequence.
Parameters:
-
(queries¶BaseKmerIndex | Sequences) –Query sequence index or raw sequences.
-
(min_score¶int, default:1) –Minimum match score threshold. Defaults to 1.
Returns:
Source code in src/kaptive/core/kmers.py
FracMinHashIndex
dataclass
¶
FracMinHashIndex(*, records: NDArray, n_seqs: int = 0, is_sorted: bool = False, k: int = 10, scaled: int = 100, canonical: bool = True, bits_per_char: int = 2, lut: NDArray[uint8] | None = None)
flowchart TD
kaptive.core.kmers.FracMinHashIndex[FracMinHashIndex]
kaptive.core.kmers.BaseKmerIndex[BaseKmerIndex]
kaptive.core.kmers.BaseKmerIndex --> kaptive.core.kmers.FracMinHashIndex
click kaptive.core.kmers.FracMinHashIndex href "" "kaptive.core.kmers.FracMinHashIndex"
click kaptive.core.kmers.BaseKmerIndex href "" "kaptive.core.kmers.BaseKmerIndex"
Specialized index for fast nucleotide sequence comparisons using FracMinHash.
Attributes:
-
scaled(int) –FracMinHash scale factor (e.g. 100).
-
canonical(bool) –True if canonical k-mers (min of fwd/rev) are hashed.
-
bits_per_char(int) –Alphabet bits per character (2 for DNA).
-
lut(NDArray[uint8] | None) –Alphabet character lookup table.
Methods:
-
__len__–Return the number of records in the index.
-
build–Build a FracMinHashIndex from nucleotide sequence batch.
-
empty–Create an empty FracMinHashIndex.
-
to_sorted–Return a new FracMinHashIndex with records sorted by hash.
-
top_hits–Find the single best-matching target sequence for each query sequence.
build
classmethod
¶
build(batch: Sequences, k: int = 21, scaled: int = 100, canonical: bool = True, seed: int = 42, sort_by_hash: bool = False, lut: NDArray[uint8] | None = None, bits_per_char: int = 2, **kwargs: Any) -> FracMinHashIndex
Build a FracMinHashIndex from nucleotide sequence batch.
Parameters:
-
(batch¶Sequences) –Input sequence collection.
-
(k¶int, default:21) –K-mer length. Defaults to 21.
-
(scaled¶int, default:100) –Sampling scale factor. Defaults to 100.
-
(canonical¶bool, default:True) –True if using canonical k-mers. Defaults to True.
-
(seed¶int, default:42) –Seed for hashing. Defaults to 42.
-
(sort_by_hash¶bool, default:False) –True to sort output records by hash. Defaults to False.
-
(lut¶NDArray[uint8] | None, default:None) –Optional DNA lookup table.
-
(bits_per_char¶int, default:2) –Bits per character (2 for DNA).
-
(**kwargs¶Any, default:{}) –Additional parameters.
Returns:
-
FracMinHashIndex(FracMinHashIndex) –Built
FracMinHashIndexinstance.
Source code in src/kaptive/core/kmers.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
empty
classmethod
¶
empty() -> FracMinHashIndex
Create an empty FracMinHashIndex.
Returns:
-
FracMinHashIndex(FracMinHashIndex) –Empty index instance.
Source code in src/kaptive/core/kmers.py
to_sorted
¶
to_sorted() -> FracMinHashIndex
Return a new FracMinHashIndex with records sorted by hash.
Returns:
-
FracMinHashIndex(FracMinHashIndex) –Sorted
FracMinHashIndex.
Source code in src/kaptive/core/kmers.py
top_hits
¶
Find the single best-matching target sequence for each query sequence.
Parameters:
-
(queries¶BaseKmerIndex | Sequences) –Query sequence index or raw sequences.
-
(min_score¶int, default:1) –Minimum match score threshold. Defaults to 1.
Returns:
Source code in src/kaptive/core/kmers.py
RandstrobeIndex
dataclass
¶
RandstrobeIndex(*, records: NDArray, n_seqs: int = 0, is_sorted: bool = False, k: int = 10, s: int = 5, w_min: int = 1, w_max: int = 5, lut: NDArray[uint8] | None = None)
flowchart TD
kaptive.core.kmers.RandstrobeIndex[RandstrobeIndex]
kaptive.core.kmers.BaseKmerIndex[BaseKmerIndex]
kaptive.core.kmers.BaseKmerIndex --> kaptive.core.kmers.RandstrobeIndex
click kaptive.core.kmers.RandstrobeIndex href "" "kaptive.core.kmers.RandstrobeIndex"
click kaptive.core.kmers.BaseKmerIndex href "" "kaptive.core.kmers.BaseKmerIndex"
Specialized index for fast amino-acid sequence comparisons using syncmer-linked randstrobes.
Attributes:
-
s(int) –Sub-k-mer size for open syncmer evaluation. Defaults to 5.
-
w_min(int) –Minimum syncmer offset window bound. Defaults to 1.
-
w_max(int) –Maximum syncmer offset window bound. Defaults to 5.
-
lut(NDArray[uint8] | None) –Optional amino acid lookup table.
Methods:
-
__len__–Return the number of records in the index.
-
build–Build a RandstrobeIndex from sequence batch.
-
empty–Create an empty RandstrobeIndex.
-
top_hits–Find the single best-matching target sequence for each query sequence.
build
classmethod
¶
build(batch: Sequences, k: int = 10, s: int = 5, w_min: int = 1, w_max: int = 5, canonical: bool = True, seed: int = 42, sort_by_hash: bool = False, lut: NDArray[uint8] | None = None, **kwargs: Any) -> RandstrobeIndex
Build a RandstrobeIndex from sequence batch.
Parameters:
-
(batch¶Sequences) –Input sequence collection.
-
(k¶int, default:10) –K-mer length. Defaults to 10.
-
(s¶int, default:5) –Sub-k-mer size. Defaults to 5.
-
(w_min¶int, default:1) –Min syncmer window bound. Defaults to 1.
-
(w_max¶int, default:5) –Max syncmer window bound. Defaults to 5.
-
(canonical¶bool, default:True) –Canonical option. Defaults to True.
-
(seed¶int, default:42) –Random seed. Defaults to 42.
-
(sort_by_hash¶bool, default:False) –True to sort records by hash. Defaults to False.
-
(lut¶NDArray[uint8] | None, default:None) –Optional alphabet lookup table.
-
(**kwargs¶Any, default:{}) –Additional parameters.
Returns:
-
RandstrobeIndex(RandstrobeIndex) –Constructed
RandstrobeIndex.
Raises:
-
ValueError–If
s >= k.
Source code in src/kaptive/core/kmers.py
empty
classmethod
¶
empty() -> RandstrobeIndex
Create an empty RandstrobeIndex.
Returns:
-
RandstrobeIndex(RandstrobeIndex) –Empty index instance.
Source code in src/kaptive/core/kmers.py
top_hits
¶
Find the single best-matching target sequence for each query sequence.
Parameters:
-
(queries¶BaseKmerIndex | Sequences) –Query sequence index or raw sequences.
-
(min_score¶int, default:1) –Minimum match score threshold. Defaults to 1.
Returns:
Source code in src/kaptive/core/kmers.py
Seed
¶
flowchart TD
kaptive.core.kmers.Seed[Seed]
click kaptive.core.kmers.Seed href "" "kaptive.core.kmers.Seed"
Alignment seed representing a potential matching region between query and target.
Attributes:
-
query_index(int) –Index of the query sequence.
-
target_index(int) –Index of the target sequence.
-
score(int) –Match score (number of shared k-mer hashes or randstrobes).
-
offset(int) –Diagonal offset calculated as
query_pos - target_pos.
Seeds
dataclass
¶
Seeds(query_indices: NDArray[uint32], target_indices: NDArray[uint32], scores: NDArray[uint32], offsets: NDArray[int32])
flowchart TD
kaptive.core.kmers.Seeds[Seeds]
kaptive.core.collections.BatchedContainer[BatchedContainer]
kaptive.core.collections.BatchedContainer --> kaptive.core.kmers.Seeds
click kaptive.core.kmers.Seeds href "" "kaptive.core.kmers.Seeds"
click kaptive.core.collections.BatchedContainer href "" "kaptive.core.collections.BatchedContainer"
Structure-of-Arrays (SoA) batch container for alignment seeds.
Stores query indices, target indices, scores, and diagonal offsets as 1D NumPy arrays.
Attributes:
-
query_indices(NDArray[uint32]) –1D array of query sequence indices.
-
target_indices(NDArray[uint32]) –1D array of target sequence indices.
-
scores(NDArray[uint32]) –1D array of alignment match scores.
-
offsets(NDArray[int32]) –1D array of diagonal offset values.
Methods:
-
__getitem__–Access seeds by integer index, slice, or boolean/integer NumPy array.
-
__len__–Return the number of seeds in the batch.
-
concat–Concatenate multiple
Seedscollections into a single batch. -
cull_overlaps–Greedily cull seeds that overlap significantly on the target sequence.
-
empty–Create an empty
Seedscollection. -
extract_sequences–Extract parallel batches of query and target sequences mapped by this seed batch.
-
filter–Return a new Seeds collection containing only records where the mask is True.
-
to_intervals–Convert seeds into target coordinates
Intervals. -
top_hits–Reduce the batch to only the highest-scoring seed for each query.
__getitem__
¶
Access seeds by integer index, slice, or boolean/integer NumPy array.
Parameters:
Returns:
Raises:
-
IndexError–If integer index is out of bounds.
Source code in src/kaptive/core/kmers.py
concat
classmethod
¶
Concatenate multiple Seeds collections into a single batch.
Parameters:
Returns:
Source code in src/kaptive/core/kmers.py
cull_overlaps
¶
cull_overlaps(query_lengths: NDArray[int32], max_overlap_fraction: float = 0.1, priority_mask: NDArray[bool_] | None = None) -> Seeds
Greedily cull seeds that overlap significantly on the target sequence.
Parameters:
-
(query_lengths¶NDArray[int32]) –Query sequence lengths array.
-
(max_overlap_fraction¶float, default:0.1) –Maximum allowable overlap fraction. Defaults to 0.1.
-
(priority_mask¶NDArray[bool_] | None, default:None) –Optional boolean mask for priority score boost.
Returns:
Source code in src/kaptive/core/kmers.py
empty
classmethod
¶
empty() -> Seeds
Create an empty Seeds collection.
Returns:
Source code in src/kaptive/core/kmers.py
extract_sequences
¶
Extract parallel batches of query and target sequences mapped by this seed batch.
Parameters:
-
(queries¶Sequences) –Source collection of query sequences.
-
(targets¶Sequences) –Source collection of target sequences.
Returns:
-
tuple[Sequences, Sequences]–tuple[Sequences, Sequences]: Paired query and target sequence collections.
Source code in src/kaptive/core/kmers.py
filter
¶
Return a new Seeds collection containing only records where the mask is True.
Parameters:
-
(mask¶NDArray[bool_]) –1D boolean mask array.
Returns:
Source code in src/kaptive/core/kmers.py
to_intervals
¶
to_intervals(query_lengths: NDArray[int32]) -> Intervals
Convert seeds into target coordinates Intervals.
Parameters:
-
(query_lengths¶NDArray[int32]) –Query sequence lengths array.
Returns:
Source code in src/kaptive/core/kmers.py
top_hits
¶
Reduce the batch to only the highest-scoring seed for each query.
Parameters:
Returns: