redicat 0.4.2

REDICAT - RNA Editing Cellular Assessment Toolkit: A highly parallelized utility for analyzing RNA editing events in single-cell RNA-seq data
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
//! BAM file processing for single-cell data
use std::path::Path;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use rust_htslib::bam::{self, pileup::Alignment, record::Record, Read};
use rustc_hash::FxHashMap;

use crate::pipeline::bam2mtx::barcode::BarcodeProcessor;

/// Base counts for a specific position
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct BaseCounts {
    /// Count of adenine (A) nucleotides
    pub a: u32,
    /// Count of thymine (T) nucleotides
    pub t: u32,
    /// Count of guanine (G) nucleotides
    pub g: u32,
    /// Count of cytosine (C) nucleotides
    pub c: u32,
}

/// Strand-specific base counts
#[derive(Debug, Clone, Default, serde::Serialize)]
pub struct StrandBaseCounts {
    /// Base counts for the forward strand
    pub forward: BaseCounts,
    /// Base counts for the reverse strand
    pub reverse: BaseCounts,
}

/// Processed data for a specific genomic position
#[derive(Debug, Clone, serde::Serialize)]
pub struct PositionData {
    /// Numeric contig identifier (matches the BAM header TID)
    pub contig_id: u32,
    /// 1-based genomic position
    pub pos: u64,
    /// Counts per cell barcode (indexed by whitelist order)
    pub counts: FxHashMap<u32, StrandBaseCounts>,
}

/// Consensus code used to indicate conflicting UMI calls
pub const UMI_CONFLICT_CODE: u8 = u8::MAX;

fn clean_tag_value(raw: &str) -> Option<String> {
    let clean = raw.split('-').next().unwrap_or(raw).trim();
    if clean.is_empty() || clean == "-" {
        None
    } else {
        Some(clean.to_string())
    }
}

/// Write the cleaned tag value into `buf`, returning `true` if a value was written.
///
/// This is the zero-allocation counterpart of [`clean_tag_value`]: the caller
/// provides a pre-allocated `String` that is **not** cleared automatically
/// (the caller should clear it before calling).
fn clean_tag_value_into(raw: &str, buf: &mut String) -> bool {
    let clean = raw.split('-').next().unwrap_or(raw).trim();
    if clean.is_empty() || clean == "-" {
        false
    } else {
        buf.push_str(clean);
        true
    }
}

/// Extract and normalize a cell barcode from the requested BAM tag.
pub fn decode_cell_barcode(record: &Record, tag: &[u8]) -> Result<Option<String>> {
    match record.aux(tag) {
        Ok(bam::record::Aux::String(s)) => Ok(clean_tag_value(s)),
        Ok(bam::record::Aux::ArrayU8(arr)) => {
            let bytes: Vec<u8> = arr.iter().collect();
            let raw = std::str::from_utf8(&bytes)?;
            Ok(clean_tag_value(raw))
        }
        Ok(_) => Ok(None),
        Err(_) => Ok(None),
    }
}

/// Buffer-reuse variant of [`decode_cell_barcode`].
///
/// Writes the cleaned barcode into `buf` (which the caller should have cleared)
/// and returns `Ok(true)` when a valid barcode was found, `Ok(false)` otherwise.
/// This avoids a `String` allocation per alignment in the hot pileup loop.
pub fn decode_cell_barcode_into(record: &Record, tag: &[u8], buf: &mut String) -> Result<bool> {
    match record.aux(tag) {
        Ok(bam::record::Aux::String(s)) => Ok(clean_tag_value_into(s, buf)),
        Ok(bam::record::Aux::ArrayU8(arr)) => {
            let bytes: Vec<u8> = arr.iter().collect();
            let raw = std::str::from_utf8(&bytes)?;
            Ok(clean_tag_value_into(raw, buf))
        }
        Ok(_) => Ok(false),
        Err(_) => Ok(false),
    }
}

/// Extract and normalize a UMI from the requested BAM tag.
pub fn decode_umi(record: &Record, tag: &[u8]) -> Result<Option<String>> {
    match record.aux(tag) {
        Ok(bam::record::Aux::String(s)) => Ok(clean_tag_value(s)),
        Ok(bam::record::Aux::ArrayU8(arr)) => {
            let bytes: Vec<u8> = arr.iter().collect();
            let raw = std::str::from_utf8(&bytes)?;
            Ok(clean_tag_value(raw))
        }
        Ok(_) => Ok(None),
        Err(_) => Ok(None),
    }
}

