seq_geom_parser 1.1.0

Parser and extractor for sequencing read geometry descriptions
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Chumsky-based parser for sequencing read geometry descriptions.
//!
//! Grammar:
//! ```text
//! FragmentGeom := Read1Desc Read2Desc
//! Read1Desc    := '1{' DescList '}'
//! Read2Desc    := '2{' DescList '}'
//! DescList     := Desc+
//! Desc         := TagSpec | DistFunc | UnboundedDesc
//! TagSpec      := TagChar '[' LenSpec ']'
//!              |  TagChar '[' Sequence ']'    (* for 'f' tag *)
//!              |  TagChar Digit '[' LenSpec ']'  (* numbered barcode *)
//! LenSpec      := Number ('-' Number)?
//! UnboundedDesc := ('x' | 'r') ':'
//! DistFunc     := 'hamming(' TagSpec ',' Number ')'
//! Number       := [1-9][0-9]* | '0'
//! Sequence     := [ACGT]+
//! TagChar      := 'b' | 'u' | 'r' | 'f' | 'x' | 's'
//! ```

use chumsky::prelude::*;

use crate::types::*;

/// Parse a geometry description string into a [`FragmentGeom`].
///
/// Returns `Ok(FragmentGeom)` on success, or an error with source spans
/// and descriptive messages on failure.
pub fn parse_geometry(input: &str) -> Result<FragmentGeom, Vec<Rich<'_, char>>> {
    let result = fragment_geom_parser().parse(input);
    result.into_result()
}

/// Format parse errors into human-readable strings.
pub fn format_errors(input: &str, errors: &[Rich<'_, char>]) -> String {
    use ariadne::{Color, Label, Report, ReportKind, Source};

    let mut buf = Vec::new();
    for err in errors {
        let span = err.span();
        Report::build(ReportKind::Error, span.start..span.end)
            .with_message(format!("{}", err))
            .with_label(
                Label::new(span.start..span.end)
                    .with_message(format!("{}", err.reason()))
                    .with_color(Color::Red),
            )
            .finish()
            .write(Source::from(input), &mut buf)
            .unwrap();
    }
    String::from_utf8(buf).unwrap_or_else(|_| "error rendering failed".to_string())
}

// ── Parser combinators ──────────────────────────────────────────────

fn fragment_geom_parser<'a>() -> impl Parser<'a, &'a str, FragmentGeom, extra::Err<Rich<'a, char>>>
{
    read_desc_parser('1')
        .then(read_desc_parser('2'))
        .then_ignore(end())
        .map(|(r1, r2)| FragmentGeom {
            read1: r1,
            read2: r2,
        })
}

fn read_desc_parser<'a>(
    read_num: char,
) -> impl Parser<'a, &'a str, ReadGeom, extra::Err<Rich<'a, char>>> {
    just(read_num)
        .ignore_then(desc_list_parser().delimited_by(just('{'), just('}')))
        .map(|parts| ReadGeom { parts })
}

fn desc_list_parser<'a>() -> impl Parser<'a, &'a str, Vec<GeoPart>, extra::Err<Rich<'a, char>>> {
    desc_parser().repeated().at_least(1).collect::<Vec<_>>()
}

fn desc_parser<'a>() -> impl Parser<'a, &'a str, GeoPart, extra::Err<Rich<'a, char>>> {
    dist_func_parser()
        .or(tag_spec_parser())
        .or(unbounded_desc_parser())
}

