gtars-refget 0.9.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
//! Streaming decoder for bit-packed encoded sequences - WASM-safe.
//!
//! Unlike [`decode_substring_from_bytes`](super::encoder::decode_substring_from_bytes),
//! which requires the entire encoded byte slice to be buffered in memory,
//! [`StreamingDecoder`] consumes an [`std::io::Read`] source incrementally and
//! emits decoded ASCII bases via its own `Read` implementation. Only a tiny
//! bit buffer (`u64`) of internal state is used, so memory is bounded regardless
//! of sequence length.
//!
//! The decoder relies on the supplied [`Alphabet`]'s `decoding_array`, so `N`
//! and other ambiguity codes are handled identically to the in-memory path.
//!
//! # Bit ordering
//!
//! Matches the encoder: MSB-first within each byte (see
//! [`encode_sequence`](super::encoder::encode_sequence)).
//!
//! # Non-byte-aligned windows
//!
//! For alphabets where `bits_per_symbol` does not divide 8 (2-bit, 3-bit, 5-bit),
//! a requested `[start, end)` base window typically does not land on byte
//! boundaries. Callers should:
//!
//! 1. Feed the decoder the exact byte range
//!    `[floor(start * bps / 8), ceil(end * bps / 8))`.
//! 2. Pass `leading_skip_bits = (start * bps) mod 8` — the number of *bits* at
//!    the very front of the byte window that precede the requested base window
//!    (0..=7). The decoder consumes and discards them before emitting.
//! 3. Pass `bases_to_emit = end - start`.
//!
//! For the Raw / ASCII case (`bits_per_symbol == 8`), byte and base boundaries
//! coincide; pass `leading_skip = 0`.

use std::io::{self, Read};

use super::alphabet::Alphabet;

/// Streaming decoder for bit-packed encoded sequences.
///
/// Wraps any `impl Read` source and implements `Read` itself, emitting
/// decoded ASCII bases. Internal state is O(bits_per_symbol) — just a
/// 64-bit bit buffer plus a handful of counters.
pub struct StreamingDecoder<R: Read> {
    inner: R,
    alphabet: &'static Alphabet,
    bits_per_symbol: u8,
    bases_remaining: u64,
    /// Number of bits to discard at the front of the byte stream (0..=7).
    /// Consumed on the first `read` call.
    leading_skip_bits: u8,
    bit_buffer: u64,
    bit_buffer_len: u8,
}

impl<R: Read> StreamingDecoder<R> {
    /// Construct a new streaming decoder.
    ///
    /// - `inner` - Byte source providing the bit-packed encoded sequence.
    /// - `alphabet` - Alphabet whose `bits_per_symbol` and `decoding_array` drive decoding.
    /// - `leading_skip_bits` - Number of bits at the very start of the byte
    ///   stream to discard before the first emitted symbol. Must be `< 8`.
    ///   Should be `0` when `bits_per_symbol == 8`.
    /// - `bases_to_emit` - Total number of bases the decoder will emit via `Read`.
    pub fn new(
        inner: R,
        alphabet: &'static Alphabet,
        leading_skip_bits: u8,
        bases_to_emit: u64,
    ) -> Self {
        let bits_per_symbol = alphabet.bits_per_symbol as u8;
        debug_assert!(
            bits_per_symbol > 0 && bits_per_symbol <= 8,
            "StreamingDecoder supports 1..=8 bits per symbol, got {}",
            bits_per_symbol
        );
        debug_assert!(
            leading_skip_bits < 8,
            "leading_skip_bits must be < 8, got {}",
            leading_skip_bits
        );
        StreamingDecoder {
            inner,
            alphabet,
            bits_per_symbol,
            bases_remaining: bases_to_emit,
            leading_skip_bits,
            bit_buffer: 0,
            bit_buffer_len: 0,
        }
    }

