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
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
use crate::core::header::QueryHeader;
use std::path::Path;

/// Supported file formats for reference identification
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FileFormat {
    /// SAM/BAM/CRAM files or plain text headers
    Sam,
    /// BAM binary format
    Bam,
    /// CRAM binary format
    Cram,
    /// Picard sequence dictionary files
    Dict,
    /// VCF files with contig headers
    Vcf,
    /// NCBI assembly report files
    NcbiReport,
    /// TSV/CSV tabular files
    Tsv,
    /// FASTA index (.fai) files
    Fai,
    /// FASTA sequence files
    Fasta,
    /// Automatically detect format
    Auto,
}

/// Errors that can occur during format detection
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum FormatError {
    #[error("Unable to detect file format from content and filename")]
    UnknownFormat,
    #[error("File appears to be binary but cannot determine specific format")]
    UnsupportedBinary,
}

/// Errors that can occur during parsing
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
    #[error("Failed to parse {format:?} content: {message}")]
    ParseFailed { format: FileFormat, message: String },
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

impl FileFormat {
    /// Get the display name for this format
    #[must_use]
    #[allow(clippy::trivially_copy_pass_by_ref)] // Idiomatic method signature
    pub fn display_name(&self) -> &'static str {
        match self {
            FileFormat::Sam => "SAM/BAM Header",
            FileFormat::Bam => "BAM File",
            FileFormat::Cram => "CRAM File",
            FileFormat::Dict => "Sequence Dictionary",
            FileFormat::Vcf => "VCF File",
            FileFormat::NcbiReport => "NCBI Assembly Report",
            FileFormat::Tsv => "TSV/CSV Table",
            FileFormat::Fai => "FASTA Index",
            FileFormat::Fasta => "FASTA File",
            FileFormat::Auto => "Auto-detect",
        }
    }
}

/// Detect file format from content and optional filename
///
/// # Errors
///
/// Returns `FormatError::UnknownFormat` if the format cannot be detected, or
/// `FormatError::UnsupportedBinary` if the file appears to be binary but the
/// specific format cannot be determined.
pub fn detect_format(content: &str, filename: Option<&str>) -> Result<FileFormat, FormatError> {
    // First try filename-based detection if available
    if let Some(name) = filename {
        if let Some(format) = detect_format_from_filename(name) {
            // For binary formats, trust filename-based detection without content validation
            if matches!(format, FileFormat::Bam | FileFormat::Cram) {
                return Ok(format);
            }
            // For text formats, validate that content matches expected format
            if validate_format_content(content, &format) {
                return Ok(format);
            }
        }
    }

    // Fall back to content-based detection
    detect_format_from_content(content)
}

/// Detect format based on filename and extension
fn detect_format_from_filename(filename: &str) -> Option<FileFormat> {
    let path = Path::new(filename);
    let lower_name = filename.to_lowercase();

    // Check for compressed formats first (multi-extension patterns)
    if lower_name.ends_with(".vcf.gz") {
        return Some(FileFormat::Vcf);
    }
    if lower_name.ends_with(".fa.gz")
        || lower_name.ends_with(".fasta.gz")
        || lower_name.ends_with(".fna.gz")
        || lower_name.ends_with(".fa.bgz")
        || lower_name.ends_with(".fasta.bgz")
        || lower_name.ends_with(".fna.bgz")
    {
        return Some(FileFormat::Fasta);
    }

    let extension = path.extension()?.to_str()?.to_lowercase();

    match extension.as_str() {
        "sam" => Some(FileFormat::Sam),
        "bam" => Some(FileFormat::Bam),
        "cram" => Some(FileFormat::Cram),
        "dict" => Some(FileFormat::Dict),
        "vcf" => Some(FileFormat::Vcf),
        "fai" => Some(FileFormat::Fai),
        "fa" | "fasta" | "fna" => Some(FileFormat::Fasta),
        "tsv" | "csv" => Some(FileFormat::Tsv),
        "txt" => {
            // Disambiguate .txt files based on filename patterns
            if lower_name.contains("assembly") || lower_name.contains("report") {
                Some(FileFormat::NcbiReport)
            } else if lower_name.ends_with(".dict.txt") {
                Some(FileFormat::Dict)
            } else {
                // Default to SAM for .txt files
                Some(FileFormat::Sam)
            }
        }
        _ => None,
    }
}

