feat: add configurable block sizes and in-place reindex command

Propagate configurable block size (`block_bits`) through index and layer construction to control unitig chunking and optimize memory/performance trade-offs. Introduce an in-place `reindex` command and library method to convert indices between exact and approximate evidence formats. Add validation to reject merging indexes with mismatched evidence types, and update parallel kmer counting to use `AtomicUsize` for thread-safe aggregation. Includes CLI argument parsing, metadata persistence, and updated tests.
This commit is contained in:
Eric Coissac committed 2026-05-23 13:28:24 +02:00
1 parent 876bc0127f
commit bc51cd9861
21 files changed
+318 -51

No files matched your search

+6
View File
@@ -55,6 +55,12 @@ pub fn partitions_to_bits(n: usize) -> usize {
n.max(1).next_power_of_two().trailing_zeros() as usize
}
/// Convert a block size (number of unitigs per block) to its `block_bits` exponent.
/// `block_size=1` → `block_bits=0` (one entry per unitig, O(1) random access).
pub fn block_size_to_bits(n: usize) -> u8 {
n.max(1).next_power_of_two().trailing_zeros() as u8
}
impl CommonArgs {
pub fn seqfile_paths(&self) -> obiread::PathIter {
let paths = self.inputs.iter().map(PathBuf::from).collect();
+8 -1
View File
@@ -12,7 +12,7 @@ use obikseq::{set_k, set_m};
use obisys::Reporter;
use tracing::info;
use crate::cli::{CommonArgs, partitions_to_bits};
use crate::cli::{CommonArgs, block_size_to_bits, partitions_to_bits};
use crate::steps::scatter;
#[derive(Args)]
@@ -68,6 +68,11 @@ pub struct IndexArgs {
#[arg(long, default_value = None)]
pub fp: Option<f64>,
/// Block size for exact evidence `.idx` (number of unitigs per block).
/// Must be a power of two; rounded up if not. Default 1 = O(1) random access.
#[arg(long, default_value_t = 1)]
pub block_size: usize,
#[command(flatten)]
pub common: CommonArgs,
}
@@ -179,12 +184,14 @@ pub fn run(args: IndexArgs) {
if effective != args.common.partitions {
info!("partitions: {} → {} (next power of 2)", args.common.partitions, effective);
}
let block_bits = block_size_to_bits(args.block_size);
let config = IndexConfig {
kmer_size: args.common.kmer_size,
minimizer_size: args.common.minimizer_size,
n_bits,
with_counts: args.with_counts,
evidence: evidence.clone(),
block_bits,
};
let genome_info = args.label.as_ref().map(|label| {
let mut info = GenomeInfo::new(label.clone());
+1
View File
@@ -6,5 +6,6 @@ pub mod index;
pub mod merge;
pub mod query;
pub mod rebuild;
pub mod reindex;
pub mod superkmer;
pub mod unitig;
+68
View File
@@ -0,0 +1,68 @@
use std::path::PathBuf;
use clap::Args;
use obikindex::KmerIndex;
use obilayeredmap::EvidenceKind;
use obisys::Reporter;
use tracing::info;
use crate::cli::block_size_to_bits;
use super::index::resolve_approx_params;
#[derive(Args)]
pub struct ReindexArgs {
/// Index directory to convert (modified in-place)
pub index: PathBuf,
/// Convert to approximate evidence (default: convert to exact).
/// Requires --evidence-bits and/or -z and/or --fp.
#[arg(long, default_value_t = false)]
pub approx: bool,
/// Findere z parameter (≥1).
#[arg(short = 'z', long, default_value = None)]
pub findere_z: Option<u8>,
/// Fingerprint bits per slot (b).
#[arg(long, default_value = None)]
pub evidence_bits: Option<u8>,
/// Target false-positive rate per z-window.
#[arg(long, default_value = None)]
pub fp: Option<f64>,
/// Block size for exact evidence `.idx` (number of unitigs per block).
/// Ignored when converting to approximate evidence.
#[arg(long, default_value_t = 1)]
pub block_size: usize,
}
pub fn run(args: ReindexArgs) {
let target = if args.approx {
let (z, b, fp) = resolve_approx_params(args.findere_z, args.evidence_bits, args.fp);
info!("target: approximate evidence — b={b}, z={z}, fp={fp:.2e}");
EvidenceKind::Approx { b, z }
} else {
info!("target: exact evidence");
EvidenceKind::Exact
};
let mut idx = KmerIndex::open(&args.index).unwrap_or_else(|e| {
eprintln!("error opening index: {e}");
std::process::exit(1);
});
info!(
"current evidence: {:?}",
idx.meta().config.evidence,
);
let block_bits = block_size_to_bits(args.block_size);
let mut rep = Reporter::new();
idx.reindex(target, block_bits, &mut rep).unwrap_or_else(|e| {
eprintln!("reindex error: {e}");
std::process::exit(1);
});
rep.print();
}
+3 -3
View File
@@ -35,13 +35,13 @@ pub fn run(args: UnitigArgs) {
return;
}
let reader = UnitigFileReader::open(&path).unwrap_or_else(|e| {
// open_sequential: works with and without .idx (approx or exact index)
let reader = UnitigFileReader::open_sequential(&path).unwrap_or_else(|e| {
eprintln!("error opening unitigs (partition {i}): {e}");
std::process::exit(1)
});
for j in 0..reader.len() {
let unitig = reader.unitig(j);
for (j, unitig) in reader.iter_unitigs() {
let mut out = stdout.lock().unwrap();
write_unitig(&unitig, k, i, j, &mut *out).unwrap_or_else(|e| {
eprintln!("write error: {e}");
+3
View File
@@ -34,6 +34,8 @@ enum Commands {
Unitig(cmd::unitig::UnitigArgs),
/// Estimate approximate-index parameters (z, evidence bits, FP rates) before indexing
Estimate(cmd::estimate::EstimateArgs),
/// Convert an index's evidence in-place: exact ↔ approx
Reindex(cmd::reindex::ReindexArgs),
}
fn main() {
@@ -65,6 +67,7 @@ fn main() {
Commands::Distance(args) => cmd::distance::run(args),
Commands::Unitig(args) => cmd::unitig::run(args),
Commands::Estimate(args) => cmd::estimate::run(args),
Commands::Reindex(args) => cmd::reindex::run(args),
}
#[cfg(feature = "profiling")]