refactor: replace no_ambiguity bool with AmbiguityPolicy enum

Replace the binary `no_ambiguity` flag with an `AmbiguityPolicy` enum (`Keep`, `Forbid`, `OnlyDiploid`) to support multiple ambiguity conventions. This change updates CLI arguments, internal logic, and persistent storage formats to distinguish between keeping all genomes, forbidding ambiguous ones, or restricting analysis to diploid sites only.

- Add `src/obikphylo/src/siblings/ambiguity.rs` defining the `AmbiguityPolicy` enum with variants `Keep`, `Forbid`, and `OnlyDiploid`, including methods for header flags and determining if a genome is dropped.
- Update `PhyloArgs` in `args.rs` to derive `AmbiquityPolicy` from CLI flags and construct `AnnexSelection` for sibling annexes.
- Modify `SamplingSpec` in `subsample.rs` to store `ambiguity: AmbiguityPolicy` instead of `no_ambiguity`, updating masking and scalar value logic accordingly.
- Update `SiblingExt` in `sibling_ext.rs` so `build_sibling_annex` accepts an `AnnexSelection` to build only requested derived annexes, and `family_overlap` uses the specific policy.
- Refactor `FamilyOverlap` in `family_overlap.rs` to store and use `AmbiguityPolicy`, replacing boolean checks with policy-based carrier determination.
- Update `FamilyScalarAnnex` in `family_scalar_annex.rs` to store `AmbiguityPolicy` in headers, enabling distinct file names for each policy variant.
- Modify `quartet_annex.rs` to generate distinct file names based on `AmbiguityPolicy`, ensuring no overlap between Keep, Forbid, and OnlyDiploid annexes.
- Update `family_annexes.rs` to use `AnnexSelection` for building entropy, quartet, and pair weight annexes under specific policies instead of a single boolean convention.
- Refactor algorithm modules (`entropy.rs`, `masking.rs`, `pair_weight.rs`, `quartet.rs`, `snp_distance.rs`) to accept `AmbiguityPolicy` arguments, replacing boolean checks with policy-specific logic (e.g., `OnlyDiploid` masking).
- Update documentation in `phylo.md` and `evolutionary_distances.md` to describe the new `--only-diploid` option, the behavior of `--sibling-annex` with specific policies, and the separate storage of entropies per convention.
This commit is contained in:
Eric Coissac committed 2026-11-09 20:59:09 +11:00
1 parent b55123a376
commit 5368c98875
21 files changed
+477 -278

No files matched your search

+25 -1
View File
@@ -1,6 +1,7 @@
use std::path::PathBuf;
use clap::Args;
use obikphylo::siblings::{AmbiguityPolicy, AnnexSelection};
use obikphylo::{DistanceMetric, GammaShape, SnpDistanceKind};
/// `--gamma-shape` CLI value: a fixed `alpha`, or `auto`/`estimate` to fit
@@ -223,7 +224,13 @@ pub struct PhyloArgs {
/// 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.
/// SNP-family distance computation that will later consume it. Rebuilding
/// it invalidates the annexes derived from it; those are rebuilt here
/// only for the options given alongside (`--entropy` the entropy annex,
/// `--quartet` the quartet one, `--pair-balance` the pair-weight and
/// family-overlap ones, `--family-overlap`/`--min-shared-family` the
/// family-overlap one), each under the `--no-ambiguity`/`--only-diploid`
/// convention given — anything not asked for is built later, on demand.
#[arg(long)]
pub sibling_annex: bool,
@@ -526,6 +533,23 @@ pub struct PhyloArgs {
}
impl PhyloArgs {
/// How genomes carrying several members of a family are treated.
pub fn ambiguity(&self) -> AmbiguityPolicy {
AmbiguityPolicy::from_flags(self.no_ambiguity, self.only_diploid)
}
/// The derived annexes the options of this invocation read, all under
/// [`ambiguity`](Self::ambiguity).
pub fn annex_selection(&self) -> AnnexSelection {
AnnexSelection {
policy: self.ambiguity(),
entropy: self.entropy.is_some(),
quartet: self.quartet,
overlap: self.family_overlap || self.min_shared_family.is_some() || self.pair_balance,
pair_weight: self.pair_balance,
}
}
/// Whether a pseudo-alignment must be drawn and written (`--pseudo-alignment`,
/// or any of its output-format flags).
pub fn alignment_output(&self) -> bool {
+4 -5
View File
@@ -85,7 +85,7 @@ pub fn run(args: PhyloArgs) {
// so a concurrent run on the same index can't corrupt these writes.
info!("building sibling-count/minorant annex");
let t = Stage::start("sibling_annex");
cache.build_sibling_annex().unwrap_or_else(|e| {
cache.build_sibling_annex(&args.annex_selection()).unwrap_or_else(|e| {
eprintln!("error building sibling annex: {e}");
std::process::exit(1);
});
@@ -155,7 +155,7 @@ pub fn run(args: PhyloArgs) {
// ── Family Overlap matrix (`--family-overlap`) ──────────────────────────────
if args.family_overlap {
let t = Stage::start("family_overlap");
let overlap = cache.family_overlap(args.no_ambiguity).unwrap_or_else(|e| {
let overlap = cache.family_overlap(args.ambiguity()).unwrap_or_else(|e| {
eprintln!("error computing family overlap: {e}");
std::process::exit(1);
});
@@ -217,7 +217,7 @@ pub fn run(args: PhyloArgs) {
// `args::PhyloArgs::min_shared_family`'s own docs on why).
let snp_exclude_mask: Vec<bool> = match args.min_shared_family {
Some(threshold) => {
let overlap = cache.family_overlap(args.no_ambiguity).unwrap_or_else(|e| {
let overlap = cache.family_overlap(args.ambiguity()).unwrap_or_else(|e| {
eprintln!("error computing family overlap: {e}");
std::process::exit(1);
});
@@ -253,8 +253,7 @@ pub fn run(args: PhyloArgs) {
};
let sampling = SamplingSpec {
free_loss: args.free_loss,
no_ambiguity: args.no_ambiguity,
only_diploid: args.only_diploid,
ambiguity: args.ambiguity(),
quartet: args.quartet,
pair_balance: args.pair_balance,
entropy_bias,