ref-solver 0.3.0

Solve reference genome identification from BAM/SAM headers
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
use std::borrow::Cow;
use std::io::BufReader;
use std::path::Path;
use thiserror::Error;
use tracing::warn;

use crate::core::contig::Contig;
use crate::core::header::QueryHeader;
use crate::utils::validation::{check_contig_limit, normalize_md5};

#[derive(Error, Debug)]
pub enum ParseError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Invalid SAM header format: {0}")]
    InvalidFormat(String),

    #[error("noodles error: {0}")]
    Noodles(String),

    #[error("Unsupported file format: {0}")]
    UnsupportedFormat(String),

    #[error("Too many contigs: {0} exceeds maximum allowed (100000)")]
    TooManyContigs(usize),
}

/// Parse a SAM/BAM/CRAM file and extract the header
///
/// # Errors
///
/// Returns `ParseError::Io` if the file cannot be read, `ParseError::Noodles` if
/// parsing fails, `ParseError::UnsupportedFormat` for unknown extensions,
/// `ParseError::InvalidFormat` if no contigs are found, or
/// `ParseError::TooManyContigs` if the limit is exceeded.
pub fn parse_file(path: &Path) -> Result<QueryHeader, ParseError> {
    let extension = path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_lowercase);

    match extension.as_deref() {
        Some("sam") => parse_sam_file(path),
        Some("bam") => parse_bam_file(path),
        Some("cram") => parse_cram_file(path),
        Some(ext) => Err(ParseError::UnsupportedFormat(ext.to_string())),
        None => {
            // Try to detect from content - default to SAM
            parse_sam_file(path)
        }
    }
}

/// Parse a SAM file (text format)
fn parse_sam_file(path: &Path) -> Result<QueryHeader, ParseError> {
    use noodles::sam;

    let mut reader = std::fs::File::open(path)
        .map(BufReader::new)
        .map(sam::io::Reader::new)?;

    let header = reader
        .read_header()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    header_to_query(&header, Some(path))
}

/// Parse a BAM file (binary format)
fn parse_bam_file(path: &Path) -> Result<QueryHeader, ParseError> {
    use noodles::bam;

    let mut reader = std::fs::File::open(path).map(bam::io::Reader::new)?;

    let header = reader
        .read_header()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    header_to_query(&header, Some(path))
}

/// Parse a CRAM file
fn parse_cram_file(path: &Path) -> Result<QueryHeader, ParseError> {
    use noodles::cram;

    let mut reader = std::fs::File::open(path).map(cram::io::Reader::new)?;

    // Read file definition
    reader
        .read_file_definition()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    let header = reader
        .read_file_header()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    header_to_query(&header, Some(path))
}

/// Parse a BAM header from any reader (no file path required).
///
/// This enables parsing from in-memory buffers (e.g. `Cursor<Vec<u8>>`)
/// without writing to a temporary file. Only the header is read;
/// the reader does not need to contain complete record data.
///
/// # Errors
///
/// Returns `ParseError::Noodles` if the BAM header cannot be parsed,
/// `ParseError::InvalidFormat` if no contigs are found, or
/// `ParseError::TooManyContigs` if the limit is exceeded.
pub fn parse_bam_from_reader<R: std::io::Read>(reader: R) -> Result<QueryHeader, ParseError> {
    use noodles::bam;

    let mut reader = bam::io::Reader::new(reader);

    let header = reader
        .read_header()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    header_to_query(&header, None)
}

/// Parse a CRAM header from any reader (no file path required).
///
/// This enables parsing from in-memory buffers (e.g. `Cursor<Vec<u8>>`)
/// without writing to a temporary file. Only the file definition and
/// header are read.
///
/// # Errors
///
/// Returns `ParseError::Noodles` if the CRAM header cannot be parsed,
/// `ParseError::InvalidFormat` if no contigs are found, or
/// `ParseError::TooManyContigs` if the limit is exceeded.
pub fn parse_cram_from_reader<R: std::io::Read>(reader: R) -> Result<QueryHeader, ParseError> {
    use noodles::cram;

    let mut reader = cram::io::Reader::new(reader);

    reader
        .read_file_definition()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    let header = reader
        .read_file_header()
        .map_err(|e| ParseError::Noodles(e.to_string()))?;

    header_to_query(&header, None)
}

