nanalogue 0.1.9

BAM/Mod BAM parsing and analysis tool with a single-molecule focus
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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! `BaseModsToSeqCoordCalls` trait, converts mod data in the fibertools
//! `BaseMods` struct into essentially an array of length = sequence length where every
//! entry is a `Vec` of `u8` containing modification probability per mod.

use crate::{Error, ModChar};
use fibertools_rs::utils::basemods::BaseMods;

/// Holds mod calls for every position along a sequence
/// (irrespective of whether every position has an associated mod call).
///
/// * `mod_types` is a `Vec` of tuples of [`ModChar`], `bool`,
///   which mean modification type, and whether the calls are on the basecalled
///   strand or the opposite.
/// * `mod calls` is a `Vec` holding a `Vec` of mod calls per position,
///   and whose length equals length of the sequence.
#[derive(Debug)]
#[non_exhaustive]
pub struct SeqCoordCalls {
    /// Types of modification + strand combination (max 8)
    mod_types: Vec<(ModChar, bool)>,

    /// Mod calls per position
    mod_calls: Vec<Vec<u8>>,
}

impl SeqCoordCalls {
    /// Returns a boolean vector indicating whether any modification is present at each position.
    ///
    /// The returned vector has the same length as the DNA sequence, with `true` at positions
    /// where at least one modification type has a non-zero quality score, and `false` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// # use nanalogue_core::utils::basemods_to_seq_coord_calls::SeqCoordCalls;
    /// # use fibertools_rs::utils::basemods::{BaseMods, BaseMod};
    /// # use fibertools_rs::utils::bamannotations::{FiberAnnotation, Ranges};
    /// // Given BaseMods with T+ at position 0 (qual=100) and C+m at position 2 (qual=200)
    /// // for a sequence of length 5:
    /// let base_mods = BaseMods {
    ///     base_mods: vec![
    ///         BaseMod {
    ///             modified_base: b'T',
    ///             strand: '+',
    ///             modification_type: 'T',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 0, end: 1, length: 1, qual: 100,
    ///                     reference_start: Some(0), reference_end: Some(0),
    ///                     reference_length: Some(0), extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///         BaseMod {
    ///             modified_base: b'C',
    ///             strand: '+',
    ///             modification_type: 'm',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 2, end: 3, length: 1, qual: 200,
    ///                     reference_start: None, reference_end: None,
    ///                     reference_length: None, extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///     ],
    /// };
    ///
    /// let seq_coord_calls = SeqCoordCalls::try_from(&base_mods).unwrap();
    /// let collapsed = seq_coord_calls.collapse_mod_calls();
    ///
    /// // Returns [true, false, true, false, false]
    /// // Position 0: true (T+ modified)
    /// // Position 1: false (no modifications)
    /// // Position 2: true (C+m modified)
    /// // Positions 3-4: false (no modifications)
    /// assert_eq!(collapsed, vec![true, false, true, false, false]);
    /// ```
    #[must_use]
    pub fn collapse_mod_calls(&self) -> Vec<bool> {
        self.mod_calls
            .iter()
            .map(|k| k.iter().any(|x| *x > 0))
            .collect()
    }

