feat: add convert command for in-place index evidence modification
Introduce a new CLI command that modifies existing indices to support exact, approximate, or hybrid evidence modes. This change enables the reindex module in obikrebuild, exposing an IndexReindex trait for evidence persistence and layer processing. It also refactors obikindexer to publicly expose an IndexBuilder trait for post-build evidence construction, while adding a set_evidence API in obikindex to safely update configuration fields without altering core parameters.
This commit is contained in:
1 parent
bd7729b095
commit
4ea3cd32ba
10 files changed
+562
-340
No files matched your search
@@ -0,0 +1,138 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::Args;
|
||||
use obikindex::KmerIndex;
|
||||
use obikindex::layer::IndexMode;
|
||||
use obikrebuild::IndexReindex;
|
||||
use obisys::Reporter;
|
||||
use tracing::info;
|
||||
|
||||
use crate::cli::block_size_to_bits;
|
||||
use super::index::resolve_approx_params;
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct ConvertArgs {
|
||||
/// Index directory to convert (modified in-place)
|
||||
pub index: PathBuf,
|
||||
|
||||
#[command(flatten)]
|
||||
mode: ModeArgs,
|
||||
|
||||
/// Fingerprint bits per slot (b) — required with --hybrid-evidence
|
||||
/// when the source index is currently exact (there is no existing
|
||||
/// fingerprint to inherit `b` from); rejected otherwise (the source
|
||||
/// already fixes `b`, either from its own --approx-evidence or from
|
||||
/// an already-approximate/hybrid index being converted to hybrid).
|
||||
#[arg(long, value_name = "BITS")]
|
||||
pub evidence_bits: Option<u8>,
|
||||
|
||||
/// Findere z parameter: number of consecutive *stored* k-mers (length
|
||||
/// = the index's own k-mer size, fixed forever at `index` build time)
|
||||
/// that must all match to confirm a hit.
|
||||
///
|
||||
/// WARNING: this does NOT shorten the indexed k-mer length the way
|
||||
/// `-z` does at `index` build time — that length can never change
|
||||
/// after the fact. Instead it *extends* the effective match window:
|
||||
/// requiring z consecutive stored k-mers of length s to all match is
|
||||
/// equivalent to requiring (s + z − 1) consecutive identical bases in
|
||||
/// the query. E.g. on a k=31 index, z=2 requires 32 consecutive
|
||||
/// matching bases, not 30 — going from z=1 to z=2 makes matching
|
||||
/// *stricter*, it does not "index shorter k-mers".
|
||||
#[arg(short = 'z', long)]
|
||||
pub findere_z: Option<u8>,
|
||||
|
||||
/// Target false-positive rate per z-window (e.g. 0.01) — derives `b`
|
||||
/// or `z` when one of them isn't given directly (see `index --fp` for
|
||||
/// the same resolution rules).
|
||||
#[arg(long)]
|
||||
pub fp: Option<f64>,
|
||||
|
||||
/// Block size for exact evidence `.idx` (number of unitigs per block).
|
||||
/// Ignored when converting to pure approximate evidence.
|
||||
#[arg(long, default_value_t = 1)]
|
||||
pub block_size: usize,
|
||||
}
|
||||
|
||||
#[derive(Args)]
|
||||
#[group(required = true, multiple = false)]
|
||||
struct ModeArgs {
|
||||
/// Convert to exact evidence (zero false positives).
|
||||
#[arg(long)]
|
||||
exact_evidence: bool,
|
||||
|
||||
/// Convert to approximate (fingerprint-only) evidence. Value = fingerprint bits per slot (b).
|
||||
#[arg(long, value_name = "BITS")]
|
||||
approx_evidence: Option<u8>,
|
||||
|
||||
/// Convert to hybrid evidence (both exact and approximate bundles kept).
|
||||
#[arg(long)]
|
||||
hybrid_evidence: bool,
|
||||
}
|
||||
|
||||
pub fn run(args: ConvertArgs) {
|
||||
// Modifies the index in place; acquired before opening so a concurrent
|
||||
// writer can't slip in between the open and the convert below.
|
||||
let _lock = obisys::DirLock::acquire(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error locking index directory {}: {e}", args.index.display());
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
let idx = KmerIndex::open(&args.index).unwrap_or_else(|e| {
|
||||
eprintln!("error opening index: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
let current = idx.meta().config().evidence.clone();
|
||||
|
||||
let target = if args.mode.exact_evidence {
|
||||
warn_if_evidence_params_given(&args, "--exact-evidence");
|
||||
info!("target: exact evidence");
|
||||
IndexMode::Exact
|
||||
} else if let Some(bits) = args.mode.approx_evidence {
|
||||
let (z, b, fp) = resolve_approx_params(args.findere_z, Some(bits), args.fp);
|
||||
info!("target: approximate evidence — b={b}, z={z}, fp={fp:.2e}");
|
||||
IndexMode::Approx { b, z }
|
||||
} else {
|
||||
debug_assert!(args.mode.hybrid_evidence);
|
||||
match current {
|
||||
IndexMode::Exact => {
|
||||
let bits = args.evidence_bits.unwrap_or_else(|| {
|
||||
eprintln!(
|
||||
"error: --hybrid-evidence requires --evidence-bits when starting from an exact index"
|
||||
);
|
||||
std::process::exit(1);
|
||||
});
|
||||
let (z, b, fp) = resolve_approx_params(args.findere_z, Some(bits), args.fp);
|
||||
info!("target: hybrid evidence — b={b}, z={z}, fp={fp:.2e}");
|
||||
IndexMode::Hybrid { b, z }
|
||||
}
|
||||
IndexMode::Approx { b, z } | IndexMode::Hybrid { b, z } => {
|
||||
if args.evidence_bits.is_some() || args.findere_z.is_some() || args.fp.is_some() {
|
||||
eprintln!(
|
||||
"error: --evidence-bits/-z/--fp are not allowed with --hybrid-evidence \
|
||||
when the source index is already approximate/hybrid — its existing \
|
||||
b={b}, z={z} are reused as-is"
|
||||
);
|
||||
std::process::exit(1);
|
||||
}
|
||||
info!("target: hybrid evidence — reusing existing b={b}, z={z}");
|
||||
IndexMode::Hybrid { b, z }
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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!("convert error: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
rep.print();
|
||||
}
|
||||
|
||||
fn warn_if_evidence_params_given(args: &ConvertArgs, mode: &str) {
|
||||
if args.evidence_bits.is_some() || args.findere_z.is_some() || args.fp.is_some() {
|
||||
eprintln!("warning: --evidence-bits/-z/--fp ignored with {mode}");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod annotate;
|
||||
pub mod convert;
|
||||
pub mod dump;
|
||||
pub mod estimate;
|
||||
pub mod filter;
|
||||
|
||||
@@ -37,6 +37,8 @@ enum Commands {
|
||||
Annotate(cmd::annotate::AnnotateArgs),
|
||||
/// Maintenance/inspection operations on already-built indexes
|
||||
Utils(cmd::utils::UtilsArgs),
|
||||
/// Convert an index's evidence representation (exact/approximate/hybrid), in place
|
||||
Convert(cmd::convert::ConvertArgs),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -61,5 +63,6 @@ fn main() {
|
||||
Commands::Estimate(args) => cmd::estimate::run(args),
|
||||
Commands::Annotate(args) => cmd::annotate::run(args),
|
||||
Commands::Utils(args) => cmd::utils::run(args),
|
||||
Commands::Convert(args) => cmd::convert::run(args),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user