forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plink_dosage.c
2279 lines (2255 loc) · 87.4 KB
/
plink_dosage.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_cluster.h"
#include "plink_data.h"
#include "plink_dosage.h"
#include "plink_filter.h"
#include "plink_glm.h"
#include "plink_matrix.h"
#include "plink_misc.h"
#include "pigz.h"
void dosage_init(Dosage_info* doip) {
doip->fname = NULL;
doip->modifier = 0;
doip->skip0 = 0;
doip->skip1 = 0;
doip->skip2 = 0;
doip->format = 2;
}
void dosage_cleanup(Dosage_info* doip) {
free_cond(doip->fname);
}
#define DOSAGE_EPSILON 0.000244140625
int32_t dosage_load_score_files(Score_info* sc_ip, char* outname, char* outname_end, uintptr_t* score_marker_ct_ptr, uintptr_t* max_score_marker_id_len_ptr, char** score_marker_ids_ptr, char*** score_allele_codes_ptr, double** score_effect_sizes_ptr, uintptr_t** score_qrange_key_exists_ptr, double** score_qrange_keys_ptr, uintptr_t* qrange_ct_ptr, uintptr_t* max_qrange_name_len_ptr, char** score_qrange_names_ptr, double** score_qrange_bounds_ptr) {
// We don't necessarily have the whole variant ID list in advance, so it
// makes sense to deviate a bit from score_report().
//
// 1. Process main --score file in three passes. First pass, just determine
// variant count and maximum ID length; second pass, save and sort just
// the relevant variant IDs and check for duplicates; third pass, save
// allele codes and scores.
// 2. If --q-score-range was specified, load those files.
FILE* infile = NULL;
uintptr_t score_marker_ct = 0;
uintptr_t max_score_marker_id_len = 0;
uintptr_t miss_ct = 0;
uintptr_t marker_idx = 0;
uintptr_t qrange_ct = 0;
uintptr_t max_qrange_name_len = 0;
uint64_t allele_code_buf_len = 0;
uintptr_t* score_qrange_key_exists = NULL;
double* score_qrange_keys = NULL;
uint32_t modifier = sc_ip->modifier;
int32_t retval = 0;
char* bufptr_arr[3];
char* score_marker_ids;
char* score_qrange_names;
char** score_allele_codes;
double* score_effect_sizes;
double* score_qrange_bounds;
char* allele_code_buf;
char* loadbuf;
char* bufptr;
uintptr_t loadbuf_size;
uintptr_t score_marker_ctl;
uintptr_t line_idx;
uintptr_t ulii;
double lbound;
double ubound;
double dxx;
uint32_t first_col_m1;
uint32_t col_01_delta;
uint32_t col_12_delta;
uint32_t varid_idx;
uint32_t allele_idx;
uint32_t effect_idx;
uint32_t rangename_len_limit;
uint32_t slen;
int32_t ii;
loadbuf_size = wkspace_left;
if (loadbuf_size > MAXLINEBUFLEN) {
loadbuf_size = MAXLINEBUFLEN;
} else if (loadbuf_size <= MAXLINELEN) {
goto dosage_load_score_files_ret_NOMEM;
}
loadbuf = (char*)wkspace_base;
retval = open_and_load_to_first_token(&infile, sc_ip->fname, loadbuf_size, '\0', "--score file", loadbuf, &bufptr, &line_idx);
if (retval) {
goto dosage_load_score_files_ret_1;
}
if (sc_ip->varid_col < sc_ip->allele_col) {
first_col_m1 = sc_ip->varid_col;
varid_idx = 0;
if (sc_ip->allele_col < sc_ip->effect_col) {
col_01_delta = sc_ip->allele_col - first_col_m1;
col_12_delta = sc_ip->effect_col - sc_ip->allele_col;
allele_idx = 1;
effect_idx = 2;
} else {
allele_idx = 2;
if (sc_ip->varid_col < sc_ip->effect_col) {
col_01_delta = sc_ip->effect_col - first_col_m1;
col_12_delta = sc_ip->allele_col - sc_ip->effect_col;
effect_idx = 1;
} else {
first_col_m1 = sc_ip->effect_col;
col_01_delta = sc_ip->varid_col - first_col_m1;
col_12_delta = sc_ip->allele_col - sc_ip->varid_col;
varid_idx = 1;
effect_idx = 0;
}
}
} else {
first_col_m1 = sc_ip->allele_col;
allele_idx = 0;
if (sc_ip->varid_col < sc_ip->effect_col) {
col_01_delta = sc_ip->varid_col - first_col_m1;
col_12_delta = sc_ip->effect_col - sc_ip->varid_col;
varid_idx = 1;
effect_idx = 2;
} else {
varid_idx = 2;
if (sc_ip->allele_col < sc_ip->effect_col) {
col_01_delta = sc_ip->effect_col - first_col_m1;
col_12_delta = sc_ip->varid_col - sc_ip->effect_col;
effect_idx = 1;
} else {
first_col_m1 = sc_ip->effect_col;
col_01_delta = sc_ip->allele_col - first_col_m1;
col_12_delta = sc_ip->varid_col - sc_ip->allele_col;
allele_idx = 1;
effect_idx = 0;
}
}
}
first_col_m1--;
if (modifier & SCORE_HEADER) {
goto dosage_load_score_files_next1;
}
// first pass: validate, count
while (1) {
bufptr_arr[0] = next_token_multz(bufptr, first_col_m1);
bufptr_arr[1] = next_token_mult(bufptr_arr[0], col_01_delta);
bufptr_arr[2] = next_token_mult(bufptr_arr[1], col_12_delta);
if (!bufptr_arr[2]) {
goto dosage_load_score_files_ret_MISSING_TOKENS;
}
if (scan_double(bufptr_arr[effect_idx], &dxx)) {
miss_ct++;
} else {
ulii = strlen_se(bufptr_arr[varid_idx]) + 1;
if (ulii > max_score_marker_id_len) {
max_score_marker_id_len = ulii;
}
slen = strlen_se(bufptr_arr[allele_idx]);
if (slen > 1) {
allele_code_buf_len += slen + 1;
}
score_marker_ct++;
}
dosage_load_score_files_next1:
line_idx++;
if (!fgets(loadbuf, loadbuf_size, infile)) {
break;
}
if (!(loadbuf[loadbuf_size - 1])) {
if (loadbuf_size == MAXLINEBUFLEN) {
sprintf(logbuf, "Error: Line %" PRIuPTR " of --score file is pathologically long.\n", line_idx);
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
goto dosage_load_score_files_ret_NOMEM;
}
bufptr = skip_initial_spaces(loadbuf);
if (is_eoln_kns(*bufptr)) {
goto dosage_load_score_files_next1;
}
}
if (!score_marker_ct) {
logprint("Error: No valid entries in --score file.\n");
goto dosage_load_score_files_ret_INVALID_FORMAT;
}
if (score_marker_ct >= 0x40000000) {
logprint("Error: --score does not support >= 2^30 variants.\n");
goto dosage_load_score_files_ret_INVALID_FORMAT;
}
#ifndef __LP64__
if (allele_code_buf_len > 0x7fffffff) {
goto dosage_load_score_files_ret_NOMEM;
}
#endif
if (wkspace_alloc_c_checked(score_marker_ids_ptr, score_marker_ct * max_score_marker_id_len) ||
wkspace_alloc_d_checked(score_effect_sizes_ptr, score_marker_ct * sizeof(double)) ||
wkspace_alloc_c_checked(&allele_code_buf, (uintptr_t)allele_code_buf_len)) {
goto dosage_load_score_files_ret_NOMEM;
}
score_marker_ids = *score_marker_ids_ptr;
score_effect_sizes = *score_effect_sizes_ptr;
score_marker_ctl = (score_marker_ct + (BITCT - 1)) / BITCT;
if (sc_ip->data_fname) {
if (wkspace_alloc_ul_checked(score_qrange_key_exists_ptr, score_marker_ctl * sizeof(intptr_t)) ||
wkspace_alloc_d_checked(score_qrange_keys_ptr, score_marker_ct * sizeof(double))) {
goto dosage_load_score_files_ret_NOMEM;
}
score_qrange_key_exists = *score_qrange_key_exists_ptr;
score_qrange_keys = *score_qrange_keys_ptr;
fill_ulong_zero(score_qrange_key_exists, score_marker_ctl);
}
score_allele_codes = (char**)wkspace_alloc(score_marker_ct * sizeof(intptr_t));
if (!score_allele_codes) {
goto dosage_load_score_files_ret_NOMEM;
}
*score_allele_codes_ptr = score_allele_codes;
rewind(infile);
loadbuf_size = wkspace_left;
if (loadbuf_size > MAXLINEBUFLEN) {
loadbuf_size = MAXLINEBUFLEN;
} else if (loadbuf_size <= MAXLINELEN) {
goto dosage_load_score_files_ret_NOMEM;
}
loadbuf = (char*)wkspace_base;
loadbuf[loadbuf_size - 1] = ' ';
// pass 2: load and sort variant IDs
retval = load_to_first_token(infile, loadbuf_size, '\0', "--score file", loadbuf, &bufptr, &line_idx);
if (modifier & SCORE_HEADER) {
goto dosage_load_score_files_next2;
}
while (1) {
bufptr_arr[0] = next_token_multz(bufptr, first_col_m1);
bufptr_arr[1] = next_token_mult(bufptr_arr[0], col_01_delta);
bufptr_arr[2] = next_token_mult(bufptr_arr[1], col_12_delta);
if (!scan_double(bufptr_arr[effect_idx], &dxx)) {
slen = strlen_se(bufptr_arr[varid_idx]);
memcpyx(&(score_marker_ids[marker_idx * max_score_marker_id_len]), bufptr_arr[varid_idx], slen, '\0');
if (++marker_idx == score_marker_ct) {
break;
}
}
dosage_load_score_files_next2:
line_idx++;
if (!fgets(loadbuf, loadbuf_size, infile)) {
goto dosage_load_score_files_ret_READ_FAIL;
}
if (!(loadbuf[loadbuf_size - 1])) {
if (loadbuf_size == MAXLINEBUFLEN) {
sprintf(logbuf, "Error: Line %" PRIuPTR " of --score file is pathologically long.\n", line_idx);
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
goto dosage_load_score_files_ret_NOMEM;
}
bufptr = skip_initial_spaces(loadbuf);
if (is_eoln_kns(*bufptr)) {
goto dosage_load_score_files_next2;
}
}
qsort(score_marker_ids, score_marker_ct, max_score_marker_id_len, strcmp_casted);
bufptr = scan_for_duplicate_ids(score_marker_ids, score_marker_ct, max_score_marker_id_len);
if (bufptr) {
LOGPREPRINTFWW("Error: Duplicate variant '%s' in --score file.\n", bufptr);
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
rewind(infile);
// pass 3: load other stuff
retval = load_to_first_token(infile, loadbuf_size, '\0', "--score file", loadbuf, &bufptr, &line_idx);
ulii = 0;
if (modifier & SCORE_HEADER) {
goto dosage_load_score_files_next3;
}
while (1) {
bufptr_arr[0] = next_token_multz(bufptr, first_col_m1);
bufptr_arr[1] = next_token_mult(bufptr_arr[0], col_01_delta);
bufptr_arr[2] = next_token_mult(bufptr_arr[1], col_12_delta);
if (!scan_double(bufptr_arr[effect_idx], &dxx)) {
// guaranteed to succeed unless the user is overwriting the file between
// load passes, which we won't bother defending against
marker_idx = (uint32_t)bsearch_str(bufptr_arr[varid_idx], strlen_se(bufptr_arr[varid_idx]), score_marker_ids, max_score_marker_id_len, score_marker_ct);
score_effect_sizes[marker_idx] = dxx;
slen = strlen_se(bufptr_arr[allele_idx]);
if (slen == 1) {
score_allele_codes[marker_idx] = (char*)(&(g_one_char_strs[2 * bufptr_arr[allele_idx][0]]));
} else {
score_allele_codes[marker_idx] = allele_code_buf;
allele_code_buf = memcpyax(allele_code_buf, bufptr_arr[allele_idx], slen, '\0');
}
if (++ulii == score_marker_ct) {
break;
}
}
dosage_load_score_files_next3:
if (!fgets(loadbuf, loadbuf_size, infile)) {
goto dosage_load_score_files_ret_READ_FAIL;
}
bufptr = skip_initial_spaces(loadbuf);
if (is_eoln_kns(*bufptr)) {
goto dosage_load_score_files_next3;
}
}
if (fclose_null(&infile)) {
goto dosage_load_score_files_ret_READ_FAIL;
}
LOGPRINTFWW("--score: %" PRIuPTR " entr%s loaded from %s.\n", score_marker_ct, (score_marker_ct == 1)? "y" : "ies", sc_ip->fname);
if (miss_ct) {
LOGPRINTF("Warning: %" PRIuPTR " line%s skipped.\n", miss_ct, (miss_ct == 1)? "" : "s");
}
*score_marker_ct_ptr = score_marker_ct;
*max_score_marker_id_len_ptr = max_score_marker_id_len;
if (sc_ip->data_fname) {
retval = open_and_load_to_first_token(&infile, sc_ip->data_fname, loadbuf_size, '\0', "--q-score-range data file", loadbuf, &bufptr, &line_idx);
if (retval) {
goto dosage_load_score_files_ret_1;
}
if (sc_ip->data_varid_col < sc_ip->data_col) {
first_col_m1 = sc_ip->data_varid_col;
col_01_delta = sc_ip->data_col - first_col_m1;
varid_idx = 0;
} else {
first_col_m1 = sc_ip->data_col;
col_01_delta = sc_ip->data_varid_col - first_col_m1;
varid_idx = 1;
}
first_col_m1--;
miss_ct = 0;
if (modifier & SCORE_DATA_HEADER) {
goto dosage_load_score_files_next4;
}
while (1) {
bufptr_arr[0] = next_token_multz(bufptr, first_col_m1);
bufptr_arr[1] = next_token_mult(bufptr_arr[0], col_01_delta);
if (!bufptr_arr[1]) {
goto dosage_load_score_files_ret_MISSING_TOKENS_Q;
}
ii = bsearch_str(bufptr_arr[varid_idx], strlen_se(bufptr_arr[varid_idx]), score_marker_ids, max_score_marker_id_len, score_marker_ct);
if (ii != -1) {
if (scan_double(bufptr_arr[1 - varid_idx], &dxx) || (dxx != dxx)) {
miss_ct++;
} else {
if (is_set(score_qrange_key_exists, ii)) {
LOGPREPRINTFWW("Error: Duplicate variant '%s' in --q-score-range data file.\n", &(score_marker_ids[((uint32_t)ii) * max_score_marker_id_len]));
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
score_qrange_keys[(uint32_t)ii] = dxx;
set_bit(score_qrange_key_exists, ii);
}
} else {
miss_ct++;
}
dosage_load_score_files_next4:
line_idx++;
if (!fgets(loadbuf, loadbuf_size, infile)) {
break;
}
if (!(loadbuf[loadbuf_size - 1])) {
if (loadbuf_size == MAXLINEBUFLEN) {
sprintf(logbuf, "Error: Line %" PRIuPTR " of --q-score-range data file is pathologically long.\n", line_idx);
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
goto dosage_load_score_files_ret_NOMEM;
}
bufptr = skip_initial_spaces(loadbuf);
if (is_eoln_kns(*bufptr)) {
goto dosage_load_score_files_next4;
}
}
if (fclose_null(&infile)) {
goto dosage_load_score_files_ret_READ_FAIL;
}
if (miss_ct) {
LOGPRINTF("Warning: %" PRIuPTR " line%s skipped in --q-score-range data file.\n", miss_ct, (miss_ct == 1)? "" : "s");
}
if (fopen_checked(&infile, sc_ip->range_fname, "r")) {
goto dosage_load_score_files_ret_OPEN_FAIL;
}
rangename_len_limit = (FNAMESIZE - 10) - ((uintptr_t)(outname_end - outname));
tbuf[MAXLINELEN - 1] = ' ';
while (fgets(tbuf, MAXLINELEN, infile)) {
line_idx++;
if (!tbuf[MAXLINELEN - 1]) {
sprintf(logbuf, "Error: Line %" PRIuPTR " of --q-score-range range file is pathologically long.\n", line_idx);
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
bufptr = skip_initial_spaces(tbuf);
if (is_eoln_kns(*bufptr)) {
continue;
}
slen = strlen_se(bufptr);
bufptr_arr[1] = skip_initial_spaces(&(bufptr[slen]));
bufptr_arr[2] = next_token(bufptr_arr[1]);
if ((!bufptr_arr[2]) || scan_double(bufptr_arr[1], &lbound) || scan_double(bufptr_arr[2], &ubound) || (lbound != lbound) || (ubound != ubound) || (lbound > ubound)) {
// count these skips?
continue;
}
if (slen > rangename_len_limit) {
sprintf(logbuf, "Error: Excessively long range name on line %" PRIuPTR " of --q-score-range range\nfile.\n", line_idx);
goto dosage_load_score_files_ret_INVALID_FORMAT_2;
}
qrange_ct++;
if (slen >= max_qrange_name_len) {
max_qrange_name_len = slen + 1;
}
}
if (!feof(infile)) {
goto dosage_load_score_files_ret_READ_FAIL;
}
if (!qrange_ct) {
logprint("Error: No valid entries in --q-score-range range file.\n");
goto dosage_load_score_files_ret_INVALID_FORMAT;
}
if (wkspace_alloc_c_checked(score_qrange_names_ptr, qrange_ct * max_qrange_name_len) ||
wkspace_alloc_d_checked(score_qrange_bounds_ptr, qrange_ct * 2 * sizeof(double))) {
goto dosage_load_score_files_ret_NOMEM;
}
score_qrange_names = *score_qrange_names_ptr;
score_qrange_bounds = *score_qrange_bounds_ptr;
rewind(infile);
ulii = 0; // range index
while (fgets(tbuf, MAXLINELEN, infile)) {
bufptr = skip_initial_spaces(tbuf);
if (is_eoln_kns(*bufptr)) {
continue;
}
slen = strlen_se(bufptr);
bufptr_arr[1] = skip_initial_spaces(&(bufptr[slen]));
bufptr_arr[2] = next_token(bufptr_arr[1]);
if ((!bufptr_arr[2]) || scan_double(bufptr_arr[1], &lbound) || scan_double(bufptr_arr[2], &ubound) || (lbound != lbound) || (ubound != ubound) || (lbound > ubound)) {
continue;
}
memcpyx(&(score_qrange_names[ulii * max_qrange_name_len]), bufptr, slen, '\0');
score_qrange_bounds[2 * ulii] = lbound;
score_qrange_bounds[2 * ulii + 1] = ubound;
ulii++;
}
if (ulii != qrange_ct) {
// catches /dev/stdin redirection
goto dosage_load_score_files_ret_READ_FAIL;
}
if (fclose_null(&infile)) {
goto dosage_load_score_files_ret_READ_FAIL;
}
LOGPRINTF("--q-score-range: %" PRIuPTR " range%s loaded.\n", qrange_ct, (qrange_ct == 1)? "" : "s");
*qrange_ct_ptr = qrange_ct;
*max_qrange_name_len_ptr = max_qrange_name_len;
}
while (0) {
dosage_load_score_files_ret_NOMEM:
retval = RET_NOMEM;
break;
dosage_load_score_files_ret_OPEN_FAIL:
retval = RET_OPEN_FAIL;
break;
dosage_load_score_files_ret_READ_FAIL:
retval = RET_READ_FAIL;
break;
dosage_load_score_files_ret_MISSING_TOKENS_Q:
LOGPRINTF("Error: Line %" PRIuPTR " of --q-score-range data file has fewer tokens than\nexpected.\n", line_idx);
retval = RET_INVALID_FORMAT;
break;
dosage_load_score_files_ret_MISSING_TOKENS:
sprintf(logbuf, "Error: Line %" PRIuPTR " of --score file has fewer tokens than expected.\n", line_idx);
dosage_load_score_files_ret_INVALID_FORMAT_2:
logprintb();
dosage_load_score_files_ret_INVALID_FORMAT:
retval = RET_INVALID_FORMAT;
break;
}
dosage_load_score_files_ret_1:
fclose_cond(infile);
return retval;
}
int32_t plink1_dosage(Dosage_info* doip, char* famname, char* mapname, char* outname, char* outname_end, char* phenoname, char* extractname, char* excludename, char* keepname, char* removename, char* keepfamname, char* removefamname, char* filtername, char* makepheno_str, char* phenoname_str, char* covar_fname, Two_col_params* qual_filter, Two_col_params* update_map, Two_col_params* update_name, char* update_ids_fname, char* update_parents_fname, char* update_sex_fname, char* filtervals_flattened, char* filter_attrib_fname, char* filter_attrib_liststr, char* filter_attrib_sample_fname, char* filter_attrib_sample_liststr, double qual_min_thresh, double qual_max_thresh, double thin_keep_prob, uint32_t thin_keep_ct, uint32_t min_bp_space, uint32_t mfilter_col, uint32_t fam_cols, int32_t missing_pheno, char* output_missing_pheno, uint32_t mpheno_col, uint32_t pheno_modifier, Chrom_info* chrom_info_ptr, double tail_bottom, double tail_top, uint64_t misc_flags, uint64_t filter_flags, uint32_t sex_missing_pheno, uint32_t update_sex_col, Cluster_info* cluster_ptr, int32_t marker_pos_start, int32_t marker_pos_end, int32_t snp_window_size, char* markername_from, char* markername_to, char* markername_snp, Range_list* snps_range_list_ptr, uint32_t covar_modifier, Range_list* covar_range_list_ptr, uint32_t mwithin_col, uint32_t glm_modifier, double glm_vif_thresh, double output_min_p, Score_info* sc_ip) {
// sucks to duplicate so much, but this code will be thrown out later so
// there's no long-term maintenance problem
FILE* phenofile = NULL;
FILE* infile = NULL;
FILE* profile_outfile = NULL;
gzFile* gz_infiles = NULL;
char* marker_ids = NULL;
char* sample_ids = NULL;
char* paternal_ids = NULL;
char* maternal_ids = NULL;
char* cluster_ids = NULL;
char* covar_names = NULL;
char* sorted_sample_ids = NULL;
char* sep_fnames = NULL;
char* cur_marker_id_buf = NULL;
char* score_marker_ids = NULL;
char* score_qrange_names = NULL;
char** score_allele_codes = NULL;
char* a1_ptr = NULL;
char* a2_ptr = NULL;
char* pzwritep = NULL;
uintptr_t* marker_exclude = NULL;
uintptr_t* sample_exclude = NULL;
uintptr_t* sex_nm = NULL;
uintptr_t* sex_male = NULL;
uintptr_t* pheno_nm = NULL;
uintptr_t* pheno_c = NULL;
uintptr_t* pheno_nm_collapsed = NULL;
uintptr_t* pheno_c_collapsed = NULL;
uintptr_t* founder_info = NULL;
uintptr_t* covar_nm = NULL;
uintptr_t* perm_vec = NULL;
uintptr_t* perm_fails = NULL; // need to enforce alignment
uintptr_t* score_qrange_key_exists = NULL;
double* pheno_d = NULL;
double* covar_d = NULL;
double* cur_dosages2 = NULL;
double* score_effect_sizes = NULL;
double* score_qrange_keys = NULL;
double* score_qrange_bounds = NULL;
double* cur_scores = NULL;
double* score_bases = NULL;
#ifndef NOLAPACK
double* pheno_d2 = NULL;
double* covars_cov_major_buf = NULL;
double* covars_sample_major_buf = NULL;
double* pheno_d_collapsed = NULL;
double* param_2d_buf = NULL;
double* param_2d_buf2 = NULL;
double* regression_results = NULL;
double* dgels_a = NULL;
double* dgels_b = NULL;
double* dgels_work = NULL;
MATRIX_INVERT_BUF1_TYPE* mi_buf = NULL;
#endif
float* covar_f = NULL;
float* covars_cov_major_f_buf = NULL;
float* coef_f = NULL;
float* pp_f = NULL;
float* sample_1d_buf_f = NULL;
float* pheno_buf_f = NULL;
float* param_1d_buf_f = NULL;
float* param_1d_buf2_f = NULL;
float* param_2d_buf_f = NULL;
float* param_2d_buf2_f = NULL;
float* regression_results_f = NULL;
Ll_ctstr_entry** htable = NULL;
uint32_t* marker_pos = NULL;
uint32_t* cluster_map = NULL;
uint32_t* cluster_starts = NULL;
uint32_t* marker_id_htable = NULL;
uint32_t* sample_id_map = NULL;
uint32_t* batch_sizes = NULL;
uint32_t* score_range_obs_cts = NULL;
uint32_t* score_miss_cts = NULL;
uint32_t* uiptr = NULL;
uint32_t* uiptr2 = NULL;
uint32_t* uiptr3 = NULL;
uintptr_t topsize = 0;
uintptr_t unfiltered_marker_ct = 0;
uintptr_t marker_exclude_ct = 0;
uintptr_t max_marker_id_len = 0;
uintptr_t unfiltered_sample_ct = 0;
uintptr_t sample_exclude_ct = 0;
uintptr_t max_sample_id_len = 4;
uintptr_t max_paternal_id_len = 2;
uintptr_t max_maternal_id_len = 2;
uintptr_t cluster_ct = 0;
uintptr_t max_cluster_id_len = 2;
uintptr_t covar_ct = 0;
uintptr_t max_covar_name_len = 0;
uintptr_t max_fn_len = 0;
uintptr_t max_sepheader_len = 0;
uintptr_t file_idx_start = 0;
uintptr_t distinct_id_ct = 0; // occur mode
uintptr_t max_occur_id_len = 0;
uintptr_t marker_idx = 0;
uintptr_t score_marker_ct = 0;
uintptr_t max_score_marker_id_len = 0;
uintptr_t qrange_ct = 0;
uintptr_t max_qrange_name_len = 0;
uintptr_t ulii = 0;
double missing_phenod = (double)missing_pheno;
uint32_t load_map = (mapname[0] != '\0');
uint32_t do_glm = (doip->modifier / DOSAGE_GLM) & 1;
uint32_t do_score = (doip->modifier / DOSAGE_SCORE) & 1;
uint32_t count_occur = doip->modifier & DOSAGE_OCCUR;
uint32_t sepheader = (doip->modifier / DOSAGE_SEPHEADER) & 1;
uint32_t noheader = doip->modifier & DOSAGE_NOHEADER;
uint32_t output_gz = doip->modifier & DOSAGE_ZOUT;
uint32_t dose1 = doip->modifier & DOSAGE_DOSE1;
uint32_t score_report_average = doip->modifier & DOSAGE_SCORE_NOSUM;
uint32_t dosage_score_cnt = doip->modifier & DOSAGE_SCORE_CNT;
uint32_t sex_covar = doip->modifier & DOSAGE_SEX;
uint32_t skip0 = doip->skip0;
uint32_t skip1p1 = doip->skip1 + 1;
uint32_t skip2 = doip->skip2;
uint32_t format_val = doip->format;
uint32_t standard_beta = glm_modifier & GLM_STANDARD_BETA;
uint32_t score_center = sc_ip->modifier & SCORE_CENTER;
uint32_t score_mean_impute = !(sc_ip->modifier & SCORE_NO_MEAN_IMPUTATION);
uint32_t map_cols = 3;
uint32_t map_is_unsorted = 0;
uint32_t affection = 0;
uint32_t infile_ct = 0;
uint32_t pheno_ctrl_ct = 0;
uint32_t batch_ct = 1;
uint32_t max_batch_size = 1;
uint32_t cur_marker_id_len = 0;
uint32_t marker_id_htable_size = 0;
uint32_t score_marker_idx = 0;
uint32_t score_a2_effect = 0;
uint32_t plink_maxfid = 0;
uint32_t plink_maxiid = 0;
uint32_t missing_pheno_len = 0;
uint32_t a1_len = 0;
uint32_t a2_len = 0;
uint32_t ujj = 0;
int32_t retval = 0;
#ifndef NOLAPACK
char dgels_trans = 'N';
__CLPK_integer dgels_m;
__CLPK_integer dgels_n;
__CLPK_integer dgels_nrhs;
__CLPK_integer dgels_ldb;
__CLPK_integer dgels_info;
__CLPK_integer dgels_lwork;
#endif
char missing_pheno_str[32];
Pigz_state ps;
unsigned char* wkspace_mark;
unsigned char* overflow_buf;
char* fnames;
char* loadbuf;
char* bufptr;
char* bufptr2;
char* bufptr3;
char* bufptr4;
char* bufptr5;
char* bufptr6;
uintptr_t* line_idx_arr;
uintptr_t* batch_samples;
uintptr_t* cur_samples;
double* cur_dosages;
double* dptr;
float* fptr;
Ll_ctstr_entry** ll_pptr;
Ll_ctstr_entry* ll_ptr;
uint32_t* file_icts;
uint32_t* read_idx_to_sample_idx;
uint32_t* skip_vals;
uintptr_t marker_ct;
uintptr_t unfiltered_sample_ctl;
uintptr_t sample_ct;
uintptr_t sample_cta4;
uintptr_t sample_ctl;
uintptr_t line_idx;
uintptr_t file_idx;
uintptr_t sample_idx;
uintptr_t cur_batch_size;
uintptr_t loadbuf_size;
uintptr_t sample_valid_ct;
uintptr_t param_ct;
uintptr_t param_cta4;
uintptr_t qrange_idx;
uintptr_t uljj;
double sample_valid_ct_recip;
double rsq;
double pval;
double beta;
double se;
double score_cur_effect_size;
double score_missing_effect;
double dxx;
double dyy;
double dzz;
uint32_t gender_unk_ct;
uint32_t pheno_nm_ct;
uint32_t batch_idx;
uint32_t read_idx_start;
uint32_t read_idx;
uint32_t slen;
uint32_t sample_uidx;
uint32_t prev_sample_uidx;
uint32_t is_valid;
uint32_t uii;
uint32_t ukk;
int32_t ii;
pzwrite_init_null(&ps);
if (load_map) {
retval = load_bim(mapname, &map_cols, &unfiltered_marker_ct, &marker_exclude_ct, &max_marker_id_len, &marker_exclude, NULL, NULL, NULL, &ulii, &marker_ids, NULL, 0, NULL, chrom_info_ptr, NULL, &marker_pos, misc_flags, filter_flags, marker_pos_start, marker_pos_end, snp_window_size, markername_from, markername_to, markername_snp, snps_range_list_ptr, &map_is_unsorted, do_glm || min_bp_space || (misc_flags & (MISC_EXTRACT_RANGE | MISC_EXCLUDE_RANGE)), 0, 0, NULL, ".map file", NULL);
if (retval) {
goto plink1_dosage_ret_1;
}
if (map_is_unsorted & UNSORTED_SPLIT_CHROM) {
logprint("Error: .map file has a split chromosome.\n");
goto plink1_dosage_ret_INVALID_FORMAT;
}
}
uii = fam_cols & FAM_COL_6;
if (uii && phenoname) {
uii = (pheno_modifier & PHENO_MERGE) && (!makepheno_str);
}
if (!uii) {
pheno_modifier &= ~PHENO_MERGE;
}
if (update_ids_fname) {
ulii = 0;
retval = scan_max_fam_indiv_strlen(update_ids_fname, 3, &max_sample_id_len);
if (retval) {
goto plink1_dosage_ret_1;
}
} else if (update_parents_fname) {
retval = scan_max_strlen(update_parents_fname, 3, 4, 0, '\0', &max_paternal_id_len, &max_maternal_id_len);
if (retval) {
goto plink1_dosage_ret_1;
}
}
retval = load_fam(famname, fam_cols, uii, missing_pheno, (misc_flags / MISC_AFFECTION_01) & 1, &unfiltered_sample_ct, &sample_ids, &max_sample_id_len, &paternal_ids, &max_paternal_id_len, &maternal_ids, &max_maternal_id_len, &sex_nm, &sex_male, &affection, &pheno_nm, &pheno_c, &pheno_d, &founder_info, &sample_exclude);
if (retval) {
goto plink1_dosage_ret_1;
}
unfiltered_sample_ctl = (unfiltered_sample_ct + (BITCT - 1)) / BITCT;
if (misc_flags & MISC_MAKE_FOUNDERS_FIRST) {
if (make_founders(unfiltered_sample_ct, unfiltered_sample_ct, sample_ids, max_sample_id_len, paternal_ids, max_paternal_id_len, maternal_ids, max_maternal_id_len, (misc_flags / MISC_MAKE_FOUNDERS_REQUIRE_2_MISSING) & 1, sample_exclude, founder_info)) {
goto plink1_dosage_ret_NOMEM;
}
}
count_genders(sex_nm, sex_male, unfiltered_sample_ct, sample_exclude, &uii, &ujj, &gender_unk_ct);
marker_ct = unfiltered_marker_ct - marker_exclude_ct;
if (gender_unk_ct) {
LOGPRINTF("%" PRIuPTR " %s (%u male%s, %u female%s, %u ambiguous) loaded from .fam.\n", unfiltered_sample_ct, species_str(unfiltered_sample_ct), uii, (uii == 1)? "" : "s", ujj, (ujj == 1)? "" : "s", gender_unk_ct);
retval = write_nosex(outname, outname_end, unfiltered_sample_ct, sample_exclude, sex_nm, gender_unk_ct, sample_ids, max_sample_id_len);
if (retval) {
goto plink1_dosage_ret_1;
}
} else {
LOGPRINTF("%" PRIuPTR " %s (%d male%s, %d female%s) loaded from .fam.\n", unfiltered_sample_ct, species_str(unfiltered_sample_ct), uii, (uii == 1)? "" : "s", ujj, (ujj == 1)? "" : "s");
}
uii = popcount_longs(pheno_nm, unfiltered_sample_ctl);
if (uii) {
LOGPRINTF("%u phenotype value%s loaded from .fam.\n", uii, (uii == 1)? "" : "s");
}
if (phenoname && fopen_checked(&phenofile, phenoname, "r")) {
goto plink1_dosage_ret_OPEN_FAIL;
}
if (phenofile || update_ids_fname || update_parents_fname || update_sex_fname || (filter_flags & FILTER_TAIL_PHENO)) {
wkspace_mark = wkspace_base;
retval = sort_item_ids(&sorted_sample_ids, &sample_id_map, unfiltered_sample_ct, sample_exclude, 0, sample_ids, max_sample_id_len, 0, 0, strcmp_deref);
if (retval) {
goto plink1_dosage_ret_1;
}
if (makepheno_str) {
retval = makepheno_load(phenofile, makepheno_str, unfiltered_sample_ct, sorted_sample_ids, max_sample_id_len, sample_id_map, pheno_nm, &pheno_c);
if (retval) {
goto plink1_dosage_ret_1;
}
} else if (phenofile) {
retval = load_pheno(phenofile, unfiltered_sample_ct, 0, sorted_sample_ids, max_sample_id_len, sample_id_map, missing_pheno, (misc_flags / MISC_AFFECTION_01) & 1, mpheno_col, phenoname_str, pheno_nm, &pheno_c, &pheno_d, NULL, 0);
if (retval) {
if (retval == LOAD_PHENO_LAST_COL) {
logprintb();
retval = RET_INVALID_FORMAT;
wkspace_reset(wkspace_mark);
}
goto plink1_dosage_ret_1;
}
}
if (filter_flags & FILTER_TAIL_PHENO) {
retval = convert_tail_pheno(unfiltered_sample_ct, pheno_nm, &pheno_c, &pheno_d, tail_bottom, tail_top, missing_phenod);
if (retval) {
goto plink1_dosage_ret_1;
}
}
wkspace_reset(wkspace_mark);
}
if (load_map) {
uii = update_map || update_name || filter_attrib_fname || qual_filter;
if (uii || extractname || excludename) {
wkspace_mark = wkspace_base;
retval = alloc_and_populate_id_htable(unfiltered_marker_ct, marker_exclude, unfiltered_marker_ct - marker_exclude_ct, marker_ids, max_marker_id_len, !uii, &marker_id_htable, &marker_id_htable_size);
if (retval) {
goto plink1_dosage_ret_1;
}
if (update_map) {
retval = update_marker_pos(update_map, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct, marker_pos, &map_is_unsorted, chrom_info_ptr);
} else if (update_name) {
retval = update_marker_names(update_name, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
if (extractname || excludename) {
wkspace_reset(wkspace_mark);
retval = alloc_and_populate_id_htable(unfiltered_marker_ct, marker_exclude, unfiltered_marker_ct - marker_exclude_ct, marker_ids, max_marker_id_len, 0, &marker_id_htable, &marker_id_htable_size);
if (retval) {
goto plink1_dosage_ret_1;
}
}
}
if (extractname) {
if (!(misc_flags & MISC_EXTRACT_RANGE)) {
retval = extract_exclude_flag_norange(extractname, marker_id_htable, marker_id_htable_size, 0, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
} else {
if (map_is_unsorted & UNSORTED_BP) {
logprint("Error: '--extract range' requires a sorted .bim. Retry this command after\nusing --make-bed to sort your data.\n");
goto plink1_dosage_ret_INVALID_CMDLINE;
}
retval = extract_exclude_range(extractname, marker_pos, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct, 0, chrom_info_ptr);
if (retval) {
goto plink1_dosage_ret_1;
}
uljj = unfiltered_marker_ct - marker_exclude_ct;
LOGPRINTF("--extract range: %" PRIuPTR " variant%s remaining.\n", uljj, (uljj == 1)? "" : "s");
}
}
if (excludename) {
if (!(misc_flags & MISC_EXCLUDE_RANGE)) {
retval = extract_exclude_flag_norange(excludename, marker_id_htable, marker_id_htable_size, 1, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
} else {
if (map_is_unsorted & UNSORTED_BP) {
logprint("Error: '--exclude range' requires a sorted .bim. Retry this command after\nusing --make-bed to sort your data.\n");
goto plink1_dosage_ret_INVALID_CMDLINE;
}
retval = extract_exclude_range(excludename, marker_pos, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct, 1, chrom_info_ptr);
if (retval) {
goto plink1_dosage_ret_1;
}
uljj = unfiltered_marker_ct - marker_exclude_ct;
LOGPRINTF("--exclude range: %" PRIuPTR " variant%s remaining.\n", uljj, (uljj == 1)? "" : "s");
}
}
if (filter_attrib_fname) {
retval = filter_attrib(filter_attrib_fname, filter_attrib_liststr, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (qual_filter) {
retval = filter_qual_scores(qual_filter, qual_min_thresh, qual_max_thresh, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
}
wkspace_reset(wkspace_mark);
}
if (thin_keep_prob != 1.0) {
if (random_thin_markers(thin_keep_prob, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct)) {
goto plink1_dosage_ret_ALL_MARKERS_EXCLUDED;
}
} else if (thin_keep_ct) {
retval = random_thin_markers_ct(thin_keep_ct, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
}
}
if (update_ids_fname || update_parents_fname || update_sex_fname || keepname || keepfamname || removename || removefamname || filter_attrib_sample_fname || filtername) {
wkspace_mark = wkspace_base;
retval = sort_item_ids(&sorted_sample_ids, &sample_id_map, unfiltered_sample_ct, sample_exclude, sample_exclude_ct, sample_ids, max_sample_id_len, 0, 0, strcmp_deref);
if (retval) {
goto plink1_dosage_ret_1;
}
ulii = unfiltered_sample_ct - sample_exclude_ct;
if (update_ids_fname) {
retval = update_sample_ids(update_ids_fname, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, sample_ids);
if (retval) {
goto plink1_dosage_ret_1;
}
} else {
if (update_parents_fname) {
retval = update_sample_parents(update_parents_fname, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, paternal_ids, max_paternal_id_len, maternal_ids, max_maternal_id_len, founder_info);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (update_sex_fname) {
retval = update_sample_sexes(update_sex_fname, update_sex_col, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, sex_nm, sex_male);
if (retval) {
goto plink1_dosage_ret_1;
}
}
}
if (keepfamname) {
retval = keep_or_remove(keepfamname, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 2);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (keepname) {
retval = keep_or_remove(keepname, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 0);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (removefamname) {
retval = keep_or_remove(removefamname, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 3);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (removename) {
retval = keep_or_remove(removename, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 1);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (filter_attrib_sample_fname) {
retval = filter_attrib_sample(filter_attrib_sample_fname, filter_attrib_sample_liststr, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct);
if (retval) {
goto plink1_dosage_ret_1;
}
}
if (filtername) {
if (!mfilter_col) {
mfilter_col = 1;
}
retval = filter_samples_file(filtername, sorted_sample_ids, ulii, max_sample_id_len, sample_id_map, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, filtervals_flattened, mfilter_col);
if (retval) {
goto plink1_dosage_ret_1;
}
}
wkspace_reset(wkspace_mark);
}
if (gender_unk_ct && (!(sex_missing_pheno & ALLOW_NO_SEX))) {
uii = popcount_longs_exclude(pheno_nm, sex_nm, unfiltered_sample_ctl);
if (uii) {
bitfield_and(pheno_nm, sex_nm, unfiltered_sample_ctl);
logprint("Warning: Ignoring phenotypes of missing-sex samples. If you don't want those\nphenotypes to be ignored, use the --allow-no-sex flag.\n");
}
}
if (do_glm || (filter_flags & FILTER_PRUNE)) {
ulii = sample_exclude_ct;
bitfield_ornot(sample_exclude, pheno_nm, unfiltered_sample_ctl);
zero_trailing_bits(sample_exclude, unfiltered_sample_ct);
sample_exclude_ct = popcount_longs(sample_exclude, unfiltered_sample_ctl);
uii = do_glm && (!(filter_flags & FILTER_PRUNE));
if (sample_exclude_ct == unfiltered_sample_ct) {
LOGPRINTF("Error: All %s removed by %s--prune.\n", g_species_plural, uii? "automatic " : "");
goto plink1_dosage_ret_ALL_SAMPLES_EXCLUDED;
}
if ((filter_flags & FILTER_PRUNE) || (sample_exclude_ct != ulii)) {
LOGPRINTF("%s--prune: %" PRIuPTR " %s remaining.\n", uii? "Automatic " : "", unfiltered_sample_ct - sample_exclude_ct, species_str(unfiltered_sample_ct == sample_exclude_ct + 1));
}
}
if (filter_flags & (FILTER_BINARY_CASES | FILTER_BINARY_CONTROLS)) {
if (!pheno_c) {
logprint("Error: --filter-cases/--filter-controls requires a case/control phenotype.\n");
goto plink1_dosage_ret_INVALID_CMDLINE;
}
ii = sample_exclude_ct;
filter_samples_bitfields(unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, pheno_c, (filter_flags / FILTER_BINARY_CASES) & 1, pheno_nm);
if (sample_exclude_ct == unfiltered_sample_ct) {
LOGPRINTF("Error: All %s removed due to case/control status (--filter-%s).\n", g_species_plural, (filter_flags & FILTER_BINARY_CASES)? "cases" : "controls");
goto plink1_dosage_ret_ALL_SAMPLES_EXCLUDED;
}
ii = sample_exclude_ct - ii;
LOGPRINTF("%d %s removed due to case/control status (--filter-%s).\n", ii, species_str(ii), (filter_flags & FILTER_BINARY_CASES)? "cases" : "controls");
}
if (filter_flags & (FILTER_BINARY_FEMALES | FILTER_BINARY_MALES)) {
ii = sample_exclude_ct;
filter_samples_bitfields(unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, sex_male, (filter_flags / FILTER_BINARY_MALES) & 1, sex_nm);
if (sample_exclude_ct == unfiltered_sample_ct) {
LOGPRINTF("Error: All %s removed due to gender filter (--filter-%s).\n", g_species_plural, (filter_flags & FILTER_BINARY_MALES)? "males" : "females");
goto plink1_dosage_ret_ALL_SAMPLES_EXCLUDED;
}
ii = sample_exclude_ct - ii;
LOGPRINTF("%d %s removed due to gender filter (--filter-%s).\n", ii, species_str(ii), (filter_flags & FILTER_BINARY_MALES)? "males" : "females");
}
if (filter_flags & (FILTER_BINARY_FOUNDERS | FILTER_BINARY_NONFOUNDERS)) {
ii = sample_exclude_ct;
filter_samples_bitfields(unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, founder_info, (filter_flags / FILTER_BINARY_FOUNDERS) & 1, NULL);
if (sample_exclude_ct == unfiltered_sample_ct) {
LOGPRINTF("Error: All %s removed due to founder status (--filter-%s).\n", g_species_plural, (filter_flags & FILTER_BINARY_FOUNDERS)? "founders" : "nonfounders");
goto plink1_dosage_ret_ALL_SAMPLES_EXCLUDED;
}
ii = sample_exclude_ct - ii;
LOGPRINTF("%d %s removed due to founder status (--filter-%s).\n", ii, species_str(ii), (filter_flags & FILTER_BINARY_FOUNDERS)? "founders" : "nonfounders");
}
if (cluster_ptr->fname || (misc_flags & MISC_FAMILY_CLUSTERS)) {
retval = load_clusters(cluster_ptr->fname, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, sample_ids, max_sample_id_len, mwithin_col, (misc_flags / MISC_LOAD_CLUSTER_KEEP_NA) & 1, &cluster_ct, &cluster_map, &cluster_starts, &cluster_ids, &max_cluster_id_len, cluster_ptr->keep_fname, cluster_ptr->keep_flattened, cluster_ptr->remove_fname, cluster_ptr->remove_flattened);
if (retval) {
goto plink1_dosage_ret_1;
}
}
sample_ct = unfiltered_sample_ct - sample_exclude_ct;
if (!sample_ct) {
LOGPRINTF("Error: No %s pass QC.\n", g_species_plural);
goto plink1_dosage_ret_ALL_SAMPLES_EXCLUDED;
}
sample_cta4 = (sample_ct + 3) & (~3);
sample_ctl = (sample_ct + (BITCT - 1)) / BITCT;
if (g_thread_ct > 1) {
logprint("Using 1 thread (no multithreaded calculations invoked).\n");
} else {
logprint("Using 1 thread.\n");
}
if ((filter_flags & FILTER_MAKE_FOUNDERS) && (!(misc_flags & MISC_MAKE_FOUNDERS_FIRST))) {
if (make_founders(unfiltered_sample_ct, sample_ct, sample_ids, max_sample_id_len, paternal_ids, max_paternal_id_len, maternal_ids, max_maternal_id_len, (misc_flags / MISC_MAKE_FOUNDERS_REQUIRE_2_MISSING) & 1, sample_exclude, founder_info)) {
goto plink1_dosage_ret_NOMEM;
}
}
if (covar_fname) {
// update this as more covariate-referencing commands are added
if (!do_glm) {
logprint("Warning: Ignoring --covar since no commands reference the covariates.\n");
} else {
retval = load_covars(covar_fname, unfiltered_sample_ct, sample_exclude, sample_ct, sex_covar? sex_nm : NULL, sex_covar? sex_male : NULL, sample_ids, max_sample_id_len, missing_phenod, covar_modifier, covar_range_list_ptr, 0, &covar_ct, &covar_names, &max_covar_name_len, pheno_nm, &covar_nm, &covar_d, NULL, NULL);