/// Buffer-reuse variant of [`decode_umi`].
///
/// Same semantics as [`decode_cell_barcode_into`] but for the UMI tag.
pub fn decode_umi_into(record: &Record, tag: &[u8], buf: &mut String) -> Result<bool> {
    match record.aux(tag) {
        Ok(bam::record::Aux::String(s)) => Ok(clean_tag_value_into(s, buf)),
        Ok(bam::record::Aux::ArrayU8(arr)) => {
            let bytes: Vec<u8> = arr.iter().collect();
            let raw = std::str::from_utf8(&bytes)?;
            Ok(clean_tag_value_into(raw, buf))
        }
        Ok(_) => Ok(false),
        Err(_) => Ok(false),
    }
}

/// Retrieve the canonical base at the requested query position.
pub fn decode_base(record: &Record, qpos: Option<usize>) -> Result<char> {
    let qpos = qpos.ok_or_else(|| anyhow!("Invalid query position"))?;
    let seq = record.seq();
    let base = seq.as_bytes()[qpos];

    Ok(match base {
        b'A' | b'a' => 'A',
        b'T' | b't' => 'T',
        b'G' | b'g' => 'G',
        b'C' | b'c' => 'C',
        _ => 'N',
    })
}

#[inline]
pub fn encode_call(stranded: bool, base: char, is_reverse: bool) -> Option<u8> {
    let base_code = match base {
        'A' => 0,
        'T' => 1,
        'G' => 2,
        'C' => 3,
        _ => return None,
    };

    if stranded {
        let strand_bit = if is_reverse { 1 } else { 0 };
        Some((base_code << 1) | strand_bit)
    } else {
        Some(base_code)
    }
}

#[inline]
pub fn apply_encoded_call(stranded: bool, code: u8, counts_entry: &mut StrandBaseCounts) {
    if stranded {
        let strand_bit = code & 1;
        let base_code = code >> 1;
        let target = if strand_bit == 1 {
            &mut counts_entry.reverse
        } else {
            &mut counts_entry.forward
        };

        match base_code {
            0 => target.a += 1,
            1 => target.t += 1,
            2 => target.g += 1,
            3 => target.c += 1,
            _ => {}
        }
    } else {
        match code {
            0 => counts_entry.forward.a += 1,
            1 => counts_entry.forward.t += 1,
            2 => counts_entry.forward.g += 1,
            3 => counts_entry.forward.c += 1,
            _ => {}
        }
    }
}

/// Configuration for BAM processing
#[derive(Debug, Clone)]
pub struct BamProcessorConfig {
    /// Minimum mapping quality for a read to be considered
    pub min_mapping_quality: u8,
    /// Minimum base quality for a base to be counted
    pub min_base_quality: u8,
    /// Whether the data is stranded (true) or unstranded (false)
    pub stranded: bool,
    /// Maximum pileup depth to examine per genomic position
    pub max_depth: u32,
    /// Tag name for UMI (Unique Molecular Identifier)
    pub umi_tag: String,
    /// Tag name for cell barcode
    pub cell_barcode_tag: String,
}

impl Default for BamProcessorConfig {
    fn default() -> Self {
        Self {
            min_mapping_quality: 255,
            min_base_quality: 30,
            stranded: true,
            max_depth: 65_536,
            umi_tag: "UB".to_string(),
            cell_barcode_tag: "CB".to_string(),
        }
    }
}

/// Main processor for BAM files
pub struct BamProcessor {
    /// Configuration for BAM processing
    config: BamProcessorConfig,
    /// Processor for validating cell barcodes
    barcode_processor: Arc<BarcodeProcessor>,
}

impl BamProcessor {
    /// Create a new BamProcessor
    pub fn new(config: BamProcessorConfig, barcode_processor: Arc<BarcodeProcessor>) -> Self {
        Self {
            config,
            barcode_processor,
        }
    }

