Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Commit

Permalink
Starting wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Jul 18, 2018
1 parent b7d874c commit 9d12fb8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cbindgen = "0.6.0"

[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"
Expand All @@ -32,3 +33,7 @@ ordslice = "0.2.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"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ target/sourmash.h: src/lib.rs src/ffi.rs src/errors.rs
RUST_BACKTRACE=1 CARGO_EXPAND_TARGET_DIR=${tempdir} cbindgen -c cbindgen.toml -o $@
-rm -rf ${tempdir}

wasm:
# wasm-pack init --mode no-installs
wasm-pack init

.phony: test
57 changes: 45 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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};
Expand All @@ -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};

Expand Down Expand Up @@ -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<u64>,
pub abunds: Option<Vec<u64>>,
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<u64>,
abunds: Option<Vec<u64>>,
}
} 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<u64>,
pub abunds: Option<Vec<u64>>,
}
}
}

impl Default for KmerMinHash {
Expand Down Expand Up @@ -531,6 +560,10 @@ impl KmerMinHash {
return Ok(0.0);
}
}

pub fn to_vec(&self) -> Vec<u64> {
self.mins.clone()
}
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
50 changes: 50 additions & 0 deletions src/wasm.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 9d12fb8

Please sign in to comment.