Skip to content

Commit

Permalink
fix parsed_bed_file and test #1
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldcampbelljr committed Mar 15, 2024
1 parent 95c9fe2 commit d73dce5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
39 changes: 12 additions & 27 deletions genimtools/src/uniwig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,20 @@ pub fn read_bed_bec(){

}

pub fn parse_bed_file(line: &str) -> Option<(String, i32, i32, Option<String>)> {
pub fn parse_bed_file(line: &str) -> Option<(String, i32, i32)> {
// TODO Eventually refactor all bed file parsing to a single shared function

let mut iter = line.split('\t');
let mut ctg: Option<String> = None;
let mut st = -1;
let mut en = -1;
let mut r: Option<String> = None;
let mut i = 0;

let iter = iter.by_ref();

while let Some(item) = iter.next() {
match i {
0 => ctg = Some(item.to_owned()),
1 => st = item.parse().unwrap_or(-1),
2 => {
en = item.parse().unwrap_or(-1);
r = iter.next().map(|x| x.to_owned());
}
_ => break,
}
i += 1;
if i == 3 || iter.next().is_none() {
break;
}
}

Some((ctg?, st, en, r))
let mut fields = line.split('\t');
// Get the first field which should be chromosome.
let ctg = fields.next()?;
// Parse 2nd and 3rd string as integers or return -1 if failure
let st = fields.next().and_then(|s| s.parse::<i32>().ok()).unwrap_or(-1);
let en = fields.next().and_then(|s| s.parse::<i32>().ok()).unwrap_or(-1);

// Original code had a remainder of the line, r, but it does not appear to have been used
// in any way

Some((ctg.parse().unwrap(), st, en))

}

Expand Down
3 changes: 1 addition & 2 deletions genimtools/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,11 @@ mod tests {

let result = parse_bed_file(&first_line);

if let Some((ctg, st, en, r)) = result {
if let Some((ctg, st, en)) = result {

println!("ctg: {}", ctg);
println!("st: {}", st);
println!("en: {}", en);
println!("r: {:?}", r);
assert_eq!(st, 7915738);
} else {
println!("Failed to parse BED record");
Expand Down

0 comments on commit d73dce5

Please sign in to comment.