-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbedder_bed.rs
172 lines (155 loc) · 4.9 KB
/
bedder_bed.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#![allow(clippy::useless_conversion)] // these are needed to support e.g. smartstring
use crate::position::{Field, FieldError, Position, Positioned, Value, Valued};
use crate::string::String;
pub use bed::record::Record;
pub use noodles::bed;
use std::io::{self, BufRead};
use std::result;
impl crate::position::Positioned for bed::record::Record<3> {
#[inline]
fn chrom(&self) -> &str {
self.reference_sequence_name()
}
#[inline]
fn start(&self) -> u64 {
// noodles position is 1-based.
self.start_position().get() as u64 - 1
}
#[inline]
fn stop(&self) -> u64 {
self.end_position().get() as u64
}
}
impl Valued for bed::record::Record<3> {
fn value(&self, v: crate::position::Field) -> result::Result<Value, FieldError> {
match v {
Field::String(s) => Ok(Value::Strings(vec![s])),
Field::Int(i) => match i {
0 => Ok(Value::Strings(vec![String::from(self.chrom())])),
1 => Ok(Value::Ints(vec![self.start() as i64])),
2 => Ok(Value::Ints(vec![self.stop() as i64])),
_ => Err(FieldError::InvalidFieldIndex(i)),
},
}
}
}
struct Last {
chrom: String,
start: u64,
stop: u64,
}
pub struct BedderBed<R>
where
R: BufRead,
{
reader: bed::Reader<R>,
buf: std::string::String,
last_record: Option<Last>,
line_number: u64,
}
impl<R> BedderBed<R>
where
R: BufRead,
{
pub fn new(r: R) -> BedderBed<R> {
BedderBed {
reader: bed::Reader::new(r),
buf: std::string::String::new(),
last_record: None,
line_number: 0,
}
}
}
impl<R> crate::position::PositionedIterator for BedderBed<R>
where
R: BufRead,
{
fn next_position(
&mut self,
_q: Option<&crate::position::Position>,
) -> Option<std::result::Result<Position, std::io::Error>> {
self.buf.clear();
loop {
self.line_number += 1;
return match self.reader.read_line(&mut self.buf) {
Ok(0) => None,
Ok(_) => {
if self.buf.starts_with('#') || self.buf.is_empty() {
continue;
}
let record: bed::record::Record<3> = match self.buf.parse() {
Err(e) => {
let msg = format!(
"line#{:?}:{:?} error: {:?}",
self.line_number, &self.buf, e
);
return Some(Err(io::Error::new(io::ErrorKind::InvalidData, msg)));
}
Ok(r) => r,
};
match &mut self.last_record {
None => {
self.last_record = Some(Last {
chrom: String::from(record.chrom()),
start: record.start(),
stop: record.stop(),
})
}
Some(r) => {
if r.chrom != record.chrom() {
r.chrom = String::from(record.chrom())
}
r.start = record.start();
r.stop = record.stop();
}
}
Some(Ok(Position::Bed(record)))
}
Err(e) => Some(Err(e)),
};
}
}
fn name(&self) -> String {
String::from(format!("bed:{}", self.line_number))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::chrom_ordering::Chromosome;
use crate::intersection::IntersectionIterator;
use hashbrown::HashMap;
use std::io::Cursor;
#[test]
fn test_bed_read() {
// write a test for bed from a string using BufRead
let ar = BedderBed::new(Cursor::new("chr1\t20\t30\nchr1\t21\t33"));
let br = BedderBed::new(Cursor::new("chr1\t21\t30\nchr1\t22\t33"));
let chrom_order = HashMap::from([
(
String::from("chr1"),
Chromosome {
index: 0usize,
length: None,
},
),
(
String::from("chr2"),
Chromosome {
index: 1usize,
length: None,
},
),
]);
let it = IntersectionIterator::new(Box::new(ar), vec![Box::new(br)], &chrom_order)
.expect("error creating iterator");
let mut n = 0;
it.for_each(|int| {
let int = int.expect("error getting intersection");
//dbg!(&int.overlapping);
assert!(int.overlapping.len() == 2);
n += 1;
});
assert!(n == 2);
}
}