This commit partitions the obikindex crate into multiple focused subcrates (obikfilter, obikmerge, obikquery, obikrebuild, obikselect, obikstats, obikdump, and obikidxcache) to reduce coupling and clarify module boundaries. It standardizes error handling across the workspace using OKIError and OKIResult, updates index APIs to support lazy, disk-backed partition access, and migrates NUMA system utilities to a new obisys crate. All modifications are structural, focusing on dependency graph expansion, import path updates, and API surface reorganization without altering core runtime behavior.
95 lines
3.5 KiB
Rust
95 lines
3.5 KiB
Rust
use std::path::Path;
|
|
|
|
use obicompactvec::{MatrixGroupOps, PersistentBitMatrix, PersistentCompactIntMatrix};
|
|
use obikseq::CanonicalKmer;
|
|
use obikindex::layer::MphfOnly;
|
|
use obikindex::{OKIError, OKIResult};
|
|
|
|
|
|
use super::MergeMode;
|
|
|
|
// ── SrcLayerData — opened source matrix for pass-2 lookup ─────────────────────
|
|
|
|
pub enum SrcLayerData {
|
|
Presence(MphfOnly, PersistentBitMatrix),
|
|
Count(MphfOnly, PersistentCompactIntMatrix),
|
|
}
|
|
|
|
impl SrcLayerData {
|
|
pub fn open(layer_dir: &Path, merge_mode: MergeMode) -> OKIResult<Self> {
|
|
let counts_dir = layer_dir.join("counts");
|
|
match merge_mode {
|
|
MergeMode::Presence => {
|
|
if counts_dir.exists() && !layer_dir.join("presence").exists() {
|
|
let mphf = MphfOnly::open(layer_dir)?;
|
|
let mat = PersistentCompactIntMatrix::open(layer_dir).map_err(OKIError::Io)?;
|
|
Ok(SrcLayerData::Count(mphf, mat))
|
|
} else {
|
|
// presence dir exists, or neither exists → Implicit handled by open()
|
|
let mphf = MphfOnly::open(layer_dir)?;
|
|
let mat = PersistentBitMatrix::open(layer_dir).map_err(OKIError::Io)?;
|
|
Ok(SrcLayerData::Presence(mphf, mat))
|
|
}
|
|
}
|
|
MergeMode::Count => {
|
|
let mphf = MphfOnly::open(layer_dir)?;
|
|
if counts_dir.exists() {
|
|
let mat = PersistentCompactIntMatrix::open(layer_dir).map_err(OKIError::Io)?;
|
|
Ok(SrcLayerData::Count(mphf, mat))
|
|
} else {
|
|
// No counts → treat as implicit presence (all 1s)
|
|
let mat = PersistentBitMatrix::open(layer_dir).map_err(OKIError::Io)?;
|
|
Ok(SrcLayerData::Presence(mphf, mat))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Return one value per source genome for `kmer`.
|
|
/// The caller guarantees `kmer` is in the source MPHF domain.
|
|
#[inline]
|
|
pub(crate) fn lookup(&self, kmer: CanonicalKmer, n_genomes: usize) -> Vec<u32> {
|
|
let mut buf = vec![0u32; n_genomes];
|
|
match self {
|
|
SrcLayerData::Presence(mphf, mat) => mat.fill_row(mphf.index(kmer), &mut buf),
|
|
SrcLayerData::Count(mphf, mat) => mat.fill_row(mphf.index(kmer), &mut buf),
|
|
}
|
|
buf
|
|
}
|
|
|
|
pub fn n_slots(&self) -> usize {
|
|
match self {
|
|
SrcLayerData::Presence(_, mat) => mat.n(),
|
|
SrcLayerData::Count(_, mat) => mat.n(),
|
|
}
|
|
}
|
|
|
|
/// MPHF lookup: returns the slot index for `kmer` (kmer must be in the domain).
|
|
#[inline]
|
|
pub fn slot(&self, kmer: CanonicalKmer) -> usize {
|
|
match self {
|
|
SrcLayerData::Presence(mphf, _) => mphf.index(kmer),
|
|
SrcLayerData::Count(mphf, _) => mphf.index(kmer),
|
|
}
|
|
}
|
|
|
|
/// Row lookup by slot index, bypassing the MPHF.
|
|
#[inline]
|
|
pub fn fill_row_by_slot(&self, slot: usize, n_genomes: usize) -> Vec<u32> {
|
|
let mut buf = vec![0u32; n_genomes];
|
|
match self {
|
|
SrcLayerData::Presence(_, mat) => mat.fill_row(slot, &mut buf),
|
|
SrcLayerData::Count(_, mat) => mat.fill_row(slot, &mut buf),
|
|
}
|
|
buf
|
|
}
|
|
|
|
/// Call `f` with a reference to the underlying matrix as `&dyn MatrixGroupOps`.
|
|
pub fn with_matrix<R>(&self, f: impl FnOnce(&dyn MatrixGroupOps) -> R) -> R {
|
|
match self {
|
|
SrcLayerData::Presence(_, mat) => f(mat),
|
|
SrcLayerData::Count(_, mat) => f(mat),
|
|
}
|
|
}
|
|
}
|