-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathblight.cpp
1384 lines (1216 loc) · 46.7 KB
/
blight.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "blight.h"
#include "include/bbhash.h"
#include "include/common.h"
#include "lz4/lz4_stream.h"
#include "include/robin_hood.h"
#include "utils.h"
#include "include/zstr.hpp"
#include <algorithm>
#include <atomic>
#include <chrono>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <mutex>
#include <omp.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <tmmintrin.h>
#include <unistd.h>
#include <unordered_map>
#include <vector>
using namespace std;
using namespace chrono;
uint64_t kmer_Set_Light::get_kmer_number(){
return number_kmer;
}
__uint128_t kmer_Set_Light::rcb(const __uint128_t& in) {
assume(k <= 64, "k=%u > 64", k);
union kmer_u {
__uint128_t k;
__m128i m128i;
uint64_t u64[2];
uint8_t u8[16];
};
kmer_u res = {.k = in};
static_assert(sizeof(res) == sizeof(__uint128_t), "kmer sizeof mismatch");
// Swap byte order
kmer_u shuffidunrevhash = {.u8 = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}};
res.m128i = _mm_shuffle_epi8(res.m128i, shuffidunrevhash.m128i);
// Swap nuc order in bytes
const uint64_t c1 = 0x0f0f0f0f0f0f0f0f;
const uint64_t c2 = 0x3333333333333333;
for (uint64_t& x : res.u64) {
x = ((x & c1) << 4) | ((x & (c1 << 4)) >> 4); // swap 2-nuc order in bytes
x = ((x & c2) << 2) | ((x & (c2 << 2)) >> 2); // swap nuc order in 2-nuc
x ^= 0xaaaaaaaaaaaaaaaa; // Complement;
}
// Realign to the right
res.m128i = mm_bitshift_right(res.m128i, 128 - 2 * k);
return res.k;
}
uint64_t kmer_Set_Light::rcb(const uint64_t& in) {
assume(k <= 32, "k=%u > 32", k);
// Complement, swap byte order
uint64_t res = __builtin_bswap64(in ^ 0xaaaaaaaaaaaaaaaa);
// Swap nuc order in bytes
const uint64_t c1 = 0x0f0f0f0f0f0f0f0f;
const uint64_t c2 = 0x3333333333333333;
res = ((res & c1) << 4) | ((res & (c1 << 4)) >> 4); // swap 2-nuc order in bytes
res = ((res & c2) << 2) | ((res & (c2 << 2)) >> 2); // swap nuc order in 2-nuc
// Realign to the right
res >>= 64 - 2 * k;
return res;
}
uint64_t kmer_Set_Light::canonize(uint64_t x, uint64_t n) {
return min(x, rcbc(x, n));
}
void kmer_Set_Light::updateK(kmer& min, char nuc) {
min <<= 2;
min += nuc2int(nuc);
min %= offsetUpdateAnchor;
}
void kmer_Set_Light::updateM(kmer& min, char nuc) {
min <<= 2;
min += nuc2int(nuc);
min %= offsetUpdateMinimizer;
}
void kmer_Set_Light::updateRCK(kmer& min, char nuc) {
min >>= 2;
min += (nuc2intrc(nuc) << (2 * k - 2));
}
void kmer_Set_Light::updateRCM(kmer& min, char nuc) {
min >>= 2;
min += (nuc2intrc(nuc) << (2 * minimizer_size_graph - 2));
}
kmer get_int_in_kmer(kmer seq, uint64_t pos, uint64_t number_nuc) {
seq >>= 2 * pos;
return ((seq) % (1 << (2 * number_nuc)));
}
kmer kmer_Set_Light::regular_minimizer(kmer seq) {
kmer mini, mmer;
mmer = seq % minimizer_number_graph;
mini = mmer = canonize(mmer, minimizer_size_graph);
uint64_t hash_mini = (unrevhash(mmer));
for (uint64_t i(1); i <= k - minimizer_size_graph; i++) {
seq >>= 2;
mmer = seq % minimizer_number_graph;
mmer = canonize(mmer, minimizer_size_graph);
uint64_t hash = (unrevhash(mmer));
if (hash_mini > hash) {
mini = mmer;
hash_mini = hash;
}
}
return revhash((uint64_t)mini) % minimizer_number;
}
kmer kmer_Set_Light::regular_minimizer_pos(kmer seq, uint64_t& position) {
kmer mini, mmer;
mmer = seq % minimizer_number_graph;
mini = mmer = canonize(mmer, minimizer_size_graph);
uint64_t hash_mini = (unrevhash(mmer));
position = 0;
for (uint64_t i(1); i <= k - minimizer_size_graph; i++) {
seq >>= 2;
mmer = seq % minimizer_number_graph;
mmer = canonize(mmer, minimizer_size_graph);
uint64_t hash = (unrevhash(mmer));
if (hash_mini > hash) {
position = k - minimizer_size_graph - i;
mini = mmer;
hash_mini = hash;
}
}
return mini;
}
void kmer_Set_Light::construct_index(const string& input_file, const string& tmp_dir) {
if (not tmp_dir.empty()) {
working_dir = tmp_dir + "/";
}
if (m1 < m2) {
cout << "n should be inferior to m" << endl;
exit(0);
}
high_resolution_clock::time_point t1 = high_resolution_clock::now();
nuc_minimizer = new uint32_t[minimizer_number.value()];
current_pos = new uint64_t[minimizer_number.value()];
start_bucket = new uint64_t[minimizer_number.value()];
create_super_buckets(input_file);
high_resolution_clock::time_point t12 = high_resolution_clock::now();
duration<double> time_span12 = duration_cast<duration<double> >(t12 - t1);
cout << "Super bucket created: " << time_span12.count() << " seconds." << endl;
read_super_buckets(working_dir + "_blout");
high_resolution_clock::time_point t13 = high_resolution_clock::now();
duration<double> time_span13 = duration_cast<duration<double> >(t13 - t12);
cout << "Indexes created: " << time_span13.count() << " seconds." << endl;
duration<double> time_spant = duration_cast<duration<double> >(t13 - t1);
cout << "The whole indexing took me " << time_spant.count() << " seconds." << endl;
delete[] nuc_minimizer;
delete[] start_bucket;
delete[] current_pos;
}
void kmer_Set_Light::reset() {
number_kmer = number_super_kmer = largest_MPHF = positions_total_size = positions_int = total_nb_minitigs = largest_bucket_nuc_all = 0;
for (uint64_t i(0); i < mphf_number; ++i) {
all_mphf[i].mphf_size = 0;
all_mphf[i].bit_to_encode = 0;
all_mphf[i].start = 0;
all_mphf[i].empty = true;
}
for (uint64_t i(0); i < minimizer_number.value(); ++i) {
//all_buckets[i].start=0;
nuc_minimizer[i] = 0;
start_bucket[i] = 0;
current_pos[i] = 0;
all_buckets[i].skmer_number = 0;
}
}
string kmer_Set_Light::compaction(const string& seq1, const string& seq2, bool recur = true) {
uint s1(seq1.size()), s2(seq2.size());
if (s1 == 0 or s2 == 0) {
return "";
}
string rc2(revComp(seq2)), end1(seq1.substr(s1 - k + 1, k - 1)), beg2(seq2.substr(0, k - 1));
if (end1 == beg2) {
return seq1 + (seq2.substr(k - 1));
}
string begrc2(rc2.substr(0, k - 1));
if (end1 == begrc2) {
return seq1 + (rc2.substr(k - 1));
}
if (recur) {
return compaction(revComp(seq1), seq2, false);
} else {
}
return "";
}
void kmer_Set_Light::create_super_buckets(const string& input_file) {
struct rlimit rl;
getrlimit(RLIMIT_NOFILE, &rl);
rl.rlim_cur = number_superbuckets.value() + 10;
setrlimit(RLIMIT_NOFILE, &rl);
uint64_t total_nuc_number(0);
istream* inUnitigs;
bool is_gzip_compressed = input_file.substr(input_file.length() - 2) == "gz";
if (is_gzip_compressed)
inUnitigs = new zstr::ifstream(input_file);
else
inUnitigs = new lz4_stream::istream(input_file);
if (not inUnitigs->good()) {
cout << "Problem with files opening" << endl;
exit(1);
}
vector<ostream*> out_files;
for (uint64_t i(0); i < number_superbuckets; ++i) {
auto out = new zstr::ofstream(working_dir + "_blout" + to_string(i) + ".gz");
if (not out->good()) {
cout << "Problem with files opening" << endl;
exit(1);
}
out_files.push_back(out);
}
omp_lock_t lock[number_superbuckets.value()];
for (uint64_t i = 0; i < number_superbuckets; i++) {
omp_init_lock(&(lock[i]));
}
#pragma omp parallel num_threads(coreNumber)
{
string ref, useless;
vector<string> buffer(number_superbuckets.value());
minimizer_type old_minimizer, minimizer;
while (not inUnitigs->eof()) {
ref = useless = "";
#pragma omp critical(dataupdate)
{
getline(*inUnitigs, useless);
getline(*inUnitigs, ref);
if (ref.size() < k) {
ref = "";
} else {
read_kmer += ref.size() - k + 1;
}
}
// FOREACH UNITIG
if (not ref.empty() and not useless.empty()) {
old_minimizer = minimizer = minimizer_number.value();
uint64_t last_position(0);
// FOREACH KMER
kmer seq(str2num(ref.substr(0, k)));
uint64_t position_min;
kmer min_seq = (str2num(ref.substr(k - minimizer_size_graph, minimizer_size_graph))), min_rcseq(rcbc(min_seq, minimizer_size_graph)),
min_canon(min(min_seq, min_rcseq));
minimizer = regular_minimizer_pos(seq, position_min);
old_minimizer = minimizer;
uint64_t hash_min = unrevhash(minimizer);
uint64_t i(0);
for (; i + k < ref.size(); ++i) {
updateK(seq, ref[i + k]);
updateM(min_seq, ref[i + k]);
updateRCM(min_rcseq, ref[i + k]);
min_canon = (min(min_seq, min_rcseq));
uint64_t new_h = unrevhash(min_canon);
// THE NEW mmer is a MINIMIZor
if (new_h < hash_min) {
minimizer = (min_canon);
hash_min = new_h;
position_min = i + k - minimizer_size_graph + 1;
} else {
// the previous minimizer is outdated
if (i >= position_min) {
minimizer = regular_minimizer_pos(seq, position_min);
hash_min = unrevhash(minimizer);
position_min += (i + 1);
} else {
}
}
// COMPUTE KMER MINIMIZER
if (revhash(old_minimizer) % minimizer_number != revhash(minimizer) % minimizer_number) {
old_minimizer = (revhash(old_minimizer) % minimizer_number);
buffer[old_minimizer / bucket_per_superBuckets.value()] += ">" + to_string(old_minimizer) + "\n" + ref.substr(last_position, i - last_position + k) + "\n";
// *(out_files[((old_minimizer))/bucket_per_superBuckets])<<">"+to_string(old_minimizer)+"\n"<<ref.substr(last_position,i-last_position+k)<<"\n";
if (buffer[old_minimizer / bucket_per_superBuckets.value()].size() > 80000) {
omp_set_lock(&(lock[((old_minimizer)) / bucket_per_superBuckets.value()]));
*(out_files[((old_minimizer)) / bucket_per_superBuckets.value()]) << buffer[old_minimizer / bucket_per_superBuckets.value()];
omp_unset_lock(&(lock[((old_minimizer)) / bucket_per_superBuckets.value()]));
buffer[old_minimizer / bucket_per_superBuckets.value()].clear();
}
#pragma omp atomic
nuc_minimizer[old_minimizer] += (i - last_position + k);
#pragma omp atomic
all_buckets[old_minimizer].skmer_number++;
#pragma omp atomic
all_mphf[old_minimizer / number_bucket_per_mphf].mphf_size += (i - last_position + k) - k + 1;
all_mphf[old_minimizer / number_bucket_per_mphf].empty = false;
#pragma omp atomic
total_nuc_number += (i - last_position + k);
last_position = i + 1;
old_minimizer = minimizer;
}
}
if (ref.size() - last_position > k - 1) {
old_minimizer = (revhash(old_minimizer) % minimizer_number);
buffer[old_minimizer / bucket_per_superBuckets.value()] += ">" + to_string(old_minimizer) + "\n" + ref.substr(last_position) + "\n";
// *(out_files[((old_minimizer))/bucket_per_superBuckets])<<">"+to_string(old_minimizer)+"\n"<<ref.substr(last_position)<<"\n";
if (buffer[old_minimizer / bucket_per_superBuckets.value()].size() > 80000) {
omp_set_lock(&(lock[((old_minimizer)) / bucket_per_superBuckets.value()]));
*(out_files[((old_minimizer)) / bucket_per_superBuckets.value()]) << buffer[old_minimizer / bucket_per_superBuckets.value()];
omp_unset_lock(&(lock[((old_minimizer)) / bucket_per_superBuckets.value()]));
buffer[old_minimizer / bucket_per_superBuckets.value()].clear();
}
#pragma omp atomic
nuc_minimizer[old_minimizer] += (ref.substr(last_position)).size();
#pragma omp atomic
all_buckets[old_minimizer].skmer_number++;
#pragma omp atomic
total_nuc_number += (ref.substr(last_position)).size();
#pragma omp atomic
all_mphf[old_minimizer / number_bucket_per_mphf].mphf_size += (ref.substr(last_position)).size() - k + 1;
all_mphf[old_minimizer / number_bucket_per_mphf].empty = false;
}
}
}
for (uint64_t i(0); i < number_superbuckets.value(); ++i) {
if (not buffer[i].empty()) {
omp_set_lock(&(lock[i]));
*(out_files[i]) << buffer[i];
omp_unset_lock(&(lock[i]));
}
}
}
delete inUnitigs;
for (uint64_t i(0); i < number_superbuckets; ++i) {
*out_files[i] << flush;
delete (out_files[i]);
}
bucketSeq.resize(total_nuc_number * 2);
bucketSeq.shrink_to_fit();
uint64_t i(0), total_pos_size(0);
uint32_t max_bucket_mphf(0);
uint64_t hash_base(0), old_hash_base(0), nb_skmer_before(0), last_skmer_number(0);
for (uint64_t BC(0); BC < minimizer_number.value(); ++BC) {
start_bucket[BC] = i;
current_pos[BC] = i;
i += nuc_minimizer[BC];
max_bucket_mphf = max(all_buckets[BC].skmer_number, max_bucket_mphf);
if (BC == 0) {
nb_skmer_before = 0; // I replace skmer_number by the total number of minitigs before this bucket
} else {
nb_skmer_before = all_buckets[BC - 1].skmer_number;
}
uint64_t local_skmercount(all_buckets[BC].skmer_number);
all_buckets[BC].skmer_number = total_nb_minitigs;
total_nb_minitigs += local_skmercount;
if ((BC + 1) % number_bucket_per_mphf == 0) {
int n_bits_to_encode((ceil(log2(max_bucket_mphf + 1))) - bit_saved_sub);
if (n_bits_to_encode < 1) {
n_bits_to_encode = 1;
}
all_mphf[BC / number_bucket_per_mphf].bit_to_encode = n_bits_to_encode;
all_mphf[BC / number_bucket_per_mphf].start = total_pos_size;
total_pos_size += (n_bits_to_encode * all_mphf[BC / number_bucket_per_mphf].mphf_size);
hash_base += all_mphf[(BC / number_bucket_per_mphf)].mphf_size;
all_mphf[BC / number_bucket_per_mphf].mphf_size = old_hash_base;
old_hash_base = hash_base;
max_bucket_mphf = 0;
}
}
total_nb_minitigs = all_buckets[(uint)minimizer_number - 1].skmer_number + last_skmer_number; // total number of minitigs
positions.resize(total_pos_size);
positions_int = positions.size() / 64 + (positions.size() % 64 == 0 ? 0 : 1);
positions.shrink_to_fit();
// initialize_buckets();
}
void kmer_Set_Light::initialize_buckets() {
bucketSeq.resize(total_nuc_number * 2);
bucketSeq.shrink_to_fit();
uint64_t i(0), total_pos_size(0);
uint32_t max_bucket_mphf(0);
uint64_t hash_base(0), old_hash_base(0), nb_skmer_before(0), last_skmer_number(0);
for (uint64_t BC(0); BC < minimizer_number.value(); ++BC) {
start_bucket[BC] = i;
current_pos[BC] = i;
i += nuc_minimizer[BC];
max_bucket_mphf = max(all_buckets[BC].skmer_number, max_bucket_mphf);
if (BC == 0) {
nb_skmer_before = 0; // I replace skmer_number by the total number of minitigs before this bucket
} else {
nb_skmer_before = all_buckets[BC - 1].skmer_number;
}
uint64_t local_skmercount(all_buckets[BC].skmer_number);
all_buckets[BC].skmer_number = total_nb_minitigs;
total_nb_minitigs += local_skmercount;
if ((BC + 1) % number_bucket_per_mphf == 0) {
int n_bits_to_encode((ceil(log2(max_bucket_mphf + 1))) - bit_saved_sub);
if (n_bits_to_encode < 1) {
n_bits_to_encode = 1;
}
all_mphf[BC / number_bucket_per_mphf].bit_to_encode = n_bits_to_encode;
all_mphf[BC / number_bucket_per_mphf].start = total_pos_size;
total_pos_size += (n_bits_to_encode * all_mphf[BC / number_bucket_per_mphf].mphf_size);
hash_base += all_mphf[(BC / number_bucket_per_mphf)].mphf_size;
all_mphf[BC / number_bucket_per_mphf].mphf_size = old_hash_base;
old_hash_base = hash_base;
max_bucket_mphf = 0;
}
}
total_nb_minitigs = all_buckets[(uint)minimizer_number - 1].skmer_number + last_skmer_number; // total number of minitigs
positions.resize(total_pos_size);
positions_int = positions.size() / 64 + (positions.size() % 64 == 0 ? 0 : 1);
positions.shrink_to_fit();
}
void kmer_Set_Light::str2bool(const string& str, uint64_t mini) {
for (uint64_t i(0); i < str.size(); ++i) {
switch (str[i]) {
case 'A':
bucketSeq[(current_pos[mini] + i) * 2] = (false);
bucketSeq[(current_pos[mini] + i) * 2 + 1] = (false);
break;
case 'C':
bucketSeq[(current_pos[mini] + i) * 2] = (false);
bucketSeq[(current_pos[mini] + i) * 2 + 1] = (true);
break;
case 'G':
bucketSeq[(current_pos[mini] + i) * 2] = (true);
bucketSeq[(current_pos[mini] + i) * 2 + 1] = (true);
break;
default:
bucketSeq[(current_pos[mini] + i) * 2] = (true);
bucketSeq[(current_pos[mini] + i) * 2 + 1] = (false);
break;
}
}
current_pos[mini] += (str.size());
}
uint64_t kmer_Set_Light::get_minimizer_from_header(zstr::ifstream& in) {
unsigned compressed_header_size;
char c = in.get(); //read ">"
int32_t minimizer;
in.read(reinterpret_cast<char*>(&minimizer), sizeof(int32_t)); // minimizer_size
in.read(reinterpret_cast<char*>(&compressed_header_size), sizeof(unsigned)); // size of colors/counts with rle
unsigned char* comp; //todo optim
comp = new unsigned char[compressed_header_size + 1];
//~ in.seekg(compressed_header_size + 1, in.cur); // rle + \n
in.read((char*) comp, compressed_header_size + 1);
delete [] comp ;
return minimizer;
}
void kmer_Set_Light::read_super_buckets_reindeer(const string& input_file) {
#pragma omp parallel num_threads(coreNumber)
{
string header, line, mini;
bm::bvector<> position_super_kmers_local;
#pragma omp for
for (uint64_t SBC = 0; SBC < number_superbuckets.value(); ++SBC) {
position_super_kmers_local.init();
vector<uint64_t> number_kmer_accu(bucket_per_superBuckets.value(), 0);
uint64_t BC(SBC * bucket_per_superBuckets);
zstr::ifstream in((input_file + to_string(SBC) + ".gz"));
int32_t minimizer;
in.peek();
while (not in.eof() and in.good()) {
header = line = "";
minimizer = get_minimizer_from_header(in);
getline(in, line); //sequence
if (not line.empty()) {
str2bool(line, minimizer);
position_super_kmers_local[number_kmer_accu[minimizer % bucket_per_superBuckets] + all_mphf[minimizer].mphf_size] = true;
#pragma omp atomic
number_kmer += line.size() - k + 1;
number_kmer_accu[minimizer % bucket_per_superBuckets] += line.size() - k + 1;
#pragma omp atomic
++number_super_kmer;
line.clear();
}
in.peek();
}
create_mphf_disk(BC, BC + bucket_per_superBuckets, position_super_kmers_local);
fill_positions(BC, BC + bucket_per_superBuckets, position_super_kmers_local);
BC += bucket_per_superBuckets.value();
cout << "-" << flush;
}
#pragma omp critical(PSK)
{ position_super_kmers.merge(position_super_kmers_local); }
}
position_super_kmers[number_kmer] = true;
position_super_kmers.optimize();
position_super_kmers.optimize_gap_size();
position_super_kmers_RS = new bm::bvector<>::rs_index_type();
position_super_kmers.build_rs_index(position_super_kmers_RS);
cout << endl;
cout << "----------------------INDEX RECAP----------------------------" << endl;
cout << "Kmer in graph: " << intToString(number_kmer) << endl;
cout << "Super Kmer in graph: " << intToString(number_super_kmer) << endl;
cout << "Average size of Super Kmer: " << intToString(number_kmer / (number_super_kmer)) << endl;
cout << "Total size of the partitionned graph: " << intToString(bucketSeq.capacity() / 2) << endl;
cout << "Largest MPHF: " << intToString(largest_MPHF) << endl;
cout << "Largest Bucket: " << intToString(largest_bucket_nuc_all) << endl;
cout << "Size of the partitionned graph (MBytes): " << intToString(bucketSeq.size() / (8 * 1024 * 1024)) << endl;
cout << "Total Positions size (MBytes): " << intToString(positions.size() / (8 * 1024 * 1024)) << endl;
cout << "Size of the partitionned graph (bit per kmer): " << ((double)(bucketSeq.size()) / (number_kmer)) << endl;
bit_per_kmer += ((double)(bucketSeq.size()) / (number_kmer));
cout << "Total Positions size (bit per kmer): " << ((double)positions.size() / number_kmer) << endl;
bit_per_kmer += ((double)positions.size() / number_kmer);
cout << "TOTAL Bits per kmer (without bbhash): " << bit_per_kmer << endl;
cout << "TOTAL Bits per kmer (with bbhash): " << bit_per_kmer + 4 << endl;
cout << "TOTAL Size estimated (MBytes): " << (bit_per_kmer + 4) * number_kmer / (8 * 1024 * 1024) << endl;
}
void kmer_Set_Light::read_super_buckets(const string& input_file) {
#pragma omp parallel num_threads(coreNumber)
{
string useless, line;
bm::bvector<> position_super_kmers_local;
#pragma omp for
for (uint64_t SBC = 0; SBC < number_superbuckets.value(); ++SBC) {
position_super_kmers_local.init();
vector<uint64_t> number_kmer_accu(bucket_per_superBuckets.value(), 0);
uint64_t BC(SBC * bucket_per_superBuckets);
zstr::ifstream in((input_file + to_string(SBC) + ".gz"));
while (not in.eof() and in.good()) {
useless = line = "";
getline(in, useless);
getline(in, line);
if (not line.empty()) {
useless = useless.substr(1);
uint64_t minimizer(stoi(useless));
str2bool(line, minimizer);
position_super_kmers_local[number_kmer_accu[minimizer % bucket_per_superBuckets] + all_mphf[minimizer].mphf_size] = true;
#pragma omp atomic
number_kmer += line.size() - k + 1;
number_kmer_accu[minimizer % bucket_per_superBuckets] += line.size() - k + 1;
#pragma omp atomic
++number_super_kmer;
line.clear();
}
}
remove((input_file + to_string(SBC) + ".gz").c_str());
create_mphf_disk(BC, BC + bucket_per_superBuckets, position_super_kmers_local);
fill_positions(BC, BC + bucket_per_superBuckets, position_super_kmers_local);
BC += bucket_per_superBuckets.value();
cout << "-" << flush;
}
#pragma omp critical(PSK)
{ position_super_kmers.merge(position_super_kmers_local); }
}
position_super_kmers[number_kmer] = true;
position_super_kmers.optimize();
position_super_kmers.optimize_gap_size();
position_super_kmers_RS = new bm::bvector<>::rs_index_type();
position_super_kmers.build_rs_index(position_super_kmers_RS);
cout << endl;
cout << "----------------------INDEX RECAP----------------------------" << endl;
cout << "Kmer in graph: " << intToString(number_kmer) << endl;
cout << "Super Kmer in graph: " << intToString(number_super_kmer) << endl;
cout << "Average size of Super Kmer: " << intToString(number_kmer / (number_super_kmer)) << endl;
cout << "Total size of the partitionned graph: " << intToString(bucketSeq.capacity() / 2) << endl;
cout << "Largest MPHF: " << intToString(largest_MPHF) << endl;
cout << "Largest Bucket: " << intToString(largest_bucket_nuc_all) << endl;
cout << "Size of the partitionned graph (MBytes): " << intToString(bucketSeq.size() / (8 * 1024 * 1024)) << endl;
cout << "Total Positions size (MBytes): " << intToString(positions.size() / (8 * 1024 * 1024)) << endl;
cout << "Size of the partitionned graph (bit per kmer): " << ((double)(bucketSeq.size()) / (number_kmer)) << endl;
bit_per_kmer += ((double)(bucketSeq.size()) / (number_kmer));
cout << "Total Positions size (bit per kmer): " << ((double)positions.size() / number_kmer) << endl;
bit_per_kmer += ((double)positions.size() / number_kmer);
cout << "TOTAL Bits per kmer (without bbhash): " << bit_per_kmer << endl;
cout << "TOTAL Bits per kmer (with bbhash): " << bit_per_kmer + 4 << endl;
cout << "TOTAL Size estimated (MBytes): " << (bit_per_kmer + 4) * number_kmer / (8 * 1024 * 1024) << endl;
}
inline kmer kmer_Set_Light::get_kmer(uint64_t mini, uint64_t pos) {
kmer res(0);
uint64_t bit = (start_bucket[mini] + pos) * 2;
const uint64_t bitlast = bit + 2 * k;
for (; bit < bitlast; bit += 2) {
res <<= 2;
res |= bucketSeq[bit] * 2 | bucketSeq[bit + 1];
}
return res;
}
inline kmer kmer_Set_Light::get_kmer(uint64_t pos) {
kmer res(0);
uint64_t bit = (pos)*2;
const uint64_t bitlast = bit + 2 * k;
for (; bit < bitlast; bit += 2) {
res <<= 2;
res |= bucketSeq[bit] * 2 | bucketSeq[bit + 1];
}
return res;
}
inline kmer kmer_Set_Light::update_kmer(uint64_t pos, kmer mini, kmer input) {
return update_kmer_local(start_bucket[mini] + pos, bucketSeq, input);
}
inline kmer kmer_Set_Light::update_kmer_local(uint64_t pos, const vector<bool>& V, kmer input) {
input <<= 2;
uint64_t bit0 = pos * 2;
input |= V[bit0] * 2 | V[bit0 + 1];
return input % offsetUpdateAnchor;
}
void kmer_Set_Light::print_kmer(kmer num, uint64_t n) {
Pow2<kmer> anc(2 * (k - 1));
for (uint64_t i(0); i < k and i < n; ++i) {
uint64_t nuc = num / anc;
num = num % anc;
if (nuc == 2) {
cout << "T";
}
if (nuc == 3) {
cout << "G";
}
if (nuc == 1) {
cout << "C";
}
if (nuc == 0) {
cout << "A";
}
if (nuc >= 4) {
cout << nuc << endl;
cout << "WTF" << endl;
}
anc >>= 2;
}
cout << endl;
}
string kmer_Set_Light::kmer2str(kmer num) {
string res(k, '\0');
Pow2<kmer> anc(2 * (k - 1));
for (uint64_t i(0); i < k; ++i) {
uint64_t nuc = num / anc;
num = num % anc;
assert(nuc < 4);
res[i] = "ACTG"[nuc];
anc >>= 2;
}
return res;
}
void kmer_Set_Light::create_mphf_mem(uint64_t begin_BC, uint64_t end_BC) {
#pragma omp parallel num_threads(coreNumber)
{
vector<kmer> anchors;
uint32_t largest_bucket_anchor(0);
uint32_t largest_bucket_nuc(0);
#pragma omp for schedule(dynamic, number_bucket_per_mphf.value())
for (uint64_t BC = (begin_BC); BC < end_BC; ++BC) {
if (nuc_minimizer[BC] != 0) {
largest_bucket_nuc = max(largest_bucket_nuc, nuc_minimizer[BC]);
largest_bucket_nuc_all = max(largest_bucket_nuc_all, nuc_minimizer[BC]);
uint32_t bucketSize(1);
kmer seq(get_kmer(BC, 0)), rcSeq(rcb(seq)), canon(min_k(seq, rcSeq));
anchors.push_back(canon);
for (uint64_t j(0); (j + k) < nuc_minimizer[BC]; j++) {
if (position_super_kmers[all_mphf[BC].mphf_size + bucketSize]) {
j += k - 1;
if ((j + k) < nuc_minimizer[BC]) {
seq = (get_kmer(BC, j + 1)), rcSeq = (rcb(seq)), canon = (min_k(seq, rcSeq));
anchors.push_back(canon);
bucketSize++;
}
} else {
seq = update_kmer(j + k, BC, seq);
rcSeq = (rcb(seq));
canon = (min_k(seq, rcSeq));
anchors.push_back(canon);
bucketSize++;
}
}
largest_bucket_anchor = max(largest_bucket_anchor, bucketSize);
}
if ((BC + 1) % number_bucket_per_mphf == 0 and not anchors.empty()) {
largest_MPHF = max(largest_MPHF, anchors.size());
all_mphf[BC / number_bucket_per_mphf].kmer_MPHF = new boomphf::mphf<kmer, hasher_t>(anchors.size(), anchors, gammaFactor);
anchors.clear();
largest_bucket_anchor = 0;
largest_bucket_nuc = (0);
}
}
}
}
void kmer_Set_Light::create_mphf_disk(uint64_t begin_BC, uint64_t end_BC, bm::bvector<>& position_super_kmers_local) {
#pragma omp parallel num_threads(coreNumber)
{
uint32_t largest_bucket_anchor(0);
uint32_t largest_bucket_nuc(0);
#pragma omp for schedule(dynamic, number_bucket_per_mphf.value())
for (uint64_t BC = (begin_BC); BC < end_BC; ++BC) {
uint64_t mphfSize(0);
string name(working_dir + "_blkmers" + to_string(BC));
if (nuc_minimizer[BC] != 0) {
ofstream out(name, ofstream::binary | ofstream::trunc);
largest_bucket_nuc = max(largest_bucket_nuc, nuc_minimizer[BC]);
largest_bucket_nuc_all = max(largest_bucket_nuc_all, nuc_minimizer[BC]);
uint32_t bucketSize(1);
kmer seq(get_kmer(BC, 0)), rcSeq(rcb(seq)), canon(min_k(seq, rcSeq));
out.write(reinterpret_cast<char*>(&canon), sizeof(canon));
mphfSize++;
for (uint64_t j(0); (j + k) < nuc_minimizer[BC]; j++) {
if (position_super_kmers_local[all_mphf[BC].mphf_size + bucketSize]) {
j += k - 1;
if ((j + k) < nuc_minimizer[BC]) {
seq = (get_kmer(BC, j + 1)), rcSeq = (rcb(seq)), canon = (min_k(seq, rcSeq));
out.write(reinterpret_cast<char*>(&canon), sizeof(canon));
bucketSize++;
mphfSize++;
}
} else {
seq = update_kmer(j + k, BC, seq);
rcSeq = (rcb(seq));
canon = (min_k(seq, rcSeq));
out.write(reinterpret_cast<char*>(&canon), sizeof(canon));
bucketSize++;
mphfSize++;
}
}
largest_bucket_anchor = max(largest_bucket_anchor, bucketSize);
}
if ((BC + 1) % number_bucket_per_mphf == 0 and mphfSize != 0) {
largest_MPHF = max(largest_MPHF, mphfSize);
auto data_iterator = file_binary(name.c_str());
all_mphf[BC / number_bucket_per_mphf].kmer_MPHF = new boomphf::mphf<kmer, hasher_t>(mphfSize, data_iterator, gammaFactor);
remove(name.c_str());
largest_bucket_anchor = 0;
largest_bucket_nuc = (0);
mphfSize = 0;
}
}
}
}
void kmer_Set_Light::int_to_bool(uint64_t n_bits_to_encode, uint64_t X, uint64_t pos, uint64_t start) {
uint64_t i_mutex(((pos * n_bits_to_encode + start) / 1024) * 4096 / (positions_int));
positions_mutex[i_mutex].lock();
for (uint64_t i(0); i < n_bits_to_encode; ++i) {
uint64_t pos_check(i + pos * n_bits_to_encode + start);
if ((pos_check / 1024) * 4096 / (positions_int) != i_mutex) {
positions_mutex[i_mutex].unlock();
i_mutex++;
positions_mutex[i_mutex].lock();
}
positions[pos_check] = X % 2;
X >>= 1;
}
positions_mutex[i_mutex].unlock();
}
uint64_t kmer_Set_Light::bool_to_int(uint64_t n_bits_to_encode, uint64_t pos, uint64_t start) {
uint64_t res(0);
uint64_t acc(1);
for (uint64_t i(0); i < n_bits_to_encode; ++i, acc <<= 1) {
if (positions[i + pos * n_bits_to_encode + start]) {
res |= acc;
}
}
return res;
}
void kmer_Set_Light::fill_positions(uint64_t begin_BC, uint64_t end_BC, bm::bvector<>& position_super_kmers_local) {
#pragma omp parallel for num_threads(coreNumber)
for (uint64_t BC = (begin_BC); BC < end_BC; ++BC) {
uint64_t super_kmer_id(0);
if (nuc_minimizer[BC] > 0) {
uint64_t kmer_id(1);
int n_bits_to_encode(all_mphf[BC / number_bucket_per_mphf].bit_to_encode);
kmer seq(get_kmer(BC, 0)), rcSeq(rcb(seq)), canon(min_k(seq, rcSeq));
int_to_bool(n_bits_to_encode,super_kmer_id / positions_to_check.value(),all_mphf[BC / number_bucket_per_mphf].kmer_MPHF->lookup(canon),all_mphf[BC / number_bucket_per_mphf].start);
for (uint64_t j(0); (j + k) < nuc_minimizer[BC]; j++) {
if (position_super_kmers_local[all_mphf[BC].mphf_size + kmer_id]) {
j += k - 1;
super_kmer_id++;
kmer_id++;
if ((j + k) < nuc_minimizer[BC]) {
seq = (get_kmer(BC, j + 1)), rcSeq = (rcb(seq)), canon = (min_k(seq, rcSeq));
int_to_bool(n_bits_to_encode,super_kmer_id / positions_to_check.value(),all_mphf[BC / number_bucket_per_mphf].kmer_MPHF->lookup(canon), all_mphf[BC / number_bucket_per_mphf].start);
}
} else {
seq = update_kmer(j + k, BC, seq);
rcSeq = (rcb(seq));
canon = (min_k(seq, rcSeq));
kmer_id++;
int_to_bool(n_bits_to_encode,super_kmer_id / positions_to_check.value(),all_mphf[BC / number_bucket_per_mphf].kmer_MPHF->lookup(canon), all_mphf[BC / number_bucket_per_mphf].start);
}
}
}
}
}
int64_t kmer_Set_Light::kmer_to_hash(const kmer canon, kmer minimizer) {
if (unlikely(all_mphf[minimizer / number_bucket_per_mphf].empty)) {
return -1;
}
uint64_t hash = (all_mphf[minimizer / number_bucket_per_mphf].kmer_MPHF->lookup(canon));
uint64_t pos;
if (unlikely(hash == ULLONG_MAX)) {
return -1;
} else {
return hash;
}
}
int64_t kmer_Set_Light::hash_to_rank(const int64_t hash, kmer minimizer) {
int n_bits_to_encode(all_mphf[minimizer / number_bucket_per_mphf].bit_to_encode);
int64_t rank(all_buckets[minimizer].skmer_number + bool_to_int(n_bits_to_encode, hash, all_mphf[minimizer / number_bucket_per_mphf].start) * positions_to_check.value());
return rank;
}
vector<kmer> kmer_Set_Light::kmer_to_superkmer(const kmer canon, kmer minimizer, int64_t& rank, int64_t& hash) {
hash = kmer_to_hash(canon, minimizer);
if (hash < 0) {
return {};
}
rank = (hash_to_rank(hash, minimizer));
if (rank < 0) {
return {};
}
vector<kmer> result;
bool found(false);
bm::id64_t pos, next_position, stop_position;
position_super_kmers.select(rank + 1, pos, *(position_super_kmers_RS));
for (uint64_t check_super_kmer(0); check_super_kmer < positions_to_check.value() and not found; ++check_super_kmer) {
next_position = (position_super_kmers.get_next(pos));
if (next_position == 0) {
stop_position = bucketSeq.size() - k;
} else {
stop_position = next_position + (rank + check_super_kmer) * (k - 1);
}
pos += (rank + check_super_kmer) * (k - 1);
if (likely(((uint64_t)pos + k - 1) < bucketSeq.size())) {
kmer seqR = get_kmer(pos);
kmer rcSeqR, canonR;
for (uint64_t j = (pos); j < stop_position; ++j) {
rcSeqR = (rcb(seqR));
canonR = (min_k(seqR, rcSeqR));
result.push_back(canonR);
if (canon == canonR) {
found = true;
}
if (likely(((j + k) * 2 < bucketSeq.size()))) {
seqR = update_kmer_local(j + k, bucketSeq, seqR);
}
}
}
pos = next_position;
}
if (found) {
return result;
}
return {};
}
vector<bool> kmer_Set_Light::get_presence_query(const string& query) {
if (query.size() < k) {
return {};
}
vector<bool> result;
kmer seq(str2num(query.substr(0, k))), rcSeq(rcb(seq)), canon(min_k(seq, rcSeq));
int64_t i(0), rank, hash;
vector<kmer> superKmers;
kmer minimizer(regular_minimizer(canon));
for (; i + k <= query.size(); ++i) {
if (superKmers.empty()) {
superKmers = kmer_to_superkmer(canon, minimizer, rank, hash);
result.push_back(not superKmers.empty());
} else {
if (kmer_in_superkmer(canon, superKmers)) {
result.push_back(true);
} else {
superKmers = kmer_to_superkmer(canon, minimizer, rank, hash);
result.push_back(not superKmers.empty());
}
}
if (i + k < query.size()) {
updateK(seq, query[i + k]);
updateRCK(rcSeq, query[i + k]);
canon = (min_k(seq, rcSeq));
minimizer = (regular_minimizer(canon));
}
}
return result;
}
vector<int64_t> kmer_Set_Light::get_rank_query(const string& query) {
if (query.size() < k) {
return {};
}
vector<int64_t> result;
kmer seq(str2num(query.substr(0, k))), rcSeq(rcb(seq)), canon(min_k(seq, rcSeq));
int64_t i(0), rank, hash;
vector<kmer> superKmers;
kmer minimizer(regular_minimizer(canon));
for (; i + k <= query.size(); ++i) {
if (superKmers.empty()) {
superKmers = kmer_to_superkmer(canon, minimizer, rank, hash);
result.push_back(superKmers.empty() ? -1 : rank);
} else {