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
use std::iter::repeat;
use crate::alphabets::Alphabet;
use crate::data_structures::suffix_array::RawSuffixArraySlice;
use crate::utils::prescan;
pub type BWT = Vec<u8>;
pub type BWTSlice = [u8];
pub type Less = Vec<usize>;
pub type BWTFind = Vec<usize>;
pub fn bwt(text: &[u8], pos: RawSuffixArraySlice) -> BWT {
assert_eq!(text.len(), pos.len());
let n = text.len();
let mut bwt: BWT = repeat(0).take(n).collect();
for r in 0..n {
let p = pos[r];
bwt[r] = if p > 0 { text[p - 1] } else { text[n - 1] };
}
bwt
}
pub fn invert_bwt(bwt: &BWTSlice) -> Vec<u8> {
let alphabet = Alphabet::new(bwt);
let n = bwt.len();
let bwtfind = bwtfind(bwt, &alphabet);
let mut inverse = Vec::with_capacity(n);
let mut r = bwtfind[0];
for _ in 0..n {
r = bwtfind[r];
inverse.push(bwt[r]);
}
inverse
}
#[derive(Serialize, Deserialize)]
pub struct Occ {
occ: Vec<Vec<usize>>,
k: u32,
}
impl Occ {
pub fn new(bwt: &BWTSlice, k: u32, alphabet: &Alphabet) -> Self {
let n = bwt.len();
let m = alphabet
.max_symbol()
.expect("Expecting non-empty alphabet.") as usize
+ 1;
let mut occ = Vec::with_capacity(n / k as usize);
let mut curr_occ: Vec<usize> = repeat(0).take(m).collect();
for (i, &c) in bwt.iter().enumerate() {
curr_occ[c as usize] += 1;
if i % k as usize == 0 {
occ.push(curr_occ.clone());
}
}
Occ { occ, k }
}
pub fn get(&self, bwt: &BWTSlice, r: usize, a: u8) -> usize {
let lo_checkpoint = r / self.k as usize;
let lo_occ = self.occ[lo_checkpoint][a as usize];
if self.k > 64 {
let hi_checkpoint = lo_checkpoint + 1;
if let Some(hi_occs) = self.occ.get(hi_checkpoint) {
let hi_occ = hi_occs[a as usize];
if lo_occ == hi_occ {
return lo_occ;
}
let hi_idx = hi_checkpoint * self.k as usize;
if (hi_idx - r) < (self.k as usize / 2) {
let hi_occ = hi_occs[a as usize];
return hi_occ - bytecount::count(&bwt[r + 1..=hi_idx], a) as usize;
}
}
}
let lo_idx = lo_checkpoint * self.k as usize;
bytecount::count(&bwt[lo_idx + 1..=r], a) as usize + lo_occ
}
}
pub fn less(bwt: &BWTSlice, alphabet: &Alphabet) -> Less {
let m = alphabet
.max_symbol()
.expect("Expecting non-empty alphabet.") as usize
+ 2;
let mut less: Less = repeat(0).take(m).collect();
for &c in bwt.iter() {
less[c as usize] += 1;
}
prescan(&mut less[..], 0, |a, b| a + b);
less
}
pub fn bwtfind(bwt: &BWTSlice, alphabet: &Alphabet) -> BWTFind {
let n = bwt.len();
let mut less = less(bwt, alphabet);
let mut bwtfind: BWTFind = repeat(0).take(n).collect();
for (r, &c) in bwt.iter().enumerate() {
bwtfind[less[c as usize]] = r;
less[c as usize] += 1;
}
bwtfind
}
#[cfg(test)]
mod tests {
use super::{bwt, bwtfind, invert_bwt, Occ};
use crate::alphabets::dna;
use crate::alphabets::Alphabet;
use crate::data_structures::suffix_array::suffix_array;
use crate::data_structures::wavelet_matrix::WaveletMatrix;
#[test]
fn test_bwtfind() {
let text = b"cabca$";
let alphabet = Alphabet::new(b"abc$");
let pos = suffix_array(text);
let bwt = bwt(text, &pos);
let bwtfind = bwtfind(&bwt, &alphabet);
assert_eq!(bwtfind, vec![5, 0, 3, 4, 1, 2]);
}
#[test]
fn test_invert_bwt() {
let text = b"cabca$";
let pos = suffix_array(text);
let bwt = bwt(text, &pos);
let inverse = invert_bwt(&bwt);
assert_eq!(inverse, text);
}
#[test]
fn test_occ() {
let bwt = vec![1u8, 3u8, 3u8, 1u8, 2u8, 0u8];
let alphabet = Alphabet::new(&[0u8, 1u8, 2u8, 3u8]);
let occ = Occ::new(&bwt, 3, &alphabet);
assert_eq!(occ.occ, [[0, 1, 0, 0], [0, 2, 0, 2]]);
assert_eq!(occ.get(&bwt, 4, 2u8), 1);
assert_eq!(occ.get(&bwt, 4, 3u8), 2);
}
#[test]
fn test_occwm() {
let text = b"GCCTTAACATTATTACGCCTA$";
let alphabet = dna::n_alphabet();
let sa = suffix_array(text);
let bwt = bwt(text, &sa);
let occ = Occ::new(&bwt, 3, &alphabet);
let wm = WaveletMatrix::new(&bwt);
for c in vec![b'A', b'C', b'G', b'T', b'$'] {
for p in 0..text.len() {
assert_eq!(occ.get(&bwt, p, c) as u64, wm.rank(c, p as u64));
}
}
}
}