Struct noodles_sam::reader::Reader[][src]

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

A SAM reader.

The SAM format is comprised of two parts: 1) a header and 2) a list of records.

Each header line is prefixed with an @ (at sign). The header is optional and may be empty.

SAM records are line-based and follow directly after the header or the start of the file until EOF.

Examples

use noodles_sam as sam;

let mut reader = File::open("sample.sam")
    .map(BufReader::new)
    .map(sam::Reader::new)?;

reader.read_header()?;

for result in reader.records() {
    let record = result?;
    println!("{:?}", record);
}

Implementations

Creates a SAM reader.

Examples

use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let reader = sam::Reader::new(&data[..]);

Reads the raw SAM header.

The position of the stream is expected to be at the start.

This returns the raw SAM header as a String. It can subsequently be parsed as a crate::Header.

The SAM header is optional, and if it is missing, an empty string is returned.

Examples

use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::Reader::new(&data[..]);
let header = reader.read_header()?;

assert_eq!(header, "@HD\tVN:1.6\n");

Reads a single raw SAM record.

This reads from the underlying stream until a newline is reached and appends it to the given buffer, sans the final newline character.

The stream is expected to be directly after the header or at the start of another record.

It is more ergonomic to read records using an iterator (see Self::records), but using this method allows control of the line buffer and whether the raw record should be parsed.

If successful, the number of bytes read is returned. If the number of bytes read is 0, the stream reached EOF.

Examples

use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::Reader::new(&data[..]);
reader.read_header()?;

let mut buf = String::new();
reader.read_record(&mut buf)?;
assert_eq!(buf, "*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*");

assert_eq!(reader.read_record(&mut buf)?, 0);

Returns an iterator over records starting from the current stream position.

The stream is expected to be directly after the header or at the start of another record.

Unlike Self::read_record, each record is parsed as a crate::Record.

Examples

use noodles_sam as sam;

let data = b"@HD\tVN:1.6
*\t4\t*\t0\t255\t*\t*\t0\t0\t*\t*
";

let mut reader = sam::Reader::new(&data[..]);
reader.read_header()?;

let mut records = reader.records();
assert!(records.next().is_some());
assert!(records.next().is_none());

Seeks the underlying BGZF stream to the given virtual position.

Virtual positions typically come from an associated index.

Examples

use std::io::BufReader;

use noodles_bgzf as bgzf;
use noodles_sam as sam;

let mut reader = File::open("sample.sam.gz")
    .map(bgzf::Reader::new)
    .map(sam::Reader::new)?;

let virtual_position = bgzf::VirtualPosition::from(102334155);
reader.seek(virtual_position)?;

Trait Implementations

Formats the value using the given formatter. 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.