binseq 0.9.0

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
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! Binary sequence writer module
//!
//! This module provides functionality for writing nucleotide sequences to binary files
//! in a compact 2-bit format. It includes support for:
//! - Single and paired sequence writing
//! - Invalid nucleotide handling with configurable policies
//! - Efficient buffering and encoding
//! - Headless mode for parallel writing

use std::io::{BufWriter, Write};

use byteorder::{LittleEndian, WriteBytesExt};
use rand::{SeedableRng, rngs::SmallRng};

use super::FileHeader;
use crate::{
    Policy, RNG_SEED, SequencingRecord,
    error::{Result, WriteError},
};

/// Writes a single flag value to a writer in little-endian format
///
/// # Arguments
///
/// * `writer` - Any type that implements the `Write` trait
/// * `flag` - The 64-bit flag value to write
///
/// # Returns
///
/// * `Ok(())` - If the flag was successfully written
/// * `Err(Error)` - If writing to the writer failed
pub fn write_flag<W: Write>(writer: &mut W, flag: u64) -> Result<()> {
    writer.write_u64::<LittleEndian>(flag)?;
    Ok(())
}

/// Writes a buffer of u64 values to a writer in little-endian format
///
/// This function is used to write encoded sequence data to the output.
/// Each u64 in the buffer contains up to 32 nucleotides in 2-bit format.
///
/// # Arguments
///
/// * `writer` - Any type that implements the `Write` trait
/// * `ebuf` - The buffer of u64 values to write
///
/// # Returns
///
/// * `Ok(())` - If the buffer was successfully written
/// * `Err(Error)` - If writing to the writer failed
pub fn write_buffer<W: Write>(writer: &mut W, ebuf: &[u64]) -> Result<()> {
    ebuf.iter()
        .try_for_each(|&x| writer.write_u64::<LittleEndian>(x))?;
    Ok(())
}

/// Encodes nucleotide sequences into a compact 2-bit binary format
///
/// The `Encoder` handles the conversion of nucleotide sequences (A, C, G, T)
/// into a compact binary representation where each nucleotide is stored using
/// 2 bits. It also handles invalid nucleotides according to a configurable policy.
///
/// The encoder maintains internal buffers to avoid repeated allocations during
/// encoding operations. These buffers are reused across multiple encode calls
/// and are cleared automatically when needed.
#[derive(Clone)]
pub struct Encoder {
    /// Header containing sequence length and format information
    header: FileHeader,

    /// Buffers for storing encoded nucleotides in 2-bit format
    /// Each u64 can store 32 nucleotides (64 bits / 2 bits per nucleotide)
    sbuffer: Vec<u64>, // Primary sequence buffer
    xbuffer: Vec<u64>, // Extended sequence buffer

    /// Temporary buffers for handling invalid nucleotides
    /// These store the processed sequences after policy application
    s_ibuf: Vec<u8>, // Primary sequence invalid buffer
    x_ibuf: Vec<u8>, // Extended sequence invalid buffer

    /// Policy for handling invalid nucleotides during encoding
    policy: Policy,

    /// Random number generator for the `RandomDraw` policy
    /// Seeded with `RNG_SEED` for reproducibility
    rng: SmallRng,
}
impl Encoder {
    /// Creates a new encoder with default invalid nucleotide policy
    ///
    /// # Arguments
    ///
    /// * `header` - The header defining sequence lengths and format
    ///
    /// # Examples
    ///
    /// ```
    /// # use binseq::bq::{FileHeaderBuilder, Encoder};
    /// let header = FileHeaderBuilder::new().slen(100).build().unwrap();
    /// let encoder = Encoder::new(header);
    /// ```
    #[must_use]
    pub fn new(header: FileHeader) -> Self {
        Self::with_policy(header, Policy::default())
    }

    /// Creates a new encoder with a specific invalid nucleotide policy
    ///
    /// # Arguments
    ///
    /// * `header` - The header defining sequence lengths and format
    /// * `policy` - The policy for handling invalid nucleotides
    ///
    /// # Examples
    ///
    /// ```
    /// # use binseq::bq::{FileHeaderBuilder, Encoder};
    /// # use binseq::Policy;
    /// let header = FileHeaderBuilder::new().slen(100).build().unwrap();
    /// let encoder = Encoder::with_policy(header, Policy::SetToA);
    /// ```
    #[must_use]
    pub fn with_policy(header: FileHeader, policy: Policy) -> Self {
        Self {
            header,
            policy,
            sbuffer: Vec::default(),
            xbuffer: Vec::default(),
            s_ibuf: Vec::default(),
            x_ibuf: Vec::default(),
            rng: SmallRng::seed_from_u64(RNG_SEED),
        }
    }

