gtars-refget 0.9.1

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
//! 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::{DigestKey, 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 line-wrapped sequence output.
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")?,
    };

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

    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.
    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.
    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();

        let name_to_digest: IndexMap<String, DigestKey> = self
            .name_lookup
            .get(&collection_key)
            .ok_or_else(|| {
                anyhow!(
                    "Collection not found: {:?}",
                    String::from_utf8_lossy(collection_digest.as_ref())
                )
            })?
            .clone();

        let names_to_export: Vec<String> = if let Some(names) = sequence_names {
            names.iter().map(|s| s.to_string()).collect()
        } else {
            name_to_digest.keys().cloned().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 seq_digest = name_to_digest
                .get(&seq_name)
                .ok_or_else(|| anyhow!("Sequence '{}' not found in collection", seq_name))?;

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

            let (metadata, sequence_data): (&SequenceMetadata, &[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 { metadata, sequence } => (metadata, sequence.as_slice()),
            };

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

        writer.flush()?;

        Ok(())
    }

    /// Export sequences by their sequence digests to a FASTA file.
    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(())
    }
}