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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! This is a crate for parsing and interpreting sequence fragment
//! geometry specifications in the sequence
//! [fragment geometry description language](https://hackmd.io/@PI7Og0l1ReeBZu_pjQGUQQ/rJMgmvr13) (FGDL).
//! The FGDL describes how sequenced fragments are laid out, and how different parts of the sequence correspond
//! to technical tags or to biological sequence.  It provides a standard and unified way to represent
//! the sequence layouts used in many different sequencing protocols, and is currently developed with
//! a focus on representing different single-cell sequencing chemistries.
//!
//! This crate provides a library for parsing these descriptions, and a set of structures for representing
//! them in memory, as well as some common traits for transforming and printing them.

extern crate pest;
#[macro_use]
extern crate pest_derive;

use anyhow::{anyhow, bail, Result};
use pest::Parser;

use std::convert::TryFrom;
use std::fmt;

#[derive(Parser)]
#[grammar = "grammar/frag_geom.pest"] // relative to src
pub struct FragGeomParser;

/// The types of lengths that a piece of
/// geometry can have.
#[derive(Debug, Copy, Clone)]
pub enum GeomLen {
    /// This piece of geometry has a single fixed length
    FixedLen(u32),
    /// This piece of geometry has some length between
    /// a provided lower and upper bound
    LenRange(u32, u32),
    /// This piece of geometry has a length whose bound is
    /// not known at geometry specification time
    Unbounded,
}

/// Represents the sequence held by a fixed
/// sequence anchor.
#[derive(Debug, Clone)]
pub enum NucStr {
    Seq(String),
}

/// The pieces of geometry (types) we
/// currently support.
#[derive(Debug, Clone)]
pub enum GeomPiece {
    /// A cellular barcode
    Barcode(GeomLen),
    /// A unique molecular identifier
    Umi(GeomLen),
    /// Sequence that will be discarded
    Discard(GeomLen),
    /// Biological read sequence
    ReadSeq(GeomLen),
    /// A fixed sequence anchor / motif
    Fixed(NucStr),
}

impl fmt::Display for GeomPiece {
    /// Formats and returns the canonical string representation of each type of
    /// `GeomPiece`.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self {
            GeomPiece::Umi(GeomLen::Unbounded) => write!(f, "u:"),
            GeomPiece::Barcode(GeomLen::Unbounded) => write!(f, "b:"),
            GeomPiece::ReadSeq(GeomLen::Unbounded) => write!(f, "r:"),
            GeomPiece::Discard(GeomLen::Unbounded) => write!(f, "x:"),
            GeomPiece::Umi(GeomLen::FixedLen(x)) => write!(f, "u[{}]", x),
            GeomPiece::Barcode(GeomLen::FixedLen(x)) => write!(f, "b[{}]", x),
            GeomPiece::ReadSeq(GeomLen::FixedLen(x)) => write!(f, "r[{}]", x),
            GeomPiece::Discard(GeomLen::FixedLen(x)) => write!(f, "x[{}]", x),
            GeomPiece::Umi(GeomLen::LenRange(l, h)) => write!(f, "u[{}-{}]", l, h),
            GeomPiece::Barcode(GeomLen::LenRange(l, h)) => write!(f, "b[{}-{}]", l, h),
            GeomPiece::ReadSeq(GeomLen::LenRange(l, h)) => write!(f, "r[{}-{}]", l, h),
            GeomPiece::Discard(GeomLen::LenRange(l, h)) => write!(f, "x[{}-{}]", l, h),
            GeomPiece::Fixed(NucStr::Seq(s)) => write!(f, "f[{}]", s),
        }
    }
}

impl GeomPiece {
    /// This method returns true if the current GeomPiece has a fixed length
    /// (either FixedLen or a Fixed(NucStr)), and false otherwise.
    pub fn is_fixed_len(&self) -> bool {
        matches!(
            self,
            GeomPiece::Umi(GeomLen::FixedLen(_))
                | GeomPiece::Barcode(GeomLen::FixedLen(_))
                | GeomPiece::ReadSeq(GeomLen::FixedLen(_))
                | GeomPiece::Discard(GeomLen::FixedLen(_))
                | GeomPiece::Fixed(NucStr::Seq(_))
        )
    }