    /// Returns the modification types and their strand information.
    ///
    /// Each entry in the returned vector is a tuple `(ModChar, bool)` where:
    /// - The first element is the modification type (e.g., 'T', 'm', 'h')
    /// - The second element indicates strand: `true` for '+' strand, `false` for '-' strand
    ///
    /// The order of mod types in this vector corresponds to the order of quality scores
    /// in the arrays returned by [`mod_calls()`](Self::mod_calls).
    ///
    /// # Example
    ///
    /// ```
    /// # use nanalogue_core::utils::basemods_to_seq_coord_calls::SeqCoordCalls;
    /// # use nanalogue_core::ModChar;
    /// # use fibertools_rs::utils::basemods::{BaseMods, BaseMod};
    /// # use fibertools_rs::utils::bamannotations::{FiberAnnotation, Ranges};
    /// // Given BaseMods with T+ and C+m modifications:
    /// let base_mods = BaseMods {
    ///     base_mods: vec![
    ///         BaseMod {
    ///             modified_base: b'T', strand: '+', modification_type: 'T',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 0, end: 1, length: 1, qual: 100,
    ///                     reference_start: Some(0), reference_end: Some(0),
    ///                     reference_length: Some(0), extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///         BaseMod {
    ///             modified_base: b'C', strand: '+', modification_type: 'm',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 2, end: 3, length: 1, qual: 200,
    ///                     reference_start: None, reference_end: None,
    ///                     reference_length: None, extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///     ],
    /// };
    ///
    /// let seq_coord_calls = SeqCoordCalls::try_from(&base_mods).unwrap();
    /// let mod_types = seq_coord_calls.mod_types();
    ///
    /// // Returns 2 mod types: T+ and C+m
    /// assert_eq!(mod_types.len(), 2);
    /// assert_eq!(mod_types[0].0, ModChar::new('T'));
    /// assert_eq!(mod_types[0].1, true);  // '+' strand
    /// assert_eq!(mod_types[1].0, ModChar::new('m'));
    /// assert_eq!(mod_types[1].1, true);  // '+' strand
    /// ```
    #[must_use]
    pub fn mod_types(&self) -> &Vec<(ModChar, bool)> {
        &self.mod_types
    }

    /// Returns modification quality scores at a given sequence position.
    ///
    /// Returns an array of `u8` quality scores, one for each modification type.
    /// The order corresponds to the modification types returned by [`mod_types()`](Self::mod_types).
    /// A value of `0` indicates no modification detected at that position for that type.
    ///
    /// Returns `None` if the position is out of bounds.
    ///
    /// # Arguments
    ///
    /// * `pos` - The sequence position (0-based index)
    ///
    /// # Example
    ///
    /// ```
    /// # use nanalogue_core::utils::basemods_to_seq_coord_calls::SeqCoordCalls;
    /// # use fibertools_rs::utils::basemods::{BaseMods, BaseMod};
    /// # use fibertools_rs::utils::bamannotations::{FiberAnnotation, Ranges};
    /// // Given BaseMods with T+ at position 0 (qual=100) and C+m at position 2 (qual=200):
    /// let base_mods = BaseMods {
    ///     base_mods: vec![
    ///         BaseMod {
    ///             modified_base: b'T', strand: '+', modification_type: 'T',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 0, end: 1, length: 1, qual: 100,
    ///                     reference_start: Some(0), reference_end: Some(0),
    ///                     reference_length: Some(0), extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///         BaseMod {
    ///             modified_base: b'C', strand: '+', modification_type: 'm',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 2, end: 3, length: 1, qual: 200,
    ///                     reference_start: None, reference_end: None,
    ///                     reference_length: None, extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///     ],
    /// };
    ///
    /// let seq_coord_calls = SeqCoordCalls::try_from(&base_mods).unwrap();
    ///
    /// // Position 0: T+ modified (100), C+m not modified (0)
    /// assert_eq!(seq_coord_calls.mod_calls(0), Some(&[100u8, 0u8][..]));
    ///
    /// // Position 1: Neither modification present
    /// assert_eq!(seq_coord_calls.mod_calls(1), Some(&[0u8, 0u8][..]));
    ///
    /// // Position 2: T+ not modified (0), C+m modified (200)
    /// assert_eq!(seq_coord_calls.mod_calls(2), Some(&[0u8, 200u8][..]));
    ///
    /// // Out of bounds returns None
    /// assert_eq!(seq_coord_calls.mod_calls(5), None);
    /// ```
    #[must_use]
    pub fn mod_calls(&self, pos: usize) -> Option<&[u8]> {
        self.mod_calls.get(pos).map(|v| &**v)
    }
}

impl TryFrom<&BaseMods> for SeqCoordCalls {
    type Error = Error;

