dnacomb 1.0.0

Count the occurances of structured sequence reads and compare to an expected library
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
//! Error and diagnostic types used throughout DNAComb.
//!
//! This module defines the main error enums used for counting, LibSpec parsing,
//! library import, and sequence-file parsing, as well as helper utilities for
//! converting sequence bytes into displayable strings for logs and output.
use bio::bio_types::alignment::Alignment;
use bio::bio_types::sequence::Sequence;
use bio::io::fastq;
use log::warn;
use std::fmt;
use std::io;

use crate::interning::RegionID;
use crate::interning::region_id_to_str;
use crate::region::RegionCompleteness;

/// Convert a Sequence `Vec<u8>` to a UTF-8 string for display/output.
///
/// If conversion fails, a warning is logged and an empty string is returned
/// rather than aborting processing. This is intended for diagnostics and TSV
/// writing, where best-effort output is preferable to panicking on unexpected
/// non-UTF-8 sequence content. UTF-8 errors should be very rare for normal input
/// and will generally be caught earlier.
pub fn seq_to_string_or_log(seq: &Sequence) -> String {
    match std::str::from_utf8(seq) {
        Ok(i) => i.into(),
        Err(_) => {
            warn!(
                "Error converting Vec<u8> Sequence {:?} to String via UTF-8",
                seq
            );
            "".to_string()
        }
    }
}

/// Error type for read counting and region extraction.
///
/// This is the main operational error type used during counting. It covers
/// invalid region structure, filter-configuration problems, unexpected alignment
/// failures, and generic counting errors.
#[derive(Debug)]
pub enum ReadCountError {
    UnexpectedRegion { region: RegionID },
    FilterConfigError { desc: String },
    BadAlignment { alignment: Box<AlignmentInfo> },
    Error { desc: String },
}

/// Detailed debugging information for an alignment/extraction failure.
///
/// This is attached to `ReadCountError::BadAlignment` to help diagnose cases
/// where alignment succeeded but region extraction from the alignment path
/// produced inconsistent or invalid coordinates.
#[derive(Debug)]
pub struct AlignmentInfo {
    /// Read name
    pub read_id: String,

    /// Read number in input file
    pub read_number: usize,

    /// Alignment string
    pub pretty_alignment: String,

    /// Alignment object
    pub alignment: Alignment,

    /// Vector of region names being matched
    pub region_ids: Vec<RegionID>,

    /// Positions of regions in the template sequence
    pub region_positions: Vec<(usize, usize)>,

    /// Vector of identified region positions positions in the input read
    pub mapped_positions: Vec<Option<(usize, usize, RegionCompleteness)>>,
}

impl fmt::Display for ReadCountError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReadCountError::UnexpectedRegion { region } => {
                write!(
                    f,
                    "Added combination contains an unexpected region: {}",
                    region_id_to_str(region)
                )
            }
            ReadCountError::BadAlignment { alignment } => {
                write!(
                    f,
                    "Alignment or region extraction error\nRead {}, id: {}\nAlignment:\n{}\n\
                     Path:\n{:?}\n\nCigar: {}\nScore: {:?}\nRegions: {:?}\nRegion positions: {:?}\n\
                     Extracted positions: {:?}",
                    alignment.read_number,
                    alignment.read_id,
                    alignment.pretty_alignment,
                    alignment.alignment.path(),
                    alignment.alignment.cigar(false),
                    alignment.alignment.score,
                    alignment.region_ids,
                    alignment.region_positions,
                    alignment.mapped_positions
                )
            }
            ReadCountError::Error { desc } => {
                write!(f, "{}", desc)
            }
            ReadCountError::FilterConfigError { desc } => {
                write!(f, "{}", desc)
            }
        }
    }
}

impl std::error::Error for ReadCountError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None // No underlying error
    }
}

/// Error type for LibSpec parsing and validation.
///
/// Covers JSON parsing, file I/O, and logical validation errors in the sequence
/// specification, such as duplicate regions, invalid lengths, or unsupported
/// region layouts.
#[derive(Debug)]
pub enum LibSpecError {
    /// Generic LibSpec error
    LibSpec { desc: String },

    /// One or more errors invalidating a library, returned from .validate()
    InvalidLibSpec { errs: Vec<String> },

    /// A region has min length greater than max length
    MinGreaterThanMax {
        id: RegionID,
        min: usize,
        max: usize,
    },

    /// Duplicate regions in library
    DuplicateRegion { id: RegionID },

    /// Required region missing
    MissingRegion { id: RegionID },

    /// Two variable regions appear consecutively without a fixed anchor region.
    NeighbouringVariable { id: RegionID },

    /// IO errors
    IOError(io::Error),

    /// JSON Error
    ParsingError(serde_json::Error),
}

impl fmt::Display for LibSpecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LibSpecError::InvalidLibSpec { errs } => {
                writeln!(f, "Multiple LibSpec errors detected:")?;
                for err in errs {
                    writeln!(f, "{}", err)?;
                }
                Ok(())
            }
            LibSpecError::MinGreaterThanMax { id, min, max } => {
                write!(
                    f,
                    "Region {}: min_length ({}) cannot be greater than max_length ({})",
                    region_id_to_str(id),
                    min,
                    max
                )
            }
            LibSpecError::DuplicateRegion { id } => {
                write!(
                    f,
                    "Duplciated region id {} in LibSpec",
                    region_id_to_str(id)
                )
            }
            LibSpecError::MissingRegion { id } => {
                write!(
                    f,
                    "{} not found in LibSpec Region list",
                    region_id_to_str(id)
                )
            }
            LibSpecError::LibSpec { desc } => write!(f, "{}", desc),
            LibSpecError::NeighbouringVariable { id } => {
                write!(
                    f,
                    "Variable region {} follows another variable region",
                    region_id_to_str(id)
                )
            }
            LibSpecError::IOError(e) => write!(f, "Error reading LibSpec JSON file: {}", e),
            LibSpecError::ParsingError(e) => write!(f, "Error parsing LibSpec JSON: {}", e),
        }
    }
}

