Implement --sibling-annex flag and k-mer family annex builder

Introduces a new `--sibling-annex` CLI flag that triggers the construction of a sibling-count/minorant annex for multi-genome indices. The implementation adds a `FamilyMask` data structure with memory-mapped I/O, enabling lock-free concurrent updates via atomic bitwise operations. Batched processing improves cache locality and parallelism, while helper functions derive family presence and minorant flags dynamically. The feature is exposed through an `IndexCache` extension trait, protected by an exclusive directory lock to prevent index corruption during construction.
This commit is contained in:
Eric Coissac committed 2026-08-28 20:29:01 +02:00
1 parent d084396aba
commit 920120d20e
10 files changed
+876 -15

No files matched your search

+10 -4
View File
@@ -36,15 +36,21 @@ impl From<MetricArg> for DistanceMetric {
}
/// Partial transfer of `obikmer`'s `phylo` command: the plain distance-metric
/// path only (`--metric`/NJ/UPGMA) — everything sibling-annex-based
/// (`--sibling-annex`, `--snp`, `--sankoff`, `--tnt`/`--phyg`/`--iqtree`, ...)
/// stays in `obikmer` until `obikphylo::siblings` is reconnected (see the
/// project memory on this).
/// path (`--metric`/NJ/UPGMA) plus annex construction (`--sibling-annex`) —
/// everything else sibling-annex-based (`--snp`, `--sankoff`,
/// `--tnt`/`--phyg`/`--iqtree`, stats, ...) stays in `obikmer` until the rest
/// of `obikphylo::siblings` is reconnected (see the project memory on this).
#[derive(Args)]
pub struct PhyloArgs {
/// Index directory
pub index: PathBuf,
/// Build (or rebuild) the sibling-count/minorant annex — independent of
/// the distance metric below, meant to be run routinely, ahead of any
/// SNP-family distance computation that will later consume it.
#[arg(long)]
pub sibling_annex: bool,
/// Distance metric to compute
#[arg(long, value_enum, default_value = "jaccard")]
pub metric: MetricArg,
+26 -4
View File
@@ -5,6 +5,7 @@ use std::sync::Arc;
use obikidxcache::index_cache::IndexCache;
use obikindex::KmerIndex;
use obikphylo::siblings::SiblingAnnexBuildExt;
use obikphylo::{Metrics, neighbor_joining, upgma};
use obisys::{Reporter, Stage};
use tracing::info;
@@ -31,13 +32,34 @@ pub fn run(args: PhyloArgs) {
let mut rep = Reporter::new();
info!("computing {:?} distances for {} genome(s)", args.metric, n);
// Every partition/layer this needs is opened once, up front, and
// handed to `Metrics::distance` — see `obikquery`'s own use of
// `IndexCache` for the same reasoning (one open, many in-memory reads).
// handed to `Metrics::distance`/`SiblingAnnexBuildExt::build_sibling_annex`
// — see `obikquery`'s own use of `IndexCache` for the same reasoning
// (one open, many in-memory reads).
let cache = IndexCache::new(Arc::clone(&idx), None);
// ── Sibling-count/minorant annex (independent of the distance metric) ──
// Meant to be (re)built routinely, ahead of any SNP-family distance
// computation that will later consume it.
if args.sibling_annex {
// Writes into the index directory — hold an exclusive lock for the
// duration so a second, concurrent `--sibling-annex` run on the
// same index can't corrupt these writes (see obisys::DirLock).
let _lock = obisys::DirLock::acquire(&args.index).unwrap_or_else(|e| {
eprintln!("error locking index directory {}: {e}", args.index.display());
std::process::exit(1);
});
info!("building sibling-count/minorant annex");
let t = Stage::start("sibling_annex");
cache.build_sibling_annex().unwrap_or_else(|e| {
eprintln!("error building sibling annex: {e}");
std::process::exit(1);
});
rep.push(t.stop());
}
info!("computing {:?} distances for {} genome(s)", args.metric, n);
let need_shared = args.shared_kmers || args.nj || args.upgma;
let t = Stage::start("distance");
let result = cache