Skip to content

Commit 12e00d8

Browse files
committed
feat(bam): add public constructors for IterAlignedBlocks and IterIntrons
- Add IterAlignedBlocks::new(pos, cigar) constructor - Add IterIntrons::new(pos, cigar) constructor - Allow external construction without BamRecordExtensions - Add comprehensive documentation and examples - Add unit tests for both constructors Resolves issue #472: Add public constructor for public structs
1 parent 0168666 commit 12e00d8

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

src/bam/ext.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,64 @@ pub trait BamRecordExtensions {
313313
fn seq_len_from_cigar(&self, include_hard_clip: bool) -> usize;
314314
}
315315

316+
impl IterAlignedBlocks {
317+
/// Create a new `IterAlignedBlocks` from its component parts.
318+
///
319+
/// This allows constructing an iterator over aligned blocks without needing
320+
/// access to a `BamRecordExtensions` instance.
321+
///
322+
/// # Arguments
323+
///
324+
/// * `pos` - The starting position on the reference sequence
325+
/// * `cigar` - The CIGAR operations as a vector
326+
///
327+
/// # Example
328+
///
329+
/// ```
330+
/// use rust_htslib::bam::record::Cigar;
331+
/// use rust_htslib::bam::ext::IterAlignedBlocks;
332+
///
333+
/// let cigar = vec![Cigar::Match(10), Cigar::Del(5), Cigar::Match(20)];
334+
/// let iter = IterAlignedBlocks::new(100, cigar);
335+
/// ```
336+
pub fn new(pos: i64, cigar: Vec<Cigar>) -> Self {
337+
Self {
338+
pos,
339+
cigar_index: 0,
340+
cigar,
341+
}
342+
}
343+
}
344+
345+
impl IterIntrons {
346+
/// Create a new `IterIntrons` from its component parts.
347+
///
348+
/// This allows constructing an iterator over introns without needing
349+
/// access to a `BamRecordExtensions` instance.
350+
///
351+
/// # Arguments
352+
///
353+
/// * `pos` - The starting position on the reference sequence
354+
/// * `cigar` - The CIGAR operations as a vector
355+
///
356+
/// # Example
357+
///
358+
/// ```
359+
/// use rust_htslib::bam::record::Cigar;
360+
/// use rust_htslib::bam::ext::IterIntrons;
361+
///
362+
/// let cigar = vec![Cigar::Match(10), Cigar::RefSkip(1000), Cigar::Match(20)];
363+
/// let iter = IterIntrons::new(100, cigar);
364+
/// ```
365+
pub fn new(pos: i64, cigar: Vec<Cigar>) -> Self {
366+
Self {
367+
pos,
368+
cigar_index: 0,
369+
cigar,
370+
}
371+
}
372+
}
373+
316374
impl BamRecordExtensions for bam::Record {
317375
fn aligned_blocks(&self) -> IterAlignedBlocks {
318376
IterAlignedBlocks {
@@ -1022,6 +1080,26 @@ mod tests {
10221080
assert_eq!(introns[1], [44589703, 44592034]);
10231081
}
10241082

1083+
#[test]
1084+
fn test_iter_constructors() {
1085+
use crate::bam::ext::{IterAlignedBlocks, IterIntrons};
1086+
1087+
// Test IterAlignedBlocks constructor
1088+
let cigar = vec![Cigar::Match(10), Cigar::Del(5), Cigar::Match(20)];
1089+
let mut iter = IterAlignedBlocks::new(100, cigar);
1090+
let blocks: Vec<_> = iter.collect();
1091+
assert_eq!(blocks.len(), 2);
1092+
assert_eq!(blocks[0], [100, 110]);
1093+
assert_eq!(blocks[1], [115, 135]);
1094+
1095+
// Test IterIntrons constructor
1096+
let cigar = vec![Cigar::Match(10), Cigar::RefSkip(1000), Cigar::Match(20)];
1097+
let mut iter = IterIntrons::new(100, cigar);
1098+
let introns: Vec<_> = iter.collect();
1099+
assert_eq!(introns.len(), 1);
1100+
assert_eq!(introns[0], [110, 1110]);
1101+
}
1102+
10251103
#[test]
10261104
fn test_aligned_pairs() {
10271105
let mut bam = bam::Reader::from_path("./test/test_spliced_reads.bam").unwrap();

0 commit comments

Comments
 (0)