Refactor chunking logic to enforce 128 k-mer limit

This change reduces the maximum number of k-mers per chunk from 256 to 128. This modification requires updating the data model to handle sequences exceeding this limit by splitting them into overlapping chunks, ensuring no k-mers are lost. Extensive testing has been added to validate the new chunking logic and k-mer constraints.
This commit is contained in:
Eric Coissac committed 2026-10-15 12:20:36 +02:00
1 parent 8468319a63
commit 025c40c0db
8 files changed
+80 -25

No files matched your search

+7 -2
View File
@@ -22,8 +22,13 @@ use crate::kmer::{CanonicalKmer, Kmer, KmerError, KLen, KmerLength, KmerOf, MLen
use crate::params::k;
use crate::revcomp_lookup::REVCOMP4;
/// Maximum kmers per stored chunk. Enforces the u8 max-kmer-index field in the binary format.
pub const MAX_KMERS_PER_CHUNK: usize = 256;
/// Maximum kmers per stored chunk.
///
/// 128 = 2^7, not the 256 the u8 length field of the binary format could
/// carry: the exact evidence of a layer (`obikindex::layer::evidence`) stores a
/// kmer's rank inside its unitig chunk on 7 bits, so every rank must be ≤ 127.
/// Longer unitigs are split into consecutive chunks at write time.
pub const MAX_KMERS_PER_CHUNK: usize = 128;
// ── PackedSeq ─────────────────────────────────────────────────────────────────
+4 -4
View File
@@ -70,7 +70,7 @@ fn seql_roundtrip() {
fn binary_test_lengths(k: usize) -> Vec<usize> {
use crate::packed_seq::MAX_KMERS_PER_CHUNK;
// Only single-chunk lengths: seql in [k, MAX_KMERS_PER_CHUNK+k-1].
(k..=k + 5).chain([255, 256, 257, MAX_KMERS_PER_CHUNK + k - 1]).collect()
(k..=k + 5).chain([MAX_KMERS_PER_CHUNK + k - 3, MAX_KMERS_PER_CHUNK + k - 2, MAX_KMERS_PER_CHUNK + k - 1]).collect()
}
#[test]
@@ -94,7 +94,7 @@ fn binary_split_roundtrip() {
use crate::packed_seq::MAX_KMERS_PER_CHUNK;
set_k(4);
let k = crate::params::k();
// seql = MAX_KMERS_PER_CHUNK + k = 260 → n_kmers = 257 > 256 → 2 chunks
// seql = MAX_KMERS_PER_CHUNK + k → n_kmers = MAX + 1 > MAX → 2 chunks
let seql = MAX_KMERS_PER_CHUNK + k;
let mut sk = SuperKmer::from_ascii(&make_seq(seql));
sk.set_count(7);
@@ -108,8 +108,8 @@ fn binary_split_roundtrip() {
assert_eq!(chunk0.count(), 7);
assert_eq!(chunk1.count(), 7);
// Chunks cover the original sequence with k-1 overlap — no kmer lost.
assert_eq!(chunk0.seql(), MAX_KMERS_PER_CHUNK + k - 1); // 259
assert_eq!(chunk1.seql(), k); // 4 (1 kmer)
assert_eq!(chunk0.seql(), MAX_KMERS_PER_CHUNK + k - 1);
assert_eq!(chunk1.seql(), k); // 1 kmer
}
#[test]
+1 -1
View File
@@ -164,7 +164,7 @@ mod tests {
use crate::packed_seq::MAX_KMERS_PER_CHUNK;
set_k(4);
let k = crate::params::k();
let valid_lengths: Vec<usize> = (k..=9).chain([255, 256, 257, MAX_KMERS_PER_CHUNK + k - 1]).collect();
let valid_lengths: Vec<usize> = (k..=9).chain([MAX_KMERS_PER_CHUNK + k - 3, MAX_KMERS_PER_CHUNK + k - 2, MAX_KMERS_PER_CHUNK + k - 1]).collect();
for len in valid_lengths {
let u = Unitig::from_ascii(&make_seq(len));
let mut buf = Vec::new();