Skip to content

Commit

Permalink
sam/io/reader/query: Extract next record reader
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jun 6, 2024
1 parent d1f042c commit 1dd365a
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions noodles-sam/src/io/reader/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ where
record: Record::default(),
}
}

fn next_record(&mut self) -> io::Result<Option<Record>> {
self.reader.read_record(&mut self.record).map(|n| match n {
0 => None,
_ => Some(self.record.clone()),
})
}
}

impl<'r, 'h, R> Iterator for Query<'r, 'h, R>
Expand All @@ -50,23 +43,16 @@ where
type Item = io::Result<Record>;

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

match intersects(
self.header,
&record,
self.reference_sequence_id,
self.interval,
) {
Ok(true) => return Some(Ok(record)),
Ok(false) => {}
Err(e) => return Some(Err(e)),
}
match next_record(
&mut self.reader,
&mut self.record,
self.header,
self.reference_sequence_id,
self.interval,
) {
Ok(0) => None,
Ok(_) => Some(Ok(self.record.clone())),
Err(e) => Some(Err(e)),
}
}
}
Expand All @@ -89,3 +75,25 @@ fn intersects(
_ => Ok(false),
}
}

fn next_record<R>(
reader: &mut Reader<csi::io::Query<'_, R>>,
record: &mut Record,
header: &Header,
reference_sequence_id: usize,
interval: Interval,
) -> io::Result<usize>
where
R: bgzf::io::BufRead + bgzf::io::Seek,
{
loop {
match reader.read_record(record)? {
0 => return Ok(0),
n => {
if intersects(header, record, reference_sequence_id, interval)? {
return Ok(n);
}
}
}
}
}

0 comments on commit 1dd365a

Please sign in to comment.