1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
//! FASTQ reader.
pub(crate) mod record;
mod records;
pub use self::records::Records;
use std::io::{self, BufRead};
use self::record::read_record;
use crate::Record;
/// A FASTQ reader.
pub struct Reader<R> {
inner: R,
}
impl<R> Reader<R>
where
R: BufRead,
{
/// Creates a FASTQ reader.
///
/// # Examples
///
/// ```
/// use noodles_fastq as fastq;
/// let data = b"@r0\nATCG\n+\nNDLS\n";
/// let reader = fastq::io::Reader::new(&data[..]);
/// ```
pub fn new(inner: R) -> Self {
Self { inner }
}
/// Returns a reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_fastq as fastq;
/// let data = [];
/// let reader = fastq::io::Reader::new(&data[..]);
/// assert!(reader.get_ref().is_empty());
/// ```
pub fn get_ref(&self) -> &R {
&self.inner
}
/// Returns a mutable reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_fastq as fastq;
/// let data = [];
/// let mut reader = fastq::io::Reader::new(&data[..]);
/// assert!(reader.get_mut().is_empty());
/// ```
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Unwraps and returns the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_fastq as fastq;
/// let data = [];
/// let reader = fastq::io::Reader::new(&data[..]);
/// assert!(reader.into_inner().is_empty());
/// ```
pub fn into_inner(self) -> R {
self.inner
}
/// Reads a FASTQ record.
///
/// This reads from the underlying stream until four lines are read: the read name, the
/// sequence, the plus line, and the quality scores. Each line omits the trailing newline.
///
/// The stream is expected to be at the start of a record.
///
/// If successful, the number of bytes read is returned. If the number of bytes read is 0, the
/// stream reached EOF.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_fastq as fastq;
///
/// let data = b"@r0\nATCG\n+\nNDLS\n";
/// let mut reader = fastq::io::Reader::new(&data[..]);
///
/// let mut record = fastq::Record::default();
/// reader.read_record(&mut record)?;
///
/// assert_eq!(record.name(), b"r0");
/// assert_eq!(record.sequence(), b"ATCG");
/// assert_eq!(record.quality_scores(), b"NDLS");
/// Ok::<(), io::Error>(())
/// ```
pub fn read_record(&mut self, record: &mut Record) -> io::Result<usize> {
read_record(&mut self.inner, record)
}
/// Returns an iterator over records starting from the current stream position.
///
/// The stream is expected to be at the start of a record.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_fastq::{self as fastq, record::Definition};
///
/// let data = b"@r0\nATCG\n+\nNDLS\n";
/// let mut reader = fastq::io::Reader::new(&data[..]);
///
/// let mut records = reader.records();
///
/// assert_eq!(
/// records.next().transpose()?,
/// Some(fastq::Record::new(Definition::new("r0", ""), "ATCG", "NDLS")
/// ));
///
/// assert!(records.next().is_none());
/// # Ok::<(), io::Error>(())
/// ```
pub fn records(&mut self) -> Records<'_, R> {
Records::new(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::record::Definition;
#[test]
fn test_read_record() -> io::Result<()> {
let data = b"\
@noodles:1/1
AGCT
+
abcd
@noodles:2/1
TCGA
+noodles:2/1
dcba
";
let mut reader = &data[..];
let mut record = Record::default();
read_record(&mut reader, &mut record)?;
let expected = Record::new(Definition::new("noodles:1/1", ""), "AGCT", "abcd");
assert_eq!(record, expected);
read_record(&mut reader, &mut record)?;
let expected = Record::new(Definition::new("noodles:2/1", ""), "TCGA", "dcba");
assert_eq!(record, expected);
let n = read_record(&mut reader, &mut record)?;
assert_eq!(n, 0);
Ok(())
}
}