    /// This method returns true if the current GeomPiece has a bounded length
    /// (either Bounded, BoundedRange, or a Fixed(NucStr)), and false otherwise.
    pub fn is_bounded(&self) -> bool {
        !matches!(
            self,
            GeomPiece::Umi(GeomLen::Unbounded)
                | GeomPiece::Barcode(GeomLen::Unbounded)
                | GeomPiece::ReadSeq(GeomLen::Unbounded)
                | GeomPiece::Discard(GeomLen::Unbounded)
        )
    }

    /// This method returns true if the current GeomPiece is "complex"
    /// (either BoundedRange, or a Fixed(NucStr)), and false otherwise.
    pub fn is_complex(&self) -> bool {
        matches!(
            self,
            GeomPiece::Fixed(NucStr::Seq(_))
                | GeomPiece::Umi(GeomLen::LenRange(_, _))
                | GeomPiece::Barcode(GeomLen::LenRange(_, _))
                | GeomPiece::ReadSeq(GeomLen::LenRange(_, _))
                | GeomPiece::Discard(GeomLen::LenRange(_, _))
        )
    }
}

// functions for parsing the different types of geometry elements

/// Parses a string "x" (assumed to be parsable as a `u32`) into an
/// integer x and returns x.
fn parse_fixed_len_as_u32(r: &mut pest::iterators::Pairs<Rule>) -> u32 {
    let rn = r.next().unwrap();
    match rn.as_rule() {
        Rule::single_len => {
            return rn.as_str().parse::<u32>().unwrap();
        }
        r => unimplemented!("Expected rule 'single_len', but found {:?}", r),
    }
}

/// Parses a string "x" (assumed to be parsable as a `u32`) into an
/// integer x and returns `GeomLen::FixedLen(x)`.
fn parse_fixed_len(r: &mut pest::iterators::Pairs<Rule>) -> GeomLen {
    GeomLen::FixedLen(parse_fixed_len_as_u32(r))
}

/// Parses a range of the format, "l-h" (where "l" and "h" assumed to be parsable as a `u32`)
/// and returns `GeomLen::LenRange(l, h)`.
fn parse_ranged_len(r: &mut pest::iterators::Pairs<Rule>) -> GeomLen {
    let rn = r.next().unwrap();
    match rn.as_rule() {
        Rule::len_range => {
            let mut ri = rn.into_inner();
            let l = parse_fixed_len_as_u32(&mut ri);
            let h = parse_fixed_len_as_u32(&mut ri);
            GeomLen::LenRange(l, h)
        }
        r => unimplemented!("expected rule 'len_range' but found {:?}", r),
    }
}

/// Parses a fixed nucleotide sequence s (matching "[ACGT]+") and returns
/// `NucStr::Seq(s)`.
fn parse_fixed_seq(r: &mut pest::iterators::Pairs<Rule>) -> NucStr {
    let rn = r.next().unwrap();
    match rn.as_rule() {
        Rule::nucstr => {
            let seq_str = rn.as_str();
            NucStr::Seq(seq_str.to_owned())
        }
        r => unimplemented!("expected rule 'nucstr' but found {:?}", r),
    }
}

/// Parses a `GeomPiece` that represents a "ranged segment", that is a
/// barcode, umi, read string, or discard segment having a ranged length.
fn parse_ranged_segment(r: pest::iterators::Pair<Rule>) -> GeomPiece {
    match r.as_rule() {
        Rule::ranged_umi_segment => {
            let gl = parse_ranged_len(&mut r.into_inner());
            GeomPiece::Umi(gl)
        }
        Rule::ranged_barcode_segment => {
            let gl = parse_ranged_len(&mut r.into_inner());
            GeomPiece::Barcode(gl)
        }
        Rule::ranged_discard_segment => {
            let gl = parse_ranged_len(&mut r.into_inner());
            GeomPiece::Discard(gl)
        }
        Rule::ranged_read_segment => {
            let gl = parse_ranged_len(&mut r.into_inner());
            GeomPiece::ReadSeq(gl)
        }
        _ => unimplemented!(),
    }
}