    /// Returns whether the header is paired-end.
    #[must_use]
    pub fn is_paired(&self) -> bool {
        self.header.is_paired()
    }

    /// Encodes a single sequence as 2-bit.
    ///
    /// Will return `None` if the sequence is invalid and the policy does not allow correction.
    pub fn encode_single(&mut self, primary: &[u8]) -> Result<Option<&[u64]>> {
        if primary.len() != self.header.slen as usize {
            return Err(WriteError::UnexpectedSequenceLength {
                expected: self.header.slen,
                got: primary.len(),
            }
            .into());
        }

        // Fill the buffer with the 2-bit representation of the nucleotides
        self.clear();
        if self.header.bits.encode(primary, &mut self.sbuffer).is_err() {
            self.clear();
            if self
                .policy
                .handle(primary, &mut self.s_ibuf, &mut self.rng)?
            {
                self.header.bits.encode(&self.s_ibuf, &mut self.sbuffer)?;
            } else {
                return Ok(None);
            }
        }

        Ok(Some(&self.sbuffer))
    }

    /// Encodes a pair of sequences as 2-bit.
    ///
    /// Will return `None` if either sequence is invalid and the policy does not allow correction.
    pub fn encode_paired(
        &mut self,
        primary: &[u8],
        extended: &[u8],
    ) -> Result<Option<(&[u64], &[u64])>> {
        if primary.len() != self.header.slen as usize {
            return Err(WriteError::UnexpectedSequenceLength {
                expected: self.header.slen,
                got: primary.len(),
            }
            .into());
        }
        if extended.len() != self.header.xlen as usize {
            return Err(WriteError::UnexpectedSequenceLength {
                expected: self.header.xlen,
                got: extended.len(),
            }
            .into());
        }

        self.clear();
        if self.header.bits.encode(primary, &mut self.sbuffer).is_err()
            || self
                .header
                .bits
                .encode(extended, &mut self.xbuffer)
                .is_err()
        {
            self.clear();
            if self
                .policy
                .handle(primary, &mut self.s_ibuf, &mut self.rng)?
                && self
                    .policy
                    .handle(extended, &mut self.x_ibuf, &mut self.rng)?
            {
                self.header.bits.encode(&self.s_ibuf, &mut self.sbuffer)?;
                self.header.bits.encode(&self.x_ibuf, &mut self.xbuffer)?;
            } else {
                return Ok(None);
            }
        }

        Ok(Some((&self.sbuffer, &self.xbuffer)))
    }

    /// Clear all buffers and reset the encoder.
    pub fn clear(&mut self) {
        self.sbuffer.clear();
        self.xbuffer.clear();
        self.s_ibuf.clear();
        self.x_ibuf.clear();
    }
}

/// Builder for creating configured `Writer` instances
///
/// This builder provides a flexible way to create writers with various
/// configurations. It follows the builder pattern, allowing for optional
/// settings to be specified in any order.
///
/// # Examples
///
/// ```
/// # use binseq::{Policy, Result};
/// # use binseq::bq::{FileHeaderBuilder, WriterBuilder};
/// # fn main() -> Result<()> {
/// let header = FileHeaderBuilder::new().slen(100).build()?;
/// let writer = WriterBuilder::default()
///     .header(header)
///     .policy(Policy::SetToA)
///     .headless(false)
///     .build(Vec::new())?;
/// # Ok(())
/// # }
/// ```
#[derive(Default)]
pub struct WriterBuilder {
    /// Required header defining sequence lengths and format
    header: Option<FileHeader>,
    /// Optional policy for handling invalid nucleotides
    policy: Option<Policy>,
    /// Optional headless mode for parallel writing scenarios
    headless: Option<bool>,
}
impl WriterBuilder {
    #[must_use]
    pub fn header(mut self, header: FileHeader) -> Self {
        self.header = Some(header);
        self
    }

    #[must_use]
    pub fn policy(mut self, policy: Policy) -> Self {
        self.policy = Some(policy);
        self
    }

    #[must_use]
    pub fn headless(mut self, headless: bool) -> Self {
        self.headless = Some(headless);
        self
    }

