minibwa 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +119 -0
  4. data/ext/minibwa/extconf.rb +105 -0
  5. data/ext/minibwa/mb_buffer.c +113 -0
  6. data/ext/minibwa/mb_hit.c +192 -0
  7. data/ext/minibwa/mb_index.c +462 -0
  8. data/ext/minibwa/mb_index_build.c +174 -0
  9. data/ext/minibwa/mb_options.c +301 -0
  10. data/ext/minibwa/minibwa/LICENSE.txt +37 -0
  11. data/ext/minibwa/minibwa/align.c +930 -0
  12. data/ext/minibwa/minibwa/bseq.h +45 -0
  13. data/ext/minibwa/minibwa/bwt.c +715 -0
  14. data/ext/minibwa/minibwa/bwt.h +86 -0
  15. data/ext/minibwa/minibwa/cs.c +161 -0
  16. data/ext/minibwa/minibwa/format.c +356 -0
  17. data/ext/minibwa/minibwa/index.c +342 -0
  18. data/ext/minibwa/minibwa/kalloc.c +224 -0
  19. data/ext/minibwa/minibwa/kalloc.h +54 -0
  20. data/ext/minibwa/minibwa/ketopt.h +123 -0
  21. data/ext/minibwa/minibwa/kommon.c +374 -0
  22. data/ext/minibwa/minibwa/kommon.h +85 -0
  23. data/ext/minibwa/minibwa/kseq.h +256 -0
  24. data/ext/minibwa/minibwa/ksort.h +163 -0
  25. data/ext/minibwa/minibwa/ksw2.h +220 -0
  26. data/ext/minibwa/minibwa/ksw2_extd2_sse.c +403 -0
  27. data/ext/minibwa/minibwa/ksw2_extz2_sse.c +296 -0
  28. data/ext/minibwa/minibwa/ksw2_ll_sse.c +341 -0
  29. data/ext/minibwa/minibwa/kthread.h +15 -0
  30. data/ext/minibwa/minibwa/l2bit.c +479 -0
  31. data/ext/minibwa/minibwa/l2bit.h +72 -0
  32. data/ext/minibwa/minibwa/lchain.c +231 -0
  33. data/ext/minibwa/minibwa/libsais.c +6985 -0
  34. data/ext/minibwa/minibwa/libsais.h +106 -0
  35. data/ext/minibwa/minibwa/libsais64.c +7064 -0
  36. data/ext/minibwa/minibwa/libsais64.h +81 -0
  37. data/ext/minibwa/minibwa/map-algo.c +769 -0
  38. data/ext/minibwa/minibwa/mbpriv.h +148 -0
  39. data/ext/minibwa/minibwa/minibwa.h +176 -0
  40. data/ext/minibwa/minibwa/options.c +116 -0
  41. data/ext/minibwa/minibwa/pe.c +559 -0
  42. data/ext/minibwa/minibwa/s2n-lite.h +59 -0
  43. data/ext/minibwa/minibwa/seed.c +354 -0
  44. data/ext/minibwa/minibwa.c +67 -0
  45. data/ext/minibwa/minibwa.h +51 -0
  46. data/lib/minibwa/hit.rb +110 -0
  47. data/lib/minibwa/index.rb +77 -0
  48. data/lib/minibwa/options.rb +235 -0
  49. data/lib/minibwa/sam.rb +85 -0
  50. data/lib/minibwa/version.rb +6 -0
  51. data/lib/minibwa.rb +11 -0
  52. metadata +88 -0
