From 09cb9347622e49f78d316075888233cb29a124f5 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Thu, 17 May 2018 12:32:49 -0700 Subject: [PATCH 1/7] Starting wasm --- Cargo.toml | 5 +++++ Makefile | 4 ++++ src/lib.rs | 57 ++++++++++++++++++++++++++++++++++++++++++----------- src/wasm.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 src/wasm.rs diff --git a/Cargo.toml b/Cargo.toml index 2e26b8e..9eb050d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ cbindgen = "0.6.1" [dependencies] backtrace = "0.3.4" +cfg-if = "0.1" error-chain = "0.12" finch = { version = "0.1.6", optional = true } lazy_static = "1.0.0" @@ -33,3 +34,7 @@ ordslice = "0.3.0" serde = "1.0" serde_derive = "1.0" serde_json = "1.0.2" + +#TODO: wasm-pack can't check optionals or this kind of config yet... +# [target. 'cfg(target_arch = "wasm32")'.dependencies] +wasm-bindgen = "0.2" diff --git a/Makefile b/Makefile index 30e3c8c..f9a7448 100644 --- a/Makefile +++ b/Makefile @@ -6,4 +6,8 @@ test: target/sourmash.h: src/lib.rs src/ffi.rs src/errors.rs RUST_BACKTRACE=1 cbindgen --clean -c cbindgen.toml -o $@ +wasm: +# wasm-pack init --mode no-installs + wasm-pack init + .phony: test diff --git a/src/lib.rs b/src/lib.rs index 1b828c5..f2b1061 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,12 @@ +#![cfg_attr( + target_arch = "wasm32", + feature(use_extern_macros, wasm_custom_section, wasm_import_module) +)] + extern crate backtrace; +#[macro_use] +extern crate cfg_if; + extern crate md5; extern crate murmurhash3; extern crate ordslice; @@ -29,9 +37,6 @@ pub mod ffi; #[cfg(feature = "from-finch")] pub mod from; -use serde::de::{Deserialize, Deserializer}; -use serde::ser::{Serialize, SerializeStruct, Serializer}; - use std::collections::HashMap; use std::collections::HashSet; use std::hash::{BuildHasherDefault, Hasher}; @@ -40,6 +45,8 @@ use std::str; use murmurhash3::murmurhash3_x64_128; use ordslice::Ext; +use serde::de::{Deserialize, Deserializer}; +use serde::ser::{Serialize, SerializeStruct, Serializer}; use errors::{ErrorKind, Result}; @@ -72,15 +79,37 @@ impl Hasher for NoHashHasher { } } -#[derive(Debug, Clone, PartialEq)] -pub struct KmerMinHash { - pub num: u32, - pub ksize: u32, - pub is_protein: bool, - pub seed: u64, - pub max_hash: u64, - pub mins: Vec, - pub abunds: Option>, +cfg_if! { + if #[cfg(target_arch = "wasm32")] { + extern crate wasm_bindgen; + + use wasm_bindgen::prelude::*; + + pub mod wasm; + + #[wasm_bindgen] + #[derive(Debug, Clone, PartialEq)] + pub struct KmerMinHash { + pub num: u32, + pub ksize: u32, + pub is_protein: bool, + pub seed: u64, + pub max_hash: u64, + mins: Vec, + abunds: Option>, + } + } else { + #[derive(Debug, Clone, PartialEq)] + pub struct KmerMinHash { + pub num: u32, + pub ksize: u32, + pub is_protein: bool, + pub seed: u64, + pub max_hash: u64, + pub mins: Vec, + pub abunds: Option>, + } + } } impl Default for KmerMinHash { @@ -531,6 +560,10 @@ impl KmerMinHash { return Ok(0.0); } } + + pub fn to_vec(&self) -> Vec { + self.mins.clone() + } } #[derive(Serialize, Deserialize, Debug)] diff --git a/src/wasm.rs b/src/wasm.rs new file mode 100644 index 0000000..757635d --- /dev/null +++ b/src/wasm.rs @@ -0,0 +1,50 @@ +use wasm_bindgen::prelude::*; + +use serde_json; + +use KmerMinHash; + +#[wasm_bindgen] +impl KmerMinHash { + #[wasm_bindgen(constructor)] + pub fn new_with_scaled( + num: u32, + ksize: u32, + is_protein: bool, + seed: u32, + scaled: u32, + track_abundance: bool, + ) -> KmerMinHash { + let max_hash = if num != 0 { + 0 + } else if scaled == 0 { + u64::max_value() + } else { + u64::max_value() / scaled as u64 + }; + + KmerMinHash::new( + num, + ksize, + is_protein, + seed as u64, + max_hash, + track_abundance, + ) + } + + #[wasm_bindgen] + pub fn add_sequence_js(&mut self, buf: &str) { + self.add_sequence(buf.as_bytes(), true); + } + + #[wasm_bindgen] + pub fn add_hash_js(&mut self, h: u64) { + self.add_hash(h); + } + + #[wasm_bindgen] + pub fn to_json(&mut self) -> String { + serde_json::to_string(self).unwrap() + } +} From e55ba3f312f7fe88486dc97a897e8fcadd93294d Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Wed, 18 Jul 2018 14:22:15 -0700 Subject: [PATCH 2/7] bump version --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9eb050d..149c9ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sourmash" -version = "0.1.1" +version = "0.2.0" authors = ["Luiz Irber "] description = "MinHash sketches for genomic data" repository = "https://github.com/luizirber/sourmash-rust" From 8cb122f5ed4e10d9fcc8eeebaa9936815a440d4e Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Thu, 26 Jul 2018 18:43:33 -0700 Subject: [PATCH 3/7] upd --- Cargo.toml | 6 ++++-- src/lib.rs | 5 +---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 149c9ad..f9bb172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,10 @@ needletail = { version = "~0.2.1", optional = true } ordslice = "0.3.0" serde = "1.0" serde_derive = "1.0" -serde_json = "1.0.2" +serde_json = "1.0" #TODO: wasm-pack can't check optionals or this kind of config yet... # [target. 'cfg(target_arch = "wasm32")'.dependencies] -wasm-bindgen = "0.2" +[dependencies.wasm-bindgen] +version = "^0.2" +features = ["serde-serialize"] diff --git a/src/lib.rs b/src/lib.rs index f2b1061..e91b648 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,4 @@ -#![cfg_attr( - target_arch = "wasm32", - feature(use_extern_macros, wasm_custom_section, wasm_import_module) -)] +#![cfg_attr(target_arch = "wasm32", feature(use_extern_macros))] extern crate backtrace; #[macro_use] From 58fb6e0af6905d27845195f0e9d865b1d95578d3 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Fri, 17 Aug 2018 15:31:02 -0700 Subject: [PATCH 4/7] Avoid exposing u64 to wasm --- src/lib.rs | 10 +++++----- src/wasm.rs | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e91b648..8b47c59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,11 +87,11 @@ cfg_if! { #[wasm_bindgen] #[derive(Debug, Clone, PartialEq)] pub struct KmerMinHash { - pub num: u32, - pub ksize: u32, - pub is_protein: bool, - pub seed: u64, - pub max_hash: u64, + num: u32, + ksize: u32, + is_protein: bool, + seed: u64, + max_hash: u64, mins: Vec, abunds: Option>, } diff --git a/src/wasm.rs b/src/wasm.rs index 757635d..7e4a4d5 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -38,10 +38,12 @@ impl KmerMinHash { self.add_sequence(buf.as_bytes(), true); } + /* #[wasm_bindgen] pub fn add_hash_js(&mut self, h: u64) { self.add_hash(h); } + */ #[wasm_bindgen] pub fn to_json(&mut self) -> String { From 785045ca35d0c204d84539062663b1d4741c8854 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Fri, 17 Aug 2018 16:16:44 -0700 Subject: [PATCH 5/7] bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f9bb172..83870b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sourmash" -version = "0.2.0" +version = "0.2.1" authors = ["Luiz Irber "] description = "MinHash sketches for genomic data" repository = "https://github.com/luizirber/sourmash-rust" From ff197ba6f12f6523ee543edfc81214a2e2a514c5 Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Sat, 18 Aug 2018 14:24:51 -0700 Subject: [PATCH 6/7] not needed for now --- src/wasm.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/wasm.rs b/src/wasm.rs index 7e4a4d5..72bd4e5 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -38,13 +38,6 @@ impl KmerMinHash { self.add_sequence(buf.as_bytes(), true); } - /* - #[wasm_bindgen] - pub fn add_hash_js(&mut self, h: u64) { - self.add_hash(h); - } - */ - #[wasm_bindgen] pub fn to_json(&mut self) -> String { serde_json::to_string(self).unwrap() From 4205d3a61ddc9d2f50b47f690c80c9900079582d Mon Sep 17 00:00:00 2001 From: Luiz Irber Date: Fri, 24 Aug 2018 11:49:17 -0700 Subject: [PATCH 7/7] no need fo use_extern_macros anymore --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8b47c59..413c480 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,3 @@ -#![cfg_attr(target_arch = "wasm32", feature(use_extern_macros))] - extern crate backtrace; #[macro_use] extern crate cfg_if;