Skip to content

Commit

Permalink
sam/io/reader/query: Remove FilterByRegion
Browse files Browse the repository at this point in the history
This is merged into Query.
  • Loading branch information
zaeleus committed Jun 5, 2024
1 parent 2347719 commit d1f042c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 57 deletions.
17 changes: 8 additions & 9 deletions noodles-sam/src/io/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use noodles_csi::BinningIndex;

pub(crate) use self::record::read_record;
pub use self::{builder::Builder, record_bufs::RecordBufs};
use self::{header::read_header, record_buf::read_record_buf};
use self::{header::read_header, query::Query, record_buf::read_record_buf};
use crate::{alignment::RecordBuf, header::ReferenceSequences, Header, Record};

/// A SAM reader.
Expand Down Expand Up @@ -319,22 +319,21 @@ where
/// }
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn query<'a, I>(
&'a mut self,
header: &'a Header,
pub fn query<'r, 'h: 'r, I>(
&'r mut self,
header: &'h Header,
index: &I,
region: &Region,
) -> io::Result<impl Iterator<Item = io::Result<Record>> + 'a>
) -> io::Result<impl Iterator<Item = io::Result<Record>> + 'r>
where
I: BinningIndex,
{
use self::query::{FilterByRegion, Query};

let reference_sequence_id = resolve_region(header.reference_sequences(), region)?;
let chunks = index.query(reference_sequence_id, region.interval())?;

Ok(FilterByRegion::new(
Query::new(self.get_mut(), chunks),
Ok(Query::new(
self.get_mut(),
chunks,
header,
reference_sequence_id,
region.interval(),
Expand Down
68 changes: 20 additions & 48 deletions noodles-sam/src/io/reader/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@ use noodles_csi::{self as csi, binning_index::index::reference_sequence::bin::Ch
use super::Reader;
use crate::{alignment::Record as _, Header, Record};

pub struct Query<'a, R> {
reader: Reader<csi::io::Query<'a, R>>,
pub struct Query<'r, 'h, R> {
reader: Reader<csi::io::Query<'r, R>>,
header: &'h Header,
reference_sequence_id: usize,
interval: Interval,
record: Record,
}

impl<'a, R> Query<'a, R>
impl<'r, 'h, R> Query<'r, 'h, R>
where
R: bgzf::io::BufRead + bgzf::io::Seek,
{
pub(super) fn new(reader: &'a mut R, chunks: Vec<Chunk>) -> Self {
pub(super) fn new(
reader: &'r mut R,
chunks: Vec<Chunk>,
header: &'h Header,
reference_sequence_id: usize,
interval: Interval,
) -> Self {
Self {
reader: Reader::new(csi::io::Query::new(reader, chunks)),
header,
reference_sequence_id,
interval,
record: Record::default(),
}
}
Expand All @@ -31,57 +43,17 @@ where
}
}

impl<'a, R> Iterator for Query<'a, R>
impl<'r, 'h, R> Iterator for Query<'r, 'h, R>
where
R: bgzf::io::BufRead + bgzf::io::Seek,
{
type Item = io::Result<Record>;

fn next(&mut self) -> Option<Self::Item> {
match self.next_record() {
Ok(Some(record)) => Some(Ok(record)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
}
}

pub struct FilterByRegion<'h, I> {
records: I,
header: &'h Header,
reference_sequence_id: usize,
interval: Interval,
}

impl<'h, I> FilterByRegion<'h, I>
where
I: Iterator<Item = io::Result<Record>>,
{
pub fn new(
records: I,
header: &'h Header,
reference_sequence_id: usize,
interval: Interval,
) -> Self {
Self {
records,
header,
reference_sequence_id,
interval,
}
}
}

impl<'h, I> Iterator for FilterByRegion<'h, I>
where
I: Iterator<Item = io::Result<Record>>,
{
type Item = io::Result<Record>;

fn next(&mut self) -> Option<Self::Item> {
loop {
let record = match self.records.next()? {
Ok(record) => record,
let record = match self.next_record() {
Ok(Some(record)) => record,
Ok(None) => return None,
Err(e) => return Some(Err(e)),
};

Expand Down

0 comments on commit d1f042c

Please sign in to comment.