Refactor algorithms to use configuration flags

Introduce boolean flags for `AlignmentReducer` and `PairwiseReducer` to control behavior. Update masking logic and introduce the `SurvivingFamily` structure for explicit data handling.
This commit is contained in:
Eric Coissac committed 2026-10-13 08:44:19 +02:00
1 parent cd06fa2ce3
commit 996ebaf977
6 files changed
+114 -124

No files matched your search

+3 -5
View File
@@ -275,11 +275,9 @@ pub fn run(args: PhyloArgs) {
std::process::exit(1); std::process::exit(1);
} }
let mut alignment_reducer = need_alignment.then(|| { let mut alignment_reducer =
obikphylo::siblings::AlignmentReducer::new(&cache, &snp_exclude_mask, args.free_loss, args.no_ambiguity) need_alignment.then(|| obikphylo::siblings::AlignmentReducer::new(&cache, &snp_exclude_mask));
}); 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 { if need_alignment || need_pairwise {
let mut reducers: Vec<&mut dyn obikphylo::siblings::LayerReducer> = Vec::new(); let mut reducers: Vec<&mut dyn obikphylo::siblings::LayerReducer> = Vec::new();
@@ -7,7 +7,7 @@
use obikidxcache::index_cache::IndexCache; use obikidxcache::index_cache::IndexCache;
use rkyv::{Archive, Deserialize, Serialize}; use rkyv::{Archive, Deserialize, Serialize};
use super::masking::{iupac_code, masked_state}; use super::masking::iupac_code;
use super::scan::LayerReducer; use super::scan::LayerReducer;
use super::subsample::SurvivingFamily; use super::subsample::SurvivingFamily;
@@ -34,21 +34,22 @@ pub struct SnpAlignment {
/// it can run alongside any other reducer (e.g. /// it can run alongside any other reducer (e.g.
/// `algorithms::pairwise::PairwiseReducer`) over the exact same batches in /// `algorithms::pairwise::PairwiseReducer`) over the exact same batches in
/// one shared scan, instead of each needing its own `sample_index` call. /// one shared scan, instead of each needing its own `sample_index` call.
/// No `free_loss`/`no_ambiguity` of its own: `SurvivingFamily::masked` has
/// already applied them, once, upstream in `subsample::sample_layer` — see
/// that field's own docs.
pub struct AlignmentReducer { pub struct AlignmentReducer {
genome_indices: Vec<usize>, genome_indices: Vec<usize>,
sequences: Vec<Vec<u8>>, sequences: Vec<Vec<u8>>,
free_loss: bool,
no_ambiguity: bool,
} }
impl AlignmentReducer { impl AlignmentReducer {
pub fn new(cache: &IndexCache, excluded: &[bool], free_loss: bool, no_ambiguity: bool) -> Self { pub fn new(cache: &IndexCache, excluded: &[bool]) -> Self {
let n_genomes = cache.meta().genomes().len(); let n_genomes = cache.meta().genomes().len();
let genome_indices: Vec<usize> = (0..n_genomes) let genome_indices: Vec<usize> = (0..n_genomes)
.filter(|&g| !excluded.get(g).copied().unwrap_or(false)) .filter(|&g| !excluded.get(g).copied().unwrap_or(false))
.collect(); .collect();
let sequences = vec![Vec::new(); genome_indices.len()]; let sequences = vec![Vec::new(); genome_indices.len()];
Self { genome_indices, sequences, free_loss, no_ambiguity } Self { genome_indices, sequences }
} }
pub fn finish(self) -> SnpAlignment { pub fn finish(self) -> SnpAlignment {
@@ -58,25 +59,18 @@ impl AlignmentReducer {
impl LayerReducer for AlignmentReducer { impl LayerReducer for AlignmentReducer {
fn reduce(&mut self, _partition: usize, _layer: usize, survivors: &[SurvivingFamily]) { fn reduce(&mut self, _partition: usize, _layer: usize, survivors: &[SurvivingFamily]) {
reduce_alignment(survivors, &self.genome_indices, self.free_loss, self.no_ambiguity, &mut self.sequences); reduce_alignment(survivors, &self.genome_indices, &mut self.sequences);
} }
} }
/// Appends one layer's worth of columns onto `sequences` — self-contained /// Appends one layer's worth of columns onto `sequences` — self-contained
/// (no sampling/masking logic of its own, just character encoding), the /// (no sampling/masking logic of its own, just character encoding over
/// actual reduce [`AlignmentReducer`] wraps. /// `family.masked`, already computed upstream), the actual reduce
fn reduce_alignment( /// [`AlignmentReducer`] wraps.
survivors: &[SurvivingFamily], fn reduce_alignment(survivors: &[SurvivingFamily], genome_indices: &[usize], sequences: &mut [Vec<u8>]) {
genome_indices: &[usize],
free_loss: bool,
no_ambiguity: bool,
sequences: &mut [Vec<u8>],
) {
for family in survivors { for family in survivors {
for (row, &g) in genome_indices.iter().enumerate() { for (row, &g) in genome_indices.iter().enumerate() {
let ch = masked_state(family.genome_mask[g], free_loss, no_ambiguity) let ch = family.masked[g].map(iupac_code).unwrap_or(b'?');
.map(iupac_code)
.unwrap_or(b'?');
sequences[row].push(ch); sequences[row].push(ch);
} }
} }
@@ -33,28 +33,31 @@ pub(crate) fn masked_state(m: u8, free_loss: bool, no_ambiguity: bool) -> Option
Some(m) Some(m)
} }
/// Whether a family's resolved `genome_mask` still carries real /// Whether a family's already-[`masked_state`]d per-genome states still
/// polymorphism (`>=2` distinct masked states) once [`masked_state`] is /// carry real polymorphism (`>=2` distinct states) among *included*
/// applied to every *included* genome — i.e. whether it remains a usable /// genomes — i.e. whether it remains a usable alignment site rather than
/// alignment site rather than one that collapsed to monomorphic (or fully /// one that collapsed to monomorphic (or fully missing) once uninformative
/// missing) once uninformative states, and excluded genomes entirely, are /// states, and excluded genomes entirely, are dropped from consideration.
/// dropped from consideration. `excluded[g]` (`false` if `g` is beyond /// Takes `masked` — one [`masked_state`] result per genome — rather than
/// `excluded`'s own length — a caller that never excludes anyone can pass /// the raw `genome_mask` and the masking flags themselves: the caller
/// an empty slice) means genome `g` is skipped outright, not merely masked /// (`subsample::sample_layer`) computes `masked_state` exactly once per
/// to "?": it never contributes to `seen`, whether or not `free_loss`/ /// (family, genome) and reuses that same array for this check, for
/// `no_ambiguity` would also have masked it. /// building the eventual alignment column, and for folding the pairwise
pub(crate) fn survives_masking( /// tally, instead of every consumer recomputing it independently (which
genome_mask: &[u8], /// used to let their masking semantics silently drift apart — see
excluded: &[bool], /// `algorithms::pairwise::reduce_pairwise`'s own docs for the bug that
free_loss: bool, /// caused). `excluded[g]` (`false` if `g` is beyond `excluded`'s own
no_ambiguity: bool, /// length — a caller that never excludes anyone can pass an empty slice)
) -> bool { /// means genome `g` is skipped outright, not merely masked to "?": it
/// never contributes to `seen`, whether or not `masked[g]` is already
/// `None`.
pub(crate) fn survives_masking(masked: &[Option<u8>], excluded: &[bool]) -> bool {
let mut seen: u16 = 0; let mut seen: u16 = 0;
for (g, &m) in genome_mask.iter().enumerate() { for (g, &state) in masked.iter().enumerate() {
if excluded.get(g).copied().unwrap_or(false) { if excluded.get(g).copied().unwrap_or(false) {
continue; continue;
} }
if let Some(state) = masked_state(m, free_loss, no_ambiguity) { if let Some(state) = state {
seen |= 1 << state; seen |= 1 << state;
} }
} }
@@ -96,18 +99,25 @@ pub(crate) fn iupac_code(state: u8) -> u8 {
mod tests { mod tests {
use super::*; use super::*;
/// One [`masked_state`] call per raw mask — mirrors what
/// `subsample::sample_layer` does once, up front, before calling
/// [`survives_masking`].
fn masked(raw: &[u8], free_loss: bool, no_ambiguity: bool) -> Vec<Option<u8>> {
raw.iter().map(|&m| masked_state(m, free_loss, no_ambiguity)).collect()
}
#[test] #[test]
fn plain_no_masking_counts_absence_as_its_own_state() { fn plain_no_masking_counts_absence_as_its_own_state() {
// Two genomes: one absent (0), one carrying base A (1) — 2 distinct // Two genomes: one absent (0), one carrying base A (1) — 2 distinct
// states without free-loss, so this survives. // states without free-loss, so this survives.
assert!(survives_masking(&[0, 1], &[], false, false)); assert!(survives_masking(&masked(&[0, 1], false, false), &[]));
} }
#[test] #[test]
fn free_loss_drops_absence_and_can_collapse_to_monomorphic() { fn free_loss_drops_absence_and_can_collapse_to_monomorphic() {
// Same data as above, but with free-loss the absent genome becomes // Same data as above, but with free-loss the absent genome becomes
// "?" (excluded) — only one real state (A) remains. // "?" (excluded) — only one real state (A) remains.
assert!(!survives_masking(&[0, 1], &[], true, false)); assert!(!survives_masking(&masked(&[0, 1], true, false), &[]));
} }
#[test] #[test]
@@ -116,8 +126,8 @@ mod tests {
// carries only A. Without no-ambiguity: 2 distinct states (0b0011, // carries only A. Without no-ambiguity: 2 distinct states (0b0011,
// 0b0001) -> survives. With no-ambiguity: genome 0 becomes "?", // 0b0001) -> survives. With no-ambiguity: genome 0 becomes "?",
// only one real state left -> collapses. // only one real state left -> collapses.
assert!(survives_masking(&[0b0011, 0b0001], &[], false, false)); assert!(survives_masking(&masked(&[0b0011, 0b0001], false, false), &[]));
assert!(!survives_masking(&[0b0011, 0b0001], &[], false, true)); assert!(!survives_masking(&masked(&[0b0011, 0b0001], false, true), &[]));
} }
#[test] #[test]
@@ -126,7 +136,8 @@ mod tests {
// this survives (2 distinct states, matching the plain test above). // this survives (2 distinct states, matching the plain test above).
// Excluding genome 1 (the only real signal) leaves just genome 0's // Excluding genome 1 (the only real signal) leaves just genome 0's
// absence — a single state, collapses. // absence — a single state, collapses.
assert!(survives_masking(&[0, 1], &[false, false], false, false)); let m = masked(&[0, 1], false, false);
assert!(!survives_masking(&[0, 1], &[false, true], false, false)); assert!(survives_masking(&m, &[false, false]));
assert!(!survives_masking(&m, &[false, true]));
} }
} }
@@ -407,29 +407,23 @@ impl PartitionDispersion {
/// own docs for why the partition axis must be captured here rather than /// own docs for why the partition axis must be captured here rather than
/// after the fact) — the "reduce" half of the map/reduce /// after the fact) — the "reduce" half of the map/reduce
/// [`super::subsample::sample_index`] drives, self-contained (no /// [`super::subsample::sample_index`] drives, self-contained (no
/// sampling logic of its own) so it can run alongside any other reduce /// sampling/masking logic of its own) so it can run alongside any other
/// over the same batch (see `algorithms::alignment::reduce_alignment`, /// reduce over the same batch (see `algorithms::alignment::reduce_alignment`,
/// run alongside this one whenever both an alignment and a tally are /// run alongside this one whenever both an alignment and a tally are
/// wanted from the same [`super::scan::sampled_scan`] call). /// wanted from the same [`super::scan::sampled_scan`] call).
/// ///
/// `free_loss`/`no_ambiguity`: the same masking /// Reads `family.masked` directly — [`super::subsample::SurvivingFamily`]'s
/// [`algorithms::alignment::reduce_alignment`] applies per genome via /// own docs explain why that's already the masked (`--free-loss`/
/// `masking::masked_state`, applied here too so the tally agrees with /// `--no-ambiguity`-applied) per-genome state, computed once upstream, the
/// what the alignment actually shows for the same family/genome — used /// same array [`algorithms::alignment::reduce_alignment`] reads too: this
/// directly for `cardinality` (a masked-away genome contributes `0`, /// is what guarantees `cardinality` agrees with what the alignment
/// matching its `?` in the alignment, not its raw, pre-masking bit /// actually shows for the same family/genome (a masked-away genome
/// count). `subst`/`same_all`/`same_variable` don't need to look at /// contributes `0`, matching its `?`). `subst`/`same_all`/`same_variable`
/// `masked_state` separately: `single_form`'s own "exactly one bit" /// don't need anything beyond `single_form`'s "exactly one bit" check on
/// requirement already agrees with "masked, then exactly one bit" for /// the already-masked state.
/// 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( pub(crate) fn reduce_pairwise(
survivors: &[SurvivingFamily], survivors: &[SurvivingFamily],
partition: usize, partition: usize,
free_loss: bool,
no_ambiguity: bool,
tally: &mut PairwiseTally, tally: &mut PairwiseTally,
dispersion: &mut PartitionDispersion, dispersion: &mut PartitionDispersion,
) { ) {
@@ -438,15 +432,10 @@ pub(crate) fn reduce_pairwise(
let mut partition_eligible: u64 = 0; let mut partition_eligible: u64 = 0;
for family in survivors { for family in survivors {
let variable = family.mask.family_size() >= 2; let variable = family.mask.family_size() >= 2;
let genome_mask = &family.genome_mask;
let single_form = |g: usize| -> Option<u8> { let single_form = |g: usize| -> Option<u8> {
let m = genome_mask[g]; family.masked[g].filter(|m| m.count_ones() == 1).map(|m| m.trailing_zeros() as u8)
(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)
}; };
let masked_cardinality = |g: usize| -> usize { family.masked[g].map_or(0, |m| m.count_ones() as usize) };
for i in 0..n_genomes { for i in 0..n_genomes {
let bi = single_form(i); let bi = single_form(i);
@@ -491,23 +480,14 @@ pub(crate) fn reduce_pairwise(
pub struct PairwiseReducer { pub struct PairwiseReducer {
tally: PairwiseTally, tally: PairwiseTally,
dispersion: PartitionDispersion, dispersion: PartitionDispersion,
free_loss: bool,
no_ambiguity: bool,
} }
impl PairwiseReducer { impl PairwiseReducer {
/// `free_loss`/`no_ambiguity`: the same masking flags passed to /// No `free_loss`/`no_ambiguity` of its own: `SurvivingFamily::masked`
/// `AlignmentReducer::new` — must match when both run over the same /// has already applied them, once, upstream in
/// [`super::scan::sampled_scan`] call, so the cardinality tally agrees /// `subsample::sample_layer` — see that field's own docs.
/// with what the alignment actually shows (see [`reduce_pairwise`]'s pub fn new(n_genomes: usize) -> Self {
/// own docs). Self { tally: PairwiseTally::new(n_genomes), dispersion: PartitionDispersion::default() }
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) { pub fn finish(self) -> (PairwiseTally, PartitionDispersion) {
@@ -517,14 +497,7 @@ impl PairwiseReducer {
impl super::scan::LayerReducer for PairwiseReducer { impl super::scan::LayerReducer for PairwiseReducer {
fn reduce(&mut self, partition: usize, _layer: usize, survivors: &[SurvivingFamily]) { fn reduce(&mut self, partition: usize, _layer: usize, survivors: &[SurvivingFamily]) {
reduce_pairwise( reduce_pairwise(survivors, partition, &mut self.tally, &mut self.dispersion);
survivors,
partition,
self.free_loss,
self.no_ambiguity,
&mut self.tally,
&mut self.dispersion,
);
} }
} }
@@ -537,20 +510,20 @@ mod cardinality_masking_tests {
// Genome 0 is ambiguous (carries both A and C, mask 0b0011 = cardinality // Genome 0 is ambiguous (carries both A and C, mask 0b0011 = cardinality
// 2); genome 1 carries only A (mask 0b0001 = cardinality 1). The family // 2); genome 1 carries only A (mask 0b0001 = cardinality 1). The family
// is variable (both A and C are observed somewhere), so it contributes // is variable (both A and C are observed somewhere), so it contributes
// to `cardinality` regardless of masking. // to `cardinality` regardless of masking. `masked` is what
fn ambiguous_vs_single() -> Vec<SurvivingFamily> { // `subsample::sample_layer` would have computed upstream (see
vec![SurvivingFamily { // `SurvivingFamily::masked`'s own docs) — the masking decision is baked
family_idx: 0, // in here, not passed to `reduce_pairwise` itself anymore.
mask: FamilyMask::EMPTY.with(0).with(1), // A, C observed fn ambiguous_vs_single(masked: Vec<Option<u8>>) -> Vec<SurvivingFamily> {
genome_mask: vec![0b0011, 0b0001], vec![SurvivingFamily { family_idx: 0, mask: FamilyMask::EMPTY.with(0).with(1), masked }]
}]
} }
#[test] #[test]
fn cardinality_uses_raw_mask_without_no_ambiguity() { fn cardinality_uses_raw_mask_without_no_ambiguity() {
let mut tally = PairwiseTally::new(2); let mut tally = PairwiseTally::new(2);
let mut dispersion = PartitionDispersion::default(); let mut dispersion = PartitionDispersion::default();
reduce_pairwise(&ambiguous_vs_single(), 0, false, false, &mut tally, &mut dispersion); let families = ambiguous_vs_single(vec![Some(0b0011), Some(0b0001)]);
reduce_pairwise(&families, 0, &mut tally, &mut dispersion);
let counts = tally.cardinality_tally(&[]).counts; let counts = tally.cardinality_tally(&[]).counts;
assert_eq!(counts[2][1], 1, "genome 0's raw cardinality (2) should be tallied"); 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"); assert_eq!(counts[0][1], 0, "no masking requested — nothing should collapse to 0");
@@ -558,12 +531,14 @@ mod cardinality_masking_tests {
#[test] #[test]
fn cardinality_masks_ambiguous_genome_to_zero_under_no_ambiguity() { fn cardinality_masks_ambiguous_genome_to_zero_under_no_ambiguity() {
// Matches what `AlignmentReducer`/`masked_state` would show for // `masked[0] == None` matches what `sample_layer` would have
// genome 0 at this site under `--no-ambiguity`: `?`, i.e. no // computed for genome 0 at this site under `--no-ambiguity`: `?`
// information — cardinality 0, not its raw 2. // in the alignment, i.e. no information — cardinality 0, not its
// raw 2.
let mut tally = PairwiseTally::new(2); let mut tally = PairwiseTally::new(2);
let mut dispersion = PartitionDispersion::default(); let mut dispersion = PartitionDispersion::default();
reduce_pairwise(&ambiguous_vs_single(), 0, false, true, &mut tally, &mut dispersion); let families = ambiguous_vs_single(vec![None, Some(0b0001)]);
reduce_pairwise(&families, 0, &mut tally, &mut dispersion);
let counts = tally.cardinality_tally(&[]).counts; let counts = tally.cardinality_tally(&[]).counts;
assert_eq!(counts[2][1], 0, "raw cardinality must not leak through under --no-ambiguity"); 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"); assert_eq!(counts[0][1], 1, "the ambiguous genome should be masked to cardinality 0");
@@ -278,12 +278,16 @@ mod tests {
/// single-base composition, an edge case irrelevant to real data, not /// single-base composition, an edge case irrelevant to real data, not
/// something these tests need to exercise). /// something these tests need to exercise).
fn tally_2genomes(pairs: &[(u8, u8)], n_shared: usize) -> PairwiseTally { fn tally_2genomes(pairs: &[(u8, u8)], n_shared: usize) -> PairwiseTally {
// No masking requested (`free_loss = no_ambiguity = false`) —
// `masked_state(m, false, false)` is always `Some(m)`, so the raw
// mask is exactly what `subsample::sample_layer` would have
// computed here.
let mut families = Vec::new(); let mut families = Vec::new();
for &(a, b) in pairs { for &(a, b) in pairs {
families.push(SurvivingFamily { families.push(SurvivingFamily {
family_idx: families.len(), family_idx: families.len(),
mask: FamilyMask::EMPTY.with(a.trailing_zeros() as u8).with(b.trailing_zeros() as u8), mask: FamilyMask::EMPTY.with(a.trailing_zeros() as u8).with(b.trailing_zeros() as u8),
genome_mask: vec![a, b], masked: vec![Some(a), Some(b)],
}); });
} }
const BASES: [u8; 4] = [A, C, G, T]; const BASES: [u8; 4] = [A, C, G, T];
@@ -293,12 +297,12 @@ mod tests {
families.push(SurvivingFamily { families.push(SurvivingFamily {
family_idx: families.len(), family_idx: families.len(),
mask: FamilyMask::EMPTY.with(b.trailing_zeros() as u8).with(other.trailing_zeros() as u8), mask: FamilyMask::EMPTY.with(b.trailing_zeros() as u8).with(other.trailing_zeros() as u8),
genome_mask: vec![b, b], masked: vec![Some(b), Some(b)],
}); });
} }
let mut tally = PairwiseTally::new(2); let mut tally = PairwiseTally::new(2);
let mut dispersion = PartitionDispersion::default(); let mut dispersion = PartitionDispersion::default();
reduce_pairwise(&families, 0, false, false, &mut tally, &mut dispersion); reduce_pairwise(&families, 0, &mut tally, &mut dispersion);
tally tally
} }
@@ -54,7 +54,7 @@ use rand::RngExt;
use super::entropy::iter_full_entropy; use super::entropy::iter_full_entropy;
use super::family_scan::{Selection, scan_layer_families}; use super::family_scan::{Selection, scan_layer_families};
use super::is_fast_mode; use super::is_fast_mode;
use super::masking::survives_masking; use super::masking::{masked_state, survives_masking};
use super::minorant_selection::eligible_bitset_layer; use super::minorant_selection::eligible_bitset_layer;
use crate::siblings::FamilyMask; use crate::siblings::FamilyMask;
use crate::siblings::extensions::SiblingBuilder; use crate::siblings::extensions::SiblingBuilder;
@@ -66,15 +66,24 @@ const MAX_TOPUP_ROUNDS: usize = 5;
/// One surviving family — already drawn (Bernoulli/entropy-bias), already /// One surviving family — already drawn (Bernoulli/entropy-bias), already
/// resolved (`scan_layer_families`), already passed post-hoc masking /// resolved (`scan_layer_families`), already passed post-hoc masking
/// (`super::masking::survives_masking`). `genome_mask` is an owned copy: the /// (`super::masking::survives_masking`). `masked[g]` is genome `g`'s
/// borrowed slice `scan_layer_families` hands its own callback doesn't /// [`super::masking::masked_state`] result — computed exactly once, here,
/// outlive that callback, but a `SurvivingFamily` is meant to be handed to /// from the raw (per-family, per-genome) resolved state, *before*
/// [`sample_index`]'s caller as part of a whole layer's batch, well after /// `--free-loss`/`--no-ambiguity` are applied — never the raw state
/// that callback returns. /// itself: every consumer downstream (this family's own survival check,
/// its eventual alignment column, its contribution to the pairwise tally)
/// needs the masked view, and computing it once and sharing it is what
/// guarantees they can never disagree about what a genome's state was at
/// this site (see `algorithms::pairwise::reduce_pairwise`'s own docs for
/// the inconsistency that arose from each consumer masking independently).
/// An owned `Vec`: the borrowed slice `scan_layer_families` hands its own
/// callback doesn't outlive that callback, but a `SurvivingFamily` is meant
/// to be handed to [`sample_index`]'s caller as part of a whole layer's
/// batch, well after that callback returns.
pub struct SurvivingFamily { pub struct SurvivingFamily {
pub family_idx: usize, pub family_idx: usize,
pub mask: FamilyMask, pub mask: FamilyMask,
pub genome_mask: Vec<u8>, pub masked: Vec<Option<u8>>,
} }
/// Gaussian-kernel entropy bias parameters — `mu`/`sigma` are the /// Gaussian-kernel entropy bias parameters — `mu`/`sigma` are the
@@ -111,9 +120,10 @@ fn circular_entropy_values(
/// [`SurvivingFamily`] this produces is already a usable alignment site, no /// [`SurvivingFamily`] this produces is already a usable alignment site, no
/// second resolution pass needed by the caller. `excluded[g]` (see /// second resolution pass needed by the caller. `excluded[g]` (see
/// `survives_masking`'s own docs) never counts genome `g` toward that check /// `survives_masking`'s own docs) never counts genome `g` toward that check
/// — `genome_mask` itself still carries every genome's own resolved state, /// — `SurvivingFamily::masked` itself still carries every genome's own
/// excluded or not, so a caller building per-genome output (e.g. an /// masked state, excluded or not, so a caller building per-genome output
/// alignment row) must apply the same exclusion itself when consuming it. /// (e.g. an alignment row) must apply the same exclusion itself when
/// consuming it.
/// ///
/// `survivors` is a single `Arc<Vec<SurvivingFamily>>` — one allocation per /// `survivors` is a single `Arc<Vec<SurvivingFamily>>` — one allocation per
/// layer (bounded by that layer's own quota, never the whole index), handed /// layer (bounded by that layer's own quota, never the whole index), handed
@@ -348,12 +358,10 @@ fn sample_layer(
mask.family_size() >= 2, mask.family_size() >= 2,
"eligible bitset must only accept non-monomorphic minorants" "eligible bitset must only accept non-monomorphic minorants"
); );
if survives_masking(genome_mask, excluded, free_loss, no_ambiguity) { let masked: Vec<Option<u8>> =
survivors.push(SurvivingFamily { genome_mask.iter().map(|&m| masked_state(m, free_loss, no_ambiguity)).collect();
family_idx, if survives_masking(&masked, excluded) {
mask, survivors.push(SurvivingFamily { family_idx, mask, masked });
genome_mask: genome_mask.to_vec(),
});
kept += 1; kept += 1;
} }
}, },