impl std::error::Error for LibSpecError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None // No underlying error
    }
}

impl From<io::Error> for LibSpecError {
    fn from(err: io::Error) -> LibSpecError {
        LibSpecError::IOError(err)
    }
}

impl From<serde_json::Error> for LibSpecError {
    fn from(err: serde_json::Error) -> LibSpecError {
        LibSpecError::ParsingError(err)
    }
}

/// Error type for expected-library import and lookup setup.
///
/// Covers malformed library TSV input, duplicate or missing region definitions,
/// and incompatibilities between a library TSV and the corresponding LibSpec.
#[derive(Debug)]
pub enum LibraryError {
    /// Generic Library error
    Library { desc: String },

    /// Duplicate regions in sub-library
    DuplicateSubLibraryRegion { id: RegionID },

    /// Duplicate regions in library
    DuplicateRegion { id: RegionID },

    /// Required region missing
    MissingRegion { id: RegionID },

    /// IO errors
    IOError(csv::Error),
}

impl fmt::Display for LibraryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LibraryError::DuplicateSubLibraryRegion { id } => {
                write!(
                    f,
                    "Region id {} is found in multiple Libraries",
                    region_id_to_str(id)
                )
            }
            LibraryError::DuplicateRegion { id } => {
                write!(
                    f,
                    "Duplicated region id {} in Library",
                    region_id_to_str(id)
                )
            }
            LibraryError::MissingRegion { id } => {
                write!(
                    f,
                    "{} not found in Library Region list",
                    region_id_to_str(id)
                )
            }
            LibraryError::Library { desc } => write!(f, "{}", desc),
            LibraryError::IOError(e) => write!(f, "Error reading Library TSV file: {}", e),
        }
    }
}

impl std::error::Error for LibraryError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None // No underlying error
    }
}

impl From<csv::Error> for LibraryError {
    fn from(err: csv::Error) -> LibraryError {
        LibraryError::IOError(err)
    }
}

/// Error while reading an individual sequence record from FASTA or FASTQ input.
///
/// Think interface for Rust Bio errors.
#[derive(Debug)]
pub enum FastaError {
    Fasta(io::Error),
    Fastq(fastq::Error),
}

impl fmt::Display for FastaError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FastaError::Fasta(e) => write!(f, "{}", e),
            FastaError::Fastq(e) => write!(f, "{}", e),
        }
    }
}

impl std::error::Error for FastaError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None // No underlying error
    }
}

impl From<io::Error> for FastaError {
    fn from(err: io::Error) -> FastaError {
        FastaError::Fasta(err)
    }
}

impl From<fastq::Error> for FastaError {
    fn from(err: fastq::Error) -> FastaError {
        FastaError::Fastq(err)
    }
}

/// Error while reading or constructing a forward/reverse read pair.
///
/// This includes per-record parsing failures, file-format problems, paired-file
/// synchronisation issues, and lower-level I/O errors.
#[derive(Debug)]
pub enum ReadPairError {
    /// Forward and/or reverse record parsing failed for the current pair.
    ReadPair {
        forward: Option<FastaError>,
        reverse: Option<FastaError>,
    },
    /// Input file format or auto-detection was invalid.
    Format { desc: String },
    /// One paired-end file ended before the other.
    EarlyExhastion { read: String },
    /// Generic file IO error.
    IO(io::Error),
}

impl fmt::Display for ReadPairError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReadPairError::ReadPair { forward, reverse } => match (forward, reverse) {
                (Some(forward), Some(reverse)) => write!(
                    f,
                    "Error in both reads.\nForward: {}\nReverse: {}",
                    forward, reverse
                ),
                (Some(forward), None) => write!(f, "Error in forward read: {}", forward),
                (None, Some(reverse)) => write!(f, "Error in reverse read: {}", reverse),
                (None, None) => write!(f, "Unknown read parsing error"),
            },
            ReadPairError::Format { desc } => write!(f, "{}", desc),
            ReadPairError::EarlyExhastion { read } => {
                write!(f, "Paired reads out of sync: {} exhausted first", read)
            }
            ReadPairError::IO(e) => write!(f, "{}", e),
        }
    }
}

impl std::error::Error for ReadPairError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None // No underlying error
    }
}

impl From<io::Error> for ReadPairError {
    fn from(err: io::Error) -> ReadPairError {
        ReadPairError::IO(err)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn seq_to_string() {
        let seq: Sequence = vec![b'A', b'C', b'G', b'T'];
        let string: String = "ACGT".to_string();
        assert_eq!(seq_to_string_or_log(&seq), string)
    }

    #[test]
    fn seq_to_string_empty() {
        let seq: Sequence = vec![];
        let string: String = "".to_string();
        assert_eq!(seq_to_string_or_log(&seq), string)
    }

    #[test]
    fn seq_to_string_warning() {
        let seq: Sequence = vec![b'A', b'C', b'G', 0xC0]; // Invalid UTF-8 byte
        let string: String = "".to_string();
        assert_eq!(seq_to_string_or_log(&seq), string)
    }

    #[test]
    fn test_libspec_error_invalid_multiple_display() {
        let errs = vec![
            "Error 1".to_string(),
            "Error 2".to_string(),
            "Error 3".to_string(),
        ];
        let err = LibSpecError::InvalidLibSpec { errs };
        let msg = format!("{}", err);
        assert!(msg.contains("Error 1"));
        assert!(msg.contains("Error 2"));
        assert!(msg.contains("Error 3"));
    }
}