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
//! Fast, streaming FASTA/FASTQ I/O for bioinformatics pipelines.
//!
//! `fastx` covers the everyday sequence work that would otherwise pull in
//! BioPython or BioPerl: reading and writing FASTA/FASTQ (plain or gzipped),
//! reverse complementing, translating, quality trimming, assembly statistics and
//! `samtools faidx`-style random access — with a streaming parser that keeps
//! memory flat regardless of file size.
//!
//! # Reading
//!
//! ```no_run
//! // Format and gzip are detected from the extension and the magic bytes.
//! for record in fastx::open("reads.fq.gz")? {
//! let record = record?;
//! println!("{}\t{}\t{:?}", record.id, record.len(), record.gc_content());
//! }
//! # Ok::<(), fastx::Error>(())
//! ```
//!
//! In a hot loop, reuse one record so parsing does not allocate at all:
//!
//! ```
//! use fastx::{FastxReader, Sequence};
//!
//! let data = b"@r1\nACGTN\n+\nIIIII\n";
//! let mut reader = FastxReader::new(&data[..]);
//! let mut record = Sequence::default();
//! while reader.read_into(&mut record)? {
//! assert_eq!(record.len(), 5);
//! }
//! # Ok::<(), fastx::Error>(())
//! ```
//!
//! For a pass that never keeps a record, [`FastxReader::read_ref`] borrows
//! straight from the read buffer and copies nothing at all:
//!
//! ```
//! use fastx::FastxReader;
//!
//! let data = b"@r1\nACGTN\n+\nIIIII\n";
//! let mut bases = 0;
//! FastxReader::new(&data[..]).for_each_ref(|record| {
//! bases += record.len();
//! Ok(())
//! })?;
//! assert_eq!(bases, 5);
//! # Ok::<(), fastx::Error>(())
//! ```
//!
//! # Writing
//!
//! ```no_run
//! use fastx::{Alphabet, Sequence, WriterBuilder};
//!
//! let mut writer = WriterBuilder::new()
//! .line_width(80) // FASTA wrapping; 0 disables it
//! .validate(Alphabet::Dna) // reject anything but ACGTN before writing
//! .create("contigs.fa.gz")?; // gzip inferred from the extension
//!
//! writer.write_record(&Sequence::fasta("contig1", b"ACGTACGT"))?;
//! writer.finish()?; // always finish: it surfaces gzip errors
//! # Ok::<(), fastx::Error>(())
//! ```
//!
//! # Converting FASTQ to FASTA
//!
//! ```
//! use fastx::{FastxReader, FastxWriter, Format};
//!
//! let fastq = b"@r1 sample\nACGTACGT\n+\nIIIIIIII\n";
//! let mut out = Vec::new();
//! let mut writer = FastxWriter::new(&mut out, Format::Fasta).line_width(4);
//! for record in FastxReader::new(&fastq[..]) {
//! writer.write_record(&record?)?;
//! }
//! writer.flush()?;
//! assert_eq!(out, b">r1 sample\nACGT\nACGT\n");
//! # Ok::<(), fastx::Error>(())
//! ```
//!
//! # Cargo features
//!
//! | feature | default | effect |
//! |---|---|---|
//! | `gzip` | yes | transparent gzip/BGZF reading and writing via `flate2` |
//! | `parallel` | no | [`parallel`] module: batched and chunk-split multicore processing via `rayon` |
//! | `zstd` | no | Zstandard reading and writing; needs a C toolchain and Rust 1.85 |
//! | `libdeflate` | no | swaps BGZF's deflate for libdeflate: ~2× faster and slightly smaller. Same C toolchain and Rust 1.85 |
//! | `full` | no | everything above |
//!
//! The crate's own MSRV is 1.74, but a feature inherits the MSRV of what it
//! pulls in: `parallel` needs 1.80, and `zstd` and `libdeflate` need 1.85.
//!
//! # Design notes
//!
//! * **Streaming by default.** [`FastxReader`] holds one growable buffer and one
//! record; a 300 GB FASTQ uses the same memory as a 300 byte one. Lines longer
//! than the buffer grow it, so single-line chromosomes work too.
//! * **Multi-line records.** Wrapped FASTA is joined transparently, and
//! multi-line FASTQ is handled by matching quality length to sequence length
//! rather than by assuming four lines per record — which also makes `@` at the
//! start of a quality line harmless.
//! * **Errors carry line numbers.** Malformed input produces [`Error::Parse`]
//! with the offending line, not a silent skip.
//! * **No silent data invention.** Writing FASTQ without quality scores is an
//! error; writing a FASTQ record as FASTA drops quality, which is what
//! `fq2fa` should do.
pub use crateSequenceRef;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use crateQualityEncoding;
pub use crate;
pub use crateSequence;
pub use crate;
pub use crateSeqStats;
pub use crate;
/// Everything you normally need, in one `use`.
///
/// ```
/// use fastx::prelude::*;
///
/// let mut stats = SeqStats::new();
/// FastxReader::new(&b">a\nACGT\n"[..]).for_each_record(|r| { stats.push(r); Ok(()) })?;
/// assert_eq!(stats.count, 1);
/// # Ok::<(), fastx::Error>(())
/// ```
/// Read every record of a file into memory.
///
/// Convenient for reference files and test fixtures; use [`open`] for anything
/// that might not fit in RAM.
///
/// ```no_run
/// let records = fastx::read_all("primers.fa")?;
/// assert!(!records.is_empty());
/// # Ok::<(), fastx::Error>(())
/// ```
/// Read every record from any reader into memory.
///
/// ```
/// let records = fastx::read_all_from(&b">a\nACGT\n>b\nTT\n"[..])?;
/// assert_eq!(records.len(), 2);
/// # Ok::<(), fastx::Error>(())
/// ```
/// Write records to a file, inferring format and compression from the path.
///
/// ```no_run
/// let records = vec![fastx::Sequence::fasta("a", b"ACGT")];
/// fastx::write_all("out.fa", &records)?;
/// # Ok::<(), fastx::Error>(())
/// ```
/// Summary statistics for a file, in one call.
///
/// ```no_run
/// let stats = fastx::stats_of("reads.fq.gz")?;
/// println!("{stats}");
/// # Ok::<(), fastx::Error>(())
/// ```