forked from DaehwanKimLab/hisat2
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gfm.h
6492 lines (6151 loc) · 230 KB
/
gfm.h
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
/*
* Copyright 2015, Daehwan Kim <[email protected]>
*
* This file is part of HISAT 2.
*
* HISAT 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HISAT 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HISAT 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GFM_H_
#define GFM_H_
#include <stdint.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
#include <fcntl.h>
#include <math.h>
#include <errno.h>
#include <set>
#include <stdexcept>
#include <sys/stat.h>
#ifdef BOWTIE_MM
#include <sys/mman.h>
#include <sys/shm.h>
#endif
#include "shmem.h"
#include "alphabet.h"
#include "assert_helpers.h"
#include "bitpack.h"
#include "blockwise_sa.h"
#include "endian_swap.h"
#include "word_io.h"
#include "random_source.h"
#include "ref_read.h"
#include "threading.h"
#include "str_util.h"
#include "mm.h"
#include "timer.h"
#include "reference.h"
#include "search_globals.h"
#include "ds.h"
#include "random_source.h"
#include "mem_ids.h"
#include "btypes.h"
#include "tokenize.h"
#ifdef POPCNT_CAPABILITY
#include "processor_support.h"
#endif
#include "gbwt_graph.h"
using namespace std;
// From ccnt_lut.cpp, automatically generated by gen_lookup_tables.pl
extern uint8_t cCntLUT_4[4][4][256];
extern uint8_t cCntLUT_4_rev[4][4][256];
extern uint8_t cCntBIT[8][256];
static const uint64_t c_table[4] = {
0xffffffffffffffff,
0xaaaaaaaaaaaaaaaa,
0x5555555555555555,
0x0000000000000000
};
#ifndef VMSG_NL
#define VMSG_NL(...) \
if(this->verbose()) { \
stringstream tmp; \
tmp << __VA_ARGS__ << endl; \
this->verbose(tmp.str()); \
}
#endif
#ifndef VMSG
#define VMSG(...) \
if(this->verbose()) { \
stringstream tmp; \
tmp << __VA_ARGS__; \
this->verbose(tmp.str()); \
}
#endif
/**
* Flags describing type of Ebwt.
*/
enum GFM_FLAGS {
GFM_ENTIRE_REV = 4 // true -> reverse Ebwt is the whole
// concatenated string reversed, rather than
// each stretch reversed
};
/**
* Extended Burrows-Wheeler transform header. This together with the
* actual data arrays and other text-specific parameters defined in
* class Ebwt constitute the entire Ebwt.
*/
template <typename index_t = uint32_t>
class GFMParams {
public:
GFMParams() { }
GFMParams(
index_t len,
index_t gbwtLen,
index_t numNodes,
int32_t lineRate,
int32_t offRate,
int32_t ftabChars,
index_t eftabLen,
bool entireReverse)
{
init(len, gbwtLen, numNodes, lineRate, offRate, ftabChars, eftabLen, entireReverse);
}
GFMParams(const GFMParams& gh) {
init(gh._len, gh._gbwtLen, gh._numNodes, gh._lineRate, gh._offRate,
gh._ftabChars, gh._eftabLen, gh._entireReverse);
}
void init(
index_t len,
index_t gbwtLen,
index_t numNodes,
int32_t lineRate,
int32_t offRate,
int32_t ftabChars,
index_t eftabLen,
bool entireReverse)
{
_entireReverse = entireReverse;
_linearFM = (len + 1 == gbwtLen || gbwtLen == 0);
_len = len;
_gbwtLen = (gbwtLen == 0 ? len + 1 : gbwtLen);
_numNodes = (numNodes == 0 ? len + 1 : numNodes);
if(_linearFM) {
_sz = (len+3)/4;
_gbwtSz = _gbwtLen/4 + 1;
} else {
_sz = (len+1)/2;
_gbwtSz = _gbwtLen/2 + 1;
}
_lineRate = lineRate;
_origOffRate = offRate;
_offRate = offRate;
_offMask = std::numeric_limits<index_t>::max() << _offRate;
_ftabChars = ftabChars;
_eftabLen = eftabLen;
_eftabSz = _eftabLen*sizeof(index_t);
_ftabLen = (1 << (_ftabChars*2))+1;
_ftabSz = _ftabLen*sizeof(index_t);
_offsLen = (_numNodes + (1 << _offRate) - 1) >> _offRate;
_offsSz = _offsLen*sizeof(index_t);
_lineSz = 1 << _lineRate;
_sideSz = _lineSz * 1 /* lines per side */;
if(_linearFM) {
_sideGbwtSz = _sideSz - (sizeof(index_t) * 4);
_sideGbwtLen = _sideGbwtSz << 2;
} else {
_sideGbwtSz = _sideSz - (sizeof(index_t) * 6);
_sideGbwtLen = _sideGbwtSz << 1;
}
_numSides = (_gbwtSz+(_sideGbwtSz)-1)/(_sideGbwtSz);
_numLines = _numSides * 1 /* lines per side */;
_gbwtTotLen = _numSides * _sideSz;
_gbwtTotSz = _gbwtTotLen;
assert(repOk());
}
index_t len() const { return _len; }
index_t lenNucs() const { return _len; }
index_t gbwtLen() const { return _gbwtLen; }
index_t sz() const { return _sz; }
index_t gbwtSz() const { return _gbwtSz; }
int32_t lineRate() const { return _lineRate; }
int32_t origOffRate() const { return _origOffRate; }
int32_t offRate() const { return _offRate; }
index_t offMask() const { return _offMask; }
int32_t ftabChars() const { return _ftabChars; }
index_t eftabLen() const { return _eftabLen; }
index_t eftabSz() const { return _eftabSz; }
index_t ftabLen() const { return _ftabLen; }
index_t ftabSz() const { return _ftabSz; }
index_t offsLen() const { return _offsLen; }
index_t offsSz() const { return _offsSz; }
index_t lineSz() const { return _lineSz; }
index_t sideSz() const { return _sideSz; }
index_t sideGbtSz() const { return _sideGbwtSz; }
index_t sideGbwtLen() const { return _sideGbwtLen; }
index_t numSides() const { return _numSides; }
index_t numLines() const { return _numLines; }
index_t gbwtTotLen() const { return _gbwtTotLen; }
index_t gbwtTotSz() const { return _gbwtTotSz; }
bool entireReverse() const { return _entireReverse; }
bool linearFM() const { return _linearFM; }
index_t numNodes() const { return _numNodes; }
/**
* Set a new suffix-array sampling rate, which involves updating
* rate, mask, sample length, and sample size.
*/
void setOffRate(int __offRate) {
_offRate = __offRate;
_offMask = std::numeric_limits<index_t>::max() << _offRate;
_offsLen = (_gbwtLen + (1 << _offRate) - 1) >> _offRate;
_offsSz = _offsLen * sizeof(index_t);
}
#ifndef NDEBUG
/// Check that this EbwtParams is internally consistent
bool repOk() const {
// assert_gt(_len, 0);
assert_gt(_lineRate, 3);
assert_geq(_offRate, 0);
assert_leq(_ftabChars, 16);
assert_geq(_ftabChars, 1);
assert_lt(_lineRate, 32);
assert_lt(_ftabChars, 32);
assert_eq(0, _gbwtTotSz % _lineSz);
return true;
}
#endif
/**
* Pretty-print the header contents to the given output stream.
*/
void print(ostream& out) const {
out << "Headers:" << endl
<< " len: " << _len << endl
<< " gbwtLen: " << _gbwtLen << endl
<< " nodes: " << _numNodes << endl
<< " sz: " << _sz << endl
<< " gbwtSz: " << _gbwtSz << endl
<< " lineRate: " << _lineRate << endl
<< " offRate: " << _offRate << endl
<< " offMask: 0x" << hex << _offMask << dec << endl
<< " ftabChars: " << _ftabChars << endl
<< " eftabLen: " << _eftabLen << endl
<< " eftabSz: " << _eftabSz << endl
<< " ftabLen: " << _ftabLen << endl
<< " ftabSz: " << _ftabSz << endl
<< " offsLen: " << _offsLen << endl
<< " offsSz: " << _offsSz << endl
<< " lineSz: " << _lineSz << endl
<< " sideSz: " << _sideSz << endl
<< " sideGbwtSz: " << _sideGbwtSz << endl
<< " sideGbwtLen: " << _sideGbwtLen << endl
<< " numSides: " << _numSides << endl
<< " numLines: " << _numLines << endl
<< " gbwtTotLen: " << _gbwtTotLen << endl
<< " gbwtTotSz: " << _gbwtTotSz << endl
<< " reverse: " << _entireReverse << endl
<< " linearFM: " << (_linearFM ? "Yes" : "No") << endl;
}
index_t _len;
index_t _gbwtLen;
index_t _sz;
index_t _gbwtSz;
int32_t _lineRate;
int32_t _origOffRate;
int32_t _offRate;
index_t _offMask;
int32_t _ftabChars;
index_t _eftabLen;
index_t _eftabSz;
index_t _ftabLen;
index_t _ftabSz;
index_t _offsLen;
index_t _offsSz;
index_t _lineSz;
index_t _sideSz;
index_t _sideGbwtSz;
index_t _sideGbwtLen;
index_t _numSides;
index_t _numLines;
index_t _gbwtTotLen;
index_t _gbwtTotSz;
bool _entireReverse;
bool _linearFM;
index_t _numNodes;
};
/**
* Exception to throw when a file-realted error occurs.
*/
class GFMFileOpenException : public std::runtime_error {
public:
GFMFileOpenException(const std::string& msg = "") :
std::runtime_error(msg) { }
};
/**
* Calculate size of file with given name.
*/
static inline int64_t fileSize(const char* name) {
std::ifstream f;
f.open(name, std::ios_base::binary | std::ios_base::in);
if (!f.good() || f.eof() || !f.is_open()) { return 0; }
f.seekg(0, std::ios_base::beg);
std::ifstream::pos_type begin_pos = f.tellg();
f.seekg(0, std::ios_base::end);
return static_cast<int64_t>(f.tellg() - begin_pos);
}
/**
* Encapsulates a location in the gbwt text in terms of the side it
* occurs in and its offset within the side.
*/
template <typename index_t = uint32_t>
struct SideLocus {
SideLocus() :
_sideByteOff(0),
_sideNum(0),
_charOff(0),
_by(-1),
_bp(-1) { }
/**
* Construct from row and other relevant information about the Ebwt.
*/
SideLocus(index_t row, const GFMParams<index_t>& ep, const uint8_t* ebwt) {
initFromRow(row, ep, ebwt);
}
/**
* Init two SideLocus objects from a top/bot pair, using the result
* from one call to initFromRow to possibly avoid a second call.
*/
static void initFromTopBot(
index_t top,
index_t bot,
const GFMParams<index_t>& gp,
const uint8_t* gfm,
SideLocus& ltop,
SideLocus& lbot)
{
const index_t sideGbwtLen = gp._sideGbwtLen;
assert_gt(bot, top);
ltop.initFromRow(top, gp, gfm);
index_t spread = bot - top;
// Many cache misses on the following lines
if(ltop._charOff + spread < sideGbwtLen) {
lbot._charOff = ltop._charOff + spread;
lbot._sideNum = ltop._sideNum;
lbot._sideByteOff = ltop._sideByteOff;
lbot._by = lbot._charOff >> 2;
assert_lt(lbot._by, (int)gp._sideGbwtSz);
lbot._bp = lbot._charOff & 0x3;
} else {
lbot.initFromRow(bot, gp, gfm);
}
}
/**
* Calculate SideLocus based on a row and other relevant
* information about the shape of the Ebwt.
*/
void initFromRow(
index_t row,
const GFMParams<index_t>& gp,
const uint8_t* gfm) {
const index_t sideSz = gp._sideSz;
// Side length is hard-coded for now; this allows the compiler
// to do clever things to accelerate / and %.
_sideNum = row / gp._sideGbwtLen;
assert_lt(_sideNum, gp._numSides);
_charOff = row % gp._sideGbwtLen;
_sideByteOff = _sideNum * sideSz;
assert_leq(row, gp._gbwtLen);
assert_leq(_sideByteOff + sideSz, gp._gbwtTotSz);
// Tons of cache misses on the next line
_by = _charOff >> 2; // byte within side
assert_lt(_by, (int)gp._sideGbwtSz);
_bp = _charOff & 0x3; // bit-pair within byte
}
/**
* Init two SideLocus objects from a top/bot pair, using the result
* from one call to initFromRow to possibly avoid a second call.
*/
static void initFromTopBot_bit(
index_t top,
index_t bot,
const GFMParams<index_t>& gp,
const uint8_t* gfm,
SideLocus& ltop,
SideLocus& lbot)
{
const index_t sideGbwtLen = gp._sideGbwtLen;
// assert_gt(bot, top);
ltop.initFromRow_bit(top, gp, gfm);
index_t spread = bot - top;
// Many cache misses on the following lines
if(ltop._charOff + spread < sideGbwtLen) {
lbot._charOff = ltop._charOff + spread;
lbot._sideNum = ltop._sideNum;
lbot._sideByteOff = ltop._sideByteOff;
lbot._by = lbot._charOff >> 3;
assert_lt(lbot._by, (int)gp._sideGbwtSz);
lbot._bp = lbot._charOff & 0x7;
} else {
lbot.initFromRow_bit(bot, gp, gfm);
}
}
/**
* Calculate SideLocus based on a row and other relevant
* information about the shape of the Ebwt.
*/
void initFromRow_bit(
index_t row,
const GFMParams<index_t>& gp,
const uint8_t* gfm) {
const index_t sideSz = gp._sideSz;
// Side length is hard-coded for now; this allows the compiler
// to do clever things to accelerate / and %.
_sideNum = row / gp._sideGbwtLen;
assert_lt(_sideNum, gp._numSides);
_charOff = row % gp._sideGbwtLen;
_sideByteOff = _sideNum * sideSz;
assert_lt(row, gp._gbwtLen);
assert_leq(_sideByteOff + sideSz, gp._gbwtTotSz);
// Tons of cache misses on the next line
_by = _charOff >> 3; // byte within side
assert_lt(_by, (int)gp._sideGbwtSz);
_bp = _charOff & 0x7; // bit-pair within byte
}
/**
* Transform this SideLocus to refer to the next side (i.e. the one
* corresponding to the next side downstream). Set all cursors to
* point to the beginning of the side.
*/
void nextSide(const GFMParams<index_t>& gp) {
assert(valid());
_sideByteOff += gp.sideSz();
_sideNum++;
_by = _bp = _charOff = 0;
assert(valid());
}
/**
* Return true iff this is an initialized SideLocus
*/
bool valid() const {
if(_bp != -1) {
return true;
}
return false;
}
/**
* Convert locus to BW row it corresponds to.
*/
index_t toBWRow(const GFMParams<index_t>& gp) const;
#ifndef NDEBUG
/**
* Check that SideLocus is internally consistent and consistent
* with the (provided) EbwtParams.
*/
bool repOk(const GFMParams<index_t>& gp) const {
ASSERT_ONLY(index_t row = toBWRow(gp));
assert_leq(row, gp._gbwtLen);
assert_range(-1, 3, _bp);
assert_range(0, (int)gp._sideGbwtSz, _by);
return true;
}
#endif
/// Make this look like an invalid SideLocus
void invalidate() {
_bp = -1;
}
/**
* Return a read-only pointer to the beginning of the top side.
*/
const uint8_t *side(const uint8_t* gbwt) const {
return gbwt + _sideByteOff;
}
/**
* Return a read-only pointer to the beginning of the top side.
*/
const uint8_t *next_side(const GFMParams<index_t>& gp, const uint8_t* gbwt) const {
if(_sideByteOff + gp._sideSz < gp._ebwtTotSz) {
return gbwt + _sideByteOff + gp._sideSz;
} else {
return NULL;
}
}
index_t _sideByteOff; // offset of top side within ebwt[]
index_t _sideNum; // index of side
index_t _charOff; // character offset within side
int32_t _by; // byte within side (not adjusted for bw sides)
int32_t _bp; // bitpair within byte (not adjusted for bw sides)
};
/**
* Convert locus to BW row it corresponds to.
*/
template <typename index_t>
inline index_t SideLocus<index_t>::toBWRow(const GFMParams<index_t>& gp) const {
return _sideNum * (gp._sideGbwtSz << (gp.linearFM() ? 2 : 1)) + _charOff;
}
#ifdef POPCNT_CAPABILITY // wrapping of "struct"
struct USE_POPCNT_GENERIC {
#endif
// Use this standard bit-bashing population count
inline static int pop64(uint64_t x) {
// Lots of cache misses on following lines (>10K)
x = x - ((x >> 1) & 0x5555555555555555llu);
x = (x & 0x3333333333333333llu) + ((x >> 2) & 0x3333333333333333llu);
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0Fllu;
x = x + (x >> 8);
x = x + (x >> 16);
x = x + (x >> 32);
return (int)(x & 0x3Fllu);
}
#ifdef POPCNT_CAPABILITY // wrapping a "struct"
};
#endif
#ifdef POPCNT_CAPABILITY
struct USE_POPCNT_INSTRUCTION {
inline static int pop64(uint64_t x) {
int64_t count;
#ifdef USING_MSC_COMPILER
count = __popcnt64(x);
#else
asm ("popcntq %[x],%[count]\n": [count] "=&r" (count): [x] "r" (x));
#endif
return (int)count;
}
};
#endif
/**
* Tricky-bit-bashing bitpair counting for given two-bit value (0-3)
* within a 64-bit argument.
*/
#ifdef POPCNT_CAPABILITY
template<typename Operation>
#endif
inline static int countInU64(int c, uint64_t dw) {
uint64_t c0 = c_table[c];
uint64_t x0 = dw ^ c0;
uint64_t x1 = (x0 >> 1);
uint64_t x2 = x1 & (0x5555555555555555);
uint64_t x3 = x0 & x2;
#ifdef POPCNT_CAPABILITY
uint64_t tmp = Operation().pop64(x3);
#else
uint64_t tmp = pop64(x3);
#endif
return (int) tmp;
}
#ifdef POPCNT_CAPABILITY // wrapping of "struct"
struct USE_POPCNT_GENERIC_BITS {
// Use this standard bit-bashing population count
inline static uint64_t pop64(uint64_t x) {
#else
// Use this standard bit-bashing population count
inline static uint64_t pop6464(uint64_t x) {
#endif
x -= (x >> 1) & 0x5555555555555555ULL;
x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
return int((x * 0x0101010101010101ULL) >> 56);
}
#ifdef POPCNT_CAPABILITY // wrapping a "struct"
};
#endif
/**
* Tricky-bit-bashing bitpair counting for given two-bit value (0-3)
* within a 64-bit argument.
*/
#ifdef POPCNT_CAPABILITY
template<typename Operation>
#endif
inline static int countInU64_bits(uint64_t dw) {
#ifdef POPCNT_CAPABILITY
uint64_t tmp = Operation().pop64(dw);
#else
uint64_t tmp = pop6464(dw);
#endif
return (int) tmp;
}
// Forward declarations for Ebwt class
class GFMSearchParams;
/**
* Extended Burrows-Wheeler transform data.
*
* An Ebwt may be transferred to and from RAM with calls to
* evictFromMemory() and loadIntoMemory(). By default, a newly-created
* Ebwt is not loaded into memory; if the user would like to use a
* newly-created Ebwt to answer queries, they must first call
* loadIntoMemory().
*/
template <class index_t = uint32_t>
class GFM {
public:
#define GFM_INITS \
_toBigEndian(currentlyBigEndian()), \
_overrideOffRate(overrideOffRate), \
_verbose(verbose), \
_passMemExc(passMemExc), \
_sanity(sanityCheck), \
fw_(fw), \
_in1(NULL), \
_in2(NULL), \
_nPat(0), \
_nFrag(0), \
_plen(EBWT_CAT), \
_rstarts(EBWT_CAT), \
_fchr(EBWT_CAT), \
_ftab(EBWT_CAT), \
_eftab(EBWT_CAT), \
_offs(EBWT_CAT), \
_gfm(EBWT_CAT), \
_useMm(false), \
useShmem_(false), \
_refnames(EBWT_CAT), \
mmFile1_(NULL), \
mmFile2_(NULL), \
_nthreads(1)
/// Construct a GFM from the given input file
GFM(const string& in,
ALTDB<index_t>* altdb,
int needEntireReverse,
bool fw,
int32_t overrideOffRate, // = -1,
int32_t offRatePlus, // = -1,
bool useMm, // = false,
bool useShmem, // = false,
bool mmSweep, // = false,
bool loadNames, // = false,
bool loadSASamp, // = true,
bool loadFtab, // = true,
bool loadRstarts, // = true,
bool loadSpliceSites, // = true,
bool verbose, // = false,
bool startVerbose, // = false,
bool passMemExc, // = false,
bool sanityCheck, // = false)
bool useHaplotype, // = false
bool skipLoading = false) :
GFM_INITS
{
assert(!useMm || !useShmem);
#ifdef POPCNT_CAPABILITY
ProcessorSupport ps;
_usePOPCNTinstruction = ps.POPCNTenabled();
#endif
packed_ = false;
_useMm = useMm;
useShmem_ = useShmem;
_in1Str = in + ".1." + gfm_ext;
_in2Str = in + ".2." + gfm_ext;
if(skipLoading) return;
readIntoMemory(
fw ? -1 : needEntireReverse, // need REF_READ_REVERSE
loadSASamp, // load the SA sample portion?
loadFtab, // load the ftab & eftab?
loadRstarts, // load the rstarts array?
true, // stop after loading the header portion?
&_gh, // params
mmSweep, // mmSweep
loadNames, // loadNames
startVerbose); // startVerbose
// If the offRate has been overridden, reflect that in the
// _eh._offRate field
if(offRatePlus > 0 && _overrideOffRate == -1) {
_overrideOffRate = _gh._offRate + offRatePlus;
}
if(_overrideOffRate > _gh._offRate) {
_gh.setOffRate(_overrideOffRate);
assert_eq(_overrideOffRate, _gh._offRate);
}
// Read ALTs
EList<ALT<index_t> >& alts = altdb->alts();
EList<Haplotype<index_t> >& haplotypes = altdb->haplotypes();
EList<string>& altnames = altdb->altnames();
alts.clear(); altnames.clear();
string in7Str = in + ".7." + gfm_ext;
string in8Str = in + ".8." + gfm_ext;
if(verbose || startVerbose) cerr << "Opening \"" << in7Str.c_str() << "\"" << endl;
ifstream in7(in7Str.c_str(), ios::binary);
if(!in7.good()) {
cerr << "Could not open index file " << in7Str.c_str() << endl;
}
EList<index_t> to_alti;
index_t to_alti_far = 0;
readI32(in7, this->toBe());
index_t numAlts = readIndex<index_t>(in7, this->toBe());
if(numAlts > 0) {
alts.resizeExact(numAlts); alts.clear();
to_alti.resizeExact(numAlts); to_alti.clear();
while(!in7.eof()) {
alts.expand();
alts.back().read(in7, this->toBe());
to_alti.push_back(to_alti_far);
to_alti_far++;
if(!loadSpliceSites) {
if(alts.back().splicesite()) {
alts.pop_back();
assert_gt(numAlts, 0);
numAlts--;
to_alti.back() = std::numeric_limits<index_t>::max();
to_alti_far--;
}
}
if(alts.size() == numAlts) break;
}
}
assert_eq(alts.size(), numAlts);
assert_eq(to_alti_far, numAlts);
if(useHaplotype) {
// Check if it hits the end of file, and this routine is needed for backward compatibility
if(in7.peek() != std::ifstream::traits_type::eof()) {
index_t numHaplotypes = readIndex<index_t>(in7, this->toBe());
if(numHaplotypes > 0) {
haplotypes.resizeExact(numHaplotypes);
haplotypes.clear();
while(!in7.eof()) {
haplotypes.expand();
haplotypes.back().read(in7, this->toBe());
Haplotype<index_t>& ht = haplotypes.back();
for(index_t h = 0; h < ht.alts.size(); h++) {
ht.alts[h] = to_alti[ht.alts[h]];
}
if(haplotypes.size() == numHaplotypes) break;
}
}
}
}
if(verbose || startVerbose) cerr << "Opening \"" << in8Str.c_str() << "\"" << endl;
ifstream in8(in8Str.c_str(), ios::binary);
if(!in8.good()) {
cerr << "Could not open index file " << in8Str.c_str() << endl;
}
readI32(in8, this->toBe());
numAlts = readIndex<index_t>(in8, this->toBe());
if(numAlts > 0) {
while(!in8.eof()) {
altnames.expand();
in8 >> altnames.back();
if(altnames.size() == numAlts) break;
}
assert_eq(altnames.size(), numAlts);
}
in7.close();
in8.close();
// Sort SNPs and Splice Sites based on positions
index_t nalts = (index_t)alts.size();
for(index_t s = 0; s < nalts; s++) {
ALT<index_t> alt = alts[s];
if(alt.snp()) altdb->setSNPs(true);
if(alt.exon()) altdb->setExons(true);
if(alt.splicesite()) {
altdb->setSpliceSites(true);
alts.push_back(alt);
alts.back().left = alt.right;
alts.back().right = alt.left;
altnames.push_back("ssr");
} else if(alt.deletion()) {
alts.push_back(alt);
alts.back().pos = alt.pos + alt.len - 1;
alts.back().reversed = true;
string altname = altnames[s];
altnames.push_back(altname);
}
}
if(alts.size() > 1 && alts.size() > nalts) {
assert_eq(alts.size(), altnames.size());
EList<pair<ALT<index_t>, index_t> > buf; buf.resize(alts.size());
EList<string> buf2; buf2.resize(alts.size());
for(size_t i = 0; i < alts.size(); i++) {
buf[i].first = alts[i];
buf[i].second = (index_t)i;
buf2[i] = altnames[i];
}
buf.sort();
for(size_t i = 0; i < alts.size(); i++) {
alts[i] = buf[i].first;
altnames[i] = buf2[buf[i].second];
if(buf[i].second < numAlts) {
to_alti[buf[i].second] = i;
}
}
}
if(useHaplotype) {
EList<index_t>& haplotype_maxrights = altdb->haplotype_maxrights();
haplotype_maxrights.resizeExact(haplotypes.size());
for(index_t h = 0; h < haplotypes.size(); h++) {
Haplotype<index_t>& ht = haplotypes[h];
for(index_t h2 = 0; h2 < ht.alts.size(); h2++) {
ht.alts[h2] = to_alti[ht.alts[h2]];
}
if(h == 0) {
haplotype_maxrights[h] = ht.right;
} else {
haplotype_maxrights[h] = std::max<index_t>(haplotype_maxrights[h - 1], ht.right);
}
}
}
assert(repOk());
}
/// Construct an Ebwt from the given header parameters and string
/// vector, optionally using a blockwise suffix sorter with the
/// given 'bmax' and 'dcv' parameters. The string vector is
/// ultimately joined and the joined string is passed to buildToDisk().
GFM(
bool packed,
int needEntireReverse,
int32_t lineRate,
int32_t offRate,
int32_t ftabChars,
const string& file, // base filename for GFM files
bool fw,
int dcv,
EList<RefRecord>& szs,
index_t sztot,
const RefReadInParams& refparams,
uint32_t seed,
int32_t overrideOffRate = -1,
bool verbose = false,
bool passMemExc = false,
bool sanityCheck = false) :
GFM_INITS,
_gh(
joinedLen(szs),
0,
0,
lineRate,
offRate,
ftabChars,
0,
refparams.reverse == REF_READ_REVERSE)
{
#ifdef POPCNT_CAPABILITY
ProcessorSupport ps;
_usePOPCNTinstruction = ps.POPCNTenabled();
#endif
packed_ = packed;
}
/// Construct an Ebwt from the given header parameters and string
/// vector, optionally using a blockwise suffix sorter with the
/// given 'bmax' and 'dcv' parameters. The string vector is
/// ultimately joined and the joined string is passed to buildToDisk().
template<typename TStr>
GFM(
TStr& s,
bool packed,
int needEntireReverse,
int32_t lineRate,
int32_t offRate,
int32_t ftabChars,
int nthreads,
const string& snpfile,
const string& htfile,
const string& ssfile,
const string& exonfile,
const string& svfile,
const string& outfile, // base filename for GFM files
bool fw,
bool useBlockwise,
index_t bmax,
index_t bmaxSqrtMult,
index_t bmaxDivN,
int dcv,
EList<FileBuf*>& is,
EList<RefRecord>& szs,
index_t sztot,
const RefReadInParams& refparams,
uint32_t seed,
int32_t overrideOffRate = -1,
bool verbose = false,
bool passMemExc = false,
bool sanityCheck = false) :
GFM_INITS,
_gh(
joinedLen(szs),
0,
0,
lineRate,
offRate,
ftabChars,
0,
refparams.reverse == REF_READ_REVERSE)
{
assert_gt(nthreads, 0);
_nthreads = nthreads;
#ifdef POPCNT_CAPABILITY
ProcessorSupport ps;
_usePOPCNTinstruction = ps.POPCNTenabled();
#endif
_in1Str = outfile + ".1." + gfm_ext;
_in2Str = outfile + ".2." + gfm_ext;
packed_ = packed;
// Open output files
ofstream fout1(_in1Str.c_str(), ios::binary);
if(!fout1.good()) {
cerr << "Could not open index file for writing: \"" << _in1Str.c_str() << "\"" << endl
<< "Please make sure the directory exists and that permissions allow writing by" << endl
<< "HISAT2." << endl;
throw 1;
}
ofstream fout2(_in2Str.c_str(), ios::binary);
if(!fout2.good()) {
cerr << "Could not open index file for writing: \"" << _in2Str.c_str() << "\"" << endl
<< "Please make sure the directory exists and that permissions allow writing by" << endl
<< "HISAT2." << endl;
throw 1;
}
// Build
initFromVector<TStr>(
s,
snpfile,
htfile,
ssfile,
exonfile,
svfile,
is,
szs,
sztot,
refparams,
fout1,
fout2,
outfile,
useBlockwise,
bmax,
bmaxSqrtMult,
bmaxDivN,
dcv,
seed,
verbose);
// Close output files
fout1.flush();
int64_t tellpSz1 = (int64_t)fout1.tellp();
VMSG_NL("Wrote " << fout1.tellp() << " bytes to primary GFM file: " << _in1Str.c_str());
fout1.close();
bool err = false;
if(tellpSz1 > fileSize(_in1Str.c_str())) {
err = true;
cerr << "Index is corrupt: File size for " << _in1Str.c_str() << " should have been " << tellpSz1
<< " but is actually " << fileSize(_in1Str.c_str()) << "." << endl;
}
fout2.flush();
int64_t tellpSz2 = (int64_t)fout2.tellp();
VMSG_NL("Wrote " << fout2.tellp() << " bytes to secondary GFM file: " << _in2Str.c_str());
fout2.close();
if(tellpSz2 > fileSize(_in2Str.c_str())) {
err = true;
cerr << "Index is corrupt: File size for " << _in2Str.c_str() << " should have been " << tellpSz2
<< " but is actually " << fileSize(_in2Str.c_str()) << "." << endl;
}
if(err) {
cerr << "Please check if there is a problem with the disk or if disk is full." << endl;
throw 1;
}