diff --git a/src/obikmer/src/cmd/phylo/mod.rs b/src/obikmer/src/cmd/phylo/mod.rs index e77ffa98..94b179e0 100644 --- a/src/obikmer/src/cmd/phylo/mod.rs +++ b/src/obikmer/src/cmd/phylo/mod.rs @@ -278,7 +278,8 @@ pub fn run(args: PhyloArgs) { let mut alignment_reducer = need_alignment.then(|| { obikphylo::siblings::AlignmentReducer::new(&cache, &snp_exclude_mask, args.free_loss, args.no_ambiguity) }); - let mut pairwise_reducer = need_pairwise.then(|| obikphylo::siblings::PairwiseReducer::new(n)); + let mut pairwise_reducer = + need_pairwise.then(|| obikphylo::siblings::PairwiseReducer::new(n, args.free_loss, args.no_ambiguity)); if need_alignment || need_pairwise { let mut reducers: Vec<&mut dyn obikphylo::siblings::LayerReducer> = Vec::new(); diff --git a/src/obikphylo/src/siblings/algorithms/pairwise.rs b/src/obikphylo/src/siblings/algorithms/pairwise.rs index e5934b3e..db738bd5 100644 --- a/src/obikphylo/src/siblings/algorithms/pairwise.rs +++ b/src/obikphylo/src/siblings/algorithms/pairwise.rs @@ -407,13 +407,29 @@ impl PartitionDispersion { /// own docs for why the partition axis must be captured here rather than /// after the fact) — the "reduce" half of the map/reduce /// [`super::subsample::sample_index`] drives, self-contained (no -/// sampling/masking logic of its own) so it can run alongside any other -/// reduce over the same batch (see `algorithms::alignment::reduce_alignment`, -/// called from the same `on_layer` closure in -/// `algorithms::sankoff::sankoff_bundle`). +/// sampling logic of its own) so it can run alongside any other reduce +/// over the same batch (see `algorithms::alignment::reduce_alignment`, +/// run alongside this one whenever both an alignment and a tally are +/// wanted from the same [`super::scan::sampled_scan`] call). +/// +/// `free_loss`/`no_ambiguity`: the same masking +/// [`algorithms::alignment::reduce_alignment`] applies per genome via +/// `masking::masked_state`, applied here too so the tally agrees with +/// what the alignment actually shows for the same family/genome — used +/// directly for `cardinality` (a masked-away genome contributes `0`, +/// matching its `?` in the alignment, not its raw, pre-masking bit +/// count). `subst`/`same_all`/`same_variable` don't need to look at +/// `masked_state` separately: `single_form`'s own "exactly one bit" +/// requirement already agrees with "masked, then exactly one bit" for +/// every possible mask, for any combination of these two flags (masking +/// only ever turns an already-`None` case — 0 bits or ≥2 bits — into a +/// differently-shaped `None`, never a `Some` into a `None` or vice +/// versa), so routing them through `masked_state` too would be a no-op. pub(crate) fn reduce_pairwise( survivors: &[SurvivingFamily], partition: usize, + free_loss: bool, + no_ambiguity: bool, tally: &mut PairwiseTally, dispersion: &mut PartitionDispersion, ) { @@ -427,10 +443,14 @@ pub(crate) fn reduce_pairwise( let m = genome_mask[g]; (m.count_ones() == 1).then(|| m.trailing_zeros() as u8) }; + let masked_cardinality = |g: usize| -> usize { + super::masking::masked_state(genome_mask[g], free_loss, no_ambiguity) + .map_or(0, |m| m.count_ones() as usize) + }; for i in 0..n_genomes { let bi = single_form(i); - let card_i = genome_mask[i].count_ones() as usize; + let card_i = masked_cardinality(i); for j in (i + 1)..n_genomes { let stats = tally.pair_mut(i, j); @@ -449,7 +469,7 @@ pub(crate) fn reduce_pairwise( } if variable { - let card_j = genome_mask[j].count_ones() as usize; + let card_j = masked_cardinality(j); stats.cardinality[card_i][card_j] += 1; if card_i != card_j { stats.cardinality[card_j][card_i] += 1; @@ -471,11 +491,23 @@ pub(crate) fn reduce_pairwise( pub struct PairwiseReducer { tally: PairwiseTally, dispersion: PartitionDispersion, + free_loss: bool, + no_ambiguity: bool, } impl PairwiseReducer { - pub fn new(n_genomes: usize) -> Self { - Self { tally: PairwiseTally::new(n_genomes), dispersion: PartitionDispersion::default() } + /// `free_loss`/`no_ambiguity`: the same masking flags passed to + /// `AlignmentReducer::new` — must match when both run over the same + /// [`super::scan::sampled_scan`] call, so the cardinality tally agrees + /// with what the alignment actually shows (see [`reduce_pairwise`]'s + /// own docs). + pub fn new(n_genomes: usize, free_loss: bool, no_ambiguity: bool) -> Self { + Self { + tally: PairwiseTally::new(n_genomes), + dispersion: PartitionDispersion::default(), + free_loss, + no_ambiguity, + } } pub fn finish(self) -> (PairwiseTally, PartitionDispersion) { @@ -485,7 +517,56 @@ impl PairwiseReducer { impl super::scan::LayerReducer for PairwiseReducer { fn reduce(&mut self, partition: usize, _layer: usize, survivors: &[SurvivingFamily]) { - reduce_pairwise(survivors, partition, &mut self.tally, &mut self.dispersion); + reduce_pairwise( + survivors, + partition, + self.free_loss, + self.no_ambiguity, + &mut self.tally, + &mut self.dispersion, + ); + } +} + +#[cfg(test)] +mod cardinality_masking_tests { + use super::{PairwiseTally, PartitionDispersion, reduce_pairwise}; + use crate::siblings::FamilyMask; + use crate::siblings::algorithms::SurvivingFamily; + + // Genome 0 is ambiguous (carries both A and C, mask 0b0011 = cardinality + // 2); genome 1 carries only A (mask 0b0001 = cardinality 1). The family + // is variable (both A and C are observed somewhere), so it contributes + // to `cardinality` regardless of masking. + fn ambiguous_vs_single() -> Vec { + vec![SurvivingFamily { + family_idx: 0, + mask: FamilyMask::EMPTY.with(0).with(1), // A, C observed + genome_mask: vec![0b0011, 0b0001], + }] + } + + #[test] + fn cardinality_uses_raw_mask_without_no_ambiguity() { + let mut tally = PairwiseTally::new(2); + let mut dispersion = PartitionDispersion::default(); + reduce_pairwise(&ambiguous_vs_single(), 0, false, false, &mut tally, &mut dispersion); + let counts = tally.cardinality_tally(&[]).counts; + assert_eq!(counts[2][1], 1, "genome 0's raw cardinality (2) should be tallied"); + assert_eq!(counts[0][1], 0, "no masking requested — nothing should collapse to 0"); + } + + #[test] + fn cardinality_masks_ambiguous_genome_to_zero_under_no_ambiguity() { + // Matches what `AlignmentReducer`/`masked_state` would show for + // genome 0 at this site under `--no-ambiguity`: `?`, i.e. no + // information — cardinality 0, not its raw 2. + let mut tally = PairwiseTally::new(2); + let mut dispersion = PartitionDispersion::default(); + reduce_pairwise(&ambiguous_vs_single(), 0, false, true, &mut tally, &mut dispersion); + let counts = tally.cardinality_tally(&[]).counts; + assert_eq!(counts[2][1], 0, "raw cardinality must not leak through under --no-ambiguity"); + assert_eq!(counts[0][1], 1, "the ambiguous genome should be masked to cardinality 0"); } } diff --git a/src/obikphylo/src/siblings/algorithms/snp_distance.rs b/src/obikphylo/src/siblings/algorithms/snp_distance.rs index 8a6528e4..099ff3c8 100644 --- a/src/obikphylo/src/siblings/algorithms/snp_distance.rs +++ b/src/obikphylo/src/siblings/algorithms/snp_distance.rs @@ -298,7 +298,7 @@ mod tests { } let mut tally = PairwiseTally::new(2); let mut dispersion = PartitionDispersion::default(); - reduce_pairwise(&families, 0, &mut tally, &mut dispersion); + reduce_pairwise(&families, 0, false, false, &mut tally, &mut dispersion); tally }