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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
//! FASTQ record.
mod definition;
pub use self::definition::Definition;
use std::fmt;
/// A FASTQ record.
#[derive(Clone, Default, Debug, Eq, PartialEq)]
pub struct Record {
definition: Definition,
sequence: Vec<u8>,
quality_scores: Vec<u8>,
}
impl Record {
/// Creates a FASTQ record.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// ```
pub fn new<S, Q>(definition: Definition, sequence: S, quality_scores: Q) -> Self
where
S: Into<Vec<u8>>,
Q: Into<Vec<u8>>,
{
Self {
definition,
sequence: sequence.into(),
quality_scores: quality_scores.into(),
}
}
/// Returns the record definition.
pub fn definition(&self) -> &Definition {
&self.definition
}
/// Returns a mutable reference to the record definition.
pub fn definition_mut(&mut self) -> &mut Definition {
&mut self.definition
}
/// Returns the name of the record.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// assert_eq!(record.name(), b"r0");
/// ```
pub fn name(&self) -> &[u8] {
self.definition.name()
}
/// Returns a mutable reference to the name.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let mut record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// *record.name_mut() = b"r1".to_vec();
/// assert_eq!(record.name(), b"r1");
/// ```
pub fn name_mut(&mut self) -> &mut Vec<u8> {
self.definition.name_mut()
}
/// Returns the description of the record.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// assert!(record.description().is_empty());
/// ```
pub fn description(&self) -> &[u8] {
self.definition.description()
}
/// Returns a mutable reference to the description.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let mut record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// *record.description_mut() = b"LN=4".to_vec();
/// assert_eq!(record.description(), b"LN=4");
/// ```
pub fn description_mut(&mut self) -> &mut Vec<u8> {
self.definition.description_mut()
}
/// Returns the sequence of the record.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// assert_eq!(record.sequence(), b"AGCT");
/// ```
pub fn sequence(&self) -> &[u8] {
&self.sequence
}
/// Returns a mutable reference to the sequence.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let mut record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// record.sequence_mut()[0] = b'C';
/// assert_eq!(record.sequence(), b"CGCT");
/// ```
pub fn sequence_mut(&mut self) -> &mut Vec<u8> {
&mut self.sequence
}
/// Returns the quality scores of the record.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// assert_eq!(record.quality_scores(), b"NDLS");
/// ```
pub fn quality_scores(&self) -> &[u8] {
&self.quality_scores
}
/// Returns a mutable reference to the quality scores.
///
/// # Examples
///
/// ```
/// use noodles_fastq::{self as fastq, record::Definition};
/// let mut record = fastq::Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
/// *record.quality_scores_mut() = b"!!!!".to_vec();
/// assert_eq!(record.quality_scores(), b"!!!!");
/// ```
pub fn quality_scores_mut(&mut self) -> &mut Vec<u8> {
&mut self.quality_scores
}
// Truncates all field buffers to 0.
pub(crate) fn clear(&mut self) {
self.definition.clear();
self.sequence.clear();
self.quality_scores.clear();
}
}
impl fmt::Display for Record {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("@")?;
for &b in self.name() {
write!(f, "{}", b as char)?;
}
if !self.description().is_empty() {
write!(f, " ")?;
for &b in self.description() {
write!(f, "{}", b as char)?;
}
}
writeln!(f)?;
for &b in self.sequence() {
write!(f, "{}", b as char)?;
}
writeln!(f)?;
writeln!(f, "+")?;
for &b in self.quality_scores() {
write!(f, "{}", b as char)?;
}
writeln!(f)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fmt() {
let mut record = Record::new(Definition::new("r0", ""), "ATCG", "NDLS");
assert_eq!(record.to_string(), "@r0\nATCG\n+\nNDLS\n");
record.description_mut().extend_from_slice(b"LN:4");
assert_eq!(record.to_string(), "@r0 LN:4\nATCG\n+\nNDLS\n");
}
#[test]
fn test_clear() {
let mut record = Record::new(Definition::new("r0", ""), "AGCT", "NDLS");
record.clear();
assert!(record.name().is_empty());
assert!(record.description().is_empty());
assert!(record.sequence().is_empty());
assert!(record.quality_scores().is_empty());
}
}