/// Parses a `GeomPiece` that represents a "ranged segment", that is a
/// barcode, umi, read string, discard segment, or fixed seq segment having a fixed length.
fn parse_fixed_segment(r: pest::iterators::Pair<Rule>) -> GeomPiece {
    match r.as_rule() {
        Rule::fixed_umi_segment => {
            let gl = parse_fixed_len(&mut r.into_inner());
            GeomPiece::Umi(gl)
        }
        Rule::fixed_barcode_segment => {
            let gl = parse_fixed_len(&mut r.into_inner());
            GeomPiece::Barcode(gl)
        }
        Rule::fixed_discard_segment => {
            let gl = parse_fixed_len(&mut r.into_inner());
            GeomPiece::Discard(gl)
        }
        Rule::fixed_read_segment => {
            let gl = parse_fixed_len(&mut r.into_inner());
            GeomPiece::ReadSeq(gl)
        }
        Rule::fixed_seq_segment => {
            let fseq = parse_fixed_seq(&mut r.into_inner());
            GeomPiece::Fixed(fseq)
        }
        _ => unimplemented!(),
    }
}

/// Parses a `GeomPiece` that represents an "unbounded segment", that is a
/// barcode, umi, read string, or discard segment that is not of fixed length
/// (i.e. that has length >=1).
fn parse_unbounded_segment(r: pest::iterators::Pair<Rule>) -> GeomPiece {
    match r.as_rule() {
        Rule::unbounded_umi_segment => GeomPiece::Umi(GeomLen::Unbounded),
        Rule::unbounded_barcode_segment => GeomPiece::Barcode(GeomLen::Unbounded),
        Rule::unbounded_discard_segment => GeomPiece::Discard(GeomLen::Unbounded),
        Rule::unbounded_read_segment => GeomPiece::ReadSeq(GeomLen::Unbounded),
        _ => unimplemented!(),
    }
}

/// Parses any type of geometry segment.  According to the grammer, this will be either
/// a fixed_segment, fixed_seq_segment, ranged_segment, or unbounded_segment. This function
/// is the top-level parser for individual "pieces" of geometry, and returns the corresponding
/// `GeomPiece`.
pub fn parse_segment(r: pest::iterators::Pair<Rule>) -> GeomPiece {
    match r.as_rule() {
        Rule::fixed_segment => parse_fixed_segment(r.into_inner().next().unwrap()),
        Rule::fixed_seq_segment => {
            let fseq = parse_fixed_seq(&mut r.into_inner());
            GeomPiece::Fixed(fseq)
        }
        Rule::ranged_segment => parse_ranged_segment(r.into_inner().next().unwrap()),
        Rule::unbounded_segment => parse_unbounded_segment(r.into_inner().next().unwrap()),
        _ => unimplemented!(),
    }
}

/// This trait says that a given implementor is able to properly add itself
/// to the command represented by `cmd`.
pub trait AppendToCmdArgs {
    fn append(&self, cmd: &mut std::process::Command);
}

// ======== for piscem

/// This struct holds a [`piscem`](https://github.com/COMBINE-lab/piscem) compatible
/// description of the fragment geometry specification.
#[derive(Debug, Eq, PartialEq)]
pub struct PiscemGeomDesc {
    /// The `piscem` format specification for read 1.
    pub read1_desc: String,
    /// The `piscem` format specification for read 2.
    pub read2_desc: String,
}

