forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plink_homozyg.c
2763 lines (2668 loc) · 110 KB
/
plink_homozyg.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_homozyg.h"
void homozyg_init(Homozyg_info* homozyg_ptr) {
homozyg_ptr->modifier = 0;
homozyg_ptr->min_snp = 100;
homozyg_ptr->min_bases = 1000000;
homozyg_ptr->max_bases_per_snp = 50000.0 + EPSILON;
homozyg_ptr->max_hets = 0xffffffffU;
homozyg_ptr->max_gap = 1000000;
homozyg_ptr->window_size = 50;
homozyg_ptr->window_max_hets = 1;
homozyg_ptr->window_max_missing = 5;
homozyg_ptr->hit_threshold = 0.05;
homozyg_ptr->overlap_min = 0.95;
homozyg_ptr->pool_size_min = 2;
}
void mask_out_homozyg_major(uintptr_t* readbuf_cur, uint32_t sample_ct) {
// if readbuf_cur were 16-byte aligned, this could be vectorized, but it
// isn't, and this isn't a limiting step anyway
uintptr_t* readbuf_cur_end = &(readbuf_cur[(sample_ct + (BITCT2 - 1)) / BITCT2]);
uintptr_t cur_word;
do {
cur_word = *readbuf_cur;
*readbuf_cur &= ((cur_word ^ (cur_word >> 1)) & FIVEMASK) * (3 * ONELU);
} while (++readbuf_cur < readbuf_cur_end);
}
void increment_het_missing(uintptr_t* readbuf_cur, uint32_t sample_ct, uint32_t* het_cts, uint32_t* missing_cts, int32_t incr) {
// assumes homozyg majors masked out
uint32_t sample_idx_offset;
uintptr_t cur_word;
uint32_t last_set_bit;
for (sample_idx_offset = 0; sample_idx_offset < sample_ct; sample_idx_offset += BITCT2) {
cur_word = *readbuf_cur++;
while (cur_word) {
last_set_bit = CTZLU(cur_word);
if (last_set_bit & 1) {
het_cts[sample_idx_offset + (last_set_bit / 2)] += incr;
} else {
missing_cts[sample_idx_offset + (last_set_bit / 2)] += incr;
}
cur_word &= cur_word - ONELU;
}
}
}
void decrement_het_missing_extend(uintptr_t* readbuf_cur, uint32_t sample_ct, uint32_t* het_cts, uint32_t* missing_cts, uint32_t* end_nonhom_uidxs, uint32_t next_uidx) {
uint32_t sample_idx_offset;
uint32_t sample_idx;
uintptr_t cur_word;
uint32_t last_set_bit;
for (sample_idx_offset = 0; sample_idx_offset < sample_ct; sample_idx_offset += BITCT2) {
cur_word = *readbuf_cur++;
while (cur_word) {
last_set_bit = CTZLU(cur_word);
sample_idx = sample_idx_offset + (last_set_bit / 2);
if (last_set_bit & 1) {
het_cts[sample_idx] -= 1;
} else {
missing_cts[sample_idx] -= 1;
}
end_nonhom_uidxs[sample_idx] = next_uidx;
cur_word &= cur_word - ONELU;
}
}
}
void update_end_nonhom(uintptr_t* readbuf_cur, uint32_t sample_ct, uint32_t* end_nonhom_uidxs, uint32_t next_uidx) {
uint32_t sample_idx_offset;
uintptr_t cur_word;
uint32_t last_set_bit;
for (sample_idx_offset = 0; sample_idx_offset < sample_ct; sample_idx_offset += BITCT2) {
cur_word = *readbuf_cur++;
while (cur_word) {
last_set_bit = CTZLU(cur_word);
end_nonhom_uidxs[sample_idx_offset + (last_set_bit / 2)] = next_uidx;
cur_word &= cur_word - ONELU;
}
}
}
#ifdef __LP64__
#define ROH_ENTRY_INTS 7
#else
#define ROH_ENTRY_INTS 6
#endif
void roh_extend_forward(uint32_t* marker_pos, uintptr_t* marker_exclude, uint32_t marker_uidx, uint32_t nsnp_incr, uint32_t is_new_lengths, double max_bases_per_snp, uint32_t* roh_list_cur) {
// marker_uidx should be the index of the marker immediately *after* the last
// eligible one.
int32_t subtract_pos = (int32_t)(marker_pos[roh_list_cur[0]] - is_new_lengths);
uint32_t orig_nsnp = roh_list_cur[2];
while (nsnp_incr) {
prev_unset_unsafe_ck(marker_exclude, &marker_uidx);
if (((int32_t)(orig_nsnp + nsnp_incr)) * max_bases_per_snp >= (double)(marker_pos[marker_uidx] - subtract_pos)) {
roh_list_cur[1] = marker_uidx;
roh_list_cur[2] += nsnp_incr;
roh_list_cur[3] += nsnp_incr;
return;
}
nsnp_incr--;
}
}
void save_confirmed_roh_extend(uint32_t uidx_first, uint32_t older_uidx, uint32_t cidx_len, uint32_t cur_roh_het_ct, uint32_t cur_roh_missing_ct, uintptr_t* sample_last_roh_idx_ptr, uintptr_t* roh_ct_ptr, uint32_t* roh_list, uint32_t* marker_pos, uintptr_t* marker_exclude, uintptr_t marker_uidx, uint32_t is_new_lengths, double max_bases_per_snp, uint32_t* prev_roh_end_cidx_ptr, uint32_t* cur_roh_earliest_extend_uidx_ptr, uint32_t cidx_cur) {
// This is straightforward when the 'extend' modifier isn't present, we just
// append to roh_list. It's a bit more complicated with 'extend':
//
// * When a new ROH is confirmed, we extend it backwards as far as possible.
// In a few pathological cases, this actually means we merge it with the
// previous ROH (consider:
// --homozyg-snp 2
// --homozyg-window-het 0
// --homozyg-window-threshold 0.0727
// chromosome with 99 loci: 24 hets, then 51 homs, then 24 hets), if that
// doesn't blow the --homozyg-density or --homozyg-het limit.
// * We defer the forwards extension until the main loop passes the last
// adjacent homozygous call after the ROH (prev_roh_end_cidxs[] is set to
// 0xffffffffU when this happens). It is theoretically possible to be in
// another ROH candidate at this point (consider what happens to the
// example above when --homozyg-window-het is changed to 1, and the 74th
// site is changed to het), but w.r.t. the density constraint, it's safe to
// first extend the previous ROH forwards and then try to merge if the new
// ROH is confirmed.
// However, --homozyg-het with a nonzero parameter can lead to ambiguity:
// --homozyg-snp 2
// --homozyg-window-het 2
// --homozyg-het 1
// --homozyg-window-threshold 0.0816
// chromosome with 96 loci: 24 hets, then 48 homs, then 24 hets
// so for now we actually prohibit it from being specified with 'extend' on
// the command line (this shouldn't be a big loss since we do permit
// --homozyg-het 0).
//
// This is biased towards backwards extension if the ROH is near the density
// bound, but that also shouldn't be a big deal. (Enough minutiae for
// now...)
uintptr_t roh_ct = *roh_ct_ptr;
uintptr_t sample_last_roh_idx = *sample_last_roh_idx_ptr;
uint32_t cidx_start = cidx_cur - cidx_len;
uint32_t* prev_roh_list_entry;
uint32_t prev_roh_end_cidx;
uint32_t prev_nsnp;
uint32_t extended_nsnp;
uint32_t add_pos;
uint32_t cur_uidx_first;
prev_roh_end_cidx = *prev_roh_end_cidx_ptr;
*prev_roh_end_cidx_ptr = cidx_cur;
add_pos = marker_pos[older_uidx] + is_new_lengths;
if (prev_roh_end_cidx == 0xffffffffU) {
cur_uidx_first = *cur_roh_earliest_extend_uidx_ptr;
} else {
// we're still in the same string of homozygous calls that the last ROH
// ended in. direct merge?
prev_roh_list_entry = &(roh_list[sample_last_roh_idx * ROH_ENTRY_INTS]);
prev_nsnp = prev_roh_list_entry[2];
extended_nsnp = prev_nsnp + cidx_cur - prev_roh_end_cidx;
// only thing that can prevent it at this point is --homozyg-density
// bound violation; every other condition was checked incrementally
// (including --homozyg-het since we've prevented it from being given any
// value other than 0 or infinity when 'extend' is present)
if (((double)((int32_t)extended_nsnp)) * max_bases_per_snp >= ((double)(add_pos - marker_pos[prev_roh_list_entry[0]]))) {
// technically the merge can unlock even more backward extension, but
// I'm just gonna a draw a line here and not check for that since we're
// already inside a corner case that should be irrelevant in practice.
prev_roh_list_entry[1] = older_uidx;
prev_roh_list_entry[2] = extended_nsnp;
// previous hom_ct = prev_nsnp - prev_het_ct - prev_missing_ct
// -> prev_het_ct + prev_missing_ct = prev_nsnp - prev_hom_ct
//
// new hom_ct = extended_nsnp - cur_roh_het_ct - cur_roh_missing_ct -
// (prev_het_ct + prev_missing_ct)
// = extended_nsnp - cur_roh_het_ct - cur_roh_missing_ct -
// (prev_nsnp - prev_hom_ct)
// = prev_hom_ct + extended_nsnp - cur_roh_het_ct -
// cur_roh_missing_ct - prev_nsnp
prev_roh_list_entry[3] += extended_nsnp - cur_roh_het_ct - cur_roh_missing_ct - prev_nsnp;
prev_roh_list_entry[4] += cur_roh_het_ct;
return;
}
// extend previous ROH as far as possible before current ROH's start,
// then extend current one backward
roh_extend_forward(marker_pos, marker_exclude, uidx_first, cidx_start - prev_roh_end_cidx, is_new_lengths, max_bases_per_snp, prev_roh_list_entry);
cur_uidx_first = prev_roh_list_entry[1] + 1;
next_unset_unsafe_ck(marker_exclude, &cur_uidx_first);
}
if (cur_uidx_first < uidx_first) {
extended_nsnp = cidx_len + uidx_first - cur_uidx_first - popcount_bit_idx(marker_exclude, cur_uidx_first, uidx_first);
do {
if (((int32_t)extended_nsnp) * max_bases_per_snp >= (double)(add_pos - marker_pos[cur_uidx_first])) {
uidx_first = cur_uidx_first;
cidx_len = extended_nsnp;
break;
}
extended_nsnp--;
cur_uidx_first++;
next_unset_unsafe_ck(marker_exclude, &cur_uidx_first);
} while (cur_uidx_first < uidx_first);
}
roh_list = &(roh_list[roh_ct * ROH_ENTRY_INTS]);
*roh_list++ = uidx_first;
*roh_list++ = older_uidx;
*roh_list++ = cidx_len;
*roh_list++ = cidx_len - cur_roh_het_ct - cur_roh_missing_ct;
*roh_list++ = cur_roh_het_ct;
#ifdef __LP64__
*roh_list++ = (uint32_t)sample_last_roh_idx;
*roh_list++ = (uint32_t)(sample_last_roh_idx >> 32);
#else
*roh_list++ = (uint32_t)sample_last_roh_idx;
#endif
*sample_last_roh_idx_ptr = roh_ct++;
*roh_ct_ptr = roh_ct;
}
uint32_t roh_update(Homozyg_info* hp, uintptr_t* readbuf_cur, uintptr_t* swbuf_cur, uint32_t* het_cts, uint32_t* missing_cts, uint32_t* marker_pos, uintptr_t* cur_sample_male, uint32_t sample_ct, uint32_t swhit_min, uint32_t older_uidx, uint32_t old_uidx, uint32_t marker_cidx, uintptr_t max_roh_ct, uint32_t* swhit_cts, uint32_t* cur_roh_uidx_starts, uint32_t* cur_roh_cidx_starts, uint32_t* cur_roh_het_cts, uint32_t* cur_roh_missing_cts, uintptr_t* sample_to_last_roh, uint32_t* roh_list, uintptr_t* roh_ct_ptr, uintptr_t* marker_exclude, uint32_t* prev_roh_end_cidxs, uint32_t* end_nonhom_uidxs, uint32_t* cur_roh_earliest_extend_uidxs) {
// Finishes processing current marker, saving all ROH that end at the
// previous one. If readbuf_cur is NULL, the previous marker is assumed to
// be the last one on the chromosome.
uint32_t* roh_list_cur = &(roh_list[(*roh_ct_ptr) * ROH_ENTRY_INTS]);
uint32_t min_snp = hp->min_snp;
uint32_t min_bases = hp->min_bases;
double max_bases_per_snp = hp->max_bases_per_snp;
uint32_t max_hets = hp->max_hets;
uint32_t max_sw_hets = hp->window_max_hets;
uint32_t max_sw_missings = hp->window_max_missing;
uint32_t is_new_lengths = 1 ^ ((hp->modifier / HOMOZYG_OLD_LENGTHS) & 1);
uint32_t forced_end = (marker_pos[old_uidx] - marker_pos[older_uidx]) > hp->max_gap;
uint32_t is_cur_hit = 0;
uintptr_t cur_word = 0;
uintptr_t cur_call;
uintptr_t last_roh_idx;
uint32_t sample_idx;
uint32_t cidx_len;
uint32_t uidx_first;
uint32_t base_len;
uint32_t cur_het_ct;
if (!prev_roh_end_cidxs) {
// ~25% performance penalty if we don't separate this out
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) {
if (!(sample_idx & (BITCT2 - 1))) {
if (readbuf_cur) {
cur_word = *readbuf_cur++;
}
} else {
cur_word >>= 2;
}
if (cur_sample_male && IS_SET(cur_sample_male, sample_idx)) {
// skip males on Xchr (cur_sample_male should be NULL when not Xchr)
continue;
}
if (readbuf_cur) {
if (swbuf_cur) {
if ((het_cts[sample_idx] <= max_sw_hets) && (missing_cts[sample_idx] <= max_sw_missings)) {
SET_BIT(swbuf_cur, sample_idx);
swhit_cts[sample_idx] += 1;
}
}
is_cur_hit = (swhit_cts[sample_idx] >= swhit_min);
}
cur_call = cur_word & (3 * ONELU);
if (cur_roh_cidx_starts[sample_idx] != 0xffffffffU) {
if ((!is_cur_hit) || ((cur_call == 2) && (cur_roh_het_cts[sample_idx] == max_hets)) || forced_end) {
cidx_len = marker_cidx - cur_roh_cidx_starts[sample_idx];
if (cidx_len >= min_snp) {
uidx_first = cur_roh_uidx_starts[sample_idx];
base_len = marker_pos[older_uidx] + is_new_lengths - marker_pos[uidx_first];
if ((base_len >= min_bases) && (((double)((int32_t)cidx_len)) * max_bases_per_snp >= ((double)base_len))) {
if (*roh_ct_ptr == max_roh_ct) {
return 1;
}
*roh_list_cur++ = uidx_first;
*roh_list_cur++ = older_uidx;
*roh_list_cur++ = cidx_len;
cur_het_ct = cur_roh_het_cts[sample_idx];
*roh_list_cur++ = cidx_len - cur_het_ct - cur_roh_missing_cts[sample_idx];
*roh_list_cur++ = cur_het_ct;
last_roh_idx = sample_to_last_roh[sample_idx];
#ifdef __LP64__
*roh_list_cur++ = (uint32_t)last_roh_idx;
*roh_list_cur++ = (uint32_t)(last_roh_idx >> 32);
#else
*roh_list_cur++ = (uint32_t)last_roh_idx;
#endif
sample_to_last_roh[sample_idx] = *roh_ct_ptr;
*roh_ct_ptr += 1;
}
}
cur_roh_cidx_starts[sample_idx] = 0xffffffffU;
}
}
if (is_cur_hit) {
if (cur_roh_cidx_starts[sample_idx] == 0xffffffffU) {
if ((!max_hets) && (cur_call == 2)) {
continue;
}
cur_roh_uidx_starts[sample_idx] = old_uidx;
cur_roh_cidx_starts[sample_idx] = marker_cidx;
cur_roh_het_cts[sample_idx] = 0;
cur_roh_missing_cts[sample_idx] = 0;
}
if (cur_call) {
if (cur_call == 2) {
cur_roh_het_cts[sample_idx] += 1;
} else {
cur_roh_missing_cts[sample_idx] += 1;
}
}
}
}
} else {
if (forced_end) {
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) {
end_nonhom_uidxs[sample_idx] = old_uidx;
}
}
for (sample_idx = 0; sample_idx < sample_ct; sample_idx++) {
if (!(sample_idx & (BITCT2 - 1))) {
if (readbuf_cur) {
cur_word = *readbuf_cur++;
}
} else {
cur_word >>= 2;
}
if (cur_sample_male && IS_SET(cur_sample_male, sample_idx)) {
continue;
}
if (readbuf_cur) {
if (swbuf_cur) {
if ((het_cts[sample_idx] <= max_sw_hets) && (missing_cts[sample_idx] <= max_sw_missings)) {
SET_BIT(swbuf_cur, sample_idx);
swhit_cts[sample_idx] += 1;
}
}
is_cur_hit = (swhit_cts[sample_idx] >= swhit_min);
}
cur_call = cur_word & (3 * ONELU);
if (cur_roh_cidx_starts[sample_idx] != 0xffffffffU) {
if ((!is_cur_hit) || ((cur_call == 2) && (cur_roh_het_cts[sample_idx] == max_hets)) || forced_end) {
cidx_len = marker_cidx - cur_roh_cidx_starts[sample_idx];
if (cidx_len >= min_snp) {
uidx_first = cur_roh_uidx_starts[sample_idx];
base_len = marker_pos[older_uidx] + is_new_lengths - marker_pos[uidx_first];
if ((base_len >= min_bases) && (((double)((int32_t)cidx_len)) * max_bases_per_snp >= ((double)base_len))) {
if (*roh_ct_ptr == max_roh_ct) {
return 1;
}
save_confirmed_roh_extend(uidx_first, older_uidx, cidx_len, cur_roh_het_cts[sample_idx], cur_roh_missing_cts[sample_idx], &(sample_to_last_roh[sample_idx]), roh_ct_ptr, roh_list, marker_pos, marker_exclude, old_uidx, is_new_lengths, max_bases_per_snp, &(prev_roh_end_cidxs[sample_idx]), &(cur_roh_earliest_extend_uidxs[sample_idx]), marker_cidx);
}
}
cur_roh_cidx_starts[sample_idx] = 0xffffffffU;
if (cur_call || forced_end) {
prev_roh_end_cidxs[sample_idx] = 0xffffffffU;
}
}
} else if ((cur_call || forced_end) && (prev_roh_end_cidxs[sample_idx] != 0xffffffffU)) {
roh_extend_forward(marker_pos, marker_exclude, old_uidx, marker_cidx - prev_roh_end_cidxs[sample_idx], is_new_lengths, max_bases_per_snp, &(roh_list[sample_to_last_roh[sample_idx] * ROH_ENTRY_INTS]));
prev_roh_end_cidxs[sample_idx] = 0xffffffffU;
}
if (is_cur_hit) {
if (cur_roh_cidx_starts[sample_idx] == 0xffffffffU) {
if ((!max_hets) && (cur_call == 2)) {
continue;
}
cur_roh_uidx_starts[sample_idx] = old_uidx;
cur_roh_cidx_starts[sample_idx] = marker_cidx;
cur_roh_het_cts[sample_idx] = 0;
cur_roh_missing_cts[sample_idx] = 0;
if (cur_call) {
cur_roh_earliest_extend_uidxs[sample_idx] = old_uidx;
goto roh_update_extend_nonhom;
}
cur_roh_earliest_extend_uidxs[sample_idx] = end_nonhom_uidxs[sample_idx];
} else if (cur_call) {
roh_update_extend_nonhom:
if (cur_call == 2) {
cur_roh_het_cts[sample_idx] += 1;
} else {
cur_roh_missing_cts[sample_idx] += 1;
}
}
}
}
}
return 0;
}
int32_t write_main_roh_reports(char* outname, char* outname_end, uintptr_t* marker_exclude, char* marker_ids, uintptr_t max_marker_id_len, uint32_t plink_maxsnp, Chrom_info* chrom_info_ptr, uint32_t* marker_pos, uintptr_t sample_ct, uintptr_t* sample_exclude, char* sample_ids, uint32_t plink_maxfid, uint32_t plink_maxiid, uintptr_t max_sample_id_len, uintptr_t* pheno_nm, uintptr_t* pheno_c, double* pheno_d, char* missing_pheno_str, uint32_t omp_is_numeric, uint32_t missing_pheno_len, uint32_t is_new_lengths, uintptr_t roh_ct, uint32_t* roh_list, uintptr_t* roh_list_chrom_starts, uintptr_t* sample_to_last_roh, uint32_t* max_pool_size_ptr, uint32_t* max_roh_len_ptr) {
unsigned char* wkspace_mark = wkspace_base;
FILE* outfile = NULL;
FILE* outfile_indiv = NULL;
char* wptr_iid = &(tbuf[plink_maxfid + 1]);
char* wptr_phe = &(tbuf[plink_maxfid + plink_maxiid + 2]);
int32_t* roh_ct_aff_adj = NULL;
uintptr_t next_roh_idx = 0;
uint32_t max_pool_size = 0;
uint32_t max_roh_len = 0;
int32_t retval = 0;
char* cptr;
char* cptr2;
char* wptr_chr;
char* wptr_bp1;
char* wptr;
uint32_t* cur_roh;
int32_t* roh_ct_unaff_adj;
uintptr_t sample_uidx;
uintptr_t sample_idx;
uintptr_t prev_roh_idx;
uintptr_t cur_roh_idx;
uintptr_t chrom_roh_start;
uintptr_t chrom_roh_ct;
uint32_t cur_roh_ct;
uint32_t chrom_fo_idx;
uint32_t marker_uidx1;
uint32_t marker_uidx2;
uint32_t chrom_start;
uint32_t chrom_len;
double dxx;
double dyy;
double kb_tot;
uint32_t slen;
uint32_t uii;
memcpy(outname_end, ".hom", 5);
if (fopen_checked(&outfile, outname, "w")) {
goto write_main_roh_reports_ret_OPEN_FAIL;
}
sprintf(tbuf, "%%%us %%%us PHE CHR %%%us %%%us POS1 POS2 KB NSNP DENSITY PHOM PHET\n", plink_maxfid, plink_maxiid, plink_maxsnp, plink_maxsnp);
fprintf(outfile, tbuf, "FID", "IID", "SNP1", "SNP2");
memcpy(&(outname_end[4]), ".indiv", 7);
if (fopen_checked(&outfile_indiv, outname, "w")) {
goto write_main_roh_reports_ret_OPEN_FAIL;
}
sprintf(tbuf, "%%%us %%%us PHE NSEG KB KBAVG\n", plink_maxfid, plink_maxiid);
fprintf(outfile_indiv, tbuf, "FID", "IID");
tbuf[plink_maxfid] = ' ';
tbuf[plink_maxfid + plink_maxiid + 1] = ' ';
sample_uidx = 0;
for (sample_idx = 0; sample_idx < sample_ct; sample_uidx++, sample_idx++) {
next_unset_ul_unsafe_ck(sample_exclude, &sample_uidx);
cptr = &(sample_ids[sample_uidx * max_sample_id_len]);
cptr2 = (char*)memchr(cptr, '\t', max_sample_id_len);
slen = (uintptr_t)(cptr2 - cptr);
memcpy(memseta(tbuf, 32, plink_maxfid - slen), cptr, slen);
slen = strlen(++cptr2);
memcpy(memseta(wptr_iid, 32, plink_maxiid - slen), cptr2, slen);
if (!IS_SET(pheno_nm, sample_uidx)) {
wptr_chr = fw_strcpyn(8, missing_pheno_len, missing_pheno_str, wptr_phe);
} else if (pheno_c) {
wptr_chr = memseta(wptr_phe, 32, 7);
*wptr_chr++ = '1' + IS_SET(pheno_c, sample_uidx);
} else {
wptr_chr = width_force(8, wptr_phe, double_f_writew3(wptr_phe, pheno_d[sample_uidx]));
}
*wptr_chr++ = ' ';
// traverse roh_list backwards, reversing the direction of [5], then
// traverse forwards and reset [5] again to sample_idx
cur_roh_idx = sample_to_last_roh[sample_idx];
cur_roh_ct = 0;
while (cur_roh_idx != ~ZEROLU) {
cur_roh = &(roh_list[cur_roh_idx * ROH_ENTRY_INTS]);
#ifdef __LP64__
prev_roh_idx = ((uintptr_t)cur_roh[5]) | (((uintptr_t)cur_roh[6]) << 32);
cur_roh[5] = (uint32_t)next_roh_idx;
cur_roh[6] = (uint32_t)(next_roh_idx >> 32);
#else
prev_roh_idx = (uintptr_t)cur_roh[5];
cur_roh[5] = (uint32_t)next_roh_idx;
#endif
next_roh_idx = cur_roh_idx;
cur_roh_idx = prev_roh_idx;
cur_roh_ct++;
}
cur_roh_idx = next_roh_idx;
kb_tot = 0;
for (uii = 0; uii < cur_roh_ct; uii++) {
cur_roh = &(roh_list[cur_roh_idx * ROH_ENTRY_INTS]);
marker_uidx1 = cur_roh[0];
marker_uidx2 = cur_roh[1];
wptr = width_force(4, wptr_chr, chrom_name_write(wptr_chr, chrom_info_ptr, get_marker_chrom(chrom_info_ptr, marker_uidx1)));
*wptr++ = ' ';
cptr = &(marker_ids[marker_uidx1 * max_marker_id_len]);
slen = strlen(cptr);
wptr = memcpya(memseta(wptr, 32, plink_maxsnp - slen), cptr, slen);
*wptr++ = ' ';
cptr = &(marker_ids[marker_uidx2 * max_marker_id_len]);
slen = strlen(cptr);
wptr = memcpya(memseta(wptr, 32, plink_maxsnp - slen), cptr, slen);
wptr = memseta(wptr, 32, 3);
wptr = uint32_writew10(wptr, marker_pos[marker_uidx1]);
wptr = memseta(wptr, 32, 3);
wptr = uint32_writew10x(wptr, marker_pos[marker_uidx2], ' ');
dxx = ((double)(marker_pos[marker_uidx2] + is_new_lengths - marker_pos[marker_uidx1])) / (1000.0 - EPSILON);
kb_tot += dxx;
wptr = width_force(10, wptr, double_f_writew3(wptr, dxx));
*wptr++ = ' ';
if (cur_roh[2] > max_roh_len) {
max_roh_len = cur_roh[2];
}
wptr = uint32_writew8x(wptr, cur_roh[2], ' ');
dyy = (1.0 + SMALLISH_EPSILON) / ((double)((int32_t)cur_roh[2]));
wptr = width_force(8, wptr, double_f_writew3(wptr, dxx * dyy));
// next two decimals guaranteed to be length 5
wptr = memseta(wptr, 32, 4);
wptr = double_f_writew3(wptr, ((double)((int32_t)cur_roh[3])) * dyy);
wptr = memseta(wptr, 32, 4);
wptr = double_f_writew3(wptr, ((double)((int32_t)cur_roh[4])) * dyy);
*wptr++ = '\n';
if (fwrite_checked(tbuf, wptr - tbuf, outfile)) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
#ifdef __LP64__
cur_roh_idx = ((uintptr_t)cur_roh[5]) | (((uintptr_t)cur_roh[6]) << 32);
#else
cur_roh_idx = (uintptr_t)cur_roh[5];
#endif
cur_roh[5] = sample_uidx;
}
if (!IS_SET(pheno_nm, sample_uidx)) {
wptr = fw_strcpyn(4, missing_pheno_len - omp_is_numeric * 4, missing_pheno_str, wptr_phe);
} else if (pheno_c) {
wptr = memseta(wptr_phe, 32, 3);
*wptr++ = '1' + IS_SET(pheno_c, sample_uidx);
} else {
wptr = width_force(4, wptr_phe, double_g_write(wptr_phe, pheno_d[sample_uidx]));
}
*wptr++ = ' ';
wptr = uint32_writew8x(wptr, cur_roh_ct, ' ');
wptr = width_force(8, wptr, double_g_write(wptr, kb_tot));
*wptr++ = ' ';
if (cur_roh_ct) {
kb_tot /= (double)((int32_t)cur_roh_ct);
}
wptr = width_force(8, wptr, double_g_write(wptr, kb_tot));
wptr = memcpya(wptr, " \n", 2);
if (fwrite_checked(tbuf, wptr - tbuf, outfile_indiv)) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
}
if (fclose_null(&outfile)) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
if (fclose_null(&outfile_indiv)) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
memcpy(&(outname_end[5]), "summary", 8);
if (fopen_checked(&outfile, outname, "w")) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
sprintf(tbuf, " CHR %%%us BP AFF UNAFF\n", plink_maxsnp);
fprintf(outfile, tbuf, "SNP");
for (chrom_fo_idx = 0; chrom_fo_idx < chrom_info_ptr->chrom_ct; chrom_fo_idx++) {
chrom_roh_start = roh_list_chrom_starts[chrom_fo_idx];
chrom_roh_ct = roh_list_chrom_starts[chrom_fo_idx + 1] - chrom_roh_start;
uii = chrom_info_ptr->chrom_file_order[chrom_fo_idx];
chrom_start = chrom_info_ptr->chrom_file_order_marker_idx[chrom_fo_idx];
chrom_len = chrom_info_ptr->chrom_file_order_marker_idx[chrom_fo_idx + 1] - chrom_start;
wkspace_reset(wkspace_mark);
if (wkspace_alloc_i_checked(&roh_ct_unaff_adj, (chrom_len + 1) * sizeof(int32_t))) {
goto write_main_roh_reports_ret_NOMEM;
}
fill_int_zero(roh_ct_unaff_adj, chrom_len);
if (pheno_c) {
if (wkspace_alloc_i_checked(&roh_ct_aff_adj, (chrom_len + 1) * sizeof(int32_t))) {
goto write_main_roh_reports_ret_NOMEM;
}
fill_int_zero(roh_ct_aff_adj, chrom_len);
}
cur_roh = &(roh_list[chrom_roh_start * ROH_ENTRY_INTS]);
for (cur_roh_idx = 0; cur_roh_idx < chrom_roh_ct; cur_roh_idx++) {
sample_uidx = cur_roh[5];
if ((!pheno_c) || (!IS_SET(pheno_c, sample_uidx))) {
roh_ct_unaff_adj[cur_roh[0] - chrom_start] += 1;
roh_ct_unaff_adj[cur_roh[1] + 1 - chrom_start] -= 1;
} else {
roh_ct_aff_adj[cur_roh[0] - chrom_start] += 1;
roh_ct_aff_adj[cur_roh[1] + 1 - chrom_start] -= 1;
}
cur_roh = &(cur_roh[ROH_ENTRY_INTS]);
}
wptr_chr = width_force(4, tbuf, chrom_name_write(tbuf, chrom_info_ptr, uii));
*wptr_chr++ = ' ';
memset(&(wptr_chr[plink_maxsnp]), 32, 3);
wptr_bp1 = &(wptr_chr[plink_maxsnp + 3]);
memset(&(wptr_bp1[10]), 32, 8);
wptr_bp1[18] = '0';
wptr_bp1[19] = ' ';
chrom_len += chrom_start; // now chrom_end
cur_roh_ct = 0; // unaff ct
uii = 0; // aff ct
for (marker_uidx1 = chrom_start; marker_uidx1 < chrom_len; marker_uidx1++) {
if (IS_SET(marker_exclude, marker_uidx1)) {
continue;
}
cptr = &(marker_ids[marker_uidx1 * max_marker_id_len]);
slen = strlen(cptr);
memcpy(memseta(wptr_chr, 32, plink_maxsnp - slen), cptr, slen);
uint32_writew10(wptr_bp1, marker_pos[marker_uidx1]);
if (!pheno_c) {
wptr = &(wptr_bp1[20]);
} else {
uii += roh_ct_aff_adj[marker_uidx1 - chrom_start];
wptr = uint32_writew8x(&(wptr_bp1[11]), uii, ' ');
}
cur_roh_ct += roh_ct_unaff_adj[marker_uidx1 - chrom_start];
if (cur_roh_ct + uii > max_pool_size) {
max_pool_size = cur_roh_ct + uii;
}
wptr = uint32_writew8x(wptr, cur_roh_ct, '\n');
if (fwrite_checked(tbuf, wptr - tbuf, outfile)) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
}
}
if (fclose_null(&outfile)) {
goto write_main_roh_reports_ret_WRITE_FAIL;
}
*max_pool_size_ptr = max_pool_size;
*max_roh_len_ptr = max_roh_len;
while (0) {
write_main_roh_reports_ret_NOMEM:
retval = RET_NOMEM;
break;
write_main_roh_reports_ret_OPEN_FAIL:
retval = RET_OPEN_FAIL;
break;
write_main_roh_reports_ret_WRITE_FAIL:
retval = RET_WRITE_FAIL;
break;
}
wkspace_reset(wkspace_mark);
fclose_cond(outfile);
fclose_cond(outfile_indiv);
return retval;
}
// heap of 64-bit integers with maximum on top
// root at element 1, heap_size offset by 1
// slightly more complicated variant of this in plink_cluster.c
void heapmax64_down(uint32_t cur_pos, uint32_t heap_size, uint64_t* heapmax64) {
uint64_t cur_val = heapmax64[cur_pos];
uint32_t child_pos = cur_pos * 2;
uint64_t tmp_val;
while (child_pos < heap_size) {
tmp_val = heapmax64[child_pos];
if ((child_pos + 1 < heap_size) && (heapmax64[child_pos + 1] > tmp_val)) {
tmp_val = heapmax64[++child_pos];
}
if (cur_val >= tmp_val) {
break;
}
heapmax64[cur_pos] = tmp_val;
cur_pos = child_pos;
child_pos *= 2;
}
heapmax64[cur_pos] = cur_val;
}
void heapmax64_up_then_down(uint32_t orig_pos, uint64_t* heapmax64, uint32_t heap_size) {
uint32_t cur_pos = orig_pos;
uint64_t cur_val = heapmax64[orig_pos];
uint32_t parent_pos = orig_pos / 2;
uint64_t tmp_val;
while (parent_pos) {
tmp_val = heapmax64[parent_pos];
if (cur_val <= tmp_val) {
break;
}
heapmax64[cur_pos] = tmp_val;
cur_pos = parent_pos;
parent_pos /= 2;
}
if (cur_pos != orig_pos) {
heapmax64[cur_pos] = cur_val;
}
heapmax64_down(cur_pos, heap_size, heapmax64);
}
void cur_roh_heap_removemax(uintptr_t* roh_slot_occupied, uint64_t* cur_roh_heap, uint32_t* cur_roh_heap_top_ptr, uint32_t* cur_roh_heap_max_ptr) {
uint32_t cur_roh_heap_top = *cur_roh_heap_top_ptr;
uint32_t initial_heap_max = *cur_roh_heap_max_ptr;
uint32_t new_heap_max;
do {
clear_bit(roh_slot_occupied, (uint32_t)(cur_roh_heap[1]));
if ((--cur_roh_heap_top) == 1) {
new_heap_max = 0;
break;
}
cur_roh_heap[1] = cur_roh_heap[cur_roh_heap_top];
heapmax64_down(1, cur_roh_heap_top, cur_roh_heap);
new_heap_max = (uint32_t)(cur_roh_heap[1] >> 32);
} while (new_heap_max == initial_heap_max);
*cur_roh_heap_top_ptr = cur_roh_heap_top;
*cur_roh_heap_max_ptr = new_heap_max;
}
void extract_pool_info(uint32_t pool_size, uintptr_t* roh_list_idxs, uint32_t* roh_list, uint32_t* con_uidx1_ptr, uint32_t* con_uidx2_ptr, uint32_t* union_uidx1_ptr, uint32_t* union_uidx2_ptr) {
uint32_t uii = 0;
uint32_t* cur_roh = &(roh_list[roh_list_idxs[0] * ROH_ENTRY_INTS]);
uint32_t con_uidx1 = cur_roh[0];
uint32_t con_uidx2 = cur_roh[1];
uint32_t union_uidx1 = cur_roh[0];
uint32_t union_uidx2 = cur_roh[1];
uint32_t cur_uidx;
for (uii = 1; uii < pool_size; uii++) {
cur_roh = &(roh_list[roh_list_idxs[uii] * ROH_ENTRY_INTS]);
cur_uidx = cur_roh[0];
if (cur_uidx > con_uidx1) {
con_uidx1 = cur_uidx;
} else if (cur_uidx < union_uidx1) {
union_uidx1 = cur_uidx;
}
cur_uidx = cur_roh[1];
if (cur_uidx < con_uidx2) {
con_uidx2 = cur_uidx;
} else if (cur_uidx > union_uidx2) {
union_uidx2 = cur_uidx;
}
}
*con_uidx1_ptr = con_uidx1;
*con_uidx2_ptr = con_uidx2;
*union_uidx1_ptr = union_uidx1;
*union_uidx2_ptr = union_uidx2;
}
void initialize_roh_slot(uint32_t* cur_roh, uint32_t chrom_start, uint32_t* marker_uidx_to_cidx, uintptr_t* roh_slot, uint32_t* roh_slot_cidx_start, uint32_t* roh_slot_cidx_end, uint32_t* roh_slot_end_uidx) {
uint32_t cidx_first = marker_uidx_to_cidx[cur_roh[0] - chrom_start];
uint32_t cidx_last = marker_uidx_to_cidx[cur_roh[1] - chrom_start];
#ifdef __LP64__
uint32_t cidx_first_block = cidx_first & (~63);
uint32_t cidx_last_block = cidx_last & (~63);
uint32_t cur_bidx = 2;
#else
uint32_t cidx_first_block = cidx_first & (~15);
uint32_t cidx_last_block = cidx_last & (~15);
uint32_t cur_bidx = 1;
#endif
uint32_t end_bidx = cur_bidx + (cidx_last_block - cidx_first_block) / BITCT2;
uint32_t uii;
// given an interval [a, b], when we can't just use the half-open convention
// everywhere, "last" means b and "end" means b+1. (yeah, I should clean up
// the huge unfiltered index vs. filtered index mess and remove the need to
// deviate from half-open.)
*roh_slot_cidx_start = cidx_first;
*roh_slot_cidx_end = cidx_last + 1;
*roh_slot_end_uidx = cur_roh[1] + 1;
uii = cidx_first & (BITCT2 - 1);
#ifdef __LP64__
if (cidx_first & 32) {
roh_slot[0] = FIVEMASK;
roh_slot[1] = 0x1555555555555555LLU >> (2 * (31 - uii));
} else {
roh_slot[0] = 0x1555555555555555LLU >> (2 * (31 - uii));
roh_slot[1] = 0;
}
#else
roh_slot[0] = 0x15555555 >> (2 * (15 - uii));
#endif
fill_ulong_zero(&(roh_slot[cur_bidx]), end_bidx - cur_bidx);
uii = cidx_last & (BITCT2 - 1);
#ifdef __LP64__
if (cidx_last & 32) {
// |= instead of = in case first_block and last_block are the same
roh_slot[end_bidx - 1] |= 0x5555555555555554LLU << (2 * uii);
} else {
roh_slot[end_bidx - 2] |= 0x5555555555555554LLU << (2 * uii);
roh_slot[end_bidx - 1] |= FIVEMASK;
}
#else
roh_slot[end_bidx - 1] |= 0x55555554 << (2 * uii);
#endif
}
void populate_roh_slot_from_lookahead_nowrap(uintptr_t* lookahead_cur_col, uint32_t read_shift, uintptr_t unfiltered_sample_ctl2, uintptr_t row_ct, uint32_t write_start, uintptr_t* write_ptr) {
uint32_t write_shift = (write_start & (BITCT2 - 1)) * 2;
uintptr_t row_idx;
uintptr_t cur_word;
write_ptr = &(write_ptr[write_start / BITCT2]);
cur_word = *write_ptr;
for (row_idx = 0; row_idx < row_ct; row_idx++) {
cur_word |= ((lookahead_cur_col[row_idx * unfiltered_sample_ctl2] >> read_shift) & (3 * ONELU)) << write_shift;
write_shift += 2;
if (write_shift == BITCT) {
*write_ptr++ = cur_word;
cur_word = *write_ptr;
write_shift = 0;
}
}
if (write_shift) {
*write_ptr = cur_word;
}
}
void populate_roh_slots_from_lookahead_buf(uintptr_t* lookahead_buf, uintptr_t max_lookahead, uintptr_t unfiltered_sample_ctl2, uintptr_t cur_lookahead_start, uintptr_t cur_lookahead_size, uint32_t lookahead_first_cidx, uint64_t* roh_slot_map, uintptr_t roh_slot_wsize, uint32_t* roh_slot_cidx_start, uint32_t* roh_slot_cidx_end, uintptr_t* roh_slots) {
uint32_t cur_lookahead_cidx_end = lookahead_first_cidx + cur_lookahead_size;
uint32_t cur_lookahead_cidx_wrap = lookahead_first_cidx + max_lookahead - cur_lookahead_start;
uint32_t sample_uidx = (uint32_t)((*roh_slot_map) >> 32);
uintptr_t* lookahead_cur_col;
uintptr_t* write_ptr;
uintptr_t slot_idx;
uint32_t read_shift;
uint32_t cidx_start_block;
uint32_t cidx_start;
uint32_t cidx_end;
do {
lookahead_cur_col = &(lookahead_buf[sample_uidx / BITCT2]);
read_shift = 2 * (sample_uidx & (BITCT2 - 1));
slot_idx = (uintptr_t)((*roh_slot_map) & 0xffffffffU);
cidx_start = roh_slot_cidx_start[slot_idx];
#ifdef __LP64__
cidx_start_block = cidx_start & (~63);
#else
cidx_start_block = cidx_start & (~15);
#endif
if (lookahead_first_cidx > cidx_start) {
cidx_start = lookahead_first_cidx;
}
cidx_end = roh_slot_cidx_end[slot_idx];
if (cidx_end > cur_lookahead_cidx_end) {
cidx_end = cur_lookahead_cidx_end;
}
if (cidx_end > cidx_start) {
write_ptr = &(roh_slots[slot_idx * roh_slot_wsize]);
if (cidx_end <= cur_lookahead_cidx_wrap) {
populate_roh_slot_from_lookahead_nowrap(&(lookahead_cur_col[(cur_lookahead_start + cidx_start - lookahead_first_cidx) * unfiltered_sample_ctl2]), read_shift, unfiltered_sample_ctl2, cidx_end - cidx_start, cidx_start - cidx_start_block, write_ptr);
} else {
if (cidx_start < cur_lookahead_cidx_wrap) {
populate_roh_slot_from_lookahead_nowrap(&(lookahead_cur_col[(cur_lookahead_start + cidx_start - lookahead_first_cidx) * unfiltered_sample_ctl2]), read_shift, unfiltered_sample_ctl2, cur_lookahead_cidx_wrap - cidx_start, cidx_start - cidx_start_block, write_ptr);
cidx_start = cur_lookahead_cidx_wrap;
}
populate_roh_slot_from_lookahead_nowrap(&(lookahead_cur_col[(cidx_start - cur_lookahead_cidx_wrap) * unfiltered_sample_ctl2]), read_shift, unfiltered_sample_ctl2, cidx_end - cidx_start, cidx_start - cidx_start_block, write_ptr);
}
}
// one-terminated list
sample_uidx = (uint32_t)((*(++roh_slot_map)) >> 32);
} while (sample_uidx != 0xffffffffU);
}
int32_t populate_roh_slots_from_disk(FILE* bedfile, uint64_t bed_offset, uintptr_t* rawbuf, uintptr_t* marker_exclude, uint64_t unfiltered_sample_ct4, uint32_t* marker_uidx_to_cidx, uint32_t chrom_start, uint32_t marker_uidx, uint32_t last_uidx, uint64_t* roh_slot_map, uintptr_t roh_slot_wsize, uint32_t* roh_slot_cidx_start, uint32_t* roh_slot_cidx_end, uintptr_t* roh_slots, uint32_t roh_read_slot_ct) {
uintptr_t roh_write_slot_idx;
uintptr_t marker_c_bidx;
uintptr_t start_c_bidx;
uint32_t roh_read_slot_idx;
uint32_t marker_cidx;
uint32_t cidx_start;
uint32_t write_shift;
uint32_t sample_uidx;
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) {
return RET_READ_FAIL;
}
marker_uidx--;
do {
marker_uidx++;
if (IS_SET(marker_exclude, marker_uidx)) {
marker_uidx = next_unset_unsafe(marker_exclude, marker_uidx);
if (fseeko(bedfile, bed_offset + ((uint64_t)marker_uidx) * unfiltered_sample_ct4, SEEK_SET)) {
return RET_READ_FAIL;
}
}
if (load_raw(bedfile, rawbuf, unfiltered_sample_ct4)) {
return RET_READ_FAIL;
}
marker_cidx = marker_uidx_to_cidx[marker_uidx - chrom_start];
marker_c_bidx = marker_cidx / BITCT2;
write_shift = 2 * (marker_cidx % BITCT2);
for (roh_read_slot_idx = 0; roh_read_slot_idx < roh_read_slot_ct; roh_read_slot_idx++) {
sample_uidx = (uint32_t)(roh_slot_map[roh_read_slot_idx] >> 32);
roh_write_slot_idx = (uintptr_t)(roh_slot_map[roh_read_slot_idx] & 0xffffffffU);
cidx_start = roh_slot_cidx_start[roh_write_slot_idx];
if ((marker_cidx >= cidx_start) && (marker_cidx < roh_slot_cidx_end[roh_write_slot_idx])) {
#ifdef __LP64__
start_c_bidx = 2 * (cidx_start / 64);
#else
start_c_bidx = cidx_start / 16;
#endif
roh_slots[roh_write_slot_idx * roh_slot_wsize + marker_c_bidx - start_c_bidx] |= ((rawbuf[sample_uidx / BITCT2] >> (2 * (sample_uidx % BITCT2))) & (3 * ONELU)) << write_shift;
}
}
} while (marker_uidx != last_uidx);
return 0;
}
static inline uint32_t is_allelic_match(double mismatch_max, uintptr_t* roh_slot_idxl, uintptr_t* roh_slot_idxs, uint32_t block_start_idxl, uint32_t block_start_idxs, uint32_t overlap_cidx_start, uint32_t overlap_cidx_end) {
#ifdef __LP64__
const __m128i m1 = {FIVEMASK, FIVEMASK};
const __m128i m2 = {0x3333333333333333LLU, 0x3333333333333333LLU};
const __m128i m4 = {0x0f0f0f0f0f0f0f0fLLU, 0x0f0f0f0f0f0f0f0fLLU};
const __m128i m8 = {0x00ff00ff00ff00ffLLU, 0x00ff00ff00ff00ffLLU};
uint32_t words_left = ((overlap_cidx_end + 31) / 32) - 2 * (overlap_cidx_start / 64);
uint32_t joint_homozyg_ct = 0;
uint32_t joint_homozyg_mismatch_ct = 0;
__m128i loader_l;
__m128i loader_s;
__m128i joint_vec;
__m128i joint_sum1;
__m128i mismatch_sum1;
__m128i joint_sum2;
__m128i mismatch_sum2;
__uni16 accj;
__uni16 accm;
__m128i* vptrl;
__m128i* vptrs;
__m128i* vptrl_end;
uintptr_t* roh_idxl_end;
uintptr_t wloader_l;
uintptr_t wloader_s;
uintptr_t joint_word;
// 1. set joint_vec to 01 at joint-homozygous spots, and 00 elsewhere
// 2. popcount joint_vec to determine number of jointly homozygous sites
// 3. then popcount (joint_vec & (roh_vslot_idxl ^ roh_vslot_idxs)) to
// determine number of mismatches at these sites
roh_slot_idxl = &(roh_slot_idxl[2 * ((overlap_cidx_start - block_start_idxl) / 64)]);
roh_slot_idxs = &(roh_slot_idxs[2 * ((overlap_cidx_start - block_start_idxs) / 64)]);
roh_idxl_end = &(roh_slot_idxl[words_left]);
if (words_left >= 11) {
vptrl = (__m128i*)roh_slot_idxl;
vptrs = (__m128i*)roh_slot_idxs;
if (words_left % 12 == 11) {
words_left++;
}
words_left -= words_left % 12;
while (words_left >= 120) {
words_left -= 120;
vptrl_end = &(vptrl[60]);
is_allelic_match_main_loop:
accj.vi = _mm_setzero_si128();
accm.vi = _mm_setzero_si128();
do {
loader_l = *vptrl++;
loader_s = *vptrs++;
joint_sum1 = _mm_andnot_si128(_mm_or_si128(_mm_xor_si128(loader_l, _mm_srli_epi64(loader_l, 1)), _mm_xor_si128(loader_s, _mm_srli_epi64(loader_s, 1))), m1);
mismatch_sum1 = _mm_and_si128(joint_sum1, _mm_xor_si128(loader_l, loader_s));
loader_l = *vptrl++;
loader_s = *vptrs++;
joint_vec = _mm_andnot_si128(_mm_or_si128(_mm_xor_si128(loader_l, _mm_srli_epi64(loader_l, 1)), _mm_xor_si128(loader_s, _mm_srli_epi64(loader_s, 1))), m1);
joint_sum1 = _mm_add_epi64(joint_sum1, joint_vec);
joint_vec = _mm_and_si128(joint_vec, _mm_xor_si128(loader_l, loader_s));
mismatch_sum1 = _mm_add_epi64(mismatch_sum1, joint_vec);
loader_l = *vptrl++;
loader_s = *vptrs++;
joint_vec = _mm_andnot_si128(_mm_or_si128(_mm_xor_si128(loader_l, _mm_srli_epi64(loader_l, 1)), _mm_xor_si128(loader_s, _mm_srli_epi64(loader_s, 1))), m1);
joint_sum1 = _mm_add_epi64(joint_sum1, joint_vec);
joint_vec = _mm_and_si128(joint_vec, _mm_xor_si128(loader_l, loader_s));
mismatch_sum1 = _mm_add_epi64(mismatch_sum1, joint_vec);
joint_sum1 = _mm_add_epi64(_mm_and_si128(joint_sum1, m2), _mm_and_si128(_mm_srli_epi64(joint_sum1, 2), m2));
mismatch_sum1 = _mm_add_epi64(_mm_and_si128(mismatch_sum1, m2), _mm_and_si128(_mm_srli_epi64(mismatch_sum1, 2), m2));
loader_l = *vptrl++;
loader_s = *vptrs++;
joint_sum2 = _mm_andnot_si128(_mm_or_si128(_mm_xor_si128(loader_l, _mm_srli_epi64(loader_l, 1)), _mm_xor_si128(loader_s, _mm_srli_epi64(loader_s, 1))), m1);
mismatch_sum2 = _mm_and_si128(joint_sum2, _mm_xor_si128(loader_l, loader_s));
loader_l = *vptrl++;
loader_s = *vptrs++;
joint_vec = _mm_andnot_si128(_mm_or_si128(_mm_xor_si128(loader_l, _mm_srli_epi64(loader_l, 1)), _mm_xor_si128(loader_s, _mm_srli_epi64(loader_s, 1))), m1);
joint_sum2 = _mm_add_epi64(joint_sum2, joint_vec);
joint_vec = _mm_and_si128(joint_vec, _mm_xor_si128(loader_l, loader_s));
mismatch_sum2 = _mm_add_epi64(mismatch_sum2, joint_vec);
loader_l = *vptrl++;
loader_s = *vptrs++;
joint_vec = _mm_andnot_si128(_mm_or_si128(_mm_xor_si128(loader_l, _mm_srli_epi64(loader_l, 1)), _mm_xor_si128(loader_s, _mm_srli_epi64(loader_s, 1))), m1);
joint_sum2 = _mm_add_epi64(joint_sum2, joint_vec);
joint_vec = _mm_and_si128(joint_vec, _mm_xor_si128(loader_l, loader_s));
mismatch_sum2 = _mm_add_epi64(mismatch_sum2, joint_vec);
joint_sum1 = _mm_add_epi64(joint_sum1, _mm_add_epi64(_mm_and_si128(joint_sum2, m2), _mm_and_si128(_mm_srli_epi64(joint_sum2, 2), m2)));
mismatch_sum1 = _mm_add_epi64(mismatch_sum1, _mm_add_epi64(_mm_and_si128(mismatch_sum2, m2), _mm_and_si128(_mm_srli_epi64(mismatch_sum2, 2), m2)));
accj.vi = _mm_add_epi64(accj.vi, _mm_add_epi64(_mm_and_si128(joint_sum1, m4), _mm_and_si128(_mm_srli_epi64(joint_sum1, 4), m4)));
accm.vi = _mm_add_epi64(accm.vi, _mm_add_epi64(_mm_and_si128(mismatch_sum1, m4), _mm_and_si128(_mm_srli_epi64(mismatch_sum1, 4), m4)));
} while (vptrl < vptrl_end);
accj.vi = _mm_add_epi64(_mm_and_si128(accj.vi, m8), _mm_and_si128(_mm_srli_epi64(accj.vi, 8), m8));
accm.vi = _mm_add_epi64(_mm_and_si128(accm.vi, m8), _mm_and_si128(_mm_srli_epi64(accm.vi, 8), m8));
joint_homozyg_ct += ((accj.u8[0] + accj.u8[1]) * 0x1000100010001LLU) >> 48;
joint_homozyg_mismatch_ct += ((accm.u8[0] + accm.u8[1]) * 0x1000100010001LLU) >> 48;
}
if (words_left) {
vptrl_end = &(vptrl[words_left / 2]);
words_left = 0;
goto is_allelic_match_main_loop;
}
roh_slot_idxl = (uintptr_t*)vptrl;
roh_slot_idxs = (uintptr_t*)vptrs;
}
#else
uint32_t joint_homozyg_ct = 0;
uint32_t joint_homozyg_mismatch_ct = 0;
uint32_t words_left = ((overlap_cidx_end + 15) / 16) - (overlap_cidx_start / 16);
uintptr_t* roh_idxl_end;
uintptr_t wloader_l;
uintptr_t wloader_s;