Struct noodles_fasta::reader::Reader
source · [−]pub struct Reader<R> { /* private fields */ }
Expand description
A FASTA reader.
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 FASTA reader.
Examples
use noodles_fasta as fasta;
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let mut reader = fasta::Reader::new(&data[..]);
sourcepub fn read_definition(&mut self, buf: &mut String) -> Result<usize>
pub fn read_definition(&mut self, buf: &mut String) -> Result<usize>
Reads a raw definition line.
The given buffer will not include the trailing newline. It can subsequently be parsed as a
crate::record::Definition
.
The position of the stream is expected to be at the start or at the start of another definition.
If successful, this returns the number of bytes read from the stream. If the number of bytes read is 0, the stream reached EOF.
Examples
use noodles_fasta as fasta;
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let mut reader = fasta::Reader::new(&data[..]);
let mut buf = String::new();
reader.read_definition(&mut buf)?;
assert_eq!(buf, ">sq0");
sourcepub fn read_sequence(&mut self, buf: &mut Vec<u8>) -> Result<usize>
pub fn read_sequence(&mut self, buf: &mut Vec<u8>) -> Result<usize>
Reads a sequence.
The given buffer consumes a sequence without newlines until another definition or EOF is reached.
The position of the stream is expected to be at the start of a sequence, which is directly after a definition.
If successful, this returns the number of bytes read from the stream. If the number of bytes read is 0, the stream reached EOF (though this case is likely an error).
Examples
use noodles_fasta as fasta;
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let mut reader = fasta::Reader::new(&data[..]);
reader.read_definition(&mut String::new())?;
let mut buf = Vec::new();
reader.read_sequence(&mut buf)?;
assert_eq!(buf, b"ACGT");
sourcepub fn records(&mut self) -> Records<'_, R>ⓘNotable traits for Records<'a, R>impl<'a, R> Iterator for Records<'a, R> where
R: BufRead, type Item = Result<Record>;
pub fn records(&mut self) -> Records<'_, R>ⓘNotable traits for Records<'a, R>impl<'a, R> Iterator for Records<'a, 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 position of the stream is expected to be at the start or at the start of another definition.
use noodles_fasta::{self as fasta, record::{Definition, Sequence}};
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let mut reader = fasta::Reader::new(&data[..]);
let mut records = reader.records();
assert_eq!(records.next().transpose()?, Some(fasta::Record::new(
Definition::new("sq0", None),
Sequence::from(b"ACGT".to_vec()),
)));
assert_eq!(records.next().transpose()?, Some(fasta::Record::new(
Definition::new("sq1", None),
Sequence::from(b"NNNNNNNNNN".to_vec()),
)));
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_fasta as fasta;
let data = Vec::new();
let reader = fasta::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<R> where
R: BufRead + Seek,
impl<R> Reader<R> where
R: BufRead + Seek,
sourcepub fn query(&mut self, index: &[Record], region: &Region) -> Result<Record>
pub fn query(&mut self, index: &[Record], region: &Region) -> Result<Record>
Returns a record of the given region.
Examples
use noodles_core::Region;
use noodles_fasta::{self as fasta, fai, record::{Definition, Sequence}};
let data = b">sq0\nNNNN\n>sq1\nACGT\n>sq2\nNNNN\n";
let index = vec![
fai::Record::new(String::from("sq0"), 4, 5, 4, 5),
fai::Record::new(String::from("sq1"), 4, 15, 4, 5),
fai::Record::new(String::from("sq2"), 4, 25, 4, 5),
];
let mut reader = fasta::Reader::new(Cursor::new(data));
let region = Region::new("sq1", ..);
let record = reader.query(&index, ®ion)?;
assert_eq!(record, fasta::Record::new(
Definition::new("sq1", None),
Sequence::from(b"ACGT".to_vec()),
));
let region = "sq1:2-3".parse()?;
let record = reader.query(&index, ®ion)?;
assert_eq!(record, fasta::Record::new(
Definition::new("sq1:2-3", None),
Sequence::from(b"CG".to_vec()),
));
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_fasta as fasta;
let mut reader = File::open("sample.fa.gz")
.map(bgzf::Reader::new)
.map(fasta::Reader::new)?;
let virtual_position = bgzf::VirtualPosition::from(102334155);
reader.seek(virtual_position)?;
Trait Implementations
sourceimpl<R> Seek for Reader<R> where
R: Read + Seek,
impl<R> Seek for Reader<R> where
R: Read + Seek,
sourcefn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seeks the underlying stream to the given position.
These positions typically come from an associated index, which start at the sequence and not the definition.
Examples
use noodles_fasta as fasta;
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let cursor = Cursor::new(&data[..]);
let mut reader = fasta::Reader::new(cursor);
reader.seek(SeekFrom::Start(14));
let mut buf = Vec::new();
reader.read_sequence(&mut buf)?;
assert_eq!(buf, b"NNNNNNNNNN");
1.55.0 · sourcefn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Rewind to the beginning of a stream. Read more
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