feat: add obipipeline for concurrent sibling annex stats
Add the `obipipeline` crate and replace sequential scatter/gather logic with a concurrent pipeline using `Flat` and `Transform` stages. Introduce `SiblingAnnexStats` API to compute distributions, and add CLI flags to `distance.rs` for constructing the annex and exporting statistics as CSV.
This commit is contained in:
1 parent
ea914bb536
commit
ba990a48a0
5 files changed
+310
-59
No files matched your search
@@ -3,7 +3,7 @@ use std::path::PathBuf;
|
||||
|
||||
use clap::Args;
|
||||
use kodama::{Method, linkage};
|
||||
use obikindex::{DistanceMetric, KmerIndex};
|
||||
use obikindex::{DistanceMetric, KmerIndex, SiblingAnnexStats};
|
||||
use speedytree::{DistanceMatrix, Hybrid, NeighborJoiningSolver, to_newick};
|
||||
use tracing::info;
|
||||
|
||||
@@ -64,7 +64,21 @@ pub struct DistanceArgs {
|
||||
#[arg(long)]
|
||||
pub upgma: bool,
|
||||
|
||||
/// Build the sibling-count/minorant annex on this (multi-genome) index
|
||||
/// — see `docmd/theory/evolutionary_distances.md`, Step 2b. Construction
|
||||
/// only; does not by itself compute or write any statistics.
|
||||
#[arg(long)]
|
||||
pub sibling_annex: bool,
|
||||
|
||||
/// Tally the sibling-count distribution (CSV) of an already-built annex
|
||||
/// (run with `--sibling-annex` first, in this invocation or an earlier
|
||||
/// one). A separate, occasional diagnostic pass — not run every time the
|
||||
/// annex itself is (re)built.
|
||||
#[arg(long)]
|
||||
pub sibling_stats: bool,
|
||||
|
||||
/// Output prefix: <prefix>_dist.csv, <prefix>_shared.csv,
|
||||
/// <prefix>_siblings.csv, <prefix>_siblings_per_genome.csv,
|
||||
/// <prefix>_nj.nwk, <prefix>_upgma.nwk.
|
||||
/// If omitted, the distance matrix is written to stdout.
|
||||
#[arg(short, long)]
|
||||
@@ -80,6 +94,26 @@ pub fn run(args: DistanceArgs) {
|
||||
|
||||
let labels: Vec<String> = idx.meta().genomes.iter().map(|g| g.label.clone()).collect();
|
||||
let n = labels.len();
|
||||
|
||||
// ── Sibling-count/minorant annex (independent of the distance metric) ──
|
||||
// Construction (`--sibling-annex`) and stats (`--sibling-stats`) are
|
||||
// deliberately decoupled: the annex is meant to be (re)built routinely,
|
||||
// the distribution only occasionally, on demand.
|
||||
if args.sibling_annex {
|
||||
info!("building sibling-count/minorant annex");
|
||||
idx.build_sibling_annex().unwrap_or_else(|e| {
|
||||
eprintln!("error building sibling annex: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
if args.sibling_stats {
|
||||
let stats = idx.sibling_annex_stats().unwrap_or_else(|e| {
|
||||
eprintln!("error computing sibling-annex stats: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
write_sibling_stats_csv(&stats, &labels, &args.output);
|
||||
}
|
||||
|
||||
info!(
|
||||
"computing {:?} distances for {} genome(s)",
|
||||
args.metric, n
|
||||
@@ -191,6 +225,45 @@ pub fn run(args: DistanceArgs) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Sibling-count distribution → CSV ────────────────────────────────────────
|
||||
|
||||
fn write_sibling_stats_csv(stats: &SiblingAnnexStats, labels: &[String], output: &Option<PathBuf>) {
|
||||
// Global histogram: one row per sibling count (0-3).
|
||||
let global_path = output.as_ref()
|
||||
.map(|p| format!("{}_siblings.csv", p.display()))
|
||||
.unwrap_or_else(|| "siblings.csv".into());
|
||||
let mut f = BufWriter::new(std::fs::File::create(&global_path).unwrap_or_else(|e| {
|
||||
eprintln!("error creating {global_path}: {e}");
|
||||
std::process::exit(1);
|
||||
}));
|
||||
writeln!(f, "siblings,slots,minorant_slots").unwrap();
|
||||
for s in 0..4 {
|
||||
writeln!(f, "{s},{},{}", stats.counts[s], stats.minorant_counts[s]).unwrap();
|
||||
}
|
||||
let total: u64 = stats.counts.iter().sum();
|
||||
info!("sibling-count distribution → {global_path} (total {total} slot(s))");
|
||||
|
||||
// Per-genome breakdown: one row per genome, 4 columns (0-3), + a total row.
|
||||
let per_genome_path = output.as_ref()
|
||||
.map(|p| format!("{}_siblings_per_genome.csv", p.display()))
|
||||
.unwrap_or_else(|| "siblings_per_genome.csv".into());
|
||||
let mut f = BufWriter::new(std::fs::File::create(&per_genome_path).unwrap_or_else(|e| {
|
||||
eprintln!("error creating {per_genome_path}: {e}");
|
||||
std::process::exit(1);
|
||||
}));
|
||||
writeln!(f, "genome,0,1,2,3").unwrap();
|
||||
let mut column_totals = [0u64; 4];
|
||||
for (label, counts) in labels.iter().zip(stats.per_genome.iter()) {
|
||||
writeln!(f, "{label},{},{},{},{}", counts[0], counts[1], counts[2], counts[3]).unwrap();
|
||||
for s in 0..4 { column_totals[s] += counts[s]; }
|
||||
}
|
||||
writeln!(
|
||||
f, "total,{},{},{},{}",
|
||||
column_totals[0], column_totals[1], column_totals[2], column_totals[3],
|
||||
).unwrap();
|
||||
info!("per-genome sibling-count distribution → {per_genome_path}");
|
||||
}
|
||||
|
||||
// ── UPGMA Newick from kodama dendrogram ───────────────────────────────────────
|
||||
|
||||
fn upgma_to_newick(dendro: &kodama::Dendrogram<f64>, names: &[String]) -> String {
|
||||
|
||||
Reference in new issue
Block a user