impl AppendToCmdArgs for PiscemGeomDesc {
    /// Adds this `piscem` format geometry specification to the command
    /// given by `cmd`.
    fn append(&self, cmd: &mut std::process::Command) {
        let geo_desc = format!("1{}2{}", self.read1_desc, self.read2_desc);
        cmd.args(["--geometry", geo_desc.as_str()]);
    }
}

fn as_piscem_geom_desc_single_read(geom_pieces: &[GeomPiece]) -> String {
    let desc = geom_pieces
        .iter()
        .map(|x| format!("{}", x))
        .collect::<Vec<String>>()
        .join("");
    format!("{{{}}}", desc)
}

impl PiscemGeomDesc {
    /// This constructor builds the `piscem` format descriptor for this fragment
    /// library from a slice of the constituent `GeomPiece`s for read 1 (`geom_pieces_r1`)
    /// and a slice of the `GeomPiece`s for read 2 (`geom_pieces_r2`).
    pub fn from_geom_pieces(geom_pieces_r1: &[GeomPiece], geom_pieces_r2: &[GeomPiece]) -> Self {
        let read1_desc = as_piscem_geom_desc_single_read(geom_pieces_r1);
        let read2_desc = as_piscem_geom_desc_single_read(geom_pieces_r2);
        Self {
            read1_desc,
            read2_desc,
        }
    }
}

// ======== for salmon

/// This struct holds a [`salmon`](https://github.com/COMBINE-lab/salmon) compatible
/// description of the fragment geometry specification.
#[derive(Debug, Eq, PartialEq)]
pub struct SalmonSeparateGeomDesc {
    pub barcode_desc: String,
    pub umi_desc: String,
    pub read_desc: String,
}

impl AppendToCmdArgs for SalmonSeparateGeomDesc {
    /// Given the `salmon` compatible geometry description, append this description
    /// to the command `cmd`, assumed to be an invocation of `salmon alevin`.
    fn append(&self, cmd: &mut std::process::Command) {
        cmd.args([
            "--read-geometry",
            self.read_desc.as_str(),
            "--bc-geometry",
            self.barcode_desc.as_str(),
            "--umi-geometry",
            self.umi_desc.as_str(),
        ]);
    }
}

// for the "separate" salmon format, we need to collect
// the intervals corresponding to each part of the geometry
// separately.  So we need to keep track of intervals which
// is just a pair of offsets.

// the offset can be bounded, or unbounded
// (i.e. goes until the end of the current read)
enum GeomOffset {
    Bounded(u32),
    Unbounded,
}

// an interval is just a pair of offsets
struct GeomInterval {
    start: GeomOffset,
    end: GeomOffset,
}

/// to be able to render a GeomInterval as a string. This
/// is basically just rendering "x-y", for offsets x and y, but
/// if y is unbounded, we render "x-end" instead.
impl fmt::Display for GeomInterval {
    // This trait requires `fmt` with this exact signature.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Write strictly the first element into the supplied output
        // stream: `f`. Returns `fmt::Result` which indicates whether the
        // operation succeeded or failed. Note that `write!` uses syntax which
        // is very similar to `println!`.
        let s = match self.start {
            GeomOffset::Bounded(x) => format!("{}", x),
            _ => "XXX".to_string(),
        };
        let e = match self.end {
            GeomOffset::Bounded(x) => format!("{}", x),
            GeomOffset::Unbounded => "end".to_string(),
        };
        write!(f, "{}-{}", s, e)
    }
}

