[][src]Module seq_io::fastx::dynamic

This module provides types and functions for FASTX reading via dynamic dispatch.

In contrast to fastx::Reader, which has an API equivalent to fasta::Reader and fastq::Reader, this module provides the traits FastxReader and FastxSeekReader. These are implemented for all readers in the fasta and fastq modules.

Example

This example uses the reader function to obtain a Box<dyn FastxReader<...>. The format is recognized based on the first byte of the first non-empty line, and an appropriate reader for the format is chosen and returned:

use seq_io::prelude::*;
use seq_io::fastx::dynamic::reader;
use seq_io::fastx::SeqFormat;

let fasta = b">id
SEQUENCE
";

let mut fasta_rdr = reader(&fasta[..], false).unwrap().expect("input empty");
// The format should be correctly recognized
assert_eq!(fasta_rdr.format(), Some(SeqFormat::FASTA));

// The 'next' method is called 'next_fastx' here to prevent name clashes:
let record = fasta_rdr.next_fastx().unwrap().unwrap();
assert_eq!(record.id(), Ok("id"));

An example with FASTQ:

let fastq = b"
@id
SEQUENCE
+
QUALITYY
";

let mut fastq_rdr = reader(&fastq[..], false).unwrap().expect("input empty");
// The format should be correctly recognized
assert_eq!(fastq_rdr.format(), Some(SeqFormat::FASTQ));

// The 'next' method is called 'next_fastx' here to prevent name clashes:
let record = fastq_rdr.next_fastx().unwrap().unwrap();
assert_eq!(record.id(), Ok("id"));

Empty input will lead to None:

let empty_lines = b"


";

assert!(reader(&empty_lines[..], false).unwrap().is_none());

Using ReaderBuilder

ReaderBuilder allows for applying different configuration options before the actual reader is created. This is different from the usual API in this crate, where reader options can be changed after creation.

This example applies several non-default settings at once:

use seq_io::fastx::dynamic::ReaderBuilder;
use seq_io::fastx::SeqFormat;
use seq_io::policy::DoubleUntil;

let fastq = b"
@id
SEQUENCE
+
QUALITYY
";

let policy = DoubleUntil(16 * 1024 * 1024);
let capacity = 32 * 1024 * 1024;

let mut reader = ReaderBuilder::new()
    .set_policy(policy)
    .set_capacity(capacity)
    .set_multiline_fastq(true)
    .from_reader(&fastq[..])
    .unwrap()
    .expect("empty input");

assert_eq!(reader.format(), Some(SeqFormat::FASTQ));

Another use of ReaderBuilder is to construct a FastxSeekReader from a source other than File (in which case you could use from_path):

use seq_io::prelude::*;
use seq_io::fastx::dynamic::ReaderBuilder;
use std::io::Cursor;

let fastq = b"
>id1
SEQUENCE1
>id2
SEQUENCE2
";
let input = Cursor::new(&fastq[..]);

let mut reader = ReaderBuilder::new()
    .from_seekable(input)
    .unwrap()
    .expect("empty input");

// first record
let rec1 = reader.next_fastx().unwrap().expect("no record");
let id1 = rec1.id().unwrap().to_owned();
let pos1 = reader.position_fastx();

// proceed to second record
let _ = reader.next_fastx().unwrap().expect("no record");
assert!(reader.next_fastx().is_none());

// now seek back
reader.seek_fastx(&pos1);
let rec1_again = reader.next_fastx().unwrap().expect("no record");
assert_eq!(rec1_again.id().unwrap(), &id1);

Structs

Error

Parsing error

OwnedRecord

A FASTX record that ownes its data (requires allocations)

ReaderBuilder

Allows building a FastxReader or FastxSeekReader with various configuration options.

RecordSet

Set of sequence records that owns it's buffer and knows the positions of each record.

RecordsIntoIter

Iterator of OwnedRecord that owns the underlying reader

RecordsIter

Borrowed iterator of OwnedRecord

RefRecord

A FASTX record that borrows data from a buffer It implements the traits BaseRecord and Record.

Traits

FastxReader

Trait for FASTX reading.

FastxSeekReader
Record

FASTX record trait implemented by both RefRecord and OwnedRecord which adds more methods to BaseRecord.

Functions

from_path

Recognizes the sequence format of the file and returns an appropriate FASTA or FASTQ reader as Box<dyn FastxSeekReader<...> or None if the input has only empty lines. See reader for more information about the behaviour.

reader

Recognizes the sequence format of the input and returns an appropriate FASTA or FASTQ reader as Box<dyn FastxReader<...> or None if the input has only empty lines.

Type Definitions

Result