forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plink_stats.c
2307 lines (2254 loc) · 63.3 KB
/
plink_stats.c
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
#include "plink_common.h"
#include "plink_stats.h"
#include "ipmpar.h"
#include "dcdflib.h"
// 2^{-40} for now, since 2^{-44} was too small on real data
#define FISHER_EPSILON 0.0000000000009094947017729282379150390625
double chiprob_p(double xx, double df) {
int st = 0;
int ww = 1;
double bnd = 1;
double pp;
double qq;
cdfchi(&ww, &pp, &qq, &xx, &df, &st, &bnd);
if (st) {
return -9;
}
return qq;
}
double inverse_chiprob(double qq, double df) {
double pp = 1 - qq;
int32_t st = 0;
int32_t ww = 2;
double bnd = 1;
double xx;
if (qq >= 1.0) {
return 0;
}
cdfchi(&ww, &pp, &qq, &xx, &df, &st, &bnd);
if (st != 0) {
return -9;
}
return xx;
}
double calc_tprob(double tt, double df) {
int32_t st = 0;
int32_t ww = 1;
double bnd = 1;
double pp;
double qq;
if (!realnum(tt)) {
return -9;
}
tt = fabs(tt);
cdft(&ww, &pp, &qq, &tt, &df, &st, &bnd);
if (st != 0) {
return -9;
}
return 2 * qq;
}
double inverse_tprob(double dbl_qq, double df) {
double qq = dbl_qq * 0.5;
double pp = 1 - qq;
int32_t st = 0;
int32_t ww = 2;
double bnd = 1;
double tt;
cdft(&ww, &pp, &qq, &tt, &df, &st, &bnd);
if (st != 0) {
return -9;
}
return tt;
}
// Inverse normal distribution
//
// Lower tail quantile for standard normal distribution function.
//
// This function returns an approximation of the inverse cumulative
// standard normal distribution function. I.e., given P, it returns
// an approximation to the X satisfying P = Pr{Z <= X} where Z is a
// random variable from the standard normal distribution.
//
// The algorithm uses a minimax approximation by rational functions
// and the result has a relative error whose absolute value is less
// than 1.15e-9.
//
// Author: Peter J. Acklam
// Time-stamp: 2002-06-09 18:45:44 +0200
// E-mail: [email protected]
// WWW URL: http://www.math.uio.no/~jacklam
//
// C implementation adapted from Peter's Perl version
// Coefficients in rational approximations.
static const double ivn_a[] =
{
-3.969683028665376e+01,
2.209460984245205e+02,
-2.759285104469687e+02,
1.383577518672690e+02,
-3.066479806614716e+01,
2.506628277459239e+00
};
static const double ivn_b[] =
{
-5.447609879822406e+01,
1.615858368580409e+02,
-1.556989798598866e+02,
6.680131188771972e+01,
-1.328068155288572e+01
};
static const double ivn_c[] =
{
-7.784894002430293e-03,
-3.223964580411365e-01,
-2.400758277161838e+00,
-2.549732539343734e+00,
4.374664141464968e+00,
2.938163982698783e+00
};
static const double ivn_d[] =
{
7.784695709041462e-03,
3.224671290700398e-01,
2.445134137142996e+00,
3.754408661907416e+00
};
#define IVN_LOW 0.02425
#define IVN_HIGH 0.97575
double ltqnorm(double p) {
// assumes 0 < p < 1
double q, r;
if (p < IVN_LOW) {
// Rational approximation for lower region
q = sqrt(-2*log(p));
return (((((ivn_c[0]*q+ivn_c[1])*q+ivn_c[2])*q+ivn_c[3])*q+ivn_c[4])*q+ivn_c[5]) /
((((ivn_d[0]*q+ivn_d[1])*q+ivn_d[2])*q+ivn_d[3])*q+1);
} else if (p > IVN_HIGH) {
// Rational approximation for upper region
q = sqrt(-2*log(1-p));
return -(((((ivn_c[0]*q+ivn_c[1])*q+ivn_c[2])*q+ivn_c[3])*q+ivn_c[4])*q+ivn_c[5]) /
((((ivn_d[0]*q+ivn_d[1])*q+ivn_d[2])*q+ivn_d[3])*q+1);
} else {
// Rational approximation for central region
q = p - 0.5;
r = q*q;
return (((((ivn_a[0]*r+ivn_a[1])*r+ivn_a[2])*r+ivn_a[3])*r+ivn_a[4])*r+ivn_a[5])*q /
(((((ivn_b[0]*r+ivn_b[1])*r+ivn_b[2])*r+ivn_b[3])*r+ivn_b[4])*r+1);
}
}
double SNPHWE2(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, uint32_t midp) {
// This function implements an exact SNP test of Hardy-Weinberg
// Equilibrium as described in Wigginton, JE, Cutler, DJ, and
// Abecasis, GR (2005) A Note on Exact Tests of Hardy-Weinberg
// Equilibrium. American Journal of Human Genetics. 76: 000 - 000.
//
// The original version was written by Jan Wigginton.
//
// This version was written by Christopher Chang. It contains the following
// improvements over the original SNPHWE():
// - Proper handling of >64k genotypes. Previously, there was a potential
// integer overflow.
// - Detection and efficient handling of floating point overflow and
// underflow. E.g. instead of summing a tail all the way down, the loop
// stops once the latest increment underflows the partial sum's 53-bit
// precision; this results in a large speedup when max heterozygote count
// >1k.
// - No malloc() call. It's only necessary to keep track of a few partial
// sums.
// - Support for the mid-p variant of this test. See Graffelman J, Moreno V
// (2013) The mid p-value in exact tests for Hardy-Weinberg equilibrium.
//
// Note that the SNPHWE_t() function below is a lot more efficient for
// testing against a p-value inclusion threshold. SNPHWE2() should only be
// used if you need the actual p-value.
intptr_t obs_homc;
intptr_t obs_homr;
if (obs_hom1 < obs_hom2) {
obs_homc = obs_hom2;
obs_homr = obs_hom1;
} else {
obs_homc = obs_hom1;
obs_homr = obs_hom2;
}
int64_t rare_copies = 2LL * obs_homr + obs_hets;
int64_t genotypes2 = (obs_hets + obs_homc + obs_homr) * 2LL;
int32_t tie_ct = 1;
double curr_hets_t2 = obs_hets;
double curr_homr_t2 = obs_homr;
double curr_homc_t2 = obs_homc;
double tailp = (1 - SMALL_EPSILON) * EXACT_TEST_BIAS;
double centerp = 0;
double lastp2 = tailp;
double lastp1 = tailp;
double curr_hets_t1;
double curr_homr_t1;
double curr_homc_t1;
double preaddp;
if (!genotypes2) {
if (midp) {
return 0.5;
} else {
return 1;
}
}
if (obs_hets * genotypes2 > rare_copies * (genotypes2 - rare_copies)) {
// tail 1 = upper
while (curr_hets_t2 > 1.5) {
// het_probs[curr_hets] = 1
// het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets
curr_homr_t2 += 1;
curr_homc_t2 += 1;
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2);
curr_hets_t2 -= 2;
if (lastp2 < EXACT_TEST_BIAS) {
if (lastp2 > (1 - 2 * SMALL_EPSILON) * EXACT_TEST_BIAS) {
tie_ct++;
}
tailp += lastp2;
break;
}
centerp += lastp2;
if (centerp == INFINITY) {
return 0;
}
}
if ((centerp == 0) && (!midp)) {
return 1;
}
while (curr_hets_t2 > 1.5) {
curr_homr_t2 += 1;
curr_homc_t2 += 1;
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2);
curr_hets_t2 -= 2;
preaddp = tailp;
tailp += lastp2;
if (tailp <= preaddp) {
break;
}
}
curr_hets_t1 = obs_hets + 2;
curr_homr_t1 = obs_homr;
curr_homc_t1 = obs_homc;
while (curr_homr_t1 > 0.5) {
// het_probs[curr_hets + 2] = het_probs[curr_hets] * 4 * curr_homr * curr_homc / ((curr_hets + 2) * (curr_hets + 1))
lastp1 *= (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1));
preaddp = tailp;
tailp += lastp1;
if (tailp <= preaddp) {
break;
}
curr_hets_t1 += 2;
curr_homr_t1 -= 1;
curr_homc_t1 -= 1;
}
} else {
// tail 1 = lower
while (curr_homr_t2 > 0.5) {
curr_hets_t2 += 2;
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1));
curr_homr_t2 -= 1;
curr_homc_t2 -= 1;
if (lastp2 < EXACT_TEST_BIAS) {
if (lastp2 > (1 - 2 * SMALL_EPSILON) * EXACT_TEST_BIAS) {
tie_ct++;
}
tailp += lastp2;
break;
}
centerp += lastp2;
if (centerp == INFINITY) {
return 0;
}
}
if ((centerp == 0) && (!midp)) {
return 1;
}
while (curr_homr_t2 > 0.5) {
curr_hets_t2 += 2;
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1));
curr_homr_t2 -= 1;
curr_homc_t2 -= 1;
preaddp = tailp;
tailp += lastp2;
if (tailp <= preaddp) {
break;
}
}
curr_hets_t1 = obs_hets;
curr_homr_t1 = obs_homr;
curr_homc_t1 = obs_homc;
while (curr_hets_t1 > 1.5) {
curr_homr_t1 += 1;
curr_homc_t1 += 1;
lastp1 *= (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1);
preaddp = tailp;
tailp += lastp1;
if (tailp <= preaddp) {
break;
}
curr_hets_t1 -= 2;
}
}
if (!midp) {
return tailp / (tailp + centerp);
} else {
return (tailp - ((1 - SMALL_EPSILON) * EXACT_TEST_BIAS * 0.5) * tie_ct) / (tailp + centerp);
}
}
int32_t SNPHWE_t(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh) {
// Threshold-test-only version of SNPHWE2() which is usually able to exit
// from the calculation earlier. Returns 0 if these counts are close enough
// to Hardy-Weinberg equilibrium, 1 otherwise.
//
// Suppose, for definiteness, that the number of observed hets is no less
// than expectation. (Same ideas apply for the other case.) We proceed as
// follows:
// - Sum the *relative* likelihoods of more likely smaller het counts.
// - Determine the minimum tail mass to pass the threshold.
// - The majority of the time, the tail boundary elements are enough to pass
// the threshold; we never need to sum the remainder of the tails.
// - And in the case of disequilibrium, we will often be able to immediately
// determine that the tail sum cannot possibly pass the threshold, just by
// looking at the tail boundary elements and using a geometric series to
// upper-bound the tail sums.
// - Only when neither of these conditions hold do we start traveling down
// the tails.
intptr_t obs_homc;
intptr_t obs_homr;
if (obs_hom1 < obs_hom2) {
obs_homc = obs_hom2;
obs_homr = obs_hom1;
} else {
obs_homc = obs_hom1;
obs_homr = obs_hom2;
}
int64_t rare_copies = 2LL * obs_homr + obs_hets;
int64_t genotypes2 = (obs_hets + obs_homc + obs_homr) * 2LL;
double curr_hets_t2 = obs_hets; // tail 2
double curr_homr_t2 = obs_homr;
double curr_homc_t2 = obs_homc;
// Subtract epsilon from initial probability mass, so that we can compare to
// 1 when determining tail vs. center membership without floating point error
// biting us in the ass
double tailp1 = (1 - SMALL_EPSILON) * EXACT_TEST_BIAS;
double centerp = 0;
double lastp2 = tailp1;
double tailp2 = 0;
double tail1_ceil;
double tail2_ceil;
double lastp1;
double curr_hets_t1;
double curr_homr_t1;
double curr_homc_t1;
// Initially, if center sum reaches this, the test can immediately fail.
// Once center is summed, this is recalculated, and when tail sum has reached
// this, we've passed.
double exit_thresh;
double exit_threshx;
double ratio;
double preaddp;
if (!genotypes2) {
return 0;
}
// Convert thresh into reverse odds ratio.
thresh = (1 - thresh) / thresh;
// Expected het count:
// 2 * rarefreq * (1 - rarefreq) * genotypes
// = 2 * (rare_copies / (2 * genotypes)) * (1 - rarefreq) * genotypes
// = rare_copies * (1 - (rare_copies / (2 * genotypes)))
// = (rare_copies * (2 * genotypes - rare_copies)) / (2 * genotypes)
//
// The computational identity is
// P(nhets == n) := P(nhets == n+2) * (n+2) * (n+1) /
// (4 * homr(n) * homc(n))
// where homr() and homc() are the number of homozygous rares/commons needed
// to maintain the same allele frequencies.
// This probability is always decreasing when proceeding away from the
// expected het count.
if (obs_hets * genotypes2 > rare_copies * (genotypes2 - rare_copies)) {
// tail 1 = upper
if (obs_hets < 2) {
return 0;
}
// An initial upper bound on the tail sum is useful, since it lets us
// report test failure before summing the entire center.
// The immediate tail is easy: if the next element out on the tail is r,
// then 1 + r + r^2 + ... = 1 / (1-r) works.
// For the far tail, we currently use the trivial bound of
// 1 + floor[het_exp_floor / 2]
// (each far tail element must be no greater than 1 and there are at
// most that many of them). This bound could be improved, but it might not
// be worth the precomputational effort.
// ...and as long as we're using such a weak bound for the far tail,
// there's no point to carefully calculating the near tail since we're very
// unlikely to use the partial result before exiting from the function.
exit_thresh = rare_copies * thresh * EXACT_TEST_BIAS;
// het_probs[curr_hets] = 1
// het_probs[curr_hets - 2] = het_probs[curr_hets] * curr_hets * (curr_hets - 1) / (4 * (curr_homr + 1) * (curr_homc + 1))
do {
curr_homr_t2 += 1;
curr_homc_t2 += 1;
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2);
curr_hets_t2 -= 2;
if (lastp2 < EXACT_TEST_BIAS) {
tailp2 = lastp2;
break;
}
centerp += lastp2;
if (centerp > exit_thresh) {
return 1;
}
} while (curr_hets_t2 > 1.5);
exit_thresh = centerp / thresh;
if (tailp1 + tailp2 >= exit_thresh) {
return 0;
}
// c + cr + cr^2 + ... = c/(1-r), which is an upper bound for the tail sum
ratio = (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * (curr_homr_t2 + 1) * (curr_homc_t2 + 1));
tail2_ceil = tailp2 / (1 - ratio);
curr_hets_t1 = obs_hets + 2;
curr_homr_t1 = obs_homr;
curr_homc_t1 = obs_homc;
// ratio for the other tail
lastp1 = (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1));
tail1_ceil = tailp1 / (1 - lastp1);
if (tail1_ceil + tail2_ceil < exit_thresh) {
return 1;
}
lastp1 *= tailp1;
tailp1 += lastp1;
if (obs_homr > 1) {
// het_probs[curr_hets + 2] = het_probs[curr_hets] * 4 * curr_homr * curr_homc / ((curr_hets + 2) * (curr_hets + 1))
exit_threshx = exit_thresh - tailp2;
do {
curr_hets_t1 += 2;
curr_homr_t1 -= 1;
curr_homc_t1 -= 1;
lastp1 *= (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1));
preaddp = tailp1;
tailp1 += lastp1;
if (tailp1 > exit_threshx) {
return 0;
}
if (tailp1 <= preaddp) {
break;
}
} while (curr_homr_t1 > 1.5);
}
if (tailp1 + tail2_ceil < exit_thresh) {
return 1;
}
exit_threshx = exit_thresh - tailp1;
while (curr_hets_t2 > 1) {
curr_homr_t2 += 1;
curr_homc_t2 += 1;
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2);
preaddp = tailp2;
tailp2 += lastp2;
if (tailp2 >= exit_threshx) {
return 0;
}
if (tailp2 <= preaddp) {
return 1;
}
curr_hets_t2 -= 2;
}
return 1;
} else {
// tail 1 = lower
if (!obs_homr) {
return 0;
}
exit_thresh = rare_copies * thresh * EXACT_TEST_BIAS;
do {
curr_hets_t2 += 2;
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1));
curr_homr_t2 -= 1;
curr_homc_t2 -= 1;
if (lastp2 < EXACT_TEST_BIAS) {
tailp2 = lastp2;
break;
}
centerp += lastp2;
if (centerp > exit_thresh) {
return 1;
}
} while (curr_homr_t2 > 0.5);
exit_thresh = centerp / thresh;
if (tailp1 + tailp2 >= exit_thresh) {
return 0;
}
ratio = (4 * curr_homr_t2 * curr_homc_t2) / ((curr_hets_t2 + 2) * (curr_hets_t2 + 1));
tail2_ceil = tailp2 / (1 - ratio);
curr_hets_t1 = obs_hets;
curr_homr_t1 = obs_homr + 1;
curr_homc_t1 = obs_homc + 1;
lastp1 = (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1);
tail1_ceil = tailp1 / (1 - lastp1);
lastp1 *= tailp1;
tailp1 += lastp1;
if (tail1_ceil + tail2_ceil < exit_thresh) {
return 1;
}
if (obs_hets >= 4) {
exit_threshx = exit_thresh - tailp2;
do {
curr_hets_t1 -= 2;
curr_homr_t1 += 1;
curr_homc_t1 += 1;
lastp1 *= (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1);
preaddp = tailp1;
tailp1 += lastp1;
if (tailp1 > exit_threshx) {
return 0;
}
if (tailp1 <= preaddp) {
break;
}
} while (curr_hets_t1 > 3.5);
}
if (tailp1 + tail2_ceil < exit_thresh) {
return 1;
}
exit_threshx = exit_thresh - tailp1;
while (curr_homr_t2 > 0.5) {
curr_hets_t2 += 2;
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1));
curr_homr_t2 -= 1;
curr_homc_t2 -= 1;
preaddp = tailp2;
tailp2 += lastp2;
if (tailp2 >= exit_threshx) {
return 0;
}
if (tailp2 <= preaddp) {
return 1;
}
}
return 1;
}
}
int32_t SNPHWE_midp_t(int32_t obs_hets, int32_t obs_hom1, int32_t obs_hom2, double thresh) {
// Mid-p version of SNPHWE_t(). (There are enough fiddly differences that I
// think it's better for this to be a separate function.) Assumes threshold
// is smaller than 0.5.
intptr_t obs_homc;
intptr_t obs_homr;
if (obs_hom1 < obs_hom2) {
obs_homc = obs_hom2;
obs_homr = obs_hom1;
} else {
obs_homc = obs_hom1;
obs_homr = obs_hom2;
}
int64_t rare_copies = 2LL * obs_homr + obs_hets;
int64_t genotypes2 = (obs_hets + obs_homc + obs_homr) * 2LL;
double curr_hets_t2 = obs_hets; // tail 2
double curr_homr_t2 = obs_homr;
double curr_homc_t2 = obs_homc;
double tailp1 = (1 - SMALL_EPSILON) * EXACT_TEST_BIAS * 0.5;
double centerp = tailp1;
double lastp2 = (1 - SMALL_EPSILON) * EXACT_TEST_BIAS;
double tailp2 = 0;
double tail1_ceil;
double tail2_ceil;
double lastp1;
double curr_hets_t1;
double curr_homr_t1;
double curr_homc_t1;
double exit_thresh;
double exit_threshx;
double ratio;
double preaddp;
if (!genotypes2) {
return 0;
}
thresh = (1 - thresh) / thresh;
if (obs_hets * genotypes2 > rare_copies * (genotypes2 - rare_copies)) {
if (obs_hets < 2) {
return 0;
}
exit_thresh = rare_copies * thresh * EXACT_TEST_BIAS;
do {
curr_homr_t2 += 1;
curr_homc_t2 += 1;
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2);
curr_hets_t2 -= 2;
if (lastp2 < EXACT_TEST_BIAS) {
if (lastp2 > (1 - 2 * SMALL_EPSILON) * EXACT_TEST_BIAS) {
// tie with original contingency table, apply mid-p correction here
// too
tailp2 = tailp1;
centerp += tailp1;
} else {
tailp2 = lastp2;
}
break;
}
centerp += lastp2;
if (centerp > exit_thresh) {
return 1;
}
} while (curr_hets_t2 > 1.5);
exit_thresh = centerp / thresh;
if (tailp1 + tailp2 >= exit_thresh) {
return 0;
}
ratio = (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * (curr_homr_t2 + 1) * (curr_homc_t2 + 1));
// this needs to work in both the tie and no-tie cases
tail2_ceil = tailp2 + lastp2 * ratio / (1 - ratio);
curr_hets_t1 = obs_hets + 2;
curr_homr_t1 = obs_homr;
curr_homc_t1 = obs_homc;
lastp1 = (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1));
// always a tie here
tail1_ceil = tailp1 * 2 / (1 - lastp1) - tailp1;
if (tail1_ceil + tail2_ceil < exit_thresh) {
return 1;
}
lastp1 *= tailp1 * 2;
tailp1 += lastp1;
if (obs_homr > 1) {
exit_threshx = exit_thresh - tailp2;
do {
curr_hets_t1 += 2;
curr_homr_t1 -= 1;
curr_homc_t1 -= 1;
lastp1 *= (4 * curr_homr_t1 * curr_homc_t1) / (curr_hets_t1 * (curr_hets_t1 - 1));
preaddp = tailp1;
tailp1 += lastp1;
if (tailp1 > exit_threshx) {
return 0;
}
if (tailp1 <= preaddp) {
break;
}
} while (curr_homr_t1 > 1.5);
}
if (tailp1 + tail2_ceil < exit_thresh) {
return 1;
}
exit_threshx = exit_thresh - tailp1;
while (curr_hets_t2 > 1) {
curr_homr_t2 += 1;
curr_homc_t2 += 1;
lastp2 *= (curr_hets_t2 * (curr_hets_t2 - 1)) / (4 * curr_homr_t2 * curr_homc_t2);
preaddp = tailp2;
tailp2 += lastp2;
if (tailp2 >= exit_threshx) {
return 0;
}
if (tailp2 <= preaddp) {
return 1;
}
curr_hets_t2 -= 2;
}
return 1;
} else {
if (!obs_homr) {
return 0;
}
exit_thresh = rare_copies * thresh * EXACT_TEST_BIAS;
do {
curr_hets_t2 += 2;
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1));
curr_homr_t2 -= 1;
curr_homc_t2 -= 1;
if (lastp2 < EXACT_TEST_BIAS) {
if (lastp2 > (1 - 2 * SMALL_EPSILON) * EXACT_TEST_BIAS) {
tailp2 = tailp1;
centerp += tailp1;
} else {
tailp2 = lastp2;
}
break;
}
centerp += lastp2;
if (centerp > exit_thresh) {
return 1;
}
} while (curr_homr_t2 > 0.5);
exit_thresh = centerp / thresh;
if (tailp1 + tailp2 >= exit_thresh) {
return 0;
}
ratio = (4 * curr_homr_t2 * curr_homc_t2) / ((curr_hets_t2 + 2) * (curr_hets_t2 + 1));
tail2_ceil = tailp2 + lastp2 * ratio / (1 - ratio);
curr_hets_t1 = obs_hets;
curr_homr_t1 = obs_homr + 1;
curr_homc_t1 = obs_homc + 1;
lastp1 = (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1);
tail1_ceil = 2 * tailp1 / (1 - lastp1) - tailp1;
lastp1 *= 2 * tailp1;
tailp1 += lastp1;
if (tail1_ceil + tail2_ceil < exit_thresh) {
return 1;
}
if (obs_hets >= 4) {
exit_threshx = exit_thresh - tailp2;
do {
curr_hets_t1 -= 2;
curr_homr_t1 += 1;
curr_homc_t1 += 1;
lastp1 *= (curr_hets_t1 * (curr_hets_t1 - 1)) / (4 * curr_homr_t1 * curr_homc_t1);
preaddp = tailp1;
tailp1 += lastp1;
if (tailp1 > exit_threshx) {
return 0;
}
if (tailp1 <= preaddp) {
break;
}
} while (curr_hets_t1 > 3.5);
}
if (tailp1 + tail2_ceil < exit_thresh) {
return 1;
}
exit_threshx = exit_thresh - tailp1;
while (curr_homr_t2 > 0.5) {
curr_hets_t2 += 2;
lastp2 *= (4 * curr_homr_t2 * curr_homc_t2) / (curr_hets_t2 * (curr_hets_t2 - 1));
curr_homr_t2 -= 1;
curr_homc_t2 -= 1;
preaddp = tailp2;
tailp2 += lastp2;
if (tailp2 >= exit_threshx) {
return 0;
}
if (tailp2 <= preaddp) {
return 1;
}
}
return 1;
}
}
double fisher22(uint32_t m11, uint32_t m12, uint32_t m21, uint32_t m22, uint32_t midp) {
// Basic 2x2 Fisher exact test p-value calculation.
double tprob = (1 - FISHER_EPSILON) * EXACT_TEST_BIAS;
double cur_prob = tprob;
double cprob = 0;
int32_t tie_ct = 1;
uint32_t uii;
double cur11;
double cur12;
double cur21;
double cur22;
double preaddp;
// Ensure we are left of the distribution center, m11 <= m22, and m12 <= m21.
if (m12 > m21) {
uii = m12;
m12 = m21;
m21 = uii;
}
if (m11 > m22) {
uii = m11;
m11 = m22;
m22 = uii;
}
if ((((uint64_t)m11) * m22) > (((uint64_t)m12) * m21)) {
uii = m11;
m11 = m12;
m12 = uii;
uii = m21;
m21 = m22;
m22 = uii;
}
cur11 = m11;
cur12 = m12;
cur21 = m21;
cur22 = m22;
while (cur12 > 0.5) {
cur11 += 1;
cur22 += 1;
cur_prob *= (cur12 * cur21) / (cur11 * cur22);
cur12 -= 1;
cur21 -= 1;
if (cur_prob == INFINITY) {
return 0;
}
if (cur_prob < EXACT_TEST_BIAS) {
if (cur_prob > (1 - 2 * FISHER_EPSILON) * EXACT_TEST_BIAS) {
tie_ct++;
}
tprob += cur_prob;
break;
}
cprob += cur_prob;
}
if ((cprob == 0) && (!midp)) {
return 1;
}
while (cur12 > 0.5) {
cur11 += 1;
cur22 += 1;
cur_prob *= (cur12 * cur21) / (cur11 * cur22);
cur12 -= 1;
cur21 -= 1;
preaddp = tprob;
tprob += cur_prob;
if (tprob <= preaddp) {
break;
}
}
if (m11) {
cur11 = m11;
cur12 = m12;
cur21 = m21;
cur22 = m22;
cur_prob = (1 - FISHER_EPSILON) * EXACT_TEST_BIAS;
do {
cur12 += 1;
cur21 += 1;
cur_prob *= (cur11 * cur22) / (cur12 * cur21);
cur11 -= 1;
cur22 -= 1;
preaddp = tprob;
tprob += cur_prob;
if (tprob <= preaddp) {
if (!midp) {
return preaddp / (cprob + preaddp);
} else {
return (preaddp - ((1 - FISHER_EPSILON) * EXACT_TEST_BIAS * 0.5) * tie_ct) / (cprob + preaddp);
}
}
} while (cur11 > 0.5);
}
if (!midp) {
return tprob / (cprob + tprob);
} else {
return (tprob - ((1 - FISHER_EPSILON) * EXACT_TEST_BIAS * 0.5) * tie_ct) / (cprob + tprob);
}
}
double fisher22_tail_pval(uint32_t m11, uint32_t m12, uint32_t m21, uint32_t m22, int32_t right_offset, double tot_prob_recip, double right_prob, uint32_t midp, uint32_t new_m11) {
// Given that the left (w.r.t. m11) reference contingency table has
// likelihood 1/tot_prob, the contingency table with m11 increased by
// right_offset has likelihood right_prob/tot_prob, and the tails (up to but
// not including the two references) sum to tail_sum/tot_prob, this
// calculates the p-value of the given m11 (which must be on one tail).
double left_prob = 1.0;
double dxx = ((intptr_t)new_m11);
double cur11;
double cur12;
double cur21;
double cur22;
double psum;
double thresh;
if (new_m11 < m11) {
cur11 = ((intptr_t)m11);
cur12 = ((intptr_t)m12);
cur21 = ((intptr_t)m21);
cur22 = ((intptr_t)m22);
dxx += 0.5; // unnecessary (53 vs. 32 bits precision), but whatever
do {
cur12 += 1;
cur21 += 1;
left_prob *= cur11 * cur22 / (cur12 * cur21);
cur11 -= 1;
cur22 -= 1;
} while (cur11 > dxx);
if (left_prob == 0) {
return 0;
}
if (!midp) {
psum = left_prob;
} else {
psum = left_prob * 0.5;
}
thresh = left_prob * (1 + FISHER_EPSILON);
do {
if (cur11 < 0.5) {
break;
}
cur12 += 1;
cur21 += 1;
left_prob *= cur11 * cur22 / (cur12 * cur21);
cur11 -= 1;
cur22 -= 1;
dxx = psum;
psum += left_prob;
} while (psum > dxx);
cur11 = ((intptr_t)(m11 + right_offset));
cur12 = ((intptr_t)(m12 - right_offset));
cur21 = ((intptr_t)(m21 - right_offset));
cur22 = ((intptr_t)(m22 + right_offset));
while (right_prob > thresh) {
cur11 += 1;
cur22 += 1;
right_prob *= cur12 * cur21 / (cur11 * cur22);
cur12 -= 1;
cur21 -= 1;
}
if (right_prob > 0) {
if (midp && (right_prob < thresh * (1 - 2 * FISHER_EPSILON))) {
psum += right_prob * 0.5;
} else {
psum += right_prob;
}
do {
cur11 += 1;
cur22 += 1;
right_prob *= cur12 * cur21 / (cur11 * cur22);
cur12 -= 1;
cur21 -= 1;
dxx = psum;
psum += right_prob;
} while (psum > dxx);
}
} else {
dxx -= 0.5;
cur11 = ((intptr_t)(m11 + right_offset));
cur12 = ((intptr_t)(m12 - right_offset));
cur21 = ((intptr_t)(m21 - right_offset));
cur22 = ((intptr_t)(m22 + right_offset));
do {
cur11 += 1;
cur22 += 1;
right_prob *= cur12 * cur21 / (cur11 * cur22);
cur12 -= 1;
cur21 -= 1;
} while (cur11 < dxx);
if (right_prob == 0) {
return 0;
}
if (!midp) {
psum = right_prob;
} else {
psum = right_prob * 0.5;
}
thresh = right_prob * (1 + FISHER_EPSILON);
do {
if (cur12 < 0.5) {
break;
}
cur11 += 1;
cur22 += 1;
right_prob *= cur12 * cur21 / (cur11 * cur22);
cur12 -= 1;
cur21 -= 1;
dxx = psum;
psum += right_prob;
} while (psum > dxx);
cur11 = ((intptr_t)m11);
cur12 = ((intptr_t)m12);
cur21 = ((intptr_t)m21);
cur22 = ((intptr_t)m22);
while (left_prob > thresh) {
cur12 += 1;
cur21 += 1;
left_prob *= cur11 * cur22 / (cur12 * cur21);
cur11 -= 1;
cur22 -= 1;
}
if (left_prob > 0) {
if (midp && (left_prob < thresh * (1 - 2 * FISHER_EPSILON))) {
psum += left_prob * 0.5;
} else {
psum += left_prob;
}
do {
cur12 += 1;
cur21 += 1;
left_prob *= cur11 * cur22 / (cur12 * cur21);
cur11 -= 1;
cur22 -= 1;
dxx = psum;
psum += left_prob;
} while (psum > dxx);
}
}
return psum * tot_prob_recip;
}
void fisher22_precomp_pval_bounds(double pval, uint32_t midp, uint32_t row1_sum, uint32_t col1_sum, uint32_t total, uint32_t* bounds, double* tprobs) {
// bounds[0] = m11 min
// bounds[1] = m11 (max + 1)
// bounds[2] = m11 min after including ties
// bounds[3] = m11 (max + 1) after including ties