feat: implement per-k-mer sibling counts and central neighbor generation

Introduce the siblingannex module in obicompactvec to store per-slot minorant flags and sibling counts in a memory-mapped annex file. Add a scatter-gather pipeline in obikindex to compute these values across index layers and write them to .psib files. Implement central_canonical_neighbors in obikseq for generating strand-aware k-mer variants around the middle base. Expose rolling statistics in obiskbuilder and update dependency graphs accordingly.
This commit is contained in:
Eric Coissac
2026-08-10 15:01:59 +02:00
parent 8bc6d533e5
commit ea914bb536
9 changed files with 609 additions and 1 deletions
+21
View File
@@ -341,6 +341,27 @@ impl<L: KmerLength> CanonicalKmerOf<L> {
]
}
/// Return the four central canonical neighbours (each already canonical),
/// substituting the base at the middle position `m = (L::len()-1)/2`
/// (well-defined for odd `L::len()`). Each of the 4 substitutions is
/// canonicalised independently — this correctly handles the case where a
/// substitution flips the canonical orientation, unlike inferring the
/// variant from a fixed-orientation flank key. One of the 4 equals
/// `self`'s own canonical form (the identity substitution); callers that
/// only want the 3 genuine variants should skip it.
pub fn central_canonical_neighbors(&self) -> [CanonicalKmerOf<L>; 4] {
let k = L::len();
let m = (k - 1) / 2;
let shift = KMER_BITS - 2 - 2 * m;
let cleared = self.0 & !((0b11 as RawKmer) << shift);
[
KmerOf::<L>(cleared | ((0 as RawKmer) << shift), PhantomData).canonical(),
KmerOf::<L>(cleared | ((1 as RawKmer) << shift), PhantomData).canonical(),
KmerOf::<L>(cleared | ((2 as RawKmer) << shift), PhantomData).canonical(),
KmerOf::<L>(cleared | ((3 as RawKmer) << shift), PhantomData).canonical(),
]
}
/// Return the inner value as a raw [`KmerOf<L>`].
#[inline]
pub fn into_kmer(self) -> KmerOf<L> {
+42
View File
@@ -210,4 +210,46 @@ mod tests {
check!(31);
check!(32);
}
// ── central_canonical_neighbors ─────────────────────────────────────────
#[test]
fn central_canonical_neighbors_hand_checked_k3() {
// k=3, centre = index 1. For "ACG", every one of the 4 central
// substitutions ("AAG","ACG","AGG","ATG") happens to stay in forward
// orientation when canonicalised (verified by hand: each is already
// lexicographically <= its own reverse complement), so this case
// exercises the substitution logic without the RC-flip edge case.
let ck = KmerOf::<ConstLen<3>>::from_ascii(b"ACG").unwrap().canonical();
let neighbours = ck.central_canonical_neighbors();
let ascii: Vec<Vec<u8>> = neighbours.iter().map(|n| n.to_ascii()).collect();
assert_eq!(ascii, vec![b"AAG".to_vec(), b"ACG".to_vec(), b"AGG".to_vec(), b"ATG".to_vec()]);
// The identity substitution (centre unchanged) must reproduce `ck`.
assert!(neighbours.contains(&ck));
}
#[test]
fn central_canonical_neighbors_identity_present_for_various_k() {
macro_rules! check {
($n:expr) => {{
let ck = KmerOf::<ConstLen<$n>>::from_ascii(&make_seq::<$n>())
.unwrap()
.canonical();
let neighbours = ck.central_canonical_neighbors();
assert!(
neighbours.contains(&ck),
"identity substitution missing from central_canonical_neighbors for k={}",
$n
);
// Every returned neighbour must itself already be canonical.
for n in &neighbours {
assert_eq!(n.into_kmer().canonical(), *n, "neighbour not canonical for k={}", $n);
}
}};
}
check!(1);
check!(3);
check!(5);
check!(31);
}
}