-
Notifications
You must be signed in to change notification settings - Fork 120
/
aligner_seed2.h
4291 lines (4025 loc) · 128 KB
/
aligner_seed2.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 2011, Ben Langmead <[email protected]>
*
* This file is part of Bowtie 2.
*
* Bowtie 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.
*
* Bowtie 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 Bowtie 2. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALIGNER_SEED2_H_
#define ALIGNER_SEED2_H_
/**
* The user of the DescentDriver class specifies a collection of search roots.
* Logic for picking these search roots is located elsewhere, not in this
* module. The search roots are annotated with a priority score, which
*
* The heap is a min-heap over pairs, where the first element of each pair is
* the score associated with a descent and the second element of each pair is
* the descent ID.
*
* Weeding out redundant descents is key; otherwise we end up reporting slight
* variations on the same alignment repeatedly, including variations with poor
* scores. What criteria do we use to determine whether two paths are
* redundant?
*
* Here's an example where the same set of read characters have been aligned in
* all three cases:
*
* Alignment 1 (sc = 0):
* Rd: GCTATATAGCGCGCTCGCATCATTTTGTGT
* ||||||||||||||||||||||||||||||
* Rf: GCTATATAGCGCGCTCGCATCATTTTGTGT
*
* Alignment 2 (sc = -22):
* Rd: GCTATATAGCGCGCTCGCATCATTTTGTGT
* ||||||||||||||||||||||| | |||
* Rf: GCTATATAGCGCGCTCGCATCAT--TTTGT
*
* Alignment 3 (sc = -22):
* Rd: GCTATATAGCGCGCTCGCATCATT--TTGTGT
* |||||||||||||||||||||||| |||||
* Rf: GCTATATAGCGCGCTCGCATCATTTTGTGTGT
*
* Rf from aln 1: GCTATATAGCGCGCTCGCATCATTTTGTGT
* Rf from aln 2: GCTATATAGCGCGCTCGCATCATTTTGT
* Rf from aln 3: GCTATATAGCGCGCTCGCATCATTTTGTGTGT
*
* Are alignments 2 and 3 redundant with alignment 1? We can't totally say
* without knowing the associated SA ranges. Take alignments 1 and 2. Either
* the SA ranges are the same or the SA range for 2 contains the SA range for
* 1. If they're the same, then alignment 2 is redundant with alignment 1.
* Otherwise, *some* of the elements in the SA range for alignment 2 are not
* redundant.
*
* In that example, the same read characters are aligned in all three
* alignments. Is it possible and profitable to consider scenarios where an
* alignment might be redundant with another alignment
*
* Another question is *when* do we try to detect the redundancy? Before we
* try to extend through the matches, or after. After is easier, but less work
* has been avoided.
*
* What data structure do we query to determine whether there's redundancy?
* The situation is harder when we try to detect overlaps between SA ranges
* rather than identical SA ranges. Maybe: read intervals -> intersection tree -> penalties.
*
* 1. If we're introducing a gap and we could have introduced it deeper in the
* descent with the same effect w/r/t homopolymer length.
* 2. If we have Descent A with penalty B and Descent a with penalty b, and A
* aligns read characters [X, Y] to SA range [Z, W], and B aligns read
* characters [x, y] to SA range [z, w], then A is redundant with B if
* [x, y] is within [X, Y].
*
* Found an alignment with total penalty = 3
* GCAATATAGCGCGCTCGCATCATTTTGTGT
* || |||||||||||||||||||||||||||
* GCTATATAGCGCGCTCGCATCATTTTGTGT
*
* Found an alignment with total penalty = 27
* gCAATATAGCGCGCTCGCATCATTTTGTGT
* | ||||||||||||||||||||||||
* TATA-TAGCGCGCTCGCATCATTTTGTGT
*/
#include <stdint.h>
#include <math.h>
#include <utility>
#include <limits>
#include "assert_helpers.h"
#include "random_util.h"
#include "aligner_result.h"
#include "gfm.h"
#include "simple_func.h"
#include "scoring.h"
#include "edit.h"
#include "read.h"
#include "ds.h"
#include "group_walk.h"
#include "btypes.h"
typedef size_t TReadOff;
typedef int64_t TScore;
typedef float TRootPri;
typedef size_t TDescentId;
typedef size_t TRootId;
/**
* enum encapsulating a few different policies for how we might extend descents
* in the direction opposite from their primary direction.
*/
enum {
// Never extened in the direction opposite from the primary. Just go in
// the primary direction until the bounce.
DESC_EX_NONE = 1,
// When we're finished extending out the matches for a descent, try to
// extend in the opposite direction in a way that extends all branches
// simultaneously. The Descent.nex_ field contains the number of positions
// we were able to extend through in this way.
DESC_EX_FROM_1ST_BRANCH = 2,
// Each time we add an edge to the summary, extend it in the opposite
// direction. The DescentEdge.nex field contains the number of positions
// we were able to extend through, and this in turn gets propagated to
// Descent.nex_ if and when we branch from the DescentEdge.
DESC_EX_EACH_EDGE = 3
};
/**
* Counters to keep track of how much work is being done.
*/
struct DescentMetrics {
DescentMetrics() { reset(); }
void reset() {
bwops = bwops_1 = bwops_bi = recalc = branch = branch_mm =
branch_del = branch_ins = heap_max = descent_max = descentpos_max =
nex = 0;
}
uint64_t bwops; // # FM Index opbs
uint64_t bwops_1; // # LF1 FM Index opbs
uint64_t bwops_bi; // # BiEx FM Index opbs
uint64_t recalc; // # times outgoing edge summary was recalculated
uint64_t branch; // # times we descended from another descent
uint64_t branch_mm; // # times branch was on a mismatch
uint64_t branch_del; // # times branch was on a deletion
uint64_t branch_ins; // # times branch was on a insertion
uint64_t heap_max; // maximum size of Descent heap
uint64_t descent_max; // maximum size of Descent factory
uint64_t descentpos_max; // maximum size of DescentPos factory
uint64_t nex; // # extensions
};
/**
* Priority used to rank which descent we should branch from next. Right now,
* priority is governed by a 4-tuple. From higher to lower priority:
*
* 1. Penalty accumulated so far
* 2. Depth into the search space, including extensions
* 3. Width of the SA range (i.e. uniqueness)
* 4. Root priority
*/
struct DescentPriority {
DescentPriority() { reset(); }
DescentPriority(
TScore pen_,
size_t depth_,
TIndexOffU width_,
float rootpri_)
{
pen = pen_;
depth = depth_;
width = width_;
rootpri = rootpri_;
}
/**
* Initialize new DescentPriority.
*/
void init(TScore pen_, size_t depth_, TIndexOffU width_, float rootpri_) {
pen = pen_;
depth = depth_;
width = width_;
rootpri = rootpri_;
}
/**
* Reset to uninitialized state.
*/
void reset() {
width = 0;
}
/**
* Return true iff DescentPriority is initialized.
*/
bool inited() const {
return width > 0;
}
/**
* Return true iff this priority is prior to given priority.
*/
bool operator<(const DescentPriority& o) const {
assert(inited());
assert(o.inited());
// 1st priority: penalty accumulated so far
if(pen < o.pen) return true;
if(pen > o.pen) return false;
// 2nd priority: depth into the search space, including extensions
if(depth > o.depth) return true;
if(depth < o.depth) return false;
// 3rd priority: width of the SA range (i.e. uniqueness)
if(width < o.width) return true;
if(width > o.width) return false;
// 4th priority: root priority
if(rootpri > o.rootpri) return true;
return false;
}
/**
* Return true iff this priority is prior to or equal to given priority.
*/
bool operator<=(const DescentPriority& o) const {
assert(inited());
assert(o.inited());
// 1st priority: penalty accumulated so far
if(pen < o.pen) return true;
if(pen > o.pen) return false;
// 2nd priority: depth into the search space, including extensions
if(depth > o.depth) return true;
if(depth < o.depth) return false;
// 3rd priority: width of the SA range (i.e. uniqueness)
if(width < o.depth) return true;
if(width > o.width) return false;
// 4th priority: root priority
if(rootpri > o.rootpri) return true;
return true;
}
/**
* Return true iff this priority is prior to or equal to given priority.
*/
bool operator==(const DescentPriority& o) const {
assert(inited());
assert(o.inited());
return pen == o.pen && depth == o.depth && width == o.width && rootpri == o.rootpri;
}
TScore pen; // total penalty accumulated so far
size_t depth; // depth from root of descent
TIndexOffU width; // width of the SA range
float rootpri; // priority of the root
};
static inline std::ostream& operator<<(
std::ostream& os,
const DescentPriority& o)
{
os << "[" << o.pen << ", " << o.depth << ", " << o.width << ", " << o.rootpri << "]";
return os;
}
static inline std::ostream& operator<<(
std::ostream& os,
const std::pair<DescentPriority, TDescentId>& o)
{
os << "{[" << o.first.pen << ", " << o.first.depth << ", "
<< o.first.width << ", " << o.first.rootpri << "], " << o.second << "}";
return os;
}
typedef std::pair<DescentPriority, TDescentId> TDescentPair;
/**
* Encapsulates the constraints limiting which outgoing edges are permitted.
* Specifically, we constrain the total penalty accumulated so far so that some
* outgoing edges will exceed the limit and be pruned. The limit is set
* according to our "depth" into the search, as measured by the number of read
* characters aligned so far. We divide the depth domain into two pieces, a
* piece close to the root, where the penty is constrained to be 0, and the
* remainder, where the maximum penalty is an interpolation between 0 and the
* maximum penalty
*/
struct DescentConstraints {
DescentConstraints() { reset(); }
/**
* Initialize with new constraint function.
*/
DescentConstraints(size_t nzero, double exp) {
init(nzero, exp);
}
/**
* Initialize with given function.
*/
void init(size_t nzero_, double exp_) {
nzero = nzero_ > 0 ? nzero_ : 1;
exp = exp_;
#ifndef NDEBUG
for(size_t i = 1; i < nzero_ + 5; i++) {
assert_geq(get(i, nzero_ + 10, 100), get(i-1, nzero_ + 10, 100));
}
#endif
}
/**
* Reset to uninitialized state.
*/
void reset() {
nzero = 0;
exp = -1.0f;
}
/**
* Return true iff the DescentConstraints has been initialized.
*/
bool inited() const {
return exp >= 0.0f;
}
/**
* Get the maximum penalty total for depth 'off'.
*/
inline TScore get(TReadOff off, TReadOff rdlen, TAlScore maxpen) const {
if(off < nzero || nzero >= rdlen) {
return 0;
}
double frac = (double)(off - nzero) / (rdlen - nzero);
if(fabs(exp - 1.0f) > 0.00001) {
if(fabs(exp - 2.0f) < 0.00001) {
frac *= frac;
} else {
frac = pow(frac, exp);
}
}
return (TAlScore)(frac * maxpen + 0.5f);
}
size_t nzero;
double exp;
};
/**
* Encapsulates settings governing how we descent.
*/
struct DescentConfig {
DescentConfig() { reset(); }
/**
* Reset the DescentConfig to an uninitialized state.
*/
void reset() { expol = 0; }
/**
* Return true iff this DescentConfig is initialized.
*/
bool inited() const { return expol != 0; }
DescentConstraints cons; // constraints
int expol; // extend policy
};
/**
* Encapsulates the state of a Descent that allows us to determine whether it
* is redundant with another Descent. Two Descents are redundant if:
*
* 1. Both are aligning the same read orientation (fw or rc)
* 2. Both are growing the alignment in the same direction (left-to-right or
* right-to-left)
* 3. They have aligned exactly the same read characters (which are always
* consecutive in the read)
* 4. The corresponding reference strings are identical
*/
struct DescentRedundancyKey {
DescentRedundancyKey() { reset(); }
DescentRedundancyKey(
TReadOff al5pf_,
size_t rflen_,
TIndexOffU topf_,
TIndexOffU botf_)
{
init(al5pf_, rflen_, topf_, botf_);
}
void reset() {
al5pf = 0;
rflen = 0;
topf = botf = 0;
}
bool inited() const { return rflen > 0; }
void init(
TReadOff al5pf_,
size_t rflen_,
TIndexOffU topf_,
TIndexOffU botf_)
{
al5pf = al5pf_;
rflen = rflen_;
topf = topf_;
botf = botf_;
}
bool operator==(const DescentRedundancyKey& o) const {
return al5pf == o.al5pf && rflen == o.rflen && topf == o.topf && botf == o.botf;
}
bool operator<(const DescentRedundancyKey& o) const {
if(al5pf < o.al5pf) return true;
if(al5pf > o.al5pf) return false;
if(rflen < o.rflen) return true;
if(rflen > o.rflen) return false;
if(topf < o.topf) return true;
if(topf > o.topf) return false;
return botf < o.botf;
}
TReadOff al5pf; // 3'-most aligned char, as offset from 5' end
size_t rflen; // number of reference characters involved in alignment
TIndexOffU topf; // top w/r/t forward index
TIndexOffU botf; // bot w/r/t forward index
};
/**
* Map from pairs to top, bot, penalty triples.
*/
class DescentRedundancyChecker {
public:
DescentRedundancyChecker() { reset(); }
void clear() { reset(); }
/**
* Reset to uninitialized state.
*/
void reset() {
bits_.reset();
inited_ = false;
totsz_ = 0; // total size
totcap_ = 0; // total capacity
}
const static int NPARTS = 8;
const static int PART_MASK = 7;
const static int NBITS = (1 << 16);
/**
* Initialize using given read length.
*/
void init(TReadOff rdlen) {
reset();
// daehwan - for debugging purposes
#if 0
bits_.resize(NBITS);
maplist_fl_.resize(NPARTS);
maplist_fr_.resize(NPARTS);
maplist_rl_.resize(NPARTS);
maplist_rr_.resize(NPARTS);
for(int i = 0; i < NPARTS; i++) {
maplist_fl_[i].resize(rdlen);
maplist_fr_[i].resize(rdlen);
maplist_rl_[i].resize(rdlen);
maplist_rr_[i].resize(rdlen);
totcap_ += maplist_fl_[i].totalCapacityBytes();
totcap_ += maplist_fr_[i].totalCapacityBytes();
totcap_ += maplist_rl_[i].totalCapacityBytes();
totcap_ += maplist_rr_[i].totalCapacityBytes();
for(size_t j = 0; j < rdlen; j++) {
maplist_fl_[i][j].clear();
maplist_fr_[i][j].clear();
maplist_rl_[i][j].clear();
maplist_rr_[i][j].clear();
totcap_ += maplist_fl_[i][j].totalCapacityBytes();
totcap_ += maplist_fr_[i][j].totalCapacityBytes();
totcap_ += maplist_rl_[i][j].totalCapacityBytes();
totcap_ += maplist_rr_[i][j].totalCapacityBytes();
}
}
#endif
inited_ = true;
}
/**
* Return true iff the checker is initialized.
*/
bool inited() const {
return inited_;
}
/**
* Check if this partial alignment is redundant with one that we've already
* explored.
*/
bool check(
bool fw,
bool l2r,
TReadOff al5pi,
TReadOff al5pf,
size_t rflen,
TIndexOffU topf,
TIndexOffU botf,
TScore pen)
{
// daehwan - for debugging purposes
return true;
assert(inited_);
assert(topf > 0 || botf > 0);
DescentRedundancyKey k(al5pf, rflen, topf, botf);
size_t i = std::numeric_limits<size_t>::max();
size_t mask = topf & PART_MASK;
EMap<DescentRedundancyKey, TScore>& map =
(fw ? (l2r ? maplist_fl_[mask][al5pi] : maplist_fr_[mask][al5pi]) :
(l2r ? maplist_rl_[mask][al5pi] : maplist_rr_[mask][al5pi]));
size_t key = (topf & 255) | ((botf & 255) << 8);
if(bits_.test(key) && map.containsEx(k, i)) {
// Already contains the key
assert_lt(i, map.size());
assert_geq(pen, map[i].second);
return false;
}
assert(!map.containsEx(k, i));
size_t oldsz = map.totalSizeBytes();
size_t oldcap = map.totalCapacityBytes();
map.insert(make_pair(k, pen));
bits_.set(key);
totsz_ += (map.totalSizeBytes() - oldsz);
totcap_ += (map.totalCapacityBytes() - oldcap);
return true;
}
/**
* Check if this partial alignment is redundant with one that we've already
* explored using the Bw index SA range.
*/
bool contains(
bool fw,
bool l2r,
TReadOff al5pi,
TReadOff al5pf,
size_t rflen,
TIndexOffU topf,
TIndexOffU botf,
TScore pen)
{
// daehwan - for debugging purposes
return false;
assert(inited_);
size_t key = (topf & 255) | ((botf & 255) << 8);
if(!bits_.test(key)) {
return false;
}
DescentRedundancyKey k(al5pf, rflen, topf, botf);
size_t mask = topf & PART_MASK;
EMap<DescentRedundancyKey, TScore>& map =
(fw ? (l2r ? maplist_fl_[mask][al5pi] : maplist_fr_[mask][al5pi]) :
(l2r ? maplist_rl_[mask][al5pi] : maplist_rr_[mask][al5pi]));
return map.contains(k);
}
/**
* Return the total size of the redundancy map.
*/
size_t totalSizeBytes() const {
return totsz_;
}
/**
* Return the total capacity of the redundancy map.
*/
size_t totalCapacityBytes() const {
return totcap_;
}
protected:
bool inited_; // initialized?
size_t totsz_; // total size
size_t totcap_; // total capacity
// List of maps. Each entry is a map for all the DescentRedundancyKeys
// with al5pi equal to the offset into the list.
ELList<EMap<DescentRedundancyKey, TScore>, NPARTS, 100> maplist_fl_; // fw, l2r
ELList<EMap<DescentRedundancyKey, TScore>, NPARTS, 100> maplist_rl_; // !fw, l2r
ELList<EMap<DescentRedundancyKey, TScore>, NPARTS, 100> maplist_fr_; // fw, !l2r
ELList<EMap<DescentRedundancyKey, TScore>, NPARTS, 100> maplist_rr_; // !fw, !l2r
EBitList<128> bits_;
};
/**
* A search root. Consists of an offset from the 5' end read and flags
* indicating (a) whether we're initially heading left-to-right or
* right-to-left, and (b) whether we're examining the read or its reverse
* complement.
*
* A root also comes with a priority ("pri") score indicating how promising it
* is as a root. Promising roots have long stretches of high-quality,
* non-repetitive nucleotides in the first several ply of the search tree.
* Also, roots beginning at the 5' end of the read may receive a higher
* priority.
*/
struct DescentRoot {
DescentRoot() { reset(); }
DescentRoot(size_t off5p_, bool l2r_, bool fw_, size_t len, float pri_) {
init(off5p_, l2r_, fw_, len, pri_);
}
/**
* Reset this DescentRoot to uninitialized state.
*/
void reset() {
off5p = std::numeric_limits<size_t>::max();
}
/**
* Return true iff this DescentRoot is uninitialized.
*/
bool inited() const {
return off5p == std::numeric_limits<size_t>::max();
}
/**
* Initialize a new descent root.
*/
void init(size_t off5p_, bool l2r_, bool fw_, size_t len, float pri_) {
off5p = off5p_;
l2r = l2r_;
fw = fw_;
pri = pri_;
assert_lt(off5p, len);
}
TReadOff off5p; // root origin offset, expressed as offset from 5' end
bool l2r; // true -> move in left-to-right direction
bool fw; // true -> work with forward read, false -> revcomp
float pri; // priority of seed
};
/**
* Set of flags indicating outgoing edges we've tried from a DescentPos.
*/
struct DescentPosFlags {
DescentPosFlags() { reset(); }
/**
* Set all flags to 1, indicating all outgoing edges are yet to be
* explored.
*/
void reset() {
mm_a = mm_c = mm_g = mm_t = rdg_a = rdg_c = rdg_g = rdg_t = rfg = 1;
reserved = 0;
}
/**
* Return true iff all outgoing edges have already been explored.
*/
bool exhausted() const {
return ((uint16_t*)this)[0] == 0;
}
/**
* Return false iff the specified mismatch has already been explored.
*/
bool mmExplore(int c) {
assert_range(0, 3, c);
if(c == 0) {
return mm_a;
} else if(c == 1) {
return mm_c;
} else if(c == 2) {
return mm_g;
} else {
return mm_t;
}
}
/**
* Try to explore a mismatch. Return false iff it has already been
* explored.
*/
bool mmSet(int c) {
assert_range(0, 3, c);
if(c == 0) {
bool ret = mm_a; mm_a = 0; return ret;
} else if(c == 1) {
bool ret = mm_c; mm_c = 0; return ret;
} else if(c == 2) {
bool ret = mm_g; mm_g = 0; return ret;
} else {
bool ret = mm_t; mm_t = 0; return ret;
}
}
/**
* Return false iff specified read gap has already been explored.
*/
bool rdgExplore(int c) {
assert_range(0, 3, c);
if(c == 0) {
return rdg_a;
} else if(c == 1) {
return rdg_c;
} else if(c == 2) {
return rdg_g;
} else {
return rdg_t;
}
}
/**
* Try to explore a read gap. Return false iff it has already been
* explored.
*/
bool rdgSet(int c) {
assert_range(0, 3, c);
if(c == 0) {
bool ret = rdg_a; rdg_a = 0; return ret;
} else if(c == 1) {
bool ret = rdg_c; rdg_c = 0; return ret;
} else if(c == 2) {
bool ret = rdg_g; rdg_g = 0; return ret;
} else {
bool ret = rdg_t; rdg_t = 0; return ret;
}
}
/**
* Return false iff the reference gap has already been explored.
*/
bool rfgExplore() {
return rfg;
}
/**
* Try to explore a reference gap. Return false iff it has already been
* explored.
*/
bool rfgSet() {
bool ret = rfg; rfg = 0; return ret;
}
uint16_t mm_a : 1;
uint16_t mm_c : 1;
uint16_t mm_g : 1;
uint16_t mm_t : 1;
uint16_t rdg_a : 1;
uint16_t rdg_c : 1;
uint16_t rdg_g : 1;
uint16_t rdg_t : 1;
uint16_t rfg : 1;
uint16_t reserved : 7;
};
/**
* FM Index state associated with a single position in a descent. For both the
* forward and backward indexes, it stores the four SA ranges corresponding to
* the four nucleotides.
*/
struct DescentPos {
/**
* Reset all tops and bots to 0.
*/
void reset() {
topf[0] = topf[1] = topf[2] = topf[3] = 0;
botf[0] = botf[1] = botf[2] = botf[3] = 0;
topb[0] = topb[1] = topb[2] = topb[3] = 0;
botb[0] = botb[1] = botb[2] = botb[3] = 0;
c = -1;
flags.reset();
}
/**
* Return true iff DescentPos has been initialized.
*/
bool inited() const {
return c >= 0;
}
#ifndef NDEBUG
/**
* Check that DescentPos is internally consistent.
*/
bool repOk() const {
assert_range(0, 3, (int)c);
return true;
}
#endif
TIndexOffU topf[4]; // SA range top indexes in fw index
TIndexOffU botf[4]; // SA range bottom indexes (exclusive) in fw index
TIndexOffU topb[4]; // SA range top indexes in bw index
TIndexOffU botb[4]; // SA range bottom indexes (exclusive) in bw index
char c; // read char that would yield match
DescentPosFlags flags; // flags
};
/**
* Encapsulates an edge outgoing from a descent.
*/
struct DescentEdge {
DescentEdge() { reset(); }
DescentEdge(
Edit e_,
TReadOff off5p_,
DescentPriority pri_,
size_t posFlag_,
TReadOff nex_
#ifndef NDEBUG
,
size_t d_,
TIndexOffU topf_,
TIndexOffU botf_,
TIndexOffU topb_,
TIndexOffU botb_
#endif
)
{
init(e_, off5p_, pri_, posFlag_
#ifndef NDEBUG
, d_, topf_, botf_, topb_, botb_
#endif
);
}
/**
* Return true iff edge is initialized.
*/
bool inited() const { return e.inited(); }
/**
* Reset to uninitialized state.
*/
void reset() { e.reset(); }
/**
* Initialize DescentEdge given 5' offset, nucleotide, and priority.
*/
void init(
Edit e_,
TReadOff off5p_,
DescentPriority pri_,
size_t posFlag_
#ifndef NDEBUG
,
size_t d_,
TIndexOffU topf_,
TIndexOffU botf_,
TIndexOffU topb_,
TIndexOffU botb_
#endif
)
{
e = e_;
off5p = off5p_;
pri = pri_;
posFlag = posFlag_;
#ifndef NDEBUG
d = d_;
topf = topf_;
botf = botf_;
topb = topb_;
botb = botb_;
#endif
}
/**
* Update flags to show this edge as visited.
*/
void updateFlags(EFactory<DescentPos>& pf) {
if(inited()) {
if(e.isReadGap()) {
assert_neq('-', e.chr);
pf[posFlag].flags.rdgSet(asc2dna[e.chr]);
} else if(e.isRefGap()) {
pf[posFlag].flags.rfgSet();
} else {
assert_neq('-', e.chr);
pf[posFlag].flags.mmSet(asc2dna[e.chr]);
}
}
}
/**
* Return true iff this edge has higher priority than the given edge.
*/
bool operator<(const DescentEdge& o) const {
if(inited() && !o.inited()) {
return true;
} else if(!inited()) {
return false;
}
return pri < o.pri;
}
DescentPriority pri; // priority of the edge
//TReadOff nex; // # extends possible from this edge
size_t posFlag; // depth of DescentPos where flag should be set
#ifndef NDEBUG
// This can be recreated by looking at the edit, the paren't descent's
// len_, al5pi_, al5pf_. I have it here so we can sanity check.
size_t d;
TIndexOffU topf, botf, topb, botb;
#endif
Edit e;
TReadOff off5p;
};
/**
* Encapsulates an incomplete summary of the outgoing edges from a descent. We
* don't try to store information about all outgoing edges, because doing so
* will generally be wasteful. We'll typically only try a handful of them per
* descent.
*/
class DescentOutgoing {
public:
/**
* Return the best edge and rotate in preparation for next call.
*/
DescentEdge rotate() {
DescentEdge tmp = best1;
assert(!(best2 < tmp));
best1 = best2;
assert(!(best3 < best2));
best2 = best3;
assert(!(best4 < best3));
best3 = best4;
assert(!(best5 < best4));
best4 = best5;
best5.reset();
return tmp;
}
/**
* Given a potental outgoing edge, place it where it belongs in the running
* list of best 5 outgoing edges from this descent.
*/
void update(DescentEdge e) {
if(!best1.inited()) {
best1 = e;
} else if(e < best1) {
best5 = best4;
best4 = best3;
best3 = best2;
best2 = best1;
best1 = e;
} else if(!best2.inited()) {
best2 = e;
} else if(e < best2) {
best5 = best4;
best4 = best3;
best3 = best2;
best2 = e;
} else if(!best3.inited()) {
best3 = e;
} else if(e < best3) {
best5 = best4;
best4 = best3;
best3 = e;
} else if(!best4.inited()) {
best4 = e;