Struct noodles_fasta::reader::Reader[][src]

pub struct Reader<R> { /* fields omitted */ }
Expand description

A FASTA reader.

Implementations

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

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

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

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());

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

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

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

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

Rewind to the beginning of a stream. Read more

🔬 This is a nightly-only experimental API. (seek_stream_len)

Returns the length of this stream (in bytes). Read more

Returns the current seek position from the start of the stream. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.