Struct noodles_bam::AsyncReader
source · [−]pub struct AsyncReader<R> { /* private fields */ }
Expand description
An async BAM reader.
Examples
use futures::TryStreamExt;
use noodles_bam as bam;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
reader.read_header().await?;
reader.read_reference_sequences().await?;
let mut records = reader.records();
while let Some(record) = records.try_next().await? {
// ...
}
Implementations
sourceimpl<R> Reader<R> where
R: AsyncRead + Unpin,
impl<R> Reader<R> where
R: AsyncRead + Unpin,
sourcepub fn get_ref(&self) -> &R
pub fn get_ref(&self) -> &R
Returns a reference to the underlying reader.
Examples
use noodles_bam as bam;
let data = [];
let reader = bam::AsyncReader::from(&data[..]);
assert!(reader.get_ref().is_empty());
sourcepub fn get_mut(&mut self) -> &mut R
pub fn get_mut(&mut self) -> &mut R
Returns a mutable reference to the underlying reader.
Examples
use noodles_bam as bam;
let data = [];
let mut reader = bam::AsyncReader::from(&data[..]);
assert!(reader.get_mut().is_empty());
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Returns the underlying reader.
Examples
use noodles_bam as bam;
let data = [];
let reader = bam::AsyncReader::from(&data[..]);
assert!(reader.into_inner().is_empty());
sourcepub async fn read_header(&mut self) -> Result<String>
pub async fn read_header(&mut self) -> Result<String>
Reads the raw SAM header.
The BAM magic number is also checked.
The position of the stream is expected to be at the start.
This returns the raw SAM header as a String
. It can subsequently be parsed as a
noodles_sam::Header
.
Examples
use noodles_bam as bam;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
let header = reader.read_header().await?;
sourcepub async fn read_reference_sequences(&mut self) -> Result<ReferenceSequences>
pub async fn read_reference_sequences(&mut self) -> Result<ReferenceSequences>
Reads the binary reference sequences after the SAM header.
This is not the same as the @SQ
records in the SAM header. A BAM has a list of reference
sequences containing name and length tuples after the SAM header and before the list of
records.
The position of the stream is expected to be directly after the header.
This returns a reference sequence dictionary (noodles_sam::header::ReferenceSequences
),
which can be used to build a minimal noodles_sam::Header
if the SAM header is empty.
Examples
use noodles_bam as bam;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
reader.read_header().await?;
let reference_sequences = reader.read_reference_sequences().await?;
sourcepub async fn read_record(&mut self, record: &mut Record) -> Result<usize>
pub async fn read_record(&mut self, record: &mut Record) -> Result<usize>
Reads a single record.
The record block size (bs
) is read from the underlying stream, and bs
bytes are read
into the given record buffer.
The stream is expected to be directly after the reference sequences or at the start of another record.
It is more ergonomic to read records using a stream (see Self::records
and
Self::query
), but using this method directly allows the reuse of a single Record
buffer.
If successful, the record block size is returned. If a block size of 0 is returned, the stream reached EOF.
Examples
use noodles_bam as bam;
use noodles_sam::alignment::Record;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
reader.read_header().await?;
reader.read_reference_sequences().await?;
let mut record = Record::default();
reader.read_record(&mut record).await?;
sourcepub async fn read_lazy_record(&mut self, record: &mut Record) -> Result<usize>
pub async fn read_lazy_record(&mut self, record: &mut Record) -> Result<usize>
Reads a single record without eagerly decoding its fields.
The record block size (bs
) is read from the underlying (input) stream and bs
bytes are
read into the lazy record’s buffer. No fields are decoded, meaning the record is not
necessarily valid. However, the structure of the byte stream is guaranteed to be
record-like.
The stream is expected to be directly after the reference sequences or at the start of another record.
If successful, the record block size is returned. If a block size of 0 is returned, the stream reached EOF.
Examples
use noodles_bam as bam;
use noodles_sam::alignment::Record;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
reader.read_header().await?;
reader.read_reference_sequences().await?;
let mut record = bam::lazy::Record::default();
reader.read_lazy_record(&mut record).await?;
sourcepub fn records(&mut self) -> impl Stream<Item = Result<Record>> + '_
pub fn records(&mut self) -> impl Stream<Item = Result<Record>> + '_
Returns an (async) stream over records starting from the current (input) stream position.
The (input) stream is expected to be directly after the reference sequences or at the start of another record.
Examples
use futures::TryStreamExt;
use noodles_bam as bam;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
reader.read_header().await?;
reader.read_reference_sequences().await?;
let mut records = reader.records();
while let Some(record) = records.try_next().await? {
// ...
}
sourcepub fn lazy_records(&mut self) -> impl Stream<Item = Result<Record>> + '_
pub fn lazy_records(&mut self) -> impl Stream<Item = Result<Record>> + '_
Returns a stream over lazy records.
The (input) stream is expected to be directly after the reference sequences or at the start of another record.
Examples
use futures::TryStreamExt;
use noodles_bam as bam;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
reader.read_header().await?;
reader.read_reference_sequences().await?;
let mut records = reader.lazy_records();
while let Some(record) = records.try_next().await? {
// ...
}
sourceimpl<R> Reader<AsyncReader<R>> where
R: AsyncRead + Unpin,
impl<R> Reader<AsyncReader<R>> where
R: AsyncRead + Unpin,
sourcepub fn builder(inner: R) -> Builder<R>
pub fn builder(inner: R) -> Builder<R>
Creates an async BAM reader builder.
Examples
use noodles_bam as bam;
let data = [];
let builder = bam::AsyncReader::builder(&data[..]);
let reader = builder.build();
sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Creates an async BAM reader.
Examples
use noodles_bam as bam;
let data = [];
let reader = bam::AsyncReader::new(&data[..]);
sourcepub fn virtual_position(&self) -> VirtualPosition
pub fn virtual_position(&self) -> VirtualPosition
Returns the current virtual position of the underlying BGZF reader.
Examples
use noodles_bam as bam;
use noodles_bgzf as bgzf;
let data = Vec::new();
let reader = bam::AsyncReader::new(&data[..]);
let virtual_position = reader.virtual_position();
assert_eq!(reader.virtual_position(), bgzf::VirtualPosition::from(0));
sourceimpl<R> Reader<AsyncReader<R>> where
R: AsyncRead + AsyncSeek + Unpin,
impl<R> Reader<AsyncReader<R>> where
R: AsyncRead + AsyncSeek + Unpin,
sourcepub async fn seek(&mut self, pos: VirtualPosition) -> Result<VirtualPosition>
pub async fn seek(&mut self, pos: VirtualPosition) -> Result<VirtualPosition>
Seeks the underlying BGZF reader to the given virtual position.
Virtual positions typically come from the associated BAM index file.
Examples
use noodles_bam as bam;
use noodles_bgzf as bgzf;
let data = [];
let mut reader = bam::AsyncReader::new(Cursor::new(data));
let virtual_position = bgzf::VirtualPosition::default();
reader.seek(virtual_position).await?;
sourcepub fn query<I>(
&mut self,
reference_sequences: &ReferenceSequences,
index: &I,
region: &Region
) -> Result<impl Stream<Item = Result<Record>> + '_> where
I: BinningIndex,
pub fn query<I>(
&mut self,
reference_sequences: &ReferenceSequences,
index: &I,
region: &Region
) -> Result<impl Stream<Item = Result<Record>> + '_> where
I: BinningIndex,
Returns a stream over records that intersect the given region.
Examples
use futures::TryStreamExt;
use noodles_bam::{self as bam, bai};
use noodles_core::Region;
use noodles_sam as sam;
use tokio::fs::File;
let mut reader = File::open("sample.bam").await.map(bam::AsyncReader::new)?;
let header: sam::Header = reader.read_header().await?.parse()?;
let reference_sequences = header.reference_sequences();
let index = bai::r#async::read("sample.bam.bai").await?;
let region = "sq0:8-13".parse()?;
let mut query = reader.query(reference_sequences, &index, ®ion)?;
while let Some(record) = query.try_next().await? {
// ...
}
Trait Implementations
Auto Trait Implementations
impl<R> RefUnwindSafe for Reader<R> where
R: RefUnwindSafe,
impl<R> Send for Reader<R> where
R: Send,
impl<R> Sync for Reader<R> where
R: Sync,
impl<R> Unpin for Reader<R> where
R: Unpin,
impl<R> UnwindSafe for Reader<R> where
R: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more