Struct noodles_fasta::io::reader::Reader
source · pub struct Reader<R> { /* private fields */ }
Expand description
A FASTA reader.
Implementations§
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 FASTA reader.
§Examples
use noodles_fasta as fasta;
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let mut reader = fasta::io::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_fasta as fasta;
let reader = fasta::io::Reader::new(&[][..]);
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_fasta as fasta;
let mut reader = fasta::io::Reader::new(&[][..]);
assert!(reader.get_mut().is_empty());
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Returns the underlying reader.
§Examples
use noodles_fasta as fasta;
let reader = fasta::io::Reader::new(&[][..]);
assert!(reader.into_inner().is_empty());
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::io::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 bases read from the stream. If the number of bases 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::io::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 sequence_reader(&mut self) -> Reader<'_, R> ⓘ
pub fn sequence_reader(&mut self) -> Reader<'_, R> ⓘ
Returns a sequence reader.
A sequence::Reader
can be used for lower-level reading of the raw sequence.
The position of the stream is expected to be at the start of a sequence to read a full sequence or within a sequence to read a partial one.
§Examples
use noodles_fasta as fasta;
let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
let mut reader = fasta::io::Reader::new(&data[..]);
reader.read_definition(&mut String::new())?;
let mut sequence_reader = reader.sequence_reader();
let mut buf = vec![0; 2];
sequence_reader.read_exact(&mut buf)?;
assert_eq!(buf, b"AC");
sourcepub fn records(&mut self) -> Records<'_, R> ⓘ
pub fn records(&mut self) -> Records<'_, R> ⓘ
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::io::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());
source§impl<R> Reader<R>
impl<R> Reader<R>
sourcepub fn query(&mut self, index: &Index, region: &Region) -> Result<Record>
pub fn query(&mut self, index: &Index, 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("sq0", 4, 5, 4, 5),
fai::Record::new("sq1", 4, 15, 4, 5),
fai::Record::new("sq2", 4, 25, 4, 5),
];
let mut reader = fasta::io::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()),
));