kaptive.core.interval¶
Genomic interval representation, strand orientations, and vectorized Structure-of-Arrays operations.
This module provides discrete coordinate intervals via Interval,
strand representations via Strand, and high-performance,
vectorized interval collections via Intervals.
Includes JIT-compiled Numba kernels for 1D spatial/sequential clustering and overlap culling.
Classes:
-
Interval–A single 0-based, half-open genomic interval with strand orientation.
-
Intervals–A high-performance Structure-of-Arrays (SoA) collection of genomic intervals.
-
Strand–Integer representation of genomic strand orientation.
Interval
dataclass
¶
A single 0-based, half-open genomic interval with strand orientation.
Represents a discrete sequence segment defined by start (inclusive), end (exclusive),
and strand orientation.
Attributes:
-
start(int) –0-based starting position (inclusive).
-
end(int) –0-based ending position (exclusive).
-
strand(Strand) –Strand orientation
Strand.
Methods:
-
__add__–Compute the minimal bounding interval covering both this interval and another.
-
__contains__–Check if a coordinate or another interval is fully contained within this interval.
-
__len__–Calculate the length of the interval in base pairs (
end - start). -
__radd__–Reverse addition operator supporting
other + self. -
expand–Expand or shrink the interval by specified base-pair amounts.
-
from_int–Create a 1-bp
Intervalfrom an integer coordinate. -
from_item–Universal factory coercing various representations into an
Interval. -
from_match–Create an
Intervalfrom a regular expression match object. -
from_slice–Create an
Intervalfrom a Python slice object. -
reverse_complement–Calculate the mirrored coordinates of the interval on the opposite strand.
-
shift–Shift interval coordinates by fixed distances.
__add__
¶
Compute the minimal bounding interval covering both this interval and another.
Parameters:
-
(other¶IntervalLike) –Interval-like object to merge with.
Returns:
Source code in src/kaptive/core/interval.py
__contains__
¶
Check if a coordinate or another interval is fully contained within this interval.
Parameters:
Returns:
-
bool(bool) –True if item is completely bounded within start and end coordinates.
Source code in src/kaptive/core/interval.py
__len__
¶
__len__() -> int
Calculate the length of the interval in base pairs (end - start).
Returns:
-
int(int) –Interval length.
__radd__
¶
Reverse addition operator supporting other + self.
Parameters:
-
(other¶IntervalLike) –Interval-like object to merge with.
Returns:
Source code in src/kaptive/core/interval.py
expand
¶
Expand or shrink the interval by specified base-pair amounts.
Parameters:
-
(left¶int) –Amount to extend start leftward (subtract from
start). -
(right¶int) –Amount to extend end rightward (add to
end). -
(clip_length¶int | None, default:None) –Maximum boundary constraint length.
Returns:
Source code in src/kaptive/core/interval.py
from_int
classmethod
¶
Create a 1-bp Interval from an integer coordinate.
Parameters:
-
(item¶int) –0-based coordinate.
-
(strand¶Strand, default:UNSTRANDED) –Strand orientation
Strand. -
(length¶int | None, default:None) –Parent sequence length to resolve negative indices.
Returns:
Source code in src/kaptive/core/interval.py
from_item
classmethod
¶
Universal factory coercing various representations into an Interval.
Parameters:
-
(item¶IntervalLike) –Object to convert (Interval, slice, int, or regex Match).
-
(strand¶Strand, default:UNSTRANDED) –Strand orientation
Strand. -
(length¶int | None, default:None) –Parent sequence length for coordinate resolution.
Returns:
Raises:
-
TypeError–If
itemis of an unsupported type.
Source code in src/kaptive/core/interval.py
from_match
classmethod
¶
Create an Interval from a regular expression match object.
Parameters:
-
(item¶Match) –Regex match object containing
start()andend(). -
(strand¶Strand, default:UNSTRANDED) –Strand orientation
Strand.
Returns:
Source code in src/kaptive/core/interval.py
from_slice
classmethod
¶
Create an Interval from a Python slice object.
Parameters:
-
(item¶slice) –Python slice with
startandstopvalues. -
(strand¶Strand, default:UNSTRANDED) –Strand orientation
Strand. -
(length¶int | None, default:None) –Parent sequence length for open-ended slices.
Returns:
Raises:
-
ValueError–If slice
stopis None and nolengthparameter is provided.
Source code in src/kaptive/core/interval.py
reverse_complement
¶
Calculate the mirrored coordinates of the interval on the opposite strand.
Parameters:
-
(length¶int | None, default:None) –Total length of parent sequence. If None, defaults to
end.
Returns:
Source code in src/kaptive/core/interval.py
shift
¶
Shift interval coordinates by fixed distances.
Parameters:
-
(x¶int) –Distance to shift the start coordinate.
-
(y¶int | None, default:None) –Distance to shift the end coordinate. If None, defaults to
x.
Returns:
Source code in src/kaptive/core/interval.py
Intervals
dataclass
¶
Intervals(starts: NDArray[int32], ends: NDArray[int32], strands: NDArray[int8], original_indices: NDArray[int32] | None = None)
flowchart TD
kaptive.core.interval.Intervals[Intervals]
kaptive.core.collections.BatchedContainer[BatchedContainer]
kaptive.core.collections.BatchedContainer --> kaptive.core.interval.Intervals
click kaptive.core.interval.Intervals href "" "kaptive.core.interval.Intervals"
click kaptive.core.collections.BatchedContainer href "" "kaptive.core.collections.BatchedContainer"
A high-performance Structure-of-Arrays (SoA) collection of genomic intervals.
Stores starts, ends, strands, and original tracking indices as parallel 1D NumPy arrays for memory-efficient vectorized operations and Numba spatial algorithms.
Attributes:
-
starts(NDArray[int32]) –1D array of 0-based start coordinates.
-
ends(NDArray[int32]) –1D array of 0-based end coordinates.
-
strands(NDArray[int8]) –1D array of strand values (+1, -1, 0).
-
original_indices(NDArray[int32] | None) –1D array tracking original item indices.
Methods:
-
__getitem__–Access intervals by integer index, slice, or boolean/integer NumPy array.
-
__len__–Return the number of intervals in the batch.
-
__post_init__–Initialize default
original_indicesarray if not explicitly supplied. -
arrange–Arrange interval coordinates across disjoint contig/piece layouts into a 1D space.
-
cluster_sequential–Perform index-based sequential clustering independent of physical base-pair distance.
-
cluster_spatial–Perform fast 1D single-linkage spatial clustering of intervals.
-
concat–Concatenate multiple
Intervalscollections into a single batch. -
cull_overlaps–Greedily cull overlapping intervals based on prior ordering and max overlap thresholds.
-
empty–Create an empty
Intervalsobject with zero-length arrays. -
from_dict–Deserialize an
Intervalscollection from a dictionary. -
from_intervals–Construct an
Intervalscollection from individual intervals. -
shift–Vectorized coordinate shift of all intervals in the collection.
-
to_dict–Serialize the interval batch into a dictionary of python lists.
__getitem__
¶
Access intervals 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/interval.py
__len__
¶
__len__() -> int
__post_init__
¶
Initialize default original_indices array if not explicitly supplied.
arrange
¶
arrange(indices: NDArray[integer], order: NDArray[integer], starts: NDArray[int32], ends: NDArray[int32], strands: NDArray[int8], gap: int = 500) -> Intervals
Arrange interval coordinates across disjoint contig/piece layouts into a 1D space.
Parameters:
-
(indices¶NDArray[integer]) –Piece index mapping for each interval.
-
(order¶NDArray[integer]) –Piece layout order.
-
(starts¶NDArray[int32]) –Piece start coordinates.
-
(ends¶NDArray[int32]) –Piece end coordinates.
-
(strands¶NDArray[int8]) –Piece orientations (+1 or -1).
-
(gap¶int, default:500) –Base-pair padding between adjacent pieces. Defaults to 500.
Returns:
Source code in src/kaptive/core/interval.py
cluster_sequential
¶
cluster_sequential(tolerance: int = 0, group_by: NDArray[integer] | None = None, enforce_strand: bool = False) -> NDArray[int32]
Perform index-based sequential clustering independent of physical base-pair distance.
Parameters:
-
(tolerance¶int, default:0) –Maximum allowed intervening item count. Defaults to 0.
-
(group_by¶NDArray[integer] | None, default:None) –Grouping array (e.g. contig ID).
-
(enforce_strand¶bool, default:False) –If True, restricts clustering to identical strands.
Returns:
-
NDArray[int32]–npt.NDArray[np.int32]: 1D array of cluster IDs parallel to original intervals.
Source code in src/kaptive/core/interval.py
cluster_spatial
¶
Perform fast 1D single-linkage spatial clustering of intervals.
Parameters:
-
(tolerance¶int, default:0) –Maximum base-pair distance threshold for clustering. Defaults to 0.
-
(group_by¶NDArray[integer] | None, default:None) –Grouping array (e.g. contig ID).
Returns:
-
NDArray[int32]–npt.NDArray[np.int32]: 1D array of cluster IDs parallel to original intervals.
Source code in src/kaptive/core/interval.py
concat
classmethod
¶
Concatenate multiple Intervals collections into a single batch.
Parameters:
Returns:
Raises:
-
ValueError–If
batchesis empty.
Source code in src/kaptive/core/interval.py
cull_overlaps
¶
cull_overlaps(order: NDArray[int32], max_overlap_fraction: float = 0.1, group_by: NDArray[integer] | None = None, secondary_group_by: NDArray[integer] | None = None) -> NDArray[bool_]
Greedily cull overlapping intervals based on prior ordering and max overlap thresholds.
Parameters:
-
(order¶NDArray[int32]) –Priority evaluation order indices.
-
(max_overlap_fraction¶float, default:0.1) –Maximum allowable overlap ratio. Defaults to 0.1.
-
(group_by¶NDArray[integer] | None, default:None) –Primary grouping array (e.g. contig ID).
-
(secondary_group_by¶NDArray[integer] | None, default:None) –Secondary grouping array.
Returns:
-
NDArray[bool_]–npt.NDArray[np.bool_]: Boolean mask array indicating kept intervals.
Source code in src/kaptive/core/interval.py
empty
classmethod
¶
empty() -> Intervals
Create an empty Intervals object with zero-length arrays.
Returns:
Source code in src/kaptive/core/interval.py
from_dict
classmethod
¶
Deserialize an Intervals collection from a dictionary.
Parameters:
Returns:
Source code in src/kaptive/core/interval.py
from_intervals
classmethod
¶
Construct an Intervals collection from individual intervals.
Parameters:
Returns:
Source code in src/kaptive/core/interval.py
shift
¶
Vectorized coordinate shift of all intervals in the collection.
Parameters:
-
(x¶int | NDArray[int32]) –Shift distance for starts.
-
(y¶int | NDArray[int32] | None, default:None) –Shift distance for ends. Defaults to
x.
Returns:
Source code in src/kaptive/core/interval.py
to_dict
¶
Serialize the interval batch into a dictionary of python lists.
Returns:
Source code in src/kaptive/core/interval.py
Strand
¶
flowchart TD
kaptive.core.interval.Strand[Strand]
click kaptive.core.interval.Strand href "" "kaptive.core.interval.Strand"
Integer representation of genomic strand orientation.
Supports conversion from common string formats ('+', '-', '1', '-1').
Attributes:
-
FORWARD(int) –Forward strand (
+1). -
REVERSE(int) –Reverse strand (
-1). -
UNSTRANDED(int) –Unstranded or unknown orientation (
0).
Methods:
-
__str__–Return the string representation of the strand (
'+','-', or'.').