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::io::reader::Builder::default().build_from_path("sample.vcf")?;
let header = reader.read_header()?;
for result in reader.records() {
let record = result?;
// ...
}
Implementations§
Source§impl<R> Reader<R>
impl<R> Reader<R>
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::io::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::io::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::io::Reader::new(&data[..]);
assert!(reader.into_inner().is_empty());
Source§impl<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::io::Reader::new(&data[..]);
Sourcepub fn read_header(&mut self) -> Result<Header>
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::io::Reader::new(&data[..]);
let header = reader.read_header()?;
Sourcepub fn read_record_buf(
&mut self,
header: &Header,
record: &mut RecordBuf,
) -> Result<usize>
pub fn read_record_buf( &mut self, header: &Header, record: &mut RecordBuf, ) -> 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::io::Reader::new(&data[..]);
let header = reader.read_header()?;
let mut record = vcf::variant::RecordBuf::default();
reader.read_record_buf(&header, &mut record)?;
Sourcepub fn record_bufs<'r, 'h: 'r>(
&'r mut self,
header: &'h Header,
) -> RecordBufs<'r, 'h, R> ⓘ
pub fn record_bufs<'r, 'h: 'r>( &'r mut self, header: &'h Header, ) -> RecordBufs<'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::io::Reader::new(&data[..]);
let header = reader.read_header()?;
let mut records = reader.record_bufs(&header);
assert!(records.next().is_some());
assert!(records.next().is_none());
Sourcepub fn read_record(&mut self, record: &mut Record) -> Result<usize>
pub fn read_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::io::Reader::new(&data[..]);
reader.read_header()?;
let mut record = vcf::Record::default();
reader.read_record(&mut record)?;
Sourcepub fn records(&mut self) -> impl Iterator<Item = Result<Record>> + '_
pub fn records(&mut self) -> impl Iterator<Item = Result<Record>> + '_
Returns an iterator over records.
The stream is expected to be directly after the header or at the start of another record.
§Examples
use noodles_vcf as vcf;
const DATA: &[u8] = b"##fileformat=VCFv4.3
#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
sq0\t1\t.\tA\t.\t.\tPASS\t.
";
let mut reader = vcf::io::Reader::new(DATA);
reader.read_header()?;
for result in reader.records() {
let record = result?;
// ...
}
Source§impl<R> Reader<R>
impl<R> Reader<R>
Sourcepub fn query<'r, 'h, I>(
&'r mut self,
header: &'h Header,
index: &I,
region: &Region,
) -> Result<Query<'r, 'h, R>>where
I: BinningIndex,
pub fn query<'r, 'h, I>(
&'r mut self,
header: &'h Header,
index: &I,
region: &Region,
) -> Result<Query<'r, 'h, R>>where
I: BinningIndex,
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::io::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, ®ion)?;
for result in query {
let record = result?;
println!("{:?}", record);
}
Ok::<_, Box<dyn std::error::Error>>(())