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 path.
81    #[deprecated(since = "0.68.0", note = "Use `Builder::build_from_path` instead.")]
82    pub async fn build_with_path<P>(self, dst: P) -> io::Result<Writer<File>>
83    where
84        P: AsRef<Path>,
85    {
86        self.build_from_path(dst).await
87    }
88
89    /// Builds an async CRAM writer from a writer.
90    ///
91    /// # Examples
92    ///
93    /// ```
94    /// use noodles_cram as cram;
95    /// use tokio::io;
96    /// let writer = cram::r#async::io::writer::Builder::default().build_from_writer(io::sink());
97    /// ```
98    pub fn build_from_writer<W>(mut self, writer: W) -> Writer<W>
99    where
100        W: AsyncWrite + Unpin,
101    {
102        use crate::io::writer::builder::uses_cram_3_1_codecs;
103
104        if uses_cram_3_1_codecs(&self.options.block_content_encoder_map) {
105            self.options.version = Version::new(3, 1);
106        }
107
108        Writer {
109            inner: writer,
110            reference_sequence_repository: self.reference_sequence_repository,
111            options: self.options,
112            records: Vec::with_capacity(RECORDS_PER_CONTAINER),
113            record_counter: 0,
114        }
115    }
116
117    /// Builds an async CRAM writer from a writer.
118    #[deprecated(since = "0.68.0", note = "Use `Builder::build_from_writer` instead.")]
119    pub fn build_with_writer<W>(self, writer: W) -> Writer<W>
120    where
121        W: AsyncWrite + Unpin,
122    {
123        self.build_from_writer(writer)
124    }
125}