Add unified matrix builder abstraction and k-mer filtering CLI
Introduces a unified MatrixBuilder abstraction for persistent bit and integer matrices, replacing custom dispatch enums and boolean flags across consumers. Refactors index, merge, and select layers to adopt explicit merge modes and centralized lifecycle management. Adds a complete k-mer filtering implementation with partition-level processing, progress tracking, and a new CLI subcommand supporting configurable predicates and thresholds.
This commit is contained in:
1 parent
b7a8b5e6cf
commit
354e6f9bf1
17 files changed
+588
-125
No files matched your search
@@ -8,6 +8,7 @@ mod format;
|
||||
mod rankselect;
|
||||
mod intmatrix;
|
||||
mod layer_meta;
|
||||
mod matrix_builder;
|
||||
mod meta;
|
||||
mod mmap_file;
|
||||
mod reader;
|
||||
@@ -27,6 +28,7 @@ pub use builder::PersistentCompactIntVecBuilder;
|
||||
pub use colgroup::{ColGroup, FilterMask, MatrixGroupOps, eval_filter_mask};
|
||||
pub use intmatrix::{PersistentCompactIntMatrix, PersistentCompactIntMatrixBuilder, pack_compact_int_matrix};
|
||||
pub use layer_meta::LayerMeta;
|
||||
pub use matrix_builder::{ColBuilder, MatrixBuilder};
|
||||
pub use reader::{PersistentCompactIntVec, Iter as CompactIntVecIter};
|
||||
pub use sparse_intmatrix::{PersistentSparseCompactIntMatrix, PersistentSparseCompactIntMatrixBuilder, pack_sparse_compact_int_matrix};
|
||||
pub use storage_kind::StorageKind;
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
//! Bit/Int-dispatching wrappers around the persistent matrix/column
|
||||
//! builders — pure row/column mechanics, no domain reference (genome,
|
||||
//! k-mer, …). [`ColBuilder`] wraps one column; [`MatrixBuilder`] wraps a
|
||||
//! whole matrix and hands out [`ColBuilder`]s. Shared by every consumer
|
||||
//! that builds a destination matrix whose content kind (count vs
|
||||
//! presence) is chosen at runtime: `obikmerge`, `obikselect`, `obikfilter`.
|
||||
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::{PersistentBitMatrixBuilder as BitMB, PersistentBitVecBuilder};
|
||||
use crate::{PersistentCompactIntMatrixBuilder as IntMB, PersistentCompactIntVecBuilder};
|
||||
use crate::{TempBitVec, TempCompactIntVec};
|
||||
|
||||
// ── ColBuilder ────────────────────────────────────────────────────────────────
|
||||
|
||||
pub enum ColBuilder {
|
||||
Bit(PersistentBitVecBuilder),
|
||||
Int(PersistentCompactIntVecBuilder),
|
||||
}
|
||||
|
||||
impl ColBuilder {
|
||||
pub fn set_val(&mut self, slot: usize, value: u32) {
|
||||
match self {
|
||||
ColBuilder::Bit(b) => b.set(slot, value > 0),
|
||||
ColBuilder::Int(b) => b.set(slot, value),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(self) -> io::Result<()> {
|
||||
match self {
|
||||
ColBuilder::Bit(b) => b.close(),
|
||||
ColBuilder::Int(b) => b.close(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── MatrixBuilder ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Wraps whichever matrix builder `presence` calls for, so callers never
|
||||
/// have to know the on-disk column naming or the matrix `meta.json` schema
|
||||
/// — both stay private to this crate. `resume` reopens a matrix directory
|
||||
/// already closed by a previous builder session, continuing from its
|
||||
/// current `n_cols` instead of starting a fresh matrix at 0.
|
||||
pub enum MatrixBuilder {
|
||||
Bit(BitMB),
|
||||
Int(IntMB),
|
||||
}
|
||||
|
||||
impl MatrixBuilder {
|
||||
pub fn new(presence: bool, n: usize, dir: &Path) -> io::Result<Self> {
|
||||
Ok(if presence {
|
||||
MatrixBuilder::Bit(BitMB::new(n, dir)?)
|
||||
} else {
|
||||
MatrixBuilder::Int(IntMB::new(n, dir)?)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resume(presence: bool, dir: &Path) -> io::Result<Self> {
|
||||
Ok(if presence {
|
||||
MatrixBuilder::Bit(BitMB::resume(dir)?)
|
||||
} else {
|
||||
MatrixBuilder::Int(IntMB::resume(dir)?)
|
||||
})
|
||||
}
|
||||
|
||||
/// Add a column with no data written (all-zero/false) — for columns
|
||||
/// absent from a given source (e.g. destination-only genomes not
|
||||
/// contributed by the source being merged in).
|
||||
pub fn add_absent_col(&mut self) -> io::Result<()> {
|
||||
match self {
|
||||
MatrixBuilder::Bit(b) => b.add_col()?.close(),
|
||||
MatrixBuilder::Int(b) => b.add_col()?.close(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_col(&mut self) -> io::Result<ColBuilder> {
|
||||
Ok(match self {
|
||||
MatrixBuilder::Bit(b) => ColBuilder::Bit(b.add_col()?),
|
||||
MatrixBuilder::Int(b) => ColBuilder::Int(b.add_col()?),
|
||||
})
|
||||
}
|
||||
|
||||
/// Add a column already computed as a bit vector.
|
||||
pub fn add_col_from_bit(&mut self, src: &TempBitVec) -> io::Result<()> {
|
||||
match self {
|
||||
MatrixBuilder::Bit(b) => b.add_col_from(src),
|
||||
MatrixBuilder::Int(b) => b.add_col_from_bit(src),
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a column already computed as an int vector.
|
||||
pub fn add_col_from_int(&mut self, src: &TempCompactIntVec) -> io::Result<()> {
|
||||
match self {
|
||||
MatrixBuilder::Bit(b) => b.add_col_from_int(src),
|
||||
MatrixBuilder::Int(b) => b.add_col_from(src),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close(self) -> io::Result<()> {
|
||||
match self {
|
||||
MatrixBuilder::Bit(b) => b.close(),
|
||||
MatrixBuilder::Int(b) => b.close(),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user