1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
use crate::signature::Signature;
use crate::sketch::Sketch;
use anyhow::anyhow;
use anyhow::Result;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{
    fmt::{self, Display, Formatter},
    ops::DerefMut,
    sync::Mutex,
};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct CompareResult {
    pub from_name: String,
    pub to_name: String,
    pub num_common: usize,
    pub num_kmers: usize,
    pub option_num_skipped: Option<usize>,
    pub reverse: bool,
    pub estimated_containment: f64,
}

impl Display for CompareResult {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.reverse {
            write!(
                f,
                "{}\t{}\t{}\t{}\t{}\t{}\t{}",
                self.to_name,
                self.from_name,
                self.num_common,
                self.num_kmers,
                self.num_common as f64 / self.num_kmers as f64 * 100.0, // Percent
                self.estimated_containment,
                self.option_num_skipped.unwrap_or(0)
            )?;
            Ok(())
        } else {
            write!(
                f,
                "{}\t{}\t{}\t{}\t{}\t{}\t{}",
                self.from_name,
                self.to_name,
                self.num_common,
                self.num_kmers,
                self.num_common as f64 / self.num_kmers as f64 * 100.0,
                self.estimated_containment,
                self.option_num_skipped.unwrap_or(0)
            )
        }
    }
}

pub struct MultiComp {
    from: Vec<Sketch>,
    to: Vec<Sketch>,
    results: Vec<CompareResult>,
    threads: usize,
    kmer_size: u8,
    cutoff: f64,
    use_stats: bool,
    gc_bounds: Option<(u8, u8)>,
}

impl MultiComp {
    pub fn new(
        mut from: Vec<Signature>,
        mut to: Vec<Signature>,
        threads: usize,
        cutoff: f64,
        use_stats: bool,
        gc_bounds: Option<(u8, u8)>,
    ) -> Result<Self> {
        let kmer_size = from
            .first()
            .ok_or_else(|| anyhow!("Empty from list"))?
            .kmer_size;

        Ok(MultiComp {
            from: from.iter_mut().map(|e| e.collapse()).collect(),
            to: to.iter_mut().map(|e| e.collapse()).collect(),
            results: Vec::new(),
            threads,
            kmer_size,
            cutoff,
            use_stats,
            gc_bounds,
        })
    }

    pub fn compare(&mut self) -> Result<()> {
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(self.threads)
            .build()?;

        let results = Mutex::new(Vec::new());

        pool.install(|| {
            self.from.par_iter().try_for_each(|origin| {
                self.to.par_iter().try_for_each(|target| {
                    if target.kmer_size != self.kmer_size || origin.kmer_size != self.kmer_size {
                        return Err(anyhow!(
                            "Kmer sizes do not match, expected: {}, got: {}",
                            self.kmer_size,
                            origin.kmer_size
                        ));
                    }
                    let mut comparator =
                        Comparator::new(origin, target, self.use_stats, self.gc_bounds);
                    comparator.compare()?;
                    results
                        .lock()
                        .unwrap()
                        .deref_mut()
                        .push(comparator.finalize());
                    Ok::<(), anyhow::Error>(())
                })
            })
        })?;

        self.results = results.into_inner().unwrap();
        Ok(())
    }

    pub fn finalize(self) -> Vec<CompareResult> {
        self.results
            .into_iter()
            .filter(|e| e.num_common as f64 / e.num_kmers as f64 * 100.0 > self.cutoff)
            .collect()
    }
}

pub struct Comparator<'a> {
    larger: &'a Sketch,
    smaller: &'a Sketch,
    num_kmers: usize,
    num_common: usize,
    num_skipped: usize,
    reverse: bool,
    use_stats: bool,
    gc_bounds: Option<(u8, u8)>,
}

impl<'a> Comparator<'a> {
    pub fn new(
        sketch_a: &'a Sketch,
        sketch_b: &'a Sketch,
        use_stats: bool,
        gc_bounds: Option<(u8, u8)>,
    ) -> Self {
        let (larger, smaller, reverse) = if sketch_a.hashes.len() >= sketch_b.hashes.len() {
            // DATABASE, INPUT -> Reverse = false
            (sketch_a, sketch_b, false)
        } else {
            // INPUT, DATABASE -> Reverse = true
            (sketch_b, sketch_a, true)
        };
        Comparator {
            larger,
            smaller,
            num_kmers: 0,
            num_common: 0,
            num_skipped: 0,
            reverse,
            use_stats,
            gc_bounds,
        }
    }

    // Stats handling:
    // GC & Size for the original contig are stored in the Stats struct
    // This comparison is always in relation to the query sketch
    // If reverse is true, the query sketch is the larger sketch
    #[inline]
    pub fn compare(&mut self) -> Result<()> {
        if self.use_stats {
            for (hash, stats) in &self.smaller.hashes {
                let smaller_stats = stats.as_ref().ok_or_else(|| anyhow!("Missing stats"))?;
                self.num_kmers += 1;
                if let Some(stats) = self.larger.hashes.get(hash) {
                    let larger_stats = stats.as_ref().ok_or_else(|| anyhow!("Missing stats"))?;
                    if self.reverse {
                        if !larger_stats.compare(smaller_stats, self.gc_bounds) {
                            self.num_skipped += 1;
                        } else {
                            self.num_common += 1;
                        }
                    } else if !smaller_stats.compare(larger_stats, self.gc_bounds) {
                        self.num_skipped += 1;
                    } else {
                        self.num_common += 1;
                    }
                };
            }
        } else {
            for hash in self.smaller.hashes.keys() {
                self.num_kmers += 1;
                if self.larger.hashes.contains_key(hash) {
                    self.num_common += 1;
                };
            }
        }
        Ok(())
    }

