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
use std::{
fs::File,
io::{self, BufReader, Read},
path::Path,
};
use noodles_bam as bam;
use noodles_cram::{self as cram, crai};
use noodles_csi::{
self as csi,
binning_index::index::reference_sequence::index::{BinnedIndex, LinearIndex},
BinningIndex,
};
use noodles_fasta as fasta;
use noodles_sam as sam;
use super::IndexedReader;
use crate::alignment::io::{
reader::builder::{detect_compression_method, detect_format},
CompressionMethod, Format,
};
/// An alignment index.
pub enum Index {
/// CSI.
Csi(Box<dyn BinningIndex>),
/// CRAI.
Crai(crai::Index),
}
impl From<csi::binning_index::Index<BinnedIndex>> for Index {
fn from(index: csi::binning_index::Index<BinnedIndex>) -> Self {
Self::Csi(Box::new(index))
}
}
impl From<csi::binning_index::Index<LinearIndex>> for Index {
fn from(index: csi::binning_index::Index<LinearIndex>) -> Self {
Self::Csi(Box::new(index))
}
}
impl From<crai::Index> for Index {
fn from(index: crai::Index) -> Self {
Self::Crai(index)
}
}
/// An indexed alignment reader builder.
#[derive(Default)]
pub struct Builder {
compression_method: Option<Option<CompressionMethod>>,
format: Option<Format>,
reference_sequence_repository: fasta::Repository,
index: Option<Index>,
}
impl Builder {
/// Sets the compression method.
///
/// By default, the compression method is autodetected on build. This can be used to override
/// it, but note that only bgzip-compressed streams can be indexed.
///
/// # Examples
///
/// ```
/// use noodles_util::alignment::{self, io::CompressionMethod};
/// let builder = alignment::io::indexed_reader::Builder::default()
/// .set_compression_method(Some(CompressionMethod::Bgzf));
/// ```
pub fn set_compression_method(mut self, compression_method: Option<CompressionMethod>) -> Self {
self.compression_method = Some(compression_method);
self
}
/// Sets the format of the input.
///
/// By default, the format is autodetected on build. This can be used to override it.
///
/// # Examples
///
/// ```
/// use noodles_util::alignment::{self, io::Format};
/// let builder = alignment::io::indexed_reader::Builder::default()
/// .set_format(Format::Sam);
/// ```
pub fn set_format(mut self, format: Format) -> Self {
self.format = Some(format);
self
}
/// 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 an index.
///
/// When building from a path ([`Self::build_from_path`]), an associated index depending on the
/// format will attempt to be loaded. This can be used to override it if the index cannot be
/// found or when building from a reader ([`Self::build_from_reader`]).
///
/// # Examples
///
/// ```
/// use noodles_bam::bai;
/// use noodles_util::alignment;
///
/// let index = bai::Index::default();
/// let builder = alignment::io::indexed_reader::Builder::default()
/// .set_index(index);
/// ```
pub fn set_index<I>(mut self, index: I) -> Self
where
I: Into<Index>,
{
self.index = Some(index.into());
self
}
/// Builds an indexed alignment reader from a path.
///
/// The compression method and format will be autodetected, if not overridden. If no index is
/// set ([`Self::set_index`]), this will attempt to load an associated index depending on the
/// format.
///
/// # Examples
///
/// ```no_run
/// use noodles_util::alignment;
/// let reader = alignment::io::indexed_reader::Builder::default()
/// .build_from_path("sample.bam")?;
/// # Ok::<_, std::io::Error>(())
/// ```
pub fn build_from_path<P>(self, src: P) -> io::Result<IndexedReader<File>>
where
P: AsRef<Path>,
{
let mut reader = File::open(src.as_ref()).map(BufReader::new)?;
let compression_method = match self.compression_method {
Some(compression_method) => compression_method,
None => detect_compression_method(&mut reader)?,
};
let format = match self.format {
Some(format) => format,
None => detect_format(&mut reader, compression_method)?,
};
match (format, compression_method) {
(Format::Sam | Format::Bam, None) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"source not bgzip-compressed",
)),
(Format::Sam, Some(CompressionMethod::Bgzf)) => {
let mut builder = sam::io::indexed_reader::Builder::default();
if let Some(Index::Csi(index)) = self.index {
builder = builder.set_index(index);
}
builder.build_from_path(src).map(IndexedReader::Sam)
}
(Format::Bam, Some(CompressionMethod::Bgzf)) => {
let mut builder = bam::io::indexed_reader::Builder::default();
if let Some(Index::Csi(index)) = self.index {
builder = builder.set_index(index);
}
builder.build_from_path(src).map(IndexedReader::Bam)
}
(Format::Cram, None) => {
let mut builder = cram::io::indexed_reader::Builder::default()
.set_reference_sequence_repository(self.reference_sequence_repository);
if let Some(Index::Crai(index)) = self.index {
builder = builder.set_index(index);
}
builder.build_from_path(src).map(IndexedReader::Cram)
}
(Format::Cram, Some(CompressionMethod::Bgzf)) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"CRAM cannot be bgzip-compressed",
)),
}
}
/// Builds an indexed alignment reader from a reader.
///
/// The compression method and format will be autodetected, if not overridden. An index must be
/// set ([`Self::set_index`]). The reader must be a bgzip-compressed stream.
///
/// # Examples
///
/// ```
/// # use std::io::{self, Write};
/// use noodles_bam::bai;
/// use noodles_bgzf as bgzf;
/// use noodles_util::alignment;
///
/// let mut writer = bgzf::Writer::new(Vec::new());
/// writer.write_all(b"BAM\x01")?;
/// let data = writer.finish()?;
///
/// let index = bai::Index::default();
/// let reader = alignment::io::indexed_reader::Builder::default()
/// .set_index(index)
/// .build_from_reader(&data[..])?;
/// # Ok::<_, std::io::Error>(())
/// ```
pub fn build_from_reader<R>(self, reader: R) -> io::Result<IndexedReader<BufReader<R>>>
where
R: Read,
{
let mut reader = BufReader::new(reader);
let compression_method = match self.compression_method {
Some(compression_method) => compression_method,
None => detect_compression_method(&mut reader)?,
};
let format = match self.format {
Some(format) => format,
None => detect_format(&mut reader, compression_method)?,
};
match (format, compression_method) {
(Format::Sam | Format::Bam, None) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"source not bgzip-compressed",
)),
(Format::Sam, Some(CompressionMethod::Bgzf)) => {
let mut builder = sam::io::indexed_reader::Builder::default();
if let Some(Index::Csi(index)) = self.index {
builder = builder.set_index(index);
}
builder.build_from_reader(reader).map(IndexedReader::Sam)
}
(Format::Bam, Some(CompressionMethod::Bgzf)) => {
let mut builder = bam::io::indexed_reader::Builder::default();
if let Some(Index::Csi(index)) = self.index {
builder = builder.set_index(index);
}
builder.build_from_reader(reader).map(IndexedReader::Bam)
}
(Format::Cram, None) => {
let mut builder = cram::io::indexed_reader::Builder::default()
.set_reference_sequence_repository(self.reference_sequence_repository);
if let Some(Index::Crai(index)) = self.index {
builder = builder.set_index(index);
}
builder.build_from_reader(reader).map(IndexedReader::Cram)
}
(Format::Cram, Some(CompressionMethod::Bgzf)) => Err(io::Error::new(
io::ErrorKind::InvalidData,
"CRAM cannot be bgzip-compressed",
)),
}
}
}