Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/bam/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,64 @@ pub trait BamRecordExtensions {
fn seq_len_from_cigar(&self, include_hard_clip: bool) -> usize;
}

impl IterAlignedBlocks {
/// Create a new `IterAlignedBlocks` from its component parts.
///
/// This allows constructing an iterator over aligned blocks without needing
/// access to a `BamRecordExtensions` instance.
///
/// # Arguments
///
/// * `pos` - The starting position on the reference sequence
/// * `cigar` - The CIGAR operations as a vector
///
/// # Example
///
/// ```
/// use rust_htslib::bam::record::Cigar;
/// use rust_htslib::bam::ext::IterAlignedBlocks;
///
/// let cigar = vec![Cigar::Match(10), Cigar::Del(5), Cigar::Match(20)];
/// let iter = IterAlignedBlocks::new(100, cigar);
/// ```
pub fn new(pos: i64, cigar: Vec<Cigar>) -> Self {
Self {
pos,
cigar_index: 0,
cigar,
}
}
}

impl IterIntrons {
/// Create a new `IterIntrons` from its component parts.
///
/// This allows constructing an iterator over introns without needing
/// access to a `BamRecordExtensions` instance.
///
/// # Arguments
///
/// * `pos` - The starting position on the reference sequence
/// * `cigar` - The CIGAR operations as a vector
///
/// # Example
///
/// ```
/// use rust_htslib::bam::record::Cigar;
/// use rust_htslib::bam::ext::IterIntrons;
///
/// let cigar = vec![Cigar::Match(10), Cigar::RefSkip(1000), Cigar::Match(20)];
/// let iter = IterIntrons::new(100, cigar);
/// ```
pub fn new(pos: i64, cigar: Vec<Cigar>) -> Self {
Self {
pos,
cigar_index: 0,
cigar,
}
}
}

impl BamRecordExtensions for bam::Record {
fn aligned_blocks(&self) -> IterAlignedBlocks {
IterAlignedBlocks {
Expand Down Expand Up @@ -1022,6 +1080,26 @@ mod tests {
assert_eq!(introns[1], [44589703, 44592034]);
}

#[test]
fn test_iter_constructors() {
use crate::bam::ext::{IterAlignedBlocks, IterIntrons};

// Test IterAlignedBlocks constructor
let cigar = vec![Cigar::Match(10), Cigar::Del(5), Cigar::Match(20)];
let iter = IterAlignedBlocks::new(100, cigar);
let blocks: Vec<_> = iter.collect();
assert_eq!(blocks.len(), 2);
assert_eq!(blocks[0], [100, 110]);
assert_eq!(blocks[1], [115, 135]);

// Test IterIntrons constructor
let cigar = vec![Cigar::Match(10), Cigar::RefSkip(1000), Cigar::Match(20)];
let iter = IterIntrons::new(100, cigar);
let introns: Vec<_> = iter.collect();
assert_eq!(introns.len(), 1);
assert_eq!(introns[0], [110, 1110]);
}

#[test]
fn test_aligned_pairs() {
let mut bam = bam::Reader::from_path("./test/test_spliced_reads.bam").unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/bam/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,8 @@ impl Read for IndexedReader {
impl Drop for IndexedReader {
fn drop(&mut self) {
unsafe {
if self.itr.is_some() {
htslib::hts_itr_destroy(self.itr.unwrap());
if let Some(itr) = self.itr {
htslib::hts_itr_destroy(itr);
}
htslib::hts_close(self.htsfile);
}
Expand Down
10 changes: 4 additions & 6 deletions src/bcf/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,16 @@ impl RecordBuffer {
}

// move overflow from last fill into ringbuffer
if self.overflow.is_some() {
let pos = self.overflow.as_ref().unwrap().pos() as u64;
if let Some(overflow) = self.overflow.take() {
let pos = overflow.pos() as u64;
if pos >= start {
if pos <= end {
self.ringbuffer.push_back(self.overflow.take().unwrap());
self.ringbuffer.push_back(overflow);
added += 1;
} else {
self.overflow = Some(overflow);
return Ok((added, deleted));
}
} else {
// discard overflow
self.overflow.take();
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ impl Read for Reader {
impl Drop for Reader {
fn drop(&mut self) {
unsafe {
if self.itr.is_some() {
htslib::hts_itr_destroy(self.itr.unwrap());
if let Some(itr) = self.itr {
htslib::hts_itr_destroy(itr);
}
htslib::tbx_destroy(self.tbx);
htslib::hts_close(self.hts_file);
Expand Down