Skip to content

Commit

Permalink
Fixing lints.
Browse files Browse the repository at this point in the history
  • Loading branch information
tfenne committed Feb 18, 2023
1 parent 5c2030e commit 0d4bb88
Showing 1 changed file with 42 additions and 33 deletions.
75 changes: 42 additions & 33 deletions src/commands/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,49 +251,58 @@ impl Count {
parse_path(Some(fastq), |parser| {
let mut read_offsets = Vec::<usize>::with_capacity(5);

parser.each(|rec| {
let read_bases = rec.seq();
let read_length = read_bases.len();
parser
.each(|rec| {
let read_bases = rec.seq();
let read_length = read_bases.len();

if read_length >= guide_length {
for trim in 0..=(read_length - guide_length) {
let bases = &read_bases[trim..trim + guide_length];
if read_length >= guide_length {
for trim in 0..=(read_length - guide_length) {
let bases = &read_bases[trim..trim + guide_length];

if lookup.contains_key(bases) {
read_offsets.push(trim);
if lookup.contains_key(bases) {
read_offsets.push(trim);
}
}
}
}

// If the read only matched at a single offset, just use it; otherwise prefer
// match(es) that are perfect matches and allocation proportionally.
if read_offsets.len() == 1 {
prefix_lengths[read_offsets[0]] += 1.0;
}
else if read_offsets.len() > 1 {
let perfect_match_offsets = read_offsets.iter().copied().filter(|&off| {
let bases = &read_bases[off..off + guide_length];
let guide = lookup.get(bases).unwrap();
guide.bases.as_slice() == bases
}).collect_vec();

let preferred_offsets = if perfect_match_offsets.is_empty() { &read_offsets } else { &perfect_match_offsets };
let addend = 1f64 / preferred_offsets.len() as f64;
for offset in preferred_offsets.iter().copied() {
prefix_lengths[offset] += addend;
// If the read only matched at a single offset, just use it; otherwise prefer
// match(es) that are perfect matches and allocation proportionally.
#[allow(clippy::comparison_chain)]
if read_offsets.len() == 1 {
prefix_lengths[read_offsets[0]] += 1.0;
} else if read_offsets.len() > 1 {
let perfect_match_offsets = read_offsets
.iter()
.copied()
.filter(|&off| {
let bases = &read_bases[off..off + guide_length];
let guide = lookup.get(bases).unwrap();
guide.bases.as_slice() == bases
})
.collect_vec();

let preferred_offsets = if perfect_match_offsets.is_empty() {
&read_offsets
} else {
&perfect_match_offsets
};
let addend = 1.0 / preferred_offsets.len() as f64;
for offset in preferred_offsets.iter().copied() {
prefix_lengths[offset] += addend;
}
}
}

read_offsets.clear();
count += 1;
count < sample_size
})
.expect("Failed to parse.");
read_offsets.clear();
count += 1;
count < sample_size
})
.expect("Failed to parse.");
})
.context(format!("Failed to read {:?}", fastq))?;

let total_matched: f64 = prefix_lengths.iter().sum();
let fraction_matched = total_matched as f64 / count as f64;
let fraction_matched = total_matched / count as f64;
info!(
"In {:?} examined {} reads for guide start position and matched {} ({:.4}).",
fastq, count, total_matched, fraction_matched
Expand All @@ -312,7 +321,7 @@ impl Count {
// Filter to just those trim lengths that have at least min_fraction of the data each
let trims_to_return: Vec<usize> = non_zeros
.into_iter()
.filter(|(_idx, n)| *n as f64 / total_matched as f64 >= min_fraction)
.filter(|(_idx, n)| *n / total_matched >= min_fraction)
.map(|(idx, _n)| idx)
.collect();

Expand Down

0 comments on commit 0d4bb88

Please sign in to comment.