    /// Converts `BaseMods` data into sequence-coordinate-indexed modification calls.
    ///
    /// Transforms modification data from the fibertools `BaseMods` format (which stores
    /// per-modification-type annotations with positions and quality scores) into a
    /// sequence-indexed format where each position has quality scores for all modification types.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The input `BaseMods` is empty (no sequence length can be determined)
    /// - Different `BaseMod` entries have inconsistent sequence lengths
    /// - Any strand character is not '+' or '-'
    /// - Any annotation is not per-base (e.g., multi-base ranges are not supported)
    /// - Position coordinates are out of bounds
    ///
    /// # Example
    ///
    /// ```
    /// # use nanalogue_core::utils::basemods_to_seq_coord_calls::SeqCoordCalls;
    /// # use fibertools_rs::utils::basemods::{BaseMods, BaseMod};
    /// # use fibertools_rs::utils::bamannotations::{FiberAnnotation, Ranges};
    /// // Create BaseMods with multiple modification types (T+ and C+m):
    /// let base_mods = BaseMods {
    ///     base_mods: vec![
    ///         BaseMod {
    ///             modified_base: b'T',
    ///             strand: '+',
    ///             modification_type: 'T',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 0, end: 1, length: 1, qual: 100,
    ///                     reference_start: Some(0), reference_end: Some(0),
    ///                     reference_length: Some(0), extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///         BaseMod {
    ///             modified_base: b'C',
    ///             strand: '+',
    ///             modification_type: 'm',
    ///             ranges: Ranges {
    ///                 annotations: vec![FiberAnnotation {
    ///                     start: 2, end: 3, length: 1, qual: 200,
    ///                     reference_start: None, reference_end: None,
    ///                     reference_length: None, extra_columns: None,
    ///                 }],
    ///                 seq_len: 5, reverse: false,
    ///             },
    ///             record_is_reverse: false,
    ///         },
    ///     ],
    /// };
    ///
    /// // Convert to sequence-indexed format
    /// let seq_coord_calls = SeqCoordCalls::try_from(&base_mods).unwrap();
    ///
    /// // Now we can access modification data by sequence position:
    /// // - Position 0: T+ has qual=100, C+m has qual=0
    /// // - Position 2: T+ has qual=0, C+m has qual=200
    /// // - Other positions: both have qual=0
    /// assert_eq!(seq_coord_calls.mod_calls(0), Some(&[100u8, 0u8][..]));
    /// assert_eq!(seq_coord_calls.mod_calls(2), Some(&[0u8, 200u8][..]));
    /// ```
    fn try_from(value: &BaseMods) -> Result<Self, Error> {
        let (seq_lengths, mod_type_collection, starts_qual) = value
            .base_mods
            .iter()
            .map(|x| {
                let seq_len = x.ranges.seq_len;
                Ok((
                    usize::try_from(seq_len)?,
                    (ModChar::new(x.modification_type),
                    match x.strand{
                        '+' => true,
                        '-' => false,
                        v => return Err(Error::InvalidState(format!("{v} is not a valid strand!"))),
                    }),
                    x.ranges
                        .annotations
                        .iter()
                        .map(|y| {
                            if (0..seq_len).contains(&y.start) && y.end.checked_sub(y.start) == Some(1) {
                                Ok((usize::try_from(y.start)?, y.qual))
                            } else {
                                Err(Error::NotImplemented(
                                    "cannot use a non-per-base annotation and/or malformed coordinates in `BaseMods`"
                                        .to_owned(),
                                ))
                            }
                        })
                        .collect::<Result<Vec<(usize, u8)>, Error>>()?,
                ))
            })
            .collect::<Result<(Vec<usize>, Vec<(ModChar, bool)>, Vec<Vec<(usize, u8)>>), Error>>(
            )?;

        let Some(seq_len) = seq_lengths.first() else {
            return Err(Error::UnavailableData(
                "In an empty `BaseMods`, we don't know sequence length, so we cannot process this"
                    .to_owned(),
            ));
        };

        if seq_lengths.iter().any(|x| *x != *seq_len) {
            return Err(Error::NotImplemented(
                "malformed `seq_len` in `BaseMods`".to_owned(),
            ));
        }

        let mut mod_list = vec![vec![0u8; seq_lengths.len()]; *seq_len];

        for (k, item) in starts_qual.into_iter().enumerate() {
            for l in item {
                *mod_list
                    .get_mut(l.0)
                    .expect("we've checked all pos < seq_len and all seq_len equal")
                    .get_mut(k)
                    .expect("we've set vector length to `seq_lengths.len()`") = l.1;
            }
        }

        Ok(SeqCoordCalls {
            mod_types: mod_type_collection,
            mod_calls: mod_list,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fibertools_rs::utils::bamannotations::{FiberAnnotation, Ranges};
    use fibertools_rs::utils::basemods::BaseMod;

    #[test]
    fn seq_coord_calls_from_basemods_example_1() -> Result<(), Error> {
        // Example from src/lib.rs nanalogue_mm_ml_parser docstring (count 0)
        let base_mods = BaseMods {
            base_mods: vec![BaseMod {
                modified_base: b'T',
                strand: '+',
                modification_type: 'T',
                ranges: Ranges {
                    annotations: vec![
                        FiberAnnotation {
                            start: 0,
                            end: 1,
                            length: 1,
                            qual: 4,
                            reference_start: Some(9),
                            reference_end: Some(9),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 3,
                            end: 4,
                            length: 1,
                            qual: 7,
                            reference_start: Some(12),
                            reference_end: Some(12),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 4,
                            end: 5,
                            length: 1,
                            qual: 9,
                            reference_start: Some(13),
                            reference_end: Some(13),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 7,
                            end: 8,
                            length: 1,
                            qual: 6,
                            reference_start: Some(16),
                            reference_end: Some(16),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                    ],
                    seq_len: 8,
                    reverse: false,
                },
                record_is_reverse: false,
            }],
        };

        let seq_coord_calls = SeqCoordCalls::try_from(&base_mods)?;

        // Test mod_types
        let mod_types = seq_coord_calls.mod_types();
        assert_eq!(mod_types.len(), 1);
        assert_eq!(
            mod_types.first().expect("has 1 element").0,
            ModChar::new('T')
        );
        assert!(mod_types.first().expect("has 1 element").1); // + strand

        // Test mod_calls for each position
        assert_eq!(seq_coord_calls.mod_calls(0), Some(&[4u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(1), Some(&[0u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(2), Some(&[0u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(3), Some(&[7u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(4), Some(&[9u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(5), Some(&[0u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(6), Some(&[0u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(7), Some(&[6u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(8), None); // Out of bounds

        // Test collapse_mod_calls
        let collapsed = seq_coord_calls.collapse_mod_calls();
        assert_eq!(
            collapsed,
            vec![true, false, false, true, true, false, false, true]
        );

        Ok(())
    }

    #[test]
    fn seq_coord_calls_from_basemods_example_2() -> Result<(), Error> {
        // Example from src/lib.rs nanalogue_mm_ml_parser docstring (count 2)
        let base_mods = BaseMods {
            base_mods: vec![BaseMod {
                modified_base: b'T',
                strand: '+',
                modification_type: 'T',
                ranges: Ranges {
                    annotations: vec![
                        FiberAnnotation {
                            start: 12,
                            end: 13,
                            length: 1,
                            qual: 3,
                            reference_start: Some(15),
                            reference_end: Some(15),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 13,
                            end: 14,
                            length: 1,
                            qual: 3,
                            reference_start: Some(16),
                            reference_end: Some(16),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 16,
                            end: 17,
                            length: 1,
                            qual: 4,
                            reference_start: Some(19),
                            reference_end: Some(19),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 19,
                            end: 20,
                            length: 1,
                            qual: 3,
                            reference_start: Some(22),
                            reference_end: Some(22),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                        FiberAnnotation {
                            start: 20,
                            end: 21,
                            length: 1,
                            qual: 182,
                            reference_start: Some(23),
                            reference_end: Some(23),
                            reference_length: Some(0),
                            extra_columns: None,
                        },
                    ],
                    seq_len: 33,
                    reverse: true,
                },
                record_is_reverse: true,
            }],
        };

        let seq_coord_calls = SeqCoordCalls::try_from(&base_mods)?;

        // Test mod_types
        let mod_types = seq_coord_calls.mod_types();
        assert_eq!(mod_types.len(), 1);
        assert_eq!(
            mod_types.first().expect("has 1 element").0,
            ModChar::new('T')
        );
        assert!(mod_types.first().expect("has 1 element").1); // + strand

        // Test mod_calls for positions with modifications
        assert_eq!(seq_coord_calls.mod_calls(12), Some(&[3u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(13), Some(&[3u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(16), Some(&[4u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(19), Some(&[3u8][..]));
        assert_eq!(seq_coord_calls.mod_calls(20), Some(&[182u8][..]));

        // Test mod_calls for positions without modifications
        for k in 0..33 {
            if [12, 13, 16, 19, 20].iter().all(|x| *x != k) {
                assert_eq!(seq_coord_calls.mod_calls(k), Some(&[0u8][..]));
            }
        }
        assert_eq!(seq_coord_calls.mod_calls(33), None); // Out of bounds

        // Test collapse_mod_calls
        let collapsed = seq_coord_calls.collapse_mod_calls();
        assert_eq!(collapsed.len(), 33);
        assert!(collapsed.get(12).expect("in range"));
        assert!(collapsed.get(13).expect("in range"));
        assert!(collapsed.get(16).expect("in range"));
        assert!(collapsed.get(19).expect("in range"));
        assert!(collapsed.get(20).expect("in range"));

        // Test collapsed_mod_calls for positions without modifications
        for k in 0..33 {
            if [12, 13, 16, 19, 20].iter().all(|x| *x != k) {
                assert!(!collapsed.get(k).expect("in range"));
            }
        }
        assert!(collapsed.get(33).is_none()); // Out of bounds

        Ok(())
    }

    #[test]
    fn seq_coord_calls_multiple_mod_types() -> Result<(), Error> {
        // Test with multiple modification types (both T+ and C+m)
        let base_mods = BaseMods {
            base_mods: vec![
                BaseMod {
                    modified_base: b'T',
                    strand: '+',
                    modification_type: 'T',
                    ranges: Ranges {
                        annotations: vec![FiberAnnotation {
                            start: 0,
                            end: 1,
                            length: 1,
                            qual: 100,
                            reference_start: Some(0),
                            reference_end: Some(0),
                            reference_length: Some(0),
                            extra_columns: None,
                        }],
                        seq_len: 5,
                        reverse: false,
                    },
                    record_is_reverse: false,
                },
                BaseMod {
                    modified_base: b'C',
                    strand: '+',
                    modification_type: 'm',
                    ranges: Ranges {
                        annotations: vec![FiberAnnotation {
                            start: 2,
                            end: 3,
                            length: 1,
                            qual: 200,
                            reference_start: None,
                            reference_end: None,
                            reference_length: None,
                            extra_columns: None,
                        }],
                        seq_len: 5,
                        reverse: false,
                    },
                    record_is_reverse: false,
                },
            ],
        };

        let seq_coord_calls = SeqCoordCalls::try_from(&base_mods)?;

        // Test mod_types - should have 2 types
        let mod_types = seq_coord_calls.mod_types();
        assert_eq!(mod_types.len(), 2);
        assert_eq!(
            mod_types.first().expect("has 2 elements").0,
            ModChar::new('T')
        );
        assert!(mod_types.first().expect("has 2 elements").1);
        assert_eq!(
            mod_types.get(1).expect("has 2 elements").0,
            ModChar::new('m')
        );
        assert!(mod_types.get(1).expect("has 2 elements").1);

        // Test mod_calls - each position should have 2 values (one for each mod type)
        assert_eq!(seq_coord_calls.mod_calls(0), Some(&[100u8, 0u8][..])); // T modified, C not
        assert_eq!(seq_coord_calls.mod_calls(1), Some(&[0u8, 0u8][..])); // Neither modified
        assert_eq!(seq_coord_calls.mod_calls(2), Some(&[0u8, 200u8][..])); // C modified, T not
        assert_eq!(seq_coord_calls.mod_calls(3), Some(&[0u8, 0u8][..])); // Neither modified
        assert_eq!(seq_coord_calls.mod_calls(4), Some(&[0u8, 0u8][..])); // Neither modified

        // Test collapse_mod_calls
        let collapsed = seq_coord_calls.collapse_mod_calls();
        assert_eq!(collapsed, vec![true, false, true, false, false]);

        Ok(())
    }

    #[test]
    fn seq_coord_calls_negative_strand() -> Result<(), Error> {
        // Test with negative strand
        let base_mods = BaseMods {
            base_mods: vec![BaseMod {
                modified_base: b'G',
                strand: '-',
                modification_type: 'h',
                ranges: Ranges {
                    annotations: vec![FiberAnnotation {
                        start: 1,
                        end: 2,
                        length: 1,
                        qual: 50,
                        reference_start: Some(1),
                        reference_end: Some(1),
                        reference_length: Some(0),
                        extra_columns: None,
                    }],
                    seq_len: 3,
                    reverse: false,
                },
                record_is_reverse: false,
            }],
        };

        let seq_coord_calls = SeqCoordCalls::try_from(&base_mods)?;

        // Test mod_types
        let mod_types = seq_coord_calls.mod_types();
        assert_eq!(mod_types.len(), 1);
        assert_eq!(
            mod_types.first().expect("has 1 element").0,
            ModChar::new('h')
        );
        assert!(!mod_types.first().expect("has 1 element").1); // - strand

        Ok(())
    }

    #[test]
    #[should_panic(expected = "UnavailableData")]
    fn seq_coord_calls_empty_basemods() {
        // Test with empty BaseMods - should fail
        let base_mods = BaseMods { base_mods: vec![] };
        let _: SeqCoordCalls = SeqCoordCalls::try_from(&base_mods).unwrap();
    }

    #[test]
    #[should_panic(expected = "NotImplemented")]
    fn seq_coord_calls_mismatched_seq_len() {
        // Test with mismatched seq_len in different BaseMod entries
        let base_mods = BaseMods {
            base_mods: vec![
                BaseMod {
                    modified_base: b'T',
                    strand: '+',
                    modification_type: 'T',
                    ranges: Ranges {
                        annotations: vec![],
                        seq_len: 5,
                        reverse: false,
                    },
                    record_is_reverse: false,
                },
                BaseMod {
                    modified_base: b'C',
                    strand: '+',
                    modification_type: 'm',
                    ranges: Ranges {
                        annotations: vec![],
                        seq_len: 10, // Different seq_len!
                        reverse: false,
                    },
                    record_is_reverse: false,
                },
            ],
        };
        let _: SeqCoordCalls = SeqCoordCalls::try_from(&base_mods).unwrap();
    }

    #[test]
    #[should_panic(expected = "InvalidState")]
    fn seq_coord_calls_invalid_strand() {
        // Test with invalid strand character
        let base_mods = BaseMods {
            base_mods: vec![BaseMod {
                modified_base: b'T',
                strand: 'x', // Invalid strand
                modification_type: 'T',
                ranges: Ranges {
                    annotations: vec![],
                    seq_len: 5,
                    reverse: false,
                },
                record_is_reverse: false,
            }],
        };
        let _: SeqCoordCalls = SeqCoordCalls::try_from(&base_mods).unwrap();
    }

    #[test]
    #[should_panic(expected = "NotImplemented")]
    fn seq_coord_calls_non_per_base_annotation() {
        // Test with multi-base annotation (not per-base)
        let base_mods = BaseMods {
            base_mods: vec![BaseMod {
                modified_base: b'T',
                strand: '+',
                modification_type: 'T',
                ranges: Ranges {
                    annotations: vec![FiberAnnotation {
                        start: 0,
                        end: 3, // Multi-base annotation (length 3, not 1)
                        length: 3,
                        qual: 100,
                        reference_start: Some(0),
                        reference_end: Some(2),
                        reference_length: Some(2),
                        extra_columns: None,
                    }],
                    seq_len: 5,
                    reverse: false,
                },
                record_is_reverse: false,
            }],
        };
        let _: SeqCoordCalls = SeqCoordCalls::try_from(&base_mods).unwrap();
    }

    #[test]
    #[should_panic(expected = "NotImplemented")]
    fn seq_coord_calls_start_beyond_seq_len() {
        // Test with start coordinate beyond seq_len
        let base_mods = BaseMods {
            base_mods: vec![BaseMod {
                modified_base: b'T',
                strand: '+',
                modification_type: 'T',
                ranges: Ranges {
                    annotations: vec![FiberAnnotation {
                        start: 10, // Start beyond seq_len (5)
                        end: 11,
                        length: 1,
                        qual: 100,
                        reference_start: Some(10),
                        reference_end: Some(10),
                        reference_length: Some(0),
                        extra_columns: None,
                    }],
                    seq_len: 5,
                    reverse: false,
                },
                record_is_reverse: false,
            }],
        };
        let _: SeqCoordCalls = SeqCoordCalls::try_from(&base_mods).unwrap();
    }
}