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
use crate::{
    cli::HashAlgorithms,
    hasher::NoHashHasher,
    sketch::{Sketch, Stats},
};
use serde::{Deserialize, Serialize};
use sourmash::signature::{Signature as SourmashSignature, SigsTrait};
use std::hash::BuildHasherDefault;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Signature {
    pub file_name: String,
    pub sketches: Vec<Sketch>,
    pub algorithm: HashAlgorithms,
    pub kmer_size: u8,
    pub max_hash: u64,
}

impl From<Signature> for SourmashSignature {
    fn from(val: Signature) -> Self {
        SourmashSignature::builder()
            .hash_function(format!("{:?}", val.algorithm))
            .filename(Some(val.file_name))
            .email("".to_string())
            .license("CC0".to_string())
            .name(None)
            .signatures(
                val.sketches
                    .into_iter()
                    .map(|sketch| sketch.into_sourmash(val.max_hash))
                    .collect(),
            )
            .build()
    }
}

impl From<SourmashSignature> for Signature {
    fn from(sourmash_signature: SourmashSignature) -> Self {
        let mut sketches = Vec::new();
        let mut max_hash = None;
        let mut kmer_size = None;
        for sketch in sourmash_signature.sketches() {
            match sketch {
                sourmash::sketch::Sketch::MinHash(mash) => {
                    if let Some(max_hash) = max_hash {
                        if max_hash != mash.max_hash() {
                            panic!("Max hash of sketches is not equal");
                        }
                    } else {
                        max_hash = Some(mash.max_hash());
                    }

                    if let Some(kmer_size) = kmer_size {
                        if kmer_size != mash.ksize() as u8 {
                            panic!("Kmer size of sketches is not equal");
                        }
                    } else {
                        kmer_size = Some(mash.ksize() as u8);
                    }

                    let mut sketch = Sketch::new(
                        sourmash_signature.filename(),
                        mash.mins().len(),
                        mash.max_hash() as usize,
                        mash.ksize() as u8,
                    );
                    sketch.hashes = mash
                        .mins()
                        .iter()
                        .map(|x| (*x, None))
                        .collect::<std::collections::HashMap<
                            u64,
                            Option<Stats>,
                            BuildHasherDefault<NoHashHasher>,
                        >>();
                    sketches.push(sketch);
                }
                sourmash::sketch::Sketch::LargeMinHash(mash) => {
                    if let Some(max_hash) = max_hash {
                        if max_hash != mash.max_hash() {
                            panic!("Max hash of sketches is not equal");
                        }
                    } else {
                        max_hash = Some(mash.max_hash());
                    }

                    if let Some(kmer_size) = kmer_size {
                        if kmer_size != mash.ksize() as u8 {
                            panic!("Kmer size of sketches is not equal");
                        }
                    } else {
                        kmer_size = Some(mash.ksize() as u8);
                    }

                    let mut sketch = Sketch::new(
                        sourmash_signature.filename(),
                        mash.mins().len(),
                        mash.max_hash() as usize,
                        mash.ksize() as u8,
                    );
                    sketch.hashes = mash
                        .mins()
                        .iter()
                        .map(|x| (*x, None))
                        .collect::<std::collections::HashMap<
                            u64,
                            Option<Stats>,
                            BuildHasherDefault<NoHashHasher>,
                        >>();
                    sketches.push(sketch);
                }
                sourmash::sketch::Sketch::HyperLogLog(_) => {
                    unimplemented!("HyperLogLog sketches are not supported")
                }
            }
        }
        Signature {
            file_name: sourmash_signature.filename(),
            sketches,
            algorithm: HashAlgorithms::Murmur3,
            kmer_size: kmer_size.expect("No sketch with kmer_size found"),
            max_hash: max_hash.expect("No sketch with max hash found"),
        }
    }
}

impl Signature {
    pub fn collapse(&mut self) -> Sketch {
        let mut sketch = Sketch::new(self.file_name.to_string(), 0, 0, self.kmer_size);
        for old_sketch in self.sketches.drain(..) {
            sketch.hashes.extend(old_sketch.hashes);
            sketch.num_kmers += old_sketch.num_kmers;
            sketch.max_kmers += old_sketch.max_kmers;
        }
        sketch
    }
}