-
Notifications
You must be signed in to change notification settings - Fork 0
/
bwt_lcp.cpp
596 lines (533 loc) · 16.9 KB
/
bwt_lcp.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include <zlib.h>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <cmath>
#include <cstring>
#include <cassert>
#include <unistd.h>
#include <sys/mman.h>
#include "kseq.h"
#include <vector>
#include <algorithm>
#include <thread>
#include <atomic>
#include <mutex>
#include "base_types.hpp"
#include "common_types.hpp"
#include "encoding.hpp"
#include "io.hpp"
KSEQ_INIT(gzFile, gzread)
static unsigned concurrency_level;
template <typename T, unsigned int N>
inline
void fill_with_0(T* const v) {
for (unsigned i = 0; i < N; ++i) v[i] = static_cast<T>(0);
}
template <typename T>
inline
T atleast1(T num) {
return num >= 1 ? num : 1;
}
struct base_env_t {
const strlen_t k;
const strnum_t m;
const fnames_t& IX_names;
lcp_writer_t& LCP;
qp_readers_t Qp;
strlen_t p;
luint_t* const midpoints;
std::vector<bool> E;
std::mutex Emutex;
base_env_t(
const strlen_t _k,
const strnum_t _m,
const fnames_t& _IX_names,
lcp_writer_t& _LCP
)
: k(_k),
m(_m),
IX_names(_IX_names),
LCP(_LCP),
p(0),
midpoints{new luint_t[concurrency_level]},
E(static_cast<luint_t>(_m)*(_k+1), false)
{
const luint_t tot = static_cast<luint_t>(m)*(k+1);
const luint_t step =
atleast1(tot/concurrency_level + ((tot%concurrency_level)>(concurrency_level/2) ? 1 : 0));
for (luint_t i = 0, pos = step; i<concurrency_level-1; ++i, pos += step) {
midpoints[i] = pos;
}
midpoints[concurrency_level-1] = tot;
E[tot-1] = true;
}
base_env_t(const base_env_t&) = delete;
~base_env_t() {
delete [] midpoints;
}
};
struct bfv_env_t {
lcp_writer_t LCP;
const strlen_t k;
bool has_pseg_wider_than_1;
std::vector<luint_t> end_pos;
std::vector<std::vector<strnum_t>> end_pos_Qp;
bfv_env_t(const base_env_t& base_env)
: LCP(base_env.LCP.clone()),
k{base_env.k},
has_pseg_wider_than_1(false)
{}
bfv_env_t(const bfv_env_t&) = delete;
bfv_env_t(bfv_env_t&& other)
: LCP(std::move(other.LCP)),
k{other.k},
has_pseg_wider_than_1(other.has_pseg_wider_than_1)
{
}
~bfv_env_t() {
}
};
void compute_partial_BWT(
const strlen_t k, const strnum_t m,
const fnames_t& S_tpl,
const fnames_t& B_tpl
) {
const fnames_t N_tpl[] = {
template_generator("supportfile-N0_%d.tmp", 0, ALPHSIZE),
template_generator("supportfile-N1_%d.tmp", 0, ALPHSIZE),
};
{ // B0 (and N0)
copy_file(S_tpl[0], B_tpl[0]);
multi_strnum_writer_t N0(N_tpl[0]);
for (strnum_t i = 0; i < m; ++i) {
N0.write(0, i);
}
}
{ // B1...Bk
for (strlen_t l = 1; l <= k; ++l) {
{
enc_fasta_reader_t Bl1(B_tpl[l-1]);
multi_strnum_reader_t Nl1(N_tpl[(l + 1) % 2]);
multi_strnum_writer_t Nl(N_tpl[l % 2]);
for (strnum_t i = 0; i < m; ++i) {
Nl.write(Bl1.read(), Nl1.read());
}
}
{
enc_fasta_rnd_reader_t Sl(S_tpl[l], m);
multi_strnum_reader_t Nl(N_tpl[l % 2]);
enc_fasta_writer_t Bl(B_tpl[l]);
for (strnum_t i = 0; i < m; ++i) {
const strnum_t Nli = Nl.read();
const enc_nucl_t SlNli = Sl.read(Nli);
Bl.write(SlNli);
}
}
}
}
// Cleaning
remove_files(N_tpl[0]);
remove_files(N_tpl[1]);
}
inline
bool compute_Lpos(
luint_t* const Lpos,
const luint_t b,
const luint_t* Lcount
) {
Lpos[0] = b;
for (enc_nucl_t sigma = 1; sigma<ALPHSIZE; ++sigma) {
Lpos[sigma] = Lpos[sigma-1] + Lcount[sigma-1];
}
bool this_has_pseg_wider_than_1 = false;
for (enc_nucl_t sigma = 1; sigma<ALPHSIZE; ++sigma) {
this_has_pseg_wider_than_1 |= (Lcount[sigma] > 1);
}
return this_has_pseg_wider_than_1;
}
void update_LCP(
lcp_writer_t& LCP,
const strlen_t p,
const luint_t base_pos,
const luint_t* Lcount
) {
LCP.seek(base_pos);
for (enc_nucl_t sigma = 1; sigma<ALPHSIZE; ++sigma) {
if (Lcount[sigma]==0) continue;
LCP.skip();
for (luint_t rh = 1; rh < Lcount[sigma]; ++rh) {
LCP.write(p);
}
}
}
void compute_Q1_l(
const strlen_t k,
const occ_countv_t& occv,
const fnames_t& Q_tpls
) {
for (strlen_t l = 1; l <= k; ++l) {
plain_fasta_writer_t Ql(Q_tpls[l]);
for (enc_nucl_t sigma = 0; sigma < ALPHSIZE; ++sigma) {
for (luint_t i = 0; i < occv[l][sigma]; ++i) {
Ql.write(sigma);
}
}
}
}
#define MARGIN (64) //(__CHAR_BIT__*sizeof(unsigned long long))
void compute_interleave_LCP_segment(
base_env_t& base_env,
bfv_env_t& bfv_env,
const luint_t segm_b, const luint_t segm_e,
const strlen_t p,
const std::vector<strnum_t>& Qppos,
unsigned idx
) {
std::cout << ".";
const luint_t tot = segm_e - segm_b;
const strlen_t k = base_env.k;
auto& E = base_env.E;
auto& LCP = bfv_env.LCP;
std::vector<strnum_t> Qppos_int = Qppos;
mmap_reader_t<strlen_t> IX0(base_env.IX_names[(p-1)%2]);
mmap_writer_t<strlen_t, ALPHSIZE> IX1(base_env.IX_names[p%2]);
qp_readers_t Qp, Qpbis;
for (strlen_t l = p+1; l <= k+1; ++l) {
Qp.emplace_back(base_env.Qp[l-p-1].cloneAndSeek(Qppos[l-1]));
Qpbis.emplace_back(base_env.Qp[l-p-1].cloneAndSeek(Qppos[l-1]));
}
luint_t Lcount[ALPHSIZE];
fill_with_0<luint_t, ALPHSIZE>(Lcount);
luint_t Lpos[ALPHSIZE];
luint_t b = segm_b;
bool has_pseg_wider_than_1 = false;
bfv_env.end_pos.clear();
bfv_env.end_pos_Qp.clear();
IX0.seek(segm_b);
IX1.seek(ALPHSIZE-1, segm_b);
for (luint_t i = segm_b; i < segm_e; ++i) {
const strlen_t IX0i = IX0.read();
const enc_nucl_t c = (IX0i < p) ? 0 : Qp[IX0i-p].read();
///printf("p=%d i=%lu IX=%d c=%x L%x=%d\n", p, i, IX0i, c, c, IX0i);
++Qppos_int[IX0i];
++Lcount[c];
if (E[i]) {
///printf("finished segment at pos %lu\n", i);
if (i+1 >= base_env.midpoints[idx-1]) {
bfv_env.end_pos.push_back(i + 1);
bfv_env.end_pos_Qp.push_back(Qppos_int);
++idx;
}
if (b==i) {
// Special case of a single-element segment
if (IX0i >= p) Qpbis[IX0i-p].skip(); // advance Qpbis as well
IX1.write(ALPHSIZE-1, IX0i);
Lcount[c] = 0;
b = i + 1;
} else {
const bool this_has_pseg_wider_than_1 = compute_Lpos(Lpos, b, Lcount);
has_pseg_wider_than_1 |= this_has_pseg_wider_than_1;
for (enc_nucl_t sigma = 0; sigma<ALPHSIZE; ++sigma) {
if (Lcount[sigma] > 0) {
const luint_t new_e = Lpos[sigma]+Lcount[sigma]-1-segm_b;
///printf("ending pos for sigma=%d in idx=%lu\n", sigma, Lpos[sigma]+Lcount[sigma]-1);
if ((new_e < MARGIN) | (tot-new_e < MARGIN)) {
std::lock_guard<std::mutex> lg(base_env.Emutex);
E[segm_b+new_e] = true;
} else {
E[segm_b+new_e] = true;
}
}
}
if (this_has_pseg_wider_than_1) {
update_LCP(LCP, p, Lpos[1], Lcount);
}
IX0.seek(b);
IX1.seek(Lpos);
for (luint_t r = b; r <= i; ++r) {
const strlen_t IX0r = IX0.read();
const enc_nucl_t cr = (IX0r < p) ? 0 : Qpbis[IX0r-p].read();
///printf("*p=%d i=%lu IX=%d c=%x L%x=%d\n", p, r, IX0r, cr, cr, IX0r);
IX1.write(cr, IX0r);
}
fill_with_0<luint_t, ALPHSIZE>(Lcount);
b = i + 1;
}
}
}
bfv_env.has_pseg_wider_than_1 = has_pseg_wider_than_1;
}
void worker_IX(const unsigned idx,
base_env_t& base_env,
bfv_env_t& bfv_env,
const std::vector<luint_t>& this_end_pos,
const std::vector<std::vector<strnum_t>>& this_end_pos_Qp
) {
const luint_t segm_b = this_end_pos[idx-1];
const luint_t segm_e = this_end_pos[idx];
compute_interleave_LCP_segment(base_env, bfv_env, segm_b, segm_e, base_env.p,
this_end_pos_Qp[idx-1], idx);
}
void worker_Qp(std::atomic<luint_t>& shared_l,
const strlen_t k, const strnum_t m,
const occ_countv_t& occv,
const fnames_t& B_tpl,
const fnames_t& prevQ_tpl,
const fnames_t& currQ_tpl
) {
luint_t l;
while ((l = shared_l.fetch_add(1)) <= k) {
assert(l <= k);
enc_fasta_reader_t Bl1(B_tpl[l]);
plain_fasta_reader_t Ql1(prevQ_tpl[l-1]);
create_empty_file<enc_nucl_t>(currQ_tpl[l], m);
mmap_writer_t<enc_nucl_t, ALPHSIZE> Ql(currQ_tpl[l]);
luint_t cumsum = 0;
for (enc_nucl_t sigma = 0; sigma < ALPHSIZE; ++sigma) {
Ql.seek(sigma, cumsum);
cumsum += occv[l][sigma];
}
for (strnum_t i = 0; i < m; ++i) {
const enc_nucl_t Ql1i = Ql1.read();
const enc_nucl_t Bl1i = Bl1.read();
Ql.write(Bl1i, Ql1i);
}
remove_file(prevQ_tpl[l-1]);
}
}
void compute_interleave_LCP(
const strlen_t k, const strnum_t m,
const occ_countv_t& occv,
const fnames_t& B_tpl,
const fname_t& IXk_name
) {
const fnames_t IX_names = {
"supportfile-IX0.tmp",
"supportfile-IX1.tmp",
};
const fnames_t Q_tpls[] = {
template_generator("supportfile-Q0_%.3d.tmp", 0, k+1),
template_generator("supportfile-Q1_%.3d.tmp", 0, k+1),
};
const luint_t tot = static_cast<luint_t>(m)*(k+1);
const fnames_t L_tpl = template_generator("supportfile-L_%d.tmp", 0, ALPHSIZE);
const fname_t LCP_name = "outfile-LCP.bin";
{ // IX0/1 and LCP
strlen_writer_t IX0(IX_names[0]);
for (strlen_t l = 0; l <= k; ++l) {
for (strnum_t i = 0; i < m; ++i) {
IX0.write(l);
}
}
create_empty_file<strlen_t>(LCP_name, tot);
create_empty_file<strlen_t>(IX_names[1], tot);
}
compute_Q1_l(k, occv, Q_tpls[0]);
lcp_writer_t LCP(LCP_name);
LCP.seek(0);
LCP.write(static_cast<strlen_t>(-1));
bool has_pseg_wider_than_1 = true;
base_env_t base_env{k, m, IX_names, LCP};
std::vector<luint_t> end_pos(concurrency_level+1, tot);
end_pos[0] = 0;
std::vector<std::vector<strnum_t>> end_pos_Qp(concurrency_level+1, std::vector<strnum_t>(k+1, 0));
std::vector<bfv_env_t> bfv_envs;
for (unsigned idx = 0; idx < concurrency_level; ++idx)
bfv_envs.emplace_back(base_env);
while (has_pseg_wider_than_1) {
has_pseg_wider_than_1 = false;
++base_env.p;
std::cout << " - iteration " << static_cast<fuint_t>(base_env.p) << std::endl;
const fnames_t& prevQ_tpl = Q_tpls[(base_env.p-1)%2];
const fnames_t& currQ_tpl = Q_tpls[base_env.p%2];
std::vector<luint_t> this_end_pos = end_pos;
std::vector<std::vector<strnum_t>> this_end_pos_Qp = end_pos_Qp;
{
std::cout << " - compute IX";
for (strlen_t l = base_env.p+1; l <= k+1; ++l) {
base_env.Qp.emplace_back(prevQ_tpl[l-1]);
}
std::vector<std::thread> threads;
unsigned idx;
for (idx = 1; idx <= concurrency_level && this_end_pos[idx-1] < tot; ++idx) {
threads
.emplace_back(worker_IX,
idx, std::ref(base_env), std::ref(bfv_envs[idx-1]),
std::cref(this_end_pos), std::cref(this_end_pos_Qp)
);
}
for (auto& t: threads) {
t.join();
}
for (unsigned j = 0; j < idx-1; ++j) {
has_pseg_wider_than_1 |= bfv_envs[j].has_pseg_wider_than_1;
}
std::vector<size_t> pos(idx-1, 0);
unsigned midi = 0;
unsigned finished = 0;
for (unsigned j = 0; j < idx-1; ++j) {
while (pos[j] < bfv_envs[j].end_pos.size() &&
bfv_envs[j].end_pos[pos[j]] < base_env.midpoints[midi]) {
++pos[j];
}
if (pos[j] >= bfv_envs[j].end_pos.size()) ++finished;
}
while (finished < idx - 1) {
luint_t cmin_j = 0;
for (unsigned j = 0; j < idx-1; ++j) {
if (
pos[j] < bfv_envs[j].end_pos.size() &&
bfv_envs[j].end_pos[pos[j]] >= base_env.midpoints[midi] &&
(bfv_envs[j].end_pos[pos[j]] < bfv_envs[cmin_j].end_pos[pos[cmin_j]] ||
bfv_envs[cmin_j].end_pos[pos[cmin_j]] < base_env.midpoints[midi])
) {
cmin_j = j;
}
}
if (pos[cmin_j] >= bfv_envs[cmin_j].end_pos.size()) break;
end_pos[midi+1]= bfv_envs[cmin_j].end_pos[pos[cmin_j]];
end_pos_Qp[midi+1] = bfv_envs[cmin_j].end_pos_Qp[pos[cmin_j]];
++midi;
++pos[cmin_j];
finished = 0;
for (unsigned j = 0; j < idx-1; ++j) {
while (pos[j] < bfv_envs[j].end_pos.size() &&
bfv_envs[j].end_pos[pos[j]] < base_env.midpoints[midi]) {
++pos[j];
}
if (pos[j] >= bfv_envs[j].end_pos.size()) ++finished;
}
}
base_env.Qp.clear();
std::cout << std::endl;
}
if (has_pseg_wider_than_1) {
std::cout << " - compute Qp" << std::endl;
remove_file(currQ_tpl[base_env.p-1]);
remove_file(prevQ_tpl[k]);
std::vector<std::thread> threads;
std::atomic<luint_t> shared_l(base_env.p+1);
for (unsigned idx = 0; idx < concurrency_level; ++idx) {
threads
.emplace_back(worker_Qp,
std::ref(shared_l), k, m, std::cref(occv),
std::cref(B_tpl), std::cref(prevQ_tpl), std::cref(currQ_tpl)
);
}
for (auto& t: threads) {
t.join();
}
} else {
remove_file(currQ_tpl[base_env.p-1]);
remove_file(prevQ_tpl[k]);
for (strlen_t l = base_env.p+1; l <= k; ++l) {
remove_file(prevQ_tpl[l-1]);
}
}
}
rename_file(IX_names[base_env.p%2], IXk_name);
remove_file(IX_names[(base_env.p-1)%2]);
}
void compute_BWT(
const strlen_t k, const strnum_t m,
const fnames_t& B_tpl,
const fname_t& IXk_name
) {
const strlen_t k1 = k+1;
const luint_t tot = static_cast<luint_t>(m)*k1;
strlen_reader_t IXk(IXk_name);
std::vector<enc_fasta_reader_t> Bl;
for (const fname_t& fname: B_tpl) {
Bl.emplace_back(fname);
}
enc_fasta_writer_t B("outfile-BWT.bin");
for (luint_t i = 0; i < tot; ++i) {
const strlen_t l = IXk.read();
const enc_nucl_t c = Bl[(l+1)%k1].read();
B.write(c);
}
remove_files(B_tpl);
remove_file(IXk_name);
}
int main(int argc, char *argv[]) {
// Initialization
gzFile fp;
kseq_t *seq;
// Error if no file is given as input
if (argc < 2 || argc > 3) {
std::cerr << "Usage: " << argv[0] << " <in.fasta> [<nthreads>]" << std::endl;
return 1;
}
if (argc == 3) {
int nthreads = std::atoi(argv[2]);
if (nthreads <= 0) {
std::cerr << "Usage: " << argv[0] << " <in.fasta> [<nthreads>]" << std::endl;
std::cerr << "nthreads cannot be less than 1. Given: " << nthreads << std::endl;
return 1;
}
concurrency_level = static_cast<unsigned>(nthreads);
} else {
unsigned ncores = std::thread::hardware_concurrency();
concurrency_level = ncores == 0 ? 1 : std::min(ncores, 4u);
}
std::cout << "Using " << concurrency_level << " threads." << std::endl;
fp = gzopen(argv[1], "r");
seq = kseq_init(fp);
strnum_t read_num = 0;
int maxl = 0;
{
// int is required by library kseq
int l = 0;
// Calculate the length of the longest read
while ((l = kseq_read(seq)) >= 0) {
++read_num;
if (l > maxl) maxl = l;
}
if (maxl > 254) {
std::cerr << "Input reads are too long! Current = "
<< maxl << ", max = 254"
<< std::endl;
kseq_destroy(seq);
gzclose(fp);
return 1;
}
}
const strlen_t read_max_length = static_cast<strlen_t>(maxl);
std::cout << "longest read length = " << static_cast<fuint_t>(read_max_length) << std::endl;
std::cout << "number of reads = " << read_num << std::endl;
const fnames_t S_tpl= template_generator("supportfile-S%.3d.tmp", 0, read_max_length+1);
const fnames_t B_tpl= template_generator("supportfile-B%.3d.tmp", 0, read_max_length+1);
const fname_t IXk_name("supportfile-IXk.tmp");
std::cout << "Preparing arrays S_l..." << std::endl;
occ_countv_t occv(read_max_length+1);
{
multi_enc_fasta_writer_t S(S_tpl);
kseq_rewind(seq);
gzrewind(fp);
int l = 0;
while ((l = kseq_read(seq)) >= 0) {
const strlen_t len = static_cast<strlen_t>(l);
for (strlen_t i = 0; i <= len; ++i) {
const enc_nucl_t c = encode(seq->seq.s[len-i]);
S.write(i, c);
++occv[i][c];
}
for (strlen_t i = len+1; i <= read_max_length; ++i) {
S.write(i, ALPHSIZE-1);
++occv[i][ALPHSIZE-1];
}
}
}
kseq_destroy(seq);
gzclose(fp);
std::cout << "Computing partial BWT..." << std::endl;
compute_partial_BWT(read_max_length, read_num, S_tpl, B_tpl);
remove_files(S_tpl);
std::cout << "Computing interleave and LCP..." << std::endl;
compute_interleave_LCP(read_max_length, read_num, occv, B_tpl, IXk_name);
std::cout << "Computing BWT..." << std::endl;
compute_BWT(read_max_length, read_num, B_tpl, IXk_name);
std::cout << "Done." << std::endl;
return 0;
}