Skip to content

Commit

Permalink
perf(derive/readlen): don't iterate through all read_lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Dec 15, 2023
1 parent 76305f2 commit d63eb2a
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/derive/readlen/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,20 @@ pub fn predict(
num_samples: u64,
majority_vote_cutoff: f64,
) -> Result<DerivedReadlenResult, anyhow::Error> {
let mut max_count = 0;
let mut max_read_length = 0;

for (read_length, count) in &read_lengths {
if *read_length > max_read_length {
max_read_length = *read_length;
max_count = *count;
}
}

if num_samples <= 0 {
bail!("No read lengths were detected in the file.");
}

let consensus_read_length = max_read_length;
let majority_detected = max_count as f64 / num_samples as f64;

// Sort the read lengths by their key for output.
let mut read_lengths: Vec<(u32, u64)> = read_lengths.into_iter().collect();
read_lengths.sort_by(|a, b| b.0.cmp(&a.0));

let max_read_length = read_lengths[0].0;
let max_count = read_lengths[0].1;

let consensus_read_length = max_read_length;
let majority_detected = max_count as f64 / num_samples as f64;

let mut result =
DerivedReadlenResult::new(false, None, majority_detected * 100.0, read_lengths);

Expand Down

0 comments on commit d63eb2a

Please sign in to comment.