fn tag_spec_parser<'a>() -> impl Parser<'a, &'a str, GeoPart, extra::Err<Rich<'a, char>>> {
    // Numbered barcode: b0[N], b1[N], etc.
    let numbered_bc = just('b')
        .ignore_then(one_of("0123456789").map(|c: char| c.to_digit(10).unwrap() as u8))
        .then(len_spec_parser().delimited_by(just('['), just(']')))
        .map(|(level, len)| GeoPart {
            tag: GeoTagType::NumberedBarcode(level),
            len,
            sequence: None,
            tolerance: None,
        });

    // Fixed sequence: f[ACGT...]
    let fixed_seq = just('f')
        .ignore_then(
            one_of("ACGTacgt")
                .repeated()
                .at_least(1)
                .collect::<String>()
                .delimited_by(just('['), just(']')),
        )
        .map(|seq| GeoPart {
            tag: GeoTagType::Fixed,
            len: GeoLen::Fixed(seq.len() as u32),
            sequence: Some(seq.to_uppercase().into_bytes()),
            tolerance: None,
        });

    // Standard tags: b[N], u[N], r[N], x[N], x[N-M], s[N]
    let standard_tag = one_of("buxrs")
        .then(len_spec_parser().delimited_by(just('['), just(']')))
        .map(|(tag_char, len)| {
            let tag = match tag_char {
                'b' => GeoTagType::Barcode,
                'u' => GeoTagType::Umi,
                'x' => GeoTagType::Discard,
                'r' => GeoTagType::Read,
                's' => GeoTagType::SampleBarcode,
                _ => unreachable!(),
            };
            GeoPart {
                tag,
                len,
                sequence: None,
                tolerance: None,
            }
        });

    // Try numbered barcode first (b0[...]), then fixed, then standard
    numbered_bc.or(fixed_seq).or(standard_tag)
}

fn len_spec_parser<'a>() -> impl Parser<'a, &'a str, GeoLen, extra::Err<Rich<'a, char>>> {
    let num = text::int(10).map(|s: &str| s.parse::<u32>().unwrap());

    num.then(just('-').ignore_then(num).or_not())
        .map(|(n, m)| match m {
            Some(m) => GeoLen::Range(n, m),
            None => GeoLen::Fixed(n),
        })
}

fn unbounded_desc_parser<'a>() -> impl Parser<'a, &'a str, GeoPart, extra::Err<Rich<'a, char>>> {
    one_of("xr").then_ignore(just(':')).map(|tag_char| {
        let tag = match tag_char {
            'x' => GeoTagType::Discard,
            'r' => GeoTagType::Read,
            _ => unreachable!(),
        };
        GeoPart {
            tag,
            len: GeoLen::Unbounded,
            sequence: None,
            tolerance: None,
        }
    })
}

fn dist_func_parser<'a>() -> impl Parser<'a, &'a str, GeoPart, extra::Err<Rich<'a, char>>> {
    let num = text::int(10).map(|s: &str| s.parse::<u8>().unwrap());

    // hamming(f[SEQ], N) or hamming(tag_spec, N)
    let hamming = just("hamming")
        .ignore_then(
            just('(')
                .ignore_then(tag_spec_parser())
                .then_ignore(just(','))
                .then_ignore(just(' ').or_not())
                .then(num)
                .then_ignore(just(')')),
        )
        .map(|(mut part, max_dist)| {
            part.tolerance = Some(MatchTolerance {
                kind: DistanceKind::Hamming,
                max_dist,
            });
            part
        });

    // Future: levenshtein(tag_spec, N)
    // let levenshtein = ...

    hamming
}

// ── Validation ──────────────────────────────────────────────────────

/// Validate a parsed geometry for semantic correctness.
pub fn validate_geometry(geom: &FragmentGeom) -> Result<(), String> {
    let all_parts: Vec<&GeoPart> = geom
        .read1
        .parts
        .iter()
        .chain(geom.read2.parts.iter())
        .collect();

    // Must have at least one barcode
    let has_barcode = all_parts.iter().any(|p| {
        matches!(
            p.tag,
            GeoTagType::Barcode | GeoTagType::SampleBarcode | GeoTagType::NumberedBarcode(_)
        )
    });
    if !has_barcode {
        return Err("geometry must contain at least one barcode tag (b, s, or bN)".into());
    }

    // Must have at least one UMI
    let has_umi = all_parts.iter().any(|p| matches!(p.tag, GeoTagType::Umi));
    if !has_umi {
        return Err("geometry must contain at least one UMI tag (u)".into());
    }

    // Must have at least one read
    let has_read = all_parts.iter().any(|p| matches!(p.tag, GeoTagType::Read));
    if !has_read {
        return Err("geometry must contain at least one biological read tag (r)".into());
    }

    // Validate range widths
    for part in &all_parts {
        if let GeoLen::Range(min, max) = &part.len {
            if min > max {
                return Err(format!(
                    "range minimum {} exceeds maximum {} in {:?} tag",
                    min, max, part.tag
                ));
            }
            if max - min > MAX_RANGE_WIDTH {
                return Err(format!(
                    "range width {} exceeds maximum {} in {:?} tag",
                    max - min,
                    MAX_RANGE_WIDTH,
                    part.tag
                ));
            }
        }
    }

    for read in [&geom.read1, &geom.read2] {
        match classify_read_complexity(read) {
            GeometryComplexity::FixedOffsets => {}
            GeometryComplexity::InferableVariable => validate_inferable_read(read)?,
            GeometryComplexity::BoundaryResolved => validate_boundary_resolved_read(read)?,
        }
    }

    Ok(())
}

