pub struct Reader<R> { /* private fields */ }
Expand description
A CRAM reader.
The CRAM format is comprised of four main parts: 1) a file definition, 2) a file header, 3) a list of data containers, and 4) an end-of-file (EOF) container.
§Examples
use noodles_cram as cram;
use noodles_fasta as fasta;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
let header = reader.read_header()?;
for result in reader.records(&header) {
let record = result?;
println!("{:?}", record);
}
Implementations§
Source§impl<R> Reader<R>where
R: Read,
impl<R> Reader<R>where
R: Read,
Sourcepub fn new(inner: R) -> Self
pub fn new(inner: R) -> Self
Creates a CRAM reader.
§Examples
use noodles_cram as cram;
let mut reader = File::open("sample.bam").map(cram::io::Reader::new)?;
Sourcepub fn get_ref(&self) -> &R
pub fn get_ref(&self) -> &R
Returns a reference to the underlying reader.
§Examples
use noodles_cram as cram;
let data = [];
let reader = cram::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_cram as cram;
let data = [];
let mut reader = cram::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 reader.
§Examples
use noodles_cram as cram;
let data = [];
let reader = cram::io::Reader::new(&data[..]);
assert!(reader.into_inner().is_empty());
Sourcepub fn read_file_definition(&mut self) -> Result<FileDefinition>
pub fn read_file_definition(&mut self) -> Result<FileDefinition>
Reads the CRAM file definition.
The CRAM magic number is also checked.
The position of the stream is expected to be at the start.
§Examples
use noodles_cram as cram;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
let file_definition = reader.read_file_definition()?;
Sourcepub fn read_file_header(&mut self) -> Result<Header>
pub fn read_file_header(&mut self) -> Result<Header>
Reads the SAM header.
The position of the stream is expected to be at the CRAM header container, i.e., directly after the file definition.
§Examples
use noodles_cram as cram;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
reader.read_file_definition()?;
let header = reader.read_file_header()?;
Sourcepub fn read_header(&mut self) -> Result<Header>
pub fn read_header(&mut self) -> Result<Header>
Reads the SAM header.
This verifies the CRAM magic number, discards the file definition, and reads and parses the file header as a SAM header.
The position of the stream is expected to be at the start.
§Examples
use noodles_cram as cram;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
let header = reader.read_header()?;
Sourcepub fn read_data_container(&mut self) -> Result<Option<DataContainer>>
pub fn read_data_container(&mut self) -> Result<Option<DataContainer>>
Reads a data container.
This returns None
if the container header is the EOF container header, which signals the
end of the stream.
§Examples
use noodles_cram as cram;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
reader.read_header()?;
while let Some(container) = reader.read_data_container()? {
// ...
}
Sourcepub fn records<'r>(&'r mut self, header: &'r Header) -> Records<'r, R> ⓘ
pub fn records<'r>(&'r mut self, header: &'r Header) -> Records<'r, R> ⓘ
Returns a iterator over records starting from the current stream position.
The stream is expected to be at the start of a data container.
§Examples
use noodles_cram as cram;
use noodles_fasta as fasta;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
let header = reader.read_header()?;
for result in reader.records(&header) {
let record = result?;
// ...
}
Source§impl<R> Reader<R>
impl<R> Reader<R>
Sourcepub fn seek(&mut self, pos: SeekFrom) -> Result<u64>
pub fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seeks the underlying reader to the given position.
Positions typically come from the associated CRAM index file.
§Examples
use std::io::SeekFrom;
use noodles_cram as cram;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
reader.seek(SeekFrom::Start(17711))?;
Sourcepub fn position(&mut self) -> Result<u64>
pub fn position(&mut self) -> Result<u64>
Returns the current position of the underlying reader.
§Examples
use noodles_cram as cram;
let data = Cursor::new(Vec::new());
let mut reader = cram::io::Reader::new(data);
let position = reader.position()?;
assert_eq!(position, 0);
Sourcepub fn query<'a>(
&'a mut self,
header: &'a Header,
index: &'a Index,
region: &Region,
) -> Result<Query<'_, R>>
pub fn query<'a>( &'a mut self, header: &'a Header, index: &'a Index, region: &Region, ) -> Result<Query<'_, R>>
Returns an iterator over records that intersects the given region.
§Examples
use noodles_cram::{self as cram, crai};
use noodles_fasta as fasta;
let mut reader = File::open("sample.cram").map(cram::io::Reader::new)?;
let header = reader.read_header()?;
let index = crai::read("sample.cram.crai")?;
let region = "sq0:8-13".parse()?;
let query = reader.query(&header, &index, ®ion)?;
for result in query {
let record = result?;
// ...
}