refactor: pass Unitig objects directly instead of raw byte slices
Refactored `try_for_each_unitig` and related pipelines across `obidebruinj` and `obikpartitionner` to accept `Unitig` instances directly. This eliminates manual `Unitig::from_nucleotides()` conversions, simplifies the data flow, and reduces unnecessary allocation overhead.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//use ahash::RandomState;
|
||||
use hashbrown::HashMap;
|
||||
use obikseq::k;
|
||||
use obikseq::{CanonicalKmer, Sequence};
|
||||
use obikseq::{CanonicalKmer, Sequence, Unitig};
|
||||
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
|
||||
use std::cell::RefCell;
|
||||
use std::fmt;
|
||||
@@ -452,17 +452,17 @@ impl GraphDeBruijn {
|
||||
pub fn try_for_each_unitig<E, F>(&self, f: F) -> Result<(), E>
|
||||
where
|
||||
E: Send,
|
||||
F: FnMut(&[u8]) -> Result<(), E> + Send,
|
||||
F: FnMut(&Unitig) -> Result<(), E> + Send,
|
||||
{
|
||||
thread_local! {
|
||||
static BUF: RefCell<Vec<u8>> = RefCell::new(Vec::with_capacity(4096));
|
||||
}
|
||||
let (tx, rx) = crossbeam_channel::bounded::<Vec<u8>>(rayon::current_num_threads() * 256);
|
||||
let (tx, rx) = crossbeam_channel::bounded::<Unitig>(rayon::current_num_threads() * 256);
|
||||
std::thread::scope(|s| {
|
||||
let writer = s.spawn(move || -> Result<(), E> {
|
||||
let mut f = f;
|
||||
for nucs in rx {
|
||||
f(&nucs)?;
|
||||
for unitig in rx {
|
||||
f(&unitig)?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
@@ -471,9 +471,7 @@ impl GraphDeBruijn {
|
||||
let mut buf = buf.borrow_mut();
|
||||
buf.clear();
|
||||
buf.extend(iter);
|
||||
let to_send = buf.clone();
|
||||
buf.clear();
|
||||
tx.send(to_send).ok();
|
||||
tx.send(Unitig::from_nucleotides(&buf)).ok();
|
||||
});
|
||||
});
|
||||
drop(tx);
|
||||
|
||||
@@ -24,8 +24,8 @@ fn canonical_kmers(seq: &[u8]) -> Vec<CanonicalKmer> {
|
||||
|
||||
fn collect_unitigs(g: &GraphDeBruijn) -> Vec<Unitig> {
|
||||
let mut unitigs = Vec::new();
|
||||
g.try_for_each_unitig(|nucs| -> Result<(), std::convert::Infallible> {
|
||||
unitigs.push(Unitig::from_nucleotides(nucs));
|
||||
g.try_for_each_unitig(|unitig| -> Result<(), std::convert::Infallible> {
|
||||
unitigs.push(unitig.clone());
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
@@ -107,8 +107,8 @@ impl KmerPartition {
|
||||
fs::create_dir_all(&layer_dir)?;
|
||||
|
||||
let mut uw = Layer::<()>::unitig_writer(&layer_dir).map_err(olm_to_sk)?;
|
||||
g.try_for_each_unitig(|nucs| {
|
||||
uw.write(&obikseq::unitig::Unitig::from_nucleotides(nucs))
|
||||
g.try_for_each_unitig(|unitig| {
|
||||
uw.write(unitig)
|
||||
})?;
|
||||
uw.close()?;
|
||||
|
||||
|
||||
@@ -307,8 +307,8 @@ impl KmerPartition {
|
||||
g.compute_degrees_and_mark_starts();
|
||||
fs::create_dir_all(&new_layer_dir)?;
|
||||
let mut uw = Layer::<()>::unitig_writer(&new_layer_dir).map_err(olm_to_sk)?;
|
||||
g.try_for_each_unitig(|nucs| {
|
||||
uw.write(&obikseq::unitig::Unitig::from_nucleotides(nucs))
|
||||
g.try_for_each_unitig(|unitig| {
|
||||
uw.write(unitig)
|
||||
})?;
|
||||
uw.close()?;
|
||||
let n = g.len();
|
||||
|
||||
@@ -168,8 +168,8 @@ impl KmerPartition {
|
||||
fs::create_dir_all(&dst_layer_dir)?;
|
||||
|
||||
let mut uw = Layer::<()>::unitig_writer(&dst_layer_dir).map_err(olm_to_sk)?;
|
||||
g.try_for_each_unitig(|nucs| {
|
||||
uw.write(&obikseq::unitig::Unitig::from_nucleotides(nucs))
|
||||
g.try_for_each_unitig(|unitig| {
|
||||
uw.write(unitig)
|
||||
})?;
|
||||
uw.close()?;
|
||||
drop(g);
|
||||
|
||||
Reference in New Issue
Block a user