Skip to content

Commit dc3cee6

Browse files
committed
work on vcf
1 parent 7c7778d commit dc3cee6

File tree

7 files changed

+59
-31
lines changed

7 files changed

+59
-31
lines changed

src/bedder_bed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ mod tests {
233233
),
234234
]);
235235

236-
let it = IntersectionIterator::new(ar, vec![Box::new(br)], &chrom_order)
236+
let it = IntersectionIterator::new(Box::new(ar), vec![Box::new(br)], &chrom_order)
237237
.expect("error creating iterator");
238238

239239
let mut n = 0;

src/bedder_vcf.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ impl BedderVCF {
2525
};
2626
Ok(v)
2727
}
28+
29+
pub fn from_path(p: &str) -> io::Result<BedderVCF> {
30+
if p == "-" || p == "stdin" || p == "/dev/stdin" {
31+
let r =
32+
bcf::Reader::from_stdin().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
33+
BedderVCF::new(r)
34+
} else {
35+
let r =
36+
bcf::Reader::from_path(p).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
37+
BedderVCF::new(r)
38+
}
39+
}
2840
}
2941

3042
pub trait Skip {
@@ -173,6 +185,4 @@ impl crate::position::PositionedIterator for BedderVCF {
173185

174186
// tests
175187
#[cfg(test)]
176-
mod tests {
177-
178-
}
188+
mod tests {}

src/intersection.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::sync::Arc;
1313
use crate::position::{Position, PositionedIterator};
1414

1515
/// An iterator that returns the intersection of multiple iterators.
16-
pub struct IntersectionIterator<'a, P: PositionedIterator> {
17-
base_iterator: P,
16+
pub struct IntersectionIterator<'a> {
17+
base_iterator: Box<dyn PositionedIterator>,
1818
other_iterators: Vec<Box<dyn PositionedIterator>>,
1919
min_heap: BinaryHeap<ReverseOrderPosition>,
2020
chromosome_order: &'a HashMap<String, Chromosome>,
@@ -127,7 +127,7 @@ fn region_str(p: &Position) -> std::string::String {
127127
}
128128

129129
/// An iterator that returns the intersection of multiple iterators for each query interval
130-
impl<P: PositionedIterator> Iterator for IntersectionIterator<'_, P> {
130+
impl Iterator for IntersectionIterator<'_> {
131131
type Item = io::Result<Intersections>;
132132

133133
fn next(&mut self) -> Option<Self::Item> {
@@ -211,9 +211,9 @@ impl<P: PositionedIterator> Iterator for IntersectionIterator<'_, P> {
211211
}
212212

213213
/// Create a new IntersectionIterator given a query (base) and a vector of other positioned iterators.
214-
impl<'a, P: PositionedIterator> IntersectionIterator<'a, P> {
214+
impl<'a> IntersectionIterator<'a> {
215215
pub fn new(
216-
base_iterator: P,
216+
base_iterator: Box<dyn PositionedIterator>,
217217
other_iterators: Vec<Box<dyn PositionedIterator>>,
218218
chromosome_order: &'a HashMap<String, Chromosome>,
219219
) -> io::Result<Self> {
@@ -497,8 +497,9 @@ mod tests {
497497

498498
//let a_ivs: Box<dyn PositionedIterator> = Box::new(a_ivs);
499499

500-
let mut iter = IntersectionIterator::new(a_ivs, vec![Box::new(b_ivs)], &chrom_order)
501-
.expect("error getting iterator");
500+
let mut iter =
501+
IntersectionIterator::new(Box::new(a_ivs), vec![Box::new(b_ivs)], &chrom_order)
502+
.expect("error getting iterator");
502503
let mut n = 0;
503504
assert!(iter.all(|intersection| {
504505
let intersection = intersection.expect("error getting intersection");
@@ -573,7 +574,7 @@ mod tests {
573574
],
574575
);
575576

576-
let iter = IntersectionIterator::new(a_ivs, vec![Box::new(b_ivs)], &chrom_order)
577+
let iter = IntersectionIterator::new(Box::new(a_ivs), vec![Box::new(b_ivs)], &chrom_order)
577578
.expect("error getting iterator");
578579
iter.for_each(|intersection| {
579580
let intersection = intersection.expect("intersection");
@@ -609,8 +610,8 @@ mod tests {
609610
},
610611
],
611612
);
612-
let mut iter =
613-
IntersectionIterator::new(a_ivs, vec![], &chrom_order).expect("error getting iterator");
613+
let mut iter = IntersectionIterator::new(Box::new(a_ivs), vec![], &chrom_order)
614+
.expect("error getting iterator");
614615

615616
let e = iter.nth(1).expect("error getting next");
616617
assert!(e.is_err());
@@ -639,8 +640,8 @@ mod tests {
639640
},
640641
],
641642
);
642-
let mut iter =
643-
IntersectionIterator::new(a_ivs, vec![], &chrom_order).expect("error getting iterator");
643+
let mut iter = IntersectionIterator::new(Box::new(a_ivs), vec![], &chrom_order)
644+
.expect("error getting iterator");
644645

645646
let e = iter.nth(1).expect("error getting next");
646647
assert!(e.is_err());
@@ -684,8 +685,9 @@ mod tests {
684685
],
685686
);
686687

687-
let mut iter = IntersectionIterator::new(a_ivs, vec![Box::new(b_ivs)], &chrom_order)
688-
.expect("error getting iterator");
688+
let mut iter =
689+
IntersectionIterator::new(Box::new(a_ivs), vec![Box::new(b_ivs)], &chrom_order)
690+
.expect("error getting iterator");
689691
let e = iter.next().expect("error getting next");
690692
assert!(e.is_err());
691693
let e = e.err().unwrap();
@@ -723,9 +725,12 @@ mod tests {
723725
..Default::default()
724726
}],
725727
);
726-
let iter =
727-
IntersectionIterator::new(a_ivs, vec![Box::new(b_ivs), Box::new(c_ivs)], &chrom_order)
728-
.expect("error getting iterator");
728+
let iter = IntersectionIterator::new(
729+
Box::new(a_ivs),
730+
vec![Box::new(b_ivs), Box::new(c_ivs)],
731+
&chrom_order,
732+
)
733+
.expect("error getting iterator");
729734
let c = iter
730735
.map(|intersection| {
731736
let intersection = intersection.expect("error getting intersection");
@@ -765,7 +770,7 @@ mod tests {
765770
..Default::default()
766771
}],
767772
);
768-
let iter = IntersectionIterator::new(a_ivs, vec![Box::new(b_ivs)], &chrom_order)
773+
let iter = IntersectionIterator::new(Box::new(a_ivs), vec![Box::new(b_ivs)], &chrom_order)
769774
.expect("error getting iterator");
770775
// check that it overlapped by asserting that the loop ran and also that there was an overlap within the loop.
771776
let c = iter

src/intersections.rs

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ impl Intersections {
165165
&report_options.b_mode,
166166
) {
167167
drop(base);
168+
log::info!("pushing here");
168169
self.push_overlap_fragments(
169170
&mut result,
170171
overlaps,
@@ -307,6 +308,7 @@ impl Intersections {
307308
.iter()
308309
.map(|o| {
309310
// TODO: avoid clone_box heere if not needed.
311+
// TODO: FIRST!! here we are pushing on the b-interval as the a-interval. need to clone the a interval and set start stop on it.
310312
let oi = o.interval.try_lock().expect("failed to lock interval");
311313
let bi = self
312314
.base_interval

src/main.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
107107

108108
let afhile = BufReader::new(File::open(&args.query_path)?);
109109

110-
let a_iter = bedder::sniff::open(afhile, &args.query_path)?;
110+
let a_iter = bedder::sniff::open(afhile, &args.query_path)?.into_positioned_iterator();
111111
let b_iters: Vec<_> = args
112112
.other_paths
113113
.iter()
@@ -117,9 +117,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
117117
})
118118
.collect::<Result<Vec<_>, _>>()?;
119119

120-
let bedder::sniff::BedderReader::BedderBed(aiter) = a_iter;
121-
122-
let ii = bedder::intersection::IntersectionIterator::new(aiter, b_iters, &chrom_order)?;
120+
let ii = bedder::intersection::IntersectionIterator::new(a_iter, b_iters, &chrom_order)?;
123121

124122
/*
125123
let mut output: Box<dyn Write> = if args.output_path.to_str().unwrap() == "-" {

src/sniff.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::bedder_bed::BedderBed;
2+
use crate::bedder_vcf::BedderVCF;
23
use crate::position::PositionedIterator;
34
use flate2::bufread::GzDecoder;
45
use log::info;
@@ -25,6 +26,7 @@ where
2526
R: io::BufRead + io::Seek + 'static,
2627
{
2728
BedderBed(BedderBed<'static, R>),
29+
BedderVcf(BedderVCF),
2830
}
2931

3032
impl<R> BedderReader<R>
@@ -38,6 +40,7 @@ where
3840
pub fn into_positioned_iterator(self) -> Box<dyn PositionedIterator> {
3941
match self {
4042
BedderReader::BedderBed(rdr) => Box::new(rdr),
43+
BedderReader::BedderVcf(rdr) => Box::new(rdr),
4144
}
4245
}
4346
}
@@ -51,7 +54,9 @@ pub fn open<P: AsRef<Path>, R: io::BufRead + io::Seek + 'static>(
5154
info!("sniffed file type: {:?}, compression: {:?}", ft, c);
5255
let rdr = match ft {
5356
FileType::Bed => BedderReader::BedderBed(BedderBed::new(reader, Some(p))),
54-
//FileType::Vcf => BedderReader::BedderVcf(BedderVcf::new(reader, Some(p))),
57+
FileType::Vcf => {
58+
BedderReader::BedderVcf(BedderVCF::from_path(p.as_ref().to_str().unwrap())?)
59+
}
5560
_ => unimplemented!("Unsupported file type {:?}", ft),
5661
};
5762
Ok(rdr)

src/writer.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,13 @@ impl Writer {
272272
}
273273
}
274274
}
275-
// TODO first: add b stuff to the a interval
275+
log::info!("frag.a: {:?}", frag.a);
276276
if let Position::Bed(ref mut bed_record) = *frag
277277
.a
278278
.as_ref()
279-
.expect("Position is not a BED interval")
279+
.expect("Fragment Position is not a BED interval")
280280
.try_lock()
281-
.expect("Failed to lock Position")
281+
.expect("Failed to lock BED Position")
282282
{
283283
// Add all values to our clone
284284
for value in values {
@@ -287,10 +287,18 @@ impl Writer {
287287

288288
// Replace the entire Arc with our updated version
289289
//intersections.base_interval = Arc::new(Position::Bed(new_bed_record));
290+
} else if let Position::Vcf(_) = *frag
291+
.a
292+
.as_ref()
293+
.expect("Fragment Position is not a VCF record")
294+
.try_lock()
295+
.expect("Failed to lock VCF Position")
296+
{
297+
unimplemented!("VCF writing not yet implemented");
290298
} else {
291299
return Err(std::io::Error::new(
292300
std::io::ErrorKind::InvalidData,
293-
"Position is not a BED interval",
301+
"Fragment Position is not implemented interval",
294302
));
295303
}
296304
}

0 commit comments

Comments
 (0)