feat: add approximate evidence matching and index estimation CLI
Introduces a new `estimate` CLI subcommand to calculate bloom filter size, evidence bits, and false-positive rates for approximate indexing. Updates the index building and querying pipelines to support both exact and approximate evidence types via a unified `EvidenceKind` abstraction. Refactors `MphfLayer` and partition index builders to route operations based on the selected evidence mode, and adds the required `obilayeredmap` dependency.
This commit is contained in:
1 parent
16a6b0d033
commit
876bc0127f
11 files changed
+243
-47
No files matched your search
@@ -18,6 +18,7 @@ obikpartitionner = { path = "../obikpartitionner" }
|
||||
obisys = { path = "../obisys" }
|
||||
obiskio = { path = "../obiskio" }
|
||||
obikindex = { path = "../obikindex" }
|
||||
obilayeredmap = { path = "../obilayeredmap" }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
csv = "1"
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
use clap::Args;
|
||||
|
||||
use super::index::resolve_approx_params;
|
||||
|
||||
#[derive(Args)]
|
||||
pub struct EstimateArgs {
|
||||
/// k-mer size used for querying (same as --kmer-size in index)
|
||||
#[arg(short = 'k', long, default_value_t = 31)]
|
||||
pub kmer_size: usize,
|
||||
|
||||
/// Findere z parameter: number of consecutive k-mers that must all match.
|
||||
/// Effective indexed k-mer size is kmer_size - z + 1.
|
||||
#[arg(short = 'z', long, default_value = None)]
|
||||
pub findere_z: Option<u8>,
|
||||
|
||||
/// Fingerprint bits per slot (b). FP per z-window = 1/2^(b·z).
|
||||
#[arg(long, default_value = None)]
|
||||
pub evidence_bits: Option<u8>,
|
||||
|
||||
/// Target false-positive rate per z-window (e.g. 0.01).
|
||||
#[arg(long, default_value = None)]
|
||||
pub fp: Option<f64>,
|
||||
}
|
||||
|
||||
pub fn run(args: EstimateArgs) {
|
||||
let (z, b, fp_window) = resolve_approx_params(args.findere_z, args.evidence_bits, args.fp);
|
||||
|
||||
let k_query = args.kmer_size;
|
||||
let k_index = k_query.saturating_sub(z as usize - 1);
|
||||
let fp_kmer = 1.0_f64 / 2_f64.powi(b as i32);
|
||||
|
||||
println!("{:<22} {}", "k (query):", k_query);
|
||||
println!("{:<22} {}", "k (indexed):", k_index);
|
||||
println!("{:<22} {}", "z:", z);
|
||||
println!("{:<22} {}", "evidence bits (b):", b);
|
||||
println!("{:<22} {:.3e} (1/2^{})", "FP per k-mer:", fp_kmer, b);
|
||||
println!("{:<22} {:.3e} (1/2^{})", "FP per z-window:", fp_window, b as u32 * z as u32);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use std::path::PathBuf;
|
||||
|
||||
use clap::Args;
|
||||
use obikindex::{GenomeInfo, IndexConfig, IndexState, KmerIndex};
|
||||
use obilayeredmap::EvidenceKind;
|
||||
|
||||
fn parse_key_value(s: &str) -> Result<(String, String), String> {
|
||||
let pos = s.find('=').ok_or_else(|| format!("invalid key=value: no '=' in '{s}'"))?;
|
||||
@@ -48,14 +49,116 @@ pub struct IndexArgs {
|
||||
#[arg(long, default_value_t = false)]
|
||||
pub keep_intermediate: bool,
|
||||
|
||||
/// Use approximate (fingerprint-based) evidence instead of exact evidence.
|
||||
/// False-positive rate per z-window: 1/2^(b·z).
|
||||
#[arg(long, default_value_t = false)]
|
||||
pub approx: bool,
|
||||
|
||||
/// Findere z parameter: number of consecutive k-mers that must all match.
|
||||
/// Effective indexed k-mer size is kmer_size - z + 1.
|
||||
#[arg(short = 'z', long, default_value = None)]
|
||||
pub findere_z: Option<u8>,
|
||||
|
||||
/// Fingerprint bits per slot (b). FP per z-window = 1/2^(b·z).
|
||||
#[arg(long, default_value = None)]
|
||||
pub evidence_bits: Option<u8>,
|
||||
|
||||
/// Target false-positive rate per z-window (e.g. 0.01).
|
||||
/// Used to derive missing b or z.
|
||||
#[arg(long, default_value = None)]
|
||||
pub fp: Option<f64>,
|
||||
|
||||
#[command(flatten)]
|
||||
pub common: CommonArgs,
|
||||
}
|
||||
|
||||
/// Resolve the (z, b, fp) triplet from the user-supplied subset.
|
||||
///
|
||||
/// Model: FP = 1/2^(b·z) ⟹ b·z = ⌈-log₂(fp)⌉
|
||||
///
|
||||
/// Rules when one value is missing (conservative = ceiling):
|
||||
/// given z, b → fp = 1/2^(b·z)
|
||||
/// given z, fp → b = ⌈-log₂(fp) / z⌉
|
||||
/// given b, fp → z = ⌈-log₂(fp) / b⌉
|
||||
/// given z only → b = 8 (default), fp derived
|
||||
/// given b only → z = 1 (default), fp derived
|
||||
/// given fp only → b = 8 (default), z derived
|
||||
/// none given → z = 1, b = 8, fp = 1/256
|
||||
pub(crate) fn resolve_approx_params(
|
||||
z_opt: Option<u8>,
|
||||
b_opt: Option<u8>,
|
||||
fp_opt: Option<f64>,
|
||||
) -> (u8, u8, f64) {
|
||||
const DEFAULT_B: u8 = 8;
|
||||
const DEFAULT_Z: u8 = 1;
|
||||
|
||||
let bits_needed = |fp: f64| -> u8 {
|
||||
(-fp.log2()).ceil() as u8
|
||||
};
|
||||
|
||||
match (z_opt, b_opt, fp_opt) {
|
||||
// All three given: use b and z, recompute fp conservatively.
|
||||
(Some(z), Some(b), Some(_fp)) => {
|
||||
let fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, fp)
|
||||
}
|
||||
// Two given, derive third.
|
||||
(Some(z), Some(b), None) => {
|
||||
let fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, fp)
|
||||
}
|
||||
(Some(z), None, Some(fp)) => {
|
||||
let bz = (-fp.log2()).ceil() as u32;
|
||||
let b = ((bz + z as u32 - 1) / z as u32).max(1) as u8;
|
||||
let actual_fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, actual_fp)
|
||||
}
|
||||
(None, Some(b), Some(fp)) => {
|
||||
let bz = (-fp.log2()).ceil() as u32;
|
||||
let z = ((bz + b as u32 - 1) / b as u32).max(1) as u8;
|
||||
let actual_fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, actual_fp)
|
||||
}
|
||||
// One given, apply defaults for the other.
|
||||
(Some(z), None, None) => {
|
||||
let b = DEFAULT_B;
|
||||
let fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, fp)
|
||||
}
|
||||
(None, Some(b), None) => {
|
||||
let z = DEFAULT_Z;
|
||||
let fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, fp)
|
||||
}
|
||||
(None, None, Some(fp)) => {
|
||||
let b = DEFAULT_B;
|
||||
let z = ((bits_needed(fp) as u32 + b as u32 - 1) / b as u32).max(1) as u8;
|
||||
let actual_fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, actual_fp)
|
||||
}
|
||||
// None given: defaults.
|
||||
(None, None, None) => {
|
||||
let b = DEFAULT_B;
|
||||
let z = DEFAULT_Z;
|
||||
let fp = 1.0_f64 / (1u64 << (b as u32 * z as u32)) as f64;
|
||||
(z, b, fp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(args: IndexArgs) {
|
||||
let output = args.output.clone();
|
||||
let mut rep = Reporter::new();
|
||||
|
||||
// ── Resolve evidence kind ────────────────────────────────────────────────
|
||||
let evidence = if args.approx {
|
||||
let (z, b, fp) = resolve_approx_params(args.findere_z, args.evidence_bits, args.fp);
|
||||
info!("approximate evidence: b={b}, z={z}, fp={fp:.2e}");
|
||||
EvidenceKind::Approx { b, z }
|
||||
} else {
|
||||
EvidenceKind::Exact
|
||||
};
|
||||
|
||||
// ── Open or create the index ─────────────────────────────────────────────
|
||||
let mut idx = if KmerIndex::exists(&output) && !args.force {
|
||||
info!("resuming from existing index at {}", output.display());
|
||||
@@ -81,6 +184,7 @@ pub fn run(args: IndexArgs) {
|
||||
minimizer_size: args.common.minimizer_size,
|
||||
n_bits,
|
||||
with_counts: args.with_counts,
|
||||
evidence: evidence.clone(),
|
||||
};
|
||||
let genome_info = args.label.as_ref().map(|label| {
|
||||
let mut info = GenomeInfo::new(label.clone());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
pub mod annotate;
|
||||
pub mod distance;
|
||||
pub mod dump;
|
||||
pub mod estimate;
|
||||
pub mod index;
|
||||
pub mod merge;
|
||||
pub mod query;
|
||||
|
||||
@@ -32,6 +32,8 @@ enum Commands {
|
||||
Distance(cmd::distance::DistanceArgs),
|
||||
/// Dump unitigs from a built index to stdout (debug)
|
||||
Unitig(cmd::unitig::UnitigArgs),
|
||||
/// Estimate approximate-index parameters (z, evidence bits, FP rates) before indexing
|
||||
Estimate(cmd::estimate::EstimateArgs),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -62,6 +64,7 @@ fn main() {
|
||||
Commands::Annotate(args) => cmd::annotate::run(args),
|
||||
Commands::Distance(args) => cmd::distance::run(args),
|
||||
Commands::Unitig(args) => cmd::unitig::run(args),
|
||||
Commands::Estimate(args) => cmd::estimate::run(args),
|
||||
}
|
||||
|
||||
#[cfg(feature = "profiling")]
|
||||
|
||||
Reference in new issue
Block a user