Struct noodles_fasta::reader::Reader

source ·
pub struct Reader<R> { /* private fields */ }
Expand description

A FASTA reader.

Implementations§

source§

impl<R> Reader<R>
where R: BufRead,

source

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[..]);
source

pub fn get_ref(&self) -> &R

Returns a reference to the underlying reader.

§Examples
use noodles_fasta as fasta;
let reader = fasta::Reader::new(&[][..]);
assert!(reader.get_ref().is_empty());
source

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::Reader::new(&[][..]);
assert!(reader.get_mut().is_empty());
source

pub fn into_inner(self) -> R

Returns the underlying reader.

§Examples
use noodles_fasta as fasta;
let reader = fasta::Reader::new(&[][..]);
assert!(reader.into_inner().is_empty());
source

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");
source

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::Reader::new(&data[..]);
reader.read_definition(&mut String::new())?;

let mut buf = Vec::new();
reader.read_sequence(&mut buf)?;

assert_eq!(buf, b"ACGT");
source

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::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");
source

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::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>
where R: BufRead + Seek,

source

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::Reader::new(Cursor::new(data));

let region = Region::new("sq1", ..);
let record = reader.query(&index, &region)?;
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, &region)?;
assert_eq!(record, fasta::Record::new(
    Definition::new("sq1:2-3", None),
    Sequence::from(b"CG".to_vec()),
));

Auto Trait Implementations§

§

impl<R> Freeze for Reader<R>
where R: Freeze,

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.