Skip to content

Commit ac93329

Browse files
committed
Fixed index out of bounds bug
I tried using this on non-size-selected nanopore data, and some reads were shorter than k, which caused an out of bounds error.
1 parent bae4095 commit ac93329

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "trio_binning"
3-
version = "0.1.0"
4-
authors = ["Edward S. Rice <esrice@ucsc.edu>"]
3+
version = "0.2.3"
4+
authors = ["Edward S. Rice <edward.rice@missouri.edu>"]
55

66
[dependencies]
77
flate2 = "1.0.2"

src/classify.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ pub fn count_kmers_in_read(hap_a_kmers: &kmer::KmerSet,
2929
let mut hap_a_count: u32 = 0;
3030
let mut hap_b_count: u32 = 0;
3131

32-
for i in 0..(read.seq.len() - k + 1) {
33-
let bits = kmer::get_canonical_repr(&read.seq[i..i+k])
34-
.and_then(|kmer| kmer::kmer_to_bits(&kmer))?;
32+
if read.seq.len() >= k {
33+
for i in 0..(read.seq.len() - k + 1) {
34+
let bits = kmer::get_canonical_repr(&read.seq[i..i+k])
35+
.and_then(|kmer| kmer::kmer_to_bits(&kmer))?;
3536

36-
if hap_a_kmers.contains(&bits) {
37-
hap_a_count += 1;
38-
}
39-
// hap_a_kmers and hap_b_kmers *should* be mutually exclusive sets, so
40-
// it shouldn't matter whether this is if or else if, but I'm doing it
41-
// this way so that the answer is still correct even if they aren't.
42-
if hap_b_kmers.contains(&bits) {
43-
hap_b_count += 1;
37+
if hap_a_kmers.contains(&bits) {
38+
hap_a_count += 1;
39+
}
40+
// hap_a_kmers and hap_b_kmers *should* be mutually exclusive sets, so
41+
// it shouldn't matter whether this is if or else if, but I'm doing it
42+
// this way so that the answer is still correct even if they aren't.
43+
if hap_b_kmers.contains(&bits) {
44+
hap_b_count += 1;
45+
}
4446
}
4547
}
4648

0 commit comments

Comments
 (0)