-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathEagle.cpp
3633 lines (3354 loc) · 132 KB
/
Eagle.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
/*
This file is part of the Eagle haplotype phasing software package
developed by Po-Ru Loh. Copyright (C) 2015-2018 Harvard University.
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <utility>
#include <numeric>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include "omp.h"
#include <htslib/thread_pool.h>
#include <htslib/vcf.h>
#include "Types.hpp"
#include "FileUtils.hpp"
#include "MemoryUtils.hpp"
#include "NumericUtils.hpp"
#include "StringUtils.hpp"
#include "Timer.hpp"
#include "HapHedge.hpp"
#include "Version.hpp"
#include "Eagle.hpp"
//#define DETAILS
namespace EAGLE {
using std::vector;
using std::string;
using std::pair;
using std::make_pair;
using std::cout;
using std::cerr;
using std::endl;
using std::max;
using std::min;
const double MEMO_UNSET = -1000;
const char noTrioInfo = '`';
const string trio1 = "\033[1;36mo\033[0m";
const string trio2 = "\033[1;31mx\033[0m";
const char IBDx2char = '_';
const char ROHchar = '=';
const char wrongChar = '@';
const char conflictChar = '#';
inline uint popcount64(uint64 i) {
i = i - ((i >> 1) & 0x5555555555555555);
i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
i = (i + (i >> 4)) & 0xF0F0F0F0F0F0F0F;
return (i * 0x101010101010101) >> 56;
}
void Eagle::init() {
totTicks = 0; extTicks = 0; diphapTicks = 0; lshTicks = 0; lshCheckTicks = 0;
dpTicks = 0; dpStaticTicks = 0; dpSwitchTicks = 0; dpUpdateTicks = 0; dpSortTicks = 0;
dpUpdateCalls = 0; blipFixTicks = 0; blipPopTicks = 0; blipVoteTicks = 0; blipLshTicks = 0;
maskSnps64j = ALIGNED_MALLOC_UCHARS(Mseg64*64);
cMs64j = ALIGNED_MALLOC_DOUBLES(Mseg64*64+1);
double cMlast = 0;
for (uint64 m64 = 0; m64 < Mseg64; m64++) {
for (uint64 j = 0; j < seg64cMvecs[m64].size(); j++) {
maskSnps64j[m64*64+j] = 1;
cMs64j[m64*64+j] = cMlast = seg64cMvecs[m64][j];
}
for (uint64 j = seg64cMvecs[m64].size(); j < 64; j++) {
maskSnps64j[m64*64+j] = 0;
cMs64j[m64*64+j] = cMlast;
}
}
cMs64j[Mseg64*64] = cMlast;
haploBits = ALIGNED_MALLOC_UINT64S(Mseg64*2*N);
haploBitsT = ALIGNED_MALLOC_UINT64S(2*N*Mseg64);
segConfs = ALIGNED_MALLOC_UCHARS(2*N*Mseg64);
maskIndivs = vector <uchar> (N, 1);
for (uint wrongBitsA = 0; wrongBitsA < (1U<<switchScoreLutBits); wrongBitsA++)
for (uint wrongBitsB = 0; wrongBitsB < (1U<<switchScoreLutBits); wrongBitsB++)
for (uint hetBits = 0; hetBits < (1U<<switchScoreLutBits); hetBits++) {
uint wrongHomBitsA = wrongBitsA & ~hetBits;
uint wrongHetBitsA = wrongBitsA & hetBits;
uint wrongHomBitsB = wrongBitsB & ~hetBits;
uint wrongHetBitsB = wrongBitsB & hetBits;
uint lutInd = (wrongBitsA<<(switchScoreLutBits+switchScoreLutBits))
| (wrongBitsB<<(switchScoreLutBits))
| hetBits;
char &minDiff = switchScoreLut[lutInd][0]; minDiff = 0;
char &cumDiff = switchScoreLut[lutInd][1]; cumDiff = 0;
for (uint k = 0; k < switchScoreLutBits; k++) {
cumDiff += ((wrongHomBitsA>>k)&1)*homErrCost + ((wrongHetBitsA>>k)&1)*hetErrCost
- ((wrongHomBitsB>>k)&1)*homErrCost - ((wrongHetBitsB>>k)&1)*hetErrCost;
if (cumDiff < minDiff) minDiff = cumDiff;
}
}
}
Eagle::Eagle(uint64 _N, uint64 _Mseg64, const uint64_masks *_genoBits,
vector < vector <double> > _seg64cMvecs, const AlleleFreqs *_seg64logPs,
vector <double> _invLD64j, const vector <IndivInfoX> &_indivs,
const vector <SnpInfoX> &_snps, const string &maskFile,
const vector <bool> &_isFlipped64j, double _pErr, int runStep2) :
N(_N), Nref(0), Mseg64(_Mseg64), genoBits(_genoBits), seg64cMvecs(_seg64cMvecs),
seg64logPs(_seg64logPs), invLD64j(_invLD64j), indivs(_indivs), snps(_snps),
isFlipped64j(_isFlipped64j), logPerr(log10(_pErr)) {
init();
if (runStep2) {
phaseConfs = ALIGNED_MALLOC_UCHARS(2*N*Mseg64*64);
phaseConfs2 = ALIGNED_MALLOC_UCHARS(2*N*Mseg64*64);
tmpHaploBitsT = NULL;
}
else {
phaseConfs = phaseConfs2 = NULL;
tmpHaploBitsT = ALIGNED_MALLOC_UINT64S(2*N*Mseg64);
memset(tmpHaploBitsT, 0, 2*N*Mseg64*sizeof(tmpHaploBitsT[0]));
}
if (!maskFile.empty()) {
int masked = 0;
vector < pair <string, string> > maskFidIids = FileUtils::readFidIids(maskFile);
std::set < pair < string, string> > maskSet(maskFidIids.begin(), maskFidIids.end());
for (uint64 n = 0; n < N; n++)
if (maskSet.count(make_pair(indivs[n].famID, indivs[n].indivID))) {
maskIndivs[n] = 0;
masked++;
}
cout << "Number of indivs masked: " << masked << endl;
}
}
// constructor for ref-mode
Eagle::Eagle(uint64 _Nref, uint64 _Ntarget, uint64 _Mseg64, const uint64_masks *_genoBits,
vector < vector <double> > _seg64cMvecs, double _pErr) :
N(_Nref+_Ntarget), Nref(_Nref), Mseg64(_Mseg64), genoBits(_genoBits),
seg64cMvecs(_seg64cMvecs), logPerr(log10(_pErr)) {
init();
isFlipped64j = vector <bool> (Mseg64*64); // no flipping in ref mode
phaseConfs = phaseConfs2 = NULL;
tmpHaploBitsT = ALIGNED_MALLOC_UINT64S(2*(N-Nref)*Mseg64);
memset(segConfs, 0, 2*N*Mseg64*sizeof(segConfs[0]));
for (uint64 nRef = 0; nRef < Nref; nRef++)
for (uint64 m64 = 0; m64 < Mseg64; m64++) { // copy ref haploBits stored in genoBits
haploBits[m64*2*N + 2*nRef] = genoBits[m64*N + nRef].is0;
haploBits[m64*2*N + 2*nRef+1] = genoBits[m64*N + nRef].is2;
for (uint64 nHap = 2*nRef; nHap <= 2*nRef+1; nHap++)
haploBitsT[nHap*Mseg64 + m64] = haploBits[m64*2*N + nHap];
}
}
void Eagle::reallocLRPtoPBWT(void) { // non-ref mode transition: LRP iters 1-2 -> PBWT iters 3+
assert(phaseConfs != NULL);
ALIGNED_FREE(phaseConfs2); phaseConfs2 = NULL;
ALIGNED_FREE(phaseConfs); phaseConfs = NULL;
assert(tmpHaploBitsT == NULL);
tmpHaploBitsT = ALIGNED_MALLOC_UINT64S(2*N*Mseg64);
}
Eagle::~Eagle() {
ALIGNED_FREE(segConfs);
ALIGNED_FREE(haploBitsT);
ALIGNED_FREE(haploBits);
if (phaseConfs != NULL) {
ALIGNED_FREE(phaseConfs2);
ALIGNED_FREE(phaseConfs);
}
if (tmpHaploBitsT != NULL) {
ALIGNED_FREE(tmpHaploBitsT); // allocated only in ref-mode
}
ALIGNED_FREE(cMs64j);
ALIGNED_FREE(maskSnps64j);
}
inline uint getNonMissingGeno(const uint64_masks &bits, uint64 j) {
if (bits.is0 & (1ULL<<j)) return 0;
if (bits.is2 & (1ULL<<j)) return 2;
return 1; // assumed to be non-missing
}
inline uint bgetGeno0123(const uint64_masks &bits, uint64 j) {
if (bits.is0 & (1ULL<<j)) return 0;
if (bits.is2 & (1ULL<<j)) return 2;
if (bits.is9 & (1ULL<<j)) return 3;
return 1;
}
uint Eagle::getGeno0123(uint64 m64j, uint64 n) const {
return bgetGeno0123(genoBits[m64j/64*N + n], m64j&63);
}
void Eagle::retractMatch(uint n0, Match &match, double memoLogBF[][4]) const {
for (int dir = 0; dir < 2; dir++) {
double cumLogBF = 0;
while (cumLogBF < log10(4)) {
uint m64j;
if (dir == 0) m64j = match.m64jStart++;
else m64j = match.m64jEnd--;
cumLogBF += memoLogBF[m64j][getGeno0123(m64j, match.n)];
}
}
match.m64jStart--;
match.m64jEnd++;
}
Match Eagle::computeDuoLogBF(double memoLogBF[][4], double workLogBF[], uint64 n0, uint64 n1, uint64 m64cur) const {
//double snpsChecked = 1;
Match match(n1, m64cur*64, m64cur*64, 0);
workLogBF[m64cur*64] = 0;
for (int dir = 0; dir < 2; dir++) {
uint64 inc, m64start, m64end, jStart, jEnd;
if (dir == 0) {
inc = 1; m64start = m64cur; m64end = Mseg64; jStart = 0; jEnd = 64;
}
else {
inc = -1ULL; m64start = m64cur-1; m64end = -1ULL; jStart = 63; jEnd = -1ULL;
}
double maxLogBF = 0, curLogBF = 0;
for (uint64 m64 = m64start; m64 != m64end; m64 += inc) {
const uint64_masks &bits0 = genoBits[m64*N + n0], &bits1 = genoBits[m64*N + n1];
uint64 wrongBits = (bits0.is0 & bits1.is2) | (bits0.is2 & bits1.is0);
if (wrongBits & (wrongBits-1)) // 2+ wrong => fail
break;
uint64 missMask = bits0.is9 | bits1.is9;
for (uint64 j = jStart; j != jEnd; j += inc) {
uint geno1 = bgetGeno0123(bits1, j);
double logBFj = 0;
if (!(missMask & (1ULL<<j))) {
double &memoLogBFj = memoLogBF[m64*64+j][geno1];
if (memoLogBFj == MEMO_UNSET) {
uint geno0 = getNonMissingGeno(bits0, j);
double logP_geno1_null = seg64logPs[m64*64+j].cond[geno1][3];
double logP_geno1_duo = seg64logPs[m64*64+j].cond[geno1][geno0];
memoLogBFj = min(max((logP_geno1_duo - logP_geno1_null) * invLD64j[m64*64+j],
logPerr), -logPerr);
}
logBFj = memoLogBFj;
}
workLogBF[m64*64+j] = logBFj;
curLogBF += logBFj;
//if (logBFj != 0) snpsChecked += invLD64j[m64*64+j];
if (curLogBF > maxLogBF) {
maxLogBF = curLogBF;
if (inc == 1) match.m64jEnd = m64*64+j;
else match.m64jStart = m64*64+j;
}
}
}
match.logBF += maxLogBF;
}
double minLogBF = 0, curLogBF = 0;
for (uint64 m64j = match.m64jStart; m64j <= match.m64jEnd; m64j++) {
curLogBF += workLogBF[m64j];
if (curLogBF < minLogBF) {
minLogBF = curLogBF;
match.m64jStart = m64j+1;
while (!maskSnps64j[match.m64jStart]) match.m64jStart++;
}
}
match.logBF -= minLogBF;
//match.logBF -= log10(snpsChecked);
match.cMlenInit = cMs64j[match.m64jEnd] - cMs64j[match.m64jStart];
return match;
}
void Eagle::trim(Match &match, const Match &ref, uint64 n0, int orientation, uint64 trimStart,
int inc, double workLogBF[]) const {
uint64 n1 = match.n, n2 = ref.n;
// find IBDx2; store IBDx2 status in workLogBF (0 or 1) to compute probabilities accordingly
double IBDx2logBF = 0; uint64 IBDx2start = trimStart;
for (uint64 m64j = trimStart; m64j+1!=match.m64jStart && m64j!=match.m64jEnd+1; m64j += inc)
workLogBF[m64j] = 0; // initialize workLogBF to 0 (not IBDx2) in *MATCH* (n1)
// check for IBDx2 in *REF* (n2)
uint64 m64jLast; // last SNP to check: go beyond end of match to ensure detection of ref IBDx2
if (inc == 1)
m64jLast = min(match.m64jEnd + 50ULL, Mseg64*64-1);
else
m64jLast = max((int) match.m64jStart - 50, 0);
for (uint64 m64j = trimStart; m64j!=m64jLast+inc; m64j += inc) {
uint g0 = getGeno0123(m64j, n0);
uint g2 = getGeno0123(m64j, n2);
bool mismatch = false;
if (g0 != 3 && g2 != 3) {
if (g0 == g2)
IBDx2logBF += seg64logPs[m64j].cond[g2][g0] * invLD64j[m64j];
else
mismatch = true;
}
if (mismatch || m64j==m64jLast) { // end of IBDx2 segment
if (IBDx2logBF < -1) { // 10:1 IBDx2
#ifdef VERBOSE
printf("IBDx2 detected in %d: %.1f-%.1f (%d SNPs)\n", (int) n2, cMs64j[IBDx2start], cMs64j[m64j], (int) (m64j-IBDx2start));
#endif
for (uint64 m64j2 = IBDx2start; m64j2 != m64j; m64j2 += inc)
workLogBF[m64j2] = 1;
}
IBDx2logBF = 0; // reset
IBDx2start = m64j+inc;
}
}
double maxLogBF = 0, curLogBF = 0; uint64 m64jBest = trimStart;
for (uint64 m64j = trimStart; m64j+1!=match.m64jStart && m64j!=match.m64jEnd+1; m64j += inc) {
double logBF = 0;
uint g0 = getGeno0123(m64j, n0);
uint g1 = getGeno0123(m64j, n1);
uint g2 = getGeno0123(m64j, n2);
if (g0 != 3 && g1 != 3) {
uint g0eff = g0;
if (g0 == 1 && ref.m64jStart <= m64j && m64j <= ref.m64jEnd && g2 != 3) {
// n0 het and n2 (ref) match info available
if (g2 != 1) { // n2 hom
if (orientation == 1)
g0eff = g2; // treat g0 as n2 hom
else
g0eff = 2-g2; // treat g0 as opp n2 hom
}
else if (workLogBF[m64j] == 0) { // n0 and n2 both hets and not IBDx2
if (orientation == 1)
g0eff = 4; // same orientation as het-het => p(hap=1) = 1-p
else
g0eff = 5; // opp orientation to het-het => p(hap=1) = p
}
}
double logP_geno1_null = seg64logPs[m64j].cond[g1][3];
double logP_geno1_duo = seg64logPs[m64j].cond[g1][g0eff];
logBF = min(max((logP_geno1_duo - logP_geno1_null) * invLD64j[m64j],
logPerr), -logPerr);
}
curLogBF += logBF;
if (curLogBF > maxLogBF) {
maxLogBF = curLogBF;
m64jBest = m64j;
}
workLogBF[m64j] = curLogBF;
}
uint64 m64jTrim = m64jBest;
if (inc == 1) match.m64jEnd = m64jTrim;
else match.m64jStart = m64jTrim;
// conservative trimming: backtrack to 10x higher prob
uint m64jTrimCons = trimStart;
for (uint64 m64j = trimStart; m64j != m64jBest; m64j += inc)
if (workLogBF[m64j] < maxLogBF - log10(10))
m64jTrimCons = m64j;
if (inc == 1) match.m64jEndCons = std::min(match.m64jEndCons, m64jTrimCons);
else match.m64jStartCons = std::max(match.m64jStartCons, m64jTrimCons);
}
vector <int> searchSigns(const vector <Match> &matches, const vector < vector <uint> > &sameEdges, const vector < vector <uint> > &oppEdges, const vector <bool> &kept) {
// process left to right so that when sign choice is arbitrary, adjacent matches have same sign
uint V = matches.size();
vector <int> signs(V);
vector < pair <uint, uint> > order(V);
for (uint v = 0; v < V; v++)
order[v] = make_pair(matches[v].m64jStart, v);
sort(order.begin(), order.end());
uint lastEnd = 0; int lastSign = 1; // sign of farthest-right match seen so far
std::queue <uint> q;
for (uint i = 0; i < V; i++) {
uint v = order[i].second;
if (!kept[v]) continue; // not used
if (signs[v]) continue; // already visited
signs[v] = lastSign;
q.push(v);
while (!q.empty()) {
uint u = q.front(); q.pop();
for (uint i = 0; i < sameEdges[u].size(); i++) {
uint w = sameEdges[u][i];
if (!kept[w]) continue;
if (signs[w]) {
if (signs[w] != signs[u])
return vector <int> ();
}
else {
signs[w] = signs[u];
q.push(w);
if (matches[w].m64jEnd > lastEnd) {
lastEnd = matches[w].m64jEnd;
lastSign = signs[w];
}
}
}
for (uint i = 0; i < oppEdges[u].size(); i++) {
uint w = oppEdges[u][i];
if (!kept[w]) continue;
if (signs[w]) {
if (signs[w] == signs[u])
return vector <int> ();
}
else {
signs[w] = -signs[u];
q.push(w);
if (matches[w].m64jEnd > lastEnd) {
lastEnd = matches[w].m64jEnd;
lastSign = signs[w];
}
}
}
}
}
return signs;
}
void updateVote(int &votesCur, int votesThresh, int vote) {
if (abs(votesCur) >= votesThresh) return;
votesCur += vote;
}
void Eagle::computePhaseConfs(uint64 n0, const vector <Match> &matches,
const vector <int> &signs, bool cons) {
vector < vector <int> > votes(2, vector <int> (Mseg64*64));
vector <int> votesThresh(Mseg64*64);
const int votesMax = 1000000;
for (uint64 m64j = 0; m64j < Mseg64*64; m64j++) {
if (maskSnps64j[m64j]) {
votesThresh[m64j] = // 2 / log10((1-p)/p) = number of votes needed to get >100:1 odds
(int) (2 / fabs(seg64logPs[m64j].cond[0][4] - seg64logPs[m64j].cond[0][5])) + 1;
if (!(votesThresh[m64j] < votesMax)) votesThresh[m64j] = votesMax;
uint g0 = getGeno0123(m64j, n0);
if (g0 == 0) { votes[0][m64j] = votes[1][m64j] = -votesMax; }
else if (g0 == 2) { votes[0][m64j] = votes[1][m64j] = votesMax; }
else if (g0 == 3) { votes[0][m64j] = votes[1][m64j] = -1; } // missing: default P(1) = p
}
}
vector <uchar> isIBDx2(Mseg64*64);
for (uint i = 0; i < matches.size(); i++) {
if (!signs[i]) continue;
uint64 start, end;
if (cons) {
start = std::max(matches[i].m64jStartCons, matches[i].m64jStart);
end = std::min(matches[i].m64jEndCons, matches[i].m64jEnd);
}
else {
start = matches[i].m64jStart;
end = matches[i].m64jEnd;
}
#ifdef VERBOSE
printf("match %d (%.1f-%.1f)\n", (int) i, cMs64j[start], cMs64j[end]);
#endif
// find IBDx2 regions
vector < pair <uint64, uint64> > IBDx2regions;
uint64 m64jFirst = max((int) start - 50, 0); // go beyond ends to detect overhanging IBDx2
uint64 m64jLast = min(end + 50ULL, Mseg64*64-1);
double IBDx2logBF = 0; uint64 IBDx2start = m64jFirst;
for (uint64 m64j = m64jFirst; m64j <= m64jLast; m64j++) {
uint g0 = getGeno0123(m64j, n0);
uint g1 = getGeno0123(m64j, matches[i].n);
bool mismatch = false;
if (g0 != 3 && g1 != 3) {
if (g0 == g1)
IBDx2logBF += seg64logPs[m64j].cond[g1][g0] * invLD64j[m64j];
else
mismatch = true;
}
if (mismatch || m64j==m64jLast) { // end of IBDx2 segment
if (IBDx2logBF < -1) { // 10:1 IBDx2
IBDx2regions.push_back(make_pair(IBDx2start, m64j));
#ifdef VERBOSE
printf("IBDx2 detected in %d: %.1f-%.1f (%d SNPs)\n", (int) matches[i].n, cMs64j[IBDx2start], cMs64j[m64j], (int) (m64j-IBDx2start));
#endif
}
IBDx2logBF = 0; // reset
IBDx2start = m64j+1;
}
}
for (uint r = 0; r < IBDx2regions.size(); r++) // set IBDx2 region flags
memset(&isIBDx2[IBDx2regions[r].first], 1, IBDx2regions[r].second-IBDx2regions[r].first);
// accumulate votes
for (uint64 m64j = start; m64j <= end; m64j++) {
if (!maskSnps64j[m64j]) continue;
uint g0 = getGeno0123(m64j, n0);
uint g1 = getGeno0123(m64j, matches[i].n);
int vote = 0;
if (g1 == 0 || g1 == 2) // n1 hom: IBDx2 status irrelevant; phase determined (votesMax)
vote = (g1-1)*votesMax*2; // super strong vote (overrides any previous small votes)
else if (g1 == 1 && !isIBDx2[m64j]) // n1 het and not IBDx2; weak phase info
vote = 1;
if (vote) {
int q = (signs[i] == 1);
updateVote(votes[q][m64j], votesThresh[m64j], vote);
if (g0 == 1) // n0 het: pass info to opp chromosome
updateVote(votes[!q][m64j], votesThresh[m64j], -vote);
}
}
for (uint r = 0; r < IBDx2regions.size(); r++) // unset IBDx2 region flags
memset(&isIBDx2[IBDx2regions[r].first], 0, IBDx2regions[r].second-IBDx2regions[r].first);
}
// fast rng: last 16 bits of Marsaglia's MWC
uint w = 521288629;
if (phaseConfs != NULL) { // need to make hard calls here
for (uint i = 0; i < (n0 & 0xff); i++)
w=18000*(w&65535)+(w>>16);
}
for (uint64 m64j = 0; m64j < Mseg64*64; m64j++) {
if (maskSnps64j[m64j]) {
for (uint64 q = 0; q <= 1; q++) {
double phaseConf;
if (votes[q][m64j] >= votesMax)
phaseConf = 1;
else if (votes[q][m64j] <= -votesMax)
phaseConf = 0;
else {
double OR = pow(10.0, fabs(seg64logPs[m64j].cond[0][4] - seg64logPs[m64j].cond[0][5])
* votes[q][m64j]);
phaseConf = OR / (1 + OR);
}
if (phaseConfs != NULL)
phaseConfs[(2*n0+q)*Mseg64*64 + m64j] = (uchar) (phaseConf * 255);
else {
uchar uPhaseConf = (uchar) (phaseConf * 255);
if (uPhaseConf == (uchar) 255 || ((w=18000*(w&65535)+(w>>16))&255) < uPhaseConf)
tmpHaploBitsT[(2*n0+q)*Mseg64 + (m64j/64)] |= 1ULL<<(m64j&63);
}
}
}
else {
if (phaseConfs != NULL)
phaseConfs[2*n0*Mseg64*64 + m64j] = phaseConfs[(2*n0+1)*Mseg64*64 + m64j] = 0;
}
}
}
vector <int> Eagle::trioRelPhase(uint64 n0, uint64 nF1, uint64 nF2) const {
bool isParent = false;
if (((int) nF1) < 0) { // nF1 is the child; n0 is a parent
isParent = true;
nF1 = -nF1;
}
vector <int> trioPhaseVec;
for (uint64 m64j = 0; m64j < Mseg64*64; m64j++) {
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if (!isParent) {
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
}
else { // n0 is a parent; nF1 is the child
int g0 = getGeno0123(m64j, nF1); // child
int g2 = getGeno0123(m64j, nF2); // other parent
if (g0+g2 != 2) { // not Mendel error or triple het
if (g0 == 0) trioPhase = -1;
if (g0 == 2) trioPhase = 1;
if (g0 == 1) { // child is a het
if (g2 == 0) trioPhase = 1;
if (g2 == 2) trioPhase = -1;
}
}
}
trioPhaseVec.push_back(trioPhase); // 0 => unknown; +/-1 => pat/mat
if (trioPhase == 0) continue;
}
vector <int> trioRelPhaseVec(trioPhaseVec.size()-1);
for (uint i = 1; i < trioPhaseVec.size(); i++)
trioRelPhaseVec[i-1] = (trioPhaseVec[i-1]==0 || trioPhaseVec[i]==0) ? -1 :
(trioPhaseVec[i-1]==trioPhaseVec[i] ? 0 : 1); // -1 => unknown; 0 => same; 1 => opp
return trioRelPhaseVec;
}
void Eagle::checkPhase(uint64 n0, uint64 nF1, uint64 nF2, double thresh) const {
cout << "checking at thresh=" << thresh << ": ";
double lastPhased = cMs64j[0];
for (uint64 m64j = 0; m64j < Mseg64*64; m64j++) {
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2)&(1ULL<<j)) continue; // hom
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
double phaseConf = phaseConfs[2*n0*Mseg64*64 + m64j] / 255.0;
if (std::min(phaseConf, 1-phaseConf) <= thresh) {
double cM = cMs64j[m64j];
for (int tick = (int) (10*lastPhased) + 1; tick < 10*cM; tick++) {
if (tick % 10 == 0) cout << StringUtils::itos(tick/10); //(char) ('0' + (tick/10)%10);
else cout << '-';
}
if ((phaseConf < 0.5) == (trioPhase == 1))
cout << trio1;
else
cout << trio2;
lastPhased = cM;
}
else
cout << '?';
}
cout << endl;
}
vector <bool> Eagle::checkPhaseConfsPhase(uint64 n0, uint64 nF1, uint64 nF2) const {
vector <bool> ret;
int lastPhased64j = -1;
for (uint64 m64j = 0; m64j < Mseg64*64; m64j++) {
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // hom
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
int hapBit = (int) phaseConfs[2*n0*Mseg64*64 + m64j] >= 128;
bool phase = hapBit == (trioPhase == 1);
if (lastPhased64j != -1 && ret.back() != phase)
printf(" %.2f", (cMs64j[lastPhased64j] + cMs64j[m64j]) / 2);
lastPhased64j = m64j;
ret.push_back(phase);
}
cout << endl;
return ret;
}
void Eagle::checkHapPhase(uint64 n0, uint64 nF1, uint64 nF2, const uint64 curHaploBitsT[],
uint64 m64, uint64 side, vector < vector <int> > votes) const {
if ((int) nF1 == -1) return;
for (uint64 m64j = (m64-side)*64; m64j < (m64+side+1)*64; m64j++) {
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
//if (((haploBits[m64cur*2*N + n1hap]>>j)&1) == (trioPhase == 1))
//if (((haploBitsT[n1hap*Mseg64 + m64cur]>>j)&1) == (trioPhase == 1))
if (((curHaploBitsT[m64cur]>>j)&1) == (trioPhase == 1))
cout << trio1;
else
cout << trio2;
if (!votes.empty())
cout << "[" << votes[j][(curHaploBitsT[m64cur]>>j)&1] << "|" << votes[j][!((curHaploBitsT[m64cur]>>j)&1)] << ";" << votes[j][((curHaploBitsT[m64cur]>>j)&1)+2] << "|" << votes[j][!((curHaploBitsT[m64cur]>>j)&1)+2] << "]";
}
cout << endl;
}
vector <bool> Eagle::checkHapPhase1(uint64 n0, uint64 nF1, uint64 nF2, uint64 n1hap,
uint64 m64start, uint64 m64end, int sign) const {
vector <bool> ret;
if ((int) nF1 == -1) return ret;
cout << "n1hap = " << n1hap << "; m64 = [" << m64start << "," << m64end << "): ";
for (uint64 m64j = m64start*64; m64j < m64end*64; m64j++) {
if (m64j != m64start*64 && (m64j&63)==0) cout << "|";
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
if (((haploBits[m64cur*2*N + n1hap]>>j)&1) == (trioPhase == sign)) {
cout << trio1;
ret.push_back(0);
}
else {
cout << trio2;
ret.push_back(1);
}
}
cout << endl;
return ret;
}
vector <bool> Eagle::checkHapPhase1j(uint64 n0, uint64 nF1, uint64 nF2, uint64 n1hap,
uint64 m64jStart, uint64 m64jEnd, int sign) const {
vector <bool> ret;
if ((int) nF1 == -1) return ret;
//cout << "n1hap = " << n1hap << "; m64 = [" << m64start << "," << m64end << "): ";
for (uint64 m64j = m64jStart; m64j < m64jEnd; m64j++) {
if (m64j != m64jStart && (m64j&63)==0) cout << m64j/64;//"|";
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
if (((haploBits[m64cur*2*N + n1hap]>>j)&1) == (trioPhase == sign)) {
cout << trio1;
ret.push_back(0);
}
else {
cout << trio2;
ret.push_back(1);
}
}
//cout << endl;
return ret;
}
vector <bool> Eagle::checkHapPhase1jCall(uint64 n0, uint64 nF1, uint64 nF2, uint64 callBitsT[],
uint64 m64jStart, uint64 m64jEnd, bool print, int sign) const {
vector <bool> ret;
if ((int) nF1 == -1) return ret;
for (uint64 m64j = m64jStart; m64j < m64jEnd; m64j++) {
if (m64j != m64jStart && (m64j&63)==0)
if (print) cout << m64j/64;//"|";
if (!maskSnps64j[m64j]) continue;
uint64 m64cur = m64j/64; uint64 j = m64j&63;
const uint64_masks &bits0 = genoBits[m64cur*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64cur*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64cur*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
if (((callBitsT[m64cur]>>j)&1) == (trioPhase == sign)) {
if (print) cout << trio1;
ret.push_back(0);
}
else {
if (print) cout << trio2;
ret.push_back(1);
}
}
if (print) cout << endl;
return ret;
}
int Eagle::checkHapPhase2(uint64 n0, uint64 nF1, uint64 nF2, uint64 n1hap,
uint64 n2hapA, uint64 n2hapB, uint64 m64, int sign) const {
vector <bool> ret;
if ((int) nF1 == -1) return 0;/*ret*/;
uint64 n1is1 = haploBitsT[n1hap*Mseg64 + m64];
uint64 n2is1A = haploBitsT[n2hapA*Mseg64 + m64];
uint64 n2is1B = haploBitsT[n2hapB*Mseg64 + m64];
const uint64_masks &bits0 = genoBits[m64*N + n0];
uint64 wrongHomBitsA = (bits0.is0 & (n1is1 | n2is1A)) | (bits0.is2 & ~(n1is1 & n2is1A));
uint64 wrongHetBitsA = (~(bits0.is0|bits0.is2|bits0.is9) & ~(n1is1 ^ n2is1A));
uint64 wrongHomBitsB = (bits0.is0 & (n1is1 | n2is1B)) | (bits0.is2 & ~(n1is1 & n2is1B));
uint64 wrongHetBitsB = (~(bits0.is0|bits0.is2|bits0.is9) & ~(n1is1 ^ n2is1B));
uint score = popcount64(wrongHomBitsB)*homErrCost
+ popcount64(wrongHetBitsB)*hetErrCost;
uint minScore = score; uint64 kSwitch = 0;
for (uint64 k = 0; k < 64; k++) {
score += ((wrongHomBitsA>>k)&1)*homErrCost + ((wrongHetBitsA>>k)&1)*hetErrCost
- ((wrongHomBitsB>>k)&1)*homErrCost - ((wrongHetBitsB>>k)&1)*hetErrCost;
if (score < minScore) {
minScore = score;
kSwitch = k+1;
}
}
cout << "m64 = " << m64 << ": (" << n1hap << "," << n2hapA;
if (n2hapA != n2hapB) cout << "-" << n2hapB;
cout << ") score = " << minScore << " ";
cout << " conf = " << (int) segConfs[n1hap*Mseg64+m64] << "," << (int) segConfs[n2hapA*Mseg64+m64];
if (n2hapA != n2hapB) cout << "-" << (int) segConfs[n2hapB*Mseg64+m64];
cout << " ";
for (uint64 j = 0; j < 64; j++) {
uint64 m64j = m64*64+j;
if (!maskSnps64j[m64j]) continue;
const uint64_masks &bits0 = genoBits[m64*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
bool phase;
if (((haploBits[m64*2*N + n1hap]>>j)&1) == (trioPhase == sign))
phase = 0;
else
phase = 1;
bool hetErr =
((haploBits[m64*2*N + n1hap]>>j)&1) ==
((haploBits[m64*2*N + (j<kSwitch?n2hapA:n2hapB)]>>j)&1);
uchar conf1 = 0, conf2 = 0;
if (hetErr) {
conf1 = phaseConfs[n1hap*Mseg64*64 + m64j];
conf2 = phaseConfs[(j<kSwitch?n2hapA:n2hapB)*Mseg64*64 + m64j];
if (min((int) conf2, 255-conf2) < min((int) conf1, 255-conf1))
phase = !phase;
}
cout << (phase==0?trio1:trio2);
if (hetErr) cout << "?" << "[" << (int) conf1 << "|" << (int) conf2 << "]";
ret.push_back(phase);
}
//cout << endl;
return minScore/*ret*/;
}
vector <bool> Eagle::checkHaploBits(uint64 n0, uint64 nF1, uint64 nF2, uint64 hapBits,
uint64 m64, int pad) const {
vector <bool> ret;
if ((int) nF1 == -1) return ret;
bool isParent = false;
if (((int) nF1) < 0) { // nF1 is the child; n0 is a parent
isParent = true;
nF1 = -nF1;
}
int printed = 0;
for (uint64 j = 0; j < 64; j++) {
uint64 m64j = m64*64+j;
if (!maskSnps64j[m64j]) continue;
const uint64_masks &bits0 = genoBits[m64*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64*N + nF2];
int trioPhase = 0;
if (!isParent) {
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
}
else { // n0 is a parent; nF1 is the child
int g0 = getGeno0123(m64j, nF1); // child
int g2 = getGeno0123(m64j, nF2); // other parent
if (g0+g2 != 2) { // not Mendel error or triple het
if (g0 == 0) trioPhase = -1;
if (g0 == 2) trioPhase = 1;
if (g0 == 1) { // child is a het
if (g2 == 0) trioPhase = 1;
if (g2 == 2) trioPhase = -1;
}
}
}
if (!trioPhase) continue;
if (pad >= 0) {
if (((hapBits>>j)&1) == (trioPhase == 1))
cout << trio1;
else
cout << trio2;
printed++;
}
ret.push_back(((hapBits>>j)&1) == (trioPhase == 1));
}
while (printed < pad) { cout << " "; printed++; }
return ret;
}
pair <uint64, uint64> Eagle::phaseSegHMM(uint64 n0, uint64 n1hap, uint64 n2hapA, uint64 n2hapB,
uint64 m64, uint64 &hetErrMask) const {
uint64 n1is1 = haploBitsT[n1hap*Mseg64 + m64];
uint64 n2is1A = haploBitsT[n2hapA*Mseg64 + m64];
uint64 n2is1B = haploBitsT[n2hapB*Mseg64 + m64];
const uint64_masks &bits0 = genoBits[m64*N + n0];
uint64 wrongHomBitsA = (bits0.is0 & (n1is1 | n2is1A)) | (bits0.is2 & ~(n1is1 & n2is1A));
uint64 wrongHetBitsA = (~(bits0.is0|bits0.is2|bits0.is9) & ~(n1is1 ^ n2is1A));
uint64 wrongHomBitsB = (bits0.is0 & (n1is1 | n2is1B)) | (bits0.is2 & ~(n1is1 & n2is1B));
uint64 wrongHetBitsB = (~(bits0.is0|bits0.is2|bits0.is9) & ~(n1is1 ^ n2is1B));
uint score = popcount64(wrongHomBitsB)*homErrCost
+ popcount64(wrongHetBitsB)*hetErrCost;
uint minScore = score;
double cMdiffMax = m64==0 ? 0.0 : cMs64j[m64*64] - cMs64j[m64*64-1];
uint64 kSwitch = 0;
uint64 kSeg = seg64cMvecs[m64].size();
for (uint64 k = 0; k < kSeg; k++) {
score += ((wrongHomBitsA>>k)&1)*homErrCost + ((wrongHetBitsA>>k)&1)*hetErrCost
- ((wrongHomBitsB>>k)&1)*homErrCost - ((wrongHetBitsB>>k)&1)*hetErrCost;
double cMdiff = (k+1==kSeg ? cMs64j[(m64+1)*64] : cMs64j[m64*64+k+1]) - cMs64j[m64*64+k];
if (score < minScore || (score == minScore && cMdiff > cMdiffMax)) {
minScore = score;
cMdiffMax = cMdiff;
kSwitch = k+1;
}
}
uint64 phaseBits1 = 0, phaseBits2 = 0; hetErrMask = 0;
for (uint64 j = 0; j < 64; j++) {
uint64 m64j = m64*64+j;
if (!maskSnps64j[m64j]) continue;
const uint64_masks &bits0 = genoBits[m64*N + n0];
if (bits0.is0&(1ULL<<j)) // dip = 0: hap1 = hap2 = 0
;
else if (bits0.is2&(1ULL<<j)) { // dip = 2: hap1 = hap2 = 1
phaseBits1 |= 1ULL<<j;
phaseBits2 |= 1ULL<<j;
}
else {
uint64 phase1 = (haploBits[m64*2*N + n1hap]>>j)&1;
uint64 phase2 = ((haploBits[m64*2*N + (j<kSwitch?n2hapA:n2hapB)]>>j)&1);
if (bits0.is9&(1ULL<<j)) { // missing
phaseBits1 |= phase1<<j;
phaseBits2 |= phase2<<j;
}
else { // het
bool phase = phase1;
bool hetErr = phase1 == phase2;
if (hetErr) {
hetErrMask |= 1ULL<<j;
if (Nref == 0) {
uchar conf1 = phaseConfs[n1hap*Mseg64*64 + m64j];
uchar conf2 = phaseConfs[(j<kSwitch?n2hapA:n2hapB)*Mseg64*64 + m64j];
if (min((int) conf2, 255-conf2) < min((int) conf1, 255-conf1))
phase = !phase;
}
}
phaseBits1 |= ((uint64) phase)<<j;
phaseBits2 |= ((uint64) !phase)<<j;
}
}
}
return make_pair(phaseBits1, phaseBits2);
}
vector <bool> Eagle::checkSegPhase(uint64 n0, uint64 nF1, uint64 nF2, uint64 n1hap, uint64 n2hap,
int sign, uint64 m64) const {
vector <bool> ret;
for (uint64 j = 0; j < 64; j++) {
uint64 m64j = m64*64+j;
if (!maskSnps64j[m64j]) continue;
const uint64_masks &bits0 = genoBits[m64*N + n0];
if ((bits0.is0|bits0.is2|bits0.is9)&(1ULL<<j)) continue; // not het
const uint64_masks &bitsF1 = genoBits[m64*N + nF1];
const uint64_masks &bitsF2 = genoBits[m64*N + nF2];
int trioPhase = 0;
if ((bitsF1.is0|bitsF2.is2)&(1ULL<<j)) trioPhase++;
if ((bitsF1.is2|bitsF2.is0)&(1ULL<<j)) trioPhase--;
if (!trioPhase) continue;
/*
int hapBit1 = (haploBits[m64*2*N + n1hap]>>j)&1;
if (sign == -1) hapBit1 = 1-hapBit1;
int hapBit2 = (haploBits[m64*2*N + n2hap]>>j)&1;
if (sign == 1) hapBit2 = 1-hapBit2;
uchar conf1 = phaseConfs[n1hap*Mseg64*64 + m64j];
uchar conf2 = phaseConfs[n2hap*Mseg64*64 + m64j];
//if (hapBit1 != hapBit2) cout << "?[" << (uint) conf1 << "|" << (uint) conf2 << "]";
int hapBitFinal = hapBit1;
if (!(conf1 == 0 || conf1 == 255) && (conf2 == 0 || conf2 == 255)) hapBitFinal = hapBit2;
*/
int hapBitFinal = (int) phaseConfs2[2*n0*Mseg64*64 + m64j] >= 128;
if (hapBitFinal == (trioPhase == 1)) {
cout << trio1;