-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(test)] | ||
pub mod parse_intersections; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use crate::intersection::{Intersection, Intersections}; | ||
use crate::interval::Interval; | ||
use crate::position::Position; | ||
use crate::string::String; | ||
use linear_map::LinearMap; | ||
use std::sync::Arc; | ||
|
||
#[cfg(test)] | ||
pub(crate) fn parse_intersections(input: &str) -> Intersections { | ||
let mut intersections = Vec::new(); | ||
let mut base_interval = None; | ||
|
||
for line in input.lines() { | ||
let line = line.trim(); | ||
|
||
if line.is_empty() { | ||
continue; | ||
} | ||
|
||
let mut parts = line.split(':'); | ||
let mut id = 0; | ||
|
||
if let (Some(name), Some(ranges)) = (parts.next(), parts.next()) { | ||
let name = name.trim(); | ||
id += 1; | ||
|
||
let ranges: Vec<(u64, u64)> = ranges | ||
.split(',') | ||
.map(|range| { | ||
let range = range.trim(); | ||
let mut range_parts = range.split('-'); | ||
|
||
if let (Some(start), Some(end)) = (range_parts.next(), range_parts.next()) { | ||
if let (Ok(start), Ok(end)) = (start.parse(), end.parse()) { | ||
return (start, end); | ||
} | ||
} | ||
|
||
panic!("Invalid range format: {}", range); | ||
}) | ||
.collect(); | ||
|
||
if name == "a" { | ||
assert_eq!(ranges.len(), 1); | ||
let interval = Interval { | ||
chrom: String::from("chr1"), | ||
start: ranges[0].0, | ||
stop: ranges[0].1, | ||
fields: LinearMap::new(), | ||
}; | ||
base_interval = Some(interval); | ||
} else { | ||
for se in ranges { | ||
let interval = Interval { | ||
chrom: String::from("chr1"), | ||
start: se.0, | ||
stop: se.1, | ||
fields: LinearMap::new(), | ||
}; | ||
|
||
intersections.push(Intersection { | ||
interval: Arc::new(Position::Interval(interval)), | ||
id: id, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
let base_interval = base_interval.expect("No base interval found"); | ||
|
||
Intersections { | ||
base_interval: Arc::new(Position::Interval(base_interval)), | ||
overlapping: intersections, | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_parse() { | ||
let input = "a: 1-10\nb: 3-6, 8-12"; | ||
let intersections = parse_intersections(input); | ||
|
||
// Access the generated Intersections struct | ||
assert_eq!(intersections.base_interval.start(), 1); | ||
assert_eq!(intersections.base_interval.stop(), 10); | ||
eprintln!("{:?}", intersections.overlapping); | ||
assert_eq!(intersections.overlapping.len(), 2); | ||
assert_eq!(intersections.overlapping[0].interval.start(), 3); | ||
assert_eq!(intersections.overlapping[0].interval.stop(), 6); | ||
assert_eq!(intersections.overlapping[1].interval.start(), 8); | ||
assert_eq!(intersections.overlapping[1].interval.stop(), 12); | ||
} |