Skip to content

Commit

Permalink
adding progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
wdecoster committed Mar 1, 2024
1 parent a260f09 commit 6e91688
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ libc = "0.2.147"
petgraph = "0.6.4"
hts-sys = "2.1.1"
reqwest = { version = "0.11", features = ["blocking", "json"] }
indicatif = { version = "0.17.1", features = ["rayon"] }

[dev-dependencies]
ctor = "*"
25 changes: 16 additions & 9 deletions src/call.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::repeats::RepeatIntervalIterator;
use indicatif::ParallelProgressIterator;
use indicatif::ProgressIterator;
use log::{debug, error};
use rayon::prelude::*;
use std::io::Write;
Expand All @@ -16,8 +18,9 @@ pub fn genotype_repeats(args: Cli) {
// When running single threaded things become easier and the tool will require less memory
// Output is returned in the same order as the bed, and therefore not sorted before writing immediately to stdout
// The indexedreader is created once and passed on to the function
let num_intervals = repeats.num_intervals;
let mut bam = parse_bam::create_bam_reader(&args.bam, &args.fasta);
for repeat in repeats {
for repeat in repeats.progress_count(num_intervals as u64) {
if let Ok(output) = genotype::genotype_repeat_singlethreaded(&repeat, &args, &mut bam) {
writeln!(handle, "{output}").expect("Failed writing the result.");
}
Expand All @@ -30,14 +33,18 @@ pub fn genotype_repeats(args: Cli) {
// genotypes contains the output of the genotyping, a struct instance
let genotypes = Mutex::new(Vec::new());
// par_bridge does not guarantee that results are returned in order
repeats.par_bridge().for_each(|repeat| {
if let Ok(output) = genotype::genotype_repeat_multithreaded(&repeat, &args) {
let mut geno = genotypes.lock().expect("Unable to lock genotypes mutex");
geno.push(output);
} else {
error!("Problem processing {repeat}");
}
});
let num_intervals = repeats.num_intervals;
repeats
.par_bridge()
.progress_count(num_intervals as u64)
.for_each(|repeat| {
if let Ok(output) = genotype::genotype_repeat_multithreaded(&repeat, &args) {
let mut geno = genotypes.lock().expect("Unable to lock genotypes mutex");
geno.push(output);
} else {
error!("Problem processing {repeat}");
}
});
let mut genotypes_vec = genotypes.lock().unwrap();
// The final output is sorted by chrom, start and end
genotypes_vec.sort_unstable();
Expand Down
10 changes: 6 additions & 4 deletions src/repeats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::io;

#[derive(Debug)]
pub struct RepeatIntervalIterator {
// Add fields here that you need for iteration.
// For example, you might need a current index and a vector of RepeatInterval.
current_index: usize,
data: Vec<RepeatInterval>,
pub num_intervals: usize,
}

impl RepeatIntervalIterator {
Expand All @@ -27,6 +26,7 @@ impl RepeatIntervalIterator {
RepeatIntervalIterator {
current_index: 0,
data: vec![repeat],
num_intervals: 1,
}
}
pub fn from_bed(region_file: &String, fasta: &str) -> Self {
Expand All @@ -41,7 +41,8 @@ impl RepeatIntervalIterator {
}
RepeatIntervalIterator {
current_index: 0,
data,
data: data.clone(),
num_intervals: data.len(),
}
}

Expand All @@ -61,7 +62,8 @@ impl RepeatIntervalIterator {
}
RepeatIntervalIterator {
current_index: 0,
data,
data: data.clone(),
num_intervals: data.len(),
}
}
}
Expand Down

0 comments on commit 6e91688

Please sign in to comment.