/// Check if a line starts with a SAM record type prefix followed by a tab or space.
///
/// SAM headers use tabs as delimiters, but copy-pasted text often has spaces instead.
fn is_sam_record(line: &str, prefix: &str) -> bool {
    line.starts_with(prefix)
        && line
            .as_bytes()
            .get(prefix.len())
            .is_some_and(|&b| b == b'\t' || b == b' ')
}

/// Detect format from file content analysis
fn detect_format_from_content(content: &str) -> Result<FileFormat, FormatError> {
    let content_trimmed = content.trim();

    // Check for empty content
    if content_trimmed.is_empty() {
        return Err(FormatError::UnknownFormat);
    }

    // Check for binary content (non-UTF8 or control characters)
    if content
        .chars()
        .any(|c| c.is_control() && c != '\n' && c != '\r' && c != '\t')
    {
        return Err(FormatError::UnsupportedBinary);
    }

    let lines: Vec<&str> = content_trimmed.lines().take(20).collect(); // Sample first 20 lines

    // Picard dictionary: starts with @HD and has @SQ lines (check BEFORE Sam)
    if lines.iter().any(|line| is_sam_record(line, "@HD"))
        && lines.iter().any(|line| is_sam_record(line, "@SQ"))
    {
        return Ok(FileFormat::Dict);
    }

    // SAM header format: starts with @SQ lines
    if lines.iter().any(|line| is_sam_record(line, "@SQ")) {
        return Ok(FileFormat::Sam);
    }

    // VCF format: starts with ## comments and has ##contig lines
    if lines
        .iter()
        .any(|line| line.starts_with("##fileformat=VCF"))
        || (lines.iter().any(|line| line.starts_with("##"))
            && lines.iter().any(|line| line.starts_with("##contig=")))
    {
        return Ok(FileFormat::Vcf);
    }

    // NCBI assembly report: has specific column headers (check all lines for header)
    if lines.iter().any(|line| {
        line.contains("Sequence-Name")
            && line.contains("Sequence-Role")
            && line.contains("Assigned-Molecule")
    }) {
        return Ok(FileFormat::NcbiReport);
    }

    // TSV format: tab-separated with consistent column count
    if lines.len() > 1 {
        let first_line_cols = lines[0].split('\t').count();
        if first_line_cols > 2
            && lines
                .iter()
                .take(5)
                .all(|line| line.split('\t').count() == first_line_cols)
        {
            // Check if it looks like sequence data (has length/size columns)
            if lines[0].to_lowercase().contains("length")
                || lines[0].to_lowercase().contains("size")
                || lines[0].to_lowercase().contains("sequence")
            {
                return Ok(FileFormat::Tsv);
            }
        }
    }

    // CSV format: comma-separated
    if lines.len() > 1 {
        let first_line_cols = lines[0].split(',').count();
        if first_line_cols > 2
            && lines
                .iter()
                .take(5)
                .all(|line| line.split(',').count() == first_line_cols)
            && (lines[0].to_lowercase().contains("length")
                || lines[0].to_lowercase().contains("size")
                || lines[0].to_lowercase().contains("sequence"))
        {
            return Ok(FileFormat::Tsv);
        }
    }

    // FAI format: exactly 5 tab-separated columns (name, length, offset, line_bases, line_width)
    // All non-empty, non-comment lines should have 5 columns with numeric values in columns 2-5
    if !lines.is_empty() {
        let fai_lines: Vec<&&str> = lines
            .iter()
            .filter(|line| !line.is_empty() && !line.starts_with('#'))
            .collect();

        if !fai_lines.is_empty()
            && fai_lines.iter().all(|line| {
                let fields: Vec<&str> = line.split('\t').collect();
                if fields.len() != 5 {
                    return false;
                }
                // All fields after the first should be numeric
                fields[1..].iter().all(|f| f.parse::<u64>().is_ok())
            })
        {
            return Ok(FileFormat::Fai);
        }
    }

    // If content looks like plain text with sequence-like data, assume SAM header
    if lines.iter().any(|line| {
        line.contains("chr")
            || line.contains("scaffold")
            || line.contains("contig")
            || line.to_lowercase().contains("sequence")
            || line.to_lowercase().contains("length")
    }) {
        return Ok(FileFormat::Sam);
    }

    Err(FormatError::UnknownFormat)
}

