Add proportional subsampling and Shannon entropy calculation
Introduces `--subsample N` and `--shannon` CLI flags to cap retained variable families via proportional reservoir sampling and compute per-family Shannon entropy. Updates the family scanning API to support explicit selection filtering with early-exit optimization, resolving an indexing drift issue in monomorphic layers. Streams entropy metrics for 15-state and 4-nucleotide spaces directly to CSV while maintaining parallel processing across sibling layers.
This commit is contained in:
1 parent
f5e4bbfc6b
commit
45b19503a1
17 files changed
+841
-70
No files matched your search
@@ -146,6 +146,28 @@ pub struct PhyloArgs {
|
||||
#[arg(long)]
|
||||
pub snp: bool,
|
||||
|
||||
/// Cap the number of variable families (non-monomorphic minorants,
|
||||
/// `family_size() >= 2`) retained by `--snp`/`--sankoff` (and everything
|
||||
/// `--sankoff` implies) and `--shannon`, to (approximately) this many —
|
||||
/// sampled proportionally per layer, so the pseudo-alignment/entropy
|
||||
/// report stays usable on an index far larger than the sample itself
|
||||
/// (mandatory, not optional, once the index is large enough that a full
|
||||
/// pseudo-alignment can't be materialized at all). See
|
||||
/// `docmd/architecture/siblings.md`, "`--subsample`/`--shannon`". If the
|
||||
/// index has fewer non-monomorphic minorants than this, every one of
|
||||
/// them is kept — no error, no under/over-shoot handling needed.
|
||||
#[arg(long, value_name = "N")]
|
||||
pub subsample: Option<usize>,
|
||||
|
||||
/// Write <prefix>_shannon.csv: per-family Shannon entropy (bits, over
|
||||
/// the 15 non-empty subsets of `{A,C,G,T}`, `∅`/absent genomes excluded
|
||||
/// from the denominator — see `docmd/architecture/siblings.md`,
|
||||
/// "Entropy definition") of every non-monomorphic minorant, one row per
|
||||
/// family. Combine with `--subsample N` for a bounded diagnostic sample
|
||||
/// instead of a full-index pass. Same annex requirement as `--snp`.
|
||||
#[arg(long)]
|
||||
pub shannon: bool,
|
||||
|
||||
/// Write an NxN CSV (`<prefix>_family_overlap.csv`) of, for each genome
|
||||
/// pair, how many variable families (same set `--snp`'s pseudo-alignment
|
||||
/// uses — `family_size() >= 2`) both genomes actually carry a call for
|
||||
@@ -244,7 +266,7 @@ pub struct PhyloArgs {
|
||||
/// Output prefix: <prefix>_dist.csv, <prefix>_shared.csv,
|
||||
/// <prefix>_siblings.csv, <prefix>_sibling_hist.csv, <prefix>_rawsnp.csv,
|
||||
/// <prefix>_rawsnp_counts.csv,
|
||||
/// <prefix>_snp.fasta, <prefix>_family_overlap.csv,
|
||||
/// <prefix>_snp.fasta, <prefix>_family_overlap.csv, <prefix>_shannon.csv,
|
||||
/// <prefix>_sankoff_matrix.csv, <prefix>_sankoff_params.yaml,
|
||||
/// <prefix>_sankoff.fasta, <prefix>_sankoff.tnt, <prefix>_sankoff.tcm,
|
||||
/// <prefix>_sankoff.pg, <prefix>_iqtree.model, <prefix>_iqtree.fasta,
|
||||
|
||||
@@ -80,7 +80,7 @@ pub(super) fn apply_min_shared_family_exclusion(
|
||||
threshold: f64,
|
||||
mask: &mut [bool],
|
||||
) {
|
||||
let alignment = idx.snp_pseudo_alignment().unwrap_or_else(|e| {
|
||||
let alignment = idx.snp_pseudo_alignment(None).unwrap_or_else(|e| {
|
||||
eprintln!("error computing SNP pseudo-alignment for --min-shared-family: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
@@ -13,8 +13,8 @@ use obikindex::KmerIndex;
|
||||
use obikphylo::{
|
||||
cardinality_transition_probs, composition_transition_probs, pairwise_cost_matrix,
|
||||
siblings::{
|
||||
CardinalityExt, DistanceExt, RawSnpDistanceOutput, SiblingAnnexBuildExt, SiblingStatsExt,
|
||||
SnpAlignment, SnpAlignmentExt,
|
||||
CardinalityExt, DistanceExt, RawSnpDistanceOutput, ShannonEntropyExt, SiblingAnnexBuildExt,
|
||||
SiblingStatsExt, SnpAlignment, SnpAlignmentExt,
|
||||
},
|
||||
};
|
||||
use obisys::{Reporter, Stage};
|
||||
@@ -177,7 +177,7 @@ pub fn run(args: PhyloArgs) {
|
||||
}
|
||||
if args.snp {
|
||||
let t = Stage::start("snp_pseudo_alignment");
|
||||
let alignment = idx.snp_pseudo_alignment().unwrap_or_else(|e| {
|
||||
let alignment = idx.snp_pseudo_alignment(args.subsample).unwrap_or_else(|e| {
|
||||
eprintln!("error computing SNP pseudo-alignment: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
@@ -187,7 +187,7 @@ pub fn run(args: PhyloArgs) {
|
||||
}
|
||||
if args.family_overlap {
|
||||
let t = Stage::start("snp_pseudo_alignment");
|
||||
let alignment = idx.snp_pseudo_alignment().unwrap_or_else(|e| {
|
||||
let alignment = idx.snp_pseudo_alignment(args.subsample).unwrap_or_else(|e| {
|
||||
eprintln!("error computing SNP pseudo-alignment: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
@@ -195,6 +195,18 @@ pub fn run(args: PhyloArgs) {
|
||||
let (alignment, kept_labels) = drop_excluded(alignment);
|
||||
write_family_overlap_csv(&alignment, &kept_labels, &args.output);
|
||||
}
|
||||
if args.shannon {
|
||||
let t = Stage::start("shannon_entropy");
|
||||
let path = args.output.as_ref()
|
||||
.map(|p| format!("{}_shannon.csv", p.display()))
|
||||
.unwrap_or_else(|| "shannon.csv".into());
|
||||
idx.shannon_entropy_csv(std::path::Path::new(&path), args.subsample).unwrap_or_else(|e| {
|
||||
eprintln!("error computing Shannon entropy: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
rep.push(t.stop());
|
||||
info!("per-family Shannon entropy → {path}");
|
||||
}
|
||||
if args.sankoff || args.tnt || args.phyg || args.iqtree {
|
||||
let t = Stage::start("raw_snp_distance");
|
||||
let mut raw = idx.raw_snp_distance().unwrap_or_else(|e| {
|
||||
@@ -223,7 +235,7 @@ pub fn run(args: PhyloArgs) {
|
||||
write_sankoff_params(&card_tally, &p_card, &base_tally, &p_comp, args.sankoff_ratio_ceiling, &args.output);
|
||||
|
||||
let t = Stage::start("snp_pseudo_alignment");
|
||||
let alignment = idx.snp_pseudo_alignment().unwrap_or_else(|e| {
|
||||
let alignment = idx.snp_pseudo_alignment(args.subsample).unwrap_or_else(|e| {
|
||||
eprintln!("error computing SNP pseudo-alignment: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
@@ -256,6 +268,7 @@ pub fn run(args: PhyloArgs) {
|
||||
|| args.raw_snp_counts
|
||||
|| args.snp
|
||||
|| args.family_overlap
|
||||
|| args.shannon
|
||||
|| args.sankoff
|
||||
|| args.tnt
|
||||
|| args.phyg
|
||||
|
||||
Reference in new issue
Block a user