forked from DaehwanKimLab/hisat2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aligner_seed.h
2922 lines (2752 loc) · 84 KB
/
aligner_seed.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_SEED_H_
#define ALIGNER_SEED_H_
#include <iostream>
#include <utility>
#include <limits>
#include "qual.h"
#include "ds.h"
#include "sstring.h"
#include "alphabet.h"
#include "edit.h"
#include "read.h"
// Threading is necessary to synchronize the classes that dump
// intermediate alignment results to files. Otherwise, all data herein
// is constant and shared, or per-thread.
#include "threading.h"
#include "aligner_result.h"
#include "aligner_cache.h"
#include "scoring.h"
#include "mem_ids.h"
#include "simple_func.h"
#include "btypes.h"
/**
* A constraint to apply to an alignment zone, or to an overall
* alignment.
*
* The constraint can put both caps and ceilings on the number and
* types of edits allowed.
*/
struct Constraint {
Constraint() { init(); }
/**
* Initialize Constraint to be fully permissive.
*/
void init() {
edits = mms = ins = dels = penalty = editsCeil = mmsCeil =
insCeil = delsCeil = penaltyCeil = MAX_I;
penFunc.reset();
instantiated = false;
}
/**
* Return true iff penalities and constraints prevent us from
* adding any edits.
*/
bool mustMatch() {
assert(instantiated);
return (mms == 0 && edits == 0) ||
penalty == 0 ||
(mms == 0 && dels == 0 && ins == 0);
}
/**
* Return true iff a mismatch of the given quality is permitted.
*/
bool canMismatch(int q, const Scoring& cm) {
assert(instantiated);
return (mms > 0 || edits > 0) &&
penalty >= cm.mm(q);
}
/**
* Return true iff a mismatch of the given quality is permitted.
*/
bool canN(int q, const Scoring& cm) {
assert(instantiated);
return (mms > 0 || edits > 0) &&
penalty >= cm.n(q);
}
/**
* Return true iff a mismatch of *any* quality (even qual=1) is
* permitted.
*/
bool canMismatch() {
assert(instantiated);
return (mms > 0 || edits > 0) && penalty > 0;
}
/**
* Return true iff a mismatch of *any* quality (even qual=1) is
* permitted.
*/
bool canN() {
assert(instantiated);
return (mms > 0 || edits > 0);
}
/**
* Return true iff a deletion of the given extension (0=open, 1=1st
* extension, etc) is permitted.
*/
bool canDelete(int ex, const Scoring& cm) {
assert(instantiated);
return (dels > 0 && edits > 0) &&
penalty >= cm.del(ex);
}
/**
* Return true iff a deletion of any extension is permitted.
*/
bool canDelete() {
assert(instantiated);
return (dels > 0 || edits > 0) &&
penalty > 0;
}
/**
* Return true iff an insertion of the given extension (0=open,
* 1=1st extension, etc) is permitted.
*/
bool canInsert(int ex, const Scoring& cm) {
assert(instantiated);
return (ins > 0 || edits > 0) &&
penalty >= cm.ins(ex);
}
/**
* Return true iff an insertion of any extension is permitted.
*/
bool canInsert() {
assert(instantiated);
return (ins > 0 || edits > 0) &&
penalty > 0;
}
/**
* Return true iff a gap of any extension is permitted
*/
bool canGap() {
assert(instantiated);
return ((ins > 0 || dels > 0) || edits > 0) && penalty > 0;
}
/**
* Charge a mismatch of the given quality.
*/
void chargeMismatch(int q, const Scoring& cm) {
assert(instantiated);
if(mms == 0) { assert_gt(edits, 0); edits--; }
else mms--;
penalty -= cm.mm(q);
assert_geq(mms, 0);
assert_geq(edits, 0);
assert_geq(penalty, 0);
}
/**
* Charge an N mismatch of the given quality.
*/
void chargeN(int q, const Scoring& cm) {
assert(instantiated);
if(mms == 0) { assert_gt(edits, 0); edits--; }
else mms--;
penalty -= cm.n(q);
assert_geq(mms, 0);
assert_geq(edits, 0);
assert_geq(penalty, 0);
}
/**
* Charge a deletion of the given extension.
*/
void chargeDelete(int ex, const Scoring& cm) {
assert(instantiated);
dels--;
edits--;
penalty -= cm.del(ex);
assert_geq(dels, 0);
assert_geq(edits, 0);
assert_geq(penalty, 0);
}
/**
* Charge an insertion of the given extension.
*/
void chargeInsert(int ex, const Scoring& cm) {
assert(instantiated);
ins--;
edits--;
penalty -= cm.ins(ex);
assert_geq(ins, 0);
assert_geq(edits, 0);
assert_geq(penalty, 0);
}
/**
* Once the constrained area is completely explored, call this
* function to check whether there were *at least* as many
* dissimilarities as required by the constraint. Bounds like this
* are helpful to resolve instances where two search roots would
* otherwise overlap in what alignments they can find.
*/
bool acceptable() {
assert(instantiated);
return edits <= editsCeil &&
mms <= mmsCeil &&
ins <= insCeil &&
dels <= delsCeil &&
penalty <= penaltyCeil;
}
/**
* Instantiate a constraint w/r/t the read length and the constant
* and linear coefficients for the penalty function.
*/
static int instantiate(size_t rdlen, const SimpleFunc& func) {
return func.f<int>((double)rdlen);
}
/**
* Instantiate this constraint w/r/t the read length.
*/
void instantiate(size_t rdlen) {
assert(!instantiated);
if(penFunc.initialized()) {
penalty = Constraint::instantiate(rdlen, penFunc);
}
instantiated = true;
}
int edits; // # edits permitted
int mms; // # mismatches permitted
int ins; // # insertions permitted
int dels; // # deletions permitted
int penalty; // penalty total permitted
int editsCeil; // <= this many edits can be left at the end
int mmsCeil; // <= this many mismatches can be left at the end
int insCeil; // <= this many inserts can be left at the end
int delsCeil; // <= this many deletions can be left at the end
int penaltyCeil;// <= this much leftover penalty can be left at the end
SimpleFunc penFunc;// penalty function; function of read len
bool instantiated; // whether constraint is instantiated w/r/t read len
//
// Some static methods for constructing some standard Constraints
//
/**
* Construct a constraint with no edits of any kind allowed.
*/
static Constraint exact();
/**
* Construct a constraint where the only constraint is a total
* penalty constraint.
*/
static Constraint penaltyBased(int pen);
/**
* Construct a constraint where the only constraint is a total
* penalty constraint related to the length of the read.
*/
static Constraint penaltyFuncBased(const SimpleFunc& func);
/**
* Construct a constraint where the only constraint is a total
* penalty constraint.
*/
static Constraint mmBased(int mms);
/**
* Construct a constraint where the only constraint is a total
* penalty constraint.
*/
static Constraint editBased(int edits);
};
/**
* We divide seed search strategies into three categories:
*
* 1. A left-to-right search where the left half of the read is
* constrained to match exactly and the right half is subject to
* some looser constraint (e.g. 1mm or 2mm)
* 2. Same as 1, but going right to left with the exact matching half
* on the right.
* 3. Inside-out search where the center half of the read is
* constrained to match exactly, and the extreme quarters of the
* read are subject to a looser constraint.
*/
enum {
SEED_TYPE_EXACT = 1,
SEED_TYPE_LEFT_TO_RIGHT,
SEED_TYPE_RIGHT_TO_LEFT,
SEED_TYPE_INSIDE_OUT
};
struct InstantiatedSeed;
/**
* Policy dictating how to size and arrange seeds along the length of
* the read, and what constraints to force on the zones of the seed.
* We assume that seeds are plopped down at regular intervals from the
* 5' to 3' ends, with the first seed flush to the 5' end.
*
* If the read is shorter than a single seed, one seed is used and it
* is shrunk to accommodate the read.
*/
struct Seed {
int len; // length of a seed
int type; // dictates anchor portion, direction of search
Constraint *overall; // for the overall alignment
Seed() { init(0, 0, NULL); }
/**
* Construct and initialize this seed with given length and type.
*/
Seed(int ln, int ty, Constraint* oc) {
init(ln, ty, oc);
}
/**
* Initialize this seed with given length and type.
*/
void init(int ln, int ty, Constraint* oc) {
len = ln;
type = ty;
overall = oc;
}
// If the seed is split into halves, we just use zones[0] and
// zones[1]; 0 is the near half and 1 is the far half. If the seed
// is split into thirds (i.e. inside-out) then 0 is the center, 1
// is the far portion on the left, and 2 is the far portion on the
// right.
Constraint zones[3];
/**
* Once the constrained seed is completely explored, call this
* function to check whether there were *at least* as many
* dissimilarities as required by all constraints. Bounds like this
* are helpful to resolve instances where two search roots would
* otherwise overlap in what alignments they can find.
*/
bool acceptable() {
assert(overall != NULL);
return zones[0].acceptable() &&
zones[1].acceptable() &&
zones[2].acceptable() &&
overall->acceptable();
}
/**
* Given a read, depth and orientation, extract a seed data structure
* from the read and fill in the steps & zones arrays. The Seed
* contains the sequence and quality values.
*/
bool instantiate(
const Read& read,
const BTDnaString& seq, // already-extracted seed sequence
const BTString& qual, // already-extracted seed quality sequence
const Scoring& pens,
int depth,
int seedoffidx,
int seedtypeidx,
bool fw,
InstantiatedSeed& si) const;
/**
* Return a list of Seed objects encapsulating
*/
static void mmSeeds(
int mms,
int ln,
EList<Seed>& pols,
Constraint& oall)
{
if(mms == 0) {
zeroMmSeeds(ln, pols, oall);
} else if(mms == 1) {
oneMmSeeds(ln, pols, oall);
} else if(mms == 2) {
twoMmSeeds(ln, pols, oall);
} else throw 1;
}
static void zeroMmSeeds(int ln, EList<Seed>&, Constraint&);
static void oneMmSeeds (int ln, EList<Seed>&, Constraint&);
static void twoMmSeeds (int ln, EList<Seed>&, Constraint&);
};
/**
* An instantiated seed is a seed (perhaps modified to fit the read)
* plus all data needed to conduct a search of the seed.
*/
struct InstantiatedSeed {
InstantiatedSeed() : steps(AL_CAT), zones(AL_CAT) { }
// Steps map. There are as many steps as there are positions in
// the seed. The map is a helpful abstraction because we sometimes
// visit seed positions in an irregular order (e.g. inside-out
// search).
EList<int> steps;
// Zones map. For each step, records what constraint to charge an
// edit to. The first entry in each pair gives the constraint for
// non-insert edits and the second entry in each pair gives the
// constraint for insert edits. If the value stored is negative,
// this indicates that the zone is "closed out" after this
// position, so zone acceptility should be checked.
EList<pair<int, int> > zones;
// Nucleotide sequence covering the seed, extracted from read
BTDnaString *seq;
// Quality sequence covering the seed, extracted from read
BTString *qual;
// Initial constraints governing zones 0, 1, 2. We precalculate
// the effect of Ns on these.
Constraint cons[3];
// Overall constraint, tailored to the read length.
Constraint overall;
// Maximum number of positions that the aligner may advance before
// its first step. This lets the aligner know whether it can use
// the ftab or not.
int maxjump;
// Offset of seed from 5' end of read
int seedoff;
// Id for seed offset; ids are such that the smallest index is the
// closest to the 5' end and consecutive ids are adjacent (i.e.
// there are no intervening offsets with seeds)
int seedoffidx;
// Type of seed (left-to-right, etc)
int seedtypeidx;
// Seed comes from forward-oriented read?
bool fw;
// Filtered out due to the pattern of Ns present. If true, this
// seed should be ignored by searchAllSeeds().
bool nfiltered;
// Seed this was instantiated from
Seed s;
#ifndef NDEBUG
/**
* Check that InstantiatedSeed is internally consistent.
*/
bool repOk() const {
return true;
}
#endif
};
/**
* Simple struct for holding a end-to-end alignments for the read with at most
* 2 edits.
*/
template <typename index_t>
struct EEHit {
EEHit() { reset(); }
void reset() {
top = bot = 0;
fw = false;
e1.reset();
e2.reset();
score = MIN_I64;
}
void init(
index_t top_,
index_t bot_,
const Edit* e1_,
const Edit* e2_,
bool fw_,
int64_t score_)
{
top = top_; bot = bot_;
if(e1_ != NULL) {
e1 = *e1_;
} else {
e1.reset();
}
if(e2_ != NULL) {
e2 = *e2_;
} else {
e2.reset();
}
fw = fw_;
score = score_;
}
/**
* Return number of mismatches in the alignment.
*/
int mms() const {
if (e2.inited()) return 2;
else if(e1.inited()) return 1;
else return 0;
}
/**
* Return the number of Ns involved in the alignment.
*/
int ns() const {
int ns = 0;
if(e1.inited() && e1.hasN()) {
ns++;
if(e2.inited() && e2.hasN()) {
ns++;
}
}
return ns;
}
/**
* Return the number of Ns involved in the alignment.
*/
int refns() const {
int ns = 0;
if(e1.inited() && e1.chr == 'N') {
ns++;
if(e2.inited() && e2.chr == 'N') {
ns++;
}
}
return ns;
}
/**
* Return true iff there is no hit.
*/
bool empty() const {
return bot <= top;
}
/**
* Higher score = higher priority.
*/
bool operator<(const EEHit& o) const {
return score > o.score;
}
/**
* Return the size of the alignments SA range.s
*/
index_t size() const { return bot - top; }
#ifndef NDEBUG
/**
* Check that hit is sane w/r/t read.
*/
bool repOk(const Read& rd) const {
assert_gt(bot, top);
if(e1.inited()) {
assert_lt(e1.pos, rd.length());
if(e2.inited()) {
assert_lt(e2.pos, rd.length());
}
}
return true;
}
#endif
index_t top;
index_t bot;
Edit e1;
Edit e2;
bool fw;
int64_t score;
};
/**
* Data structure for holding all of the seed hits associated with a read. All
* the seed hits for a given read are encapsulated in a single QVal object. A
* QVal refers to a range of values in the qlist, where each qlist value is a
* BW range and a slot to hold the hit's suffix array offset. QVals are kept
* in two lists (hitsFw_ and hitsRc_), one for seeds on the forward read strand,
* one for seeds on the reverse read strand. The list is indexed by read
* offset index (e.g. 0=closest-to-5', 1=second-closest, etc).
*
* An assumption behind this data structure is that all the seeds are found
* first, then downstream analyses try to extend them. In between finding the
* seed hits and extending them, the sort() member function is called, which
* ranks QVals according to the order they should be extended. Right now the
* policy is that QVals with fewer elements (hits) should be tried first.
*/
template <typename index_t>
class SeedResults {
public:
SeedResults() :
seqFw_(AL_CAT),
seqRc_(AL_CAT),
qualFw_(AL_CAT),
qualRc_(AL_CAT),
hitsFw_(AL_CAT),
hitsRc_(AL_CAT),
isFw_(AL_CAT),
isRc_(AL_CAT),
sortedFw_(AL_CAT),
sortedRc_(AL_CAT),
offIdx2off_(AL_CAT),
rankOffs_(AL_CAT),
rankFws_(AL_CAT),
mm1Hit_(AL_CAT)
{
clear();
}
/**
* Set the current read.
*/
void nextRead(const Read& read) {
read_ = &read;
}
/**
* Set the appropriate element of either hitsFw_ or hitsRc_ to the given
* QVal. A QVal encapsulates all the BW ranges for reference substrings
* that are within some distance of the seed string.
*/
void add(
const QVal<index_t>& qv, // range of ranges in cache
const AlignmentCache<index_t>& ac, // cache
index_t seedIdx, // seed index (from 5' end)
bool seedFw) // whether seed is from forward read
{
assert(qv.repOk(ac));
assert(repOk(&ac));
assert_lt(seedIdx, hitsFw_.size());
assert_gt(numOffs_, 0); // if this fails, probably failed to call reset
if(qv.empty()) return;
if(seedFw) {
assert(!hitsFw_[seedIdx].valid());
hitsFw_[seedIdx] = qv;
numEltsFw_ += qv.numElts();
numRangesFw_ += qv.numRanges();
if(qv.numRanges() > 0) nonzFw_++;
} else {
assert(!hitsRc_[seedIdx].valid());
hitsRc_[seedIdx] = qv;
numEltsRc_ += qv.numElts();
numRangesRc_ += qv.numRanges();
if(qv.numRanges() > 0) nonzRc_++;
}
numElts_ += qv.numElts();
numRanges_ += qv.numRanges();
if(qv.numRanges() > 0) {
nonzTot_++;
}
assert(repOk(&ac));
}
/**
* Clear buffered seed hits and state. Set the number of seed
* offsets and the read.
*/
void reset(
const Read& read,
const EList<index_t>& offIdx2off,
size_t numOffs)
{
assert_gt(numOffs, 0);
clearSeeds();
numOffs_ = numOffs;
seqFw_.resize(numOffs_);
seqRc_.resize(numOffs_);
qualFw_.resize(numOffs_);
qualRc_.resize(numOffs_);
hitsFw_.resize(numOffs_);
hitsRc_.resize(numOffs_);
isFw_.resize(numOffs_);
isRc_.resize(numOffs_);
sortedFw_.resize(numOffs_);
sortedRc_.resize(numOffs_);
offIdx2off_ = offIdx2off;
for(size_t i = 0; i < numOffs_; i++) {
sortedFw_[i] = sortedRc_[i] = false;
hitsFw_[i].reset();
hitsRc_[i].reset();
isFw_[i].clear();
isRc_[i].clear();
}
read_ = &read;
sorted_ = false;
}
/**
* Clear seed-hit state.
*/
void clearSeeds() {
sortedFw_.clear();
sortedRc_.clear();
rankOffs_.clear();
rankFws_.clear();
offIdx2off_.clear();
hitsFw_.clear();
hitsRc_.clear();
isFw_.clear();
isRc_.clear();
seqFw_.clear();
seqRc_.clear();
nonzTot_ = 0;
nonzFw_ = 0;
nonzRc_ = 0;
numOffs_ = 0;
numRanges_ = 0;
numElts_ = 0;
numRangesFw_ = 0;
numEltsFw_ = 0;
numRangesRc_ = 0;
numEltsRc_ = 0;
}
/**
* Clear seed-hit state and end-to-end alignment state.
*/
void clear() {
clearSeeds();
read_ = NULL;
exactFwHit_.reset();
exactRcHit_.reset();
mm1Hit_.clear();
mm1Sorted_ = false;
mm1Elt_ = 0;
assert(empty());
}
/**
* Extract key summaries from this SeedResults and put into 'ssum'.
*/
void toSeedAlSumm(SeedAlSumm& ssum) const {
// Number of positions with at least 1 range
ssum.nonzTot = nonzTot_;
ssum.nonzFw = nonzFw_;
ssum.nonzRc = nonzRc_;
// Number of ranges
ssum.nrangeTot = numRanges_;
ssum.nrangeFw = numRangesFw_;
ssum.nrangeRc = numRangesRc_;
// Number of elements
ssum.neltTot = numElts_;
ssum.neltFw = numEltsFw_;
ssum.neltRc = numEltsRc_;
// Other summaries
ssum.maxNonzRangeFw = ssum.minNonzRangeFw = 0;
ssum.maxNonzRangeRc = ssum.minNonzRangeRc = 0;
ssum.maxNonzEltFw = ssum.minNonzEltFw = 0;
ssum.maxNonzEltRc = ssum.minNonzEltRc = 0;
for(size_t i = 0; i < numOffs_; i++) {
if(hitsFw_[i].valid()) {
if(ssum.minNonzEltFw == 0 || hitsFw_[i].numElts() < ssum.minNonzEltFw) {
ssum.minNonzEltFw = hitsFw_[i].numElts();
}
if(ssum.maxNonzEltFw == 0 || hitsFw_[i].numElts() > ssum.maxNonzEltFw) {
ssum.maxNonzEltFw = hitsFw_[i].numElts();
}
if(ssum.minNonzRangeFw == 0 || hitsFw_[i].numRanges() < ssum.minNonzRangeFw) {
ssum.minNonzRangeFw = hitsFw_[i].numRanges();
}
if(ssum.maxNonzRangeFw == 0 || hitsFw_[i].numRanges() > ssum.maxNonzRangeFw) {
ssum.maxNonzRangeFw = hitsFw_[i].numRanges();
}
}
if(hitsRc_[i].valid()) {
if(ssum.minNonzEltRc == 0 || hitsRc_[i].numElts() < ssum.minNonzEltRc) {
ssum.minNonzEltRc = hitsRc_[i].numElts();
}
if(ssum.maxNonzEltRc == 0 || hitsRc_[i].numElts() > ssum.maxNonzEltRc) {
ssum.maxNonzEltRc = hitsRc_[i].numElts();
}
if(ssum.minNonzRangeRc == 0 || hitsRc_[i].numRanges() < ssum.minNonzRangeRc) {
ssum.minNonzRangeRc = hitsRc_[i].numRanges();
}
if(ssum.maxNonzRangeRc == 0 || hitsRc_[i].numRanges() > ssum.maxNonzRangeRc) {
ssum.maxNonzRangeRc = hitsRc_[i].numRanges();
}
}
}
}
/**
* Return average number of hits per seed.
*/
float averageHitsPerSeed() const {
return (float)numElts_ / (float)nonzTot_;
}
/**
* Return median of all the non-zero per-seed # hits
*/
float medianHitsPerSeed() const {
EList<size_t>& median = const_cast<EList<size_t>&>(tmpMedian_);
median.clear();
for(size_t i = 0; i < numOffs_; i++) {
if(hitsFw_[i].valid() && hitsFw_[i].numElts() > 0) {
median.push_back(hitsFw_[i].numElts());
}
if(hitsRc_[i].valid() && hitsRc_[i].numElts() > 0) {
median.push_back(hitsRc_[i].numElts());
}
}
if(tmpMedian_.empty()) {
return 0.0f;
}
median.sort();
float med1 = (float)median[tmpMedian_.size() >> 1];
float med2 = med1;
if((median.size() & 1) == 0) {
med2 = (float)median[(tmpMedian_.size() >> 1) - 1];
}
return med1 + med2 * 0.5f;
}
/**
* Return a number that's meant to quantify how hopeful we are that this
* set of seed hits will lead to good alignments.
*/
double uniquenessFactor() const {
double result = 0.0;
for(size_t i = 0; i < numOffs_; i++) {
if(hitsFw_[i].valid()) {
size_t nelt = hitsFw_[i].numElts();
result += (1.0 / (double)(nelt * nelt));
}
if(hitsRc_[i].valid()) {
size_t nelt = hitsRc_[i].numElts();
result += (1.0 / (double)(nelt * nelt));
}
}
return result;
}
/**
* Return the number of ranges being held.
*/
index_t numRanges() const { return numRanges_; }
/**
* Return the number of elements being held.
*/
index_t numElts() const { return numElts_; }
/**
* Return the number of ranges being held for seeds on the forward
* read strand.
*/
index_t numRangesFw() const { return numRangesFw_; }
/**
* Return the number of elements being held for seeds on the
* forward read strand.
*/
index_t numEltsFw() const { return numEltsFw_; }
/**
* Return the number of ranges being held for seeds on the
* reverse-complement read strand.
*/
index_t numRangesRc() const { return numRangesRc_; }
/**
* Return the number of elements being held for seeds on the
* reverse-complement read strand.
*/
index_t numEltsRc() const { return numEltsRc_; }
/**
* Given an offset index, return the offset that has that index.
*/
index_t idx2off(size_t off) const {
return offIdx2off_[off];
}
/**
* Return true iff there are 0 hits being held.
*/
bool empty() const { return numRanges() == 0; }
/**
* Get the QVal representing all the reference hits for the given
* orientation and seed offset index.
*/
const QVal<index_t>& hitsAtOffIdx(bool fw, size_t seedoffidx) const {
assert_lt(seedoffidx, numOffs_);
assert(repOk(NULL));
return fw ? hitsFw_[seedoffidx] : hitsRc_[seedoffidx];
}
/**
* Get the Instantiated seeds for the given orientation and offset.
*/
EList<InstantiatedSeed>& instantiatedSeeds(bool fw, size_t seedoffidx) {
assert_lt(seedoffidx, numOffs_);
assert(repOk(NULL));
return fw ? isFw_[seedoffidx] : isRc_[seedoffidx];
}
/**
* Return the number of different seed offsets possible.
*/
index_t numOffs() const { return numOffs_; }
/**
* Return the read from which seeds were extracted, aligned.
*/
const Read& read() const { return *read_; }
#ifndef NDEBUG
/**
* Check that this SeedResults is internally consistent.
*/
bool repOk(
const AlignmentCache<index_t>* ac,
bool requireInited = false) const
{
if(requireInited) {
assert(read_ != NULL);
}
if(numOffs_ > 0) {
assert_eq(numOffs_, hitsFw_.size());
assert_eq(numOffs_, hitsRc_.size());
assert_leq(numRanges_, numElts_);
assert_leq(nonzTot_, numRanges_);
size_t nonzs = 0;
for(int fw = 0; fw <= 1; fw++) {
const EList<QVal<index_t> >& rrs = (fw ? hitsFw_ : hitsRc_);
for(size_t i = 0; i < numOffs_; i++) {
if(rrs[i].valid()) {
if(rrs[i].numRanges() > 0) nonzs++;
if(ac != NULL) {
assert(rrs[i].repOk(*ac));
}
}
}
}
assert_eq(nonzs, nonzTot_);
assert(!sorted_ || nonzTot_ == rankFws_.size());
assert(!sorted_ || nonzTot_ == rankOffs_.size());
}
return true;
}
#endif
/**
* Populate rankOffs_ and rankFws_ with the list of QVals that need to be
* examined for this SeedResults, in order. The order is ascending by
* number of elements, so QVals with fewer elements (i.e. seed sequences
* that are more unique) will be tried first and QVals with more elements
* (i.e. seed sequences
*/
void rankSeedHits(RandomSource& rnd) {
while(rankOffs_.size() < nonzTot_) {
index_t minsz = (index_t)0xffffffff;
index_t minidx = 0;
bool minfw = true;
// Rank seed-hit positions in ascending order by number of elements
// in all BW ranges
bool rb = rnd.nextBool();
assert(rb == 0 || rb == 1);
for(int fwi = 0; fwi <= 1; fwi++) {
bool fw = (fwi == (rb ? 1 : 0));
EList<QVal<index_t> >& rrs = (fw ? hitsFw_ : hitsRc_);
EList<bool>& sorted = (fw ? sortedFw_ : sortedRc_);
index_t i = (rnd.nextU32() % (index_t)numOffs_);
for(index_t ii = 0; ii < numOffs_; ii++) {
if(rrs[i].valid() && // valid QVal
rrs[i].numElts() > 0 && // non-empty
!sorted[i] && // not already sorted
rrs[i].numElts() < minsz) // least elts so far?
{
minsz = rrs[i].numElts();