From 4defa476b370f0da0ab961ecd8f55b6e7068e247 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Sat, 5 Mar 2022 16:25:44 +0000 Subject: [PATCH] Fix containment calculation for nodegraphs (#1862) * fix containment calculation for nodegraphs * add a containment test --- src/core/src/sketch/nodegraph.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/src/sketch/nodegraph.rs b/src/core/src/sketch/nodegraph.rs index 802117570c..9c288ce3ad 100644 --- a/src/core/src/sketch/nodegraph.rs +++ b/src/core/src/sketch/nodegraph.rs @@ -309,7 +309,7 @@ impl Nodegraph { .zip(&other.bs) .map(|(bs, bs_other)| bs.intersection(bs_other).count()) .sum(); - let size: usize = self.bs.iter().map(|bs| bs.len()).sum(); + let size: usize = self.bs.iter().map(|bs| bs.count_ones(..)).sum(); result as f64 / size as f64 } } @@ -435,6 +435,24 @@ mod test { assert_eq!(ng.unique_kmers(), 1); } + #[test] + fn containment() { + let mut ng1: Nodegraph = Nodegraph::new(&[31], 3); + let mut ng2: Nodegraph = Nodegraph::new(&[31], 3); + + (0..20).for_each(|i| { + if i % 2 == 0 { + ng1.count(i); + }; + ng2.count(i); + }); + + assert_eq!(ng1.containment(&ng2), 1.0); + assert_eq!(ng1.similarity(&ng2), 0.5); + assert_eq!(ng1.unique_kmers(), 10); + assert_eq!(ng2.unique_kmers(), 20); + } + #[test] fn load_save_nodegraph() { let mut datadir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));