/// should return struct or enum instead
fn as_salmon_desc_separate_helper(geom_pieces: &[GeomPiece]) -> (String, String, String) {
    let mut offset = 0_u32;

    let mut bc_intervals = Vec::<GeomInterval>::new();
    let mut umi_intervals = Vec::<GeomInterval>::new();
    let mut read_intervals = Vec::<GeomInterval>::new();

    let append_interval_bounded = |offset: &mut u32, x: u32, intervals: &mut Vec<GeomInterval>| {
        let start = *offset + 1;
        let end = *offset + x;
        intervals.push(GeomInterval {
            start: GeomOffset::Bounded(start),
            end: GeomOffset::Bounded(end),
        });
        *offset += x;
    };

    let append_interval_unbounded = |offset: &mut u32, intervals: &mut Vec<GeomInterval>| {
        let start = *offset + 1;
        intervals.push(GeomInterval {
            start: GeomOffset::Bounded(start),
            end: GeomOffset::Unbounded,
        });
    };

    for gp in geom_pieces {
        match gp {
            GeomPiece::Barcode(GeomLen::FixedLen(x)) => {
                append_interval_bounded(&mut offset, *x, &mut bc_intervals);
            }
            GeomPiece::Umi(GeomLen::FixedLen(x)) => {
                append_interval_bounded(&mut offset, *x, &mut umi_intervals);
            }
            GeomPiece::ReadSeq(GeomLen::FixedLen(x)) => {
                append_interval_bounded(&mut offset, *x, &mut read_intervals);
            }
            GeomPiece::Discard(GeomLen::FixedLen(x)) => {
                offset += x;
            }
            GeomPiece::Fixed(NucStr::Seq(_s)) => {
                unimplemented!("Fixed content nucleotide tags are not supported in the salmon separate description format");
            }
            GeomPiece::Barcode(GeomLen::Unbounded) => {
                append_interval_unbounded(&mut offset, &mut bc_intervals);
            }
            GeomPiece::Umi(GeomLen::Unbounded) => {
                append_interval_unbounded(&mut offset, &mut umi_intervals);
            }
            GeomPiece::ReadSeq(GeomLen::Unbounded) => {
                append_interval_unbounded(&mut offset, &mut read_intervals);
            }
            GeomPiece::Discard(GeomLen::Unbounded) => {}
            r => unimplemented!("encountered unexpected GeomPiece {:?}", r),
        };
    }

    let bc_str = bc_intervals
        .iter()
        .map(|x| format!("{}", x))
        .collect::<Vec<String>>()
        .join(",");

    let umi_str = umi_intervals
        .iter()
        .map(|x| format!("{}", x))
        .collect::<Vec<String>>()
        .join(",");

    let read_str = read_intervals
        .iter()
        .map(|x| format!("{}", x))
        .collect::<Vec<String>>()
        .join(",");
    (
        format!("[{}]", bc_str),
        format!("[{}]", umi_str),
        format!("[{}]", read_str),
    )
}

impl SalmonSeparateGeomDesc {
    pub fn from_geom_pieces(geom_pieces_r1: &[GeomPiece], geom_pieces_r2: &[GeomPiece]) -> Self {
        let mut barcode_rep = String::new();
        let mut umi_rep = String::new();
        let mut read_rep = String::new();
        let (bcp, up, rp) = as_salmon_desc_separate_helper(geom_pieces_r1);
        if bcp != "[]" {
            barcode_rep += &format!("1{}", bcp);
        }
        if up != "[]" {
            umi_rep += &format!("1{}", up);
        }
        if rp != "[]" {
            read_rep += &format!("1{}", rp);
        }

        let (bcp, up, rp) = as_salmon_desc_separate_helper(geom_pieces_r2);
        if bcp != "[]" {
            barcode_rep += &format!("2{}", bcp);
        }
        if up != "[]" {
            umi_rep += &format!("2{}", up);
        }
        if rp != "[]" {
            read_rep += &format!("2{}", rp);
        }

        Self {
            barcode_desc: barcode_rep,
            umi_desc: umi_rep,
            read_desc: read_rep,
        }
    }
}

/// This structure holds our representation of the parsed fragment
/// geometry description.
#[derive(Debug)]
pub struct FragmentGeomDesc {
    /// The sequence of `GeomPiece`s describing read 1 of this fragment in left-to-right order.
    pub read1_desc: Vec<GeomPiece>,
    /// The sequence of `GeomPiece`s describing read 2 of this fragment in left-to-right order.
    pub read2_desc: Vec<GeomPiece>,
}

