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
//! FASTQ reading and writing //! //! # Flavours //! //! There are two flavours of this parser: //! //! * [`fastq::Reader`](crate::fastq::Reader) in this module parses standard //! single-line FASTQ. //! * [`Reader`](crate::fastq::multiline::Reader) in [`fastq::multiline`](crate::fastq::multiline) //! parses multi-line FASTQ as well. This parser runs slightly slower on //! single-line FASTQ than `fastq::Reader`. //! //! # Example //! //! The following example shows how to use [`Reader`](Reader). //! //! ```rust //! use seq_io::prelude::*; // needed to import necessary traits //! use seq_io::fastq::Reader; //! //! # fn main() { //! let seq = b"@id1 some description //! SEQUENCE //! + //! IIIIIIII //! @id2 //! SEQUENCE //! + //! IIIIIIII //! "; //! //! // Construct the reader //! let mut reader = Reader::new(&seq[..]); //! //! // We'll write the records back to this vector //! let mut output = vec![]; //! //! while let Some(result) = reader.next() { //! let rec = result.unwrap(); //! //! // Access the ID and the description parts of the header (separated by a space) //! let id = rec.id().unwrap(); //! let desc = rec.desc().transpose().unwrap(); //! println!("ID: {}, description: {:?}", id, desc); //! //! // Print the sequence and quality scores //! println!("seq: {}", std::str::from_utf8(rec.seq()).unwrap()); //! println!("qual: {}", std::str::from_utf8(rec.qual()).unwrap()); //! //! // Write the record to 'output' //! rec.write(&mut output).unwrap(); //! } //! //! // The output is identical //! assert_eq!(&seq[..], output.as_slice()); //! # } //! ``` //! //! The output will be: //! //! ```text //! ID: id1, description: Some("some description") //! seq: SEQUENCE //! qual: IIIIIIII //! ID: id2, description: None //! seq: SEQUENCE //! qual: IIIIIIII //! ``` //! //! As the record returned by the [`next()`](Reader::next) //! method borrows its data from the underlying buffer, it is not possible to //! use a `for` loop for iterating. Therefore, we use the `while let ...` //! construct. //! //! # Sequence record types //! //! Similarly to [`fasta`](crate::fasta))`::Reader`, there are two record types, //! which both implement the common [`BaseRecord`](crate::BaseRecord) //! trait, [and `fastq::Record`](Record) providing additional FASTQ //! specific methods: //! //! * [`RefRecord`](RefRecord), the type returned by //! `Reader::next()`, only remembers the position of the record in the buffer //! without copying any data. //! * [`OwnedRecord`](OwnedRecord) owns its data. //! //! # Writing FASTQ //! //! Records can be written to output using //! [`BaseRecord::write()`](crate::BaseRecord::write). //! `RefRecord` additionally has the method //! [`write_unchanged`](RefRecord::write_unchanged), which may be //! faster. //! //! It is also possible to write data not part of a FASTQ record directly using //! a set of different functions [listed here](#functions). //! //! # Details on parsing and writing //! //! * Like all parsers in this crate, `fasta::Reader` handles UNIX (LF) and //! Windows (CRLF) line endings, but not old Mac-style (CR) endings. LF and //! CRLF may be mixed within the same file. //! * FASTQ writing currently always uses UNIX line endings. //! * The first non-empty line should start with `@`, indicating the first //! header. If not, an error with `ErrorKind::InvalidStart` is returned. //! * Empty lines are allowed before and after records, but *not within* //! records. //! * Whitespace at the end of header and sequence lines is never removed. //! * Empty input will result in `None` being returned immediately by //! `Reader::next()` and in empty iterators for `RecordsIter` / //! `RecordsIntoIter`. //! * `Reader::next()` compares sequence and quality line lengths and returns //! an error of `ErrorKind::UnequalLengths` if different. It is possible to //! omit this check by calling //! [`Reader::next_unchecked_len()`](struct.Reader::next_unchecked_len). //! The lengths can be checked later with [`Record::check_lengths()`](Record::check_lengths) //! or [`RefRecord::check_lengths_strict()`](RefRecord::check_lengths_strict) //! * The quality line of the last record should either terminated by a line ending. //! If not, an error of `ErrorKind::UnexpectedEnd` is returned. //! //! ## Error priority //! //! Validity checks are done in the following order: //! //! * Is the start byte correct (`@`)? If not: [`InvalidStart`](ErrorKind::InvalidStart). //! * Do the the header, sequence and separator lines have a line terminator? //! If not: [`UnexpectedEnd`](ErrorKind::UnexpectedEnd). //! * Is the separator byte correct (`+`)? If not: [`InvalidSep`](ErrorKind::InvalidSep). //! * Are the quality scores have a line terminator *or* are they //! non-empty? If not: [`UnexpectedEnd`](ErrorKind::UnexpectedEnd). #[macro_use] mod error; #[macro_use] mod reader; pub mod multiline; mod position; mod record; mod write; pub use self::error::*; pub use self::position::*; pub use self::reader::*; pub use self::record::*; pub use self::write::*;