/// Convert noodles header to `QueryHeader`
fn header_to_query(
    header: &noodles::sam::Header,
    source: Option<&Path>,
) -> Result<QueryHeader, ParseError> {
    use noodles::sam::header::record::value::map::tag::Other;

    let mut contigs = Vec::new();

    for (name, map) in header.reference_sequences() {
        let name_str = name.to_string();
        let length = map.length().get() as u64;

        let mut contig = Contig::new(name_str, length);

        // Extract M5 (MD5) tag from other_fields
        // The M5 tag contains the MD5 checksum of the sequence
        if let Ok(m5_tag) = Other::try_from(*b"M5") {
            if let Some(md5_value) = map.other_fields().get(&m5_tag) {
                let md5_str = md5_value.to_string();
                // Validate and normalize MD5 using centralized helper
                if let Some(normalized) = normalize_md5(&md5_str) {
                    contig.md5 = Some(normalized);
                } else {
                    warn!(
                        contig = %contig.name,
                        md5 = %md5_str,
                        "Invalid MD5 checksum format, ignoring"
                    );
                }
            }
        }

        // Extract AS (Assembly) tag
        if let Ok(as_tag) = Other::try_from(*b"AS") {
            if let Some(assembly_value) = map.other_fields().get(&as_tag) {
                contig.assembly = Some(assembly_value.to_string());
            }
        }

        // Extract UR (URI) tag
        if let Ok(ur_tag) = Other::try_from(*b"UR") {
            if let Some(uri_value) = map.other_fields().get(&ur_tag) {
                contig.uri = Some(uri_value.to_string());
            }
        }

        // Extract SP (Species) tag
        if let Ok(sp_tag) = Other::try_from(*b"SP") {
            if let Some(species_value) = map.other_fields().get(&sp_tag) {
                contig.species = Some(species_value.to_string());
            }
        }

        // Extract AN (Alternate Names) tag - comma-separated list of aliases
        if let Ok(an_tag) = Other::try_from(*b"AN") {
            if let Some(aliases_value) = map.other_fields().get(&an_tag) {
                let aliases: Vec<String> = aliases_value
                    .to_string()
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
                if !aliases.is_empty() {
                    contig.aliases = aliases;
                }
            }
        }

        // Check contig limit for DOS protection
        if check_contig_limit(contigs.len()).is_some() {
            return Err(ParseError::TooManyContigs(contigs.len()));
        }

        contigs.push(contig);
    }

    let mut query = QueryHeader::new(contigs);
    if let Some(path) = source {
        query = query.with_source(path.display().to_string());
    }

    Ok(query)
}

/// Normalize SAM header lines that use spaces instead of tabs.
///
/// Browsers and copy-paste often convert tabs to spaces. This function detects
/// SAM header lines (`@XX` prefix) where fields are space-separated instead of
/// tab-separated and converts the spaces to tabs.
///
/// Only normalizes lines that start with a SAM header record type (`@HD`, `@SQ`,
/// `@RG`, `@PG`) followed by a space and a TAG: pattern. Lines that already
/// contain tabs are left unchanged. `@CO` (comment) lines are not normalized
/// because their content is free-form text where spaces are meaningful.
///
/// Returns the (possibly normalized) text and a boolean indicating whether any
/// normalization was performed. Uses `Cow` to avoid allocation when no
/// normalization is needed (the common case for well-formed input).
#[must_use]
pub fn normalize_sam_whitespace(text: &str) -> (Cow<'_, str>, bool) {
    // Fast path: check if any line needs normalization before allocating
    if !text.lines().any(needs_space_to_tab_normalization) {
        return (Cow::Borrowed(text), false);
    }

    let mut normalized = String::with_capacity(text.len());

    for line in text.lines() {
        if !normalized.is_empty() {
            normalized.push('\n');
        }

        if needs_space_to_tab_normalization(line) {
            let mut first = true;
            for field in line.split_whitespace() {
                if first {
                    normalized.push_str(field);
                    first = false;
                } else {
                    normalized.push('\t');
                    normalized.push_str(field);
                }
            }
        } else {
            normalized.push_str(line);
        }
    }

    // Preserve trailing newline if present
    if text.ends_with('\n') {
        normalized.push('\n');
    }

    (Cow::Owned(normalized), true)
}

/// Check if a line is a SAM header line that uses spaces instead of tabs.
///
/// Returns true only when the line starts with a recognized SAM record type
/// followed by whitespace and a TAG: pattern, and the line contains NO tab
/// characters.
fn needs_space_to_tab_normalization(line: &str) -> bool {
    if line.contains('\t') {
        return false;
    }

    // @CO lines are free-form comments — do not normalize
    let sam_prefixes = ["@HD ", "@SQ ", "@RG ", "@PG "];
    if !sam_prefixes.iter().any(|p| line.starts_with(p)) {
        return false;
    }

    // Must have at least one TAG:VALUE pattern after the record type
    line.split_whitespace().skip(1).any(|field| {
        field.len() >= 3
            && field.as_bytes().get(2) == Some(&b':')
            && field.as_bytes()[0].is_ascii_uppercase()
            && field.as_bytes()[1].is_ascii_uppercase()
    })
}

