Struct noodles_vcf::reader::Reader

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

A 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 noodles_vcf as vcf;

let mut reader = vcf::reader::Builder::default().build_from_path("sample.vcf")?;
let header = reader.read_header()?;

for result in reader.records(&header) {
    let record = result?;
    println!("{:?}", record);
}

Implementations§

source§

impl<R> Reader<R>where R: BufRead,

source

pub fn new(inner: R) -> Self

Creates a VCF reader.

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 reader = vcf::Reader::new(&data[..]);
source

pub fn get_ref(&self) -> &R

Returns a reference to the underlying reader.

Examples
use noodles_vcf as vcf;
let data = [];
let reader = vcf::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_vcf as vcf;
let data = [];
let mut reader = vcf::Reader::new(&data[..]);
assert!(reader.get_mut().is_empty());
source

pub fn into_inner(self) -> R

Unwraps and returns the underlying writer.

Examples
use noodles_vcf as vcf;
let data = [];
let reader = vcf::Reader::new(&data[..]);
assert!(reader.into_inner().is_empty());
source

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

Reads the VCF header.

This reads all header lines prefixed with a # (number sign), which includes the header header (#CHROM…), and parses it as a crate::Header.

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

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::Reader::new(&data[..]);
let header = reader.read_header()?;
source

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

Reads a single VCF record.

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 allows control of the record buffer.

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::Reader::new(&data[..]);
let header = reader.read_header()?;

let mut record = vcf::Record::default();
reader.read_record(&header, &mut record)?;
source

pub fn records<'r, 'h: 'r>( &'r mut self, header: &'h Header ) -> Records<'r, 'h, R>

Returns an iterator over records starting from the current stream position.

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

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::Reader::new(&data[..]);
let header = reader.read_header()?;

let mut records = reader.records(&header);
assert!(records.next().is_some());
assert!(records.next().is_none());
source

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

Reads a single record without eagerly parsing its fields.

The reads VCF record fields from the underlying stream into the given record’s buffer until a newline is reached. No fields are parsed, meaning the record is not necessarily valid. However, the structure of the line 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_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::Reader::new(&data[..]);
reader.read_header()?;

let mut record = vcf::lazy::Record::default();
reader.read_lazy_record(&mut record)?;
source§

impl<R> Reader<Reader<R>>where R: Read,

source

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 = Vec::new();
let reader = vcf::Reader::new(bgzf::Reader::new(&data[..]));
let virtual_position = reader.virtual_position();

assert_eq!(virtual_position.compressed(), 0);
assert_eq!(virtual_position.uncompressed(), 0);
source§

impl<R> Reader<Reader<R>>where R: Read + Seek,

source

pub 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(Vec::new());
let mut reader = vcf::Reader::new(bgzf::Reader::new(data));

let virtual_position = bgzf::VirtualPosition::default();
reader.seek(virtual_position)?;
source

pub fn query<'r, 'h>( &'r mut self, header: &'h Header, index: &Index, region: &Region ) -> Result<Query<'r, 'h, R>>

Returns an iterator over records that intersects the given region.

Examples
use noodles_bgzf as bgzf;;
use noodles_core::Region;
use noodles_tabix as tabix;
use noodles_vcf as vcf;

let mut reader = File::open("sample.vcf.gz")
    .map(bgzf::Reader::new)
    .map(vcf::Reader::new)?;

let header = reader.read_header()?;

let index = tabix::read("sample.vcf.gz.tbi")?;
let region = "sq0:8-13".parse()?;
let query = reader.query(&header, &index, &region)?;

for result in query {
    let record = result?;
    println!("{:?}", record);
}
Ok::<_, Box<dyn std::error::Error>>(())

Trait Implementations§

source§

impl<R: Debug> Debug for Reader<R>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R> VariantReader<R> for Reader<R>where R: BufRead,

source§

fn read_variant_header(&mut self) -> Result<Header>

Reads a VCF header.
source§

fn variant_records<'r, 'h: 'r>( &'r mut self, header: &'h Header ) -> Box<dyn Iterator<Item = Result<Record>> + 'r>

Returns an iterator over records.

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§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more