impl fmt::Display for FragmentGeomDesc {
    /// Write back a geometry fragment specification as exactly
    /// the type of string the parser should accept in the first place.
    /// This is the canonical representation of the geometry.
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let desc1 = self
            .read1_desc
            .iter()
            .map(|x| format!("{}", x))
            .collect::<Vec<String>>()
            .join("");
        let desc2 = self
            .read2_desc
            .iter()
            .map(|x| format!("{}", x))
            .collect::<Vec<String>>()
            .join("");
        write!(f, "1{{{}}}2{{{}}}", desc1, desc2)
    }
}

impl FragmentGeomDesc {
    /// A "complex" geometry is one that contains
    /// a FixedSeq piece, and/or a BoundedRange piece
    pub fn is_complex_geometry(&self) -> bool {
        for gp in self.read1_desc.iter().chain(self.read2_desc.iter()) {
            if gp.is_complex() {
                return true;
            }
        }
        false
    }

    /// A "simple" geometry is one that contains only fixed length pieces
    /// (but doesn't include any fixed seq segments) and unbounded pieces.
    pub fn is_simple_geometry(&self) -> bool {
        !self.is_complex_geometry()
    }
}

/// Parse the description of a single read.  It's expected that this function is called
/// when the enclosing rule matches a rule description.  In that case, this function is
/// called with the `into_inner` of that Pair.  This function returns a vector containing
/// the parsed geometry of the input description.
fn parse_read_description(read_desc: pest::iterators::Pairs<Rule>) -> Vec<GeomPiece> {
    let mut read_geom = Vec::<GeomPiece>::new();
    for rd in read_desc {
        match rd.as_rule() {
            Rule::read_desc => {
                for geom_piece in rd.into_inner() {
                    read_geom.push(parse_segment(geom_piece));
                }
            }
            _ => unreachable!(),
        };
    }
    read_geom
}

impl<'a> TryFrom<&'a str> for FragmentGeomDesc {
    type Error = anyhow::Error;

    /// This is the main entry point to obtain a `FragmentGeomDesc` structure.
    /// This function parses the FGDL description string provided as `arg`, and
    /// returns either `Ok(FragGeomDesc)`, if the parse is succesful or an
    /// `anyhow::Error` if the parsing fails.
    ///
    /// Currently, the FGDL makes a structural assumption that is reflected in the
    /// way this function works.  The description string will describe the fragment
    /// geometry for a fragment consisting of a pair of reads (i.e. currently
    /// there is no support for single-end reads or fragments containing > 2 reads).
    fn try_from(arg: &'a str) -> Result<Self, Self::Error> {
        match FragGeomParser::parse(Rule::frag_desc, arg) {
            Ok(fragment_desc) => {
                // Where we'll hold the `GeomPiece`s that constitute the
                // parse of each read.
                let mut r1_desc = None;
                let mut r2_desc = None;

                // Because ident_list is silent, the iterator will contain idents
                for read_desc in fragment_desc {
                    match read_desc.as_rule() {
                        Rule::read_1_desc => {
                            let rd = read_desc.into_inner();
                            r1_desc = Some(parse_read_description(rd));
                        }
                        Rule::read_2_desc => {
                            let rd = read_desc.into_inner();
                            r2_desc = Some(parse_read_description(rd));
                        }
                        Rule::EOI => {}
                        e => {
                            dbg!("{:?}", e);
                            bail!("Expected to parse a description for read 1, or 2, but didn't find the corresponding rule!")
                        }
                    };
                }

                if let (Some(read1_desc), Some(read2_desc)) = (r1_desc, r2_desc) {
                    Ok(FragmentGeomDesc {
                        read1_desc,
                        read2_desc,
                    })
                } else {
                    bail!("Was not able to obtain a succesful parse for both read 1 and read 2.")
                }
            }
            Err(e) => Err(anyhow!(
                "Could not succesfully parse geometry description {}.\nParse Error : {:#?}",
                arg,
                e
            )),
        }
    }
}