binseq 0.9.3

A high efficiency binary format for sequencing data
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
use std::io;

use zstd::zstd_safe;

use crate::{
    Result, SequencingRecord,
    cbq::core::{BlockHeader, ColumnarBlock, FileHeader, Index, IndexFooter, IndexHeader},
};

/// Writer for CBQ files operating on generic writers (streaming).
pub struct ColumnarBlockWriter<W: io::Write> {
    /// Internal writer for the block
    inner: W,

    /// A reusable block for this writer
    block: ColumnarBlock,

    /// All block headers written by this writer
    headers: Vec<BlockHeader>,

    /// Compression context for the thread
    cctx: zstd_safe::CCtx<'static>,
}
impl<W: io::Write + Clone> Clone for ColumnarBlockWriter<W> {
    fn clone(&self) -> Self {
        let mut writer = Self {
            inner: self.inner.clone(),
            block: self.block.clone(),
            headers: self.headers.clone(),
            cctx: zstd_safe::CCtx::create(),
        };
        writer
            .init_compressor()
            .expect("Failed to set compression level in writer clone");
        writer
    }
}
impl<W: io::Write> ColumnarBlockWriter<W> {
    /// Creates a new writer with the header written to the inner writer
    pub fn new(inner: W, header: FileHeader) -> Result<Self> {
        // Build the writer
        let mut writer = Self::new_headless(inner, header)?;

        // Ensure the header is written to the file
        writer.inner.write_all(header.as_bytes())?;

        Ok(writer)
    }

    /// Creates a new writer without writing the header to the inner writer
    pub fn new_headless(inner: W, header: FileHeader) -> Result<Self> {
        let mut writer = Self {
            inner,
            block: ColumnarBlock::new(header),
            headers: Vec::default(),
            cctx: zstd_safe::CCtx::create(),
        };

        // Set the compression level for this writer
        writer.init_compressor()?;

        Ok(writer)
    }

    /// Sets the compression level for Writer
    ///
    /// Note: only used on init, shouldn't be set by the user
    fn init_compressor(&mut self) -> Result<()> {
        // Initialize the compressor with the compression level
        self.cctx
            .set_parameter(zstd_safe::CParameter::CompressionLevel(
                self.block.header.compression_level as i32,
            ))
            .map_err(|e| io::Error::other(zstd_safe::get_error_name(e)))?;

        // Set long distance matching
        self.cctx
            .set_parameter(zstd_safe::CParameter::EnableLongDistanceMatching(true))
            .map_err(|e| io::Error::other(zstd_safe::get_error_name(e)))?;
        Ok(())
    }

    pub fn header(&self) -> FileHeader {
        self.block.header
    }

    /// Calculate the usage of the block as a percentage
    pub fn usage(&self) -> f64 {
        self.block.usage()
    }

    /// Push a record to the writer
    ///
    /// Returns `Ok(true)` if the record was written successfully.
    /// CBQ handles N's explicitly in its encoding, so records are never skipped.
    pub fn push(&mut self, record: SequencingRecord) -> Result<bool> {
        if !self.block.can_fit(&record) {
            self.flush()?;
        }
        self.block.push(record)?;
        Ok(true)
    }

    pub fn flush(&mut self) -> Result<()> {
        if let Some(header) = self.block.flush_to(&mut self.inner, &mut self.cctx)? {
            self.headers.push(header);
        }
        Ok(())
    }

    pub fn finish(&mut self) -> Result<()> {
        self.flush()?;
        self.write_index()?;
        Ok(())
    }

    fn write_index(&mut self) -> Result<()> {
        let index = Index::from_block_headers(&self.headers);
        let z_index = index.encoded()?;
        let header = IndexHeader::new(index.size(), z_index.len() as u64);
        let footer = IndexFooter::new(z_index.len() as u64);

        // Write the index to the inner writer
        {
            self.inner.write_all(header.as_bytes())?;
            self.inner.write_all(&z_index)?;
            self.inner.write_all(footer.as_bytes())?;
        }
        Ok(())
    }