    pub fn build<W: Write>(self, inner: W) -> Result<Writer<W>> {
        let Some(header) = self.header else {
            return Err(WriteError::MissingHeader.into());
        };
        Writer::new(
            inner,
            header,
            self.policy.unwrap_or_default(),
            self.headless.unwrap_or(false),
        )
    }
}

/// High-level writer for binary sequence files
///
/// This writer provides a convenient interface for writing nucleotide sequences
/// to binary files in a compact format. It handles sequence encoding, invalid
/// nucleotide processing, and file format compliance.
///
/// The writer can operate in two modes:
/// - Normal mode: Writes the header followed by records
/// - Headless mode: Writes only records (useful for parallel writing)
///
/// # Type Parameters
///
/// * `W` - The underlying writer type that implements `Write`
#[derive(Clone)]
pub struct Writer<W: Write> {
    /// The underlying writer for output
    inner: W,

    /// Encoder for converting sequences to binary format
    encoder: Encoder,

    /// Whether this writer is in headless mode
    /// When true, the header is not written to the output
    headless: bool,
}
impl<W: Write> Writer<W> {
    /// Creates a new `Writer` instance with specified configuration
    ///
    /// This is a low-level constructor. For a more convenient way to create a
    /// `Writer`, use the `WriterBuilder` struct.
    ///
    /// # Arguments
    ///
    /// * `inner` - The underlying writer to write to
    /// * `header` - The header defining sequence lengths and format
    /// * `policy` - The policy for handling invalid nucleotides
    /// * `headless` - Whether to skip writing the header (for parallel writing)
    ///
    /// # Returns
    ///
    /// * `Ok(Writer)` - A new writer instance
    /// * `Err(Error)` - If writing the header fails
    ///
    /// # Examples
    ///
    /// ```
    /// # use binseq::bq::{FileHeaderBuilder, Writer};
    /// # use binseq::{Result, Policy};
    /// # fn main() -> Result<()> {
    /// let header = FileHeaderBuilder::new().slen(100).build()?;
    /// let writer = Writer::new(
    ///     Vec::new(),
    ///     header,
    ///     Policy::default(),
    ///     false
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(mut inner: W, header: FileHeader, policy: Policy, headless: bool) -> Result<Self> {
        if !headless {
            header.write_bytes(&mut inner)?;
        }
        Ok(Self {
            inner,
            encoder: Encoder::with_policy(header, policy),
            headless,
        })
    }

    /// Returns whether the header is paired-end.
    pub fn is_paired(&self) -> bool {
        self.encoder.is_paired()
    }

    /// Returns the header of the writer
    pub fn header(&self) -> FileHeader {
        self.encoder.header
    }

    /// Returns the N-policy of the writer
    pub fn policy(&self) -> Policy {
        self.encoder.policy
    }

