gtars-refget 0.11.0

Rust implementation of the refget standard for accessing reference sequences
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
//! FASTA export and BED region extraction for RefgetStore.

use super::*;
use super::readonly::ReadonlyRefgetStore;

use std::ffi::OsStr;

use indexmap::IndexMap;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

use anyhow::{anyhow, Context, Result};
use flate2::Compression;
use flate2::write::GzEncoder;
use flate2::read::MultiGzDecoder;
use gtars_core::utils::get_file_info;

use crate::digest::{
    SequenceMetadata, SequenceRecord,
    decode_substring_from_bytes, lookup_alphabet,
};
use crate::hashkeyable::HashKeyable;


// ============================================================================
// Free function
// ============================================================================

/// Helper function to decode a sequence record and write it as a FASTA entry.
///
/// Handles decoding (encoded or raw storage modes), header formatting (with optional
/// description), and sequence output.
///
/// `line_width` controls sequence wrapping: a value `> 0` wraps the sequence body at
/// that many bases per line; a value of `0` disables wrapping entirely, emitting the
/// whole sequence on a single line (one-sequence-per-line, as GGCAT/SSHash expect).
pub(crate) fn write_fasta_record(
    writer: &mut dyn Write,
    metadata: &SequenceMetadata,
    sequence_data: &[u8],
    mode: StorageMode,
    line_width: usize,
) -> Result<()> {
    let decoded_sequence = match mode {
        StorageMode::Encoded => {
            let alphabet = lookup_alphabet(&metadata.alphabet);
            let decoded =
                decode_substring_from_bytes(sequence_data, 0, metadata.length, alphabet);
            String::from_utf8(decoded).context("Failed to decode sequence as UTF-8")?
        }
        StorageMode::Raw => String::from_utf8(sequence_data.to_vec())
            .context("Failed to decode raw sequence as UTF-8")?,
        StorageMode::Zstd => {
            let ascii = zstd::decode_all(sequence_data)
                .context("Failed to zstd-decompress sequence record")?;
            String::from_utf8(ascii).context("Failed to decode sequence as UTF-8")?
        }
    };

    let header = match &metadata.description {
        Some(desc) => format!(">{} {}", metadata.name, desc),
        None => format!(">{}", metadata.name),
    };
    writeln!(writer, "{}", header)?;

    if line_width == 0 {
        // Unwrapped: emit the entire sequence on a single line. `chunks(0)` would
        // panic, so this branch is required, not merely an optimization.
        writer.write_all(decoded_sequence.as_bytes())?;
        writer.write_all(b"\n")?;
    } else {
        for chunk in decoded_sequence.as_bytes().chunks(line_width) {
            writer.write_all(chunk)?;
            writer.write_all(b"\n")?;
        }
    }

    Ok(())
}

// ============================================================================
// Shared per-region substring retrieval
// ============================================================================

/// Retrieve the substring for a single region, shared by the file-based and
/// vectors-based iterators.
///
/// Mutates the caller-owned `previous_parsed_chr` / `current_seq_digest` cache
/// so the consecutive-same-chrom optimization works across either path.
/// `region_label` (e.g. `"Line 5"` or `"Region 5"`) is interpolated into error
/// messages.
#[allow(clippy::too_many_arguments)]
fn retrieve_substring_for_region<K: AsRef<[u8]>>(
    store: &ReadonlyRefgetStore,
    collection_digest: &K,
    parsed_chr: String,
    parsed_start: i64,
    parsed_end: i64,
    previous_parsed_chr: &mut String,
    current_seq_digest: &mut String,
    region_label: &str,
) -> Result<RetrievedSequence> {
    if parsed_start < 0 || parsed_end < 0 {
        return Err(anyhow!(
            "{} has invalid start or end coordinates: start={}, end={}",
            region_label,
            parsed_start,
            parsed_end
        ));
    }

    if *previous_parsed_chr != parsed_chr {
        *previous_parsed_chr = parsed_chr.clone();

        let result = store
            .get_sequence_by_name(collection_digest, &parsed_chr)
            .map_err(|e| {
                anyhow!(
                    "{}: sequence '{}' not found in collection '{}': {}",
                    region_label,
                    parsed_chr,
                    String::from_utf8_lossy(collection_digest.as_ref()),
                    e
                )
            })?;

        *current_seq_digest = result.metadata().sha512t24u.clone();
    }

    let retrieved_substring = store
        .get_substring(&*current_seq_digest, parsed_start as usize, parsed_end as usize)
        .map_err(|e| {
            anyhow!(
                "{}: failed to get substring for digest '{}' from {} to {}: {}",
                region_label,
                current_seq_digest,
                parsed_start,
                parsed_end,
                e
            )
        })?;

    Ok(RetrievedSequence {
        sequence: retrieved_substring,
        chrom_name: parsed_chr,
        start: parsed_start as u32,
        end: parsed_end as u32,
    })
}

