feat: add threshold-based Hamming distance and refine phylo documentation

Add `threshold_hamming_dist_matrix` to calculate k-mers present in exactly one genome using union minus intersection. Update `DistanceArgs` and `distance.rs` to support this metric on count indexes via `--presence-threshold`, removing the previous restriction that hamming was only for presence/absence indexes.

- Add `threshold_hamming_dist_matrix` method to `CountPartials` in `src/obikmer/src/traits.rs`
- Implement new test `threshold_hamming_counts_kmers_present_in_one_genome_only` in `src/obicompactvec/src/tests/intmatrix.rs`
- Update `DistanceArgs` in `src/obikmer/src/cmd/phylo/args/distance.rs` to clarify threshold effects on Jaccard/Mash/Hamming
- Modify `distance` function in `src/obikphylo/src/distance.rs` to support Hamming on count indexes and use pre-computed bit structures
- Update `obicompactvec.md` documentation with new function details and partial result explanations
- Refine `index.md` descriptions for `phylo`, `unitig`, and `select` options
This commit is contained in:
Eric Coissac committed 2026-11-19 07:11:57 +01:00
1 parent c4eacb3f72
commit c93616b432
6 files changed
+33 -13

No files matched your search

+14
View File
@@ -249,6 +249,20 @@ fn partial_threshold_jaccard_consistent() {
}
}
#[test]
fn threshold_hamming_counts_kmers_present_in_one_genome_only() {
// At threshold 2 the genomes (columns) hold the k-mers (rows):
// c0 = {r0, r2}, c1 = {r1}, c2 = {r1}.
let (_d, m) = make_matrix(&[&[3, 0, 2], &[1, 2, 0], &[0, 3, 1]]);
let h = m.threshold_hamming_dist_matrix(2);
let expected = [[0, 3, 3], [3, 0, 0], [3, 0, 0]];
for i in 0..3 {
for j in 0..3 {
assert_eq!(h[[i, j]], expected[i][j], "[{i},{j}]");
}
}
}
#[test]
fn single_col_distance_matrix_is_zero() {
let (_d, m) = make_matrix(&[&[1, 2, 3]]);
+10
View File
@@ -129,6 +129,16 @@ pub trait CountPartials: ColumnWeights {
jaccard_to_mash(&self.threshold_jaccard_dist_matrix(threshold), k)
}
/// Hamming distance on presence/absence: the number of k-mers present
/// (count ≥ `threshold`) in exactly one of the two genomes, i.e. the union
/// minus the intersection that [`partial_threshold_jaccard`] already gives.
///
/// [`partial_threshold_jaccard`]: Self::partial_threshold_jaccard
fn threshold_hamming_dist_matrix(&self, threshold: u32) -> Array2<u64> {
let (inter, union) = self.partial_threshold_jaccard(threshold);
union - inter
}
fn relfreq_bray_dist_matrix(&self) -> Array2<f64> {
let global = self.col_weights();
let mut m = self.partial_relfreq_bray(&global).mapv(|v| 1.0 - v);