Refactor k-mer sibling tracking to compact bitmask and on-demand counts
Replaces the explicit `SiblingInfo` struct and 3-bit minorant flags with a derived 4-bit presence mask (`FamilyMask`) that tracks observed bases per family. This eliminates redundant file I/O overhead by introducing a `PartitionCache` for batch lookups, simplifies serialization, and updates all downstream builders, stats computation, and tests to operate on the new bitmask representation. Adjusts CLI output to report deduplicated family sizes instead of histograms, ignores generated CSV files, and updates documentation to reflect the fixed canonical reference and new theory.
This commit is contained in:
1 parent
ba990a48a0
commit
1a470eab9e
6 files changed
+671
-277
No files matched your search
@@ -78,8 +78,7 @@ pub struct DistanceArgs {
|
||||
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.
|
||||
/// <prefix>_siblings.csv, <prefix>_nj.nwk, <prefix>_upgma.nwk.
|
||||
/// If omitted, the distance matrix is written to stdout.
|
||||
#[arg(short, long)]
|
||||
pub output: Option<PathBuf>,
|
||||
@@ -114,6 +113,17 @@ pub fn run(args: DistanceArgs) {
|
||||
write_sibling_stats_csv(&stats, &labels, &args.output);
|
||||
}
|
||||
|
||||
// `--sibling-annex`/`--sibling-stats` are their own operation, not a
|
||||
// modifier on top of a distance-metric computation — a metric was
|
||||
// never requested by asking for either of them, so there is nothing
|
||||
// for the rest of this function to compute. Not a historical accident
|
||||
// to keep: stop here rather than always also running a Jaccard (or
|
||||
// whichever `--metric` defaults to) pass and printing an unrequested
|
||||
// matrix.
|
||||
if args.sibling_annex || args.sibling_stats {
|
||||
return;
|
||||
}
|
||||
|
||||
info!(
|
||||
"computing {:?} distances for {} genome(s)",
|
||||
args.metric, n
|
||||
@@ -225,43 +235,38 @@ pub fn run(args: DistanceArgs) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Sibling-count distribution → CSV ────────────────────────────────────────
|
||||
// ── Family-size distribution → CSV ──────────────────────────────────────────
|
||||
//
|
||||
// Each row is a family (the up-to-4 k-mers sharing flanks, differing only at
|
||||
// the centre), counted once — at its minorant — regardless of how many of
|
||||
// its members are observed. Family size 1..4 (not "sibling count" 0..3):
|
||||
// see `docmd/theory/evolutionary_distances.md`, "Definitions".
|
||||
|
||||
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()
|
||||
// One row per genome (4 columns, family size 1-4: number of families of
|
||||
// that size for which the genome carries at least one member), plus a
|
||||
// `global` row — the actual deduplicated family-size histogram
|
||||
// (`stats.counts`), NOT a sum of the per-genome columns (a family shared
|
||||
// by several genomes would otherwise be counted once per genome it
|
||||
// appears in, inflating the total beyond the real family count).
|
||||
let 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}");
|
||||
let mut f = BufWriter::new(std::fs::File::create(&path).unwrap_or_else(|e| {
|
||||
eprintln!("error creating {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];
|
||||
writeln!(f, "genome,1,2,3,4").unwrap();
|
||||
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],
|
||||
f, "global,{},{},{},{}",
|
||||
stats.counts[0], stats.counts[1], stats.counts[2], stats.counts[3],
|
||||
).unwrap();
|
||||
info!("per-genome sibling-count distribution → {per_genome_path}");
|
||||
let total: u64 = stats.counts.iter().sum();
|
||||
info!("family-size distribution → {path} (total {total} famil{})",
|
||||
if total == 1 { "y" } else { "ies" });
|
||||
}
|
||||
|
||||
// ── UPGMA Newick from kodama dendrogram ───────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user