    /// Process a single genomic position
    pub fn process_position(&self, bam_path: &Path, chrom: &str, pos: u64) -> Result<PositionData> {
        let mut reader = bam::IndexedReader::from_path(bam_path)?;

        // Convert to 0-based position for rust-htslib
        let start_pos = (pos - 1) as u32;
        let end_pos = pos as u32;

        // Get chromosome ID
        let header = reader.header().to_owned();
        let tid = header
            .tid(chrom.as_bytes())
            .ok_or_else(|| anyhow::anyhow!("Chromosome '{}' not found", chrom))?;

        // Fetch the region
        reader.fetch((tid, start_pos, end_pos))?;
        let mut pileups: bam::pileup::Pileups<'_, bam::IndexedReader> = reader.pileup();
        pileups.set_max_depth(self.config.max_depth.min(i32::MAX as u32));
        let mut counts: FxHashMap<u32, StrandBaseCounts> = FxHashMap::default();
        let mut umi_consensus: FxHashMap<(u32, String), u8> = FxHashMap::default();

        // Process pileup
        for pileup in pileups {
            let pileup = pileup?;
            if pileup.pos() != start_pos {
                continue;
            }

            if (pileup.depth() as u32) >= self.config.max_depth {
                continue;
            }

            // let mut processed = 0u32;

            for read in pileup.alignments() {
                if !self.should_process_read(&read) {
                    continue;
                }

                // processed = processed.saturating_add(1);
                // if processed > self.config.max_depth {
                //     break;
                // }

                let record = read.record();
                let cell_id =
                    match decode_cell_barcode(&record, self.config.cell_barcode_tag.as_bytes())? {
                        Some(barcode) => match self.barcode_processor.id_of(&barcode) {
                            Some(id) => id,
                            None => continue,
                        },
                        None => continue,
                    };

                let umi = match decode_umi(&record, self.config.umi_tag.as_bytes())? {
                    Some(umi) => umi,
                    None => continue,
                };

                let base = decode_base(&record, read.qpos())?;
                if let Some(encoded) = encode_call(self.config.stranded, base, record.is_reverse())
                {
                    umi_consensus
                        .entry((cell_id, umi))
                        .and_modify(|existing| {
                            if *existing != encoded {
                                *existing = UMI_CONFLICT_CODE;
                            }
                        })
                        .or_insert(encoded);
                }
            }
        }

        // Aggregate counts by cell barcode
        for ((cell_id, _umi), encoded) in umi_consensus.drain() {
            if encoded == UMI_CONFLICT_CODE {
                continue;
            }

            let counts_entry = counts.entry(cell_id).or_default();

            apply_encoded_call(self.config.stranded, encoded, counts_entry);
        }

        Ok(PositionData {
            contig_id: tid,
            pos,
            counts,
        })
    }

