Skip to content

Commit

Permalink
fix: in bam record buffer, change the start of the window to the firs…
Browse files Browse the repository at this point in the history
…t added item in last iteration (#430)

* Change the start of the window to the first added item in last iteration

* Change linting

* Pay attention when start_pos is None

* Change location of start_pos update

* Remove debug infos

* fmt

* Change to make rust clippy happy

---------

Co-authored-by: Johannes Köster <[email protected]>
  • Loading branch information
Addimator and johanneskoester authored May 10, 2024
1 parent d869fdd commit 56ee2bd
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/bam/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::str;
use crate::bam;
use crate::bam::Read;
use crate::errors::{Error, Result};

/// A buffer for BAM records. This allows access regions in a sorted BAM file while iterating
/// over it in a single pass.
/// The buffer is implemented as a ringbuffer, such that extension or movement to the right has
Expand All @@ -25,6 +24,7 @@ pub struct RecordBuffer {
cache_cigar: bool,
min_refetch_distance: u64,
buffer_record: Rc<bam::Record>,
start_pos: Option<u64>,
}

unsafe impl Sync for RecordBuffer {}
Expand All @@ -45,6 +45,7 @@ impl RecordBuffer {
cache_cigar,
min_refetch_distance: 1,
buffer_record: Rc::new(bam::Record::new()),
start_pos: Some(0),
}
}

Expand Down Expand Up @@ -89,7 +90,7 @@ impl RecordBuffer {
if self.inner.is_empty()
|| window_start.saturating_sub(self.end().unwrap()) >= self.min_refetch_distance
|| self.tid().unwrap() != tid as i32
|| self.start().unwrap() > window_start
|| self.start().unwrap() > self.start_pos.unwrap()
{
let end = self.reader.header.target_len(tid).unwrap();
self.reader.fetch((tid, window_start, end))?;
Expand Down Expand Up @@ -145,6 +146,7 @@ impl RecordBuffer {
added += 1;
}
}
self.start_pos = Some(self.start().unwrap_or(window_start));

Ok((added, deleted))
} else {
Expand Down

0 comments on commit 56ee2bd

Please sign in to comment.