// ============================================================================
// SubstringsFromRegions Iterator impl
// ============================================================================

impl<K> Iterator for SubstringsFromRegions<'_, K>
where
    K: AsRef<[u8]>,
{
    type Item = Result<RetrievedSequence, anyhow::Error>;

    fn next(&mut self) -> Option<Self::Item> {
        use gtars_core::utils::parse_bedlike_file;

        let mut line_string = String::new();

        let num_bytes = self.reader.read_line(&mut line_string);
        match num_bytes {
            Ok(bytes) => {
                if bytes == 0 {
                    return None;
                }
            }
            Err(err) => return Some(Err(err.into())),
        };

        self.line_num += 1;

        let (parsed_chr, parsed_start, parsed_end) = match parse_bedlike_file(line_string.trim()) {
            Some(coords) => coords,
            None => {
                let err_str = format!(
                    "Error reading line {} because it could not be parsed as a BED-like entry: '{}'",
                    self.line_num + 1,
                    line_string
                );
                return Some(Err(anyhow!(err_str)));
            }
        };

        Some(retrieve_substring_for_region(
            self.store,
            &self.collection_digest,
            parsed_chr,
            parsed_start as i64,
            parsed_end as i64,
            &mut self.previous_parsed_chr,
            &mut self.current_seq_digest,
            &format!("Line {}", self.line_num + 1),
        ))
    }
}

// ============================================================================
// SubstringsFromRegionVectors Iterator impl
// ============================================================================

impl<K> Iterator for SubstringsFromRegionVectors<'_, K>
where
    K: AsRef<[u8]>,
{
    type Item = Result<RetrievedSequence, anyhow::Error>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.chroms.len() {
            return None;
        }
        let i = self.index;
        self.index += 1;

        let parsed_chr = self.chroms[i].clone();
        let parsed_start = self.starts[i] as i64;
        let parsed_end = self.ends[i] as i64;

        Some(retrieve_substring_for_region(
            self.store,
            &self.collection_digest,
            parsed_chr,
            parsed_start,
            parsed_end,
            &mut self.previous_parsed_chr,
            &mut self.current_seq_digest,
            &format!("Region {}", i + 1),
        ))
    }
}

// ============================================================================
// ReadonlyRefgetStore export methods
// ============================================================================