    /// Ingest only the *completed* (already-compressed) blocks from `other`.
    ///
    /// Unlike [`ingest`](Self::ingest), this never touches either writer's
    /// incomplete block, so it performs no zstd compression. The work done
    /// under a global lock is reduced to a `write_all` of pre-compressed bytes
    /// plus a header copy — compression has already been paid for on the worker
    /// thread when `other`'s blocks were flushed in `push`.
    ///
    /// `other` keeps building its incomplete block across calls; only its
    /// completed-block buffer and headers are drained.
    pub fn ingest_completed(&mut self, other: &mut ColumnarBlockWriter<Vec<u8>>) -> Result<()> {
        if other.headers.is_empty() {
            return Ok(()); // short-circuit
        }

        // Write all completed blocks from the other
        self.inner.write_all(other.inner_data())?;

        // Take all headers from the other
        self.headers.extend_from_slice(&other.headers);

        // Clear only the drained completed-block state, leaving the incomplete
        // block intact so the worker thread can keep accumulating into it.
        other.clear_completed_data();

        Ok(())
    }

    /// Ingests only the *incomplete* (non-compressed) blocks from the `other`.
    ///
    /// This should not be used in isolation and should be handled from the [`ingest`](Self::ingest) API only
    /// to avoid any mistakes.
    ///
    /// [`ingest_completed`](Self::ingest_completed) should always be called first.
    fn ingest_incompleted(&mut self, other: &mut ColumnarBlockWriter<Vec<u8>>) -> Result<()> {
        if other.block.num_records == 0 {
            return Ok(()); // short-circuit
        }

        // Attempt to ingest the incomplete block from the other
        if !self.block.can_ingest(&other.block) {
            // Make space by flushing the current block
            // Then ingest the incomplete block from the other
            self.flush()?;
        }
        self.block.take_incomplete(&other.block)?;

        // clear the drained incomplete-block state
        other.clear_incomplete_data();

        Ok(())
    }

    pub fn ingest(&mut self, other: &mut ColumnarBlockWriter<Vec<u8>>) -> Result<()> {
        self.ingest_completed(other)?;
        self.ingest_incompleted(other)?;
        Ok(())
    }
}

/// Specialized implementation when using a local `Vec<u8>` as the inner data structure
impl ColumnarBlockWriter<Vec<u8>> {
    #[must_use]
    pub fn inner_data(&self) -> &[u8] {
        &self.inner
    }

    /// Clears only the completed-block state (compressed bytes + headers),
    /// leaving the incomplete block intact.
    ///
    /// Used by [`ingest_completed`](ColumnarBlockWriter::ingest_completed) so a
    /// worker thread can keep accumulating records into its in-progress block
    /// across batches.
    pub fn clear_completed_data(&mut self) {
        self.inner.clear();
        self.headers.clear();
    }

    /// Clears the incomplete-block state
    pub fn clear_incomplete_data(&mut self) {
        self.block.clear();
    }

    /// Returns the number of bytes written to the inner data structure
    #[must_use]
    pub fn bytes_written(&self) -> usize {
        self.inner.len()
    }
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use super::*;
    use crate::{
        BinseqRecord, SequencingRecordBuilder,
        cbq::{BlockRange, Reader, core::FileHeaderBuilder},
    };

    /// Build a `FileHeader` for sequence-only records with the given block size.
    fn header(block_size: usize) -> FileHeader {
        FileHeaderBuilder::default()
            .is_paired(false)
            .with_headers(false)
            .with_qualities(false)
            .with_flags(false)
            .with_block_size(block_size)
            .build()
    }