    pub fn finalize(self) -> CompareResult {
        // Eg 0.1
        let larger_fraction = self.larger.num_kmers as f64 / self.larger.max_kmers as f64;
        // Eg 1.0
        let smaller_fraction = self.smaller.num_kmers as f64 / self.smaller.max_kmers as f64;
        // How much smaller is the smaller sketch
        let fraction = if larger_fraction < smaller_fraction {
            smaller_fraction / larger_fraction
        } else {
            larger_fraction / smaller_fraction
        };
        let estimated_containment =
            self.num_common as f64 / self.num_kmers as f64 * fraction * 100.0;

        CompareResult {
            from_name: self.larger.name.clone(),
            to_name: self.smaller.name.clone(),
            num_kmers: self.num_kmers,
            num_common: self.num_common,
            option_num_skipped: if self.use_stats {
                Some(self.num_skipped)
            } else {
                None
            },
            reverse: self.reverse,
            estimated_containment,
        }
    }

    #[allow(dead_code)]
    pub fn reset(&mut self) {
        self.num_kmers = 0;
        self.num_common = 0;
        self.num_skipped = 0;
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::{compare::CompareResult, signature::Signature, sketch::Stats};

    use super::MultiComp;

    #[test]
    fn test_comp_without_stats() {
        let mut hashmap = HashMap::default();
        hashmap.extend([(1, None), (2, None), (3, None)]);
        let sketch_a = crate::sketch::Sketch {
            name: "a".to_string(),
            hashes: hashmap,
            num_kmers: 3,
            max_kmers: 10,
            kmer_size: 21,
        };
        let mut hashmap2 = HashMap::default();
        hashmap2.extend([(1, None), (2, None), (4, None)]);
        let sketch_b = crate::sketch::Sketch {
            name: "b".to_string(),
            hashes: hashmap2,
            num_kmers: 3,
            max_kmers: 10,
            kmer_size: 21,
        };

        let mut comp = super::Comparator::new(&sketch_a, &sketch_b, false, None);
        comp.compare().unwrap();
        let result = comp.finalize();
        assert_eq!(result.num_kmers, 3);
        assert_eq!(result.num_common, 2);
        assert_eq!(result.estimated_containment, 66.66666666666666);
        assert_eq!(result.option_num_skipped, None);

        let constructed_result = CompareResult {
            from_name: "a".to_string(),
            to_name: "b".to_string(),
            num_kmers: 3,
            num_common: 2,
            option_num_skipped: None,
            reverse: false,
            estimated_containment: 66.66666666666666,
        };
        assert_eq!(result, constructed_result);
    }

    #[test]
    fn test_multi_comp() {
        let mut hashmap = HashMap::default();
        hashmap.extend([
            (1, Some(Stats::new(3, 20))),
            (2, Some(Stats::new(3, 20))),
            (3, Some(Stats::new(3, 20))),
            (4, Some(Stats::new(3, 20))),
        ]);
        let sketch_a = crate::sketch::Sketch {
            name: "a".to_string(),
            hashes: hashmap,
            num_kmers: 4,
            max_kmers: 10,
            kmer_size: 21,
        };
        let mut hashmap = HashMap::default();
        hashmap.extend([
            (1, Some(Stats::new(5, 20))),
            (2, Some(Stats::new(3, 20))),
            (3, Some(Stats::new(2, 30))),
            (4, Some(Stats::new(2, 60))),
        ]);
        let sketch_b = crate::sketch::Sketch {
            name: "b".to_string(),
            hashes: hashmap,
            num_kmers: 4,
            max_kmers: 10,
            kmer_size: 21,
        };
        let mut comp = MultiComp::new(
            vec![Signature {
                file_name: "test".to_string(),
                sketches: vec![sketch_a],
                algorithm: crate::cli::HashAlgorithms::Ahash,
                kmer_size: 21,
                max_hash: u64::MAX,
            }],
            vec![Signature {
                file_name: "test2".to_string(),
                sketches: vec![sketch_b],
                algorithm: crate::cli::HashAlgorithms::Ahash,
                kmer_size: 21,
                max_hash: u64::MAX,
            }],
            1,
            0.0,
            true,
            Some((10, 10)),
        )
        .unwrap();

        comp.compare().unwrap();
        let res = comp.finalize();

        assert_eq!(res.len(), 1);
        let expected = CompareResult {
            from_name: "test".to_string(),
            to_name: "test2".to_string(),
            num_kmers: 4,
            num_common: 2,
            option_num_skipped: Some(2),
            reverse: false,
            estimated_containment: 50.0,
        };
        assert_eq!(res[0], expected);

        assert_eq!(
            res[0].to_string(),
            "test\ttest2\t2\t4\t50\t50\t2".to_string()
        );
    }
}