/// Validate that content matches the expected format
#[allow(clippy::trivially_copy_pass_by_ref)] // Clearer API with reference
fn validate_format_content(content: &str, format: &FileFormat) -> bool {
    match format {
        FileFormat::Sam => {
            content.contains("@SQ") || content.contains("SN:") || content.contains("LN:")
        }
        FileFormat::Dict => content.contains("@HD") && content.contains("@SQ"),
        FileFormat::Vcf => {
            content.contains("##")
                && (content.contains("##contig=") || content.contains("##fileformat=VCF"))
        }
        FileFormat::NcbiReport => {
            content.contains("Sequence-Name") || content.contains("Sequence-Role")
        }
        FileFormat::Tsv => {
            content.contains('\t')
                && (content.to_lowercase().contains("length")
                    || content.to_lowercase().contains("sequence"))
        }
        FileFormat::Fai => {
            // FAI format has 5 tab-separated columns
            let lines: Vec<&str> = content.lines().take(5).collect();
            lines.iter().any(|line| {
                let fields: Vec<&str> = line.split('\t').collect();
                fields.len() == 5 && fields[1..].iter().all(|f| f.parse::<u64>().is_ok())
            })
        }
        FileFormat::Bam | FileFormat::Cram | FileFormat::Fasta => {
            // Binary formats should not be validated against text content
            false
        }
        FileFormat::Auto => true, // Auto-detect always passes validation
    }
}

/// Parse content with the specified format
///
/// # Errors
///
/// Returns `ParseError::ParseFailed` if the content cannot be parsed with the
/// specified format.
pub fn parse_with_format(content: &str, format: FileFormat) -> Result<QueryHeader, ParseError> {
    match format {
        FileFormat::Sam => {
            crate::parsing::sam::parse_header_text(content).map_err(|e| ParseError::ParseFailed {
                format: FileFormat::Sam,
                message: e.to_string(),
            })
        }
        FileFormat::Dict => {
            crate::parsing::dict::parse_dict_text(content).map_err(|e| ParseError::ParseFailed {
                format: FileFormat::Dict,
                message: e.to_string(),
            })
        }
        FileFormat::Vcf => crate::parsing::vcf::parse_vcf_header_text(content).map_err(|e| {
            ParseError::ParseFailed {
                format: FileFormat::Vcf,
                message: e.to_string(),
            }
        }),
        FileFormat::NcbiReport => {
            // NCBI report parser returns Vec<NcbiContigEntry>, we need to convert
            match crate::parsing::ncbi_report::parse_ncbi_report_text(content) {
                Ok(entries) => {
                    let contigs = entries.into_iter().map(|entry| entry.to_contig()).collect();
                    Ok(crate::core::header::QueryHeader::new(contigs))
                }
                Err(e) => Err(ParseError::ParseFailed {
                    format: FileFormat::NcbiReport,
                    message: e.to_string(),
                }),
            }
        }
        FileFormat::Tsv => {
            // TSV parser requires delimiter - try tab first, then comma
            match crate::parsing::tsv::parse_tsv_text(content, '\t') {
                Ok(query) => Ok(query),
                Err(_) => crate::parsing::tsv::parse_tsv_text(content, ',').map_err(|e| {
                    ParseError::ParseFailed {
                        format: FileFormat::Tsv,
                        message: format!("Failed to parse as TSV or CSV: {e}"),
                    }
                }),
            }
        }
        FileFormat::Fai => {
            crate::parsing::fai::parse_fai_text(content).map_err(|e| ParseError::ParseFailed {
                format: FileFormat::Fai,
                message: e.to_string(),
            })
        }
        FileFormat::Bam => Err(ParseError::ParseFailed {
            format: FileFormat::Bam,
            message: "BAM files must be parsed as binary, not text".to_string(),
        }),
        FileFormat::Cram => Err(ParseError::ParseFailed {
            format: FileFormat::Cram,
            message: "CRAM files must be parsed as binary, not text".to_string(),
        }),
        FileFormat::Fasta => Err(ParseError::ParseFailed {
            format: FileFormat::Fasta,
            message: "FASTA files must be parsed as binary, not text".to_string(),
        }),
        FileFormat::Auto => {
            // For auto-detection, detect format first then parse
            let detected_format =
                detect_format_from_content(content).map_err(|e| ParseError::ParseFailed {
                    format: FileFormat::Auto,
                    message: format!("Auto-detection failed: {e}"),
                })?;

            parse_with_format(content, detected_format)
        }
    }
}