/// Classify the executor complexity tier required for a geometry.
///
/// The classifier chooses the most complex tier required by either read:
/// - [`GeometryComplexity::FixedOffsets`] for fully static layouts such as
///   `1{b[16]u[12]x:}2{r:}`
/// - [`GeometryComplexity::InferableVariable`] for a single right-bounded
///   variable-width region such as `1{b[9-10]f[ACGT]u[12]}2{r:}`
/// - [`GeometryComplexity::BoundaryResolved`] for geometries with interior
///   unbounded segments such as `1{r:f[ACAGT]b[9-11]}2{u[12]x:}`
///
/// This is the same tier selection used by [`crate::extract::CompiledGeom`].
pub fn geometry_complexity(geom: &FragmentGeom) -> GeometryComplexity {
    classify_read_complexity(&geom.read1).max(classify_read_complexity(&geom.read2))
}

fn classify_read_complexity(read: &ReadGeom) -> GeometryComplexity {
    let has_non_terminal_unbounded = read
        .parts
        .iter()
        .enumerate()
        .any(|(i, part)| matches!(part.len, GeoLen::Unbounded) && i + 1 < read.parts.len());
    if has_non_terminal_unbounded {
        return GeometryComplexity::BoundaryResolved;
    }

    let has_variable = read
        .parts
        .iter()
        .any(|part| matches!(part.len, GeoLen::Range(_, _)));
    if has_variable {
        GeometryComplexity::InferableVariable
    } else {
        GeometryComplexity::FixedOffsets
    }
}

fn validate_inferable_read(read: &ReadGeom) -> Result<(), String> {
    let Some(first_range_idx) = read
        .parts
        .iter()
        .position(|part| matches!(part.len, GeoLen::Range(_, _)))
    else {
        return Ok(());
    };

    let Some(anchor_rel_idx) = read.parts[(first_range_idx + 1)..]
        .iter()
        .position(|part| matches!(part.tag, GeoTagType::Fixed))
    else {
        return Err(
            "variable-length fields must be followed by a fixed anchor (f[SEQ]) so their boundaries can be inferred"
                .into(),
        );
    };

    let anchor_idx = first_range_idx + 1 + anchor_rel_idx;
    let variable_count = read.parts[first_range_idx..anchor_idx]
        .iter()
        .filter(|part| matches!(part.len, GeoLen::Range(_, _)))
        .count();
    if variable_count > 1 {
        return Err(
            "at most one variable-length field before a fixed anchor is currently supported".into(),
        );
    }

    if read.parts[(anchor_idx + 1)..]
        .iter()
        .any(|part| matches!(part.len, GeoLen::Range(_, _)))
    {
        return Err(
            "multiple variable-length anchored regions in the same read are not yet supported"
                .into(),
        );
    }

    Ok(())
}

fn validate_boundary_resolved_read(read: &ReadGeom) -> Result<(), String> {
    let mut segment_start = 0usize;
    let mut saw_anchor = false;
    for (idx, part) in read.parts.iter().enumerate() {
        if matches!(part.tag, GeoTagType::Fixed) {
            validate_boundary_segment(&read.parts[segment_start..idx])?;
            segment_start = idx + 1;
            saw_anchor = true;
        }
    }

    validate_boundary_segment(&read.parts[segment_start..])?;

    let has_non_terminal_unbounded = read
        .parts
        .iter()
        .enumerate()
        .any(|(i, part)| matches!(part.len, GeoLen::Unbounded) && i + 1 < read.parts.len());
    if has_non_terminal_unbounded && !saw_anchor {
        return Err(
            "non-terminal unbounded fields require at least one fixed anchor (f[SEQ]) to resolve boundaries"
                .into(),
        );
    }

    Ok(())
}

