Skip to main content

FastqReader

Struct FastqReader 

Source
pub struct FastqReader<R> { /* private fields */ }
Expand description

Slab-based FASTQ reader over any Read input.

The reader frames four-line FASTQ records from a reusable byte slab. Records crossing slab boundaries are carried into the next read. Returned batches borrow from the reader and must be consumed before calling next_batch again.

Implementations§

Source§

impl<R: Read> FastqReader<R>

Source

pub fn new(reader: R) -> Self

Create a reader with FastqConfig::default.

Examples found in repository?
examples/fastq_chunk_sink.rs (line 47)
45fn main() -> Result<()> {
46    let stdin = io::stdin();
47    let mut reader = FastqReader::new(stdin.lock());
48    let config = FastqChunkConfig::new(10_000_000).min_records(40_000);
49    let mut sink = ReadBuffer::default();
50
51    sink.reserve_records(config.estimated_records(150));
52    while let Some(stats) = reader.next_chunk_with_sink(config, &mut sink)? {
53        let checksum: usize = sink
54            .reads
55            .iter()
56            .map(|read| read.name.len() ^ read.seq.len() ^ read.qual.len())
57            .sum();
58        eprintln!(
59            "chunk first_record={} records={} bases={} checksum={}",
60            stats.first_record_index(),
61            stats.records(),
62            stats.bases(),
63            checksum
64        );
65
66        // Downstream tools typically hand `sink.reads` to alignment or another
67        // processing stage here, then reuse the allocation for the next chunk.
68        sink.clear();
69        sink.reserve_records(config.estimated_records(150));
70    }
71
72    Ok(())
73}
Source

pub fn with_config(reader: R, config: FastqConfig) -> Self

Create a reader with explicit configuration.

Source

pub fn into_inner(self) -> R

Return the wrapped reader.

Source

pub fn next_batch(&mut self) -> Result<Option<FastqBatch<'_>>>

Read the next batch of FASTQ records.

Returns Ok(None) at EOF. Format errors include byte offset, record index, and line index when the failing location is known.

Source

pub fn visit_records<F>(&mut self, visit: F) -> Result<()>
where F: FnMut(FastqVisitRecord<'_>) -> Result<()>,

Visit every record in the stream without building a batch side table.

This is the fastest public parse-only path for single-pass consumers. It still honors FastqConfig::validate for record structure, but it yields individual records and does not perform paired-end identifier validation. Use next_batch, interleaved_pairs, or PairedFastqReader when pair validation is required.

Source

pub fn count_records(&mut self) -> Result<FastqStats>

Count remaining records and bases without building record views.

This consumes the reader to EOF. It validates the same four-line FASTQ structure as visit_records when FastqConfig::validate is enabled, but it avoids the per-record visitor callback and borrowed-record construction.

Source

pub fn next_chunk_with_sink<S>( &mut self, config: FastqChunkConfig, sink: &mut S, ) -> Result<Option<FastqChunkStats>>
where S: FastqRecordSink,

Emit one resumable chunk of FASTQ records into a caller-owned sink.

This is the low-overhead path for downstream tools that already have a target output representation. It does not build the RecordRef side table used by next_batch, and it returns after the configured chunk limit so callers can interleave parsing with their own pipeline stages.

Returns Ok(None) only when EOF is reached before emitting another record. A subsequent call resumes at the first unconsumed record.

Examples found in repository?
examples/fastq_chunk_sink.rs (line 52)
45fn main() -> Result<()> {
46    let stdin = io::stdin();
47    let mut reader = FastqReader::new(stdin.lock());
48    let config = FastqChunkConfig::new(10_000_000).min_records(40_000);
49    let mut sink = ReadBuffer::default();
50
51    sink.reserve_records(config.estimated_records(150));
52    while let Some(stats) = reader.next_chunk_with_sink(config, &mut sink)? {
53        let checksum: usize = sink
54            .reads
55            .iter()
56            .map(|read| read.name.len() ^ read.seq.len() ^ read.qual.len())
57            .sum();
58        eprintln!(
59            "chunk first_record={} records={} bases={} checksum={}",
60            stats.first_record_index(),
61            stats.records(),
62            stats.bases(),
63            checksum
64        );
65
66        // Downstream tools typically hand `sink.reads` to alignment or another
67        // processing stage here, then reuse the allocation for the next chunk.
68        sink.clear();
69        sink.reserve_records(config.estimated_records(150));
70    }
71
72    Ok(())
73}

Trait Implementations§

Source§

impl<R: Debug> Debug for FastqReader<R>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

impl<R> RefUnwindSafe for FastqReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for FastqReader<R>
where R: Send,

§

impl<R> Sync for FastqReader<R>
where R: Sync,

§

impl<R> Unpin for FastqReader<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for FastqReader<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for FastqReader<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>,

Source§

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>,

Source§

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.