/// Parse header from raw text (stdin or pasted)
///
/// # Errors
///
/// Returns `ParseError::InvalidFormat` if the text has invalid format, missing
/// required fields, or no contigs are found, or `ParseError::TooManyContigs`
/// if the limit is exceeded.
pub fn parse_header_text(text: &str) -> Result<QueryHeader, ParseError> {
    let (normalized_text, _) = normalize_sam_whitespace(text);
    let text = &normalized_text;
    let mut contigs = Vec::new();

    for line in text.lines() {
        if !line.starts_with("@SQ") {
            continue;
        }

        let mut name: Option<String> = None;
        let mut length: Option<u64> = None;
        let mut md5_raw: Option<String> = None;
        let mut assembly: Option<String> = None;
        let mut uri: Option<String> = None;
        let mut species: Option<String> = None;
        let mut aliases: Vec<String> = Vec::new();

        for field in line.split('\t').skip(1) {
            if let Some((tag, value)) = field.split_once(':') {
                match tag {
                    "SN" => name = Some(value.to_string()),
                    "LN" => length = value.parse().ok(),
                    "M5" => md5_raw = Some(value.to_string()),
                    "AS" => assembly = Some(value.to_string()),
                    "UR" => uri = Some(value.to_string()),
                    "SP" => species = Some(value.to_string()),
                    "AN" => {
                        // Alternate names (aliases), comma-separated
                        aliases = value
                            .split(',')
                            .map(|s| s.trim().to_string())
                            .filter(|s| !s.is_empty())
                            .collect();
                    }
                    _ => {}
                }
            }
        }

        if let (Some(ref name_str), Some(length)) = (&name, length) {
            // Check contig limit for DOS protection
            if check_contig_limit(contigs.len()).is_some() {
                return Err(ParseError::TooManyContigs(contigs.len()));
            }

            // Validate and normalize MD5, warn if invalid
            let md5 = if let Some(ref raw) = md5_raw {
                if let Some(normalized) = normalize_md5(raw) {
                    Some(normalized)
                } else {
                    warn!(
                        contig = %name_str,
                        md5 = %raw,
                        "Invalid MD5 checksum format, ignoring"
                    );
                    None
                }
            } else {
                None
            };

            let mut contig = Contig::new(name_str.clone(), length);
            contig.md5 = md5;
            contig.assembly = assembly;
            contig.uri = uri;
            contig.species = species;
            contig.aliases = aliases;
            contigs.push(contig);
        }
    }

    if contigs.is_empty() {
        return Err(ParseError::InvalidFormat(
            "No @SQ lines found in header".to_string(),
        ));
    }

    Ok(QueryHeader::new(contigs))
}

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

    #[test]
    fn test_parse_header_text() {
        let header = r"@HD	VN:1.6	SO:coordinate
@SQ	SN:chr1	LN:248956422	M5:6aef897c3d6ff0c78aff06ac189178dd
@SQ	SN:chr2	LN:242193529	M5:f98db672eb0993dcfdabafe2a882905c
@SQ	SN:chrM	LN:16569
@RG	ID:sample1
";

        let query = parse_header_text(header).unwrap();
        assert_eq!(query.contigs.len(), 3);

        assert_eq!(query.contigs[0].name, "chr1");
        assert_eq!(query.contigs[0].length, 248_956_422);
        assert_eq!(
            query.contigs[0].md5,
            Some("6aef897c3d6ff0c78aff06ac189178dd".to_string())
        );

        assert_eq!(query.contigs[1].name, "chr2");
        assert_eq!(query.contigs[2].name, "chrM");
        assert!(query.contigs[2].md5.is_none());
    }

    #[test]
    fn test_parse_header_text_no_sq() {
        let header = "@HD\tVN:1.6\n@RG\tID:sample1\n";
        let result = parse_header_text(header);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_header_text_with_aliases() {
        let header = r"@HD	VN:1.6
@SQ	SN:chr1	LN:248956422	M5:6aef897c3d6ff0c78aff06ac189178dd	AN:1,NC_000001.11
@SQ	SN:chrM	LN:16569	AN:MT,chrMT,NC_012920.1
";

        let query = parse_header_text(header).unwrap();
        assert_eq!(query.contigs.len(), 2);

        // Check chr1 aliases
        assert_eq!(query.contigs[0].name, "chr1");
        assert_eq!(
            query.contigs[0].aliases,
            vec!["1".to_string(), "NC_000001.11".to_string()]
        );

        // Check chrM aliases
        assert_eq!(query.contigs[1].name, "chrM");
        assert_eq!(
            query.contigs[1].aliases,
            vec![
                "MT".to_string(),
                "chrMT".to_string(),
                "NC_012920.1".to_string()
            ]
        );
    }

    #[test]
    fn test_normalize_sam_whitespace_spaces_to_tabs() {
        let input = "@SQ SN:chr1 LN:248956422 M5:6aef897c3d6ff0c78aff06ac189178dd\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(was_normalized);
        assert_eq!(
            normalized,
            "@SQ\tSN:chr1\tLN:248956422\tM5:6aef897c3d6ff0c78aff06ac189178dd\n"
        );
    }

    #[test]
    fn test_normalize_sam_whitespace_already_tabs() {
        let input = "@SQ\tSN:chr1\tLN:248956422\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(!was_normalized);
        assert_eq!(normalized, input);
    }

    #[test]
    fn test_normalize_sam_whitespace_mixed_lines() {
        let input =
            "@HD VN:1.6 SO:coordinate\n@SQ SN:chr1 LN:248956422\n@SQ SN:chr2 LN:242193529\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(was_normalized);
        assert!(normalized.contains("@HD\tVN:1.6\tSO:coordinate"));
        assert!(normalized.contains("@SQ\tSN:chr1\tLN:248956422"));
        assert!(normalized.contains("@SQ\tSN:chr2\tLN:242193529"));
    }

    #[test]
    fn test_normalize_sam_whitespace_multiple_spaces() {
        let input = "@SQ  SN:chr1  LN:248956422\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(was_normalized);
        assert_eq!(normalized, "@SQ\tSN:chr1\tLN:248956422\n");
    }

    #[test]
    fn test_normalize_sam_whitespace_preserves_non_header_lines() {
        let input = "some random text with spaces\n@SQ SN:chr1 LN:100\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(was_normalized);
        assert!(normalized.starts_with("some random text with spaces\n"));
        assert!(normalized.contains("@SQ\tSN:chr1\tLN:100"));
    }

    #[test]
    fn test_normalize_sam_whitespace_tabs_and_spaces_mixed() {
        // Line has some tabs and some spaces — leave it alone
        let input = "@SQ\tSN:chr1 LN:248956422\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(!was_normalized);
        assert_eq!(normalized, input);
    }

    #[test]
    fn test_normalize_sam_whitespace_skips_comment_lines() {
        let input = "@CO This is a comment with VN:1.0 mentioned\n@SQ SN:chr1 LN:100\n";
        let (normalized, was_normalized) = normalize_sam_whitespace(input);
        assert!(was_normalized);
        // Comment line should be preserved as-is
        assert!(normalized.starts_with("@CO This is a comment with VN:1.0 mentioned\n"));
        assert!(normalized.contains("@SQ\tSN:chr1\tLN:100"));
    }

    #[test]
    fn test_parse_header_text_with_spaces() {
        let header = "@SQ SN:chr1 LN:248956422 M5:6aef897c3d6ff0c78aff06ac189178dd\n\
                      @SQ SN:chr2 LN:242193529\n";
        let query = parse_header_text(header).unwrap();
        assert_eq!(query.contigs.len(), 2);
        assert_eq!(query.contigs[0].name, "chr1");
        assert_eq!(query.contigs[0].length, 248_956_422);
        assert_eq!(
            query.contigs[0].md5,
            Some("6aef897c3d6ff0c78aff06ac189178dd".to_string())
        );
        assert_eq!(query.contigs[1].name, "chr2");
        assert_eq!(query.contigs[1].length, 242_193_529);
    }
}

