Update Cargo manifests, dependency paths, and Rust imports across the workspace to reflect the `obikpartitionner` to `obikpartition` rename. Synchronize architecture and implementation documentation with the new module structure. Fix minor syntax issues in test assertions to ensure compilation compatibility. No behavioral or API changes are introduced.
145 lines
6.1 KiB
Rust
145 lines
6.1 KiB
Rust
use std::sync::Arc;
|
|
|
|
use obikpartition::KmerPartitions;
|
|
use obisys::progress_bar;
|
|
|
|
use obikindex::KmerIndex;
|
|
use obikindex::{OKIError, OKIResult};
|
|
|
|
use super::cache::PartitionCache;
|
|
use super::family_scan::{Selection, scan_layer_families};
|
|
use super::subsample::EntropyBias;
|
|
|
|
/// IUPAC ambiguity code for a per-genome family presence mask (bit `b` set
|
|
/// iff the genome carries the member whose own central base is `b`):
|
|
/// single bit -> the plain base; 2 or 3 bits -> the matching IUPAC
|
|
/// ambiguity code (preserves partial information instead of collapsing to
|
|
/// `N`, the same convention used for diploid heterozygous VCF/FASTA sites);
|
|
/// all 4 bits -> `N`; no bits (genome carries none of the family's observed
|
|
/// members) -> `-` (no data at this locus for this genome).
|
|
pub(super) fn iupac_code(mask: u8) -> u8 {
|
|
match mask & 0b1111 {
|
|
0b0000 => b'-',
|
|
0b0001 => b'A',
|
|
0b0010 => b'C',
|
|
0b0100 => b'G',
|
|
0b1000 => b'T',
|
|
0b0101 => b'R', // A/G
|
|
0b1010 => b'Y', // C/T
|
|
0b0110 => b'S', // C/G
|
|
0b1001 => b'W', // A/T
|
|
0b1100 => b'K', // G/T
|
|
0b0011 => b'M', // A/C
|
|
0b1110 => b'B', // C/G/T
|
|
0b1101 => b'D', // A/G/T
|
|
0b1011 => b'H', // A/C/T
|
|
0b0111 => b'V', // A/C/G
|
|
0b1111 => b'N',
|
|
_ => unreachable!("masked to 4 bits"),
|
|
}
|
|
}
|
|
|
|
/// A SNP-only pseudo-alignment: one row (byte sequence, IUPAC-coded) per
|
|
/// genome, one column per variable family (`family_size() >= 2` — monomorphic
|
|
/// families carry no signal and are skipped, unlike `raw_snp_distance`'s
|
|
/// tally which does count them as `shared`). Column order is the same,
|
|
/// deterministic sweep order as the annex build (partition, then layer, then
|
|
/// slot) — arbitrary but stable and identical across genomes, which is all a
|
|
/// pseudo-alignment needs (there is no natural genomic coordinate to sort by
|
|
/// once flanks are dropped). See `DevDocMD/theory/evolutionary_distances.md`,
|
|
/// "Multi-genome framing: family as pseudo-alignment column".
|
|
pub struct SnpAlignment {
|
|
/// `sequences[g]` = genome `g`'s IUPAC-coded row, same length for every
|
|
/// genome (`sequences.len()` columns).
|
|
pub sequences: Vec<Vec<u8>>,
|
|
}
|
|
|
|
/// Adds [`snp_pseudo_alignment`](Self::snp_pseudo_alignment) to `KmerIndex` —
|
|
/// phylo-domain functionality, kept out of `obikindex` itself (see
|
|
/// `DevDocMD/architecture/siblings.md`).
|
|
pub trait SnpAlignmentExt {
|
|
/// Build the SNP-only pseudo-alignment from an already-built sibling
|
|
/// annex (run [`build_sibling_annex`](super::build::SiblingAnnexBuildExt::build_sibling_annex) first).
|
|
///
|
|
/// `subsample`: `--subsample N` — cap the number of variable families
|
|
/// retained to (approximately) `N`, sampled proportionally per layer
|
|
/// among non-monomorphic minorants (see
|
|
/// `DevDocMD/architecture/siblings.md`, "`--subsample`/`--shannon`").
|
|
/// `None` keeps today's behaviour: every non-monomorphic minorant of
|
|
/// every layer. `entropy_bias`: `--entropy`/`--entropy-sd` — weight
|
|
/// that sample (or, with `subsample = None`, filter the whole index)
|
|
/// by a Gaussian kernel on each family's entropy — see
|
|
/// `DevDocMD/architecture/siblings.md`, "Entropy-biased selection".
|
|
fn snp_pseudo_alignment(
|
|
&self,
|
|
subsample: Option<usize>,
|
|
entropy_bias: Option<EntropyBias>,
|
|
) -> OKIResult<SnpAlignment>;
|
|
}
|
|
|
|
impl SnpAlignmentExt for KmerIndex {
|
|
fn snp_pseudo_alignment(
|
|
&self,
|
|
subsample: Option<usize>,
|
|
entropy_bias: Option<EntropyBias>,
|
|
) -> OKIResult<SnpAlignment> {
|
|
let n_parts = self.n_partitions();
|
|
let n_genomes = self.meta().genomes.len();
|
|
let with_counts = self.meta().config.with_counts;
|
|
let k = self.kmer_size();
|
|
let n_bits = n_parts.trailing_zeros() as usize;
|
|
|
|
let partition = KmerPartitions::open_with_config(
|
|
self.root_path(),
|
|
self.kmer_size(),
|
|
self.minimizer_size(),
|
|
n_bits,
|
|
)
|
|
.map_err(OKIError::Partition)?;
|
|
let cache = Arc::new(PartitionCache::build(&partition, n_parts, with_counts)?);
|
|
let layer_dirs = super::family_scan::sibling_layer_dirs(self)?;
|
|
let selections =
|
|
super::subsample::compute_selections(self, &layer_dirs, subsample, entropy_bias)?;
|
|
|
|
let pb = progress_bar("snp_pseudo_alignment", layer_dirs.len() as u64, "layers");
|
|
// One layer at a time, not `par_iter()` over layers — same
|
|
// rationale as `build_sibling_annex`: running many layers'
|
|
// `scan_layer_families` concurrently would each group their own
|
|
// lookups by partition internally, but interleave those sweeps
|
|
// across layers at the OS level, scattering page-cache access over
|
|
// every partition at once again and defeating the whole point of
|
|
// the grouping. Columns appended straight into `sequences` as each
|
|
// family comes back from `scan_layer_families` (bounded-batch, not
|
|
// whole-layer) — no intermediate per-layer buffer, layer order
|
|
// giving a single deterministic column order across the whole index.
|
|
let mut sequences: Vec<Vec<u8>> = vec![Vec::new(); n_genomes];
|
|
for (layer_dir, layer_selection) in layer_dirs.iter().zip(selections.iter()) {
|
|
let selection = match layer_selection {
|
|
None => Selection::All,
|
|
Some(set) => Selection::Some(set),
|
|
};
|
|
scan_layer_families(
|
|
layer_dir,
|
|
n_parts,
|
|
n_genomes,
|
|
with_counts,
|
|
k,
|
|
&cache,
|
|
&selection,
|
|
|_family_idx, mask, genome_mask| {
|
|
if mask.family_size() < 2 {
|
|
return; // monomorphic family — no signal, skip
|
|
}
|
|
for (g, &m) in genome_mask.iter().enumerate() {
|
|
sequences[g].push(iupac_code(m));
|
|
}
|
|
},
|
|
)?;
|
|
pb.inc(1);
|
|
}
|
|
pb.finish_and_clear();
|
|
|
|
Ok(SnpAlignment { sequences })
|
|
}
|
|
}
|