    /// Writes a single record to the output
    ///
    /// This method encodes and writes a primary sequence along with an associated flag.
    ///
    /// # Arguments
    ///
    /// * `flag` - A 64-bit flag value associated with the sequence
    /// * `primary` - The nucleotide sequence to write
    ///
    /// # Returns
    ///
    /// * `Ok(true)` if the record was written successfully
    /// * `Ok(false)` if the record was not written because it was empty
    /// * `Err(WriteError::FlagSet)` if the flag is set but no flag value is provided
    #[deprecated]
    pub fn write_record(&mut self, flag: Option<u64>, primary: &[u8]) -> Result<bool> {
        let has_flag = self.encoder.header.flags;
        if let Some(sbuffer) = self.encoder.encode_single(primary)? {
            if has_flag {
                write_flag(&mut self.inner, flag.unwrap_or(0))?;
            }
            write_buffer(&mut self.inner, sbuffer)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Writes a paired record to the output
    ///
    /// This method writes a paired record to the output. It takes a flag, primary sequence, and extended sequence as input.
    /// If the flag is set but no flag value is provided, it returns an error.
    /// Otherwise, it writes the encoded single and extended sequences to the output and returns true.
    ///
    /// # Arguments
    /// * `flag` - The flag value to write to the output
    /// * `primary` - The primary sequence to encode and write to the output
    /// * `extended` - The extended sequence to encode and write to the output
    ///
    /// # Returns
    /// * `Result<bool>` - A result indicating whether the write was successful or not
    #[deprecated]
    pub fn write_paired_record(
        &mut self,
        flag: Option<u64>,
        primary: &[u8],
        extended: &[u8],
    ) -> Result<bool> {
        let has_flag = self.encoder.header.flags;
        if let Some((sbuffer, xbuffer)) = self.encoder.encode_paired(primary, extended)? {
            if has_flag {
                write_flag(&mut self.inner, flag.unwrap_or(0))?;
            }
            write_buffer(&mut self.inner, sbuffer)?;
            write_buffer(&mut self.inner, xbuffer)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Writes a record using the unified [`SequencingRecord`] API
    ///
    /// This method provides a consistent interface with VBQ and CBQ writers.
    /// Note that BQ format does not support quality scores or headers - these
    /// fields from the record will be ignored.
    ///
    /// # Arguments
    ///
    /// * `record` - A [`SequencingRecord`] containing the sequence data to write
    ///
    /// # Returns
    ///
    /// * `Ok(true)` if the record was written successfully
    /// * `Ok(false)` if the record was skipped due to invalid nucleotides
    /// * `Err(_)` if writing failed
    ///
    /// # Examples
    ///
    /// ```
    /// # use binseq::bq::{FileHeaderBuilder, WriterBuilder};
    /// # use binseq::{Result, SequencingRecordBuilder};
    /// # fn main() -> Result<()> {
    /// let header = FileHeaderBuilder::new().slen(8).build()?;
    /// let mut writer = WriterBuilder::default()
    ///     .header(header)
    ///     .build(Vec::new())?;
    ///
    /// let record = SequencingRecordBuilder::default()
    ///     .s_seq(b"ACGTACGT")
    ///     .flag(42)
    ///     .build()?;
    ///
    /// writer.push(record)?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn push(&mut self, record: SequencingRecord) -> Result<bool> {
        let has_flag = self.encoder.header.flags;
        if has_flag {
            write_flag(&mut self.inner, record.flag().unwrap_or(0))?;
        }

        // Check paired status - writer can require paired (record must have R2),
        // but if writer is single-end, we simply ignore any R2 data in the record.
        if self.encoder.header.is_paired() && !record.is_paired() {
            return Err(WriteError::ConfigurationMismatch {
                attribute: "paired",
                expected: self.encoder.header.is_paired(),
                actual: record.is_paired(),
            }
            .into());
        }

        if self.encoder.header.is_paired() {
            if let Some((sbuffer, xbuffer)) = self
                .encoder
                .encode_paired(record.s_seq, record.x_seq.unwrap_or_default())?
            {
                write_buffer(&mut self.inner, sbuffer)?;
                write_buffer(&mut self.inner, xbuffer)?;
                Ok(true)
            } else {
                Ok(false)
            }
        } else if let Some(buffer) = self.encoder.encode_single(record.s_seq)? {
            write_buffer(&mut self.inner, buffer)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Consumes the writer and returns the underlying writer
    ///
    /// This is useful when you need to access the underlying writer after
    /// writing is complete, for example to get the contents of a `Vec<u8>`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use binseq::bq::{FileHeaderBuilder, WriterBuilder};
    /// # use binseq::Result;
    /// # fn main() -> Result<()> {
    /// let header = FileHeaderBuilder::new().slen(100).build()?;
    /// let writer = WriterBuilder::default()
    ///     .header(header)
    ///     .build(Vec::new())?;
    ///
    /// // After writing sequences...
    /// let bytes = writer.into_inner();
    /// # Ok(())
    /// # }
    /// ```
    pub fn into_inner(self) -> W {
        self.inner
    }

    /// Gets a mutable reference to the underlying writer
    ///
    /// This allows direct access to the underlying writer while retaining
    /// ownership of the `Writer`.
    pub fn by_ref(&mut self) -> &mut W {
        &mut self.inner
    }

    /// Flushes any buffered data to the underlying writer
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the flush was successful
    /// * `Err(Error)` - If flushing failed
    pub fn flush(&mut self) -> Result<()> {
        self.inner.flush()?;
        Ok(())
    }

    /// Creates a new encoder with the same configuration as this writer
    ///
    /// This is useful when you need a separate encoder instance for parallel
    /// processing or other scenarios where you need independent encoding.
    /// The new encoder is initialized with a cleared state.
    ///
    /// # Returns
    ///
    /// A new `Encoder` instance with the same configuration but cleared buffers
    pub fn new_encoder(&self) -> Encoder {
        let mut encoder = self.encoder.clone();
        encoder.clear();
        encoder
    }

    /// Checks if this writer is in headless mode
    ///
    /// In headless mode, the writer does not write the header to the output.
    /// This is useful for parallel writing scenarios where only one writer
    /// should write the header.
    ///
    /// # Returns
    ///
    /// `true` if the writer is in headless mode, `false` otherwise
    pub fn is_headless(&self) -> bool {
        self.headless
    }

    /// Ingests the contents of another writer's buffer
    ///
    /// This method is used in parallel writing scenarios to combine the output
    /// of multiple writers. It takes the contents of another writer's buffer
    /// and writes them to this writer's output.
    ///
    /// # Arguments
    ///
    /// * `other` - Another writer whose underlying writer is a `Vec<u8>`
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the contents were successfully ingested
    /// * `Err(Error)` - If writing the contents failed
    pub fn ingest(&mut self, other: &mut Writer<Vec<u8>>) -> Result<()> {
        let other_inner = other.by_ref();
        self.inner.write_all(other_inner)?;
        other_inner.clear();
        Ok(())
    }
}

/// A streaming writer for binary sequence data
///
/// This writer buffers data before writing it to the underlying writer,
/// providing efficient streaming capabilities suitable for:
/// - Writing to network connections
/// - Processing very large datasets
/// - Pipeline processing
///
/// The `StreamWriter` is a specialized version of `Writer` that
/// adds internal buffering and is optimized for streaming scenarios.
pub struct StreamWriter<W: Write> {
    /// The underlying writer for processing sequences
    writer: Writer<BufWriter<W>>,
}

impl<W: Write> StreamWriter<W> {
    /// Creates a new `StreamWriter` with the default buffer size
    ///
    /// This constructor initializes a `StreamWriter` with an 8K buffer
    /// for efficient writing to the underlying writer.
    ///
    /// # Arguments
    ///
    /// * `inner` - The writer to write binary sequence data to
    /// * `header` - The header defining sequence lengths and format
    /// * `policy` - The policy for handling invalid nucleotides
    /// * `headless` - Whether to skip writing the header
    ///
    /// # Returns
    ///
    /// * `Ok(StreamWriter)` - A new streaming writer
    /// * `Err(Error)` - If initialization fails
    pub fn new(inner: W, header: FileHeader, policy: Policy, headless: bool) -> Result<Self> {
        Self::with_capacity(inner, 8192, header, policy, headless)
    }

    /// Creates a new `StreamWriter` with a specified buffer capacity
    ///
    /// This constructor allows customizing the buffer size based on
    /// expected usage patterns and performance requirements.
    ///
    /// # Arguments
    ///
    /// * `inner` - The writer to write binary sequence data to
    /// * `capacity` - The size of the internal buffer in bytes
    /// * `header` - The header defining sequence lengths and format
    /// * `policy` - The policy for handling invalid nucleotides
    /// * `headless` - Whether to skip writing the header
    ///
    /// # Returns
    ///
    /// * `Ok(StreamWriter)` - A new streaming writer with the specified buffer capacity
    /// * `Err(Error)` - If initialization fails
    pub fn with_capacity(
        inner: W,
        capacity: usize,
        header: FileHeader,
        policy: Policy,
        headless: bool,
    ) -> Result<Self> {
        let buffered = BufWriter::with_capacity(capacity, inner);
        let writer = Writer::new(buffered, header, policy, headless)?;

        Ok(Self { writer })
    }

    #[deprecated(note = "use `push` method with SequencingRecord instead")]
    pub fn write_record(&mut self, flag: Option<u64>, primary: &[u8]) -> Result<bool> {
        #[allow(deprecated)]
        self.writer.write_record(flag, primary)
    }

    #[deprecated(note = "use `push` method with SequencingRecord instead")]
    pub fn write_paired_record(
        &mut self,
        flag: Option<u64>,
        primary: &[u8],
        extended: &[u8],
    ) -> Result<bool> {
        #[allow(deprecated)]
        self.writer.write_paired_record(flag, primary, extended)
    }

    /// Writes a record using the unified [`SequencingRecord`] API
    pub fn push(&mut self, record: SequencingRecord) -> Result<bool> {
        self.writer.push(record)
    }

    /// Flushes any buffered data to the underlying writer
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the flush was successful
    /// * `Err(Error)` - If flushing failed
    pub fn flush(&mut self) -> Result<()> {
        self.writer.flush()
    }

    /// Consumes the streaming writer and returns the inner writer after flushing
    ///
    /// This method is useful when you need access to the underlying writer
    /// after all writing is complete.
    ///
    /// # Returns
    ///
    /// * `Ok(W)` - The inner writer after flushing all data
    /// * `Err(Error)` - If flushing failed
    pub fn into_inner(self) -> Result<W> {
        // First unwrap the writer inner (BufWriter<W>)
        let bufw = self.writer.into_inner();
        // Now unwrap the BufWriter to get W
        match bufw.into_inner() {
            Ok(inner) => Ok(inner),
            Err(e) => Err(std::io::Error::from(e).into()),
        }
    }
}

/// Builder for `StreamWriter` instances
///
/// This builder provides a convenient way to create and configure `StreamWriter`
/// instances with custom buffer sizes and other settings.
#[derive(Default)]
pub struct StreamWriterBuilder {
    /// Required header defining sequence lengths and format
    header: Option<FileHeader>,
    /// Optional policy for handling invalid nucleotides
    policy: Option<Policy>,
    /// Optional headless mode for parallel writing scenarios
    headless: Option<bool>,
    /// Optional buffer capacity setting
    buffer_capacity: Option<usize>,
}

impl StreamWriterBuilder {
    /// Sets the header for the writer
    #[must_use]
    pub fn header(mut self, header: FileHeader) -> Self {
        self.header = Some(header);
        self
    }

    /// Sets the policy for handling invalid nucleotides
    #[must_use]
    pub fn policy(mut self, policy: Policy) -> Self {
        self.policy = Some(policy);
        self
    }

    /// Sets headless mode (whether to skip writing the header)
    #[must_use]
    pub fn headless(mut self, headless: bool) -> Self {
        self.headless = Some(headless);
        self
    }

    /// Sets the buffer capacity for the writer
    #[must_use]
    pub fn buffer_capacity(mut self, capacity: usize) -> Self {
        self.buffer_capacity = Some(capacity);
        self
    }

    /// Builds a `StreamWriter` with the configured settings
    ///
    /// # Arguments
    ///
    /// * `inner` - The writer to write binary sequence data to
    ///
    /// # Returns
    ///
    /// * `Ok(StreamWriter)` - A new streaming writer with the specified configuration
    /// * `Err(Error)` - If building the writer fails
    pub fn build<W: Write>(self, inner: W) -> Result<StreamWriter<W>> {
        let Some(header) = self.header else {
            return Err(WriteError::MissingHeader.into());
        };

        let capacity = self.buffer_capacity.unwrap_or(8192);
        StreamWriter::with_capacity(
            inner,
            capacity,
            header,
            self.policy.unwrap_or_default(),
            self.headless.unwrap_or(false),
        )
    }
}

#[cfg(test)]
mod testing {

    use std::{fs::File, io::BufWriter};

    use super::*;
    use crate::bq::{FileHeaderBuilder, SIZE_HEADER};

    #[test]
    fn test_headless() -> Result<()> {
        let inner = Vec::new();
        let mut writer = WriterBuilder::default()
            .header(FileHeaderBuilder::new().slen(32).build()?)
            .headless(true)
            .build(inner)?;
        assert!(writer.is_headless());
        let inner = writer.by_ref();
        assert!(inner.is_empty());
        Ok(())
    }

    #[test]
    fn test_not_headless() -> Result<()> {
        let inner = Vec::new();
        let mut writer = WriterBuilder::default()
            .header(FileHeaderBuilder::new().slen(32).build()?)
            .build(inner)?;
        assert!(!writer.is_headless());
        let inner = writer.by_ref();
        assert_eq!(inner.len(), SIZE_HEADER);
        Ok(())
    }

    #[test]
    fn test_stdout() -> Result<()> {
        let writer = WriterBuilder::default()
            .header(FileHeaderBuilder::new().slen(32).build()?)
            .build(std::io::stdout())?;
        assert!(!writer.is_headless());
        Ok(())
    }

    #[test]
    fn test_to_path() -> Result<()> {
        let path = "test_to_path.file";
        let inner = File::create(path).map(BufWriter::new)?;
        let mut writer = WriterBuilder::default()
            .header(FileHeaderBuilder::new().slen(32).build()?)
            .build(inner)?;
        assert!(!writer.is_headless());
        let inner = writer.by_ref();
        inner.flush()?;

        // delete file
        std::fs::remove_file(path)?;

        Ok(())
    }

    #[test]
    fn test_stream_writer() -> Result<()> {
        let inner = Vec::new();
        let writer = StreamWriterBuilder::default()
            .header(FileHeaderBuilder::new().slen(32).build()?)
            .buffer_capacity(16384)
            .build(inner)?;

        // Convert back to Vec to verify it works
        let inner = writer.into_inner()?;
        assert_eq!(inner.len(), SIZE_HEADER);
        Ok(())
    }
}