fn validate_boundary_segment(parts: &[GeoPart]) -> Result<(), String> {
    let flexible_count = parts
        .iter()
        .filter(|part| matches!(part.len, GeoLen::Range(_, _) | GeoLen::Unbounded))
        .count();
    if flexible_count > 1 {
        return Err(
            "at most one variable-width or unbounded field is supported within each boundary-resolved segment"
                .into(),
        );
    }

    Ok(())
}

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

    #[test]
    fn parse_chromium_v3() {
        let geom = parse_geometry("1{b[16]u[12]x:}2{r:}").unwrap();
        assert_eq!(geom.read1.parts.len(), 3);
        assert_eq!(geom.read1.parts[0].tag, GeoTagType::Barcode);
        assert_eq!(geom.read1.parts[0].len, GeoLen::Fixed(16));
        assert_eq!(geom.read1.parts[1].tag, GeoTagType::Umi);
        assert_eq!(geom.read1.parts[1].len, GeoLen::Fixed(12));
        assert_eq!(geom.read1.parts[2].tag, GeoTagType::Discard);
        assert_eq!(geom.read1.parts[2].len, GeoLen::Unbounded);
        assert_eq!(geom.read2.parts.len(), 1);
        assert_eq!(geom.read2.parts[0].tag, GeoTagType::Read);
        assert_eq!(geom.read2.parts[0].len, GeoLen::Unbounded);
    }

    #[test]
    fn parse_flex_v1() {
        let geom = parse_geometry("1{b[16]u[12]x:}2{r[50]x[18]s[8]x:}").unwrap();
        assert_eq!(geom.read2.parts.len(), 4);
        assert_eq!(geom.read2.parts[0].tag, GeoTagType::Read);
        assert_eq!(geom.read2.parts[0].len, GeoLen::Fixed(50));
        assert_eq!(geom.read2.parts[2].tag, GeoTagType::SampleBarcode);
        assert_eq!(geom.read2.parts[2].len, GeoLen::Fixed(8));
    }

    #[test]
    fn parse_flex_v2_with_anchor() {
        let geom = parse_geometry("1{b[16]u[12]x[0-3]f[TTGCTAGGACCG]s[10]x:}2{r:}").unwrap();
        assert_eq!(geom.read1.parts.len(), 6);
        assert_eq!(geom.read1.parts[2].tag, GeoTagType::Discard);
        assert_eq!(geom.read1.parts[2].len, GeoLen::Range(0, 3));
        assert_eq!(geom.read1.parts[3].tag, GeoTagType::Fixed);
        assert_eq!(geom.read1.parts[3].sequence, Some(b"TTGCTAGGACCG".to_vec()));
        assert_eq!(geom.read1.parts[4].tag, GeoTagType::SampleBarcode);
        assert_eq!(geom.read1.parts[4].len, GeoLen::Fixed(10));
    }

    #[test]
    fn parse_hamming_distance() {
        let geom =
            parse_geometry("1{b[16]u[12]x[0-3]hamming(f[TTGCTAGGACCG],1)s[10]x:}2{r:}").unwrap();
        let anchor = &geom.read1.parts[3];
        assert_eq!(anchor.tag, GeoTagType::Fixed);
        assert_eq!(
            anchor.tolerance,
            Some(MatchTolerance {
                kind: DistanceKind::Hamming,
                max_dist: 1,
            })
        );
    }

    #[test]
    fn parse_numbered_barcodes() {
        let geom = parse_geometry("1{b0[8]b1[16]u[12]x:}2{r:}").unwrap();
        assert_eq!(geom.read1.parts[0].tag, GeoTagType::NumberedBarcode(0));
        assert_eq!(geom.read1.parts[0].len, GeoLen::Fixed(8));
        assert_eq!(geom.read1.parts[1].tag, GeoTagType::NumberedBarcode(1));
        assert_eq!(geom.read1.parts[1].len, GeoLen::Fixed(16));
    }

    #[test]
    fn parse_fixed_sequence() {
        let geom = parse_geometry("1{b[16]u[12]f[ACGT]r:}2{r:}").unwrap();
        assert_eq!(geom.read1.parts[2].tag, GeoTagType::Fixed);
        assert_eq!(geom.read1.parts[2].sequence, Some(b"ACGT".to_vec()));
        assert_eq!(geom.read1.parts[2].len, GeoLen::Fixed(4));
    }

    #[test]
    fn parse_boundary_resolved_unbounded() {
        let geom = parse_geometry("1{r:f[ACAGT]b[9-11]}2{u[12]x:}").unwrap();
        assert_eq!(geom.read1.parts.len(), 3);
        assert_eq!(geom.read1.parts[0].len, GeoLen::Unbounded);
        assert_eq!(geom.read1.parts[1].tag, GeoTagType::Fixed);
        assert_eq!(geom.read1.parts[2].len, GeoLen::Range(9, 11));
    }

    #[test]
    fn validate_missing_barcode() {
        let geom = parse_geometry("1{u[12]x:}2{r:}").unwrap();
        assert!(validate_geometry(&geom).is_err());
    }

    #[test]
    fn validate_missing_umi() {
        let geom = parse_geometry("1{b[16]x:}2{r:}").unwrap();
        assert!(validate_geometry(&geom).is_err());
    }

    #[test]
    fn validate_range_without_anchor() {
        let geom = parse_geometry("1{b[16]u[12]x[0-3]s[10]x:}2{r:}").unwrap();
        let err = validate_geometry(&geom);
        assert!(err.is_err());
        assert!(err
            .unwrap_err()
            .contains("must be followed by a fixed anchor"));
    }

    #[test]
    fn validate_range_too_wide() {
        let geom = parse_geometry("1{b[16]u[12]x[0-5]f[TTGCTAGGACCG]s[10]x:}2{r:}").unwrap();
        let err = validate_geometry(&geom);
        assert!(err.is_err());
        assert!(err.unwrap_err().contains("exceeds maximum"));
    }

    #[test]
    fn validate_variable_length_barcode_with_anchor() {
        let geom = parse_geometry("1{b[9-10]u[12]f[ACGT]x:}2{r:}").unwrap();
        assert!(validate_geometry(&geom).is_ok());
    }

    #[test]
    fn validate_variable_length_barcode_without_anchor_rejected() {
        let geom = parse_geometry("1{b[9-10]u[12]x:}2{r:}").unwrap();
        let err = validate_geometry(&geom);
        assert!(err.is_err());
        assert!(err
            .unwrap_err()
            .contains("must be followed by a fixed anchor"));
    }

    #[test]
    fn validate_boundary_segment_with_two_flexible_fields_rejected() {
        let geom = parse_geometry("1{r:b[9-11]f[ACAGT]}2{u[12]x:}").unwrap();
        let err = validate_geometry(&geom);
        assert!(err.is_err());
        assert!(err
            .unwrap_err()
            .contains("at most one variable-width or unbounded field"));
    }

    #[test]
    fn error_message_quality() {
        let result = parse_geometry("1{b[16]u[12]z:}2{r:}");
        assert!(result.is_err());
        let errors = result.unwrap_err();
        let msg = format_errors("1{b[16]u[12]z:}2{r:}", &errors);
        // The error message should contain some indication of the problem
        assert!(!msg.is_empty());
        // It should reference the error position (the 'z' character)
        assert!(msg.contains("Error"));
    }

    #[test]
    fn parse_hamming_with_space() {
        // Allow optional space after comma
        let geom =
            parse_geometry("1{b[16]u[12]x[0-3]hamming(f[TTGCTAGGACCG], 1)s[10]x:}2{r:}").unwrap();
        let anchor = &geom.read1.parts[3];
        assert_eq!(
            anchor.tolerance,
            Some(MatchTolerance {
                kind: DistanceKind::Hamming,
                max_dist: 1,
            })
        );
    }

    #[test]
    fn classify_fixed_vs_variable_complexity() {
        let fixed = parse_geometry("1{b[16]u[12]x:}2{r:}").unwrap();
        assert_eq!(
            geometry_complexity(&fixed),
            GeometryComplexity::FixedOffsets
        );

        let variable = parse_geometry("1{b[9-10]u[12]f[ACGT]x:}2{r:}").unwrap();
        assert_eq!(
            geometry_complexity(&variable),
            GeometryComplexity::InferableVariable
        );

        let boundary = parse_geometry("1{r:f[ACAGT]b[9-11]}2{u[12]x:}").unwrap();
        assert_eq!(
            geometry_complexity(&boundary),
            GeometryComplexity::BoundaryResolved
        );
    }
}