noodles_sam::async::io

Struct Reader

Source
pub struct Reader<R> { /* private fields */ }
Expand description

An async SAM reader.

Implementations§

Source§

impl<R> Reader<R>

Source

pub fn get_ref(&self) -> &R

Returns a reference to the underlying reader.

§Examples
use noodles_sam as sam;
let data = [];
let reader = sam::r#async::io::Reader::new(&data[..]);
assert!(reader.get_ref().is_empty());
Source

pub fn get_mut(&mut self) -> &mut R

Returns a mutable reference to the underlying reader.

§Examples
use noodles_sam as sam;
let data = [];
let mut reader = sam::r#async::io::Reader::new(&data[..]);
assert!(reader.get_mut().is_empty());
Source

pub fn into_inner(self) -> R

Returns the underlying reader.

§Examples
use noodles_sam as sam;
let data = [];
let reader = sam::r#async::io::Reader::new(&data[..]);
assert!(reader.into_inner().is_empty());
Source§

impl<R> Reader<R>
where R: AsyncBufRead + Unpin,

Source

pub fn new(inner: R) -> Self

Creates an async SAM reader.

§Examples
use noodles_sam as sam;
let data = [];
let reader = sam::r#async::io::Reader::new(&data[..]);
Source

pub async fn read_header(&mut self) -> Result<Header>

Reads the SAM header.

The position of the stream is expected to be at the start.

The SAM header is optional, and if it is missing, an empty Header is returned.

§Examples
use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::r#async::io::Reader::new(&data[..]);
let header = reader.read_header().await?;
Source

pub async fn read_record_buf( &mut self, header: &Header, record: &mut RecordBuf, ) -> Result<usize>

Reads a record into an alignment record buffer.

This reads a line from the underlying stream until a newline is reached and parses that line into the given record.

The stream is expected to be directly after the header or at the start of another record.

It is more ergonomic to read records using an iterator (see Self::records), but using this method directly allows reuse of a RecordBuf.

If successful, the number of bytes read is returned. If the number of bytes read is 0, the stream reached EOF.

§Examples
use noodles_sam::{self as sam, alignment::RecordBuf};

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::r#async::io::Reader::new(&data[..]);
let header = reader.read_header().await?;

let mut record = RecordBuf::default();
reader.read_record_buf(&header, &mut record).await?;

assert_eq!(record, RecordBuf::default());
Source

pub fn record_bufs<'a>( &'a mut self, header: &'a Header, ) -> impl Stream<Item = Result<RecordBuf>> + 'a

Returns an (async) stream over alignment record buffers starting from the current (input) stream position.

The (input) stream is expected to be directly after the header or at the start of another record.

§Examples
use futures::TryStreamExt;
use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::r#async::io::Reader::new(&data[..]);
let header = reader.read_header().await?;

let mut records = reader.record_bufs(&header);

while let Some(record) = records.try_next().await? {
    // ...
}
Source

pub async fn read_record(&mut self, record: &mut Record) -> Result<usize>

Reads a record.

This reads SAM fields from the underlying stream into the given record’s buffer until a newline is reached. No fields are decoded, meaning the record is not necessarily valid. However, the structure of the buffer is guaranteed to be record-like.

The stream is expected to be directly after the header or at the start of another record.

If successful, the number of bytes read is returned. If the number of bytes read is 0, the stream reached EOF.

§Examples
use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::r#async::io::Reader::new(&data[..]);
let header = reader.read_header().await?;

let mut record = sam::Record::default();
reader.read_record(&mut record).await?;

assert_eq!(record, sam::Record::default());
Source

pub fn records(&mut self) -> impl Stream<Item = Result<Record>> + '_

Returns an (async) stream over records.

The (input) stream is expected to be directly after the header or at the start of a record.

§Examples
use futures::TryStreamExt;
use noodles_sam as sam;

let mut reader = sam::r#async::io::Reader::new(io::empty());
let header = reader.read_header().await?;

let mut records = reader.records();

while let Some(record) = records.try_next().await? {
    // ...
}
Source§

impl<R> Reader<AsyncReader<R>>
where R: AsyncRead + AsyncSeek + Unpin,

Source

pub fn query<'r, 'h: 'r, I>( &'r mut self, header: &'h Header, index: &I, region: &Region, ) -> Result<impl Stream<Item = Result<Record>> + 'r>
where I: BinningIndex,

Returns a stream over records that intersect the given region.

To query for unmapped records, use Self::query_unmapped.

§Examples
use futures::TryStreamExt;
use noodles_bgzf as bgzf;
use noodles_csi as csi;
use noodles_sam as sam;
use tokio::fs::File;

let mut reader = File::open("sample.sam")
    .await
    .map(bgzf::AsyncReader::new)
    .map(sam::r#async::io::Reader::new)?;

let header = reader.read_header().await?;

let index = csi::r#async::read("sample.sam.csi").await?;
let region = "sq0:8-13".parse()?;
let mut query = reader.query(&header, &index, &region)?;

while let Some(record) = query.try_next().await? {
    // ...
}
Source

pub async fn query_unmapped<I>( &mut self, index: &I, ) -> Result<impl Stream<Item = Result<Record>> + '_>
where I: BinningIndex,

Returns an iterator of unmapped records after querying for the unmapped region.

§Examples
use futures::TryStreamExt;
use noodles_bgzf as bgzf;
use noodles_csi as csi;
use noodles_sam as sam;
use tokio::fs::File;

let mut reader = File::open("sample.sam")
    .await
    .map(bgzf::AsyncReader::new)
    .map(sam::r#async::io::Reader::new)?;

let index = csi::r#async::read("sample.sam.csi").await?;
let mut query = reader.query_unmapped(&index).await?;

while let Some(record) = query.try_next().await? {
    // ...
}

Auto Trait Implementations§

§

impl<R> Freeze for Reader<R>
where R: Freeze,

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.