Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Use to_writer/from_reader across the codebase #3443

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/benches/minhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn intersection(c: &mut Criterion) {
filename.push("../../tests/test-data/gather-abund/genome-s10.fa.gz.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let mut sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let mut sigs = Signature::from_reader(reader).expect("Loading error");
let mh = if let Sketch::MinHash(mh) = &sigs.swap_remove(0).sketches()[0] {
mh.clone()
} else {
Expand All @@ -24,7 +24,7 @@ fn intersection(c: &mut Criterion) {
filename.push("../../tests/test-data/gather-abund/genome-s11.fa.gz.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let mut sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let mut sigs = Signature::from_reader(reader).expect("Loading error");
let mh2 = if let Sketch::MinHash(mh) = &sigs.swap_remove(0).sketches()[0] {
mh.clone()
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/core/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ mod test {
filename.push("../../tests/test-data/47+63-multisig.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
// create Selection object
let mut selection = Selection::default();
selection.set_scaled(2000);
Expand All @@ -293,7 +293,7 @@ mod test {
filename.push("../../tests/test-data/47+63-multisig.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
// create Selection object
let mut selection = Selection::default();
selection.set_scaled(500);
Expand All @@ -314,7 +314,7 @@ mod test {
filename.push("../../tests/test-data/genome-s11.fa.gz.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
assert_eq!(sigs.len(), 4);
// create Selection object
let mut selection = Selection::default();
Expand All @@ -336,7 +336,7 @@ mod test {
filename.push("../../tests/test-data/genome-s11.fa.gz.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
let sigs_copy = sigs.clone();
assert_eq!(sigs.len(), 4);
// create Selection object
Expand Down Expand Up @@ -366,7 +366,7 @@ mod test {
filename.push("../../tests/test-data/47+63-multisig.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
assert_eq!(sigs.len(), 6);
// create Selection object
let mut selection = Selection::default();
Expand All @@ -388,7 +388,7 @@ mod test {
filename.push("../../tests/test-data/genome-s11.fa.gz.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
assert_eq!(sigs.len(), 4);
// load sigs into collection + select compatible signatures
let mut cl = Collection::from_sigs(sigs).unwrap();
Expand All @@ -413,7 +413,7 @@ mod test {
filename.push("../../tests/test-data/47+63-multisig.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
// create Selection object
let mut selection = Selection::default();
selection.set_scaled(2000);
Expand Down Expand Up @@ -480,7 +480,7 @@ mod test {
.push("../../tests/test-data/prot/hp/GCA_001593925.1_ASM159392v1_protein.faa.gz.sig");
let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");
// create Selection object
let mut selection = Selection::default();
selection.set_moltype(HashFunctions::Murmur64Hp);
Expand Down
8 changes: 5 additions & 3 deletions src/core/src/ffi/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use crate::ffi::cmd::compute::SourmashComputeParameters;
use crate::ffi::minhash::SourmashKmerMinHash;
use crate::ffi::utils::{ForeignObject, SourmashStr};
use crate::prelude::ToWriter;

pub struct SourmashSignature;

Expand Down Expand Up @@ -193,8 +194,9 @@
ffi_fn! {
unsafe fn signature_save_json(ptr: *const SourmashSignature) -> Result<SourmashStr> {
let sig = SourmashSignature::as_rust(ptr);
let st = serde_json::to_string(sig)?;
Ok(SourmashStr::from_string(st))
let mut st: Vec<u8> = vec![];
sig.to_writer(&mut st)?;
Ok(SourmashStr::from_string(String::from_utf8_unchecked(st)))

Check warning on line 199 in src/core/src/ffi/signature.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/ffi/signature.rs#L197-L199

Added lines #L197 - L199 were not covered by tests
}
}

Expand Down Expand Up @@ -248,7 +250,7 @@
} else {
Box::new(&mut buffer)
};
serde_json::to_writer(&mut writer, &rsigs)?;
rsigs.to_writer(&mut writer)?;

Check warning on line 253 in src/core/src/ffi/signature.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/ffi/signature.rs#L253

Added line #L253 was not covered by tests
}

let b = buffer.into_boxed_slice();
Expand Down
32 changes: 21 additions & 11 deletions src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,16 @@
}
}

impl ToWriter for Vec<&Signature> {
fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>

Check warning on line 796 in src/core/src/signature.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/signature.rs#L796

Added line #L796 was not covered by tests
where
W: io::Write,
{
serde_json::to_writer(writer, &self)?;
Ok(())

Check warning on line 801 in src/core/src/signature.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/signature.rs#L800-L801

Added lines #L800 - L801 were not covered by tests
}
}

impl Select for Signature {
fn select(mut self, selection: &Selection) -> Result<Self, Error> {
self.signatures.retain(|s| {
Expand Down Expand Up @@ -949,7 +959,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

assert_eq!(sigs.len(), 4);

Expand Down Expand Up @@ -1072,7 +1082,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

assert_eq!(sigs.len(), 1);

Expand All @@ -1088,7 +1098,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

assert_eq!(sigs.len(), 1);

Expand All @@ -1112,7 +1122,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

assert_eq!(sigs.len(), 1);

Expand All @@ -1137,7 +1147,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

assert_eq!(sigs.len(), 1);

Expand All @@ -1161,7 +1171,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
Expand All @@ -1187,7 +1197,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
Expand All @@ -1207,7 +1217,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
Expand All @@ -1227,7 +1237,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
Expand All @@ -1248,7 +1258,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
Expand All @@ -1266,7 +1276,7 @@

let file = File::open(filename).unwrap();
let reader = BufReader::new(file);
let sigs: Vec<Signature> = serde_json::from_reader(reader).expect("Loading error");
let sigs = Signature::from_reader(reader).expect("Loading error");

// create Selection object
let mut selection = Selection::default();
Expand Down
42 changes: 42 additions & 0 deletions src/core/src/sketch/minhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::collections::{BTreeMap, BTreeSet};
use std::f64::consts::PI;
use std::fmt::Write;
use std::io;
use std::iter::Peekable;
use std::str;
use std::sync::Mutex;
Expand All @@ -13,6 +14,7 @@
use typed_builder::TypedBuilder;

use crate::encodings::HashFunctions;
use crate::prelude::ToWriter;
use crate::signature::SigsTrait;
use crate::sketch::hyperloglog::HyperLogLog;
use crate::Error;
Expand Down Expand Up @@ -183,6 +185,16 @@
}
}

impl ToWriter for KmerMinHash {
fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>

Check warning on line 189 in src/core/src/sketch/minhash.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/sketch/minhash.rs#L189

Added line #L189 was not covered by tests
where
W: io::Write,
{
serde_json::to_writer(writer, &self)?;
Ok(())
}
}

impl KmerMinHash {
pub fn new(
scaled: ScaledType,
Expand Down Expand Up @@ -856,6 +868,16 @@

Ok((abundances, total_abundance))
}

pub fn from_reader<R>(rdr: R) -> Result<KmerMinHash, Error>
where
R: std::io::Read,
{
let (rdr, _format) = niffler::get_reader(Box::new(rdr))?;

let mh: KmerMinHash = serde_json::from_reader(rdr)?;
Ok(mh)

Check warning on line 879 in src/core/src/sketch/minhash.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/sketch/minhash.rs#L879

Added line #L879 was not covered by tests
}
}

impl SigsTrait for KmerMinHash {
Expand Down Expand Up @@ -1113,6 +1135,16 @@
}
}

impl ToWriter for KmerMinHashBTree {
fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>

Check warning on line 1139 in src/core/src/sketch/minhash.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/sketch/minhash.rs#L1139

Added line #L1139 was not covered by tests
where
W: io::Write,
{
serde_json::to_writer(writer, &self)?;
Ok(())
}
}

impl KmerMinHashBTree {
pub fn new(
scaled: ScaledType,
Expand Down Expand Up @@ -1594,6 +1626,16 @@
self.size() as u64
}
}

pub fn from_reader<R>(rdr: R) -> Result<KmerMinHashBTree, Error>
where
R: std::io::Read,
{
let (rdr, _format) = niffler::get_reader(Box::new(rdr))?;

let mh: KmerMinHashBTree = serde_json::from_reader(rdr)?;
Ok(mh)

Check warning on line 1637 in src/core/src/sketch/minhash.rs

View check run for this annotation

Codecov / codecov/patch

src/core/src/sketch/minhash.rs#L1637

Added line #L1637 was not covered by tests
}
}

impl SigsTrait for KmerMinHashBTree {
Expand Down
11 changes: 7 additions & 4 deletions src/core/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use wasm_bindgen::prelude::*;

use crate::cmd::ComputeParameters as _ComputeParameters;
use crate::encodings::HashFunctions;
use crate::prelude::ToWriter;
use crate::signature::Signature as _Signature;
use crate::signature::SigsTrait;
use crate::sketch::minhash::KmerMinHash as _KmerMinHash;
Expand Down Expand Up @@ -66,8 +67,9 @@ impl KmerMinHash {

#[wasm_bindgen]
pub fn to_json(&mut self) -> Result<String, JsErrors> {
let json = serde_json::to_string(&self.0)?;
Ok(json)
let mut st: Vec<u8> = vec![];
self.0.to_writer(&mut st)?;
Ok(unsafe { String::from_utf8_unchecked(st) })
}
}

Expand Down Expand Up @@ -160,8 +162,9 @@ impl Signature {

#[wasm_bindgen]
pub fn to_json(&mut self) -> Result<String, JsErrors> {
let json = serde_json::to_string(&self.0)?;
Ok(json)
let mut st: Vec<u8> = vec![];
self.0.to_writer(&mut st)?;
Ok(unsafe { String::from_utf8_unchecked(st) })
}

pub fn size(&self) -> usize {
Expand Down
Loading
Loading