-
Notifications
You must be signed in to change notification settings - Fork 67
/
BaseAligner.cpp
1566 lines (1341 loc) · 59.3 KB
/
BaseAligner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*++
Module Name:
BaseAligner.cpp
Abstract:
SNAP genome aligner
Authors:
Bill Bolosky, August, 2011
Environment:
User mode service.
This class is NOT thread safe. It's the caller's responsibility to ensure that
at most one thread uses an instance at any time.
Revision History:
Adapted from Matei Zaharia's Scala implementation.
--*/
#include "stdafx.h"
#include "BaseAligner.h"
#include "Compat.h"
#include "LandauVishkin.h"
#include "BigAlloc.h"
#include "mapq.h"
using std::min;
BaseAligner::BaseAligner(
GenomeIndex *i_genomeIndex,
unsigned i_confDiff,
unsigned i_maxHitsToConsider,
unsigned i_maxK,
unsigned i_maxReadSize,
unsigned i_maxSeedsToUse,
unsigned i_lvCutoff,
unsigned i_adaptiveConfDiffThreshold,
LandauVishkin *i_landauVishkin) :
genomeIndex(i_genomeIndex), confDiff(i_confDiff), maxHitsToConsider(i_maxHitsToConsider), maxK(i_maxK),
maxReadSize(i_maxReadSize), maxSeedsToUse(i_maxSeedsToUse), lvCutoff(i_lvCutoff), readId(-1),
adaptiveConfDiffThreshold(i_adaptiveConfDiffThreshold), landauVishkin(i_landauVishkin),
explorePopularSeeds(false), stopOnFirstHit(false)
/*++
Routine Description:
Constructor for the BaseAligner class. Aligners align reads against an indexed genome.
Arguments:
i_genomeIndex - The index against which to do the alignments
i_confDiff - The string difference between two matches necessary to believe they're really different
i_maxHitsToConsider - The maximum number of hits to use from a seed lookup. Any lookups that return more
than this are ignored.
i_maxK - The largest string difference to consider for any comparison.
i_maxReadSize - Bound on the number of bases in any read. There's no reason to make it tight, it just affects a little memory allocation.
i_maxSeedsToUse - The maximum number of seeds to use when aligning any read (not counting ones ignored because they resulted in too many
hits). Once we've looked up this many seeds, we just score what we've got.
--*/
{
#if MAINTAIN_HISTOGRAMS
lvHistogram = new Histogram(20, true);
lookupHistogram = new Histogram(20, true);
lvHistogramForMulti = new Histogram(20, true);
lvCountWhenBestFound = new Histogram(maxHitsToConsider*4,false);
#endif // MAINTAIN_HISTOGRAMS
nHashTableLookups = 0;
nLocationsScored = 0;
nHitsIgnoredBecauseOfTooHighPopularity = 0;
nReadsIgnoredBecauseOfTooManyNs = 0;
nIndelsMerged = 0;
genome = genomeIndex->getGenome();
seedLen = genomeIndex->getSeedLength();
bsd = new BoundedStringDistance<>(3);
if (i_landauVishkin == NULL) {
landauVishkin = new LandauVishkin;
ownLandauVishkin = true;
} else {
ownLandauVishkin = false;
}
unsigned nCandidates = __min(maxHitsToConsider * (maxReadSize - seedLen + 1) * 2, 200000); // *2 is for reverse complement
candidates = (Candidate *)BigAlloc(sizeof(Candidate) * nCandidates);
for (unsigned i = 0 ; i < nCandidates; i++) {
candidates[i].init();
}
rcReadData = (char *)BigAlloc(sizeof(char) * maxReadSize * 2); // The *2 is to allocte space for the quality string
rcReadQuality = rcReadData + maxReadSize;
rcTranslationTable['A'] = 'T';
rcTranslationTable['G'] = 'C';
rcTranslationTable['C'] = 'G';
rcTranslationTable['T'] = 'A';
rcTranslationTable['N'] = 'N';
for (unsigned i = 0; i < 256; i++) {
nTable[i] = 0;
}
nTable['N'] = 1;
seedUsed = (BYTE *)BigAlloc(sizeof(BYTE) * (maxReadSize + 7 + 128) / 8); // +128 to make sure it extends at both before and after at least an _int64
seedUsedAsAllocated = seedUsed; // Save the pointer for the delete.
seedUsed += 8; // This moves the pointer up an _int64, so we now have the appropriate before buffer.
nUsedHashTableElements = 0;
hashTableElementPoolSize = maxHitsToConsider * maxSeedsToUse * 2 ; // *2 for RC
/*
if (maxK*2 > 64) {
fprintf(stderr,"The HashTableElement struct uses a 64 bit bitvector for used candidates. As a result, you can't have maxK > 32. Not that you'd want to anyway.\n");
exit(1);
}
*/
candidateHashTablesSize = (maxHitsToConsider * maxSeedsToUse * 3)/2; // *1.5 for hash table slack
hashTableElementPool = (HashTableElement *)BigAlloc(sizeof(HashTableElement) * hashTableElementPoolSize);
candidateHashTable[0] = (HashTableAnchor *)BigAlloc(sizeof(HashTableAnchor) * candidateHashTablesSize);
candidateHashTable[1] = (HashTableAnchor *)BigAlloc(sizeof(HashTableAnchor) * candidateHashTablesSize);
weightLists = (HashTableElement *)BigAlloc(sizeof(HashTableElement) * (maxSeedsToUse + 1));
for (unsigned i = 0; i < hashTableElementPoolSize; i++) {
hashTableElementPool[i].init();
}
for (unsigned i = 0; i < maxSeedsToUse + 1; i++) {
weightLists[i].init();
}
memset(candidateHashTable[0],0,sizeof(HashTableAnchor) * candidateHashTablesSize);
memset(candidateHashTable[1],0,sizeof(HashTableAnchor) * candidateHashTablesSize);
hashTableEpoch = 0;
}
AlignmentResult
BaseAligner::AlignRead(Read *read, unsigned *genomeLocation, bool *hitIsRC, int *finalScore, int *mapq)
{
return AlignRead(read, genomeLocation, hitIsRC, finalScore, mapq, 0, 0, false, 0, NULL, NULL, NULL, NULL);
}
AlignmentResult
BaseAligner::AlignRead(
Read *read,
unsigned *genomeLocation,
bool *hitIsRC,
int *finalScore,
int *mapq,
unsigned searchRadius,
unsigned searchLocation,
bool searchRC)
{
return AlignRead(read, genomeLocation, hitIsRC, finalScore, mapq,
searchRadius, searchLocation, searchRC, 0, NULL, NULL, NULL, NULL);
}
#ifdef _DEBUG
bool _DumpAlignments = false;
#endif // _DEBUG
AlignmentResult
BaseAligner::AlignRead(
Read *read,
unsigned *genomeLocation,
bool *hitIsRC,
int *finalScore,
int *mapq,
unsigned searchRadius,
unsigned searchLocation,
bool searchRC,
int maxHitsToGet,
int *multiHitsFound,
unsigned *multiHitLocations,
bool *multiHitRCs,
int *multiHitScores)
/*++
Routine Description:
Align a particular read, possibly constraining the search around a given location.
Arguments:
read - the read to align
genomeLocation - if this aligned to a SingleHit, the 0-based offset in the genome that this hit. The aligner treats the entire
genome as a single string, even though it's really a set of chrosomes. It just makes the code simpler.
The caller can convert to chromosome+offset by looking up the piece boudaries using the Genome object.
hitIsRC - the aligner tries to align both the given read and its reverse complement. If we found a SingleHit this
bool is set to indicate whether that hit was on the reverse complement.
finalScore - if a single or confident hit, this is the score of the hit (i.e., the LV distance from the read)
mapq - return the mapping quality
searchRadius - if non-zero, constrain the search to this distance around searchLocation, in orientation searchRC
searchLocation - location to search around if searchRadius is given
searchRC - whether to search in reverse complement orientation if searchRadius is given
maxHitsToGet - if greater than 0, output up to this many hits within confDiff of the best (if any) in multiHitLocations,
writing their count in multiHitsFound, instead of returning MultipleHits immediately
multiHitsFound - output parameter for number of alternative hits found if maxHitsToGet is true
multiHitLocations - output parameter for locations of alternative hits found if maxHitsToGet is true
Return Value:
ConfidentHit, SingleHit, MultiHit or NotFound depending on how the alignment went.
--*/
{
//
// mapq and finalScore are optional parameters. Rather than checking all over the code to see if
// they're null, we just point them at locals if they're not passed in.
//
int unusedMapq;
int unusedFinalScore;
bestPassSeedsNotSkipped = 0;
bestPassRCSeedsNotSkipped = 0;
thisPassSeedsNotSkipped = 0;
thisPassRCSeedsNotSkipped = 0;
smallestSkippedSeed = 0xffffffff;
smallestSkippedRCSeed = 0xffffffff;
if (NULL == mapq) {
mapq = &unusedMapq;
}
if (NULL == finalScore) {
finalScore = &unusedFinalScore;
}
*genomeLocation = 0xFFFFFFFF; // Value to return if we don't find a location.
*hitIsRC = false; // So we deterministically print the read forward in this case.
*finalScore = UnusedScoreValue;
unsigned lookupsThisRun = 0;
popularSeedsSkipped = 0;
// Range of genome locations to search in.
unsigned minLocation = 0;
unsigned maxLocation = 0xFFFFFFFF;
if (searchRadius != 0) {
minLocation = (searchLocation > searchRadius) ? searchLocation - searchRadius : 0;
maxLocation = (searchLocation < 0xFFFFFFFF - searchRadius) ? searchLocation + searchRadius : 0xFFFFFFFF;
}
// If asked to return the locations of multiple hits, make sure we get sensible counts and results
if (maxHitsToGet > 0) {
memset(hitCount, 0, MAX_K * sizeof(unsigned));
*multiHitsFound = 0;
}
AlignmentResult finalResult;
//
// A bitvector for used seeds, indexed on the starting location of the seed within the read.
//
if (read->getDataLength() > maxReadSize) {
fprintf(stderr,"BaseAligner:: got too big read (%d > %d)", read->getDataLength(),maxReadSize);
exit(1);
}
if ((int)read->getDataLength() < seedLen) {
//
// Too short to have any seeds, it's hopeless.
//
return NotFound;
}
#ifdef _DEBUG
if (_DumpAlignments) {
printf("BaseAligner: aligning read ID '%.*s', data '%.*s'\n", read->getIdLength(), read->getId(), read->getDataLength(), read->getData());
}
#endif // _DEBUG
//
// Clear out the seed used array.
//
memset(seedUsed, 0, (read->getDataLength() + 7) / 8);
unsigned readLen = read->getDataLength();
const char *readData = read->getData();
const char *readQuality = read->getQuality();
unsigned countOfNs = 0;
for (unsigned i = 0; i < readLen; i++) {
char baseByte = readData[i];
rcReadData[readLen - i - 1] = rcTranslationTable[baseByte];
rcReadQuality[readLen - i - 1] = readQuality[i];
countOfNs += nTable[baseByte];
}
if (countOfNs > maxK) {
nReadsIgnoredBecauseOfTooManyNs++;
return NotFound;
}
//
// Block off any seeds that would contain an N.
//
if (countOfNs > 0) {
int minSeedToConsiderNing = 0; // In English, any word can be verbed. Including, apparently, "N."
for (int i = 0; i < (int) readLen; i++) {
if (readData[i] == 'N') {
int limit = __min(i + seedLen - 1, readLen-1);
for (int j = __max(minSeedToConsiderNing, i - (int) seedLen + 1); j <= limit; j++) {
SetSeedUsed(j);
}
minSeedToConsiderNing = limit+1;
if (minSeedToConsiderNing >= (int) readLen)
break;
}
}
}
Read rcRead[1];
rcRead->init(NULL, 0, rcReadData, rcReadQuality, readLen);
clearCandidates();
//
// Initialize the bases table, which represents which bases we've checked.
// We have readSize - seeds size + 1 possible seeds.
//
unsigned nPossibleSeeds = readLen - seedLen + 1;
unsigned nextSeedToTest = 0;
unsigned wrapCount = 0;
lowestPossibleScoreOfAnyUnseenLocation = 0;
lowestPossibleRCScoreOfAnyUnseenLocation = 0;
mostSeedsContainingAnyParticularBase = 1; // Instead of tracking this for real, we're just conservative and use wrapCount+1. It's faster.
mostRCSeedsContainingAnyParticularBase = 1;// ditto
bestScore = UnusedScoreValue;
secondBestScore = UnusedScoreValue;
nSeedsApplied = 0;
nRCSeedsApplied = 0;
lvScores = 0;
lvScoresAfterBestFound = 0;
probabilityOfAllCandidates = 0.0;
probabilityOfBestCandidate = 0.0;
if (maxHitsToGet > 0) {
scoreLimit = maxK + 3;
} else {
scoreLimit = maxK + confDiff - 1;
}
while (nSeedsApplied + nRCSeedsApplied < maxSeedsToUse) {
//
// Choose the next seed to use. Choose the first one that isn't used
//
if (nextSeedToTest >= nPossibleSeeds) {
//
// We're wrapping. We want to space the seeds out as much as possible, so if we had
// a seed length of 20 we'd want to take 0, 10, 5, 15, 2, 7, 12, 17. To make the computation
// fast, we use use a table lookup.
//
bestPassSeedsNotSkipped = __max(bestPassSeedsNotSkipped, thisPassSeedsNotSkipped);
bestPassRCSeedsNotSkipped = __max(bestPassRCSeedsNotSkipped, thisPassRCSeedsNotSkipped);
thisPassSeedsNotSkipped = 0;
thisPassRCSeedsNotSkipped = 0;
wrapCount++;
if (wrapCount >= seedLen) {
//
// We tried all possible seeds without matching or even getting enough seeds to
// exceed our seed count. Do the best we can with what we have.
//
#ifdef TRACE_ALIGNER
printf("Calling score with force=true because we wrapped around enough\n");
#endif
score(
true,
read,
rcRead,
&finalResult,
finalScore,
genomeLocation,
hitIsRC,
candidates,
maxHitsToGet,
mapq);
#if MAINTAIN_HISTOGRAMS
lvHistogram->addToCount(lvScores,lvScores);
lookupHistogram->addToCount(lookupsThisRun,lookupsThisRun);
if (MultipleHits == finalResult) {
lvHistogramForMulti->addToCount(lvScores,lvScores);
} else if (SingleHit == finalResult) {
lvCountWhenBestFound->addToCount(lvScores-lvScoresAfterBestFound);
}
#endif // MAINTAIN_HISTOGRAMS
fillHitsFound(maxHitsToGet, multiHitsFound,
multiHitLocations, multiHitRCs, multiHitScores);
#ifdef _DEBUG
if (_DumpAlignments) printf("\tFinal result score %d MAPQ %d at %u\n", *finalScore, *mapq, *genomeLocation);
#endif // _DEBUG
return finalResult;
}
nextSeedToTest = getWrappedNextSeedToTest(wrapCount);
mostSeedsContainingAnyParticularBase = wrapCount + 1;
mostRCSeedsContainingAnyParticularBase = wrapCount + 1;
}
while (nextSeedToTest < nPossibleSeeds && IsSeedUsed(nextSeedToTest)) {
//
// This seed is already used. Try the next one.
//
nextSeedToTest++;
}
if (nextSeedToTest >= nPossibleSeeds) {
//
// Unusable seeds have pushed us past the end of the read. Go back around the outer loop so we wrap properly.
//
continue;
}
Seed seed(read->getData() + nextSeedToTest, seedLen);
unsigned nHits; // Number of times this seed hits in the genome
const unsigned *hits; // The actual hits (of size nHits)
unsigned nRCHits; // Number of hits for the seed's reverse complement
const unsigned *rcHits; // The actual reverse complement hits
unsigned minSeedLoc = (minLocation < readLen ? 0 : minLocation - readLen);
unsigned maxSeedLoc = (maxLocation > 0xFFFFFFFF - readLen ? 0xFFFFFFFF : maxLocation + readLen);
genomeIndex->lookupSeed(seed, minSeedLoc, maxSeedLoc, &nHits, &hits, &nRCHits, &rcHits);
nHashTableLookups++;
lookupsThisRun++;
#ifdef _DEBUG
if (_DumpAlignments) {
printf("\tSeed offset %2d, %4d hits, %4d rcHits.", nextSeedToTest, nHits, nRCHits);
for (unsigned i = 0; i < __min(nHits, 5); i++) {
printf(" Hit at %9u.", hits[i]);
}
for (unsigned i = 0; i < __min(nRCHits, 5); i++) {
printf(" RC hit at %9u.", rcHits[i]);
}
printf("\n");
}
#endif // _DEUBG
#ifdef TRACE_ALIGNER
printf("Looked up seed %llx: hits=%u, rchits=%u\n", seed.getBases(), nHits, nRCHits);
printf("Hits:");
for (int i = 0; i < nHits; i++)
printf(" %u", hits[i]);
printf("\n");
printf("RC hits:");
for (int i = 0; i < nRCHits; i++)
printf(" %u", rcHits[i]);
printf("\n");
#endif
SetSeedUsed(nextSeedToTest);
bool appliedEitherSeed = false;
//
// Look for the seed in the forward direction, unless we were asked to search only the RC.
//
if (searchRadius == 0 || searchRC == false) {
if (nHits > maxHitsToConsider && !explorePopularSeeds) {
//
// This seed is matching too many places. Just pretend we never looked and keep going.
//
nHitsIgnoredBecauseOfTooHighPopularity++;
popularSeedsSkipped++;
smallestSkippedSeed = __min(nHits, smallestSkippedSeed);
} else {
if (0 == wrapCount) {
thisPassSeedsNotSkipped++;
}
//
// Update the candidates list with any hits from this seed. If lowest possible score of any unseen location is
// more than best_score + confDiff then we know that if this location is newly seen then its location won't ever be a
// winner, and we can ignore it.
//
for (unsigned i = 0 ; i < min(nHits, maxHitsToConsider); i++) {
//
// Find the genome location where the beginning of the read would hit, given a match on this seed.
//
unsigned genomeLocationOfThisHit = hits[i] - nextSeedToTest;
if (genomeLocationOfThisHit < minLocation || genomeLocationOfThisHit > maxLocation)
continue;
Candidate *candidate;
HashTableElement *hashTableElement;
findCandidate(genomeLocationOfThisHit,false,&candidate,&hashTableElement);
if (NULL != hashTableElement) {
incrementWeight(hashTableElement);
} else if (lowestPossibleScoreOfAnyUnseenLocation <= scoreLimit) {
allocateNewCandidate(genomeLocationOfThisHit,false, lowestPossibleScoreOfAnyUnseenLocation, &candidate, &hashTableElement);
}
}
nSeedsApplied++;
appliedEitherSeed = true;
}
}
//
// Look for the seed in the RC direction, unless we were asked to search only forwards.
//
if (searchRadius == 0 || searchRC == true) {
if (nRCHits > maxHitsToConsider && !explorePopularSeeds) {
//
// This seed is matching too many places. Just pretend we never looked and keep going.
//
nHitsIgnoredBecauseOfTooHighPopularity++;
popularSeedsSkipped++;
smallestSkippedRCSeed = __min(nRCHits, smallestSkippedRCSeed);
} else {
if (0 == wrapCount) {
thisPassRCSeedsNotSkipped++;
}
//
// The RC seed is at offset ReadSize - SeedSize - seed offset in the RC seed.
//
// To see why, imagine that you had a read that looked like 0123456 (where the digits
// represented some particular bases, and digit' is the base's complement). Then the
// RC of that read is 6'5'4'3'2'1'. So, when we look up the hits for the seed at
// offset 0 in the forward read (i.e. 012 assuming a seed size of 3) then the index
// will also return the results for the seed's reverse complement, i.e., 3'2'1'.
// This happens as the last seed in the RC read.
//
unsigned rcOffset = readLen - seedLen - nextSeedToTest;
//
// And now update the candidates list with any hits from this seed. If lowest possible score of any unseen RC location is
// more than best_score + confDiff then we know that if this location is newly seen then its location won't ever be a
// winner, and we can ignore it.
//
for (unsigned i = 0 ; i < min(nRCHits, maxHitsToConsider); i++) {
//
// Find the genome location where the beginning of the read would hit, given a match on this seed.
//
unsigned genomeLocationOfThisHit = rcHits[i] - rcOffset;
if (genomeLocationOfThisHit < minLocation || genomeLocationOfThisHit > maxLocation)
continue;
Candidate *candidate;
HashTableElement *hashTableElement;
findCandidate(genomeLocationOfThisHit,true,&candidate,&hashTableElement);
if (NULL != hashTableElement) {
incrementWeight(hashTableElement);
} else if (lowestPossibleRCScoreOfAnyUnseenLocation <= scoreLimit) {
allocateNewCandidate(genomeLocationOfThisHit, true, lowestPossibleRCScoreOfAnyUnseenLocation, &candidate, &hashTableElement);
}
}
nRCSeedsApplied++;
appliedEitherSeed = true;
}
}
//
// Move us along.
//
nextSeedToTest += seedLen;
if (appliedEitherSeed) {
//
// And finally, try scoring.
//
if (score( false,
read,
rcRead,
&finalResult,
finalScore,
genomeLocation,
hitIsRC,
candidates,
maxHitsToGet,
mapq)) {
#if MAINTAIN_HISTOGRAMS
lvHistogram->addToCount(lvScores,lvScores);
lookupHistogram->addToCount(lookupsThisRun,lookupsThisRun);
if (MultipleHits == finalResult) {
lvHistogramForMulti->addToCount(lvScores,lvScores);
} else if (SingleHit == finalResult) {
lvCountWhenBestFound->addToCount(lvScores-lvScoresAfterBestFound);
}
#endif // MAINTAIN_HISTOGRAMS
fillHitsFound(maxHitsToGet, multiHitsFound,
multiHitLocations, multiHitRCs, multiHitScores);
#ifdef _DEBUG
if (_DumpAlignments) printf("\tFinal result score %d MAPQ %d at %u\n", *finalScore, *mapq, *genomeLocation);
#endif // _DEBUG
return finalResult;
}
}
}
//
// Do the best with what we've got.
//
#ifdef TRACE_ALIGNER
printf("Calling score with force=true because we ran out of seeds\n");
#endif
score( true,
read,
rcRead,
&finalResult,
finalScore,
genomeLocation,
hitIsRC,
candidates,
maxHitsToGet,
mapq);
#if MAINTAIN_HISTOGRAMS
lvHistogram->addToCount(lvScores,lvScores);
lookupHistogram->addToCount(lookupsThisRun,lookupsThisRun);
if (MultipleHits == finalResult) {
lvHistogramForMulti->addToCount(lvScores,lvScores);
} else if (SingleHit == finalResult) {
lvCountWhenBestFound->addToCount(lvScores-lvScoresAfterBestFound);
}
#endif // MAINTAIN_HISTOGRAMS
fillHitsFound(maxHitsToGet, multiHitsFound,
multiHitLocations, multiHitRCs, multiHitScores);
#ifdef _DEBUG
if (_DumpAlignments) printf("\tFinal result score %d MAPQ %d at %u\n", *finalScore, *mapq, *genomeLocation);
#endif // _DEBUG
return finalResult;
}
bool
BaseAligner::score(
bool forceResult,
Read *read,
Read *rcRead,
AlignmentResult *result,
int *finalScore,
unsigned *singleHitGenomeLocation,
bool *hitIsRC,
Candidate *candidates,
unsigned maxHitsToGet,
int *mapq)
/*++
Routine Description:
Make progress in scoring a possibly partial alignment. This is a private method of the BaseAligner class that's used
only by AlignRead.
It does a number of things. First, it computes the lowest possible score of any unseen location. This is useful
because once we have a scored hit that's more than confDiff better than all unseen locations, there's no need to
lookup more of them, we can just score what we've got and be sure that the answer is right (unless errors have
pushed the read to be closer to a wrong location than to the correct one, in which case it's hopeless).
It then decides whether it should score a location, and if so what one to score. It chooses the unscored
location that's got the highest weight (i.e., appeared in the most hash table lookups), since that's most
likely to be the winner. If there are multiple candidates with the same best weight, it breaks the tie using
the best possible score for the candidates (which is determined when they're first hit). Remaining ties are
broken arbitrarily.
It merges indels with scored candidates. If there's an insertion or deletion in the read, then we'll get very
close but unequal results out of the hash table lookup for parts of the read on opposite sides of the
insertion or deletion. This throws out the one with the worse score.
It then figures out if we have a definitive answer, and says what that is.
Arguments:
forceResult - should we generate an answer even if it's not definitive?
read - the read we're aligning
rcRead - the reverse complement of read
result - returns the result if we reach one
singleHitGenomeLocation - returns the location in the genome if we return a single hit
hitIsRC - if we return a single hit, indicates whether it's on the reverse complement
candidates - in/out the array of candidates that have hit and possibly been scored
maxHitsToGet - whether the user wants to get multiple non-confident hits in the MultiHit case
mapq - returns the map quality if we've reached a final result
Return Value:
true iff we've reached a result. When called with forceResult, we'll always return true.
--*/
{
#ifdef TRACE_ALIGNER
printf("score() called with force=%d nsa=%d nrcsa=%d best=%u bestloc=%u 2nd=%u\n",
forceResult, nSeedsApplied, nRCSeedsApplied, bestScore, bestScoreGenomeLocation, secondBestScore);
printf("Read:\n ");
for (int i = 0; i < read->getDataLength(); i++) printf("%c", read->getData()[i]);
printf("\n");
//printf("Candidates:\n");
//for (int i = 0; i < nCandidates; i++) {
// Candidate* c = candidates + i;
// printf(" loc=%u rc=%d weight=%u minps=%u scored=%d score=%u r=%u-%u\n",
// c->genomeLocation, c->isRC, c->weight, c->minPossibleScore, c->scored,
// c->score, c->minRange, c->maxRange);
//}
//printf("\n\n");
#endif
if (0 == mostSeedsContainingAnyParticularBase && 0 == mostRCSeedsContainingAnyParticularBase) {
//
// The only way we can get here is if we've tried all of the seeds that we're willing
// to try and every one of them generated too many hits to process. Declare
// a multi hit and give up.
//
_ASSERT(forceResult);
*result = MultipleHits;
*mapq = 0;
return true;
}
//
// Recompute lowestPossibleScore.
//
if (0 != mostSeedsContainingAnyParticularBase) {
lowestPossibleScoreOfAnyUnseenLocation =
__max(lowestPossibleScoreOfAnyUnseenLocation,
nSeedsApplied / mostSeedsContainingAnyParticularBase);
}
if (0 != mostRCSeedsContainingAnyParticularBase) {
lowestPossibleRCScoreOfAnyUnseenLocation =
__max(lowestPossibleRCScoreOfAnyUnseenLocation,
nRCSeedsApplied / mostRCSeedsContainingAnyParticularBase);
}
#ifdef TRACE_ALIGNER
printf("Lowest possible scores for unseen locations: %d (fwd), %d (RC)\n",
lowestPossibleScoreOfAnyUnseenLocation,
lowestPossibleRCScoreOfAnyUnseenLocation);
#endif
// Return early if we haven't tried enough seeds
//if (__max(nSeedsApplied, nRCSeedsApplied) < 4 && !forceResult) {
// return false;
//}
unsigned weightListToCheck = highestUsedWeightList;
do {
HashTableElement *elementToScore;
_ASSERT(weightListToCheck <= maxSeedsToUse);
//
// Grab the next element to score, and score it.
//
while (weightListToCheck > 0 && weightLists[weightListToCheck].weightNext == &weightLists[weightListToCheck]) {
weightListToCheck--;
highestUsedWeightList = weightListToCheck;
}
_ASSERT(weightListToCheck <= maxSeedsToUse);
if (__min(lowestPossibleScoreOfAnyUnseenLocation,lowestPossibleRCScoreOfAnyUnseenLocation) > scoreLimit || forceResult) {
if (weightListToCheck == 0) {
//
// We've scored all live candidates and excluded all non-candidates. We have our
// answer.
//
int realConfDiff = confDiff + (popularSeedsSkipped >= adaptiveConfDiffThreshold ? 1 : 0);
*finalScore = bestScore;
if (bestScore + realConfDiff <= secondBestScore && bestScore <= maxK) {
if (popularSeedsSkipped == 0 && !forceResult) {
*result = CertainHit;
} else {
*result = SingleHit;
}
*singleHitGenomeLocation = bestScoreGenomeLocation;
*mapq = computeMAPQ(
probabilityOfAllCandidates,
probabilityOfBestCandidate,
bestScore,
__max(thisPassSeedsNotSkipped, bestPassSeedsNotSkipped),
__max(thisPassRCSeedsNotSkipped, bestPassRCSeedsNotSkipped),
smallestSkippedSeed,
smallestSkippedRCSeed
);
return true;
} else if (bestScore > maxK) {
// If none of our seeds was below the popularity threshold, report this as MultipleHits; otherwise,
// report it as NotFound
*result = (nSeedsApplied == 0 && nRCSeedsApplied == 0) ? MultipleHits : NotFound;
*mapq = 0;
return true;
} else {
*result = MultipleHits;
*mapq = computeMAPQ(
probabilityOfAllCandidates,
probabilityOfBestCandidate,
bestScore,
__max(thisPassSeedsNotSkipped, bestPassSeedsNotSkipped),
__max(thisPassRCSeedsNotSkipped, bestPassRCSeedsNotSkipped),
smallestSkippedSeed,
smallestSkippedRCSeed
);
return true;
}
}
//
// Nothing that we haven't already looked up can possibly be the answer. Score what we've got and exit.
//
forceResult = true;
} else if (weightListToCheck == 0) {
if (forceResult) {
*finalScore = bestScore;
int realConfDiff = confDiff + (popularSeedsSkipped >= adaptiveConfDiffThreshold ? 1 : 0);
if (bestScore + realConfDiff <= secondBestScore && bestScore <= maxK) {
*result = SingleHit;
*singleHitGenomeLocation = bestScoreGenomeLocation;
*mapq = computeMAPQ(
probabilityOfAllCandidates,
probabilityOfBestCandidate,
bestScore,
__max(thisPassSeedsNotSkipped, bestPassSeedsNotSkipped),
__max(thisPassRCSeedsNotSkipped, bestPassRCSeedsNotSkipped),
smallestSkippedSeed,
smallestSkippedRCSeed
);
return true;
} else if (bestScore > maxK) {
// If none of our seeds was below the popularity threshold, report this as MultipleHits; otherwise,
// report it as NotFound
*result = (nSeedsApplied == 0 && nRCSeedsApplied == 0) ? MultipleHits : NotFound;
return true;
} else {
*result = MultipleHits;
return true;
}
} else {
//
// No candidates, look for more.
//
return false;
}
}
elementToScore = weightLists[weightListToCheck].weightNext;
_ASSERT(!elementToScore->allExtantCandidatesScored);
_ASSERT(elementToScore->candidatesUsed != 0);
_ASSERT(elementToScore != &weightLists[weightListToCheck]);
if (elementToScore->lowestPossibleScore <= scoreLimit) {
unsigned long candidateIndexToScore;
_uint64 candidatesMask = elementToScore->candidatesUsed;
while (_BitScanForward64(&candidateIndexToScore,candidatesMask)) {
candidatesMask &= ~(_uint64)1 << candidateIndexToScore;
Candidate *candidateToScore = &elementToScore->candidates[candidateIndexToScore];
if (candidateToScore->scoredInEpoch == hashTableEpoch) {
//
// This can happen if we find a candidate, score it, and then hit on an indel
// near it.
//
continue;
}
unsigned genomeLocation = elementToScore->baseGenomeLocation + candidateIndexToScore;
unsigned score = -1;
double matchProbability;
unsigned readDataLength = elementToScore->isRC ? rcRead->getDataLength() : read->getDataLength();
unsigned genomeDataLength = readDataLength + MAX_K; // Leave extra space in case the read has deletions
const char *data = genome->getSubstring(genomeLocation, genomeDataLength);
if (NULL == data) {
//
// We're up against the end of a chromosome. Reduce the extra space enough that it isn't too
// long. We're willing to reduce it to less than the length of a read, because the read could
// but up against the end of the chromosome and have insertions in it.
//
const Genome::Piece *piece = genome->getPieceAtLocation(genomeLocation);
const Genome::Piece *nextPiece = genome->getPieceAtLocation(genomeLocation + readDataLength + MAX_K);
_ASSERT(NULL != piece && piece->beginningOffset <= genomeLocation && piece != nextPiece);
unsigned endOffset;
if (NULL != nextPiece) {
endOffset = nextPiece->beginningOffset;
} else {
endOffset = genome->getCountOfBases();
}
genomeDataLength = endOffset - genomeLocation - 1;
if (genomeDataLength >= readDataLength - MAX_K) {
data = genome->getSubstring(genomeLocation, genomeDataLength);
_ASSERT(NULL != data);
}
}
if (elementToScore->isRC) {
if (data != NULL) {
#ifdef USE_NEW_DISTANCE
score = bsd->compute(data, rcRead->getData(), rcRead->getDataLength(), scoreLimit);
#else
_uint64 cacheKey = 0;
if (readId != -1) {
cacheKey = ((_uint64) readId) << 33 | ((_uint64) elementToScore->isRC) << 32 | genomeLocation;
}
score = landauVishkin->computeEditDistance(
data, genomeDataLength,
rcRead->getData(), rcRead->getQuality(), rcRead->getDataLength(),
scoreLimit, &matchProbability, cacheKey);
if (-1 != score) {
probabilityOfAllCandidates += matchProbability;
}
#endif
}
#ifdef TRACE_ALIGNER
printf("Computing distance at %u (RC) with limit %d: %d\n", genomeLocation, scoreLimit, score);
#endif
} else {
if (data != NULL) {
#ifdef USE_NEW_DISTANCE
score = bsd->compute(data, read->getData(), read->getDataLength(), scoreLimit);
#else
_uint64 cacheKey = 0;
if (readId != -1) {
cacheKey = ((_uint64) readId) << 33 | ((_uint64) elementToScore->isRC) << 32 | genomeLocation;
}
score = landauVishkin->computeEditDistance(
data, genomeDataLength,
read->getData(), read->getQuality(), read->getDataLength(),
scoreLimit, &matchProbability, cacheKey);
if (-1 != score) {
probabilityOfAllCandidates += matchProbability;
}
#endif
}
#ifdef TRACE_ALIGNER
printf("Computing distance at %u (fwd) with limit %d: %d\n", genomeLocation, scoreLimit, score);
#endif
}
#ifdef _DEBUG
if (_DumpAlignments) printf("Scored %9u weight %2d limit %d, result %2d %s\n", genomeLocation, elementToScore->weight, scoreLimit, score, elementToScore->isRC ? "RC" : "");
#endif // _DEBUG
if (maxHitsToGet > 0 && score != -1 && hitCount[score] < maxHitsToGet) {
// Remember the location of this hit because we don't have enough at this distance
hitLocations[score][hitCount[score]] = genomeLocation;
hitRCs[score][hitCount[score]] = elementToScore->isRC;
hitCount[score]++;
}
candidateToScore->score = score;
candidateToScore->scoredInEpoch = hashTableEpoch;
elementToScore->bestScore = __min(elementToScore->bestScore, score);
nLocationsScored++;
lvScores++;
lvScoresAfterBestFound++;
// off until we fix the insert/delete problem _ASSERT(candidates[candidateToScore].score >= candidates[candidateToScore].minPossibleScore); // Else we messed up minPossibleScore (or LV or something)
if (bestScore > score ||
bestScore == score && matchProbability > probabilityOfBestCandidate) {
if ((secondBestScore == UnusedScoreValue || !(secondBestScoreGenomeLocation + maxK > genomeLocation && secondBestScoreGenomeLocation < genomeLocation + maxK)) &&
(bestScore == UnusedScoreValue || !(bestScoreGenomeLocation + maxK > genomeLocation && bestScoreGenomeLocation < genomeLocation + maxK)))
{
secondBestScore = bestScore;
secondBestScoreGenomeLocation = bestScoreGenomeLocation;
secondBestScoreIsRC = *hitIsRC;
}
//
// We have a new best score.
//
bestScore = score;
probabilityOfBestCandidate = matchProbability;
bestScoreGenomeLocation = genomeLocation;
*singleHitGenomeLocation = bestScoreGenomeLocation;
*finalScore = bestScore;
*hitIsRC = elementToScore->isRC;
lvScoresAfterBestFound = 0;
} else if (secondBestScore > score &&
(secondBestScore == UnusedScoreValue || !(secondBestScoreGenomeLocation + maxK > genomeLocation && secondBestScoreGenomeLocation < genomeLocation + maxK)) &&
!(bestScoreGenomeLocation + maxK > genomeLocation && bestScoreGenomeLocation < genomeLocation + maxK)) {
//
// A new second best.
//
secondBestScore = score;
secondBestScoreGenomeLocation = genomeLocation;
secondBestScoreIsRC = elementToScore->isRC;
}
if (stopOnFirstHit && bestScore <= maxK) {