Struct noodles_vcf::AsyncReader
source · [−]pub struct AsyncReader<R> { /* private fields */ }
Expand description
An async VCF reader.
The VCF format has two main parts: 1) a header and 2) a list of VCF records.
Each header line is prefixed with a #
(number sign) and is terminated by the header header
(#CHROM
…; inclusive).
VCF records are line-based and follow directly after the header until EOF.
Examples
use futures::TryStreamExt;
use noodles_vcf as vcf;
use tokio::{fs::File, io::BufReader};
let mut reader = File::open("sample.vcf")
.await
.map(BufReader::new)
.map(vcf::AsyncReader::new)?;
let header = reader.read_header().await?.parse()?;
let mut records = reader.records(&header);
while let Some(record) = records.try_next().await? {
// ...
}
Implementations
sourceimpl<R> Reader<R>where
R: AsyncBufRead + Unpin,
impl<R> Reader<R>where
R: AsyncBufRead + Unpin,
sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Creates an async VCF reader.
Examples
use noodles_vcf as vcf;
let data = [];
let reader = vcf::AsyncReader::new(&data[..]);
sourcepub async fn read_header(&mut self) -> Result<String>
pub async fn read_header(&mut self) -> Result<String>
Reads the raw VCF header.
This reads all header lines prefixed with a #
(number sign), which includes the header
header (#CHROM
…).
The position of the stream is expected to be at the start.
This returns the raw VCF header as a String
, and as such, it is not necessarily valid.
The raw header can subsequently be parsed as a crate::Header
.
Examples
use noodles_vcf as vcf;
let data = b"##fileformat=VCFv4.3
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
sq0\t1\t.\tA\t.\t.\tPASS\t.
";
let mut reader = vcf::AsyncReader::new(&data[..]);
let header = reader.read_header().await?;
assert_eq!(header, "##fileformat=VCFv4.3\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n");
sourcepub async fn read_record(&mut self, buf: &mut String) -> Result<usize>
pub async fn read_record(&mut self, buf: &mut String) -> Result<usize>
Reads a single raw VCF record.
This reads from the underlying stream until a newline is reached and appends it to the
given buffer, sans the final newline. The buffer does not necessarily represent a valid VCF
record but can subsequently be parsed as a crate::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 a stream (see Self::records
and
Self::query
), but using this method allows control of the line buffer and whether the
raw record should be parsed.
If successful, the number of bytes read is returned. If the number of bytes read is 0, the stream reached EOF.
Examples
use noodles_vcf as vcf;
let data = b"##fileformat=VCFv4.3
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
sq0\t1\t.\tA\t.\t.\tPASS\t.
";
let mut reader = vcf::AsyncReader::new(&data[..]);
reader.read_header().await?;
let mut buf = String::new();
reader.read_record(&mut buf).await?;
assert_eq!(buf, "sq0\t1\t.\tA\t.\t.\tPASS\t.");
sourcepub fn records<'r, 'h: 'r>(
&'r mut self,
header: &'h Header
) -> impl Stream<Item = Result<Record>> + 'r
pub fn records<'r, 'h: 'r>(
&'r mut self,
header: &'h Header
) -> impl Stream<Item = Result<Record>> + 'r
Returns an (async) stream over records 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.
Unlike Self::read_record
, each record is parsed as a Record
.
Examples
use futures::TryStreamExt;
use noodles_vcf as vcf;
let data = b"##fileformat=VCFv4.3
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
sq0\t1\t.\tA\t.\t.\tPASS\t.
";
let mut reader = vcf::AsyncReader::new(&data[..]);
let header = reader.read_header().await?.parse()?;
let mut records = reader.records(&header);
while let Some(record) = records.try_next().await? {
// ...
}
sourceimpl<R> Reader<AsyncReader<R>>where
R: AsyncRead,
impl<R> Reader<AsyncReader<R>>where
R: AsyncRead,
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_bgzf as bgzf;
use noodles_vcf as vcf;
let data = [];
let reader = vcf::AsyncReader::new(bgzf::AsyncReader::new(&data[..]));
assert_eq!(reader.virtual_position(), bgzf::VirtualPosition::default());
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 stream to the given virtual position.
Virtual positions typically come from an associated index.
Examples
use noodles_bgzf as bgzf;
use noodles_vcf as vcf;
let data = Cursor::new([]);
let mut reader = vcf::AsyncReader::new(bgzf::AsyncReader::new(data));
let virtual_position = bgzf::VirtualPosition::default();
reader.seek(virtual_position).await?;
sourcepub fn query<'r>(
&'r mut self,
header: &'r Header,
index: &Index,
region: &Region
) -> Result<impl Stream<Item = Result<Record>> + 'r>
pub fn query<'r>(
&'r mut self,
header: &'r Header,
index: &Index,
region: &Region
) -> Result<impl Stream<Item = Result<Record>> + 'r>
Returns a stream over records that intersects the given region.
The position of the (input) stream is expected to after the header or at the start of another record.
Examples
use futures::TryStreamExt;
use noodles_bgzf as bgzf;
use noodles_core::Region;
use noodles_tabix as tabix;
use noodles_vcf as vcf;
use tokio::fs::File;
let mut reader = File::open("sample.vcf.gz")
.await
.map(bgzf::AsyncReader::new)
.map(vcf::AsyncReader::new)?;
let header = reader.read_header().await?.parse()?;
let index = tabix::read("sample.vcf.gz.tbi")?;
let region = "sq0:8-13".parse()?;
let mut query = reader.query(&header, &index, ®ion)?;
while let Some(record) = query.try_next().await? {
// ...
}