seqair 0.1.0

Pure-Rust BAM/SAM/CRAM/FASTA reader and pileup engine
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Parse FAI indexes to locate sequences within a FASTA file. [`FastaIndex`] maps sequence
//! names to [`FaiEntry`] offsets and line widths; used by [`IndexedFastaReader`](super::IndexedFastaReader)
//! to seek directly to any subsequence.

use rustc_hash::FxHashMap;
use seqair_types::SmolStr;
use std::path::Path;
use tracing::instrument;

// r[impl io.errors]
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum FaiError {
    #[error("I/O error reading FAI index {path}")]
    Read { path: std::path::PathBuf, source: std::io::Error },

    #[error("FAI index {path} is empty")]
    Empty { path: std::path::PathBuf },

    #[error("FAI index {path} line {line_number}: {kind}")]
    InvalidEntry { path: std::path::PathBuf, line_number: usize, kind: FaiEntryError },
}

#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum FaiEntryError {
    #[error("expected 5 tab-separated fields, found {found}")]
    TooFewFields { found: usize },

    #[error("expected 5 tab-separated fields, found {found}")]
    TooManyFields { found: usize },

    #[error("invalid {field} value {raw_value:?}: not a valid integer")]
    InvalidField { field: &'static str, raw_value: String },

    #[error("linebases is 0 (would cause division by zero) for sequence {name:?}")]
    ZeroLinebases { name: SmolStr },

    #[error("linewidth ({linewidth}) < linebases ({linebases}) for sequence {name:?}")]
    LinewidthTooSmall { name: SmolStr, linebases: u64, linewidth: u64 },

    #[error("sequence length is 0 for sequence {name:?}")]
    ZeroLength { name: SmolStr },

    #[error("duplicate sequence name {name:?}")]
    DuplicateName { name: SmolStr },
}

/// A single entry from a `.fai` index file.
#[derive(Debug, Clone)]
pub struct FaiEntry {
    pub name: SmolStr,
    pub length: u64,
    pub offset: u64,
    pub linebases: u64,
    pub linewidth: u64,
}

impl FaiEntry {
    // r[impl fasta.index.offset_calculation]
    pub fn byte_offset(&self, pos: u64) -> u64 {
        let line =
            pos.checked_div(self.linebases).expect("linebases is non-zero, enforced by FAI parser");
        let col =
            pos.checked_rem(self.linebases).expect("linebases is non-zero, enforced by FAI parser");
        self.offset.wrapping_add(line.wrapping_mul(self.linewidth)).wrapping_add(col)
    }
}

// r[impl fasta.index.parse]
// r[impl fasta.index.name_lookup]
#[derive(Debug, Clone)]
pub struct FastaIndex {
    entries: Vec<FaiEntry>,
    name_to_idx: FxHashMap<SmolStr, usize>,
}

impl FastaIndex {
    #[instrument(level = "debug", fields(path = %path.display()))]
    pub fn from_file(path: &Path) -> Result<Self, FaiError> {
        let contents = std::fs::read_to_string(path)
            .map_err(|source| FaiError::Read { path: path.to_path_buf(), source })?;
        Self::parse(&contents, path)
    }

    /// Parse FAI index from string contents.
    #[cfg(feature = "fuzz")]
    pub fn from_contents(contents: &str) -> Result<Self, FaiError> {
        Self::parse(contents, Path::new("<fuzz>"))
    }

    fn parse(contents: &str, path: &Path) -> Result<Self, FaiError> {
        let mut entries = Vec::new();
        let mut name_to_idx = FxHashMap::default();

        for (line_num, line) in contents.lines().enumerate() {
            if line.is_empty() {
                continue;
            }

            // r[impl fasta.index.fields]
            let mut fields = line.splitn(6, '\t');
            let (Some(name_str), Some(len_s), Some(off_s), Some(lb_s), Some(lw_s)) =
                (fields.next(), fields.next(), fields.next(), fields.next(), fields.next())
            else {
                let found = line.split('\t').count();
                return Err(FaiError::InvalidEntry {
                    path: path.to_path_buf(),
                    line_number: line_num.wrapping_add(1),
                    kind: FaiEntryError::TooFewFields { found },
                });
            };
            if fields.next().is_some() {
                let found = line.split('\t').count();
                return Err(FaiError::InvalidEntry {
                    path: path.to_path_buf(),
                    line_number: line_num.wrapping_add(1),
                    kind: FaiEntryError::TooManyFields { found },
                });
            }

            let name = SmolStr::new(name_str);
            let length = parse_fai_field(len_s, "length", line_num, path)?;
            let offset = parse_fai_field(off_s, "offset", line_num, path)?;
            let linebases = parse_fai_field(lb_s, "linebases", line_num, path)?;
            let linewidth = parse_fai_field(lw_s, "linewidth", line_num, path)?;

            // r[impl fasta.index.validation]
            if linebases == 0 {
                return Err(FaiError::InvalidEntry {
                    path: path.to_path_buf(),
                    line_number: line_num.wrapping_add(1),
                    kind: FaiEntryError::ZeroLinebases { name },
                });
            }
            if linewidth < linebases {
                return Err(FaiError::InvalidEntry {
                    path: path.to_path_buf(),
                    line_number: line_num.wrapping_add(1),
                    kind: FaiEntryError::LinewidthTooSmall { name, linebases, linewidth },
                });
            }
            if length == 0 {
                return Err(FaiError::InvalidEntry {
                    path: path.to_path_buf(),
                    line_number: line_num.wrapping_add(1),
                    kind: FaiEntryError::ZeroLength { name },
                });
            }

            if name_to_idx.contains_key(&name) {
                return Err(FaiError::InvalidEntry {
                    path: path.to_path_buf(),
                    line_number: line_num.wrapping_add(1),
                    kind: FaiEntryError::DuplicateName { name },
                });
            }

            name_to_idx.insert(name.clone(), entries.len());
            entries.push(FaiEntry { name, length, offset, linebases, linewidth });
        }

        if entries.is_empty() {
            return Err(FaiError::Empty { path: path.to_path_buf() });
        }

        Ok(FastaIndex { entries, name_to_idx })
    }

