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
pub mod bin;
mod builder;
pub use self::bin::Bin;
pub(crate) use self::builder::Builder;
use std::{
error, fmt,
ops::{Bound, RangeBounds},
};
use bit_vec::BitVec;
use noodles_bgzf as bgzf;
use noodles_csi::{index::reference_sequence::Metadata, BinningIndexReferenceSequence};
const MIN_SHIFT: i32 = 14;
const DEPTH: i32 = 5;
const MIN_POSITION: i32 = 1;
const MAX_POSITION: i32 = 1 << (MIN_SHIFT + 3 * DEPTH);
const WINDOW_SIZE: i32 = 1 << MIN_SHIFT;
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ReferenceSequence {
bins: Vec<Bin>,
intervals: Vec<bgzf::VirtualPosition>,
metadata: Option<Metadata>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum QueryError {
InvalidStartPosition(i32),
InvalidEndPosition(i32),
}
impl error::Error for QueryError {}
impl fmt::Display for QueryError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidStartPosition(start) => {
write!(
f,
"expected start position >= {}, got {}",
MIN_POSITION, start
)
}
Self::InvalidEndPosition(end) => {
write!(f, "expected end position <= {}, got {}", MAX_POSITION, end)
}
}
}
}
impl ReferenceSequence {
pub(crate) fn builder() -> Builder {
Builder::default()
}
pub fn new(
bins: Vec<Bin>,
intervals: Vec<bgzf::VirtualPosition>,
metadata: Option<Metadata>,
) -> Self {
Self {
bins,
intervals,
metadata,
}
}
pub fn bins(&self) -> &[Bin] {
&self.bins
}
pub fn intervals(&self) -> &[bgzf::VirtualPosition] {
&self.intervals
}
pub fn query<B>(&self, interval: B) -> Result<Vec<&Bin>, QueryError>
where
B: RangeBounds<i32>,
{
let start = match interval.start_bound() {
Bound::Included(s) => *s,
Bound::Excluded(s) => *s + 1,
Bound::Unbounded => MIN_POSITION,
};
if start < MIN_POSITION {
return Err(QueryError::InvalidStartPosition(start));
}
let end = match interval.end_bound() {
Bound::Included(e) => *e,
Bound::Excluded(e) => *e - 1,
Bound::Unbounded => MAX_POSITION,
};
if end > MAX_POSITION {
return Err(QueryError::InvalidEndPosition(end));
}
let region_bins = region_to_bins((start - 1) as usize, end as usize);
let query_bins = self
.bins()
.iter()
.filter(|b| region_bins[b.id() as usize])
.collect();
Ok(query_bins)
}
pub fn min_offset(&self, start: i32) -> bgzf::VirtualPosition {
let i = ((start - 1) / WINDOW_SIZE) as usize;
self.intervals.get(i).copied().unwrap_or_default()
}
}
impl BinningIndexReferenceSequence for ReferenceSequence {
fn metadata(&self) -> Option<&Metadata> {
self.metadata.as_ref()
}
fn first_record_in_last_linear_bin_start_position(&self) -> Option<bgzf::VirtualPosition> {
self.intervals().last().copied()
}
}
fn region_to_bins(start: usize, mut end: usize) -> BitVec {
end -= 1;
let mut bins = BitVec::from_elem(bin::MAX_ID as usize, false);
bins.set(0, true);
for k in (1 + (start >> 26))..=(1 + (end >> 26)) {
bins.set(k, true);
}
for k in (9 + (start >> 23))..=(9 + (end >> 23)) {
bins.set(k, true);
}
for k in (73 + (start >> 20))..=(73 + (end >> 20)) {
bins.set(k, true);
}
for k in (585 + (start >> 17))..=(585 + (end >> 17)) {
bins.set(k, true);
}
for k in (4681 + (start >> 14))..=(4681 + (end >> 14)) {
bins.set(k, true);
}
bins
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query() {
let reference_sequence = ReferenceSequence::new(Vec::new(), Vec::new(), None);
assert_eq!(
reference_sequence.query(0..=8),
Err(QueryError::InvalidStartPosition(0))
);
assert_eq!(
reference_sequence.query(1..=i32::MAX),
Err(QueryError::InvalidEndPosition(i32::MAX))
);
}
#[test]
fn test_region_to_bins() {
let actual = region_to_bins(7, 13);
let mut expected = BitVec::from_elem(bin::MAX_ID as usize, false);
for &k in &[0, 1, 9, 73, 585, 4681] {
expected.set(k, true);
}
assert_eq!(actual, expected);
let actual = region_to_bins(63245985, 63255986);
let mut expected = BitVec::from_elem(bin::MAX_ID as usize, false);
for &k in &[0, 1, 16, 133, 1067, 8541] {
expected.set(k, true);
}
assert_eq!(actual, expected);
}
}