-
Notifications
You must be signed in to change notification settings - Fork 77
/
range_source.h
2465 lines (2305 loc) · 65.9 KB
/
range_source.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
/*
* range_source.h
*/
#ifndef RANGE_SOURCE_H_
#define RANGE_SOURCE_H_
#include <stdint.h>
#include <queue>
#include <vector>
#include "ds.h"
#include "ebwt.h"
#include "range.h"
#include "pool.h"
#include "edit.h"
enum AdvanceUntil {
ADV_FOUND_RANGE = 1,
ADV_COST_CHANGES,
ADV_STEP
};
/**
* List of Edits that automatically expands as edits are added.
*/
struct EditList {
EditList() : sz_(0), moreEdits_(NULL), yetMoreEdits_(NULL) { }
/**
* Add an edit to the edit list.
*/
bool add(const Edit& e, AllocOnlyPool<Edit>& pool, size_t qlen) {
assert_lt(sz_, qlen + 10);
if(sz_ < numEdits) {
assert(moreEdits_ == NULL);
assert(yetMoreEdits_ == NULL);
edits_[sz_++] = e;
} else if(sz_ == numEdits) {
assert(moreEdits_ == NULL);
assert(yetMoreEdits_ == NULL);
moreEdits_ = pool.alloc(numMoreEdits);
if(moreEdits_ == NULL) {
return false;
}
assert(moreEdits_ != NULL);
moreEdits_[0] = e;
sz_++;
} else if(sz_ < (numEdits + numMoreEdits)) {
assert(moreEdits_ != NULL);
assert(yetMoreEdits_ == NULL);
moreEdits_[sz_ - numEdits] = e;
sz_++;
} else if(sz_ == (numEdits + numMoreEdits)) {
assert(moreEdits_ != NULL);
assert(yetMoreEdits_ == NULL);
yetMoreEdits_ = pool.alloc((uint32_t)qlen + 10 - numMoreEdits - numEdits);
if(yetMoreEdits_ == NULL) {
return false;
}
assert(yetMoreEdits_ != NULL);
yetMoreEdits_[0] = e;
sz_++;
} else {
assert(moreEdits_ != NULL);
assert(yetMoreEdits_ != NULL);
yetMoreEdits_[sz_ - numEdits - numMoreEdits] = e;
sz_++;
}
return true;
}
/**
* Return a const reference to the ith Edit in the list.
*/
const Edit& get(size_t i) const {
assert_lt(i, sz_);
if(i < numEdits) {
return edits_[i];
} else if(i < (numEdits + numMoreEdits)) {
assert(moreEdits_ != NULL);
return moreEdits_[i-numEdits];
} else {
assert(moreEdits_ != NULL);
assert(yetMoreEdits_ != NULL);
return yetMoreEdits_[i-numEdits-numMoreEdits];
}
}
/**
* Get most recently added Edit.
*/
const Edit& top() const {
assert_gt(size(), 0);
return get(size()-1);
}
/**
* Return true iff no Edits have been added.
*/
bool empty() const { return size() == 0; }
/**
* Set a particular element of the EditList.
*/
void set(size_t i, const Edit& e) {
assert_lt(i, sz_);
if(i < numEdits) {
edits_[i] = e;
} else if(i < (numEdits + numMoreEdits)) {
assert(moreEdits_ != NULL);
moreEdits_[i-numEdits] = e;
} else {
assert(moreEdits_ != NULL);
assert(yetMoreEdits_ != NULL);
yetMoreEdits_[i-numEdits-numMoreEdits] = e;
}
}
/**
* Remove all Edits from the list.
*/
void clear() {
sz_ = 0;
moreEdits_ = NULL;
yetMoreEdits_ = NULL;
}
/**
* Return number of Edits in the List.
*/
size_t size() const { return sz_; }
/**
* Free all the heap-allocated edit lists
*/
void free(AllocOnlyPool<Edit>& epool, size_t qlen) {
if(yetMoreEdits_ != NULL)
epool.free(yetMoreEdits_, (uint32_t)qlen + 10 - numMoreEdits - numEdits);
if(moreEdits_ != NULL)
epool.free(moreEdits_, numMoreEdits);
}
const static size_t numEdits = 6; // part of object allocation
const static size_t numMoreEdits = 16; // first extra allocation
size_t sz_; // number of Edits stored in the EditList
Edit edits_[numEdits]; // first 4 edits; typically, no more are needed
Edit *moreEdits_; // if used, size is dictated by numMoreEdits
Edit *yetMoreEdits_; // if used, size is dictated by length of read
};
/**
* Holds per-position information about what outgoing paths have been
* eliminated and what the quality value(s) is (are) at the position.
*/
union ElimsAndQual {
/**
* Assuming qual A/C/G/T are already set, set quallo and quallo2
* to the additional cost incurred by the least and second-least
* costly paths.
*/
void updateLo() {
flags.quallo = 127;
flags.quallo2 = 127;
if(!flags.mmA) {
// A mismatch to an A in the genome has not been ruled out
if(flags.qualA < flags.quallo) {
//flags.quallo2 = flags.quallo;
flags.quallo = flags.qualA;
}
//else if(flags.qualA == flags.quallo) {
// flags.quallo2 = flags.quallo;
//} else if(flags.qualA < flags.quallo2) {
// flags.quallo2 = flags.qualA;
//}
}
if(!flags.mmC) {
// A mismatch to a C in the genome has not been ruled out
if(flags.qualC < flags.quallo) {
flags.quallo2 = flags.quallo;
flags.quallo = flags.qualC;
} else if(flags.qualC == flags.quallo) {
flags.quallo2 = flags.quallo;
} else if(flags.qualC < flags.quallo2) {
flags.quallo2 = flags.qualC;
}
}
if(!flags.mmG) {
// A mismatch to a G in the genome has not been ruled out
if(flags.qualG < flags.quallo) {
flags.quallo2 = flags.quallo;
flags.quallo = flags.qualG;
} else if(flags.qualG == flags.quallo) {
flags.quallo2 = flags.quallo;
} else if(flags.qualG < flags.quallo2) {
flags.quallo2 = flags.qualG;
}
}
if(!flags.mmT) {
// A mismatch to a T in the genome has not been ruled out
if(flags.qualT < flags.quallo) {
flags.quallo2 = flags.quallo;
flags.quallo = flags.qualT;
} else if(flags.qualT == flags.quallo) {
flags.quallo2 = flags.quallo;
} else if(flags.qualT < flags.quallo2) {
flags.quallo2 = flags.qualT;
}
}
assert(repOk());
}
/**
* Set all 13 elimination bits of the flags field to 1, indicating
* that all outgoing paths are eliminated.
*/
inline void eliminate() {
join.elims = ((1 << 13) - 1);
}
/**
* Internal consistency check. Basically just checks that lo and
* lo2 are set correctly.
*/
bool repOk() const {
uint8_t lo = 127;
uint8_t lo2 = 127;
assert_lt(flags.qualA, 127);
assert_lt(flags.qualC, 127);
assert_lt(flags.qualG, 127);
assert_lt(flags.qualT, 127);
if(!flags.mmA) {
if(flags.qualA < lo) {
lo = flags.qualA;
}
//else if(flags.qualA == lo || flags.qualA < lo2) {
// lo2 = flags.qualA;
//}
}
if(!flags.mmC) {
if(flags.qualC < lo) {
lo2 = lo;
lo = flags.qualC;
} else if(flags.qualC == lo || flags.qualC < lo2) {
lo2 = flags.qualC;
}
}
if(!flags.mmG) {
if(flags.qualG < lo) {
lo2 = lo;
lo = flags.qualG;
} else if(flags.qualG == lo || flags.qualG < lo2) {
lo2 = flags.qualG;
}
}
if(!flags.mmT) {
if(flags.qualT < lo) {
lo2 = lo;
lo = flags.qualT;
} else if(flags.qualT == lo || flags.qualT < lo2) {
lo2 = flags.qualT;
}
}
assert_eq((int)lo, (int)flags.quallo);
assert_eq((int)lo2, (int)flags.quallo2);
return true;
}
struct {
uint64_t mmA : 1; // A in ref aligns to non-A char in read
uint64_t mmC : 1; // C in ref aligns to non-C char in read
uint64_t mmG : 1; // G in ref aligns to non-G char in read
uint64_t mmT : 1; // T in ref aligns to non-T char in read
uint64_t snpA : 1; // Same as mmA, but we think it's a SNP and not a miscall
uint64_t snpC : 1; // Same as mmC, but we think it's a SNP and not a miscall
uint64_t snpG : 1; // Same as mmG, but we think it's a SNP and not a miscall
uint64_t snpT : 1; // Same as mmT, but we think it's a SNP and not a miscall
uint64_t insA : 1; // A insertion in reference w/r/t read
uint64_t insC : 1; // C insertion in reference w/r/t read
uint64_t insG : 1; // G insertion in reference w/r/t read
uint64_t insT : 1; // T insertion in reference w/r/t read
uint64_t del : 1; // deletion of read character
uint64_t qualA : 7; // quality penalty for picking A at this position
uint64_t qualC : 7; // quality penalty for picking C at this position
uint64_t qualG : 7; // quality penalty for picking G at this position
uint64_t qualT : 7; // quality penalty for picking T at this position
uint64_t quallo : 7; // lowest quality penalty at this position
uint64_t quallo2 : 7; // 2nd-lowest quality penalty at this position
uint64_t reserved : 9;
} flags;
struct {
uint64_t elims : 13; // all of the edit-elim flags bundled together
uint64_t quals : 42; // quality of positions
uint64_t reserved : 9;
} join;
struct {
uint64_t mmElims : 4; // substitution flags bundled together
uint64_t snpElims : 4; // substitution flags bundled together
uint64_t insElims : 4; // inserts-in-reference flags bundled together
uint64_t delElims : 1; // deletion of read character
uint64_t quals : 42; // quality of positions
uint64_t reserved : 9;
} join2;
};
/**
* All per-position state, including the ranges calculated for each
* character, the quality value at the position, and a set of flags
* recording whether we've tried each way of proceeding from this
* position.
*/
struct RangeState {
/**
* Using randomness when picking from among multiple choices, pick
* an edit to make. TODO: Only knows how to pick mismatches for
* now.
*/
Edit pickEdit(int pos, RandomSource& rand,
TIndexOffU& top, TIndexOffU& bot, bool indels,
bool& last)
{
Edit ret;
ret.type = EDIT_TYPE_MM;
ret.pos = pos;
ret.chr = 0;
ret.qchr = 0;
ret.reserved = 0;
assert(!eliminated_);
assert(!eq.flags.mmA || !eq.flags.mmC || !eq.flags.mmG || !eq.flags.mmT);
int num = !eq.flags.mmA + !eq.flags.mmC + !eq.flags.mmG + !eq.flags.mmT;
assert_leq(num, 4);
assert_gt(num, 0);
if(num == 2) eq.flags.quallo2 = 127;
// Only need to pick randomly if there's a quality tie
if(num > 1) {
last = false; // not the last at this pos
// Sum up range sizes and do a random weighted pick
TIndexOffU tot = 0;
bool candA = !eq.flags.mmA; bool candC = !eq.flags.mmC;
bool candG = !eq.flags.mmG; bool candT = !eq.flags.mmT;
bool candInsA = false, candInsC = false;
bool candInsG = false, candInsT = false;
bool candDel = false;
if(indels) {
// Insertions and deletions can only be candidates
// if the user asked for indels
candInsA = !eq.flags.insA;
candInsC = !eq.flags.insC;
candInsG = !eq.flags.insG;
candInsT = !eq.flags.insT;
candDel = !eq.flags.del;
}
ASSERT_ONLY(int origNum = num);
if(candA) {
assert_gt(bots[0], tops[0]);
tot += (bots[0] - tops[0]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candC) {
assert_gt(bots[1], tops[1]);
tot += (bots[1] - tops[1]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candG) {
assert_gt(bots[2], tops[2]);
tot += (bots[2] - tops[2]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candT) {
assert_gt(bots[3], tops[3]);
tot += (bots[3] - tops[3]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(indels) {
if(candInsA) {
assert_gt(bots[0], tops[0]);
tot += (bots[0] - tops[0]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candInsC) {
assert_gt(bots[1], tops[1]);
tot += (bots[1] - tops[1]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candInsG) {
assert_gt(bots[2], tops[2]);
tot += (bots[2] - tops[2]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candInsT) {
assert_gt(bots[3], tops[3]);
tot += (bots[3] - tops[3]);
assert_gt(num, 0);
ASSERT_ONLY(num--);
}
if(candDel) {
// Always a candidate?
// Always a candidate just within the window?
// We can eliminate some indels based on the content downstream, but not many
}
}
assert_geq(num, 0);
assert_lt(num, origNum);
// Throw a dart randomly that hits one of the possible
// substitutions, with likelihoods weighted by range size
uint32_t dart = rand.nextU32() % tot;
if(candA) {
if(dart < (bots[0] - tops[0])) {
// Eliminate A mismatch
top = tops[0];
bot = bots[0];
eq.flags.mmA = 1;
assert_lt(eq.join2.mmElims, 15);
ret.chr = 'A';
return ret;
}
dart -= (bots[0] - tops[0]);
}
if(candC) {
if(dart < (bots[1] - tops[1])) {
// Eliminate C mismatch
top = tops[1];
bot = bots[1];
eq.flags.mmC = 1;
assert_lt(eq.join2.mmElims, 15);
ret.chr = 'C';
return ret;
}
dart -= (bots[1] - tops[1]);
}
if(candG) {
if(dart < (bots[2] - tops[2])) {
// Eliminate G mismatch
top = tops[2];
bot = bots[2];
eq.flags.mmG = 1;
assert_lt(eq.join2.mmElims, 15);
ret.chr = 'G';
return ret;
}
dart -= (bots[2] - tops[2]);
}
if(candT) {
assert_lt(dart, (bots[3] - tops[3]));
// Eliminate T mismatch
top = tops[3];
bot = bots[3];
eq.flags.mmT = 1;
assert_lt(eq.join2.mmElims, 15);
ret.chr = 'T';
}
} else {
last = true; // last at this pos
// There's only one; pick it!
int chr = -1;
if(!eq.flags.mmA) {
chr = 0;
} else if(!eq.flags.mmC) {
chr = 1;
} else if(!eq.flags.mmG) {
chr = 2;
} else {
assert(!eq.flags.mmT);
chr = 3;
}
ret.chr = "ACGT"[chr];
top = tops[chr];
bot = bots[chr];
//assert_eq(15, eq.join2.mmElims);
// Mark entire position as eliminated
eliminated_ = true;
}
return ret;
}
/**
* Return true (without assertion) iff this RangeState is
* internally consistent.
*/
bool repOk() {
if(eliminated_) return true;
// Uneliminated chars must have non-empty ranges
if(!eq.flags.mmA || !eq.flags.insA) assert_gt(bots[0], tops[0]);
if(!eq.flags.mmC || !eq.flags.insC) assert_gt(bots[1], tops[1]);
if(!eq.flags.mmG || !eq.flags.insG) assert_gt(bots[2], tops[2]);
if(!eq.flags.mmT || !eq.flags.insT) assert_gt(bots[3], tops[3]);
return true;
}
// Outgoing ranges; if the position being described is not a
// legitimate jumping-off point for a branch, tops[] and bots[]
// will be filled with 0s and all possibilities in eq will be
// eliminated
TIndexOffU tops[4]; // A, C, G, T top offsets
TIndexOffU bots[4]; // A, C, G, T bot offsets
ElimsAndQual eq; // Which outgoing paths have been tried already
bool eliminated_; // Whether all outgoing paths have been eliminated
};
/**
* Encapsulates a "branch" of the search space; i.e. all of the
* information deduced by walking along a path with only matches, along
* with information about the decisions that lead to the root of that
* path.
*/
class Branch {
typedef std::pair<TIndexOffU, TIndexOffU> UPair;
public:
Branch() :
delayedCost_(0), curtailed_(false), exhausted_(false),
prepped_(false), delayedIncrease_(false) { }
/**
* Initialize a new branch object with an empty path.
*/
bool init(AllocOnlyPool<RangeState>& rsPool,
AllocOnlyPool<Edit>& epool,
uint32_t id,
uint32_t qlen,
uint16_t depth0,
uint16_t depth1,
uint16_t depth2,
uint16_t depth3,
uint16_t rdepth,
uint16_t len,
uint16_t cost,
uint16_t ham,
TIndexOffU itop,
TIndexOffU ibot,
const EbwtParams& ep,
const uint8_t* ebwt,
const EditList* edits = NULL)
{
id_ = id;
delayedCost_ = 0;
depth0_ = depth0;
depth1_ = depth1;
depth2_ = depth2;
depth3_ = depth3;
assert_gt(depth3_, 0);
rdepth_ = rdepth;
len_ = len;
cost_ = cost;
ham_ = ham;
top_ = itop;
bot_ = ibot;
if(ibot > itop+1) {
// Care about both top and bot
SideLocus::initFromTopBot(itop, ibot, ep, ebwt, ltop_, lbot_);
} else if(ibot > itop) {
// Only care about top
ltop_.initFromRow(itop, ep, ebwt);
lbot_.invalidate();
}
if(qlen - rdepth_ > 0) {
ranges_ = rsPool.allocC(qlen - rdepth_); // allocated from the RangeStatePool
if(ranges_ == NULL) {
return false; // RangeStatePool exhausted
}
rangesSz_ = qlen - rdepth_;
} else {
ranges_ = NULL;
rangesSz_ = 0;
}
#ifndef NDEBUG
for(size_t i = 0; i < (qlen - rdepth_); i++) {
for(int j = 0; j < 4; j++) {
assert_eq(0, ranges_[i].tops[j]);
assert_eq(0, ranges_[i].bots[j]);
}
}
#endif
curtailed_ = false;
exhausted_ = false;
prepped_ = true;
delayedIncrease_ = false;
edits_.clear();
if(edits != NULL) {
const size_t numEdits = edits->size();
for(size_t i = 0; i < numEdits; i++) {
edits_.add(edits->get(i), epool, qlen);
}
}
// If we're starting with a non-zero length, that means we're
// jumping over a bunch of unrevisitable positions.
for(size_t i = 0; i < len_; i++) {
ranges_[i].eliminated_ = true;
assert(eliminated((int)i));
}
assert(repOk(qlen));
return true;
}
/**
* Depth of the deepest tip of the branch.
*/
uint16_t tipDepth() const {
return rdepth_ + len_;
}
/**
* Return true iff all outgoing edges from position i have been
* eliminated.
*/
inline bool eliminated(int i) const {
assert(!exhausted_);
if(i <= len_ && i < rangesSz_) {
assert(ranges_ != NULL);
#ifndef NDEBUG
if(!ranges_[i].eliminated_) {
// Someone must be as-yet-uneliminated
assert(!ranges_[i].eq.flags.mmA ||
!ranges_[i].eq.flags.mmC ||
!ranges_[i].eq.flags.mmG ||
!ranges_[i].eq.flags.mmT);
assert_lt(ranges_[i].eq.flags.quallo, 127);
}
#endif
return ranges_[i].eliminated_;
}
return true;
}
/**
* Split off a new branch by selecting a good outgoing path and
* creating a new Branch object for it and inserting that branch
* into the priority queue. Mark that outgoing path from the
* parent branch as eliminated. If the second-best outgoing path
* cost more, add the difference to the cost of this branch (since
* that's the best we can do starting from here from now on).
*/
Branch* splitBranch(AllocOnlyPool<RangeState>& rpool,
AllocOnlyPool<Edit>& epool,
AllocOnlyPool<Branch>& bpool,
uint32_t pmSz,
RandomSource& rand, uint32_t qlen,
uint32_t qualLim, int seedLen,
bool qualOrder, const EbwtParams& ep,
const uint8_t* ebwt, bool ebwtFw,
bool verbose,
bool quiet)
{
assert(!exhausted_);
assert(ranges_ != NULL);
assert(curtailed_);
assert_gt(pmSz, 0);
Branch *newBranch = bpool.alloc();
if(newBranch == NULL) {
return NULL;
}
assert(newBranch != NULL);
uint32_t id = bpool.lastId();
int tiedPositions[3];
int numTiedPositions = 0;
// Lowest marginal cost incurred by any of the positions with
// non-eliminated outgoing edges
uint16_t bestCost = 0xffff;
// Next-lowest
uint16_t nextCost = 0xffff;
int numNotEliminated = 0;
int i = (int)depth0_;
i = max(0, i - rdepth_);
// Iterate over revisitable positions in the path
for(; i <= len_; i++) {
// If there are still valid options for leaving out of this
// position
if(!eliminated(i)) {
numNotEliminated++;
uint16_t stratum = (rdepth_ + i < seedLen) ? (1 << 14) : 0;
uint16_t cost = stratum;
cost |= (qualOrder ? ranges_[i].eq.flags.quallo : 0);
if(cost < bestCost) {
// Demote the old best to the next-best
nextCost = bestCost;
// Update the new best
bestCost = cost;
numTiedPositions = 1;
tiedPositions[0] = i;
} else if(cost == bestCost) {
// As good as the best so far
assert_gt(numTiedPositions, 0);
if(numTiedPositions < 3) {
tiedPositions[numTiedPositions++] = i;
} else {
tiedPositions[0] = tiedPositions[1];
tiedPositions[1] = tiedPositions[2];
tiedPositions[2] = i;
}
} else if(cost < nextCost) {
// 'cost' isn't beter than the best, but it is
// better than the next-best
nextCost = cost;
}
}
}
assert_gt(numNotEliminated, 0);
assert_gt(numTiedPositions, 0);
//if(nextCost != 0xffff) assert_gt(nextCost, bestCost);
int r = 0;
if(numTiedPositions > 1) {
r = rand.nextU32() % numTiedPositions;
}
int pos = tiedPositions[r];
bool last = false;
// Pick an edit from among the edits tied for lowest cost
// (using randomness to break ties). If the selected edit is
// the last remaining one at this position, 'last' is set to
// true.
TIndexOffU top = 0, bot = 0;
Edit e = ranges_[pos].pickEdit(pos + rdepth_, rand, top, bot, false, last);
assert_gt(bot, top);
// Create and initialize a new Branch
uint16_t newRdepth = rdepth_ + pos + 1;
assert_lt((bestCost >> 14), 4);
uint32_t hamadd = (bestCost & ~0xc000);
uint16_t depth = pos + rdepth_;
assert_geq(depth, depth0_);
uint16_t newDepth0 = depth0_;
uint16_t newDepth1 = depth1_;
uint16_t newDepth2 = depth2_;
uint16_t newDepth3 = depth3_;
if(depth < depth1_) newDepth0 = depth1_;
if(depth < depth2_) newDepth1 = depth2_;
if(depth < depth3_) newDepth2 = depth3_;
assert_eq((uint32_t)(cost_ & ~0xc000), (uint32_t)(ham_ + hamadd));
if(!newBranch->init(
rpool, epool, id, qlen,
newDepth0, newDepth1, newDepth2, newDepth3,
newRdepth, 0, cost_, ham_ + hamadd,
top, bot, ep, ebwt, &edits_))
{
return NULL;
}
// Add the new edit
newBranch->edits_.add(e, epool, qlen);
if(numNotEliminated == 1 && last) {
// This branch is totally exhausted; there are no more
// valid outgoing paths from any positions within it.
// Remove it from the PathManager and mark it as exhausted.
// The caller should delete it.
exhausted_ = true;
if(ranges_ != NULL) {
assert_gt(rangesSz_, 0);
if(rpool.free(ranges_, rangesSz_)) {
ranges_ = NULL;
rangesSz_ = 0;
}
}
}
else if(numTiedPositions == 1 && last) {
// We exhausted the last outgoing edge at the current best
// cost; update the best cost to be the next-best
assert_neq(0xffff, nextCost);
if(bestCost != nextCost) {
assert_gt(nextCost, bestCost);
delayedCost_ = (cost_ - bestCost + nextCost);
delayedIncrease_ = true;
}
}
return newBranch;
}
/**
* Free a branch and all its contents.
*/
void free(uint32_t qlen,
AllocOnlyPool<RangeState>& rpool,
AllocOnlyPool<Edit>& epool,
AllocOnlyPool<Branch>& bpool)
{
edits_.free(epool, qlen);
if(ranges_ != NULL) {
assert_gt(rangesSz_, 0);
rpool.free(ranges_, rangesSz_);
ranges_ = NULL;
rangesSz_ = 0;
}
bpool.free(this);
}
/**
* Pretty-print the state of this branch.
*/
void print(const BTDnaString& qry,
const BTString& quals,
uint16_t minCost,
std::ostream& out,
bool halfAndHalf,
bool seeded,
bool fw,
bool ebwtFw)
{
size_t editidx = 0;
size_t printed = 0;
const size_t qlen = qry.length();
if(exhausted_) out << "E ";
else if(curtailed_) out << "C ";
else out << " ";
if(ebwtFw) out << "<";
else out << ">";
if(fw) out << "F ";
else out << "R ";
std::stringstream ss;
ss << cost_;
string s = ss.str();
if(s.length() < 6) {
for(size_t i = 0; i < 6 - s.length(); i++) {
out << "0";
}
}
out << s << " ";
std::stringstream ss2;
ss2 << minCost;
s = ss2.str();
if(s.length() < 6) {
for(size_t i = 0; i < 6 - s.length(); i++) {
out << "0";
}
}
out << s;
if(halfAndHalf) out << " h ";
else if(seeded) out << " s ";
else out << " ";
std::stringstream ss3;
const size_t numEdits = edits_.size();
if(rdepth_ > 0) {
for(size_t i = 0; i < rdepth_; i++) {
if(editidx < numEdits && edits_.get(editidx).pos == i) {
ss3 << " " << (char)tolower(edits_.get(editidx).chr);
editidx++;
} else {
ss3 << " " << (char)qry.toChar(qlen - i - 1);
}
printed++;
}
ss3 << "|";
} else {
ss3 << " ";
}
for(size_t i = 0; i < len_; i++) {
if(editidx < numEdits && edits_.get(editidx).pos == printed) {
ss3 << (char)tolower(edits_.get(editidx).chr) << " ";
editidx++;
} else {
ss3 << (char)qry.toChar(qlen - printed - 1) << " ";
}
printed++;
}
assert_eq(editidx, edits_.size());
for(size_t i = printed; i < qlen; i++) {
ss3 << "= ";
}
s = ss3.str();
if(ebwtFw) {
std::reverse(s.begin(), s.end());
}
out << s << endl;
}
/**
* Called when the most recent branch extension resulted in an
* empty range or some other constraint violation (e.g., a
* half-and-half constraint).
*/
void curtail(AllocOnlyPool<RangeState>& rpool, int seedLen, bool qualOrder) {
assert(!curtailed_);
assert(!exhausted_);
if(ranges_ == NULL) {
exhausted_ = true;
curtailed_ = true;
return;
}
uint16_t lowestCost = 0xffff;
// Iterate over positions in the path looking for the cost of
// the lowest-cost non-eliminated position
uint32_t eliminatedStretch = 0;
int i = (int)depth0_;
i = max(0, i - rdepth_);
// TODO: It matters whether an insertion/deletion at given
// position would be a gap open or a gap extension
for(; i <= len_; i++) {
if(!eliminated(i)) {
eliminatedStretch = 0;
uint16_t stratum = (rdepth_ + i < seedLen) ? (1 << 14) : 0;
uint16_t cost = (qualOrder ? /*TODO*/ ranges_[i].eq.flags.quallo : 0) | stratum;
if(cost < lowestCost) lowestCost = cost;
} else if(i < rangesSz_) {
eliminatedStretch++;
}
}
if(lowestCost > 0 && lowestCost != 0xffff) {
// This branch's cost will change when curtailed; the
// caller should re-insert it into the priority queue so
// that the new cost takes effect.
cost_ += lowestCost;
} else if(lowestCost == 0xffff) {
// This branch is totally exhausted; there are no more
// valid outgoing paths from any positions within it.
// Remove it from the PathManager and mark it as exhausted.
// The caller should delete it.
exhausted_ = true;
if(ranges_ != NULL) {
assert_gt(rangesSz_, 0);
if(rpool.free(ranges_, rangesSz_)) {
ranges_ = NULL;
rangesSz_ = 0;
}
}
} else {
// Just mark it as curtailed and keep the same cost
}
if(ranges_ != NULL) {
// Try to trim off no-longer-relevant elements of the
// ranges_ array
assert(!exhausted_);
assert_gt(rangesSz_, 0);
uint32_t trim = (rangesSz_ - len_ - 1) + eliminatedStretch;
assert_leq(trim, rangesSz_);
if(rpool.free(ranges_ + rangesSz_ - trim, trim)) {
rangesSz_ -= trim;
if(rangesSz_ == 0) {
ranges_ = NULL;
}
}
}
curtailed_ = true;
}
/**
* Prep this branch for the next extension by calculating the
* SideLocus information and prefetching cache lines from the
* appropriate loci.
*/
void prep(const EbwtParams& ep, const uint8_t* ebwt) {
if(bot_ > top_+1) {
SideLocus::initFromTopBot(top_, bot_, ep, ebwt, ltop_, lbot_);
} else if(bot_ > top_) {
ltop_.initFromRow(top_, ep, ebwt);
lbot_.invalidate();
}
prepped_ = true;
}
/**
* Get the furthest-out RangeState.
*/
RangeState* rangeState() {
assert(!exhausted_);
assert(ranges_ != NULL);
assert_lt(len_, rangesSz_);
return &ranges_[len_];
}
/**
* Set the elims to match the ranges in ranges_[len_], already
* calculated by the caller. Only does mismatches for now.
*/
int installRanges(int c, int nextc, uint32_t qAllow, const uint8_t* qs) {
assert(!exhausted_);
assert(ranges_ != NULL);
RangeState& r = ranges_[len_];
int ret = 0;
r.eliminated_ = true; // start with everything eliminated
r.eq.eliminate(); // set all elim flags to 1
assert_lt(qs[0], 127);
assert_lt(qs[1], 127);
assert_lt(qs[2], 127);
assert_lt(qs[3], 127);
assert_eq(qs[0], qs[1]);
assert_eq(qs[0], qs[2]);
assert_eq(qs[0], qs[3]);
r.eq.flags.quallo = qs[0];
if(qs[0] > qAllow) return 0;
// Set one/both of these to true to do the accounting for
// insertions and deletions as well as mismatches
bool doInserts = false;
bool doDeletes = false;
// We can proceed on an A
if(c != 0 && r.bots[0] > r.tops[0] && qs[0] <= qAllow) {
r.eliminated_ = false;
r.eq.flags.mmA = 0; // A substitution is an option
if(doInserts) r.eq.flags.insA = 0;
if(doDeletes && nextc == 0) r.eq.flags.del = 0;
ret++;
}
// We can proceed on a C
if(c != 1 && r.bots[1] > r.tops[1] && qs[1] <= qAllow) {
r.eliminated_ = false;