forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplink_assoc.c
12995 lines (12821 loc) · 464 KB
/
plink_assoc.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_assoc.h"
#include "plink_cluster.h"
#include "plink_ld.h"
#include "plink_matrix.h"
#include "plink_stats.h"
void aperm_init(Aperm_info* apip) {
apip->min = 6;
apip->max = 1000000;
apip->alpha = 0;
apip->beta = 0.0001;
apip->init_interval = 1;
apip->interval_slope = 0.001;
}
void single_marker_cc_freqs(uintptr_t sample_ctl2, uintptr_t* lptr, uintptr_t* ctrl_include2, uintptr_t* case_include2, uint32_t* ctrl_setp, uint32_t* ctrl_missingp, uint32_t* case_setp, uint32_t* case_missingp) {
// Counts the number of A2 alleles and missing calls for both cases and
// controls, for an autosomal marker. (The caller is expected to calculate
// the A1 allele count.)
// See single_marker_freqs_and_hwe() for discussion.
// A := genotype & 0x5555...
// B := (genotype >> 1) & 0x5555...
// C := A & (~B)
// missing: popcount(C)
// A2: [popcount(A) + popcount(B)] - popcount(C)
uint32_t tot_ctrl_ab = 0;
uint32_t tot_ctrl_c = 0;
uint32_t tot_case_ab = 0;
uint32_t tot_case_c = 0;
uintptr_t* lptr_end = &(lptr[sample_ctl2]);
uintptr_t loader;
uintptr_t loader2;
uintptr_t loader3;
uintptr_t loader4;
#ifdef __LP64__
uintptr_t cur_decr = 60;
uintptr_t* lptr_6x_end;
sample_ctl2 -= sample_ctl2 % 6;
while (sample_ctl2 >= 60) {
single_marker_cc_freqs_loop:
lptr_6x_end = &(lptr[cur_decr]);
count_2freq_dbl_60v((__m128i*)lptr, (__m128i*)lptr_6x_end, (__m128i*)ctrl_include2, (__m128i*)case_include2, &tot_ctrl_ab, &tot_ctrl_c, &tot_case_ab, &tot_case_c);
lptr = lptr_6x_end;
ctrl_include2 = &(ctrl_include2[cur_decr]);
case_include2 = &(case_include2[cur_decr]);
sample_ctl2 -= cur_decr;
}
if (sample_ctl2) {
cur_decr = sample_ctl2;
goto single_marker_cc_freqs_loop;
}
#else
uintptr_t* lptr_six_end = &(lptr[sample_ctl2 - (sample_ctl2 % 6)]);
while (lptr < lptr_six_end) {
count_2freq_dbl_6(lptr, ctrl_include2, case_include2, &tot_ctrl_ab, &tot_ctrl_c, &tot_case_ab, &tot_case_c);
lptr = &(lptr[6]);
ctrl_include2 = &(ctrl_include2[6]);
case_include2 = &(case_include2[6]);
}
#endif
while (lptr < lptr_end) {
loader = *lptr++;
loader2 = *ctrl_include2++;
loader3 = loader >> 1;
loader4 = loader2 & loader;
tot_ctrl_ab += popcount2_long(loader4 + (loader3 & loader2));
tot_ctrl_c += popcount2_long(loader4 & (~loader3));
loader2 = *case_include2++;
loader4 = loader2 & loader;
tot_case_ab += popcount2_long(loader4 + (loader3 & loader2));
tot_case_c += popcount2_long(loader4 & (~loader3));
}
*ctrl_missingp = tot_ctrl_c;
*ctrl_setp = tot_ctrl_ab - tot_ctrl_c;
*case_missingp = tot_case_c;
*case_setp = tot_case_ab - tot_case_c;
}
void haploid_single_marker_cc_freqs(uintptr_t sample_ctl2, uintptr_t* lptr, uintptr_t* ctrl_include2, uintptr_t* case_include2, uint32_t* ctrl_setp, uint32_t* ctrl_missingp, uint32_t* case_setp, uint32_t* case_missingp) {
// Counts the number of A1 and A2 alleles for both cases and controls, for a
// haploid marker.
// A := genotype & 0x5555...
// B := (genotype >> 1) & 0x5555...
// C := B ^ A
// missing: popcount(C)
// A2: popcount(A & B)
uint32_t tot_ctrl_ab = 0;
uint32_t tot_ctrl_c = 0;
uint32_t tot_case_ab = 0;
uint32_t tot_case_c = 0;
uintptr_t* lptr_end = &(lptr[sample_ctl2]);
uintptr_t loader;
uintptr_t loader2;
uintptr_t loader3;
while (lptr < lptr_end) {
loader = *lptr++;
loader2 = loader >> 1;
loader3 = loader2 ^ loader;
loader &= loader2;
loader2 = *ctrl_include2++;
tot_ctrl_ab += popcount2_long(loader & loader2);
tot_ctrl_c += popcount2_long(loader3 & loader2);
loader2 = *case_include2++;
tot_case_ab += popcount2_long(loader & loader2);
tot_case_c += popcount2_long(loader3 & loader2);
}
*ctrl_setp = tot_ctrl_ab;
*ctrl_missingp = tot_ctrl_c;
*case_setp = tot_case_ab;
*case_missingp = tot_case_c;
}
void single_marker_cc_3freqs(uintptr_t sample_ctl2, uintptr_t* lptr, uintptr_t* ctrl_include2, uintptr_t* case_include2, uint32_t* ctrl_hom2p, uint32_t* ctrl_hetp, uint32_t* ctrl_missingp, uint32_t* case_hom2p, uint32_t* case_hetp, uint32_t* case_missingp) {
// Counts the number of heterozygotes, A2 homozygotes, and missing calls for
// both cases and controls. Assumes marker is diploid. The caller is
// expected to calculate the A1 allele count.
// See single_marker_freqs_and_hwe() for discussion.
// A := genotype & 0x5555...
// B := (genotype >> 1) & 0x5555...
// C := A & B
// popcount(C) = homozyg major ct
// popcount(B) = het ct + homozyg major ct
// popcount(A) = missing_ct + homozyg major ct
// hom2: popcount(C)
// het: popcount(B) - popcount(C)
// missing: popcount(A) - popcount(C)
uint32_t tot_ctrl_a = 0;
uint32_t tot_ctrl_b = 0;
uint32_t tot_ctrl_c = 0;
uint32_t tot_case_a = 0;
uint32_t tot_case_b = 0;
uint32_t tot_case_c = 0;
uintptr_t* lptr_end = &(lptr[sample_ctl2]);
uintptr_t loader;
uintptr_t loader2;
uintptr_t loader3;
#ifdef __LP64__
uintptr_t cur_decr = 120;
uintptr_t* lptr_12x_end;
sample_ctl2 -= sample_ctl2 % 12;
while (sample_ctl2 >= 120) {
single_marker_cc_3freqs_loop:
lptr_12x_end = &(lptr[cur_decr]);
count_3freq_120v((__m128i*)lptr, (__m128i*)lptr_12x_end, (__m128i*)ctrl_include2, &tot_ctrl_a, &tot_ctrl_b, &tot_ctrl_c);
count_3freq_120v((__m128i*)lptr, (__m128i*)lptr_12x_end, (__m128i*)case_include2, &tot_case_a, &tot_case_b, &tot_case_c);
lptr = lptr_12x_end;
ctrl_include2 = &(ctrl_include2[cur_decr]);
case_include2 = &(case_include2[cur_decr]);
sample_ctl2 -= cur_decr;
}
if (sample_ctl2) {
cur_decr = sample_ctl2;
goto single_marker_cc_3freqs_loop;
}
#else
uintptr_t* lptr_twelve_end = &(lptr[sample_ctl2 - (sample_ctl2 % 12)]);
while (lptr < lptr_twelve_end) {
count_3freq_12(lptr, ctrl_include2, &tot_ctrl_a, &tot_ctrl_b, &tot_ctrl_c);
count_3freq_12(lptr, case_include2, &tot_case_a, &tot_case_b, &tot_case_c);
lptr = &(lptr[12]);
ctrl_include2 = &(ctrl_include2[12]);
case_include2 = &(case_include2[12]);
}
#endif
while (lptr < lptr_end) {
// A := genotype & 0x5555...
// B := (genotype >> 1) & 0x5555...
// C := A & B
// popcount(C) = homozyg major ct
// popcount(B) = het ct + homozyg major ct
// popcount(A) = missing_ct + homozyg major ct
loader = *lptr++;
loader2 = *ctrl_include2++;
loader3 = (loader >> 1) & loader2;
loader2 &= loader;
tot_ctrl_a += popcount2_long(loader2);
tot_ctrl_b += popcount2_long(loader3);
tot_ctrl_c += popcount2_long(loader2 & loader3);
loader2 = *case_include2++;
loader3 = (loader >> 1) & loader2;
loader2 &= loader;
tot_case_a += popcount2_long(loader2);
tot_case_b += popcount2_long(loader3);
tot_case_c += popcount2_long(loader2 & loader3);
}
*ctrl_hom2p = tot_ctrl_c;
*ctrl_hetp = tot_ctrl_b - tot_ctrl_c;
*ctrl_missingp = tot_ctrl_a - tot_ctrl_c;
*case_hom2p = tot_case_c;
*case_hetp = tot_case_b - tot_case_c;
*case_missingp = tot_case_a - tot_case_c;
}
static inline void adjust_print(double pval, double output_min_p, const char* output_min_p_str, uint32_t output_min_p_strlen, char** bufpp) {
if (pval < 0) {
*bufpp = memcpya(*bufpp, " NA ", 11);
} else if (pval <= output_min_p) {
*bufpp = memcpya(*bufpp, output_min_p_str, output_min_p_strlen);
} else {
*bufpp = double_g_writewx4x(*bufpp, pval, 10, ' ');
}
}
static inline void adjust_print_log10(double pval, double output_min_p, const char* output_min_logp_str, uint32_t output_min_logp_strlen, char** bufpp) {
if (pval < 0) {
*bufpp = memcpya(*bufpp, " NA ", 11);
} else if (pval <= output_min_p) {
*bufpp = memcpya(*bufpp, output_min_logp_str, output_min_logp_strlen);
} else if (pval < 1) {
*bufpp = double_g_writewx4x(*bufpp, -log10(pval), 10, ' ');
} else {
*bufpp = memcpya(*bufpp, " 0 ", 11);
}
}
int32_t multcomp(char* outname, char* outname_end, uint32_t* marker_uidxs, uintptr_t chi_ct, char* marker_ids, uintptr_t max_marker_id_len, uint32_t plink_maxsnp, Chrom_info* chrom_info_ptr, double* chi, double pfilter, double output_min_p, uint32_t mtest_adjust, uint32_t skip_gc, double adjust_lambda, uint32_t* tcnt, double* pvals) {
// Association statistics can be provided in three ways:
// 1. Just p-values (pvals[]).
// 2. T statistics (in chi[]) and dfs (in tcnt[]).
// 3. 1df chi-square stats (in chi[]).
unsigned char* wkspace_mark = wkspace_base;
uint32_t is_log10 = mtest_adjust & ADJUST_LOG10;
uint32_t qq_plot = mtest_adjust & ADJUST_QQ;
FILE* outfile = NULL;
double pv_holm = 0.0;
double pv_sidak_sd = 0;
int32_t retval = 0;
uint32_t is_set_test = !plink_maxsnp;
uint32_t adjust_gc = (mtest_adjust & ADJUST_GC) && (!skip_gc);
uint32_t output_min_p_strlen = 11;
uint32_t uii = 0;
uint32_t* new_tcnt = NULL;
double* unadj = NULL;
char output_min_p_str[16];
uint32_t pct;
double* sp;
double* schi;
double* pv_bh;
double* pv_by;
uint32_t* new_order;
uint32_t cur_idx;
uintptr_t marker_uidx;
double dxx;
double dyy;
double dzz;
double harmonic_sum;
double dct;
double pval;
double unadj_pval;
double* pv_gc;
double lambda_recip;
double bonf;
double pv_sidak_ss;
char* bufptr;
uint32_t ujj;
uint32_t loop_end;
if (wkspace_alloc_d_checked(&sp, chi_ct * sizeof(double)) ||
wkspace_alloc_d_checked(&schi, chi_ct * sizeof(double)) ||
wkspace_alloc_ui_checked(&new_order, chi_ct * sizeof(int32_t))) {
goto multcomp_ret_NOMEM;
}
if (pvals) {
for (cur_idx = 0; cur_idx < chi_ct; cur_idx++) {
dxx = pvals[cur_idx];
dyy = inverse_chiprob(dxx, 1);
if (dyy >= 0) {
sp[uii] = dxx;
new_order[uii] = marker_uidxs[cur_idx];
schi[uii] = dyy;
uii++;
}
}
} else if (tcnt) {
if (wkspace_alloc_ui_checked(&new_tcnt, chi_ct * sizeof(int32_t))) {
goto multcomp_ret_NOMEM;
}
for (cur_idx = 0; cur_idx < chi_ct; cur_idx++) {
ujj = tcnt[cur_idx];
if (ujj) {
dxx = chi[cur_idx]; // not actually squared
dyy = calc_tprob(dxx, ujj);
if (dyy > -1) {
sp[uii] = dyy;
new_order[uii] = marker_uidxs[cur_idx];
schi[uii] = dxx * dxx;
new_tcnt[uii] = ujj;
uii++;
}
}
}
} else {
for (cur_idx = 0; cur_idx < chi_ct; cur_idx++) {
dxx = chi[cur_idx];
if (dxx >= 0) {
dyy = chiprob_p(dxx, 1);
if (dyy > -1) {
sp[uii] = dyy;
new_order[uii] = marker_uidxs[cur_idx];
schi[uii] = dxx;
uii++;
}
}
}
}
chi_ct = uii;
if (!chi_ct) {
logprint("Zero valid tests; --adjust skipped.\n");
goto multcomp_ret_1;
}
if (qsort_ext((char*)sp, chi_ct, sizeof(double), double_cmp_deref, (char*)new_order, sizeof(int32_t))) {
goto multcomp_ret_NOMEM;
}
if (tcnt) {
if (qsort_ext((char*)schi, chi_ct, sizeof(double), double_cmp_deref, (char*)new_tcnt, sizeof(int32_t))) {
goto multcomp_ret_NOMEM;
}
} else {
#ifdef __cplusplus
std::sort(schi, &(schi[chi_ct]));
#else
qsort(schi, chi_ct, sizeof(double), double_cmp);
#endif
}
dct = chi_ct;
// get lambda...
if (skip_gc) {
lambda_recip = 1.0;
} else if (mtest_adjust & ADJUST_LAMBDA) {
lambda_recip = adjust_lambda;
} else {
if (chi_ct & 1) {
lambda_recip = schi[(chi_ct - 1) / 2];
} else {
lambda_recip = (schi[chi_ct / 2 - 1] + schi[chi_ct / 2]) * 0.5;
}
lambda_recip = lambda_recip / 0.456;
if (lambda_recip < 1.0) {
lambda_recip = 1.0;
}
LOGPRINTF("--adjust: Genomic inflation est. lambda (based on median chisq) = %g.\n", lambda_recip);
}
// ...now take the reciprocal (bugfix: forgot to do this with --lambda)
if (lambda_recip > 1.0) {
lambda_recip = 1.0 / lambda_recip;
}
// handle reverse-order calculations
if (wkspace_alloc_d_checked(&pv_bh, chi_ct * sizeof(double)) ||
wkspace_alloc_d_checked(&pv_by, chi_ct * sizeof(double)) ||
wkspace_alloc_d_checked(&pv_gc, chi_ct * sizeof(double))) {
goto multcomp_ret_NOMEM;
}
if (adjust_gc) {
unadj = sp;
sp = pv_gc;
}
uii = chi_ct;
if (tcnt) {
for (cur_idx = 0; cur_idx < chi_ct; cur_idx++) {
uii--;
pv_gc[cur_idx] = calc_tprob(sqrt(schi[uii] * lambda_recip), new_tcnt[uii]);
}
} else {
for (cur_idx = 0; cur_idx < chi_ct; cur_idx++) {
pv_gc[cur_idx] = chiprob_p(schi[--uii] * lambda_recip, 1);
}
}
dyy = sp[chi_ct - 1];
pv_bh[chi_ct - 1] = dyy;
harmonic_sum = 1.0;
for (cur_idx = chi_ct - 1; cur_idx > 0; cur_idx--) {
dzz = dct / ((double)((int32_t)cur_idx));
harmonic_sum += dzz;
dxx = dzz * sp[cur_idx - 1];
if (dyy > dxx) {
dyy = dxx;
}
pv_bh[cur_idx - 1] = dyy;
}
dzz = 1.0 / dct;
harmonic_sum *= dzz;
dyy = harmonic_sum * sp[chi_ct - 1];
if (dyy >= 1) {
dyy = 1;
}
pv_by[chi_ct - 1] = dyy;
harmonic_sum *= dct;
for (cur_idx = chi_ct - 1; cur_idx > 0; cur_idx--) {
dxx = (harmonic_sum / ((double)((int32_t)cur_idx))) * sp[cur_idx - 1];
if (dyy > dxx) {
dyy = dxx;
}
pv_by[cur_idx - 1] = dyy;
}
uii = strlen(outname_end);
memcpy(&(outname_end[uii]), ".adjusted", 10);
if (fopen_checked(&outfile, outname, "w")) {
goto multcomp_ret_OPEN_FAIL;
}
if (!is_set_test) {
sprintf(tbuf, " CHR %%%us UNADJ %s", plink_maxsnp, skip_gc? "" : " GC ");
fprintf(outfile, tbuf, "SNP");
} else {
plink_maxsnp = max_marker_id_len - 1;
if (plink_maxsnp < 3) {
plink_maxsnp = 3;
}
sprintf(tbuf, " %%%us UNADJ ", plink_maxsnp);
fprintf(outfile, tbuf, "SET");
}
if (qq_plot) {
fputs(" QQ ", outfile);
}
if (fputs_checked(" BONF HOLM SIDAK_SS SIDAK_SD FDR_BH FDR_BY\n", outfile)) {
goto multcomp_ret_WRITE_FAIL;
}
fputs("0%", stdout);
fflush(stdout);
cur_idx = 0;
if (!is_log10) {
if (output_min_p == 0.0) {
memcpy(output_min_p_str, " INF ", 11);
} else {
bufptr = double_g_writewx4x(output_min_p_str, output_min_p, 10, ' ');
output_min_p_strlen = (uintptr_t)(bufptr - output_min_p_str);
}
} else {
if (output_min_p == 0.0) {
memcpy(output_min_p_str, " INF ", 11);
} else {
bufptr = double_g_writewx4x(output_min_p_str, -log10(output_min_p), 10, ' ');
output_min_p_strlen = (uintptr_t)(bufptr - output_min_p_str);
}
}
for (pct = 1; pct <= 100; pct++) {
loop_end = (((uint64_t)pct) * chi_ct) / 100LLU;
for (; cur_idx < loop_end; cur_idx++) {
pval = sp[cur_idx];
// if --pfilter specified, filter out both nan and negative pvals, since
// both are currently used by upstream functions
if ((pfilter != 2.0) && ((!(pval >= 0.0)) || (pval > pfilter))) {
continue;
}
if (adjust_gc) {
unadj_pval = unadj[cur_idx];
} else {
unadj_pval = pval;
}
marker_uidx = new_order[cur_idx];
if (!is_set_test) {
bufptr = width_force(4, tbuf, chrom_name_write(tbuf, chrom_info_ptr, get_marker_chrom(chrom_info_ptr, marker_uidx)));
} else {
bufptr = tbuf;
}
*bufptr++ = ' ';
bufptr = fw_strcpy(plink_maxsnp, &(marker_ids[marker_uidx * max_marker_id_len]), bufptr);
*bufptr++ = ' ';
if (fwrite_checked(tbuf, bufptr - tbuf, outfile)) {
goto multcomp_ret_WRITE_FAIL;
}
bonf = pval * dct;
if (bonf > 1) {
bonf = 1;
}
if (pv_holm < 1) {
dyy = (chi_ct - cur_idx) * pval;
if (dyy > 1) {
pv_holm = 1;
} else if (pv_holm < dyy) {
pv_holm = dyy;
}
}
// avoid catastrophic cancellation for small p-values
// 1 - (1-p)^c = 1 - (1 - cp + (c(c-1) / 2)p^2 + ...)
// = cp - (c(c-1) / 2)p^2 + [stuff smaller than (c^3p^3)/6]
// current threshold is chosen to ensure at least 4 digits of precision
// in (1 - pval) if pow() is used, since 4 significant digits are printed
// in the .adjusted file. but in theory we should take dct into account
// too: small dct lets us use a higher threshold.
if (pval >= RECIP_2_53 * 16384) {
pv_sidak_ss = 1 - pow(1 - pval, dct);
dyy = 1 - pow(1 - pval, dct - ((double)((int32_t)cur_idx)));
} else {
// for very large dct, we might want to include the p^3 term of the
// binomial expansion as well
pv_sidak_ss = pval * (dct - pval * (dct - 1));
dyy = dct - (double)((int32_t)cur_idx);
dyy = pval * (dyy - pval * (dyy - 1));
}
if (pv_sidak_sd < dyy) {
pv_sidak_sd = dyy;
}
bufptr = tbuf;
if (!is_log10) {
adjust_print(unadj_pval, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
if (!skip_gc) {
adjust_print(pv_gc[cur_idx], output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
}
if (qq_plot) {
bufptr = double_g_writewx4x(bufptr, (((double)((int32_t)cur_idx)) + 0.5) * dzz, 10, ' ');
}
adjust_print(bonf, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print(pv_holm, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print(pv_sidak_ss, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print(pv_sidak_sd, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print(pv_bh[cur_idx], output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print(pv_by[cur_idx], output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
} else {
adjust_print_log10(pval, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
if (!is_set_test) {
adjust_print_log10(pv_gc[cur_idx], output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
}
if (qq_plot) {
bufptr = double_g_writewx4x(bufptr, (((double)((int32_t)cur_idx)) + 0.5) * dzz, 10, ' ');
}
adjust_print_log10(bonf, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print_log10(pv_holm, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print_log10(pv_sidak_ss, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print_log10(pv_sidak_sd, output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print_log10(pv_bh[cur_idx], output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
adjust_print_log10(pv_by[cur_idx], output_min_p, output_min_p_str, output_min_p_strlen, &bufptr);
}
*bufptr++ = '\n';
if (fwrite_checked(tbuf, bufptr - tbuf, outfile)) {
goto multcomp_ret_WRITE_FAIL;
}
}
if (pct < 100) {
if (pct > 10) {
putchar('\b');
}
printf("\b\b%u%%", pct);
fflush(stdout);
}
}
fputs("\b\b\b", stdout);
LOGPRINTFWW("--adjust values (%" PRIuPTR " %s%s) written to %s .\n", chi_ct, is_set_test? "nonempty set" : "variant", (chi_ct == 1)? "" : "s", outname);
while (0) {
multcomp_ret_NOMEM:
retval = RET_NOMEM;
break;
multcomp_ret_OPEN_FAIL:
retval = RET_OPEN_FAIL;
break;
multcomp_ret_WRITE_FAIL:
retval = RET_WRITE_FAIL;
break;
}
multcomp_ret_1:
fclose_cond(outfile);
wkspace_reset(wkspace_mark);
return retval;
}
void generate_cc_perm_vec(uint32_t tot_ct, uint32_t set_ct, uint32_t tot_quotient, uint64_t totq_magic, uint32_t totq_preshift, uint32_t totq_postshift, uint32_t totq_incr, uintptr_t* perm_vec, sfmt_t* sfmtp) {
// Assumes tot_quotient is 2^32 / tot_ct, and
// totq_magic/totq_preshift/totq_postshift/totq_incr have been precomputed
// from magic_num().
uint32_t num_set = 0;
uint32_t upper_bound = tot_ct * tot_quotient - 1;
uintptr_t widx;
uintptr_t wcomp;
uintptr_t pv_val;
uint32_t urand;
uint32_t uii;
if (set_ct * 2 < tot_ct) {
fill_ulong_zero(perm_vec, 2 * ((tot_ct + (BITCT - 1)) / BITCT));
for (; num_set < set_ct; num_set++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = (totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift;
widx = uii / BITCT2;
wcomp = ONELU << (2 * (uii % BITCT2));
pv_val = perm_vec[widx];
} while (pv_val & wcomp);
perm_vec[widx] = pv_val | wcomp;
}
} else {
fill_vec_55(perm_vec, tot_ct);
set_ct = tot_ct - set_ct;
for (; num_set < set_ct; num_set++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = (totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift;
widx = uii / BITCT2;
wcomp = ONELU << (2 * (uii % BITCT2));
pv_val = perm_vec[widx];
} while (!(pv_val & wcomp));
perm_vec[widx] = pv_val - wcomp;
}
}
}
void generate_cc_perm1(uint32_t tot_ct, uint32_t set_ct, uint32_t tot_quotient, uint64_t totq_magic, uint32_t totq_preshift, uint32_t totq_postshift, uint32_t totq_incr, uintptr_t* perm_vec, sfmt_t* sfmtp) {
// generate_cc_perm_vec() variant which uses 1-bit packing instead of 2.
uint32_t num_set = 0;
uint32_t upper_bound = tot_ct * tot_quotient - 1;
uintptr_t widx;
uintptr_t wcomp;
uintptr_t pv_val;
uint32_t urand;
uint32_t uii;
if (set_ct * 2 < tot_ct) {
fill_ulong_zero(perm_vec, (tot_ct + (BITCT - 1)) / BITCT);
for (; num_set < set_ct; num_set++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = (totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift;
widx = uii / BITCT;
wcomp = ONELU << (uii % BITCT);
pv_val = perm_vec[widx];
} while (pv_val & wcomp);
perm_vec[widx] = pv_val | wcomp;
}
} else {
fill_all_bits(perm_vec, tot_ct);
set_ct = tot_ct - set_ct;
for (; num_set < set_ct; num_set++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = (totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift;
widx = uii / BITCT;
wcomp = ONELU << (uii % BITCT);
pv_val = perm_vec[widx];
} while (!(pv_val & wcomp));
perm_vec[widx] = pv_val - wcomp;
}
}
}
void generate_cc_cluster_perm_vec(uint32_t tot_ct, uintptr_t* preimage, uint32_t cluster_ct, uint32_t* cluster_map, uint32_t* cluster_starts, uint32_t* cluster_case_cts, uint32_t* tot_quotients, uint64_t* totq_magics, uint32_t* totq_preshifts, uint32_t* totq_postshifts, uint32_t* totq_incrs, uintptr_t* perm_vec, sfmt_t* sfmtp) {
uint32_t tot_ctl2 = 2 * ((tot_ct + (BITCT - 1)) / BITCT);
uint32_t cluster_idx;
uint32_t target_ct;
uint32_t cluster_end;
uint32_t* map_ptr;
uint32_t num_swapped;
uint32_t cluster_size;
uint32_t upper_bound;
uint64_t totq_magic;
uint32_t totq_preshift;
uint32_t totq_postshift;
uint32_t totq_incr;
uintptr_t widx;
uintptr_t wcomp;
uintptr_t pv_val;
uint32_t urand;
uint32_t uii;
memcpy(perm_vec, preimage, tot_ctl2 * sizeof(intptr_t));
for (cluster_idx = 0; cluster_idx < cluster_ct; cluster_idx++) {
target_ct = cluster_case_cts[cluster_idx];
cluster_end = cluster_starts[cluster_idx + 1];
cluster_size = cluster_end - cluster_starts[cluster_idx];
if (target_ct && (target_ct != cluster_size)) {
upper_bound = cluster_size * tot_quotients[cluster_idx] - 1;
totq_magic = totq_magics[cluster_idx];
totq_preshift = totq_preshifts[cluster_idx];
totq_postshift = totq_postshifts[cluster_idx];
totq_incr = totq_incrs[cluster_idx];
map_ptr = &(cluster_map[cluster_starts[cluster_idx]]);
if (target_ct * 2 < cluster_size) {
for (num_swapped = 0; num_swapped < target_ct; num_swapped++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = map_ptr[(uint32_t)((totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift)];
widx = uii / BITCT2;
wcomp = ONELU << (2 * (uii % BITCT2));
pv_val = perm_vec[widx];
} while (pv_val & wcomp);
perm_vec[widx] = pv_val | wcomp;
}
} else {
target_ct = cluster_size - target_ct;
for (num_swapped = 0; num_swapped < target_ct; num_swapped++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = map_ptr[(uint32_t)((totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift)];
widx = uii / BITCT2;
wcomp = ONELU << (2 * (uii % BITCT2));
pv_val = perm_vec[widx];
} while (!(pv_val & wcomp));
perm_vec[widx] = pv_val - wcomp;
}
}
}
}
}
void generate_cc_cluster_perm1(uint32_t tot_ct, uintptr_t* preimage, uint32_t cluster_ct, uint32_t* cluster_map, uint32_t* cluster_starts, uint32_t* cluster_case_cts, uint32_t* tot_quotients, uint64_t* totq_magics, uint32_t* totq_preshifts, uint32_t* totq_postshifts, uint32_t* totq_incrs, uintptr_t* perm_vec, sfmt_t* sfmtp) {
uint32_t tot_ctl = (tot_ct + (BITCT - 1)) / BITCT;
uint32_t cluster_idx;
uint32_t target_ct;
uint32_t cluster_end;
uint32_t cluster_size;
uint32_t* map_ptr;
uint32_t num_swapped;
uint32_t upper_bound;
uint64_t totq_magic;
uint32_t totq_preshift;
uint32_t totq_postshift;
uint32_t totq_incr;
uintptr_t widx;
uintptr_t wcomp;
uintptr_t pv_val;
uint32_t urand;
uint32_t uii;
memcpy(perm_vec, preimage, tot_ctl * sizeof(intptr_t));
for (cluster_idx = 0; cluster_idx < cluster_ct; cluster_idx++) {
target_ct = cluster_case_cts[cluster_idx];
cluster_end = cluster_starts[cluster_idx + 1];
cluster_size = cluster_end - cluster_starts[cluster_idx];
if (target_ct && (target_ct != cluster_size)) {
upper_bound = cluster_size * tot_quotients[cluster_idx] - 1;
totq_magic = totq_magics[cluster_idx];
totq_preshift = totq_preshifts[cluster_idx];
totq_postshift = totq_postshifts[cluster_idx];
totq_incr = totq_incrs[cluster_idx];
map_ptr = &(cluster_map[cluster_starts[cluster_idx]]);
if (target_ct * 2 < cluster_size) {
for (num_swapped = 0; num_swapped < target_ct; num_swapped++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = map_ptr[(uint32_t)((totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift)];
widx = uii / BITCT;
wcomp = ONELU << (uii % BITCT);
pv_val = perm_vec[widx];
} while (pv_val & wcomp);
perm_vec[widx] = pv_val | wcomp;
}
} else {
target_ct = cluster_size - target_ct;
for (num_swapped = 0; num_swapped < target_ct; num_swapped++) {
do {
do {
urand = sfmt_genrand_uint32(sfmtp);
} while (urand > upper_bound);
uii = map_ptr[(uint32_t)((totq_magic * ((urand >> totq_preshift) + totq_incr)) >> totq_postshift)];
widx = uii / BITCT;
wcomp = ONELU << (uii % BITCT);
pv_val = perm_vec[widx];
} while (!(pv_val & wcomp));
perm_vec[widx] = pv_val - wcomp;
}
}
}
}
}
void transpose_perms(uintptr_t* perm_vecs, uint32_t perm_vec_ct, uint32_t pheno_nm_ct, uint32_t* perm_vecst) {
// Transpose permutations so PRESTO/PERMORY-style genotype indexing can work.
//
// We used a 32-ply interleaved format, to allow counts up to the uint32_t
// limit without giving up highly parallel adds in the calc_git() inner loop.
// The index order used here is:
// 64-bit build:
// first 16 bytes: 0 32 64 96 16 48 80 112 4 36 68 100 20 52 84 116
// 8 40 72 104 24 56 88 120 12 44 76 108 28 60 92 124 1...
// next 16 bytes: 128 160 192...
//
// 32-bit build:
// first 4 bytes: 0 8 16 24 4 12 20 28 1 9 17 25 5 13 21 29 2 10 18...
// next 4 bytes: 32 40 48...
uintptr_t sample_idx = 0;
uintptr_t pheno_nm_ctl2 = 2 * ((pheno_nm_ct + (BITCT - 1)) / BITCT);
#ifdef __LP64__
uint32_t wbuf[4];
uint32_t* wbptr;
#else
uint32_t wval;
#endif
uint32_t rshift;
uint32_t wshift;
uintptr_t* pvptr;
uintptr_t perm_idx;
for (; sample_idx < pheno_nm_ct; sample_idx++) {
perm_idx = 0;
pvptr = &(perm_vecs[sample_idx / BITCT2]);
rshift = 2 * (sample_idx % BITCT2);
goto transpose_perms_loop_start;
#ifdef __LP64__
do {
if (!(perm_idx % 4)) {
if (perm_idx % 128) {
wshift = ((perm_idx & 96) >> 5) | ((perm_idx & 16) >> 2) | ((perm_idx & 12) << 1);
} else {
memcpy(perm_vecst, wbuf, 16);
perm_vecst = &(perm_vecst[4]);
transpose_perms_loop_start:
fill_uint_zero(wbuf, 4);
wshift = 0;
}
wbptr = wbuf;
}
*wbptr |= ((pvptr[perm_idx * pheno_nm_ctl2] >> rshift) & 1) << wshift;
wbptr++;
} while (++perm_idx < perm_vec_ct);
memcpy(perm_vecst, wbuf, 16);
perm_vecst = &(perm_vecst[4]);
#else
do {
if (perm_idx % 32) {
wshift = ((perm_idx & 24) >> 3) | (perm_idx & 4) | ((perm_idx & 3) << 3);
} else {
*perm_vecst++ = wval;
transpose_perms_loop_start:
wval = 0;
wshift = 0;
}
wval |= ((pvptr[perm_idx * pheno_nm_ctl2] >> rshift) & 1) << wshift;
} while (++perm_idx < perm_vec_ct);
*perm_vecst++ = wval;
#endif
}
}
void transpose_perm1s(uintptr_t* perm_vecs, uint32_t perm_vec_ct, uint32_t pheno_nm_ct, uint32_t* perm_vecst) {
uintptr_t sample_idx = 0;
uintptr_t pheno_nm_ctl = (pheno_nm_ct + (BITCT - 1)) / BITCT;
#ifdef __LP64__
uint32_t wbuf[4];
uint32_t* wbptr;
#else
uint32_t wval;
#endif
uint32_t rshift;
uint32_t wshift;
uintptr_t* pvptr;
uintptr_t perm_idx;
for (; sample_idx < pheno_nm_ct; sample_idx++) {
perm_idx = 0;
pvptr = &(perm_vecs[sample_idx / BITCT]);
rshift = sample_idx % BITCT;
goto transpose_perm1s_loop_start;
#ifdef __LP64__
do {
if (!(perm_idx % 4)) {
if (perm_idx % 128) {
wshift = ((perm_idx & 96) >> 5) | ((perm_idx & 16) >> 2) | ((perm_idx & 12) << 1);
} else {
memcpy(perm_vecst, wbuf, 16);
perm_vecst = &(perm_vecst[4]);
transpose_perm1s_loop_start:
fill_uint_zero(wbuf, 2);
wshift = 0;
}
wbptr = wbuf;
}
*wbptr |= ((pvptr[perm_idx * pheno_nm_ctl] >> rshift) & 1) << wshift;
wbptr++;
} while (++perm_idx < perm_vec_ct);
memcpy(perm_vecst, wbuf, 16);
perm_vecst = &(perm_vecst[4]);
#else
do {
if (perm_idx % 32) {
wshift = ((perm_idx & 24) >> 3) | (perm_idx & 4) | ((perm_idx & 3) << 3);
} else {
*perm_vecst++ = wval;
transpose_perm1s_loop_start:
wval = 0;
wshift = 0;
}
wval |= ((pvptr[perm_idx * pheno_nm_ctl] >> rshift) & 1) << wshift;
} while (++perm_idx < perm_vec_ct);
*perm_vecst++ = wval;
#endif
}
}
char* model_assoc_tna(uint32_t model_fisher, char* wptr) {
// write terminal NAs to buffer
if (model_fisher) {
return memcpya(wptr, " NA\n", 13);
} else {
return memcpya(wptr, " NA NA NA\n", 31);
}
}
void calc_git(uint32_t pheno_nm_ct, uint32_t perm_vec_ct, uintptr_t* __restrict__ loadbuf, uint32_t* perm_vecst, uint32_t* results_bufs, uint32_t* thread_wkspace) {
// Brian Browning's genotype indexing algorithm for low-MAF (and low missing
// frequency) markers.
// We accelerate it by using a special interleaved permutation representation
// which supports vector addition without occupying extra space: see
// generate_cc_perm_vec(). Counting the number of e.g. case heterozygote
// genotypes across all permutations then proceeds as follows:
// 1. For the first 15 heterozygote samples, just use 4-bit accumulators.
// This allows the inner loop to increment 32 counters simultaneously.
// 2. Right before they'd otherwise be at risk of overflowing, we unfold the
// 4-bit accumulators into a larger buffer of 8-bit accumulators. Then we
// zero out the 4-bit accumulators, and restart the inner loop.
// 3. This can happen up to 17 times before the 8-bit accumulators risk
// overflow. Then, they are unfolded into the final output array of
// 32-bit ints, zeroed out, and the second loop restarts.
// Note that results_bufs[] is assumed to be zeroed out before this function
// is called.
uint32_t pheno_nm_ctl2x = (pheno_nm_ct + (BITCT2 - 1)) / BITCT2;
uint32_t perm_ct16 = (perm_vec_ct + 15) / 16;
#ifdef __LP64__
uint32_t perm_ct128 = (perm_vec_ct + 127) / 128;
uint32_t perm_ct128x4 = perm_ct128 * 4;
uint32_t perm_ct32 = (perm_vec_ct + 31) / 32;
uint32_t perm_ct16x4 = 4 * perm_ct16;
const __m128i m1x4 = {0x1111111111111111LLU, 0x1111111111111111LLU};
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU};
const __m128i m8x32 = {0x000000ff000000ffLLU, 0x000000ff000000ffLLU};
__m128i* permsv = (__m128i*)perm_vecst;
__m128i* gitv[9];
__m128i* __restrict__ git_merge4; // no conflicts, please
__m128i* __restrict__ git_merge8;
__m128i* __restrict__ git_write;
__m128i* __restrict__ perm_ptr;
__m128i loader;
#else
uint32_t perm_ct32 = (perm_vec_ct + 31) / 32;
uint32_t perm_ct32x4 = perm_ct32 * 4;
uint32_t perm_ct8 = (perm_vec_ct + 7) / 8;
uint32_t perm_ct4 = (perm_vec_ct + 3) / 4;
uint32_t perm_ct16x16 = 16 * perm_ct16;
uint32_t* permsv = perm_vecst;
uint32_t* gitv[9];
uint32_t* git_merge4;
uint32_t* git_merge8;
uint32_t* git_write;
uint32_t* perm_ptr;
uintptr_t loader;
#endif
uint32_t cur_cts[3];
uintptr_t ulii;
uint32_t pbidx;
uint32_t uii;
uint32_t ujj;
uint32_t ukk;
uint32_t sample_type;
#ifdef __LP64__
// 4- and 8-bit partial counts
gitv[0] = (__m128i*)thread_wkspace;
gitv[1] = &(((__m128i*)thread_wkspace)[perm_ct128x4]);
gitv[2] = &(((__m128i*)thread_wkspace)[2 * perm_ct128x4]);
gitv[3] = &(((__m128i*)thread_wkspace)[3 * perm_ct128x4]);
gitv[4] = &(((__m128i*)thread_wkspace)[3 * perm_ct128x4 + 2 * perm_ct32]);
gitv[5] = &(((__m128i*)thread_wkspace)[3 * perm_ct128x4 + 4 * perm_ct32]);
gitv[6] = &(((__m128i*)results_bufs)[2 * perm_ct16x4]);
gitv[7] = &(((__m128i*)results_bufs)[perm_ct16x4]);
gitv[8] = (__m128i*)results_bufs;
#else
gitv[0] = thread_wkspace;
gitv[1] = &(thread_wkspace[perm_ct32x4]);
gitv[2] = &(thread_wkspace[2 * perm_ct32x4]);
gitv[3] = &(thread_wkspace[3 * perm_ct32x4]);
gitv[4] = &(thread_wkspace[3 * perm_ct32x4 + 2 * perm_ct8]);
gitv[5] = &(thread_wkspace[3 * perm_ct32x4 + 4 * perm_ct8]);
gitv[6] = &(results_bufs[2 * perm_ct16x16]);
gitv[7] = &(results_bufs[perm_ct16x16]);
gitv[8] = results_bufs;
#endif
cur_cts[0] = 0;
cur_cts[1] = 0;
cur_cts[2] = 0;
for (uii = 0; uii < pheno_nm_ctl2x; uii++) {
ulii = ~(*loadbuf++);
if (uii + 1 == pheno_nm_ctl2x) {
ujj = pheno_nm_ct & (BITCT2 - 1);
if (ujj) {
ulii &= (ONELU << (ujj * 2)) - ONELU;
}
}
while (ulii) {
ujj = CTZLU(ulii) & (BITCT - 2); // get pos of next non-[hom A2] sample
sample_type = ((ulii >> ujj) & 3) - 1;
git_merge4 = gitv[sample_type];
#ifdef __LP64__
perm_ptr = &(permsv[(ujj / 2) * perm_ct128]);
for (pbidx = 0; pbidx < perm_ct128; pbidx++) {
loader = *perm_ptr++;
git_merge4[0] = _mm_add_epi64(git_merge4[0], _mm_and_si128(loader, m1x4));
git_merge4[1] = _mm_add_epi64(git_merge4[1], _mm_and_si128(_mm_srli_epi64(loader, 1), m1x4));