    /// Ensure `bit_buffer` contains at least `min_bits` valid bits, reading
    /// more bytes from `inner` if needed. Returns `UnexpectedEof` if the
    /// source is exhausted before the requirement is met.
    fn refill(&mut self, min_bits: u8) -> io::Result<()> {
        while self.bit_buffer_len < min_bits {
            // Pull up to 6 bytes at a time when the bit buffer has room.
            // `bit_buffer` is u64 = 64 bits; keep headroom so we never
            // overflow when shifting in a full byte.
            let headroom_bits = 64u8.saturating_sub(self.bit_buffer_len);
            let max_bytes = (headroom_bits / 8) as usize;
            let want = max_bytes.clamp(1, 6);
            let mut tmp = [0u8; 6];
            let n = loop {
                match self.inner.read(&mut tmp[..want]) {
                    Ok(0) => {
                        return Err(io::Error::new(
                            io::ErrorKind::UnexpectedEof,
                            "StreamingDecoder: source ended before all bases were decoded",
                        ));
                    }
                    Ok(n) => break n,
                    Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
                    Err(e) => return Err(e),
                }
            };
            for &b in &tmp[..n] {
                self.bit_buffer = (self.bit_buffer << 8) | (b as u64);
                self.bit_buffer_len += 8;
            }
        }
        Ok(())
    }

    /// Pull the next decoded base from the stream, or `Ok(None)` if the
    /// configured base budget has been exhausted.
    fn next_symbol(&mut self) -> io::Result<Option<u8>> {
        if self.bases_remaining == 0 {
            return Ok(None);
        }
        self.refill(self.bits_per_symbol)?;

        let shift = self.bit_buffer_len - self.bits_per_symbol;
        let mask: u64 = if self.bits_per_symbol == 64 {
            u64::MAX
        } else {
            (1u64 << self.bits_per_symbol) - 1
        };
        let code = ((self.bit_buffer >> shift) & mask) as u8;
        self.bit_buffer_len -= self.bits_per_symbol;
        // Mask off consumed bits so they don't leak into later symbols.
        let keep_mask: u64 = if self.bit_buffer_len == 0 {
            0
        } else {
            (1u64 << self.bit_buffer_len) - 1
        };
        self.bit_buffer &= keep_mask;

        self.bases_remaining -= 1;
        Ok(Some(self.alphabet.decoding_array[code as usize]))
    }
}