impl ReadonlyRefgetStore {
    /// Get an iterator over substrings defined by BED file regions.
    pub fn substrings_from_regions<'a, K: AsRef<[u8]>>(
        &'a self,
        collection_digest: K,
        bed_file_path: &str,
    ) -> Result<SubstringsFromRegions<'a, K>> {
        let path = Path::new(bed_file_path);
        let file_info = get_file_info(path);
        let is_gzipped = file_info.is_gzipped;

        let opened_bed_file = File::open(path)?;

        let reader: Box<dyn std::io::Read> = match is_gzipped {
            true => Box::new(MultiGzDecoder::new(std::io::BufReader::new(opened_bed_file))),
            false => Box::new(opened_bed_file),
        };
        let reader = std::io::BufReader::new(reader);

        Ok(SubstringsFromRegions {
            store: self,
            reader,
            collection_digest,
            previous_parsed_chr: String::new(),
            current_seq_digest: String::new(),
            line_num: 0,
        })
    }

    /// Get an iterator over substrings defined by in-memory region vectors.
    ///
    /// `chroms`, `starts`, and `ends` are parallel slices; element `i` defines
    /// region `chroms[i]:starts[i]-ends[i]`. Yields the same `RetrievedSequence`
    /// results as `substrings_from_regions` without any temp-file round-trip.
    pub fn substrings_from_region_vectors<'a, K, S>(
        &'a self,
        collection_digest: K,
        chroms: &[S],
        starts: &[u32],
        ends: &[u32],
    ) -> Result<SubstringsFromRegionVectors<'a, K>>
    where
        K: AsRef<[u8]>,
        S: AsRef<str>,
    {
        if chroms.len() != starts.len() || chroms.len() != ends.len() {
            return Err(anyhow!(
                "Mismatched region vector lengths: chroms={}, starts={}, ends={}",
                chroms.len(),
                starts.len(),
                ends.len()
            ));
        }

        Ok(SubstringsFromRegionVectors {
            store: self,
            collection_digest,
            chroms: chroms.iter().map(|c| c.as_ref().to_string()).collect(),
            starts: starts.to_vec(),
            ends: ends.to_vec(),
            index: 0,
            previous_parsed_chr: String::new(),
            current_seq_digest: String::new(),
        })
    }

    /// Export sequences from BED file regions to a FASTA file.
    ///
    /// Region export is inherently unwrapped: each region's sequence is written on a
    /// single line under a `>chrom:start-end` header, regardless of any line width.
    pub fn export_fasta_from_regions<K: AsRef<[u8]>>(
        &self,
        collection_digest: K,
        bed_file_path: &str,
        output_file_path: &str,
    ) -> Result<()> {
        let output_path_obj = Path::new(output_file_path);
        if let Some(parent) = output_path_obj.parent() {
            std::fs::create_dir_all(parent)?;
        }

        let file = File::create(output_file_path)?;

        let mut writer: Box<dyn Write> = if output_path_obj.extension() == Some(OsStr::new("gz")) {
            Box::new(GzEncoder::new(file, Compression::default()))
        } else {
            Box::new(file)
        };

        let seq_iter = self.substrings_from_regions(&collection_digest, bed_file_path)?;

        for rs in seq_iter.into_iter() {
            let rs = rs?;

            // Write one header per region with coordinates
            let header = format!(">{}:{}-{}\n", rs.chrom_name, rs.start, rs.end);
            writer.write_all(header.as_bytes())?;
            writer.write_all(rs.sequence.as_bytes())?;
            writer.write_all(b"\n")?;
        }

        writer.flush()?;

        Ok(())
    }

    /// Export sequences from a collection to a FASTA file.
    ///
    /// `line_width` controls sequence wrapping via the sentinel convention:
    /// `None` defaults to 80 bases per line; `Some(n)` with `n > 0` wraps at `n`;
    /// `Some(0)` disables wrapping, emitting one sequence per line (unwrapped) as
    /// GGCAT/SSHash and similar k-mer tooling expect.
    pub fn export_fasta<K: AsRef<[u8]>, P: AsRef<Path>>(
        &self,
        collection_digest: K,
        output_path: P,
        sequence_names: Option<Vec<&str>>,
        line_width: Option<usize>,
    ) -> Result<()> {
        let line_width = line_width.unwrap_or(80);
        let output_path = output_path.as_ref();
        let collection_key = collection_digest.as_ref().to_key();

        // Headers come from the collection's own per-sequence records so a
        // sequence shared across collections is exported under this collection's
        // name and description, not the label of whichever import came first.
        let records = self.collection_sequence_metadata(&collection_key).map_err(|e| {
            anyhow!(
                "Collection not found: {:?}: {}",
                String::from_utf8_lossy(collection_digest.as_ref()),
                e
            )
        })?;
        let name_to_meta: IndexMap<&str, &SequenceMetadata> = records
            .iter()
            .map(|r| (r.metadata().name.as_str(), r.metadata()))
            .collect();

        let names_to_export: Vec<&str> = if let Some(names) = sequence_names {
            names
        } else {
            name_to_meta.keys().copied().collect()
        };

        let file = File::create(output_path).context(format!(
            "Failed to create output file: {}",
            output_path.display()
        ))?;

        let mut writer: Box<dyn Write> = if output_path.extension() == Some(OsStr::new("gz")) {
            Box::new(GzEncoder::new(BufWriter::new(file), Compression::default()))
        } else {
            Box::new(BufWriter::new(file))
        };

        for seq_name in names_to_export {
            let meta = *name_to_meta
                .get(seq_name)
                .ok_or_else(|| anyhow!("Sequence '{}' not found in collection", seq_name))?;
            let seq_digest = meta.sha512t24u.to_key();

            let record = self
                .sequence_store
                .get(&seq_digest)
                .ok_or_else(|| anyhow!("Sequence record not found for digest: {}", meta.sha512t24u))?;

            // Only the bytes come from the global record. `alphabet` and `length`
            // are content-derived and identical in the per-collection stub.
            let sequence_data: &[u8] = match record {
                SequenceRecord::Stub(_) => {
                    return Err(anyhow!("Sequence data not loaded for '{}'. Call load_sequence() or load_all_sequences() first.", seq_name));
                }
                SequenceRecord::Full { sequence, .. } => sequence.as_slice(),
            };

            write_fasta_record(&mut *writer, meta, sequence_data, self.mode, line_width)?;
        }

        writer.flush()?;

        Ok(())
    }

    /// Export sequences by their sequence digests to a FASTA file.
    ///
    /// There is no collection context here, so each header uses the store-wide
    /// record's name and description (first import wins for a sequence shared by
    /// several collections). Use [`Self::export_fasta`] for collection-specific names.
    ///
    /// `line_width` follows the same sentinel convention as [`Self::export_fasta`]:
    /// `None` defaults to 80; `Some(n)` with `n > 0` wraps at `n`; `Some(0)` disables
    /// wrapping, emitting one sequence per line (unwrapped).
    pub fn export_fasta_by_digests<P: AsRef<Path>>(
        &self,
        seq_digests: Vec<&str>,
        output_path: P,
        line_width: Option<usize>,
    ) -> Result<()> {
        let line_width = line_width.unwrap_or(80);
        let output_path = output_path.as_ref();

        let file = File::create(output_path).context(format!(
            "Failed to create output file: {}",
            output_path.display()
        ))?;

        let mut writer: Box<dyn Write> = if output_path.extension() == Some(OsStr::new("gz")) {
            Box::new(GzEncoder::new(BufWriter::new(file), Compression::default()))
        } else {
            Box::new(BufWriter::new(file))
        };

        for digest_str in seq_digests {
            let digest_key = digest_str.as_bytes().to_key();

            let record = self
                .sequence_store
                .get(&digest_key)
                .ok_or_else(|| anyhow!("Sequence record not found for digest: {}", digest_str))?;

            let (metadata, sequence_data): (&SequenceMetadata, &[u8]) = match record {
                SequenceRecord::Stub(_) => {
                    return Err(anyhow!(
                        "Sequence data not loaded for digest: {}",
                        digest_str
                    ));
                }
                SequenceRecord::Full { metadata, sequence } => (metadata, sequence.as_slice()),
            };

            write_fasta_record(&mut *writer, metadata, sequence_data, self.mode, line_width)?;
        }

        writer.flush()?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::store::{FastaImportOptions, RefgetStore};
    use std::io::Write;
    use tempfile::{NamedTempFile, TempDir};

    /// Build a small in-memory store from a temp FASTA and return it plus the
    /// digest of the single collection created.
    fn build_store() -> (RefgetStore, String) {
        let mut fasta = NamedTempFile::new().expect("create temp fasta");
        // A sequence longer than 80 bases so the wrapped export spans >1 line.
        writeln!(fasta, ">seq1").unwrap();
        writeln!(fasta, "{}", "ACGT".repeat(30)).unwrap(); // 120 bases
        writeln!(fasta, ">seq2").unwrap();
        writeln!(fasta, "TTGGCCAA").unwrap();
        fasta.flush().unwrap();

        let mut store = RefgetStore::in_memory();
        store
            .add_sequence_collection_from_fasta(fasta.path(), FastaImportOptions::new())
            .expect("import fasta");

        let collections = store.list_collections(0, usize::MAX, &[]).unwrap();
        let digest = collections.results[0].digest.clone();
        (store, digest)
    }

    /// Two single-record FASTAs with byte-identical sequences but different
    /// headers: `>chr1 ucsc desc` and `>1 flybase desc`. Mirrors the UCSC vs
    /// FlyBase situation from issue #270.
    fn shared_sequence_fastas() -> (NamedTempFile, NamedTempFile) {
        let seq = "ACGT".repeat(10);
        let mut a = NamedTempFile::new().expect("create temp fasta A");
        writeln!(a, ">chr1 ucsc desc").unwrap();
        writeln!(a, "{}", seq).unwrap();
        a.flush().unwrap();

        let mut b = NamedTempFile::new().expect("create temp fasta B");
        writeln!(b, ">1 flybase desc").unwrap();
        writeln!(b, "{}", seq).unwrap();
        b.flush().unwrap();
        (a, b)
    }

    /// Import `first` then `second` into `store`; return their collection digests.
    fn import_pair(store: &mut RefgetStore, first: &NamedTempFile, second: &NamedTempFile) -> (String, String) {
        let (meta_first, _) = store
            .add_sequence_collection_from_fasta(first.path(), FastaImportOptions::new())
            .expect("import first fasta");
        let (meta_second, _) = store
            .add_sequence_collection_from_fasta(second.path(), FastaImportOptions::new())
            .expect("import second fasta");
        (meta_first.digest, meta_second.digest)
    }

    /// Export a collection unwrapped and return the headers in order.
    fn export_headers(store: &crate::store::ReadonlyRefgetStore, digest: &str, dir: &TempDir) -> Vec<String> {
        let out = dir.path().join(format!("{}.fa", digest));
        store
            .export_fasta(digest, &out, None, Some(0))
            .expect("export collection");
        let content = std::fs::read_to_string(&out).unwrap();
        records(&content).into_iter().map(|(h, _)| h).collect()
    }

    /// Split a FASTA into (header, body-lines) pairs.
    fn records(fasta: &str) -> Vec<(String, Vec<String>)> {
        let mut out: Vec<(String, Vec<String>)> = Vec::new();
        for line in fasta.lines() {
            if let Some(header) = line.strip_prefix('>') {
                out.push((header.to_string(), Vec::new()));
            } else if let Some(last) = out.last_mut() {
                last.1.push(line.to_string());
            }
        }
        out
    }

    #[test]
    fn export_unwrapped_puts_each_sequence_on_one_line() {
        let (store, digest) = build_store();
        let readonly = store.into_readonly();

        let dir = TempDir::new().unwrap();
        let out = dir.path().join("unwrapped.fa");
        readonly
            .export_fasta(&digest, &out, None, Some(0))
            .expect("export unwrapped");

        let content = std::fs::read_to_string(&out).unwrap();
        let recs = records(&content);
        assert_eq!(recs.len(), 2, "expected two records");
        for (header, body) in &recs {
            assert_eq!(
                body.len(),
                1,
                "record '{}' should have exactly one body line when unwrapped, got {:?}",
                header,
                body
            );
        }
        // seq1 body is the full 120 bases on a single line.
        assert_eq!(recs[0].1[0].len(), 120);
    }

    #[test]
    fn export_wrapped_at_80_splits_long_sequences() {
        let (store, digest) = build_store();
        let readonly = store.into_readonly();

        let dir = TempDir::new().unwrap();
        let out = dir.path().join("wrapped.fa");
        readonly
            .export_fasta(&digest, &out, None, Some(80))
            .expect("export wrapped");

        let content = std::fs::read_to_string(&out).unwrap();
        let recs = records(&content);
        // seq1 (120 bases) wraps to two lines at width 80; seq2 (8 bases) stays one.
        assert_eq!(recs[0].1.len(), 2, "120-base seq should wrap to 2 lines");
        assert_eq!(recs[0].1[0].len(), 80);
        assert_eq!(recs[0].1[1].len(), 40);
        assert_eq!(recs[1].1.len(), 1);
    }

    #[test]
    fn shared_sequence_exports_with_each_collections_own_header() {
        let (fasta_a, fasta_b) = shared_sequence_fastas();

        // Both import orders: the first-imported label must never leak into
        // the other collection's export.
        for swap in [false, true] {
            let mut store = RefgetStore::in_memory();
            let (digest_a, digest_b) = if swap {
                let (b, a) = import_pair(&mut store, &fasta_b, &fasta_a);
                (a, b)
            } else {
                import_pair(&mut store, &fasta_a, &fasta_b)
            };
            let readonly = store.into_readonly();
            let dir = TempDir::new().unwrap();

            assert_eq!(
                export_headers(&readonly, &digest_a, &dir),
                vec!["chr1 ucsc desc".to_string()],
                "swap={}",
                swap
            );
            assert_eq!(
                export_headers(&readonly, &digest_b, &dir),
                vec!["1 flybase desc".to_string()],
                "swap={}",
                swap
            );
        }
    }

    #[test]
    fn get_collection_returns_per_collection_description() {
        let (fasta_a, fasta_b) = shared_sequence_fastas();
        let mut store = RefgetStore::in_memory();
        let (digest_a, digest_b) = import_pair(&mut store, &fasta_a, &fasta_b);
        let readonly = store.into_readonly();

        let coll_b = readonly.get_collection(&digest_b).expect("get collection B");
        assert_eq!(coll_b.sequences.len(), 1);
        assert_eq!(coll_b.sequences[0].metadata().name, "1");
        assert_eq!(
            coll_b.sequences[0].metadata().description.as_deref(),
            Some("flybase desc")
        );

        let coll_a = readonly.get_collection(&digest_a).expect("get collection A");
        assert_eq!(coll_a.sequences[0].metadata().name, "chr1");
        assert_eq!(
            coll_a.sequences[0].metadata().description.as_deref(),
            Some("ucsc desc")
        );
    }

    #[test]
    fn shared_sequence_disk_roundtrip_exports_own_header() {
        let (fasta_a, fasta_b) = shared_sequence_fastas();
        let store_dir = TempDir::new().unwrap();

        let (digest_a, digest_b) = {
            let mut store = RefgetStore::on_disk(store_dir.path()).expect("create disk store");
            import_pair(&mut store, &fasta_a, &fasta_b)
        };

        // Reopen so collection stubs come from collections/<digest>.rgsi via
        // ensure_collection_loaded, not from the import-time in-memory records.
        let mut store = RefgetStore::open_local(store_dir.path()).expect("reopen store");
        store.load_collection(&digest_b).expect("load collection B");
        store.load_collection(&digest_a).expect("load collection A");
        store.load_all_sequences().expect("load sequences");
        let readonly = store.into_readonly();

        let dir = TempDir::new().unwrap();
        assert_eq!(
            export_headers(&readonly, &digest_b, &dir),
            vec!["1 flybase desc".to_string()]
        );
        assert_eq!(
            export_headers(&readonly, &digest_a, &dir),
            vec!["chr1 ucsc desc".to_string()]
        );

        let coll_b = readonly.get_collection(&digest_b).unwrap();
        assert_eq!(
            coll_b.sequences[0].metadata().description.as_deref(),
            Some("flybase desc")
        );
    }

    #[test]
    fn get_sequence_by_name_returns_per_collection_metadata() {
        let (fasta_a, fasta_b) = shared_sequence_fastas();

        // Both import orders: the first-imported label must never leak into
        // the other collection's get_sequence_by_name() result.
        for swap in [false, true] {
            let mut store = RefgetStore::in_memory();
            let (digest_a, digest_b) = if swap {
                let (b, a) = import_pair(&mut store, &fasta_b, &fasta_a);
                (a, b)
            } else {
                import_pair(&mut store, &fasta_a, &fasta_b)
            };
            let readonly = store.into_readonly();

            let rec_b = readonly
                .get_sequence_by_name(&digest_b, "1")
                .expect("get sequence '1' from collection B");
            assert_eq!(rec_b.metadata().name, "1", "swap={}", swap);
            assert_eq!(
                rec_b.metadata().description.as_deref(),
                Some("flybase desc"),
                "swap={}",
                swap
            );

            let rec_a = readonly
                .get_sequence_by_name(&digest_a, "chr1")
                .expect("get sequence 'chr1' from collection A");
            assert_eq!(rec_a.metadata().name, "chr1", "swap={}", swap);
            assert_eq!(
                rec_a.metadata().description.as_deref(),
                Some("ucsc desc"),
                "swap={}",
                swap
            );

            // Same underlying bytes, shared across both collections.
            assert_eq!(
                rec_a.metadata().sha512t24u,
                rec_b.metadata().sha512t24u,
                "swap={}",
                swap
            );

            // Names do not leak across collections.
            assert!(
                readonly.get_sequence_by_name(&digest_b, "chr1").is_err(),
                "swap={}",
                swap
            );
        }
    }

    #[test]
    fn get_sequence_by_name_disk_roundtrip() {
        let (fasta_a, fasta_b) = shared_sequence_fastas();
        let store_dir = TempDir::new().unwrap();

        let (digest_a, digest_b) = {
            let mut store = RefgetStore::on_disk(store_dir.path()).expect("create disk store");
            import_pair(&mut store, &fasta_a, &fasta_b)
        };

        // Reopen so collection stubs come from collections/<digest>.rgsi via
        // ensure_collection_loaded, not from the import-time in-memory records.
        let mut store = RefgetStore::open_local(store_dir.path()).expect("reopen store");
        store.load_collection(&digest_b).expect("load collection B");
        store.load_collection(&digest_a).expect("load collection A");
        store.load_all_sequences().expect("load sequences");
        let readonly = store.into_readonly();

        let rec_b = readonly
            .get_sequence_by_name(&digest_b, "1")
            .expect("get sequence '1' from collection B");
        assert_eq!(rec_b.metadata().name, "1");
        assert_eq!(rec_b.metadata().description.as_deref(), Some("flybase desc"));
        assert!(rec_b.is_loaded());
        assert_eq!(rec_b.decode().unwrap(), "ACGT".repeat(10));

        let rec_a = readonly
            .get_sequence_by_name(&digest_a, "chr1")
            .expect("get sequence 'chr1' from collection A");
        assert_eq!(rec_a.metadata().name, "chr1");
        assert_eq!(rec_a.metadata().description.as_deref(), Some("ucsc desc"));
        assert!(rec_a.is_loaded());
        assert_eq!(rec_a.decode().unwrap(), "ACGT".repeat(10));
    }
}