#[cfg(test)]
mod reader_tests {
    use super::*;
    use std::io::Cursor;

    /// Helper: create a minimal BAM file in memory with the given contigs.
    fn create_test_bam(contigs: &[(&str, usize)]) -> Vec<u8> {
        use noodles::bam;
        use noodles::sam;
        use noodles::sam::header::record::value::map::ReferenceSequence;
        use noodles::sam::header::record::value::Map;
        use std::num::NonZeroUsize;

        let mut header = sam::Header::builder();
        for &(name, length) in contigs {
            header = header.add_reference_sequence(
                name,
                Map::<ReferenceSequence>::new(NonZeroUsize::new(length).unwrap()),
            );
        }
        let header = header.build();

        let mut buf = Vec::new();
        {
            let mut writer = bam::io::Writer::new(&mut buf);
            writer.write_header(&header).unwrap();
        }
        buf
    }

    #[test]
    fn test_parse_bam_from_reader_basic() {
        let bam_bytes = create_test_bam(&[("chr1", 248_956_422), ("chr2", 242_193_529)]);
        let cursor = Cursor::new(&bam_bytes);
        let query = parse_bam_from_reader(cursor).unwrap();
        assert_eq!(query.contigs.len(), 2);
        assert_eq!(query.contigs[0].name, "chr1");
        assert_eq!(query.contigs[0].length, 248_956_422);
        assert_eq!(query.contigs[1].name, "chr2");
        assert_eq!(query.contigs[1].length, 242_193_529);
    }