/// Parse binary file content (for BAM/CRAM/FASTA files)
///
/// BAM and CRAM are parsed directly from memory via a `Cursor<&[u8]>`.
/// FASTA still requires a temporary file since the parser must read full sequences
/// to compute contig lengths.
///
/// # Errors
///
/// Returns `ParseError::Io` if a temporary file cannot be created or written (FASTA only),
/// or `ParseError::ParseFailed` if parsing fails.
pub fn parse_binary_file(
    file_content: &[u8],
    format: FileFormat,
) -> Result<QueryHeader, ParseError> {
    match format {
        FileFormat::Bam => {
            let cursor = std::io::Cursor::new(file_content);
            crate::parsing::sam::parse_bam_from_reader(cursor).map_err(|e| {
                ParseError::ParseFailed {
                    format,
                    message: format!("BAM file parsing failed: {e}"),
                }
            })
        }
        FileFormat::Cram => {
            let cursor = std::io::Cursor::new(file_content);
            crate::parsing::sam::parse_cram_from_reader(cursor).map_err(|e| {
                ParseError::ParseFailed {
                    format,
                    message: format!("CRAM file parsing failed: {e}"),
                }
            })
        }
        FileFormat::Fasta => {
            // FASTA still needs temp file — must read full sequences for lengths
            use std::io::Write;
            use tempfile::NamedTempFile;

            let is_gzipped =
                file_content.len() >= 2 && file_content[0] == 0x1f && file_content[1] == 0x8b;
            let file_extension = if is_gzipped { ".fa.gz" } else { ".fa" };

            let mut temp_file =
                NamedTempFile::with_suffix(file_extension).map_err(ParseError::Io)?;
            temp_file.write_all(file_content).map_err(ParseError::Io)?;

            let result = crate::parsing::fasta::parse_fasta_file(temp_file.path());
            result.map_err(|e| ParseError::ParseFailed {
                format,
                message: format!("FASTA file parsing failed: {e}"),
            })
        }
        _ => Err(ParseError::ParseFailed {
            format,
            message: "Format is not a binary file format".to_string(),
        }),
    }
}