    /// Check if a read should be processed
    fn should_process_read(&self, read: &Alignment) -> bool {
        if read.is_del() || read.is_refskip() {
            return false;
        }

        let record = read.record();

        // Check mapping quality
        if record.mapq() < self.config.min_mapping_quality {
            return false;
        }

        // Check base quality
        if let Some(qpos) = read.qpos() {
            if let Some(qual) = record.qual().get(qpos) {
                if *qual < self.config.min_base_quality {
                    return false;
                }
            }
        }

        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_htslib::bam::{self, Read};
    use std::collections::BTreeSet;

    fn collect_barcodes_at_pos(
        bam_path: &Path,
        chrom: &str,
        pos: u64,
        cell_tag: &str,
    ) -> Result<Vec<String>> {
        let mut reader = bam::IndexedReader::from_path(bam_path)?;
        let header = reader.header().to_owned();
        let tid = header
            .tid(chrom.as_bytes())
            .ok_or_else(|| anyhow!("chromosome '{}' not found", chrom))?;
        reader.fetch((tid, (pos - 1) as u32, pos as u32))?;

        let mut barcodes = BTreeSet::new();
        for pileup in reader.pileup() {
            let pileup = pileup?;
            if pileup.pos() != (pos - 1) as u32 {
                continue;
            }
            for aln in pileup.alignments() {
                if aln.is_del() || aln.is_refskip() {
                    continue;
                }
                let record = aln.record();
                if let Some(cb) = decode_cell_barcode(&record, cell_tag.as_bytes())? {
                    barcodes.insert(cb);
                }
            }
        }

        Ok(barcodes.into_iter().collect())
    }

    fn manual_consensus(
        bam_path: &Path,
        chrom: &str,
        pos: u64,
        config: &BamProcessorConfig,
        barcode_processor: &BarcodeProcessor,
    ) -> Result<FxHashMap<u32, StrandBaseCounts>> {
        let mut reader = bam::IndexedReader::from_path(bam_path)?;
        let header = reader.header().to_owned();
        let tid = header
            .tid(chrom.as_bytes())
            .ok_or_else(|| anyhow!("chromosome '{}' not found", chrom))?;
        reader.fetch((tid, (pos - 1) as u32, pos as u32))?;

        let mut pileups = reader.pileup();
        pileups.set_max_depth(config.max_depth.min(i32::MAX as u32));

        let mut umi_consensus: FxHashMap<(u32, String), u8> = FxHashMap::default();
        let mut counts: FxHashMap<u32, StrandBaseCounts> = FxHashMap::default();

        for pileup in pileups {
            let pileup = pileup?;
            if pileup.pos() != (pos - 1) as u32 {
                continue;
            }

            if (pileup.depth() as u32) >= config.max_depth {
                continue;
            }

            for read in pileup.alignments() {
                if read.is_del() || read.is_refskip() {
                    continue;
                }

                let record = read.record();
                if record.mapq() < config.min_mapping_quality {
                    continue;
                }

                if let Some(qpos) = read.qpos() {
                    if let Some(qual) = record.qual().get(qpos) {
                        if *qual < config.min_base_quality {
                            continue;
                        }
                    }
                }

                let cell_id = match decode_cell_barcode(&record, config.cell_barcode_tag.as_bytes())? {
                    Some(cb) => match barcode_processor.id_of(&cb) {
                        Some(id) => id,
                        None => continue,
                    },
                    None => continue,
                };

                let umi = match decode_umi(&record, config.umi_tag.as_bytes())? {
                    Some(umi) => umi,
                    None => continue,
                };

                let base = decode_base(&record, read.qpos())?;
                if let Some(encoded) = encode_call(config.stranded, base, record.is_reverse()) {
                    umi_consensus
                        .entry((cell_id, umi))
                        .and_modify(|existing| {
                            if *existing != encoded {
                                *existing = UMI_CONFLICT_CODE;
                            }
                        })
                        .or_insert(encoded);
                }
            }
        }

        for ((cell_id, _umi), encoded) in umi_consensus.drain() {
            if encoded == UMI_CONFLICT_CODE {
                continue;
            }
            let counts_entry = counts.entry(cell_id).or_default();
            apply_encoded_call(config.stranded, encoded, counts_entry);
        }

        Ok(counts)
    }

    #[test]
    fn clean_tag_value_strips_suffix_and_whitespace() {
        assert_eq!(clean_tag_value("AAACCTG-1"), Some("AAACCTG".to_string()));
        assert_eq!(clean_tag_value("  TTTGCAA  "), Some("TTTGCAA".to_string()));
        assert_eq!(clean_tag_value("-"), None);
        assert_eq!(clean_tag_value("   "), None);
    }

    #[test]
    fn encode_and_apply_calls_work_for_stranded_and_unstranded() {
        let mut stranded_counts = StrandBaseCounts::default();
        let mut unstranded_counts = StrandBaseCounts::default();

        let fwd_a = encode_call(true, 'A', false).unwrap();
        let rev_g = encode_call(true, 'G', true).unwrap();
        apply_encoded_call(true, fwd_a, &mut stranded_counts);
        apply_encoded_call(true, rev_g, &mut stranded_counts);

        assert_eq!(stranded_counts.forward.a, 1);
        assert_eq!(stranded_counts.reverse.g, 1);

        let t = encode_call(false, 'T', true).unwrap();
        let c = encode_call(false, 'C', false).unwrap();
        apply_encoded_call(false, t, &mut unstranded_counts);
        apply_encoded_call(false, c, &mut unstranded_counts);

        assert_eq!(unstranded_counts.forward.t, 1);
        assert_eq!(unstranded_counts.forward.c, 1);
        assert_eq!(unstranded_counts.reverse.t, 0);
        assert!(encode_call(false, 'N', false).is_none());
    }

    #[test]
    fn process_position_chr22_matches_manual_consensus() -> Result<()> {
        let bam_path = Path::new("test/chr22.bam");
        if !bam_path.exists() {
            return Ok(());
        }

        let chrom = "chr22";
        let pos = 50_783_283u64;

        let config = BamProcessorConfig {
            min_mapping_quality: 255,
            min_base_quality: 30,
            stranded: true,
            max_depth: 10_000,
            umi_tag: "UB".to_string(),
            cell_barcode_tag: "CB".to_string(),
        };

        let barcodes = collect_barcodes_at_pos(bam_path, chrom, pos, &config.cell_barcode_tag)?;
        if barcodes.is_empty() {
            return Ok(());
        }

        let barcode_processor = Arc::new(BarcodeProcessor::from_vec(barcodes));
        let processor = BamProcessor::new(config.clone(), Arc::clone(&barcode_processor));

        let observed = processor.process_position(bam_path, chrom, pos)?;
        let expected = manual_consensus(bam_path, chrom, pos, &config, &barcode_processor)?;

        assert_eq!(observed.pos, pos);
        assert_eq!(observed.counts.len(), expected.len());

        for (cell_id, exp) in expected.iter() {
            let got = observed
                .counts
                .get(cell_id)
                .unwrap_or_else(|| panic!("missing cell_id {} in observed counts", cell_id));
            assert_eq!(got.forward.a, exp.forward.a);
            assert_eq!(got.forward.t, exp.forward.t);
            assert_eq!(got.forward.g, exp.forward.g);
            assert_eq!(got.forward.c, exp.forward.c);
            assert_eq!(got.reverse.a, exp.reverse.a);
            assert_eq!(got.reverse.t, exp.reverse.t);
            assert_eq!(got.reverse.g, exp.reverse.g);
            assert_eq!(got.reverse.c, exp.reverse.c);
        }

        Ok(())
    }

    // =========================================================================
    // Pre-refactor regression tests
    // =========================================================================

    /// Verify decode_base for all canonical bases and N fallback.
    #[test]
    fn decode_base_handles_all_bases_and_n() {
        // We can't easily construct a Record in tests, but we can test encode_call
        // and apply_encoded_call exhaustively for all base/strand combinations.
        for (base, expected_code_unstranded) in [('A', 0u8), ('T', 1), ('G', 2), ('C', 3)] {
            let code = encode_call(false, base, false).unwrap();
            assert_eq!(code, expected_code_unstranded, "unstranded encode for {}", base);

            let code_rev = encode_call(false, base, true).unwrap();
            assert_eq!(code_rev, expected_code_unstranded, "unstranded+reverse for {}", base);
        }
        assert!(encode_call(false, 'N', false).is_none());
        assert!(encode_call(true, 'N', true).is_none());
    }

    /// Verify stranded encode_call produces distinct codes for fwd vs rev.
    #[test]
    fn encode_call_stranded_distinguishes_strands() {
        for base in ['A', 'T', 'G', 'C'] {
            let fwd = encode_call(true, base, false).unwrap();
            let rev = encode_call(true, base, true).unwrap();
            assert_ne!(fwd, rev, "stranded codes should differ for base {}", base);
            // Forward strand bit = 0, reverse = 1
            assert_eq!(fwd & 1, 0);
            assert_eq!(rev & 1, 1);
        }
    }

    /// Verify apply_encoded_call increments the correct field.
    #[test]
    fn apply_encoded_call_increments_correct_fields() {
        // Test all 8 stranded combinations
        let bases = ['A', 'T', 'G', 'C'];
        for &base in &bases {
            for is_reverse in [false, true] {
                let mut counts = StrandBaseCounts::default();
                let code = encode_call(true, base, is_reverse).unwrap();
                apply_encoded_call(true, code, &mut counts);

                let (fwd, rev) = (&counts.forward, &counts.reverse);
                let total = fwd.a + fwd.t + fwd.g + fwd.c + rev.a + rev.t + rev.g + rev.c;
                assert_eq!(total, 1, "exactly one field should be incremented");

                if !is_reverse {
                    match base {
                        'A' => assert_eq!(fwd.a, 1),
                        'T' => assert_eq!(fwd.t, 1),
                        'G' => assert_eq!(fwd.g, 1),
                        'C' => assert_eq!(fwd.c, 1),
                        _ => unreachable!(),
                    }
                } else {
                    match base {
                        'A' => assert_eq!(rev.a, 1),
                        'T' => assert_eq!(rev.t, 1),
                        'G' => assert_eq!(rev.g, 1),
                        'C' => assert_eq!(rev.c, 1),
                        _ => unreachable!(),
                    }
                }
            }
        }
    }

    /// Verify multiple apply_encoded_call accumulates.
    #[test]
    fn apply_encoded_call_accumulates() {
        let mut counts = StrandBaseCounts::default();
        let code_a_fwd = encode_call(true, 'A', false).unwrap();
        for _ in 0..5 {
            apply_encoded_call(true, code_a_fwd, &mut counts);
        }
        assert_eq!(counts.forward.a, 5);
    }

    /// Verify UMI_CONFLICT_CODE is 0xFF.
    #[test]
    fn umi_conflict_code_is_max_u8() {
        assert_eq!(UMI_CONFLICT_CODE, 0xFF);
        // No valid encode_call produces 0xFF
        for base in ['A', 'T', 'G', 'C'] {
            for stranded in [true, false] {
                for is_reverse in [true, false] {
                    if let Some(code) = encode_call(stranded, base, is_reverse) {
                        assert_ne!(code, UMI_CONFLICT_CODE);
                    }
                }
            }
        }
    }

    /// Verify clean_tag_value handles edge cases.
    #[test]
    fn clean_tag_value_edge_cases() {
        assert_eq!(clean_tag_value(""), None);
        assert_eq!(clean_tag_value("-"), None);
        assert_eq!(clean_tag_value("ABC-1-2"), Some("ABC".to_string()));
        assert_eq!(clean_tag_value("NOPREFIX"), Some("NOPREFIX".to_string()));
    }

    /// Verify BamProcessorConfig defaults.
    #[test]
    fn bam_processor_config_defaults() {
        let config = BamProcessorConfig::default();
        assert_eq!(config.min_mapping_quality, 255);
        assert_eq!(config.min_base_quality, 30);
        assert!(config.stranded);
        assert_eq!(config.max_depth, 65_536);
        assert_eq!(config.umi_tag, "UB");
        assert_eq!(config.cell_barcode_tag, "CB");
    }

    /// Verify BaseCounts default is all zeros.
    #[test]
    fn base_counts_default_is_zero() {
        let bc = BaseCounts::default();
        assert_eq!(bc.a, 0);
        assert_eq!(bc.t, 0);
        assert_eq!(bc.g, 0);
        assert_eq!(bc.c, 0);
    }

    /// Verify StrandBaseCounts default is all zeros.
    #[test]
    fn strand_base_counts_default_is_zero() {
        let sbc = StrandBaseCounts::default();
        assert_eq!(sbc.forward.a + sbc.forward.t + sbc.forward.g + sbc.forward.c, 0);
        assert_eq!(sbc.reverse.a + sbc.reverse.t + sbc.reverse.g + sbc.reverse.c, 0);
    }

    // =========================================================================
    // Buffer-reuse function tests (Phase 2)
    // =========================================================================

    #[test]
    fn clean_tag_value_into_writes_to_buffer() {
        let mut buf = String::new();
        assert!(clean_tag_value_into("AAACCTG-1", &mut buf));
        assert_eq!(buf, "AAACCTG");
    }

    #[test]
    fn clean_tag_value_into_strips_whitespace() {
        let mut buf = String::new();
        assert!(clean_tag_value_into("  TTTGCAA  ", &mut buf));
        assert_eq!(buf, "TTTGCAA");
    }

    #[test]
    fn clean_tag_value_into_returns_false_for_empty_and_dash() {
        let mut buf = String::new();
        assert!(!clean_tag_value_into("", &mut buf));
        assert!(buf.is_empty());

        assert!(!clean_tag_value_into("-", &mut buf));
        assert!(buf.is_empty());

        assert!(!clean_tag_value_into("   ", &mut buf));
        assert!(buf.is_empty());
    }

    #[test]
    fn clean_tag_value_into_matches_original() {
        // Verify that the buffer variant produces the same result as the
        // original for a variety of inputs.
        let inputs = [
            "AAACCTG-1",
            "NOPREFIX",
            "ABC-1-2",
            "  TTTGCAA  ",
            "-",
            "",
            "   ",
        ];
        for input in inputs {
            let original = clean_tag_value(input);
            let mut buf = String::new();
            let ok = clean_tag_value_into(input, &mut buf);
            match original {
                Some(ref s) => {
                    assert!(ok, "expected true for {:?}", input);
                    assert_eq!(&buf, s, "mismatch for {:?}", input);
                }
                None => {
                    assert!(!ok, "expected false for {:?}", input);
                    assert!(buf.is_empty(), "buffer should be empty for {:?}", input);
                }
            }
        }
    }
}