    #[test]
    fn test_parse_bam_from_reader_truncated_after_header() {
        let mut bam_bytes = create_test_bam(&[("chr1", 248_956_422)]);
        bam_bytes.extend_from_slice(&[0u8; 100]);
        let cursor = Cursor::new(&bam_bytes);
        let query = parse_bam_from_reader(cursor).unwrap();
        assert_eq!(query.contigs.len(), 1);
        assert_eq!(query.contigs[0].name, "chr1");
    }

    #[test]
    fn test_parse_bam_from_reader_empty() {
        let cursor = Cursor::new(Vec::<u8>::new());
        let result = parse_bam_from_reader(cursor);
        assert!(result.is_err());
    }

    /// Helper: create a minimal CRAM file in memory with the given contigs.
    ///
    /// Each contig is `(name, length, md5)`.  The CRAM writer requires MD5
    /// checksums on every reference sequence, so we must supply them here.
    fn create_test_cram(contigs: &[(&str, usize, &str)]) -> Vec<u8> {
        use noodles::cram;
        use noodles::sam;
        use noodles::sam::header::record::value::map::reference_sequence::tag;
        use noodles::sam::header::record::value::map::ReferenceSequence;
        use noodles::sam::header::record::value::Map;
        use std::num::NonZeroUsize;

        let mut header = sam::Header::builder();
        for &(name, length, md5) in contigs {
            let map = Map::<ReferenceSequence>::builder()
                .set_length(NonZeroUsize::new(length).unwrap())
                .insert(tag::MD5_CHECKSUM, md5)
                .build()
                .unwrap();
            header = header.add_reference_sequence(name, map);
        }
        let header = header.build();

        let mut buf = Vec::new();
        {
            let mut writer = cram::io::Writer::new(&mut buf);
            writer.write_file_definition().unwrap();
            writer.write_file_header(&header).unwrap();
        }
        buf
    }

    #[test]
    fn test_parse_cram_from_reader_basic() {
        let cram_bytes = create_test_cram(&[
            ("chr1", 248_956_422, "6aef897c3d6ff0c78aff06ac189178dd"),
            ("chrX", 156_040_895, "01234567890abcdef01234567890abcd"),
        ]);
        let cursor = Cursor::new(&cram_bytes);
        let query = parse_cram_from_reader(cursor).unwrap();
        assert_eq!(query.contigs.len(), 2);
        assert_eq!(query.contigs[0].name, "chr1");
        assert_eq!(query.contigs[0].length, 248_956_422);
        assert_eq!(
            query.contigs[0].md5,
            Some("6aef897c3d6ff0c78aff06ac189178dd".to_string())
        );
        assert_eq!(query.contigs[1].name, "chrX");
        assert_eq!(query.contigs[1].length, 156_040_895);
        assert_eq!(
            query.contigs[1].md5,
            Some("01234567890abcdef01234567890abcd".to_string())
        );
    }

    #[test]
    fn test_parse_cram_from_reader_truncated_after_header() {
        let mut cram_bytes =
            create_test_cram(&[("chr1", 248_956_422, "6aef897c3d6ff0c78aff06ac189178dd")]);
        // Append garbage bytes to simulate a truncated upload that ends mid-record.
        // parse_cram_from_reader only reads the header, so this must still succeed.
        cram_bytes.extend_from_slice(&[0u8; 100]);
        let cursor = Cursor::new(&cram_bytes);
        let query = parse_cram_from_reader(cursor).unwrap();
        assert_eq!(query.contigs.len(), 1);
        assert_eq!(query.contigs[0].name, "chr1");
    }

    #[test]
    fn test_parse_cram_from_reader_empty() {
        let cursor = Cursor::new(Vec::<u8>::new());
        let result = parse_cram_from_reader(cursor);
        assert!(result.is_err());
    }
}