Refactor distance matrix calculation logic for consistency
Ensures that combined distance matrix calculations use the exact same site selection by retaining sampled data within Sankoff bundles. This involves refactoring post-sampling logic into shared functions, implementing `SankoffBundle` to reuse internal tally data, and adding validation to guarantee consistent site selection across all distance calculation paths.
This commit is contained in:
1 parent
be838da256
commit
a8bcf5ffac
6 files changed
+196
-33
No files matched your search
@@ -268,6 +268,14 @@ pub fn run(args: PhyloArgs) {
|
||||
}
|
||||
|
||||
// ── Sankoff cost-matrix calibration (`--sankoff`, `--tnt`, `--phyg`, `--iqtree`) ──
|
||||
// `bundle` is kept around (not dropped at the end of this block) so the
|
||||
// `snp-*` `--distance` branch below can reuse its already-sampled
|
||||
// tally instead of resampling — see `SankoffBundle::snp_distance`'s own
|
||||
// docs for why that's required for correctness, not just speed, when
|
||||
// both are requested in the same invocation (they always share the
|
||||
// same `n`/`free_loss`/`no_ambiguity`/excluded-set/`entropy_bias`: the
|
||||
// CLI has no way to give them different values in one run).
|
||||
let mut bundle = None;
|
||||
if args.sankoff || args.tnt || args.phyg || args.iqtree {
|
||||
let Some(subsample_n) = args.subsample else {
|
||||
eprintln!("error: --sankoff requires --subsample <N>");
|
||||
@@ -276,7 +284,7 @@ pub fn run(args: PhyloArgs) {
|
||||
|
||||
info!("sampling Sankoff calibration bundle (target {subsample_n} site(s))");
|
||||
let t = Stage::start("sankoff_bundle");
|
||||
let bundle = cache
|
||||
let b = cache
|
||||
.sankoff_bundle(
|
||||
subsample_n,
|
||||
args.free_loss,
|
||||
@@ -291,25 +299,25 @@ pub fn run(args: PhyloArgs) {
|
||||
});
|
||||
rep.push(t.stop());
|
||||
|
||||
let p_card = cardinality_transition_probs(&bundle.cardinality_tally);
|
||||
let p_comp = composition_transition_probs(&bundle.base_pair_tally);
|
||||
let p_card = cardinality_transition_probs(&b.cardinality_tally);
|
||||
let p_comp = composition_transition_probs(&b.base_pair_tally);
|
||||
let matrix = pairwise_cost_matrix(&p_card, &p_comp, args.free_loss);
|
||||
|
||||
write_sankoff_matrix_csv(&matrix, &args.output);
|
||||
write_sankoff_params(
|
||||
&bundle.cardinality_tally,
|
||||
&b.cardinality_tally,
|
||||
&p_card,
|
||||
&bundle.base_pair_tally,
|
||||
&b.base_pair_tally,
|
||||
&p_comp,
|
||||
args.sankoff_ratio_ceiling,
|
||||
&args.output,
|
||||
);
|
||||
write_sankoff_alignment_fasta(&bundle.alignment, &labels, &args.output, args.free_loss);
|
||||
write_sankoff_alignment_fasta(&b.alignment, &labels, &args.output, args.free_loss);
|
||||
|
||||
if args.tnt {
|
||||
write_sankoff_tnt(
|
||||
&matrix,
|
||||
&bundle.alignment,
|
||||
&b.alignment,
|
||||
&labels,
|
||||
&args.output,
|
||||
args.sankoff_cost_scale,
|
||||
@@ -322,13 +330,14 @@ pub fn run(args: PhyloArgs) {
|
||||
if args.iqtree {
|
||||
write_iqtree(
|
||||
&matrix,
|
||||
&bundle.alignment,
|
||||
&b.alignment,
|
||||
&labels,
|
||||
&args.output,
|
||||
args.free_loss,
|
||||
args.iqtree_min_freq,
|
||||
);
|
||||
}
|
||||
bundle = Some(b);
|
||||
}
|
||||
|
||||
// ── Distance computation: classic whole-index metric vs. `snp-*` ───────────
|
||||
@@ -354,28 +363,39 @@ pub fn run(args: PhyloArgs) {
|
||||
std::process::exit(1);
|
||||
}
|
||||
let kind = args.distance.as_snp().expect("DistanceArg is always classic or snp");
|
||||
info!(
|
||||
"computing {kind:?} SNP distance for {n} genome(s){}",
|
||||
match args.subsample {
|
||||
Some(n) => format!(" (subsampled, target {n} site(s))"),
|
||||
None => " (exhaustive)".into(),
|
||||
}
|
||||
);
|
||||
let gamma_shape = args.gamma_shape.map_or(GammaShape::Disabled, Into::into);
|
||||
let t = Stage::start("snp_distance");
|
||||
let matrix = cache
|
||||
.snp_distance(
|
||||
kind,
|
||||
args.subsample,
|
||||
args.free_loss,
|
||||
args.no_ambiguity,
|
||||
&snp_exclude_mask,
|
||||
entropy_bias,
|
||||
args.gamma_shape.map_or(GammaShape::Disabled, Into::into),
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("error computing SNP distance: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
let matrix = match &bundle {
|
||||
Some(b) => {
|
||||
info!(
|
||||
"computing {kind:?} SNP distance for {n} genome(s) \
|
||||
(reusing the Sankoff bundle's sample)"
|
||||
);
|
||||
b.snp_distance(kind, gamma_shape)
|
||||
}
|
||||
None => {
|
||||
info!(
|
||||
"computing {kind:?} SNP distance for {n} genome(s){}",
|
||||
match args.subsample {
|
||||
Some(n) => format!(" (subsampled, target {n} site(s))"),
|
||||
None => " (exhaustive)".into(),
|
||||
}
|
||||
);
|
||||
cache.snp_distance(
|
||||
kind,
|
||||
args.subsample,
|
||||
args.free_loss,
|
||||
args.no_ambiguity,
|
||||
&snp_exclude_mask,
|
||||
entropy_bias,
|
||||
gamma_shape,
|
||||
)
|
||||
}
|
||||
}
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("error computing SNP distance: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
rep.push(t.stop());
|
||||
(matrix, None)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user