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 = File::open("sample.vcf")
.map(BufReader::new)
.map(vcf::Reader::new)?;
let header = reader.read_header()?.parse()?;
for result in reader.records(&header) {
let record = result?;
println!("{:?}", record);
}
Implementations
sourceimpl<R> Reader<R> where
R: BufRead,
impl<R> Reader<R> where
R: BufRead,
sourcepub fn new(inner: R) -> Self
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[..]);
sourcepub fn get_ref(&self) -> &R
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());
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_vcf as vcf;
let data = [];
let mut reader = vcf::Reader::new(&data[..]);
assert!(reader.get_mut().is_empty());
sourcepub fn into_inner(self) -> R
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());
sourcepub fn read_header(&mut self) -> Result<String>
pub 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::Reader::new(&data[..]);
let header = reader.read_header()?;
assert_eq!(header, "##fileformat=VCFv4.3\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n");
sourcepub fn read_record(&mut self, buf: &mut String) -> Result<usize>
pub 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 character. The buffer 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 an iterator (see Self::records
), 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::Reader::new(&data[..]);
reader.read_header()?;
let mut buf = String::new();
reader.read_record(&mut buf)?;
assert_eq!(buf, "sq0\t1\t.\tA\t.\t.\tPASS\t.");
sourcepub fn records<'r, 'h>(&'r mut self, header: &'h Header) -> Records<'r, 'h, R>ⓘNotable traits for Records<'r, 'h, R>impl<'r, 'h, R> Iterator for Records<'r, 'h, R> where
R: BufRead, type Item = Result<Record>;
pub fn records<'r, 'h>(&'r mut self, header: &'h Header) -> Records<'r, 'h, R>ⓘNotable traits for Records<'r, 'h, R>impl<'r, 'h, R> Iterator for Records<'r, 'h, R> where
R: BufRead, type Item = Result<Record>;
R: BufRead, type Item = Result<Record>;
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.
Unlike Self::read_record
, each record is parsed as a crate::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()?.parse()?;
let mut records = reader.records(&header);
assert!(records.next().is_some());
assert!(records.next().is_none());
sourceimpl<R> Reader<Reader<R>> where
R: Read,
impl<R> Reader<Reader<R>> where
R: Read,
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 = 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);
sourceimpl<R> Reader<Reader<R>> where
R: Read + Seek,
impl<R> Reader<Reader<R>> where
R: Read + Seek,
sourcepub fn seek(&mut self, pos: VirtualPosition) -> Result<VirtualPosition>
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)?;
sourcepub fn query<'r, 'h>(
&'r mut self,
header: &'h Header,
index: &Index,
region: &Region
) -> Result<Query<'r, 'h, R>>
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()?.parse()?;
let index = tabix::read("sample.vcf.gz.tbi")?;
let region = Region::mapped("sq0", 8..=13);
let query = reader.query(&header, &index, ®ion)?;
for result in query {
let record = result?;
println!("{:?}", record);
}
Ok::<_, Box<dyn std::error::Error>>(())
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