noodles_cram/async/io/writer/builder.rs
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
use std::path::Path;
use noodles_fasta as fasta;
use tokio::{
fs::File,
io::{self, AsyncWrite},
};
use super::Writer;
use crate::{
data_container::BlockContentEncoderMap, file_definition::Version, io::writer::Options,
DataContainer,
};
/// An async CRAM writer builder.
#[derive(Default)]
pub struct Builder {
reference_sequence_repository: fasta::Repository,
options: Options,
}
impl Builder {
/// Sets the reference sequence repository.
pub fn set_reference_sequence_repository(
mut self,
reference_sequence_repository: fasta::Repository,
) -> Self {
self.reference_sequence_repository = reference_sequence_repository;
self
}
/// Sets whether to preserve read names.
///
/// If `false`, read names are discarded.
///
/// The default is `true`.
pub fn preserve_read_names(mut self, value: bool) -> Self {
self.options.preserve_read_names = value;
self
}
/// Sets whether to encode alignment start positions as deltas.
///
/// If `false`, record alignment start positions are written with their actual values.
///
/// The default is `true`.
pub fn encode_alignment_start_positions_as_deltas(mut self, value: bool) -> Self {
self.options.encode_alignment_start_positions_as_deltas = value;
self
}
/// Sets the block content-encoder map.
pub fn set_block_content_encoder_map(mut self, map: BlockContentEncoderMap) -> Self {
self.options.block_content_encoder_map = map;
self
}
/// Builds an async CRAM writer from a path.
///
/// # Examples
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_cram::r#async::io::writer::Builder;
/// let writer = Builder::default().build_from_path("out.cram").await?;
/// # Ok(())
/// # }
/// ```
pub async fn build_from_path<P>(self, dst: P) -> io::Result<Writer<File>>
where
P: AsRef<Path>,
{
File::create(dst)
.await
.map(|file| self.build_from_writer(file))
}
/// Builds an async CRAM writer from a path.
#[deprecated(since = "0.68.0", note = "Use `Builder::build_from_path` instead.")]
pub async fn build_with_path<P>(self, dst: P) -> io::Result<Writer<File>>
where
P: AsRef<Path>,
{
self.build_from_path(dst).await
}
/// Builds an async CRAM writer from a writer.
///
/// # Examples
///
/// ```
/// use noodles_cram as cram;
/// use tokio::io;
/// let writer = cram::r#async::io::writer::Builder::default().build_from_writer(io::sink());
/// ```
pub fn build_from_writer<W>(mut self, writer: W) -> Writer<W>
where
W: AsyncWrite + Unpin,
{
use crate::io::writer::builder::uses_cram_3_1_codecs;
if uses_cram_3_1_codecs(&self.options.block_content_encoder_map) {
self.options.version = Version::new(3, 1);
}
Writer {
inner: writer,
reference_sequence_repository: self.reference_sequence_repository,
options: self.options,
data_container_builder: DataContainer::builder(0),
record_counter: 0,
}
}
/// Builds an async CRAM writer from a writer.
#[deprecated(since = "0.68.0", note = "Use `Builder::build_from_writer` instead.")]
pub fn build_with_writer<W>(self, writer: W) -> Writer<W>
where
W: AsyncWrite + Unpin,
{
self.build_from_writer(writer)
}
}