forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplink_ld.c
13878 lines (13650 loc) · 546 KB
/
plink_ld.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 <stddef.h>
#include "plink_assoc.h"
#include "plink_glm.h"
#include "plink_ld.h"
#include "plink_stats.h"
#include "pigz.h"
#define MULTIPLEX_LD 1920
#define MULTIPLEX_2LD (MULTIPLEX_LD * 2)
void ld_epi_init(Ld_info* ldip, Epi_info* epi_ip, Clump_info* clump_ip) {
ldip->modifier = 0;
ldip->prune_window_size = 0;
ldip->prune_window_incr = 0;
ldip->prune_last_param = 0.0;
ldip->window_size = 0xffffffffU;
ldip->window_bp = 0xffffffffU;
ldip->window_r2 = 0.2;
ldip->blocks_max_bp = 0xffffffffU;
ldip->blocks_min_maf = 0.05;
ldip->blocks_strong_lowci_outer = 71;
ldip->blocks_strong_lowci = 72;
ldip->blocks_strong_highci = 97;
ldip->blocks_recomb_highci = 89;
ldip->blocks_inform_frac = 0.95;
ldip->flipscan_window_size = 10;
ldip->flipscan_window_bp = 0xffffffffU;
ldip->flipscan_thresh = 0.5;
ldip->show_tags_bp = 250000;
ldip->show_tags_r2 = 0.8;
ldip->snpstr = NULL;
ldip->show_tags_fname = NULL;
range_list_init(&(ldip->snps_rl));
epi_ip->modifier = 0;
epi_ip->case_only_gap = 1000000;
epi_ip->epi1 = 0.0;
epi_ip->epi2 = 0.01;
epi_ip->je_cellmin = 5;
epi_ip->ld_mkr1 = NULL;
epi_ip->ld_mkr2 = NULL;
epi_ip->twolocus_mkr1 = NULL;
epi_ip->twolocus_mkr2 = NULL;
epi_ip->summary_merge_prefix = NULL;
clump_ip->modifier = 0;
clump_ip->fname_ct = 0;
clump_ip->bp_radius = 249999;
clump_ip->range_border = 0;
clump_ip->fnames_flattened = NULL;
clump_ip->annotate_flattened = NULL;
clump_ip->snpfield_search_order = NULL;
clump_ip->pfield_search_order = NULL;
clump_ip->range_fname = NULL;
clump_ip->p1 = 1e-4;
clump_ip->p2 = 1e-2;
clump_ip->r2 = 0.5;
}
void ld_epi_cleanup(Ld_info* ldip, Epi_info* epi_ip, Clump_info* clump_ip) {
free_cond(ldip->snpstr);
free_cond(ldip->show_tags_fname);
free_range_list(&(ldip->snps_rl));
free_cond(epi_ip->ld_mkr1);
free_cond(epi_ip->ld_mkr2);
free_cond(epi_ip->twolocus_mkr1);
free_cond(epi_ip->twolocus_mkr2);
free_cond(epi_ip->summary_merge_prefix);
free_cond(clump_ip->fnames_flattened);
free_cond(clump_ip->annotate_flattened);
free_cond(clump_ip->snpfield_search_order);
free_cond(clump_ip->pfield_search_order);
free_cond(clump_ip->range_fname);
}
#ifdef __LP64__
static inline void ld_dot_prod_batch(__m128i* vec1, __m128i* vec2, __m128i* mask1, __m128i* mask2, int32_t* return_vals, uint32_t iters) {
// Main routine for computation of \sum_i^M (x_i - \mu_x)(y_i - \mu_y), where
// x_i, y_i \in \{-1, 0, 1\}, but there are missing values.
//
//
// We decompose this sum into
// \sum_i x_iy_i - \mu_y\sum_i x_i - \mu_x\sum_i y_i +
// (M - # missing)\mu_x\mu_y.
// *Without* missing values, this can be handled very cleanly. The last
// three terms can all be precomputed, and \sum_i x_iy_i can be handled in a
// manner very similar to bitwise Hamming distance. This is several times as
// fast as the lookup tables used for relationship matrices.
//
// Unfortunately, when missing values are present,
// \mu_y\sum_{i: nonmissing from y} x_i and
// \mu_x\sum_{i: nonmissing from x} y_i must also be evaluated (and, in
// practice, \mu_y\sum_{i: nonmissing from y} x_i^2 and
// \mu_x\sum_{i: nonmissing from x} y_i^2 should be determined here as well);
// this removes much of the speed advantage, and the best applications of the
// underlying ternary dot product algorithm used here lie elsewhere.
// Nevertheless, it is still faster, so we use it.
// (possible todo: accelerated function when there really are no missing
// values, similar to what is now done for --fast-epistasis)
//
//
// Input:
// * vec1 and vec2 are encoded -1 -> 00, 0/missing -> 01, 1 -> 10.
// * mask1 and mask2 mask out missing values (i.e. 00 for missing, 11 for
// nonmissing).
// * return_vals provides space for return values.
// * iters is the number of 48-byte windows to process, anywhere from 1 to 10
// inclusive.
//
// This function performs the update
// return_vals[0] += (-N) + \sum_i x_iy_i
// return_vals[1] += N_y + \sum_{i: nonmissing from y} x_i
// return_vals[2] += N_x + \sum_{i: nonmissing from x} y_i
// return_vals[3] += N_y - \sum_{i: nonmissing from y} x_i^2
// return_vals[4] += N_x - \sum_{i: nonmissing from x} y_i^2
// where N is the number of samples processed after applying the missingness
// masks indicated by the subscripts.
//
// Computation of terms [1]-[4] is based on the identity
// N_y + \sum_{i: nonmissing from y} x_i = popcount2(vec1 & mask2)
// where "popcount2" refers to starting with two-bit integers instead of
// one-bit integers in our summing process (this allows us to skip a few
// operations). (Once we can assume the presence of hardware popcount, a
// slightly different implementation may be better.)
//
// The trickier [0] computation currently proceeds as follows:
//
// 1. zcheck := (vec1 | vec2) & 0x5555...
// Detects whether at least one member of the pair has a 0/missing value.
//
// 2. popcount2(((vec1 ^ vec2) & (0xaaaa... - zcheck)) | zcheck)
// Subtracting this *from* a bias will give us our desired \sum_i x_iy_i dot
// product.
//
// MULTIPLEX_LD sets of values are usually handled per function call. If
// fewer values are present, the ends of all input vectors should be zeroed
// out.
const __m128i m1 = {FIVEMASK, FIVEMASK};
const __m128i m2 = {0x3333333333333333LLU, 0x3333333333333333LLU};
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU};
__m128i loader1;
__m128i loader2;
__m128i sum1;
__m128i sum2;
__m128i sum11;
__m128i sum22;
__m128i sum12;
__m128i tmp_sum1;
__m128i tmp_sum2;
__m128i tmp_sum12;
__uni16 acc;
__uni16 acc1;
__uni16 acc2;
__uni16 acc11;
__uni16 acc22;
acc.vi = _mm_setzero_si128();
acc1.vi = _mm_setzero_si128();
acc2.vi = _mm_setzero_si128();
acc11.vi = _mm_setzero_si128();
acc22.vi = _mm_setzero_si128();
do {
loader1 = *vec1++;
loader2 = *vec2++;
sum1 = *mask2++;
sum2 = *mask1++;
sum12 = _mm_and_si128(_mm_or_si128(loader1, loader2), m1);
// sum11 = _mm_and_si128(_mm_and_si128(_mm_xor_si128(sum1, m1), m1), loader1);
// sum22 = _mm_and_si128(_mm_and_si128(_mm_xor_si128(sum2, m1), m1), loader2);
sum1 = _mm_and_si128(sum1, loader1);
sum2 = _mm_and_si128(sum2, loader2);
sum11 = _mm_and_si128(sum1, m1);
sum22 = _mm_and_si128(sum2, m1);
// use andnot to eliminate need for 0xaaaa... to occupy an xmm register
loader1 = _mm_andnot_si128(_mm_add_epi64(m1, sum12), _mm_xor_si128(loader1, loader2));
sum12 = _mm_or_si128(sum12, loader1);
// sum1, sum2, and sum12 now store the (biased) two-bit sums of
// interest; merge to 4 bits to prevent overflow. this merge can be
// postponed for sum11 and sum22 because the individual terms are 0/1
// instead of 0/1/2.
sum1 = _mm_add_epi64(_mm_and_si128(sum1, m2), _mm_and_si128(_mm_srli_epi64(sum1, 2), m2));
sum2 = _mm_add_epi64(_mm_and_si128(sum2, m2), _mm_and_si128(_mm_srli_epi64(sum2, 2), m2));
sum12 = _mm_add_epi64(_mm_and_si128(sum12, m2), _mm_and_si128(_mm_srli_epi64(sum12, 2), m2));
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum1 = *mask2++;
tmp_sum2 = *mask1++;
tmp_sum12 = _mm_and_si128(_mm_or_si128(loader1, loader2), m1);
tmp_sum1 = _mm_and_si128(tmp_sum1, loader1);
tmp_sum2 = _mm_and_si128(tmp_sum2, loader2);
sum11 = _mm_add_epi64(sum11, _mm_and_si128(tmp_sum1, m1));
sum22 = _mm_add_epi64(sum22, _mm_and_si128(tmp_sum2, m1));
loader1 = _mm_andnot_si128(_mm_add_epi64(m1, tmp_sum12), _mm_xor_si128(loader1, loader2));
tmp_sum12 = _mm_or_si128(loader1, tmp_sum12);
sum1 = _mm_add_epi64(sum1, _mm_add_epi64(_mm_and_si128(tmp_sum1, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum1, 2), m2)));
sum2 = _mm_add_epi64(sum2, _mm_add_epi64(_mm_and_si128(tmp_sum2, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum2, 2), m2)));
sum12 = _mm_add_epi64(sum12, _mm_add_epi64(_mm_and_si128(tmp_sum12, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum12, 2), m2)));
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum1 = *mask2++;
tmp_sum2 = *mask1++;
tmp_sum12 = _mm_and_si128(_mm_or_si128(loader1, loader2), m1);
tmp_sum1 = _mm_and_si128(tmp_sum1, loader1);
tmp_sum2 = _mm_and_si128(tmp_sum2, loader2);
sum11 = _mm_add_epi64(sum11, _mm_and_si128(tmp_sum1, m1));
sum22 = _mm_add_epi64(sum22, _mm_and_si128(tmp_sum2, m1));
loader1 = _mm_andnot_si128(_mm_add_epi64(m1, tmp_sum12), _mm_xor_si128(loader1, loader2));
tmp_sum12 = _mm_or_si128(loader1, tmp_sum12);
sum1 = _mm_add_epi64(sum1, _mm_add_epi64(_mm_and_si128(tmp_sum1, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum1, 2), m2)));
sum2 = _mm_add_epi64(sum2, _mm_add_epi64(_mm_and_si128(tmp_sum2, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum2, 2), m2)));
sum11 = _mm_add_epi64(_mm_and_si128(sum11, m2), _mm_and_si128(_mm_srli_epi64(sum11, 2), m2));
sum22 = _mm_add_epi64(_mm_and_si128(sum22, m2), _mm_and_si128(_mm_srli_epi64(sum22, 2), m2));
sum12 = _mm_add_epi64(sum12, _mm_add_epi64(_mm_and_si128(tmp_sum12, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum12, 2), m2)));
acc1.vi = _mm_add_epi64(acc1.vi, _mm_add_epi64(_mm_and_si128(sum1, m4), _mm_and_si128(_mm_srli_epi64(sum1, 4), m4)));
acc2.vi = _mm_add_epi64(acc2.vi, _mm_add_epi64(_mm_and_si128(sum2, m4), _mm_and_si128(_mm_srli_epi64(sum2, 4), m4)));
acc11.vi = _mm_add_epi64(acc11.vi, _mm_add_epi64(_mm_and_si128(sum11, m4), _mm_and_si128(_mm_srli_epi64(sum11, 4), m4)));
acc22.vi = _mm_add_epi64(acc22.vi, _mm_add_epi64(_mm_and_si128(sum22, m4), _mm_and_si128(_mm_srli_epi64(sum22, 4), m4)));
acc.vi = _mm_add_epi64(acc.vi, _mm_add_epi64(_mm_and_si128(sum12, m4), _mm_and_si128(_mm_srli_epi64(sum12, 4), m4)));
} while (--iters);
// moved down because we've almost certainly run out of xmm registers
const __m128i m8 = {0x00ff00ff00ff00ffLLU, 0x00ff00ff00ff00ffLLU};
#if MULTIPLEX_LD > 960
acc1.vi = _mm_add_epi64(_mm_and_si128(acc1.vi, m8), _mm_and_si128(_mm_srli_epi64(acc1.vi, 8), m8));
acc2.vi = _mm_add_epi64(_mm_and_si128(acc2.vi, m8), _mm_and_si128(_mm_srli_epi64(acc2.vi, 8), m8));
acc.vi = _mm_add_epi64(_mm_and_si128(acc.vi, m8), _mm_and_si128(_mm_srli_epi64(acc.vi, 8), m8));
#else
acc1.vi = _mm_and_si128(_mm_add_epi64(acc1.vi, _mm_srli_epi64(acc1.vi, 8)), m8);
acc2.vi = _mm_and_si128(_mm_add_epi64(acc2.vi, _mm_srli_epi64(acc2.vi, 8)), m8);
acc.vi = _mm_and_si128(_mm_add_epi64(acc.vi, _mm_srli_epi64(acc.vi, 8)), m8);
#endif
acc11.vi = _mm_and_si128(_mm_add_epi64(acc11.vi, _mm_srli_epi64(acc11.vi, 8)), m8);
acc22.vi = _mm_and_si128(_mm_add_epi64(acc22.vi, _mm_srli_epi64(acc22.vi, 8)), m8);
return_vals[0] -= ((acc.u8[0] + acc.u8[1]) * 0x1000100010001LLU) >> 48;
return_vals[1] += ((acc1.u8[0] + acc1.u8[1]) * 0x1000100010001LLU) >> 48;
return_vals[2] += ((acc2.u8[0] + acc2.u8[1]) * 0x1000100010001LLU) >> 48;
return_vals[3] += ((acc11.u8[0] + acc11.u8[1]) * 0x1000100010001LLU) >> 48;
return_vals[4] += ((acc22.u8[0] + acc22.u8[1]) * 0x1000100010001LLU) >> 48;
}
void ld_dot_prod(uintptr_t* vec1, uintptr_t* vec2, uintptr_t* mask1, uintptr_t* mask2, int32_t* return_vals, uint32_t batch_ct_m1, uint32_t last_batch_size) {
while (batch_ct_m1--) {
ld_dot_prod_batch((__m128i*)vec1, (__m128i*)vec2, (__m128i*)mask1, (__m128i*)mask2, return_vals, MULTIPLEX_LD / 192);
vec1 = &(vec1[MULTIPLEX_LD / BITCT2]);
vec2 = &(vec2[MULTIPLEX_LD / BITCT2]);
mask1 = &(mask1[MULTIPLEX_LD / BITCT2]);
mask2 = &(mask2[MULTIPLEX_LD / BITCT2]);
}
ld_dot_prod_batch((__m128i*)vec1, (__m128i*)vec2, (__m128i*)mask1, (__m128i*)mask2, return_vals, last_batch_size);
}
static inline int32_t ld_dot_prod_nm_batch(__m128i* vec1, __m128i* vec2, uint32_t iters) {
// faster ld_dot_prod_batch() for no-missing-calls case.
const __m128i m1 = {FIVEMASK, FIVEMASK};
const __m128i m2 = {0x3333333333333333LLU, 0x3333333333333333LLU};
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU};
const __m128i m8 = {0x00ff00ff00ff00ffLLU, 0x00ff00ff00ff00ffLLU};
__m128i loader1;
__m128i loader2;
__m128i sum12;
__m128i tmp_sum12;
__uni16 acc;
acc.vi = _mm_setzero_si128();
do {
loader1 = *vec1++;
loader2 = *vec2++;
sum12 = _mm_and_si128(_mm_or_si128(loader1, loader2), m1);
loader1 = _mm_andnot_si128(_mm_add_epi64(m1, sum12), _mm_xor_si128(loader1, loader2));
sum12 = _mm_or_si128(sum12, loader1);
sum12 = _mm_add_epi64(_mm_and_si128(sum12, m2), _mm_and_si128(_mm_srli_epi64(sum12, 2), m2));
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum12 = _mm_and_si128(_mm_or_si128(loader1, loader2), m1);
loader1 = _mm_andnot_si128(_mm_add_epi64(m1, tmp_sum12), _mm_xor_si128(loader1, loader2));
tmp_sum12 = _mm_or_si128(loader1, tmp_sum12);
sum12 = _mm_add_epi64(sum12, _mm_add_epi64(_mm_and_si128(tmp_sum12, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum12, 2), m2)));
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum12 = _mm_and_si128(_mm_or_si128(loader1, loader2), m1);
loader1 = _mm_andnot_si128(_mm_add_epi64(m1, tmp_sum12), _mm_xor_si128(loader1, loader2));
tmp_sum12 = _mm_or_si128(loader1, tmp_sum12);
sum12 = _mm_add_epi64(sum12, _mm_add_epi64(_mm_and_si128(tmp_sum12, m2), _mm_and_si128(_mm_srli_epi64(tmp_sum12, 2), m2)));
acc.vi = _mm_add_epi64(acc.vi, _mm_add_epi64(_mm_and_si128(sum12, m4), _mm_and_si128(_mm_srli_epi64(sum12, 4), m4)));
} while (--iters);
#if MULTIPLEX_LD > 960
acc.vi = _mm_add_epi64(_mm_and_si128(acc.vi, m8), _mm_and_si128(_mm_srli_epi64(acc.vi, 8), m8));
#else
acc.vi = _mm_and_si128(_mm_add_epi64(acc.vi, _mm_srli_epi64(acc.vi, 8)), m8);
#endif
return (uint32_t)(((acc.u8[0] + acc.u8[1]) * 0x1000100010001LLU) >> 48);
}
int32_t ld_dot_prod_nm(uintptr_t* vec1, uintptr_t* vec2, uint32_t founder_ct, uint32_t batch_ct_m1, uint32_t last_batch_size) {
// accelerated implementation for no-missing-loci case
int32_t result = (int32_t)founder_ct;
while (batch_ct_m1--) {
result -= ld_dot_prod_nm_batch((__m128i*)vec1, (__m128i*)vec2, MULTIPLEX_LD / 192);
vec1 = &(vec1[MULTIPLEX_LD / BITCT2]);
vec2 = &(vec2[MULTIPLEX_LD / BITCT2]);
}
result -= ld_dot_prod_nm_batch((__m128i*)vec1, (__m128i*)vec2, last_batch_size);
return result;
}
#else
static inline void ld_dot_prod_batch(uintptr_t* vec1, uintptr_t* vec2, uintptr_t* mask1, uintptr_t* mask2, int32_t* return_vals, uint32_t iters) {
uint32_t final_sum1 = 0;
uint32_t final_sum2 = 0;
uint32_t final_sum11 = 0;
uint32_t final_sum22 = 0;
uint32_t final_sum12 = 0;
uintptr_t loader1;
uintptr_t loader2;
uintptr_t sum1;
uintptr_t sum2;
uintptr_t sum11;
uintptr_t sum22;
uintptr_t sum12;
uintptr_t tmp_sum1;
uintptr_t tmp_sum2;
uintptr_t tmp_sum12;
do {
// (The important part of the header comment on the 64-bit version is
// copied below.)
//
// Input:
// * vec1 and vec2 are encoded -1 -> 00, 0/missing -> 01, 1 -> 10.
// * mask1 and mask2 mask out missing values (i.e. 00 for missing, 11 for
// nonmissing).
// * return_vals provides space for return values.
// * iters is the number of 12-byte windows to process, anywhere from 1 to
// 40 inclusive. (No, this is not the interface you'd use for a
// general-purpose library.) [32- and 64-bit differ here.]
//
// This function performs the update
// return_vals[0] += (-N) + \sum_i x_iy_i
// return_vals[1] += N_y + \sum_{i: nonmissing from y} x_i
// return_vals[2] += N_x + \sum_{i: nonmissing from x} y_i
// return_vals[3] += N_y - \sum_{i: nonmissing from y} x_i^2
// return_vals[4] += N_x - \sum_{i: nonmissing from x} y_i^2
// where N is the number of samples processed after applying the
// missingness masks indicated by the subscripts.
//
// Computation of terms [1]-[4] is based on the identity
// N_y + \sum_{i: nonmissing from y} x_i = popcount2(vec1 & mask2)
// where "popcount2" refers to starting with two-bit integers instead of
// one-bit integers in our summing process (this allows us to skip a few
// operations). (Once we can assume the presence of hardware popcount, a
// slightly different implementation may be better.)
//
// The trickier [0] computation currently proceeds as follows:
//
// 1. zcheck := (vec1 | vec2) & 0x5555...
// Detects whether at least one member of the pair has a 0/missing value.
//
// 2. popcount2(((vec1 ^ vec2) & (0xaaaa... - zcheck)) | zcheck)
// Subtracting this *from* a bias will give us our desired \sum_i x_iy_i
// dot product.
loader1 = *vec1++;
loader2 = *vec2++;
sum1 = *mask2++;
sum2 = *mask1++;
sum12 = (loader1 | loader2) & FIVEMASK;
sum1 = sum1 & loader1;
sum2 = sum2 & loader2;
loader1 = (loader1 ^ loader2) & (AAAAMASK - sum12);
sum12 = sum12 | loader1;
sum11 = sum1 & FIVEMASK;
sum22 = sum2 & FIVEMASK;
sum1 = (sum1 & 0x33333333) + ((sum1 >> 2) & 0x33333333);
sum2 = (sum2 & 0x33333333) + ((sum2 >> 2) & 0x33333333);
sum12 = (sum12 & 0x33333333) + ((sum12 >> 2) & 0x33333333);
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum1 = *mask2++;
tmp_sum2 = *mask1++;
tmp_sum12 = (loader1 | loader2) & FIVEMASK;
tmp_sum1 = tmp_sum1 & loader1;
tmp_sum2 = tmp_sum2 & loader2;
loader1 = (loader1 ^ loader2) & (AAAAMASK - tmp_sum12);
tmp_sum12 = tmp_sum12 | loader1;
sum11 += tmp_sum1 & FIVEMASK;
sum22 += tmp_sum2 & FIVEMASK;
sum1 += (tmp_sum1 & 0x33333333) + ((tmp_sum1 >> 2) & 0x33333333);
sum2 += (tmp_sum2 & 0x33333333) + ((tmp_sum2 >> 2) & 0x33333333);
sum12 += (tmp_sum12 & 0x33333333) + ((tmp_sum12 >> 2) & 0x33333333);
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum1 = *mask2++;
tmp_sum2 = *mask1++;
tmp_sum12 = (loader1 | loader2) & FIVEMASK;
tmp_sum1 = tmp_sum1 & loader1;
tmp_sum2 = tmp_sum2 & loader2;
loader1 = (loader1 ^ loader2) & (AAAAMASK - tmp_sum12);
tmp_sum12 = tmp_sum12 | loader1;
sum11 += tmp_sum1 & FIVEMASK;
sum22 += tmp_sum2 & FIVEMASK;
sum1 += (tmp_sum1 & 0x33333333) + ((tmp_sum1 >> 2) & 0x33333333);
sum2 += (tmp_sum2 & 0x33333333) + ((tmp_sum2 >> 2) & 0x33333333);
sum11 = (sum11 & 0x33333333) + ((sum11 >> 2) & 0x33333333);
sum22 = (sum22 & 0x33333333) + ((sum22 >> 2) & 0x33333333);
sum12 += (tmp_sum12 & 0x33333333) + ((tmp_sum12 >> 2) & 0x33333333);
sum1 = (sum1 & 0x0f0f0f0f) + ((sum1 >> 4) & 0x0f0f0f0f);
sum2 = (sum2 & 0x0f0f0f0f) + ((sum2 >> 4) & 0x0f0f0f0f);
sum11 = (sum11 & 0x0f0f0f0f) + ((sum11 >> 4) & 0x0f0f0f0f);
sum22 = (sum22 & 0x0f0f0f0f) + ((sum22 >> 4) & 0x0f0f0f0f);
sum12 = (sum12 & 0x0f0f0f0f) + ((sum12 >> 4) & 0x0f0f0f0f);
// technically could do the multiply-and-shift only once every two rounds
final_sum1 += (sum1 * 0x01010101) >> 24;
final_sum2 += (sum2 * 0x01010101) >> 24;
final_sum11 += (sum11 * 0x01010101) >> 24;
final_sum22 += (sum22 * 0x01010101) >> 24;
final_sum12 += (sum12 * 0x01010101) >> 24;
} while (--iters);
return_vals[0] -= final_sum12;
return_vals[1] += final_sum1;
return_vals[2] += final_sum2;
return_vals[3] += final_sum11;
return_vals[4] += final_sum22;
}
void ld_dot_prod(uintptr_t* vec1, uintptr_t* vec2, uintptr_t* mask1, uintptr_t* mask2, int32_t* return_vals, uint32_t batch_ct_m1, uint32_t last_batch_size) {
while (batch_ct_m1--) {
ld_dot_prod_batch(vec1, vec2, mask1, mask2, return_vals, MULTIPLEX_LD / 48);
vec1 = &(vec1[MULTIPLEX_LD / BITCT2]);
vec2 = &(vec2[MULTIPLEX_LD / BITCT2]);
mask1 = &(mask1[MULTIPLEX_LD / BITCT2]);
mask2 = &(mask2[MULTIPLEX_LD / BITCT2]);
}
ld_dot_prod_batch(vec1, vec2, mask1, mask2, return_vals, last_batch_size);
}
static inline int32_t ld_dot_prod_nm_batch(uintptr_t* vec1, uintptr_t* vec2, uint32_t iters) {
uint32_t final_sum12 = 0;
uintptr_t loader1;
uintptr_t loader2;
uintptr_t sum12;
uintptr_t tmp_sum12;
do {
loader1 = *vec1++;
loader2 = *vec2++;
sum12 = (loader1 | loader2) & FIVEMASK;
loader1 = (loader1 ^ loader2) & (AAAAMASK - sum12);
sum12 = sum12 | loader1;
sum12 = (sum12 & 0x33333333) + ((sum12 >> 2) & 0x33333333);
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum12 = (loader1 | loader2) & FIVEMASK;
loader1 = (loader1 ^ loader2) & (AAAAMASK - tmp_sum12);
tmp_sum12 = tmp_sum12 | loader1;
sum12 += (tmp_sum12 & 0x33333333) + ((tmp_sum12 >> 2) & 0x33333333);
loader1 = *vec1++;
loader2 = *vec2++;
tmp_sum12 = (loader1 | loader2) & FIVEMASK;
loader1 = (loader1 ^ loader2) & (AAAAMASK - tmp_sum12);
tmp_sum12 = tmp_sum12 | loader1;
sum12 += (tmp_sum12 & 0x33333333) + ((tmp_sum12 >> 2) & 0x33333333);
sum12 = (sum12 & 0x0f0f0f0f) + ((sum12 >> 4) & 0x0f0f0f0f);
final_sum12 += (sum12 * 0x01010101) >> 24;
} while (--iters);
return final_sum12;
}
int32_t ld_dot_prod_nm(uintptr_t* vec1, uintptr_t* vec2, uint32_t founder_ct, uint32_t batch_ct_m1, uint32_t last_batch_size) {
int32_t result = (int32_t)founder_ct;
while (batch_ct_m1--) {
result -= ld_dot_prod_nm_batch(vec1, vec2, MULTIPLEX_LD / 48);
vec1 = &(vec1[MULTIPLEX_LD / BITCT2]);
vec2 = &(vec2[MULTIPLEX_LD / BITCT2]);
}
result -= ld_dot_prod_nm_batch(vec1, vec2, last_batch_size);
return result;
}
#endif // __LP64__
uint32_t ld_process_load(uintptr_t* geno_buf, uintptr_t* mask_buf, uintptr_t* missing_buf, uint32_t* missing_ct_ptr, double* sum_ptr, double* variance_recip_ptr, uint32_t founder_ct, uint32_t is_x, uint32_t weighted_x, uint32_t nonmale_founder_ct, uintptr_t* founder_male_include2, uintptr_t* nonmale_geno, uintptr_t* nonmale_masks, uintptr_t nonmale_offset) {
uintptr_t* geno_ptr = geno_buf;
uintptr_t founder_ctl2 = (founder_ct + (BITCT2 - 1)) / BITCT2;
uintptr_t* geno_end = &(geno_buf[founder_ctl2]);
uintptr_t* mask_buf_ptr = mask_buf;
uintptr_t* missing_ptr = missing_buf;
uintptr_t new_missing = 0;
int64_t llii;
uint32_t missing_bit_offset = 0;
uint32_t ssq = 0;
uint32_t missing_ct = 0;
int32_t sum = -founder_ct;
uintptr_t* nm_mask_ptr;
uintptr_t cur_geno;
uintptr_t shifted_masked_geno;
uintptr_t new_geno;
uintptr_t new_mask;
while (1) {
// Desired encodings:
// new_geno: nonset homozygote -> 00
// het/missing -> 01
// set homozygote -> 10
// Given PLINK encoding xx, this is (xx - ((xx >> 1) & FIVEMASK)).
//
// new_mask: missing -> 00
// otherwise -> 11
// ...and this is (((xx >> 1) & FIVEMASK) | ((~xx) & FIVEMASK)) * 3.
//
// new_missing: missing -> 1
// otherwise -> 0
// This can be assembled via repeated CTZLU on ~new_mask.
cur_geno = *geno_ptr;
shifted_masked_geno = (cur_geno >> 1) & FIVEMASK;
new_geno = cur_geno - shifted_masked_geno;
*geno_ptr++ = new_geno;
new_mask = (((~cur_geno) & FIVEMASK) | shifted_masked_geno) * 3;
*mask_buf_ptr++ = new_mask;
new_mask = (~new_mask) & FIVEMASK;
while (new_mask) {
new_missing |= ONELU << (missing_bit_offset + (CTZLU(new_mask) / 2));
missing_ct++;
new_mask &= new_mask - 1;
}
if (geno_ptr == geno_end) {
break;
}
if (missing_bit_offset) {
missing_bit_offset = 0;
*missing_ptr++ = new_missing;
new_missing = 0;
} else {
missing_bit_offset = BITCT2;
}
}
*missing_ptr = new_missing;
if (is_x && (!weighted_x)) {
// special case #1: recode male clear homozygotes to 01 on X chromosome,
// for backwards compatibility
//
// this is a bit ugly (e.g. results are actually affected by which allele
// is A1), so may want to switch the default to mode 3
geno_ptr = geno_buf;
do {
new_geno = *geno_ptr;
*geno_ptr++ = new_geno + ((~(new_geno | (new_geno >> 1))) & (*founder_male_include2++));
} while (geno_ptr < geno_end);
}
geno_ptr = geno_buf;
while (1) {
new_geno = *geno_ptr++;
sum += popcount2_long(new_geno);
new_geno = (new_geno ^ FIVEMASK) & FIVEMASK;
if (geno_ptr == geno_end) {
break;
}
ssq += popcount2_long(new_geno);
}
// have to be careful with trailing zeroes here
ssq += popcount2_long(new_geno << (BITCT - 2 * (1 + ((founder_ct - 1) % BITCT2))));
if (founder_ct % BITCT2) {
mask_buf[founder_ct / BITCT2] &= (ONELU << (2 * (founder_ct % BITCT2))) - ONELU;
}
if (is_x && weighted_x) {
// special case #2: double-count nonmales
geno_ptr = geno_buf;
sum -= founder_ct;
nonmale_geno = &(nonmale_geno[nonmale_offset]);
nonmale_masks = &(nonmale_masks[nonmale_offset]);
mask_buf_ptr = mask_buf;
nm_mask_ptr = nonmale_masks;
while (1) {
new_mask = ~((*founder_male_include2) * 3);
new_geno = ((*geno_ptr++) & new_mask) | (*founder_male_include2++);
*nonmale_geno++ = new_geno;
*nm_mask_ptr++ = new_mask & (*mask_buf_ptr++);
sum += popcount2_long(new_geno);
new_geno = (new_geno ^ FIVEMASK) & FIVEMASK;
if (geno_ptr == geno_end) {
break;
}
ssq += popcount2_long(new_geno);
}
ssq += popcount2_long(new_geno << (BITCT - 2 * (1 + ((founder_ct - 1) % BITCT2))));
missing_ct += founder_ct - (popcount_longs(nonmale_masks, founder_ctl2) / 2);
founder_ct *= 2;
} else if (!missing_ct) {
// save sum and (n^2)/variance, for faster processing of pairwise
// no-missing-calls case
llii = (int64_t)((uint64_t)ssq) * founder_ct - ((int64_t)sum) * sum;
if (!llii) {
return 0;
}
*missing_ct_ptr = 0;
*sum_ptr = (double)sum;
*variance_recip_ptr = 1.0 / ((double)llii);
return 1;
}
*missing_ct_ptr = missing_ct;
return (((int64_t)((uint64_t)ssq)) * (founder_ct - missing_ct) - ((int64_t)sum) * sum)? 1 : 0;
}
uint32_t ld_prune_next_valid_chrom_start(uintptr_t* marker_exclude, uint32_t cur_uidx, Chrom_info* chrom_info_ptr, uint32_t chrom_code_end, uint32_t unfiltered_marker_ct) {
uint32_t chrom_idx;
cur_uidx = next_unset(marker_exclude, cur_uidx, unfiltered_marker_ct);
while (cur_uidx < unfiltered_marker_ct) {
chrom_idx = get_marker_chrom(chrom_info_ptr, cur_uidx);
// --aec 0 support
if (chrom_idx && (chrom_idx < chrom_code_end)) {
return cur_uidx;
}
cur_uidx = next_unset(marker_exclude, chrom_info_ptr->chrom_end[chrom_idx], unfiltered_marker_ct);
}
return cur_uidx;
}
void ld_prune_start_chrom(uint32_t ld_window_kb, uint32_t* cur_chrom_ptr, uint32_t* chrom_end_ptr, uint32_t window_unfiltered_start, uint32_t* live_indices, uint32_t* start_arr, uint32_t* window_unfiltered_end_ptr, uint32_t ld_window_size, uint32_t* cur_window_size_ptr, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude, Chrom_info* chrom_info_ptr, uint32_t* marker_pos, uint32_t* is_haploid_ptr, uint32_t* is_x_ptr, uint32_t* is_y_ptr) {
uint32_t cur_chrom = get_marker_chrom(chrom_info_ptr, window_unfiltered_start);
uint32_t window_unfiltered_end = window_unfiltered_start + 1;
uint32_t chrom_end = chrom_info_ptr->chrom_end[cur_chrom];
uint32_t uii = 0;
uint32_t window_size;
live_indices[0] = window_unfiltered_start;
if (ld_window_kb) {
window_size = 0;
while ((window_unfiltered_start + window_size < chrom_end) && (marker_pos[window_unfiltered_start + window_size] <= marker_pos[window_unfiltered_start] + (1000 * ld_window_size))) {
window_size++;
}
} else {
window_size = ld_window_size;
}
for (uii = 1; uii < window_size; window_unfiltered_end++, uii++) {
next_unset_ck(marker_exclude, &window_unfiltered_end, chrom_end);
if (window_unfiltered_end == chrom_end) {
break;
}
start_arr[uii - 1] = window_unfiltered_end;
live_indices[uii] = window_unfiltered_end;
}
*cur_window_size_ptr = uii;
start_arr[uii - 1] = window_unfiltered_end;
*cur_chrom_ptr = cur_chrom;
*chrom_end_ptr = chrom_end;
*window_unfiltered_end_ptr = window_unfiltered_end;
*is_haploid_ptr = IS_SET(chrom_info_ptr->haploid_mask, cur_chrom);
*is_x_ptr = (((int32_t)cur_chrom) == chrom_info_ptr->x_code);
*is_y_ptr = (((int32_t)cur_chrom) == chrom_info_ptr->y_code);
}
int32_t ld_prune_write(char* outname, char* outname_end, uintptr_t* marker_exclude, uintptr_t* pruned_arr, char* marker_ids, uintptr_t max_marker_id_len, Chrom_info* chrom_info_ptr, uint32_t chrom_code_end) {
FILE* outfile = NULL;
int32_t retval = 0;
uint32_t cur_chrom;
uint32_t chrom_end;
uint32_t marker_uidx;
fputs("Writing...", stdout);
fflush(stdout);
strcpy(outname_end, ".prune.in");
if (fopen_checked(&outfile, outname, "w")) {
goto ld_prune_write_ret_OPEN_FAIL;
}
for (cur_chrom = 1; cur_chrom < chrom_code_end; cur_chrom++) {
chrom_end = chrom_info_ptr->chrom_end[cur_chrom];
for (marker_uidx = chrom_info_ptr->chrom_start[cur_chrom]; marker_uidx < chrom_end; marker_uidx++) {
// pruned_arr initialized to marker_exclude
if (!IS_SET(pruned_arr, marker_uidx)) {
fputs(&(marker_ids[marker_uidx * max_marker_id_len]), outfile);
putc('\n', outfile);
}
}
}
if (fclose_null(&outfile)) {
goto ld_prune_write_ret_WRITE_FAIL;
}
strcpy(outname_end, ".prune.out");
if (fopen_checked(&outfile, outname, "w")) {
goto ld_prune_write_ret_OPEN_FAIL;
}
for (cur_chrom = 1; cur_chrom < chrom_code_end; cur_chrom++) {
chrom_end = chrom_info_ptr->chrom_end[cur_chrom];
for (marker_uidx = chrom_info_ptr->chrom_start[cur_chrom]; marker_uidx < chrom_end; marker_uidx++) {
if ((!IS_SET(marker_exclude, marker_uidx)) && IS_SET(pruned_arr, marker_uidx)) {
fputs(&(marker_ids[marker_uidx * max_marker_id_len]), outfile);
putc('\n', outfile);
}
}
}
if (fclose_null(&outfile)) {
goto ld_prune_write_ret_WRITE_FAIL;
}
*outname_end = '\0';
putchar('\r');
LOGPRINTFWW("Marker lists written to %s.prune.in and %s.prune.out .\n", outname, outname);
while (0) {
ld_prune_write_ret_OPEN_FAIL:
retval = RET_OPEN_FAIL;
break;
ld_prune_write_ret_WRITE_FAIL:
retval = RET_WRITE_FAIL;
break;
}
fclose_cond(outfile);
return retval;
}
int32_t ld_prune(Ld_info* ldip, FILE* bedfile, uintptr_t bed_offset, uintptr_t marker_ct, uintptr_t unfiltered_marker_ct, uintptr_t* marker_exclude, uintptr_t* marker_reverse, char* marker_ids, uintptr_t max_marker_id_len, Chrom_info* chrom_info_ptr, double* set_allele_freqs, uint32_t* marker_pos, uintptr_t unfiltered_sample_ct, uintptr_t* founder_info, uintptr_t* sex_male, char* outname, char* outname_end, uint32_t hh_exists) {
// Results are slightly different from PLINK 1.07 when missing data is
// present, but that's due to a minor bug in 1.07 (sample per-marker
// variances don't exclude the missing markers).
// for future consideration: chromosome-based multithread/parallel?
unsigned char* wkspace_mark = wkspace_base;
uintptr_t unfiltered_marker_ctl = (unfiltered_marker_ct + (BITCT - 1)) / BITCT;
uintptr_t unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4;
uintptr_t unfiltered_sample_ctl2 = 2 * ((unfiltered_sample_ct + (BITCT - 1)) / BITCT);
uintptr_t founder_ct = popcount_longs(founder_info, unfiltered_sample_ctl2 / 2);
uintptr_t founder_ctl = (founder_ct + BITCT - 1) / BITCT;
#ifdef __LP64__
uintptr_t founder_ctv = 2 * ((founder_ct + 127) / 128);
#else
uintptr_t founder_ctv = founder_ctl;
#endif
uintptr_t founder_ct_mld = (founder_ct + MULTIPLEX_LD - 1) / MULTIPLEX_LD;
uint32_t founder_ct_mld_m1 = ((uint32_t)founder_ct_mld) - 1;
#ifdef __LP64__
uint32_t founder_ct_mld_rem = (MULTIPLEX_LD / 192) - (founder_ct_mld * MULTIPLEX_LD - founder_ct) / 192;
#else
uint32_t founder_ct_mld_rem = (MULTIPLEX_LD / 48) - (founder_ct_mld * MULTIPLEX_LD - founder_ct) / 48;
#endif
uintptr_t founder_ct_192_long = founder_ct_mld_m1 * (MULTIPLEX_LD / BITCT2) + founder_ct_mld_rem * (192 / BITCT2);
uintptr_t final_mask = get_final_mask(founder_ct);
uint32_t weighted_founder_ct = founder_ct;
uint32_t founder_trail_ct = founder_ct_192_long - founder_ctl * 2;
uint32_t pairwise = (ldip->modifier / LD_PRUNE_PAIRWISE) & 1;
uint32_t ignore_x = (ldip->modifier / LD_IGNORE_X) & 1;
uint32_t weighted_x = (ldip->modifier / LD_WEIGHTED_X) & 1;
uint32_t window_is_kb = (ldip->modifier / LD_PRUNE_KB_WINDOW) & 1;
uint32_t ld_window_size = ldip->prune_window_size;
uint32_t ld_window_incr = ldip->prune_window_incr;
double ld_last_param = ldip->prune_last_param;
uint32_t nonmale_founder_ct = 0;
uintptr_t window_max = 1;
uintptr_t* geno = NULL;
uintptr_t* founder_include2 = NULL;
uintptr_t* founder_male_include2 = NULL;
uintptr_t* nonmale_geno = NULL;
uintptr_t* nonmale_masks = NULL;
double* cov_matrix = NULL;
double* new_cov_matrix = NULL;
MATRIX_INVERT_BUF1_TYPE* irow = NULL;
double* work = NULL;
uint32_t* idx_remap = NULL;
uint32_t tot_exclude_ct = 0;
uint32_t at_least_one_prune = 0;
uint32_t chrom_code_end = chrom_info_ptr->max_code + 1 + chrom_info_ptr->name_ct;
int32_t retval = 0;
uintptr_t* geno_masks;
uintptr_t* geno_mmasks;
uintptr_t* pruned_arr;
uint32_t* live_indices;
uint32_t* start_arr;
uint32_t pct;
uint32_t pct_thresh;
uint32_t window_unfiltered_start;
uint32_t window_unfiltered_end;
uint32_t cur_window_size;
uint32_t old_window_size;
uint32_t uii;
uint32_t ujj;
uint32_t ukk;
int32_t ii;
uint32_t cur_chrom;
uint32_t chrom_end;
uint32_t is_haploid;
uint32_t is_x;
uint32_t is_y;
uintptr_t* loadbuf;
double* sums;
double* variance_recips; // entries are actually n^2 / variance
uint32_t* missing_cts;
uint32_t fixed_missing_ct;
uintptr_t ulii;
double dxx;
double dyy;
double cov12;
uint32_t fixed_non_missing_ct;
uint32_t non_missing_ct;
int32_t dp_result[5];
double non_missing_ctd;
uintptr_t* geno_fixed_vec_ptr;
uintptr_t* geno_var_vec_ptr;
uintptr_t* mask_fixed_vec_ptr;
uintptr_t* mask_var_vec_ptr;
uintptr_t cur_exclude_ct;
uint32_t prev_end;
__CLPK_integer window_rem_li;
uint32_t old_window_rem;
uint32_t window_rem;
uint32_t bsearch_min;
uint32_t bsearch_max;
uint32_t bsearch_cur;
double prune_ld_thresh;
if (founder_ct < 2) {
LOGPRINTF("Warning: Skipping --indep%s since there are less than two founders.\n(--make-founders may come in handy here.)\n", pairwise? "-pairwise" : "");
goto ld_prune_ret_1;
}
if (is_set(chrom_info_ptr->chrom_mask, 0)) {
ulii = count_chrom_markers(chrom_info_ptr, 0, marker_exclude);
if (chrom_info_ptr->zero_extra_chroms) {
for (uii = chrom_info_ptr->max_code + 1; uii < chrom_code_end; uii++) {
ulii += count_chrom_markers(chrom_info_ptr, uii, marker_exclude);
}
chrom_code_end = chrom_info_ptr->max_code + 1;
}
marker_ct -= ulii;
LOGPRINTF("--indep%s: Ignoring %" PRIuPTR " chromosome 0 variant%s.\n", pairwise? "-pairwise" : "", ulii, (ulii == 1)? "" : "s");
}
if (marker_ct < 2) {
LOGPRINTF("Error: Too few valid variants for --indep%s.\n", pairwise? "-pairwise" : "");
goto ld_prune_ret_INVALID_FORMAT;
}
// force founder_male_include2 allocation
if (alloc_collapsed_haploid_filters(unfiltered_sample_ct, founder_ct, XMHH_EXISTS | hh_exists, 1, founder_info, sex_male, &founder_include2, &founder_male_include2)) {
goto ld_prune_ret_NOMEM;
}
if (weighted_x) {
nonmale_founder_ct = founder_ct - popcount01_longs(founder_male_include2, founder_ctl);
if (founder_ct + nonmale_founder_ct > 0x7fffffff) {
// no, this shouldn't ever happen, but may as well document that there
// theoretically is a 32-bit integer range issue here
logprint("Error: Too many founders for --indep[-pairwise] + --ld-xchr 3.\n");
goto ld_prune_ret_1;
}
}
if (window_is_kb) {
// determine maximum number of markers that may need to be loaded at once
for (cur_chrom = 1; cur_chrom < chrom_code_end; cur_chrom++) {
if (chrom_exists(chrom_info_ptr, cur_chrom)) {
window_max = chrom_window_max(marker_pos, marker_exclude, chrom_info_ptr, cur_chrom, 0x7fffffff, ld_window_size * 1000, window_max);
}
}
}
if (pairwise) {
prune_ld_thresh = ld_last_param * (1 + SMALL_EPSILON);
} else {
#ifdef __LP64__
if (window_max > 46340) {
// todo: check what LAPACK's matrix inversion limit actually is. Guess
// sqrt(2^31 - 1) for now.
logprint("Error: --indep does not currently support window sizes > 46340.\n");
goto ld_prune_ret_INVALID_CMDLINE;
}
#endif
// r, not r2, in this case
prune_ld_thresh = 0.999999;
}
window_unfiltered_start = ld_prune_next_valid_chrom_start(marker_exclude, 0, chrom_info_ptr, chrom_code_end, unfiltered_marker_ct);
if (wkspace_alloc_ul_checked(&pruned_arr, unfiltered_marker_ctl * sizeof(intptr_t))) {
goto ld_prune_ret_NOMEM;
}
memcpy(pruned_arr, marker_exclude, unfiltered_marker_ctl * sizeof(intptr_t));
if (!window_is_kb) {
window_max = ld_window_size;
}
ulii = window_max;
if (wkspace_alloc_ui_checked(&live_indices, ulii * sizeof(int32_t)) ||
wkspace_alloc_ui_checked(&start_arr, ulii * sizeof(int32_t)) ||
wkspace_alloc_ul_checked(&loadbuf, unfiltered_sample_ctl2 * sizeof(intptr_t)) ||
wkspace_alloc_ul_checked(&geno, ulii * founder_ct_192_long * sizeof(intptr_t)) ||
wkspace_alloc_ul_checked(&geno_masks, ulii * founder_ct_192_long * sizeof(intptr_t)) ||
wkspace_alloc_ul_checked(&geno_mmasks, ulii * founder_ctv * sizeof(intptr_t)) ||
wkspace_alloc_ui_checked(&missing_cts, ulii * sizeof(int32_t)) ||
wkspace_alloc_d_checked(&sums, ulii * sizeof(double)) ||
wkspace_alloc_d_checked(&variance_recips, ulii * sizeof(double))) {
goto ld_prune_ret_NOMEM;
}
if (weighted_x) {
if (wkspace_alloc_ul_checked(&nonmale_geno, ulii * founder_ct_192_long * sizeof(intptr_t)) ||
wkspace_alloc_ul_checked(&nonmale_masks, ulii * founder_ct_192_long * sizeof(intptr_t))) {
goto ld_prune_ret_NOMEM;
}
}
for (ulii = 1; ulii <= window_max; ulii++) {
fill_ulong_zero(&(geno[ulii * founder_ct_192_long - founder_trail_ct - 2]), founder_trail_ct + 2);
fill_ulong_zero(&(geno_masks[ulii * founder_ct_192_long - founder_trail_ct - 2]), founder_trail_ct + 2);
if (weighted_x) {
fill_ulong_zero(&(nonmale_geno[ulii * founder_ct_192_long - founder_trail_ct - 2]), founder_trail_ct + 2);
fill_ulong_zero(&(nonmale_masks[ulii * founder_ct_192_long - founder_trail_ct - 2]), founder_trail_ct + 2);
}
}
if (!pairwise) {
if (wkspace_alloc_d_checked(&cov_matrix, window_max * window_max * sizeof(double)) ||
wkspace_alloc_d_checked(&new_cov_matrix, window_max * window_max * sizeof(double)) ||
wkspace_alloc_ui_checked(&idx_remap, window_max * sizeof(int32_t))) {
goto ld_prune_ret_NOMEM;
}
irow = (MATRIX_INVERT_BUF1_TYPE*)wkspace_alloc(window_max * 2 * sizeof(MATRIX_INVERT_BUF1_TYPE));
if (!irow) {
goto ld_prune_ret_NOMEM;
}
if (window_max < 4) {
ulii = 4;
} else {
ulii = window_max;
}
if (wkspace_alloc_d_checked(&work, ulii * window_max * sizeof(double))) {
goto ld_prune_ret_NOMEM;
}
}
do {
prev_end = 0;
ld_prune_start_chrom(window_is_kb, &cur_chrom, &chrom_end, window_unfiltered_start, live_indices, start_arr, &window_unfiltered_end, ld_window_size, &cur_window_size, unfiltered_marker_ct, pruned_arr, chrom_info_ptr, marker_pos, &is_haploid, &is_x, &is_y);
if (weighted_x) {
if (is_x) {
weighted_founder_ct = 2 * founder_ct;
} else {
weighted_founder_ct = founder_ct;
}
}
old_window_size = 0;
cur_exclude_ct = 0;
if (cur_window_size > 1) {
for (ulii = 0; ulii < (uintptr_t)cur_window_size; ulii++) {
uii = live_indices[ulii];
if (fseeko(bedfile, bed_offset + (uii * ((uint64_t)unfiltered_sample_ct4)), SEEK_SET)) {
goto ld_prune_ret_READ_FAIL;
}
if (load_and_collapse_incl(bedfile, loadbuf, unfiltered_sample_ct, &(geno[ulii * founder_ct_192_long]), founder_ct, founder_info, final_mask, IS_SET(marker_reverse, uii))) {
goto ld_prune_ret_READ_FAIL;
}
if (is_haploid && hh_exists) {
haploid_fix(hh_exists, founder_include2, founder_male_include2, founder_ct, is_x, is_y, (unsigned char*)(&(geno[ulii * founder_ct_192_long])));
}
if (!ld_process_load(&(geno[ulii * founder_ct_192_long]), &(geno_masks[ulii * founder_ct_192_long]), &(geno_mmasks[ulii * founder_ctv]), &(missing_cts[ulii]), &(sums[ulii]), &(variance_recips[ulii]), founder_ct, is_x && (!ignore_x), weighted_x, nonmale_founder_ct, founder_male_include2, nonmale_geno, nonmale_masks, ulii * founder_ct_192_long)) {
SET_BIT(pruned_arr, uii);
cur_exclude_ct++;
}
}
}
pct = 1;
pct_thresh = window_unfiltered_start + ((uint64_t)pct * (chrom_end - chrom_info_ptr->chrom_start[cur_chrom])) / 100;
while ((window_unfiltered_start < chrom_end) || (cur_window_size > 1)) {
if (cur_window_size > 1) {
do {
at_least_one_prune = 0;
for (uii = 0; uii < cur_window_size - 1; uii++) {
if (IS_SET(pruned_arr, live_indices[uii])) {
continue;
}
fixed_missing_ct = missing_cts[uii];
fixed_non_missing_ct = weighted_founder_ct - fixed_missing_ct;
geno_fixed_vec_ptr = &(geno[uii * founder_ct_192_long]);
mask_fixed_vec_ptr = &(geno_masks[uii * founder_ct_192_long]);
ujj = uii + 1;
while (live_indices[ujj] < start_arr[uii]) {
if (++ujj == cur_window_size) {
break;
}
}
for (; ujj < cur_window_size; ujj++) {
if (IS_SET(pruned_arr, live_indices[ujj])) {
continue;
}
geno_var_vec_ptr = &(geno[ujj * founder_ct_192_long]);
if ((!fixed_missing_ct) && (!missing_cts[ujj]) && ((!is_x) || (!weighted_x))) {
cov12 = (double)(ld_dot_prod_nm(geno_fixed_vec_ptr, geno_var_vec_ptr, weighted_founder_ct, founder_ct_mld_m1, founder_ct_mld_rem) * ((int64_t)founder_ct)) - sums[uii] * sums[ujj];
dxx = variance_recips[uii] * variance_recips[ujj];
} else {
mask_var_vec_ptr = &(geno_masks[ujj * founder_ct_192_long]);
dp_result[0] = weighted_founder_ct;
// reversed from what I initially thought because I'm passing
// the ujj-associated buffers before the uii-associated ones.
dp_result[1] = -((int32_t)fixed_non_missing_ct);
dp_result[2] = missing_cts[ujj] - weighted_founder_ct;
dp_result[3] = dp_result[1];