    pub fn get(&self, name: &str) -> Option<&FaiEntry> {
        let &idx = self.name_to_idx.get(name)?;
        self.entries.get(idx)
    }

    pub fn sequence_names(&self) -> Vec<SmolStr> {
        self.entries.iter().map(|e| e.name.clone()).collect()
    }

    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

fn parse_fai_field(
    s: &str,
    field_name: &'static str,
    line_num: usize,
    path: &Path,
) -> Result<u64, FaiError> {
    s.parse().map_err(|_| FaiError::InvalidEntry {
        path: path.to_path_buf(),
        line_number: line_num.wrapping_add(1),
        kind: FaiEntryError::InvalidField { field: field_name, raw_value: s.to_string() },
    })
}

#[cfg(test)]
#[allow(clippy::arithmetic_side_effects, reason = "test arithmetic is not safety-critical")]
mod tests {
    use super::*;
    use std::path::PathBuf;

    fn test_path() -> PathBuf {
        PathBuf::from("test.fai")
    }

    // r[verify fasta.index.parse]
    // r[verify fasta.index.name_lookup]
    #[test]
    fn parse_simple_index() {
        let contents = "seq1\t100\t6\t50\t51\nseq2\t200\t160\t80\t81\n";
        let idx = FastaIndex::parse(contents, &test_path()).unwrap();

        assert_eq!(idx.len(), 2);
        let e1 = idx.get("seq1").unwrap();
        assert_eq!(e1.length, 100);
        assert_eq!(e1.offset, 6);
        assert_eq!(e1.linebases, 50);
        assert_eq!(e1.linewidth, 51);

        let e2 = idx.get("seq2").unwrap();
        assert_eq!(e2.length, 200);
        assert_eq!(e2.offset, 160);
    }

    // r[verify fasta.index.name_lookup]
    #[test]
    fn lookup_missing_sequence() {
        let contents = "seq1\t100\t6\t50\t51\n";
        let idx = FastaIndex::parse(contents, &test_path()).unwrap();
        assert!(idx.get("nonexistent").is_none());
    }

    // r[verify fasta.index.fields]
    #[test]
    fn reject_too_few_fields() {
        let contents = "seq1\t100\t6\t50\n"; // only 4 fields
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        match err {
            FaiError::InvalidEntry {
                kind: FaiEntryError::TooFewFields { found },
                line_number,
                ..
            } => {
                assert_eq!(found, 4, "should report 4 fields found");
                assert_eq!(line_number, 1);
            }
            other => panic!("expected TooFewFields, got {other:?}"),
        }
    }

    // r[verify fasta.index.fields]
    #[test]
    fn reject_too_many_fields() {
        let contents = "seq1\t100\t6\t50\t51\textra\n"; // 6 fields
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        match err {
            FaiError::InvalidEntry {
                kind: FaiEntryError::TooManyFields { found },
                line_number,
                ..
            } => {
                assert_eq!(found, 6, "should report 6 fields found");
                assert_eq!(line_number, 1);
            }
            other => panic!("expected TooManyFields, got {other:?}"),
        }
    }

    // r[verify fasta.index.fields]
    #[test]
    fn reject_invalid_field_value() {
        let contents = "chr1\tNOTANUM\t0\t50\t51\n"; // non-integer length
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        match err {
            FaiError::InvalidEntry {
                kind: FaiEntryError::InvalidField { field, raw_value },
                line_number,
                ..
            } => {
                assert_eq!(field, "length");
                assert_eq!(raw_value, "NOTANUM");
                assert_eq!(line_number, 1);
            }
            other => panic!("expected InvalidField, got {other:?}"),
        }
    }