@@ -0,0 +1,715 @@
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #include <assert.h>
5
+ #include <stdint.h>
6
+ #include "kommon.h"
7
+ #include "kalloc.h"
8
+ #include "bwt.h"
9
+
10
+ /********************
11
+ * Basic operations *
12
+ ********************/
13
+
14
+ static void bwt_gen_cnt_table(uint32_t cnt[256])
15
+ {
16
+ int i, j;
17
+ for (i = 0; i != 256; ++i) {
18
+ uint32_t x = 0;
19
+ for (j = 0; j != 4; ++j)
20
+ x |= (((i&3) == j) + ((i>>2&3) == j) + ((i>>4&3) == j) + (i>>6 == j)) << (j<<3);
21
+ cnt[i] = x;
22
+ }
23
+ }
24
+
25
+ mb_bwt_t *mb_bwt_init(void)
26
+ {
27
+ mb_bwt_t *bwt;
28
+ bwt = kom_calloc(mb_bwt_t, 1);
29
+ bwt->sa_bit = (uint32_t)-1;
30
+ bwt_gen_cnt_table(bwt->cnt_table);
31
+ return bwt;
32
+ }
33
+
34
+ void mb_bwt_destroy(mb_bwt_t *bwt)
35
+ {
36
+ if (bwt == 0) return;
37
+ free(bwt->pre); // pre is always heap-allocated (by mb_bwt_cache)
38
+ if (bwt->mmap) kom_munmap(bwt->mmap, bwt->mmap_len); // data/sa point into the mapped file
39
+ else { free(bwt->sa); free(bwt->data); }
40
+ free(bwt);
41
+ }
42
+
43
+ /******************
44
+ * Encode raw BWT *
45
+ ******************/
46
+
47
+ #define raw_B00(b, k) ((b)[(k)>>4]>>((~(k)&0xf)<<1)&3)
48
+
49
+ #define BWT_CNT_SHIFT 56
50
+ #define BWT_CNT_MASK ((1ULL<<BWT_CNT_SHIFT) - 1)
51
+
52
+ static uint64_t mb_bwt_data_len(uint64_t len)
53
+ {
54
+ uint64_t bwt_len, occ_len;
55
+ bwt_len = (len + 127) / 128 * 4;
56
+ occ_len = ((len + 127) / 128 + 1) * 4; // +1 for the final counts
57
+ return bwt_len + occ_len;
58
+ }
59
+
60
+ /* BWT layout. Each block consists of u64[4]+u32[8], 64 bytes in total. The
61
+ * lower 56 bits of each u64[4] (see BWT_CNT_SHIFT) store the accumulative
62
+ * count of A/C/G/T bases. The higher 8 bits store the count of A/C/G/T in the
63
+ * next 64nt. u32[8] keeps a BWT substring of 128nt in length. Because it
64
+ * follows little endian, it can also be considered as u64[4] etc.
65
+ */
66
+ mb_bwt_t *mb_bwt_init_from_raw(int is_byte, const void *raw_, uint64_t len, uint64_t primary)
67
+ {
68
+ uint64_t c[4], x[4], i, k, *last_c = 0;
69
+ mb_bwt_t *bwt;
70
+ const uint32_t *raw32 = is_byte? 0 : (const uint32_t*)raw_;
71
+ const uint8_t *raw8 = is_byte? (const uint8_t*)raw_ : 0;
72
+
73
+ bwt = mb_bwt_init();
74
+ bwt->primary = primary;
75
+ bwt->seq_len = len;
76
+ bwt->data_len = mb_bwt_data_len(len);
77
+ bwt->data = kom_calloc(uint64_t, bwt->data_len);
78
+ if (len == 0) return bwt; // nothing to encode; avoid the last-block overflow
79
+
80
+ memset(c, 0, 32);
81
+ for (i = k = 0; i < len; ++i) {
82
+ uint8_t j, a = is_byte? raw8[i]&3 : raw_B00(raw32, i);
83
+ if ((i & 0x7f) == 0) { // hard coded: each block encodes 128bp
84
+ if (i > 0) {
85
+ memcpy(&bwt->data[k], x, 32);
86
+ k += 4;
87
+ }
88
+ last_c = &bwt->data[k];
89
+ memcpy(&bwt->data[k], c, 32);
90
+ k += 4;
91
+ memset(x, 0, 32);
92
+ } else if ((i & 0x3f) == 0 && last_c) {
93
+ for (j = 0; j < 4; ++j)
94
+ last_c[j] |= (c[j] - last_c[j]) << BWT_CNT_SHIFT;
95
+ }
96
+ ++c[a];
97
+ x[(i&0x7f)>>5] |= (uint64_t)a << ((i&0x1f)<<1); // little endian
98
+ }
99
+ // the last block
100
+ memcpy(&bwt->data[k], x, 32);
101
+ k += 4;
102
+ memcpy(&bwt->data[k], c, 32);
103
+ k += 4;
104
+ assert(k == bwt->data_len);
105
+ for (i = 0, bwt->L2[0] = 0; i < 4; ++i)
106
+ bwt->L2[i+1] = bwt->L2[i] + c[i];
107
+ assert(bwt->L2[4] == len);
108
+ return bwt;
109
+ }
110
+
111
+ /********
112
+ * Rank *
113
+ ********/
114
+
115
+ #define bwt_block(b, k) ((b)->data + ((k)>>7<<3))
116
+
117
+ // retrieve a character from the $-removed BWT string. Note that mb_bwt_t::data is
118
+ // not exactly the BWT string and therefore this macro is called bwt_B0 instead of bwt_B
119
+ #define bwt_B0(b, k) ((b)->data[((k)>>7<<3) + 4 + (((k)&127)>>5)] >> (((k)&31)<<1) & 3)
120
+
121
+ static inline void mb_bwt_block_prefetch(const mb_bwt_t *bwt, uint64_t k)
122
+ {
123
+ if (k > 0) __builtin_prefetch(bwt_block(bwt, k - 1 - (k - 1 >= bwt->primary)));
124
+ }
125
+
126
+ static inline int rank_aux1(uint64_t y, uint8_t c)
127
+ {
128
+ // reduce nucleotide counting to bits counting
129
+ y = ((c&2)? y : ~y) >> 1 & ((c&1)? y : ~y) & 0x5555555555555555ull;
130
+ // count the number of 1s in y
131
+ #if 0
132
+ y = (y & 0x3333333333333333ull) + (y >> 2 & 0x3333333333333333ull);
133
+ return ((y + (y >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56;
134
+ #else
135
+ return __builtin_popcountll(y);
136
+ #endif
137
+ }
138
+
139
+ uint64_t mb_bwt_rank11(const mb_bwt_t *bwt, uint64_t k, uint8_t c)
140
+ {
141
+ const uint64_t *p, *end;
142
+ uint64_t n, mask;
143
+ if (k == 0) return 0;
144
+ if (k == bwt->seq_len + 1) return bwt->L2[c+1] - bwt->L2[c];
145
+ --k;
146
+ k -= (k >= bwt->primary); // because $ is not in bwt
147
+ mask = (k&0x7f) >= 64? (1ULL << (64 - BWT_CNT_SHIFT)) - 1 : 0;
148
+ p = bwt_block(bwt, k);
149
+ n = (p[c] & BWT_CNT_MASK) + ((p[c] >> BWT_CNT_SHIFT) & mask);
150
+ p += 4; // p points to 2-bit encoded BWT
151
+ end = p + ((k&0x7f) >> 5);
152
+ p += (k&0x7f) >= 64? 2 : 0;
153
+ // for (; p < end; ++p) n += rank_aux1(*p, c); // we go through this loop 0 or 1 time
154
+ if (p < end) n += rank_aux1(*p, c), ++p;
155
+ n += rank_aux1(*p << ((~k&0x1f) << 1), c);
156
+ if (c == 0) n -= ~k&0x1f; // "A" may be overcounted due to the shift above; this line corrects that
157
+ return n;
158
+ }
159
+
160
+ static inline const uint32_t *seek_block(const mb_bwt_t *bwt, uint64_t k, uint64_t cnt[4])
161
+ {
162
+ const uint64_t *p = bwt_block(bwt, k);
163
+ uint64_t mask = (k&0x7f) >= 64? (1ULL << (64 - BWT_CNT_SHIFT)) - 1 : 0;
164
+ cnt[0] = (p[0] & BWT_CNT_MASK) + ((p[0] >> BWT_CNT_SHIFT) & mask);
165
+ cnt[1] = (p[1] & BWT_CNT_MASK) + ((p[1] >> BWT_CNT_SHIFT) & mask);
166
+ cnt[2] = (p[2] & BWT_CNT_MASK) + ((p[2] >> BWT_CNT_SHIFT) & mask);
167
+ cnt[3] = (p[3] & BWT_CNT_MASK) + ((p[3] >> BWT_CNT_SHIFT) & mask);
168
+ return (const uint32_t*)(p + 4);
169
+ }
170
+
171
+ static inline uint32_t rank_aux4(const mb_bwt_t *bwt, uint32_t x)
172
+ {
173
+ return bwt->cnt_table[x&0xff] + bwt->cnt_table[x>>8&0xff] + bwt->cnt_table[x>>16&0xff] + bwt->cnt_table[x>>24];
174
+ }
175
+
176
+ void mb_bwt_rank1a(const mb_bwt_t *bwt, uint64_t k, uint64_t cnt[4])
177
+ {
178
+ const uint32_t *q, *end;
179
+ uint32_t x, tmp;
180
+ if (k == 0) {
181
+ memset(cnt, 0, 4 * sizeof(uint64_t));
182
+ return;
183
+ }
184
+ --k;
185
+ k -= (k >= bwt->primary); // because $ is not in bwt
186
+ q = seek_block(bwt, k, cnt);
187
+ end = q + ((k&0x7f) >> 4);
188
+ q += (k&0x7f) >= 64? 4 : 0;
189
+ for (x = 0; q < end; ++q) x += rank_aux4(bwt, *q); // NB: this assumes little endian
190
+ tmp = *q << ((~k&0xf) << 1);
191
+ x += rank_aux4(bwt, tmp) - (~k&0xf);
192
+ cnt[0] += x&0xff, cnt[1] += x>>8&0xff, cnt[2] += x>>16&0xff, cnt[3] += x>>24;
193
+ }
194
+
195
+ void mb_bwt_rank2a(const mb_bwt_t *bwt, uint64_t k, uint64_t l, uint64_t cntk[4], uint64_t cntl[4])
196
+ {
197
+ uint64_t k1 = k - 1, l1 = l - 1;
198
+ k1 -= (k1 >= bwt->primary);
199
+ l1 -= (l1 >= bwt->primary);
200
+ mb_bwt_block_prefetch(bwt, k);
201
+ if (k1>>7 != l1>>7 || k == 0 || l == 0) {
202
+ mb_bwt_block_prefetch(bwt, l);
203
+ mb_bwt_rank1a(bwt, k, cntk);
204
+ mb_bwt_rank1a(bwt, l, cntl);
205
+ } else if (l - k == 1) { // we can use a simpler procedure
206
+ uint64_t z = k - (k > bwt->primary);
207
+ mb_bwt_rank1a(bwt, k, cntk);
208
+ memcpy(cntl, cntk, 4 * sizeof(uint64_t));
209
+ ++cntl[bwt_B0(bwt, z)];
210
+ } else {
211
+ const uint32_t *q, *endk, *endl;
212
+ uint32_t x, y, tmp;
213
+ k = k1, l = l1;
214
+ q = seek_block(bwt, k, cntk);
215
+ // prepare cntk[]
216
+ endk = q + ((k&0x7f) >> 4);
217
+ endl = q + ((l&0x7f) >> 4);
218
+ q += (k&0x7f) >= 64? 4 : 0;
219
+ for (x = 0; q < endk; ++q) x += rank_aux4(bwt, *q);
220
+ y = x;
221
+ tmp = *q << ((~k&0xf) << 1);
222
+ x += rank_aux4(bwt, tmp) - (~k&0xf);
223
+ // calculate cntl[] and finalize cntk[]
224
+ for (; q < endl; ++q) y += rank_aux4(bwt, *q);
225
+ tmp = *q << ((~l&0xf) << 1);
226
+ y += rank_aux4(bwt, tmp) - (~l&0xf);
227
+ memcpy(cntl, cntk, 4 * sizeof(uint64_t));
228
+ cntk[0] += x&0xff; cntk[1] += x>>8&0xff; cntk[2] += x>>16&0xff; cntk[3] += x>>24;
229
+ cntl[0] += y&0xff; cntl[1] += y>>8&0xff; cntl[2] += y>>16&0xff; cntl[3] += y>>24;
230
+ }
231
+ }
232
+
233
+ /*********************
234
+ * Bidirectional BWT *
235
+ *********************/
236
+
237
+ void mb_bwt_extend(const mb_bwt_t *bwt, const mb_sai_t *ik, mb_sai_t ok[4], int is_back)
238
+ {
239
+ uint64_t tk[4], tl[4];
240
+ int i;
241
+ mb_bwt_rank2a(bwt, ik->x[!is_back], ik->x[!is_back] + ik->size, tk, tl);
242
+ for (i = 0; i != 4; ++i) {
243
+ ok[i].x[!is_back] = bwt->L2[i] + 1 + tk[i]; // +1 for the missing sentinel
244
+ ok[i].size = (tl[i] -= tk[i]);
245
+ }
246
+ ok[3].x[is_back] = ik->x[is_back] + (ik->x[!is_back] <= bwt->primary && ik->x[!is_back] + ik->size > bwt->primary);
247
+ ok[2].x[is_back] = ok[3].x[is_back] + tl[3];
248
+ ok[1].x[is_back] = ok[2].x[is_back] + tl[2];
249
+ ok[0].x[is_back] = ok[1].x[is_back] + tl[1];
250
+ }
251
+
252
+ // backward search from pos
253
+ static int64_t mb_bwt_back(const mb_bwt_t *f, const uint8_t *q, int64_t st, int64_t pos, int64_t min_occ, mb_sai_t *p)
254
+ {
255
+ int64_t i = pos - 1;
256
+ mb_sai_t ok[4];
257
+ assert(q[pos] < 4); // the backward pass never involves N
258
+ if (f->pre && pos - st >= f->pre_len) { // then use precomputed k-mer index instead of base-by-base extension
259
+ uint64_t z = 0, l = 0;
260
+ for (i = pos; l < f->pre_len; --i, ++l) // get the k-mer
261
+ z = z << 2 | q[i]; // NB: this loop doesn't check N
262
+ assert(z < 1<<f->pre_len*2);
263
+ *p = f->pre[z];
264
+ } else p->size = 0;
265
+ if (p->size < min_occ) { // then we need to use the standard procedure
266
+ mb_bwt_set_intv(f, q[pos], p);
267
+ i = pos - 1;
268
+ }
269
+ for (; i >= st; --i) { // backward extension
270
+ int c = q[i];
271
+ if (c > 3) break;
272
+ mb_bwt_extend(f, p, ok, 1);
273
+ if (ok[c].size < min_occ) break;
274
+ *p = ok[c];
275
+ }
276
+ return i;
277
+ }
278
+
279
+ // find super MEMs (SMEMs). See ropebwt3
280
+ int64_t mb_bwt_smem(const mb_bwt_t *f, uint32_t len, const uint8_t *q, int64_t x, int64_t min_len, int64_t min_occ, mb_sai_t *p)
281
+ {
282
+ int64_t i, j, xn;
283
+ mb_sai_t ik, ok[4];
284
+
285
+ assert(len <= INT32_MAX); // this can be relaxed if we define a new struct for mem
286
+ p->size = ik.size = 0;
287
+ if (len - x < min_len) return len;
288
+ for (i = x, xn = -1; i < x + min_len; ++i) // find the last N in [x,x+min_len)
289
+ if (q[i] > 3) xn = i;
290
+ if (xn >= 0) return xn + 1;
291
+ i = mb_bwt_back(f, q, x, x + min_len - 1, min_occ, &ik);
292
+ if (i >= x) return i + 1; // no MEM found
293
+ for (j = x + min_len; j < len; ++j) { // forward extension
294
+ int c = 3 - q[j];
295
+ if (q[j] > 3) break;
296
+ mb_bwt_extend(f, &ik, ok, 0);
297
+ if (ok[c].size < min_occ) break;
298
+ ik = ok[c];
299
+ }
300
+ *p = ik;
301
+ p->info = (uint64_t)x<<32 | j;
302
+ if (j == len) return len; // reaching end; no need to do another round
303
+ i = q[j] > 3? j : mb_bwt_back(f, q, x + 1, j, min_occ, &ik);
304
+ return i + 1;
305
+ }
306
+
307
+ /**************
308
+ * Batch SMEM *
309
+ **************/
310
+
311
+ typedef struct { // a simplified version of kdq
312
+ int32_t front, count, cap;
313
+ int32_t *a;
314
+ } tiny_queue_t;
315
+
316
+ static void tq_init(void *km, tiny_queue_t *q, int32_t n)
317
+ {
318
+ q->cap = n;
319
+ kom_roundup32(q->cap);
320
+ q->a = Kcalloc(km, int32_t, q->cap);
321
+ q->front = q->count = 0;
322
+ }
323
+
324
+ static inline void tq_push(tiny_queue_t *q, int32_t x)
325
+ {
326
+ q->a[((q->count++) + q->front) & (q->cap - 1)] = x;
327
+ }
328
+
329
+ static inline int32_t tq_shift(tiny_queue_t *q)
330
+ {
331
+ int32_t x;
332
+ if (q->count == 0) return -1;
333
+ x = q->a[q->front++];
334
+ q->front &= q->cap - 1;
335
+ --q->count;
336
+ return x;
337
+ }
338
+
339
+ static inline void se_one_step_back(const mb_bwt_t *bwt, mb_smem_entry_t *s)
340
+ {
341
+ mb_sai_t ok[4];
342
+ int32_t c = s->q[s->i];
343
+ assert(c < 4); // shouldn't happen
344
+ mb_bwt_extend(bwt, &s->p, ok, 1);
345
+ if (ok[c].size < s->min_occ) { // move back to stage1
346
+ s->x = s->i + 1;
347
+ s->stage = 1;
348
+ } else { // stay in the two backward stages
349
+ s->p = ok[c];
350
+ s->i--;
351
+ mb_bwt_block_prefetch(bwt, s->p.x[0]); // prefetch for the next backward iteration
352
+ mb_bwt_block_prefetch(bwt, s->p.x[0] + s->p.size);
353
+ }
354
+ }
355
+
356
+ void mb_bwt_smem_batch(void *km, const mb_bwt_t *bwt, int32_t n, mb_smem_entry_t *a)
357
+ {
358
+ int32_t i;
359
+ tiny_queue_t tq;
360
+
361
+ // initialize
362
+ tq_init(km, &tq, n);
363
+ for (i = 0; i < n; ++i) {
364
+ mb_smem_entry_t *s = &a[i];
365
+ tq_push(&tq, i);
366
+ s->stage = 1;
367
+ s->x = s->st;
368
+ if (s->v->m < 64) { // preallocate to avoid frequent krealloc(), which can be slow
369
+ s->v->m = 64;
370
+ s->v->a = Krealloc(km, mb_sai_t, s->v->a, s->v->m);
371
+ }
372
+ }
373
+
374
+ // core loop
375
+ while (tq.count > 0) {
376
+ int32_t idx;
377
+ mb_smem_entry_t *s;
378
+
379
+ idx = tq_shift(&tq);
380
+ s = &a[idx];
381
+ if (s->stage == 1) { // set interval for the first backward pass in smem; require ->x
382
+ int32_t i, xn;
383
+ if (s->en - s->x < s->min_len)
384
+ continue; // IMPORTANT: this skips the tq_push() at the end of this long while loop
385
+ for (i = s->x, xn = -1; i < s->x + s->min_len; ++i) // find the position of the last N
386
+ if (s->q[i] > 3) xn = i;
387
+ if (xn >= 0) { // skip N and stay in stage 1
388
+ s->x = xn + 1;
389
+ } else {
390
+ s->i = s->x + s->min_len - 1;
391
+ if (bwt->pre && s->min_len >= bwt->pre_len) { // get k-mer for prefetch
392
+ for (i = 0, s->kmer = 0; i < bwt->pre_len; ++i, s->i--)
393
+ s->kmer = s->kmer << 2 | s->q[s->i]; // backward pass shouldn't meet N
394
+ __builtin_prefetch(&bwt->pre[s->kmer]);
395
+ s->stage = 2;
396
+ } else { // skip stage 2
397
+ mb_bwt_set_intv(bwt, s->q[s->i--], &s->p);
398
+ s->stage = 3;
399
+ }
400
+ }
401
+ } else if (s->stage == 2 || s->stage == 5) { // k-mer lookup
402
+ s->p = bwt->pre[s->kmer];
403
+ if (s->p.size < s->min_occ) { // jumped too far with the k-mer cache; revert
404
+ s->i += bwt->pre_len;
405
+ mb_bwt_set_intv(bwt, s->q[s->i--], &s->p);
406
+ }
407
+ s->stage++;
408
+ } else if (s->stage == 3) { // first backward pass; require ->{i,p}
409
+ if (s->i < s->x) { // move to the next stage
410
+ mb_bwt_block_prefetch(bwt, s->p.x[1]); // prefetch for the forward pass
411
+ mb_bwt_block_prefetch(bwt, s->p.x[1] + s->p.size);
412
+ s->i = s->x + s->min_len;
413
+ s->stage = 4;
414
+ } else se_one_step_back(bwt, s);
415
+ } else if (s->stage == 4) { // forward pass; require ->{i,p}
416
+ if (s->i == s->en) {
417
+ s->p.info = (uint64_t)s->x << 32 | s->i;
418
+ Kgrow(km, mb_sai_t, s->v->a, s->v->n, s->v->m);
419
+ s->v->a[s->v->n++] = s->p; // save the interval
420
+ continue; // trigger termination as tq_push() at the end of the loop is skipped
421
+ } else {
422
+ int32_t i, c = 3 - (int32_t)s->q[s->i];
423
+ mb_sai_t ok[4];
424
+ if (c >= 0) mb_bwt_extend(bwt, &s->p, ok, 0);
425
+ if (c >= 0 && ok[c].size >= s->min_occ) { // stay in stage 4
426
+ s->p = ok[c];
427
+ s->i++;
428
+ mb_bwt_block_prefetch(bwt, s->p.x[1]);
429
+ mb_bwt_block_prefetch(bwt, s->p.x[1] + s->p.size);
430
+ } else {
431
+ s->p.info = (uint64_t)s->x << 32 | s->i;
432
+ Kgrow(km, mb_sai_t, s->v->a, s->v->n, s->v->m);
433
+ s->v->a[s->v->n++] = s->p; // save the interval
434
+ if (c < 0) { // if N, skip it and move back to stage 1
435
+ s->x = s->i + 1;
436
+ s->stage = 1;
437
+ } else if (bwt->pre && s->i - s->x - 1 >= bwt->pre_len) { // get k-mer
438
+ for (i = 0, s->kmer = 0; i < bwt->pre_len; ++i, s->i--)
439
+ s->kmer = s->kmer << 2 | s->q[s->i];
440
+ __builtin_prefetch(&bwt->pre[s->kmer]);
441
+ s->stage = 5;
442
+ } else { // skip stage 5
443
+ mb_bwt_set_intv(bwt, s->q[s->i--], &s->p);
444
+ s->stage = 6;
445
+ }
446
+ }
447
+ }
448
+ } else if (s->stage == 6) { // second backward pass
449
+ if (s->i < s->x + 1) {
450
+ s->x = s->i + 1;
451
+ s->stage = 1;
452
+ } else se_one_step_back(bwt, s);
453
+ }
454
+ tq_push(&tq, idx);
455
+ }
456
+ kfree(km, tq.a);
457
+ }
458
+
459
+ /***************************
460
+ * Suffix array operations *
461
+ ***************************/
462
+
463
+ static inline uint64_t bwt_invPsi(const mb_bwt_t *bwt, uint64_t k) // compute inverse CSA
464
+ {
465
+ uint64_t x = k - (k > bwt->primary);
466
+ int c = bwt_B0(bwt, x);
467
+ x = bwt->L2[c] + 1 + mb_bwt_rank11(bwt, k, c); // +1 to account for the sentinel
468
+ return k == bwt->primary? 0 : x;
469
+ }
470
+
471
+ // bwt->bwt and bwt->occ must be precalculated
472
+ void mb_bwt_gen_sa(mb_bwt_t *bwt, uint32_t sa_bit)
473
+ {
474
+ uint64_t isa, sa, i, mask; // S(isa) = sa
475
+
476
+ assert(bwt->data);
477
+ if (bwt->sa) free(bwt->sa);
478
+ bwt->sa_bit = sa_bit;
479
+ bwt->n_sa = (bwt->seq_len + (1<<sa_bit)) >> sa_bit;
480
+ bwt->sa = kom_calloc(uint64_t, bwt->n_sa);
481
+
482
+ // calculate SA value
483
+ isa = 0, sa = bwt->seq_len, mask = (1ULL<<sa_bit) - 1;
484
+ for (i = 0; i < bwt->seq_len; ++i) {
485
+ if ((isa & mask) == 0) bwt->sa[isa >> bwt->sa_bit] = sa;
486
+ --sa;
487
+ isa = bwt_invPsi(bwt, isa);
488
+ }
489
+ if ((isa & mask) == 0) bwt->sa[isa >> bwt->sa_bit] = sa;
490
+ bwt->sa[0] = (uint64_t)-1; // before this line, bwt->sa[0] = bwt->seq_len
491
+ }
492
+
493
+ uint64_t mb_bwt_sa(const mb_bwt_t *bwt, uint64_t k)
494
+ {
495
+ uint64_t sa = 0, mask = (1ULL<<bwt->sa_bit) - 1;
496
+ while (k & mask) {
497
+ ++sa;
498
+ k = bwt_invPsi(bwt, k);
499
+ }
500
+ // without setting bwt->sa[0] = -1, the following line should be
501
+ // changed to (sa + bwt->sa[k/bwt->sa_intv]) % (bwt->seq_len + 1)
502
+ return sa + bwt->sa[k >> bwt->sa_bit];
503
+ }
504
+
505
+ void mb_bwt_sa_batch(void *km, const mb_bwt_t *bwt, int64_t n, uint64_t *x)
506
+ {
507
+ uint64_t mask = (1ULL<<bwt->sa_bit) - 1;
508
+ int64_t i, step = 0, r = n;
509
+ kom128_t *z;
510
+ if (n <= 0) return;
511
+ z = Kmalloc(km, kom128_t, n);
512
+ for (i = 0; i < n; ++i) {
513
+ z[i].x = x[i], z[i].y = i;
514
+ if ((z[i].x & mask) == 0)
515
+ __builtin_prefetch(&bwt->sa[z[i].x >> bwt->sa_bit]);
516
+ else
517
+ mb_bwt_block_prefetch(bwt, z[i].x);
518
+ }
519
+ for (step = 0; r > 0; ++step) {
520
+ int64_t r0 = r;
521
+ for (i = 0, r = 0; i < r0; ++i) {
522
+ if ((z[i].x & mask) == 0)
523
+ x[z[i].y] = step + bwt->sa[z[i].x >> bwt->sa_bit];
524
+ else z[r++] = z[i];
525
+ }
526
+ for (i = 0; i < r; ++i) {
527
+ z[i].x = bwt_invPsi(bwt, z[i].x);
528
+ if ((z[i].x & mask) == 0)
529
+ __builtin_prefetch(&bwt->sa[z[i].x >> bwt->sa_bit]);
530
+ else
531
+ mb_bwt_block_prefetch(bwt, z[i].x);
532
+ }
533
+ }
534
+ kfree(km, z);
535
+ }
536
+
537
+ /******************
538
+ * k-mer counting *
539
+ ******************/
540
+
541
+ typedef struct {
542
+ mb_sai_t p;
543
+ int d, c;
544
+ } count_pair64_t;
545
+
546
+ void mb_bwt_count_kmer(const mb_bwt_t *bwt, int32_t depth, mb_sai_t *s) // adapted from kount in ropebwt3
547
+ {
548
+ count_pair64_t *p, stack[64];
549
+ int32_t i, a, s_top = 0;
550
+ uint8_t str[16];
551
+ assert(depth <= 15);
552
+ for (a = 0; a < 4; ++a) {
553
+ p = &stack[s_top++];
554
+ mb_bwt_set_intv(bwt, a, &p->p);
555
+ p->d = 1, p->c = a;
556
+ }
557
+ while (s_top > 0) {
558
+ count_pair64_t top = stack[--s_top];
559
+ mb_sai_t ok[4];
560
+ if (top.d > 0) str[depth - top.d] = top.c;
561
+ mb_bwt_extend(bwt, &top.p, ok, 1);
562
+ for (a = 0; a < 4; ++a) {
563
+ str[depth - top.d - 1] = a;
564
+ if (top.d != depth - 1) {
565
+ p = &stack[s_top++];
566
+ p->p = ok[a];
567
+ p->d = top.d + 1;
568
+ p->c = a;
569
+ } else { // reaching the length; store in s[]
570
+ uint64_t x = 0;
571
+ for (i = 0; i < depth; ++i)
572
+ x |= (uint64_t)str[i] << i * 2;
573
+ s[x] = ok[a];
574
+ }
575
+ }
576
+ }
577
+ }
578
+
579
+ void mb_bwt_cache(mb_bwt_t *bwt, int32_t len)
580
+ {
581
+ if (bwt->pre) free(bwt->pre);
582
+ bwt->pre_len = len;
583
+ bwt->pre = kom_calloc(mb_sai_t, 1 << len*2);
584
+ mb_bwt_count_kmer(bwt, len, bwt->pre);
585
+ }
586
+
587
+ /*************************
588
+ * Read/write BWT and SA *
589
+ *************************/
590
+
591
+ static uint64_t read_huge(FILE *fp, uint64_t size, void *a)
592
+ { // Mac/Darwin has a bug when reading data longer than 2GB. This function fixes this issue by reading data in small chunks
593
+ const int bufsize = 0x1000000; // 16M block
594
+ uint64_t offset = 0;
595
+ while (size) {
596
+ int x = bufsize < size? bufsize : size;
597
+ if ((x = fread(a + offset, 1, x, fp)) == 0) break;
598
+ size -= x; offset += x;
599
+ }
600
+ return offset;
601
+ }
602
+
603
+ mb_bwt_t *mb_bwt_load_raw(const char *fn)
604
+ {
605
+ mb_bwt_t *bwt;
606
+ uint32_t *raw;
607
+ uint64_t L2[5], primary, raw_size;
608
+ FILE *fp;
609
+
610
+ fp = fopen(fn, "rb");
611
+ fseek(fp, 0, SEEK_END);
612
+ raw_size = (ftell(fp) - sizeof(uint64_t) * 5) >> 2;
613
+ raw = kom_calloc(uint32_t, raw_size);
614
+ fseek(fp, 0, SEEK_SET);
615
+ fread(&primary, sizeof(uint64_t), 1, fp);
616
+ fread(L2 + 1, sizeof(uint64_t), 4, fp);
617
+ L2[0] = 0;
618
+ read_huge(fp, raw_size<<2, raw);
619
+ fclose(fp);
620
+ bwt = mb_bwt_init_from_raw(0, raw, L2[4], primary);
621
+ free(raw);
622
+ return bwt;
623
+ }
624
+
625
+ int mb_bwt_save(const char *fn, const mb_bwt_t *bwt)
626
+ {
627
+ FILE *fp;
628
+ fp = fopen(fn, "wb");
629
+ if (fp == 0) return -1;
630
+ fwrite(MB_MAGIC, 1, 4, fp);
631
+ fwrite(&bwt->sa_bit, 4, 1, fp);
632
+ fwrite(&bwt->primary, 8, 1, fp);
633
+ fwrite(&bwt->L2[1], 8, 4, fp);
634
+ fwrite(bwt->data, 8, bwt->data_len, fp);
635
+ fwrite(&bwt->n_sa, 8, 1, fp);
636
+ if (bwt->sa_bit != (uint32_t)-1 && bwt->n_sa > 0 && bwt->sa)
637
+ fwrite(bwt->sa, 8, bwt->n_sa, fp);
638
+ fclose(fp);
639
+ return 0;
640
+ }
641
+
642
+ mb_bwt_t *mb_bwt_load(const char *fn)
643
+ {
644
+ FILE *fp;
645
+ char magic[4];
646
+ uint64_t x[5];
647
+ mb_bwt_t *bwt;
648
+
649
+ fp = fopen(fn, "rb");
650
+ if (fp == 0) return 0;
651
+ fread(magic, 1, 4, fp);
652
+ if (strncmp(magic, MB_MAGIC, 4) != 0) {
653
+ fclose(fp);
654
+ return 0;
655
+ }
656
+ bwt = mb_bwt_init();
657
+ fread(&bwt->sa_bit, 4, 1, fp);
658
+ fread(x, 8, 5, fp);
659
+ bwt->primary = x[0];
660
+ memcpy(&bwt->L2[1], &x[1], 32);
661
+ bwt->seq_len = bwt->L2[4];
662
+ bwt->data_len = mb_bwt_data_len(bwt->seq_len);
663
+ bwt->data = kom_calloc(uint64_t, bwt->data_len);
664
+ read_huge(fp, bwt->data_len << 3, bwt->data);
665
+ fread(&bwt->n_sa, 8, 1, fp);
666
+ if (bwt->sa_bit != (uint32_t)-1 && bwt->n_sa > 0) {
667
+ uint64_t expected_n_sa = (bwt->seq_len + (1ULL << bwt->sa_bit)) >> bwt->sa_bit;
668
+ if (bwt->n_sa != expected_n_sa) {
669
+ mb_bwt_destroy(bwt);
670
+ return NULL;
671
+ }
672
+ bwt->sa = kom_malloc(uint64_t, bwt->n_sa);
673
+ fread(bwt->sa, 8, bwt->n_sa, fp);
674
+ }
675
+ fclose(fp);
676
+ return bwt;
677
+ }
678
+
679
+ mb_bwt_t *mb_bwt_load_mmap(const char *fn, int preload)
680
+ {
681
+ uint8_t *base;
682
+ size_t map_len;
683
+ uint64_t data_off, sa_off, n_sa_off, min_len;
684
+ mb_bwt_t *bwt;
685
+
686
+ base = (uint8_t*)kom_mmap_file(fn, &map_len, preload);
687
+ if (base == 0) return 0;
688
+ if (map_len < 48 || strncmp((const char*)base, MB_MAGIC, 4) != 0) { kom_munmap(base, map_len); return 0; }
689
+
690
+ bwt = mb_bwt_init();
691
+ bwt->mmap = base;
692
+ bwt->mmap_len = map_len;
693
+ bwt->sa_bit = *(const uint32_t*)(base + 4);
694
+ bwt->primary = *(const uint64_t*)(base + 8);
695
+ memcpy(&bwt->L2[1], base + 16, 32);
696
+ bwt->seq_len = bwt->L2[4];
697
+ bwt->data_len = mb_bwt_data_len(bwt->seq_len);
698
+
699
+ data_off = 48;
700
+ n_sa_off = data_off + bwt->data_len * 8;
701
+ sa_off = n_sa_off + 8;
702
+ min_len = sa_off; // file must at least hold up to n_sa
703
+ if (map_len < min_len) { mb_bwt_destroy(bwt); return 0; }
704
+ bwt->data = (uint64_t*)(base + data_off);
705
+ bwt->n_sa = *(const uint64_t*)(base + n_sa_off);
706
+ if (bwt->sa_bit != (uint32_t)-1 && bwt->n_sa > 0) {
707
+ uint64_t expected_n_sa = (bwt->seq_len + (1ULL << bwt->sa_bit)) >> bwt->sa_bit;
708
+ if (bwt->n_sa != expected_n_sa || map_len < sa_off + bwt->n_sa * 8) {
709
+ mb_bwt_destroy(bwt);
710
+ return 0;
711
+ }
712
+ bwt->sa = (uint64_t*)(base + sa_off);
713
+ }
714
+ return bwt;
715
+ }