/// Parse a binary file directly from a file path (for streamed uploads).
///
/// Unlike [`parse_binary_file`], this function does not need to create a temporary file —
/// the caller already has one.  The file may be truncated after the header; only the
/// header portion is needed for BAM/CRAM.
///
/// # Errors
///
/// Returns `ParseError::ParseFailed` if parsing fails, or if the format is not a
/// supported binary format.
pub fn parse_binary_file_from_path(
    path: &std::path::Path,
    format: FileFormat,
) -> Result<QueryHeader, ParseError> {
    match format {
        FileFormat::Bam | FileFormat::Cram => {
            crate::parsing::sam::parse_file(path).map_err(|e| ParseError::ParseFailed {
                format,
                message: format!("Binary file parsing failed: {e}"),
            })
        }
        FileFormat::Fasta => {
            crate::parsing::fasta::parse_fasta_file(path).map_err(|e| ParseError::ParseFailed {
                format,
                message: format!("FASTA file parsing failed: {e}"),
            })
        }
        _ => Err(ParseError::ParseFailed {
            format,
            message: "Format is not a binary file format".to_string(),
        }),
    }
}

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

    #[test]
    fn test_filename_detection() {
        assert_eq!(
            detect_format_from_filename("test.sam"),
            Some(FileFormat::Sam)
        );
        assert_eq!(
            detect_format_from_filename("test.bam"),
            Some(FileFormat::Bam)
        );
        assert_eq!(
            detect_format_from_filename("test.dict"),
            Some(FileFormat::Dict)
        );
        assert_eq!(
            detect_format_from_filename("test.vcf"),
            Some(FileFormat::Vcf)
        );
        assert_eq!(
            detect_format_from_filename("test.vcf.gz"),
            Some(FileFormat::Vcf)
        );
        assert_eq!(
            detect_format_from_filename("assembly_report.txt"),
            Some(FileFormat::NcbiReport)
        );
        assert_eq!(
            detect_format_from_filename("reference.fai"),
            Some(FileFormat::Fai)
        );
        assert_eq!(
            detect_format_from_filename("reference.fa"),
            Some(FileFormat::Fasta)
        );
        assert_eq!(
            detect_format_from_filename("reference.fasta"),
            Some(FileFormat::Fasta)
        );
        assert_eq!(
            detect_format_from_filename("reference.fa.gz"),
            Some(FileFormat::Fasta)
        );
        assert_eq!(
            detect_format_from_filename("reference.fasta.gz"),
            Some(FileFormat::Fasta)
        );
        assert_eq!(detect_format_from_filename("unknown.xyz"), None);
    }

    #[test]
    fn test_sam_header_detection() {
        let content = "@SQ\tSN:chr1\tLN:248956422\tM5:6aef897c3d6ff0c78aff06ac189178dd\n";
        assert_eq!(detect_format_from_content(content), Ok(FileFormat::Sam));
    }

    #[test]
    fn test_dict_detection() {
        let content = "@HD\tVN:1.0\tSO:coordinate\n@SQ\tSN:chr1\tLN:248956422\tM5:abc123\n";
        assert_eq!(detect_format_from_content(content), Ok(FileFormat::Dict));
    }

    #[test]
    fn test_vcf_detection() {
        let content = "##fileformat=VCFv4.2\n##contig=<ID=chr1,length=248956422>\n";
        assert_eq!(detect_format_from_content(content), Ok(FileFormat::Vcf));
    }

    #[test]
    fn test_ncbi_report_detection() {
        let content =
            "# Sequence-Name\tSequence-Role\tAssigned-Molecule\tAssigned-Molecule-Location/Type\n";
        assert_eq!(
            detect_format_from_content(content),
            Ok(FileFormat::NcbiReport)
        );
    }

    #[test]
    fn test_fai_detection() {
        let content = "chr1\t248956422\t112\t70\t71\nchr2\t242193529\t253404903\t70\t71\n";
        assert_eq!(detect_format_from_content(content), Ok(FileFormat::Fai));
    }

    #[test]
    fn test_fai_validation() {
        assert!(validate_format_content(
            "chr1\t248956422\t112\t70\t71",
            &FileFormat::Fai
        ));
        assert!(!validate_format_content(
            "chr1\t248956422\t112",
            &FileFormat::Fai
        ));
    }

    #[test]
    fn test_format_validation() {
        assert!(validate_format_content(
            "@SQ\tSN:chr1\tLN:123",
            &FileFormat::Sam
        ));
        assert!(!validate_format_content("random text", &FileFormat::Sam));

        assert!(validate_format_content(
            "##contig=<ID=chr1>",
            &FileFormat::Vcf
        ));
        assert!(!validate_format_content("@SQ\tSN:chr1", &FileFormat::Vcf));
    }

    #[test]
    fn test_sam_header_detection_with_spaces() {
        let content = "@SQ SN:chr1 LN:248956422 M5:6aef897c3d6ff0c78aff06ac189178dd\n";
        assert_eq!(detect_format_from_content(content), Ok(FileFormat::Sam));
    }

    #[test]
    fn test_dict_detection_with_spaces() {
        let content = "@HD VN:1.0 SO:coordinate\n@SQ SN:chr1 LN:248956422\n";
        assert_eq!(detect_format_from_content(content), Ok(FileFormat::Dict));
    }

    #[test]
    fn test_sam_validation_with_spaces() {
        assert!(validate_format_content(
            "@SQ SN:chr1 LN:123",
            &FileFormat::Sam
        ));
    }

    #[test]
    fn test_combined_detection() {
        let content = "@SQ\tSN:chr1\tLN:248956422\n";
        assert_eq!(
            detect_format(content, Some("test.sam")),
            Ok(FileFormat::Sam)
        );
        assert_eq!(
            detect_format(content, Some("test.dict")),
            Ok(FileFormat::Sam)
        ); // Content overrides filename
        assert_eq!(detect_format(content, None), Ok(FileFormat::Sam));
    }

    #[test]
    fn test_parse_binary_file_bam_from_bytes() {
        use noodles::bam;
        use noodles::sam;
        use noodles::sam::header::record::value::map::{Map, ReferenceSequence};
        use std::num::NonZeroUsize;

        let header = sam::Header::builder()
            .add_reference_sequence(
                "chr1",
                Map::<ReferenceSequence>::new(NonZeroUsize::new(248_956_422).unwrap()),
            )
            .build();
        let mut bam_bytes = Vec::new();
        {
            let mut writer = bam::io::Writer::new(&mut bam_bytes);
            writer.write_header(&header).unwrap();
        }

        let query = parse_binary_file(&bam_bytes, FileFormat::Bam).unwrap();
        assert_eq!(query.contigs.len(), 1);
        assert_eq!(query.contigs[0].name, "chr1");
        assert_eq!(query.contigs[0].length, 248_956_422);
    }

    /// Helper: build a minimal BAM byte buffer from a SAM header string.
    fn build_bam_bytes(header_text: &str) -> Vec<u8> {
        use noodles::bam;
        use noodles::sam;

        let mut reader = sam::io::Reader::new(header_text.as_bytes());
        let header = reader.read_header().unwrap();

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

    #[test]
    fn test_parse_binary_file_from_path_bam() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let bam_bytes = build_bam_bytes(
            "@HD\tVN:1.6\n@SQ\tSN:chr1\tLN:248956422\n@SQ\tSN:chr2\tLN:242193529\n",
        );

        let mut temp = NamedTempFile::with_suffix(".bam").unwrap();
        temp.write_all(&bam_bytes).unwrap();

        let result = parse_binary_file_from_path(temp.path(), FileFormat::Bam);
        assert!(result.is_ok());
        let query = result.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_binary_file_from_path_truncated_bam() {
        // Verify that parsing works on a BAM file truncated after the header.
        // This simulates the server-side streaming behavior where only the first
        // N bytes of a large BAM are written to a temp file.
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut bam_bytes = build_bam_bytes("@HD\tVN:1.6\n@SQ\tSN:chr1\tLN:248956422\n");

        // Append junk data to simulate a truncated file (records cut off mid-stream)
        bam_bytes.extend_from_slice(&[0u8; 1024]);

        let mut temp = NamedTempFile::with_suffix(".bam").unwrap();
        temp.write_all(&bam_bytes).unwrap();

        let result = parse_binary_file_from_path(temp.path(), FileFormat::Bam);
        assert!(result.is_ok());
        let query = result.unwrap();
        assert_eq!(query.contigs.len(), 1);
        assert_eq!(query.contigs[0].name, "chr1");
    }

    #[test]
    fn test_parse_binary_file_from_path_unsupported_format() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut temp = NamedTempFile::with_suffix(".txt").unwrap();
        temp.write_all(b"not a binary file").unwrap();

        let result = parse_binary_file_from_path(temp.path(), FileFormat::Sam);
        assert!(result.is_err());
    }
}