    /// Build a sequence-only record from a sequence slice.
    fn record(seq: &[u8]) -> SequencingRecord<'_> {
        SequencingRecordBuilder::default()
            .s_seq(seq)
            .build()
            .expect("failed to build record")
    }

    /// Read every sequence back from a finished CBQ byte buffer.
    fn read_all_sequences(bytes: Vec<u8>) -> Vec<Vec<u8>> {
        let mut reader = Reader::new(Cursor::new(bytes)).expect("failed to open reader");
        let mut out = Vec::new();
        let mut cumulative = 0u64;
        while let Some(block_header) = reader.read_block().expect("failed to read block") {
            cumulative += block_header.num_records;
            // `read_block` only loads the compressed columns; decode them before iterating.
            reader
                .block
                .decompress_columns()
                .expect("failed to decompress block");
            let range = BlockRange::new(0, cumulative);
            for rec in reader.block.iter_records(range) {
                out.push(rec.sseq().to_vec());
            }
        }
        out
    }

    /// 64 distinct fixed-length sequences (each unique by index).
    fn sample_sequences() -> Vec<Vec<u8>> {
        const BASES: [u8; 4] = [b'A', b'C', b'G', b'T'];
        (0..64u32)
            .map(|i| {
                (0..40)
                    .map(|j| BASES[(i as usize + j) % 4])
                    .collect::<Vec<u8>>()
            })
            .collect()
    }

    /// `ingest_completed` must leave the source's incomplete block untouched
    /// while draining its completed blocks and headers.
    #[test]
    fn test_ingest_completed_preserves_incomplete_block() -> Result<()> {
        // Small block size so a handful of records fills multiple blocks.
        let block_size = 64;
        let mut global = ColumnarBlockWriter::new(Vec::new(), header(block_size))?;
        let mut local = ColumnarBlockWriter::new_headless(Vec::new(), header(block_size))?;

        // Push enough records to flush several completed blocks plus a partial tail.
        let seqs = sample_sequences();
        for seq in &seqs {
            local.push(record(seq))?;
        }

        // There must be both completed blocks (compressed bytes) and an
        // in-progress incomplete block at this point.
        assert!(
            !local.inner_data().is_empty(),
            "expected completed blocks before ingest_completed"
        );
        assert!(
            local.block.num_records > 0,
            "expected a non-empty incomplete block before ingest_completed"
        );

        let incomplete_records_before = local.block.num_records;
        let completed_headers = local.headers.len();

        global.ingest_completed(&mut local)?;

        // The completed-block buffer and headers are drained from the source...
        assert!(
            local.inner_data().is_empty(),
            "completed bytes should be drained"
        );
        assert!(local.headers.is_empty(), "headers should be drained");

        // ...but the incomplete block is left intact for further accumulation.
        assert_eq!(
            local.block.num_records, incomplete_records_before,
            "incomplete block must be preserved across ingest_completed"
        );

        // The global writer received exactly the completed headers (no flush of
        // its own empty incomplete block).
        assert_eq!(global.headers.len(), completed_headers);
        assert_eq!(
            global.block.num_records, 0,
            "ingest_completed must not touch the global incomplete block"
        );

        Ok(())
    }

    /// The parallel pattern (`ingest_completed` per batch, then a final full
    /// `ingest`) must round-trip every record in order.
    #[test]
    fn test_batched_ingest_completed_then_finish_roundtrips() -> Result<()> {
        let block_size = 64;
        let mut global = ColumnarBlockWriter::new(Vec::new(), header(block_size))?;
        let mut local = ColumnarBlockWriter::new_headless(Vec::new(), header(block_size))?;

        let seqs = sample_sequences();

        // Simulate batches: push a chunk, drain completed blocks, repeat.
        for chunk in seqs.chunks(7) {
            for seq in chunk {
                local.push(record(seq))?;
            }
            global.ingest_completed(&mut local)?;
        }

        // Final thread completion: drain the residual incomplete block.
        global.ingest(&mut local)?;
        global.finish()?;

        // The source has been fully drained.
        assert!(local.inner_data().is_empty());
        assert_eq!(local.block.num_records, 0);

        let read_back = read_all_sequences(global.inner);
        assert_eq!(read_back, seqs, "round-trip mismatch after batched ingest");

        Ok(())
    }

    /// A single full `ingest` (no prior `ingest_completed`) must round-trip.
    #[test]
    fn test_full_ingest_roundtrips() -> Result<()> {
        let block_size = 64;
        let mut global = ColumnarBlockWriter::new(Vec::new(), header(block_size))?;
        let mut local = ColumnarBlockWriter::new_headless(Vec::new(), header(block_size))?;

        let seqs = sample_sequences();
        for seq in &seqs {
            local.push(record(seq))?;
        }

        global.ingest(&mut local)?;
        global.finish()?;

        let read_back = read_all_sequences(global.inner);
        assert_eq!(read_back, seqs);

        Ok(())
    }

    /// Multiple local writers draining into one global writer (as distinct
    /// threads would) must preserve every record without loss or duplication.
    ///
    /// Note: input *order* is not preserved across independent locals. When one
    /// local's full `ingest` leaves a tail in the global incomplete block, a
    /// later local's `ingest_completed` writes its completed blocks ahead of
    /// that buffered tail. The guarantee is multiset equality, not sequence
    /// equality — which matches real parallel writing, where threads merge into
    /// the global in lock-acquisition order rather than input order.
    #[test]
    fn test_multiple_locals_ingest_into_global() -> Result<()> {
        let block_size = 64;
        let mut global = ColumnarBlockWriter::new(Vec::new(), header(block_size))?;

        let seqs = sample_sequences();
        let mut expected = Vec::new();

        // Three "threads", each owning a third of the records.
        for group in seqs.chunks(seqs.len().div_ceil(3)) {
            let mut local = ColumnarBlockWriter::new_headless(Vec::new(), header(block_size))?;
            for seq in group {
                local.push(record(seq))?;
                expected.push(seq.clone());
            }
            // per-batch drain followed by a thread-final full ingest
            global.ingest_completed(&mut local)?;
            global.ingest(&mut local)?;
        }

        global.finish()?;

        let mut read_back = read_all_sequences(global.inner);
        read_back.sort();
        expected.sort();
        assert_eq!(read_back, expected, "records lost or duplicated on merge");

        Ok(())
    }

    /// Sequences containing `N`s, which drive the Elias-Fano `npos` column.
    /// Blocks with no `N`s at all never populate `z_npos`, so a suite built
    /// only from `sample_sequences` (all ACGT) never exercises this path.
    fn sample_sequences_with_n(n_seq: usize, seq_len: usize) -> Vec<Vec<u8>> {
        const BASES: [u8; 4] = [b'A', b'C', b'G', b'T'];
        (0..n_seq)
            .map(|i| {
                (0..seq_len)
                    .map(|j| {
                        if i % 5 == 0 {
                            b'N'
                        } else {
                            BASES[(i as usize + j) % 4]
                        }
                    })
                    .collect::<Vec<u8>>()
            })
            .collect()
    }

    /// Round-trips sequences containing `N`s through a `ColumnarBlockWriter`
    /// and back through a `Reader`, exercising the Elias-Fano `npos`
    /// decompression path in [`ColumnarBlock::decompress_columns`].
    #[test]
    fn test_roundtrip_sequences_with_n() -> Result<()> {
        // Small block size so N-bearing sequences span multiple blocks.
        let block_size = 256;
        let mut writer = ColumnarBlockWriter::new(Vec::new(), header(block_size))?;

        let seqs = sample_sequences_with_n(1024, 100);
        assert!(
            seqs.iter().any(|s| s.contains(&b'N')),
            "test fixture must actually contain N's to exercise npos"
        );
        for seq in &seqs {
            writer.push(record(seq))?;
        }
        writer.finish()?;

        let read_back = read_all_sequences(writer.inner);
        assert_eq!(
            read_back, seqs,
            "round-trip mismatch for N-bearing sequences"
        );

        Ok(())
    }

    /// `ingest_completed` on a source with no completed blocks is a no-op for
    /// the global writer and preserves the source's incomplete block.
    #[test]
    fn test_ingest_completed_no_completed_blocks() -> Result<()> {
        // Large block size so a few small records never fill a block.
        let block_size = 1 << 20;
        let mut global = ColumnarBlockWriter::new(Vec::new(), header(block_size))?;
        let mut local = ColumnarBlockWriter::new_headless(Vec::new(), header(block_size))?;

        local.push(record(b"ACGTACGTACGT"))?;
        assert!(
            local.inner_data().is_empty(),
            "no block should have flushed"
        );

        let records_before = local.block.num_records;
        global.ingest_completed(&mut local)?;

        assert_eq!(local.block.num_records, records_before);
        assert!(global.headers.is_empty());
        assert_eq!(global.block.num_records, 0);

        Ok(())
    }
}