/
wasm.rs
99 lines (85 loc) · 2.33 KB
/
wasm.rs
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
use wasm_bindgen::prelude::*;
use serde_json;
use crate::cmd::ComputeParameters;
use crate::encodings::HashFunctions;
use crate::signature::{Signature, SigsTrait};
use crate::sketch::minhash::KmerMinHash;
#[wasm_bindgen]
impl KmerMinHash {
#[wasm_bindgen(constructor)]
pub fn new_with_scaled(
num: u32,
ksize: u32,
is_protein: bool,
dayhoff: bool,
hp: bool,
seed: u32,
scaled: u32,
track_abundance: bool,
) -> KmerMinHash {
// TODO: at most one of (prot, dayhoff, hp) should be true
let hash_function = if dayhoff {
HashFunctions::murmur64_dayhoff
} else if hp {
HashFunctions::murmur64_hp
} else if is_protein {
HashFunctions::murmur64_protein
} else {
HashFunctions::murmur64_DNA
};
KmerMinHash::new(
scaled as u64,
ksize,
hash_function,
seed as u64,
track_abundance,
num,
)
}
#[wasm_bindgen]
pub fn add_sequence_js(&mut self, buf: &str) {
self.add_sequence(buf.as_bytes(), true)
.expect("Error adding sequence");
}
#[wasm_bindgen]
pub fn to_json(&mut self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[wasm_bindgen]
impl ComputeParameters {
#[wasm_bindgen(constructor)]
pub fn new_with_params() -> ComputeParameters {
let params = ComputeParameters::default();
params
}
}
#[wasm_bindgen]
impl Signature {
#[wasm_bindgen(constructor)]
pub fn new_from_params(params: &ComputeParameters) -> Signature {
//let params = ComputeParameters::default();
Signature::from_params(¶ms)
}
#[wasm_bindgen]
pub fn add_sequence_js(&mut self, buf: &str) {
self.add_sequence(buf.as_bytes(), true)
.expect("Error adding sequence");
}
#[wasm_bindgen]
pub fn to_json(&mut self) -> String {
serde_json::to_string(self).unwrap()
}
}
#[cfg(test)]
mod test {
use super::*;
use wasm_bindgen_test::*;
#[wasm_bindgen_test]
fn wasm_test() {
let mut params = ComputeParameters::new_with_params();
params.set_ksizes(vec![19, 29, 49]);
let sig = Signature::new_from_params(¶ms);
assert_eq!(sig.size(), 3);
}
}