Skip to content

Commit 6b57e3c

Browse files
Fixed bug in variant that led to skipping chromosomes.
1 parent 85ea0ae commit 6b57e3c

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

src/bcf/match_variants.rs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,51 @@ impl RecordBuffer {
214214
}
215215
}
216216

217-
pub fn rid(&self) -> Option<u32> {
217+
pub fn last_rid(&self) -> Option<u32> {
218218
self.ringbuffer.back().map(|var| var.rid)
219219
}
220220

221+
pub fn next_rid(&self) -> Option<u32> {
222+
self.ringbuffer2.back().map(|var| var.rid)
223+
}
224+
225+
fn swap_buffers(&mut self) {
226+
// swap with buffer for next rid
227+
mem::swap(&mut self.ringbuffer2, &mut self.ringbuffer);
228+
// clear second buffer
229+
self.ringbuffer2.clear();
230+
}
231+
232+
fn drain_left(&mut self, rid: u32, window_start: u32) {
233+
// remove records too far left or from wrong rid
234+
let to_remove = self.ringbuffer.iter().take_while(|var| var.pos < window_start || var.rid != rid).count();
235+
self.ringbuffer.drain(..to_remove);
236+
}
237+
221238
pub fn fill(&mut self, chrom: &[u8], pos: u32) -> Result<(), Box<Error>> {
222239
let window_start = cmp::max(pos as i32 - self.window as i32 - 1, 0) as u32;
223240
let window_end = pos + self.window;
224241
let rid = try!(self.reader.header.name2rid(chrom));
225242

226-
if let Some(last_rid) = self.rid() {
227-
if rid != last_rid {
228-
// swap with buffer for next rid
229-
mem::swap(&mut self.ringbuffer2, &mut self.ringbuffer);
230-
// clear second buffer
231-
self.ringbuffer2.clear();
232-
} else {
233-
// remove records too far left of from wrong rid
234-
let to_remove = self.ringbuffer.iter().take_while(|var| var.pos < window_start || var.rid != rid).count();
235-
self.ringbuffer.drain(..to_remove);
236-
}
243+
match (self.last_rid(), self.next_rid()) {
244+
(Some(last_rid), _) => {
245+
if last_rid != rid {
246+
self.swap_buffers();
247+
} else {
248+
self.drain_left(rid, window_start);
249+
}
250+
},
251+
(_, Some(_)) => {
252+
self.swap_buffers();
253+
self.drain_left(rid, window_start);
254+
},
255+
_ => ()
256+
}
257+
258+
if !self.ringbuffer2.is_empty() {
259+
// We have already read beyond the current rid. Hence we can't extend to the right for
260+
// this rid.
261+
return Ok(())
237262
}
238263

239264
// extend to the right

tests/expected/matching.bcf

12 Bytes
Binary file not shown.

tests/test2.vcf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
##FILTER=<ID=PASS,Description="All filters passed">
33
##contig=<ID=1>
44
##contig=<ID=2>
5+
##contig=<ID=3>
56
##FORMAT=<ID=S,Number=1,Type=String,Description="Text">
67
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
78
##INFO=<ID=T,Number=A,Type=Integer,Description="Text">
@@ -16,3 +17,4 @@
1617
2 503 . N <DEL> . . SVLEN=200 S:GT
1718
2 510 . A ATT . . . S:GT
1819
2 600 . GGT G . . SVTYPE=DEL;SVLEN=2 S:GT
20+
3 6000 . GGT G . . SVTYPE=DEL;SVLEN=2 S:GT

tests/test3.vcf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
##FILTER=<ID=PASS,Description="All filters passed">
33
##contig=<ID=1>
44
##contig=<ID=2>
5+
##contig=<ID=3>
56
##FORMAT=<ID=S,Number=1,Type=String,Description="Text">
67
##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
78
##INFO=<ID=T,Number=A,Type=Integer,Description="Text">
@@ -11,3 +12,4 @@
1112
1 100 a A G . . T=2 S:GT
1213
1 200 . A AGG . . . S:GT
1314
2 540 . GGAT G . . SVTYPE=DEL;SVLEN=3 S:GT
15+
3 6000 . GGAT G . . SVTYPE=DEL;SVLEN=3 S:GT

0 commit comments

Comments
 (0)