feat: Add parallel execution and thread-safe graph operations
Integrate rayon to enable parallel processing of k-mer partitions and degree computation. Replace Cell with AtomicU8 to ensure thread-safe node state management, and add a merge method for combining disjoint graphs. Additionally, introduce progress tracking utilities and a test-utils feature flag for development dependencies.
This commit is contained in:
1 parent
edd5e3f8ee
commit
2f29ee2240
5 files changed
+85
-39
No files matched your search
@@ -5,6 +5,8 @@ use clap::Args;
|
||||
use obidebruinj::GraphDeBruijn;
|
||||
use obifastwrite::write_unitig;
|
||||
use obikindex::KmerIndex;
|
||||
use obisys::{progress_bar, spinner};
|
||||
use rayon::prelude::*;
|
||||
use tracing::info;
|
||||
|
||||
use super::predicate::FilterArgs;
|
||||
@@ -32,25 +34,37 @@ pub fn run(args: UnitigArgs) {
|
||||
info!("unitig: building de Bruijn graph from {n} partition(s) (k={k})");
|
||||
|
||||
let filters = args.filter.build_filters(&idx.meta().genomes);
|
||||
let partition = idx.partition();
|
||||
|
||||
// ── Collect all filtered kmers into a single de Bruijn graph ─────────────
|
||||
let mut g = GraphDeBruijn::new();
|
||||
|
||||
for i in 0..n {
|
||||
idx.partition()
|
||||
.iter_partition_kmers(i, use_counts, n_genomes, &filters, |kmer, _row| {
|
||||
g.push(kmer);
|
||||
})
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("error reading partition {i}: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
}
|
||||
// ── Phase 1 : collect filtered kmers in parallel ──────────────────────────
|
||||
let pb = progress_bar("unitig", n as u64, "partitions");
|
||||
let g = (0..n)
|
||||
.into_par_iter()
|
||||
.fold(
|
||||
GraphDeBruijn::new,
|
||||
|mut local_g, i| {
|
||||
partition
|
||||
.iter_partition_kmers(i, use_counts, n_genomes, &filters, |kmer, _row| {
|
||||
local_g.push(kmer);
|
||||
})
|
||||
.unwrap_or_else(|e| {
|
||||
eprintln!("error reading partition {i}: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
pb.inc(1);
|
||||
local_g
|
||||
},
|
||||
)
|
||||
.reduce(GraphDeBruijn::new, |mut a, b| { a.merge(b); a });
|
||||
pb.finish_and_clear();
|
||||
|
||||
info!("unitig: {} distinct k-mers", g.len());
|
||||
|
||||
// ── Phase 2 : compute degrees (in-memory, no progress needed) ────────────
|
||||
g.compute_degrees();
|
||||
|
||||
// ── Enumerate unitigs and write as FASTA ──────────────────────────────────
|
||||
// ── Phase 3 : enumerate unitigs and write as FASTA ────────────────────────
|
||||
let pb = spinner("unitig");
|
||||
let stdout = io::stdout();
|
||||
let mut out = BufWriter::new(stdout.lock());
|
||||
|
||||
@@ -59,7 +73,11 @@ pub fn run(args: UnitigArgs) {
|
||||
eprintln!("write error: {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
if j % 10_000 == 0 {
|
||||
pb.set_message(format!("{j} unitigs written"));
|
||||
}
|
||||
}
|
||||
pb.finish_and_clear();
|
||||
|
||||
out.flush().expect("flush error");
|
||||
}
|
||||
Reference in new issue
Block a user