    // r[verify fasta.index.fields]
    #[test]
    fn reject_invalid_field_via_file() {
        use std::io::Write;
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.fai");
        let mut f = std::fs::File::create(&path).unwrap();
        writeln!(f, "chr1\t1000\tBAD_OFFSET\t50\t51").unwrap();
        drop(f);

        let err = FastaIndex::from_file(&path).unwrap_err();
        match err {
            FaiError::InvalidEntry {
                kind: FaiEntryError::InvalidField { field, raw_value },
                ..
            } => {
                assert_eq!(field, "offset");
                assert_eq!(raw_value, "BAD_OFFSET");
            }
            other => panic!("expected InvalidField, got {other:?}"),
        }
    }

    // r[verify fasta.index.validation]
    #[test]
    fn reject_zero_linebases() {
        let contents = "seq1\t100\t6\t0\t1\n";
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        assert!(
            matches!(err, FaiError::InvalidEntry { kind: FaiEntryError::ZeroLinebases { .. }, .. }),
            "expected ZeroLinebases, got {err:?}"
        );
    }

    // r[verify fasta.index.validation]
    #[test]
    fn reject_linewidth_less_than_linebases() {
        let contents = "seq1\t100\t6\t50\t49\n";
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        assert!(
            matches!(
                err,
                FaiError::InvalidEntry { kind: FaiEntryError::LinewidthTooSmall { .. }, .. }
            ),
            "expected LinewidthTooSmall, got {err:?}"
        );
    }

    // r[verify fasta.index.validation]
    #[test]
    fn reject_zero_length() {
        let contents = "seq1\t0\t6\t50\t51\n";
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        assert!(
            matches!(err, FaiError::InvalidEntry { kind: FaiEntryError::ZeroLength { .. }, .. }),
            "expected ZeroLength, got {err:?}"
        );
    }

    // r[verify fasta.index.name_lookup]
    #[test]
    fn reject_duplicate_names() {
        let contents = "seq1\t100\t6\t50\t51\nseq1\t200\t160\t80\t81\n";
        let err = FastaIndex::parse(contents, &test_path()).unwrap_err();
        assert!(
            matches!(err, FaiError::InvalidEntry { kind: FaiEntryError::DuplicateName { .. }, .. }),
            "expected DuplicateName, got {err:?}"
        );
    }

    #[test]
    fn reject_empty_index() {
        let err = FastaIndex::parse("", &test_path()).unwrap_err();
        assert!(matches!(err, FaiError::Empty { .. }), "expected Empty, got {err:?}");
    }

    #[test]
    fn byte_offset_single_line() {
        // Sequence fits on one line: 4 bases, linebases=4, linewidth=5
        let entry = FaiEntry { name: "s".into(), length: 4, offset: 6, linebases: 4, linewidth: 5 };
        // pos 0 → offset + 0 = 6
        assert_eq!(entry.byte_offset(0), 6);
        // pos 3 → offset + 3 = 9
        assert_eq!(entry.byte_offset(3), 9);
    }

    #[test]
    fn byte_offset_multi_line() {
        // 100 bases, 50 per line, linewidth 51 (50 bases + \n)
        let entry =
            FaiEntry { name: "s".into(), length: 100, offset: 10, linebases: 50, linewidth: 51 };
        // pos 0 → 10
        assert_eq!(entry.byte_offset(0), 10);
        // pos 49 → 10 + 49 = 59 (last base of first line)
        assert_eq!(entry.byte_offset(49), 59);
        // pos 50 → 10 + 1*51 + 0 = 61 (first base of second line, skipping \n)
        assert_eq!(entry.byte_offset(50), 61);
        // pos 51 → 10 + 1*51 + 1 = 62
        assert_eq!(entry.byte_offset(51), 62);
    }

    #[test]
    fn byte_offset_crlf() {
        // Windows-style line endings: linewidth = linebases + 2
        let entry = FaiEntry {
            name: "s".into(),
            length: 100,
            offset: 10,
            linebases: 50,
            linewidth: 52, // 50 bases + \r\n
        };
        // pos 50 → 10 + 1*52 + 0 = 62 (skips \r\n)
        assert_eq!(entry.byte_offset(50), 62);
    }

    #[test]
    fn parse_real_fai() {
        let contents = "chr19\t61431566\t7\t50\t51\n\
                         2kb_3_Unmodified\t2018\t62660223\t2018\t2019\n\
                         bacteriophage_lambda_CpG\t48502\t62662268\t48502\t48503\n";
        let idx = FastaIndex::parse(contents, &test_path()).unwrap();
        assert_eq!(idx.len(), 3);

        let chr19 = idx.get("chr19").unwrap();
        assert_eq!(chr19.length, 61_431_566);
        assert_eq!(chr19.offset, 7);

        let lambda = idx.get("bacteriophage_lambda_CpG").unwrap();
        assert_eq!(lambda.length, 48502);
    }
}