impl<R: Read> Read for StreamingDecoder<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        // Discard any leading bits from the start of the byte stream (the
        // bits that preceded the requested base window within the first byte).
        if self.leading_skip_bits > 0 {
            self.refill(self.leading_skip_bits)?;
            // Consume (discard) the top `leading_skip_bits` of the buffer.
            self.bit_buffer_len -= self.leading_skip_bits;
            let keep_mask: u64 = if self.bit_buffer_len == 0 {
                0
            } else {
                (1u64 << self.bit_buffer_len) - 1
            };
            self.bit_buffer &= keep_mask;
            self.leading_skip_bits = 0;
        }

        let mut written = 0;
        while written < buf.len() {
            match self.next_symbol()? {
                Some(base) => {
                    buf[written] = base;
                    written += 1;
                }
                None => break,
            }
        }
        Ok(written)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::digest::alphabet::{
        ASCII_ALPHABET, DNA_2BIT_ALPHABET, DNA_3BIT_ALPHABET, DNA_IUPAC_ALPHABET, PROTEIN_ALPHABET,
    };
    use crate::digest::encoder::{decode_substring_from_bytes, encode_sequence};
    use std::io::Cursor;

    /// Compute the tight byte window and leading-skip for a `[start, end)`
    /// base range, matching what `RefgetStore::stream_sequence` will do.
    fn byte_window(
        start: usize,
        end: usize,
        bits_per_symbol: usize,
    ) -> (usize, usize, u8) {
        let start_bit = start * bits_per_symbol;
        let end_bit = end * bits_per_symbol;
        let byte_start = start_bit / 8;
        let byte_end = end_bit.div_ceil(8);
        let leading_skip_bits = (start_bit - byte_start * 8) as u8;
        (byte_start, byte_end, leading_skip_bits)
    }

    fn run_range_test(
        sequence: &[u8],
        alphabet: &'static Alphabet,
        start: usize,
        end: usize,
    ) {
        let encoded = encode_sequence(sequence, alphabet);
        let (byte_start, byte_end, leading_skip) =
            byte_window(start, end, alphabet.bits_per_symbol);
        let slice = &encoded[byte_start..byte_end.min(encoded.len())];

        let expected = decode_substring_from_bytes(&encoded, start, end, alphabet);

        let mut decoder = StreamingDecoder::new(
            Cursor::new(slice.to_vec()),
            alphabet,
            leading_skip,
            (end - start) as u64,
        );
        let mut out = Vec::new();
        decoder.read_to_end(&mut out).expect("streaming read failed");
        assert_eq!(
            out, expected,
            "alphabet bps={} range [{}, {}) sequence={:?}",
            alphabet.bits_per_symbol,
            start,
            end,
            std::str::from_utf8(sequence).unwrap_or("<non-utf8>")
        );
    }

    // Test inputs per alphabet. Each sequence is chosen to exercise the decoder
    // across the full symbol set where possible.
    fn fixtures() -> Vec<(&'static Alphabet, &'static [u8])> {
        vec![
            (&DNA_2BIT_ALPHABET, b"ACGTACGTACGTACGT" as &[u8]),
            (&DNA_3BIT_ALPHABET, b"ACGTNRYXACGTNRYX" as &[u8]),
            (&DNA_IUPAC_ALPHABET, b"ACGTRYMKSWBDHVN-" as &[u8]),
            (&PROTEIN_ALPHABET, b"ACDEFGHIKLMNPQRSTVWY*X-" as &[u8]),
            (&ASCII_ALPHABET, b"Hello, World! 1234" as &[u8]),
        ]
    }

    #[test]
    fn test_full_sequence() {
        for (alphabet, seq) in fixtures() {
            run_range_test(seq, alphabet, 0, seq.len());
        }
    }

    #[test]
    fn test_aligned_start_aligned_end() {
        // For 2-bit, 4 symbols per byte — [4, 12) is byte-aligned.
        run_range_test(
            b"ACGTACGTACGTACGT",
            &DNA_2BIT_ALPHABET,
            4,
            12,
        );
    }

    #[test]
    fn test_unaligned_start() {
        run_range_test(
            b"ACGTACGTACGTACGT",
            &DNA_2BIT_ALPHABET,
            1,
            16,
        );
    }

    #[test]
    fn test_unaligned_end() {
        run_range_test(
            b"ACGTACGTACGTACGT",
            &DNA_2BIT_ALPHABET,
            0,
            15,
        );
    }

    #[test]
    fn test_unaligned_both_3bit() {
        run_range_test(
            b"ACGTNRYXACGTNRYX",
            &DNA_3BIT_ALPHABET,
            1,
            7,
        );
    }

    #[test]
    fn test_zero_length() {
        // Zero-length windows should emit nothing regardless of alphabet or
        // leading_skip_bits — the bit-buffer never needs refilling.
        for (alphabet, seq) in fixtures() {
            let encoded = encode_sequence(seq, alphabet);
            // A zero-length window at offset 5 — the decoder should read
            // zero bytes and produce an empty output.
            let mut decoder = StreamingDecoder::new(
                Cursor::new(encoded.clone()),
                alphabet,
                0,
                0,
            );
            let mut out = Vec::new();
            decoder.read_to_end(&mut out).expect("zero-length read");
            assert!(out.is_empty());
        }
    }

    #[test]
    fn test_single_base() {
        for (alphabet, seq) in fixtures() {
            for start in 0..seq.len() {
                run_range_test(seq, alphabet, start, start + 1);
            }
        }
    }

    #[test]
    fn test_start_at_end_minus_one() {
        for (alphabet, seq) in fixtures() {
            run_range_test(seq, alphabet, seq.len() - 1, seq.len());
        }
    }

    #[test]
    fn test_read_small_buf() {
        let sequence = b"ACGTACGTACGTACGT";
        let alphabet = &DNA_2BIT_ALPHABET;
        let encoded = encode_sequence(sequence, alphabet);
        let (byte_start, byte_end, leading_skip) =
            byte_window(1, 15, alphabet.bits_per_symbol);
        let slice = &encoded[byte_start..byte_end];

        let mut decoder = StreamingDecoder::new(
            Cursor::new(slice.to_vec()),
            alphabet,
            leading_skip,
            (15 - 1) as u64,
        );

        // Read into a 3-byte buffer repeatedly.
        let mut small_buf = [0u8; 3];
        let mut collected = Vec::new();
        loop {
            let n = decoder.read(&mut small_buf).unwrap();
            if n == 0 {
                break;
            }
            collected.extend_from_slice(&small_buf[..n]);
        }

        let expected = decode_substring_from_bytes(&encoded, 1, 15, alphabet);
        assert_eq!(collected, expected);
    }

    #[test]
    fn test_protein_5bit_unaligned() {
        let sequence = b"ACDEFGHIKLMNPQRSTVWY*X-";
        let alphabet = &PROTEIN_ALPHABET;
        // Test starts that are non-byte-aligned at 5 bits per symbol.
        for start in 0..sequence.len() {
            for end in (start + 1)..=sequence.len() {
                run_range_test(sequence, alphabet, start, end);
            }
        }
    }

    #[test]
    fn test_ascii_passthrough() {
        let sequence = b"The quick brown fox";
        let alphabet = &ASCII_ALPHABET;
        run_range_test(sequence, alphabet, 0, sequence.len());
        run_range_test(sequence, alphabet, 4, 9);
    }

    #[test]
    fn test_unexpected_eof() {
        // Provide a truncated source and ask for more bases than fit.
        let sequence = b"ACGTACGT";
        let alphabet = &DNA_2BIT_ALPHABET;
        let encoded = encode_sequence(sequence, alphabet);
        // Only feed 1 byte (4 bases worth) but ask for 8 bases.
        let mut decoder = StreamingDecoder::new(
            Cursor::new(encoded[..1].to_vec()),
            alphabet,
            0,
            8,
        );
        let mut buf = [0u8; 16];
        let err = decoder.read(&mut buf).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::UnexpectedEof);
    }

    /// A reader that yields at most 1 byte per `read` call, simulating the
    /// worst-case partial-read behaviour of an unbuffered network source.
    struct TrickleReader<R: Read> {
        inner: R,
    }

    impl<R: Read> TrickleReader<R> {
        fn new(inner: R) -> Self {
            Self { inner }
        }
    }

    impl<R: Read> Read for TrickleReader<R> {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            if buf.is_empty() {
                return Ok(0);
            }
            // Only ever read 1 byte even when buf is larger.
            self.inner.read(&mut buf[..1])
        }
    }

    #[test]
    fn test_multi_byte_refill_with_trickle_reader() {
        // Verifies the multi-byte refill path handles partial reads (1 byte
        // per call) correctly — the decoder should produce identical output
        // whether the inner reader yields 1 byte or many at a time.
        for (alphabet, seq) in fixtures() {
            let encoded = encode_sequence(seq, alphabet);
            for start in 0..seq.len() {
                for end in (start + 1)..=seq.len() {
                    let (byte_start, byte_end, leading_skip) =
                        byte_window(start, end, alphabet.bits_per_symbol);
                    let slice = &encoded[byte_start..byte_end.min(encoded.len())];
                    let expected = decode_substring_from_bytes(&encoded, start, end, alphabet);

                    let trickle = TrickleReader::new(Cursor::new(slice.to_vec()));
                    let mut decoder = StreamingDecoder::new(
                        trickle,
                        alphabet,
                        leading_skip,
                        (end - start) as u64,
                    );
                    let mut out = Vec::new();
                    decoder
                        .read_to_end(&mut out)
                        .expect("trickle read failed");
                    assert_eq!(
                        out, expected,
                        "TrickleReader: bps={} range [{}, {}) alphabet",
                        alphabet.bits_per_symbol, start, end
                    );
                }
            }
        }
    }
}