noodles_cram/async/io/writer/
builder.rs

1use std::path::Path;
2
3use noodles_fasta as fasta;
4use tokio::{
5    fs::File,
6    io::{self, AsyncWrite},
7};
8
9use super::Writer;
10use crate::{
11    container::BlockContentEncoderMap,
12    file_definition::Version,
13    io::writer::{Options, RECORDS_PER_CONTAINER},
14};
15
16/// An async CRAM writer builder.
17#[derive(Default)]
18pub struct Builder {
19    reference_sequence_repository: fasta::Repository,
20    options: Options,
21}
22
23impl Builder {
24    /// Sets the reference sequence repository.
25    pub fn set_reference_sequence_repository(
26        mut self,
27        reference_sequence_repository: fasta::Repository,
28    ) -> Self {
29        self.reference_sequence_repository = reference_sequence_repository;
30        self
31    }
32
33    /// Sets whether to preserve read names.
34    ///
35    /// If `false`, read names are discarded.
36    ///
37    /// The default is `true`.
38    pub fn preserve_read_names(mut self, value: bool) -> Self {
39        self.options.preserve_read_names = value;
40        self
41    }
42
43    /// Sets whether to encode alignment start positions as deltas.
44    ///
45    /// If `false`, record alignment start positions are written with their actual values.
46    ///
47    /// The default is `true`.
48    pub fn encode_alignment_start_positions_as_deltas(mut self, value: bool) -> Self {
49        self.options.encode_alignment_start_positions_as_deltas = value;
50        self
51    }
52
53    /// Sets the block content-encoder map.
54    pub fn set_block_content_encoder_map(mut self, map: BlockContentEncoderMap) -> Self {
55        self.options.block_content_encoder_map = map;
56        self
57    }
58
59    /// Builds an async CRAM writer from a path.
60    ///
61    /// # Examples
62    ///
63    /// ```no_run
64    /// # #[tokio::main]
65    /// # async fn main() -> tokio::io::Result<()> {
66    /// use noodles_cram::r#async::io::writer::Builder;
67    /// let writer = Builder::default().build_from_path("out.cram").await?;
68    /// # Ok(())
69    /// # }
70    /// ```
71    pub async fn build_from_path<P>(self, dst: P) -> io::Result<Writer<File>>
72    where
73        P: AsRef<Path>,
74    {
75        File::create(dst)
76            .await
77            .map(|file| self.build_from_writer(file))
78    }
79
80    /// Builds an async CRAM writer from a writer.
81    ///
82    /// # Examples
83    ///
84    /// ```
85    /// use noodles_cram as cram;
86    /// use tokio::io;
87    /// let writer = cram::r#async::io::writer::Builder::default().build_from_writer(io::sink());
88    /// ```
89    pub fn build_from_writer<W>(mut self, writer: W) -> Writer<W>
90    where
91        W: AsyncWrite + Unpin,
92    {
93        use crate::io::writer::builder::uses_cram_3_1_codecs;
94
95        if uses_cram_3_1_codecs(&self.options.block_content_encoder_map) {
96            self.options.version = Version::new(3, 1);
97        }
98
99        Writer {
100            inner: writer,
101            reference_sequence_repository: self.reference_sequence_repository,
102            options: self.options,
103            records: Vec::with_capacity(RECORDS_PER_CONTAINER),
104            record_counter: 0,
105        }
106    }
107}