forked from chrchang/plink-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plink.c
13454 lines (13269 loc) · 562 KB
/
plink.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
// PLINK 1.90
// Copyright (C) 2005-2015 Shaun Purcell, Christopher Chang
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Uncomment "#define NOLAPACK" in plink_common.h to build without LAPACK.
#include "plink_common.h"
#include <ctype.h>
#include <locale.h>
#include <time.h>
#include <unistd.h>
#ifdef __APPLE__
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#include "plink_assoc.h"
#include "plink_calc.h"
#include "plink_cnv.h"
#include "plink_data.h"
#include "plink_dosage.h"
#include "plink_family.h"
#include "plink_filter.h"
#include "plink_glm.h"
#include "plink_help.h"
#include "plink_homozyg.h"
#include "plink_lasso.h"
#include "plink_ld.h"
#include "plink_misc.h"
#ifdef __cplusplus
#ifndef _WIN32
#include "plink_rserve.h"
#endif
#endif
#include "plink_set.h"
#include "plink_stats.h"
#include "pigz.h"
#define DEFAULT_PPC_GAP 500000
#define DEFAULT_IBS_TEST_PERMS 100000
// should switch this to IMPORT_... and make it include .ped/Oxford too; only
// .bed and future .pgen format don't require an import step
#define LOAD_RARE_GRM 1
#define LOAD_RARE_GRM_BIN 2
#define LOAD_RARE_LGEN 4
#define LOAD_RARE_TRANSPOSE 8
#define LOAD_RARE_TPED 0x10
#define LOAD_RARE_TFAM 0x20
#define LOAD_RARE_TRANSPOSE_MASK (LOAD_RARE_TRANSPOSE | LOAD_RARE_TPED | LOAD_RARE_TFAM)
#define LOAD_RARE_DUMMY 0x40
#define LOAD_RARE_SIMULATE 0x80
#define LOAD_RARE_CNV 0x100
#define LOAD_RARE_GVAR 0x200
#define LOAD_RARE_23 0x400
// er, this won't actually be rare...
#define LOAD_RARE_VCF 0x800
#define LOAD_RARE_BCF 0x1000
#define LOAD_RARE_DOSAGE 0x2000
#define LOAD_PARAMS_PED 1
#define LOAD_PARAMS_MAP 2
#define LOAD_PARAMS_TEXT_ALL 3
#define LOAD_PARAMS_BED 4
#define LOAD_PARAMS_BIM 8
#define LOAD_PARAMS_FAM 0x10
#define LOAD_PARAMS_BFILE_ALL 0x1c
#define LOAD_PARAMS_OXGEN 0x20
#define LOAD_PARAMS_OXBGEN 0x40
#define LOAD_PARAMS_OXSAMPLE 0x80
#define LOAD_PARAMS_OX_ALL 0xe0
// maximum number of usable cluster computers, this is arbitrary though it
// shouldn't be larger than 2^31 - 1
#define PARALLEL_MAX 32768
const char ver_str[] =
#ifdef STABLE_BUILD
"PLINK v1.90b3n"
#else
"PLINK v1.90p"
#endif
#ifdef NOLAPACK
"NL"
#endif
#ifdef __LP64__
" 64-bit"
#else
" 32-bit"
#endif
" (12 May 2015)";
const char ver_str2[] =
// include leading space if day < 10, so character length stays the same
""
#ifdef STABLE_BUILD
"" // (don't want this when version number has a trailing letter)
#else
" " // (don't want this when version number has e.g. "b3" before "p")
#endif
#ifndef NOLAPACK
" "
#endif
" https://www.cog-genomics.org/plink2\n"
"(C) 2005-2015 Shaun Purcell, Christopher Chang GNU General Public License v3\n";
const char errstr_append[] = "For more information, try '" PROG_NAME_STR " --help [flag name]' or '" PROG_NAME_STR " --help | more'.\n";
#ifdef STABLE_BUILD
#ifndef NOLAPACK
const char notestr_null_calc2[] = "Commands include --make-bed, --recode, --flip-scan, --merge-list,\n--write-snplist, --list-duplicate-vars, --freqx, --missing, --test-mishap,\n--hardy, --mendel, --ibc, --impute-sex, --indep-pairphase, --r2, --show-tags,\n--blocks, --distance, --genome, --homozyg, --make-rel, --make-grm-gz,\n--rel-cutoff, --cluster, --pca, --neighbour, --ibs-test, --regress-distance,\n--model, --bd, --gxe, --logistic, --dosage, --lasso, --test-missing,\n--make-perm-pheno, --tdt, --qfam, --annotate, --clump, --gene-report,\n--meta-analysis, --epistasis, --fast-epistasis, and --score.\n\n'" PROG_NAME_STR " --help | more' describes all functions (warning: long).\n";
#else
const char notestr_null_calc2[] = "Commands include --make-bed, --recode, --flip-scan, --merge-list,\n--write-snplist, --list-duplicate-vars, --freqx, --missing, --test-mishap,\n--hardy, --mendel, --ibc, --impute-sex, --indep-pairphase, --r2, --show-tags,\n--blocks, --distance, --genome, --homozyg, --make-rel, --make-grm-gz,\n--rel-cutoff, --cluster, --neighbour, --ibs-test, --regress-distance, --model,\n--bd, --gxe, --logistic, --dosage, --lasso, --test-missing, --make-perm-pheno,\n--tdt, --qfam, --annotate, --clump, --gene-report, --meta-analysis,\n--epistasis, --fast-epistasis, and --score.\n\n'" PROG_NAME_STR " --help | more' describes all functions (warning: long).\n";
#endif
#else
#ifndef NOLAPACK
const char notestr_null_calc2[] = "Commands include --make-bed, --recode, --flip-scan, --merge-list,\n--write-snplist, --list-duplicate-vars, --freqx, --missing, --test-mishap,\n--hardy, --mendel, --ibc, --impute-sex, --indep-pairphase, --r2, --show-tags,\n--blocks, --distance, --genome, --homozyg, --make-rel, --make-grm-gz,\n--rel-cutoff, --cluster, --pca, --neighbour, --ibs-test, --regress-distance,\n--model, --bd, --gxe, --logistic, --dosage, --lasso, --test-missing,\n--make-perm-pheno, --unrelated-heritability, --tdt, --qfam, --annotate,\n--clump, --gene-report, --meta-analysis, --epistasis, --fast-epistasis, and\n--score.\n\n'" PROG_NAME_STR " --help | more' describes all functions (warning: long).\n";
#else
const char notestr_null_calc2[] = "Commands include --make-bed, --recode, --flip-scan, --merge-list,\n--write-snplist, --list-duplicate-vars, --freqx, --missing, --test-mishap,\n--hardy, --mendel, --ibc, --impute-sex, --indep-pairphase, --r2, --show-tags,\n--blocks, --distance, --genome, --homozyg, --make-rel, --make-grm-gz,\n--rel-cutoff, --cluster, --neighbour, --ibs-test, --regress-distance, --model,\n--bd, --gxe, --logistic, --dosage, --lasso, --test-missing, --make-perm-pheno,\n--tdt, --qfam, --annotate, --clump, --gene-report, --meta-analysis,\n--epistasis, --fast-epistasis, and --score.\n\n'" PROG_NAME_STR " --help | more' describes all functions (warning: long).\n";
#endif
#endif
unsigned char* wkspace;
void disp_exit_msg(int32_t retval) {
switch (retval) {
case RET_NOMEM:
logprint("\nError: Out of memory. Try the --memory and/or --parallel flags.\n");
break;
case RET_WRITE_FAIL:
logprint("\nError: File write failure.\n");
break;
case RET_READ_FAIL:
logprint("\nError: File read failure.\n");
break;
case RET_THREAD_CREATE_FAIL:
logprint("\nError: Failed to create thread.\n");
break;
}
}
// back to our regular program
const unsigned char acgt_reverse_arr[] = "1B2DEF3HIJKLMNOPQRS4";
const unsigned char acgt_arr[] = "ACGT";
// g_one_char_strs offsets (double)
const unsigned char acgt_reverse_arr1[] = "b\204d\210\212\214f\220\222\224\226\230\232\234\236\240\242\244\246h";
const unsigned char acgt_arr1[] = "\202\206\216\250";
// const unsigned char acgt_reverse_arr1[] = "\"D$HJL&PRTVXZ\\^`bdf(";
// const unsigned char acgt_arr1[] = "BFNh";
static inline unsigned char conditional_convert(unsigned char diff, unsigned char ucc2_max, const unsigned char* convert_arr, unsigned char ucc) {
unsigned char ucc2 = ucc - diff;
return (ucc2 < ucc2_max)? convert_arr[ucc2] : ucc;
}
static inline void conditional_convert1(unsigned char diff, unsigned char ucc2_max, const unsigned char* convert_arr, char** allele_ptr) {
unsigned char ucc2 = ((unsigned char)(**allele_ptr)) - diff;
if (ucc2 < ucc2_max) {
*allele_ptr = (char*)(&(g_one_char_strs[convert_arr[ucc2]]));
}
}
void allelexxxx_recode(uint32_t allelexxxx, char** marker_allele_ptrs, uint32_t unfiltered_marker_ct, uintptr_t* marker_exclude, uint32_t marker_ct) {
uint32_t marker_uidx = 0;
uint32_t markers_done = 0;
uint32_t recode_multichar = allelexxxx & ALLELE_RECODE_MULTICHAR;
const unsigned char* convert_arr;
const unsigned char* convert_arr1;
char** map_ptr;
char** map_ptr_stop;
char* cptr;
uint32_t marker_uidx_stop;
unsigned char diff;
unsigned char ucc2_max;
unsigned char ucc;
if (allelexxxx & ALLELE_RECODE_ACGT) {
diff = 49;
ucc2_max = 4;
convert_arr = acgt_arr;
convert_arr1 = acgt_arr1;
} else {
diff = 65;
ucc2_max = 20;
convert_arr = acgt_reverse_arr;
convert_arr1 = acgt_reverse_arr1;
}
while (markers_done < marker_ct) {
marker_uidx = next_unset_unsafe(marker_exclude, marker_uidx);
marker_uidx_stop = next_set(marker_exclude, marker_uidx, unfiltered_marker_ct);
markers_done += marker_uidx_stop - marker_uidx;
map_ptr = &(marker_allele_ptrs[marker_uidx * 2]);
map_ptr_stop = &(marker_allele_ptrs[marker_uidx_stop * 2]);
marker_uidx = marker_uidx_stop;
if (recode_multichar) {
do {
cptr = *map_ptr;
if (!cptr[1]) {
conditional_convert1(diff, ucc2_max, convert_arr1, map_ptr);
} else {
ucc = *cptr;
do {
*cptr = conditional_convert(diff, ucc2_max, convert_arr, ucc);
ucc = *(++cptr);
} while (ucc);
}
} while (++map_ptr < map_ptr_stop);
} else {
do {
if (!(map_ptr[0][1])) {
conditional_convert1(diff, ucc2_max, convert_arr1, map_ptr);
}
} while (++map_ptr < map_ptr_stop);
}
}
}
void calc_marker_reverse_bin(uintptr_t* marker_reverse, uintptr_t* marker_exclude, uint32_t unfiltered_marker_ct, uint32_t marker_ct, double* set_allele_freqs) {
uint32_t marker_uidx = 0;
uint32_t markers_done = 0;
uint32_t marker_uidx_stop;
double dxx;
do {
marker_uidx = next_unset_unsafe(marker_exclude, marker_uidx);
marker_uidx_stop = next_set(marker_exclude, marker_uidx, unfiltered_marker_ct);
markers_done += marker_uidx_stop - marker_uidx;
for (; marker_uidx < marker_uidx_stop; marker_uidx++) {
dxx = set_allele_freqs[marker_uidx];
if (dxx < 0.5) {
SET_BIT(marker_reverse, marker_uidx);
set_allele_freqs[marker_uidx] = 1.0 - dxx;
}
}
} while (markers_done < marker_ct);
}
void swap_reversed_marker_alleles(uintptr_t unfiltered_marker_ct, uintptr_t* marker_reverse, char** marker_allele_ptrs) {
uintptr_t marker_uidx = 0;
char* swap_ptr;
while (1) {
next_set_ul_ck(marker_reverse, &marker_uidx, unfiltered_marker_ct);
if (marker_uidx == unfiltered_marker_ct) {
return;
}
swap_ptr = marker_allele_ptrs[marker_uidx * 2];
marker_allele_ptrs[marker_uidx * 2] = marker_allele_ptrs[marker_uidx * 2 + 1];
marker_allele_ptrs[marker_uidx * 2 + 1] = swap_ptr;
marker_uidx++;
}
}
static inline uint32_t are_marker_pos_needed(uint64_t calculation_type, uint64_t misc_flags, char* cm_map_fname, char* set_fname, uint32_t min_bp_space, uint32_t genome_skip_write, uint32_t ld_modifier, uint32_t epi_modifier, uint32_t cluster_modifier) {
return (calculation_type & (CALC_MAKE_BED | CALC_MAKE_BIM | CALC_RECODE | CALC_GENOME | CALC_HOMOZYG | CALC_LD_PRUNE | CALC_REGRESS_PCS | CALC_MODEL | CALC_GLM | CALC_CLUMP | CALC_BLOCKS | CALC_FLIPSCAN | CALC_TDT | CALC_QFAM | CALC_FST | CALC_SHOW_TAGS | CALC_DUPVAR | CALC_RPLUGIN)) || (misc_flags & (MISC_EXTRACT_RANGE | MISC_EXCLUDE_RANGE)) || cm_map_fname || set_fname || min_bp_space || genome_skip_write || ((calculation_type & CALC_LD) && (!(ld_modifier & LD_MATRIX_SHAPEMASK))) || ((calculation_type & CALC_EPI) && (epi_modifier & EPI_FAST_CASE_ONLY)) || ((calculation_type & CALC_CMH) && (!(cluster_modifier & CLUSTER_CMH2)));
}
static inline uint32_t are_marker_cms_needed(uint64_t calculation_type, char* cm_map_fname, Two_col_params* update_cm) {
if (calculation_type & (CALC_MAKE_BED | CALC_MAKE_BIM | CALC_RECODE)) {
if (cm_map_fname || update_cm) {
return MARKER_CMS_FORCED;
} else {
return MARKER_CMS_OPTIONAL;
}
}
return 0;
}
static inline uint32_t are_marker_alleles_needed(uint64_t calculation_type, char* freqname, Homozyg_info* homozyg_ptr, Two_col_params* a1alleles, Two_col_params* a2alleles, uint32_t ld_modifier, uint32_t snp_only, uint32_t clump_modifier, uint32_t cluster_modifier) {
return (freqname || (calculation_type & (CALC_FREQ | CALC_HARDY | CALC_MAKE_BED | CALC_MAKE_BIM | CALC_RECODE | CALC_REGRESS_PCS | CALC_MODEL | CALC_GLM | CALC_LASSO | CALC_LIST_23_INDELS | CALC_EPI | CALC_TESTMISHAP | CALC_SCORE | CALC_MENDEL | CALC_TDT | CALC_FLIPSCAN | CALC_QFAM | CALC_HOMOG | CALC_DUPVAR | CALC_RPLUGIN | CALC_DFAM)) || ((calculation_type & CALC_HOMOZYG) && (homozyg_ptr->modifier & HOMOZYG_GROUP_VERBOSE)) || ((calculation_type & CALC_LD) && (ld_modifier & LD_INPHASE)) || ((calculation_type & CALC_CMH) && (!(cluster_modifier & CLUSTER_CMH2))) || a1alleles || a2alleles || snp_only || (clump_modifier & (CLUMP_VERBOSE | CLUMP_BEST)));
}
static inline int32_t relationship_or_ibc_req(uint64_t calculation_type) {
return (relationship_req(calculation_type) || (calculation_type & CALC_IBC));
}
int32_t plink(char* outname, char* outname_end, char* bedname, char* bimname, char* famname, char* cm_map_fname, char* cm_map_chrname, char* phenoname, char* extractname, char* excludename, char* keepname, char* removename, char* keepfamname, char* removefamname, char* filtername, char* freqname, char* distance_wts_fname, char* read_dists_fname, char* read_dists_id_fname, char* evecname, char* mergename1, char* mergename2, char* mergename3, char* missing_mid_template, char* missing_marker_id_match, char* makepheno_str, char* phenoname_str, Two_col_params* a1alleles, Two_col_params* a2alleles, char* recode_allele_name, char* covar_fname, char* update_alleles_fname, char* read_genome_fname, Two_col_params* qual_filter, Two_col_params* update_chr, Two_col_params* update_cm, Two_col_params* update_map, Two_col_params* update_name, char* update_ids_fname, char* update_parents_fname, char* update_sex_fname, char* loop_assoc_fname, char* flip_fname, char* flip_subset_fname, char* sample_sort_fname, char* filtervals_flattened, char* condition_mname, char* condition_fname, char* filter_attrib_fname, char* filter_attrib_liststr, char* filter_attrib_sample_fname, char* filter_attrib_sample_liststr, char* rplugin_fname, uint32_t rplugin_port, double qual_min_thresh, double qual_max_thresh, double thin_keep_prob, double thin_keep_sample_prob, uint32_t new_id_max_allele_len, uint32_t thin_keep_ct, uint32_t thin_keep_sample_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, Oblig_missing_info* om_ip, Family_info* fam_ip, double check_sex_fthresh, double check_sex_mthresh, uint32_t check_sex_f_yobs, uint32_t check_sex_m_yobs, double distance_exp, double min_maf, double max_maf, double geno_thresh, double mind_thresh, double hwe_thresh, double tail_bottom, double tail_top, uint64_t misc_flags, uint64_t filter_flags, uint64_t calculation_type, uint32_t dist_calc_type, uintptr_t groupdist_iters, uint32_t groupdist_d, uintptr_t regress_iters, uint32_t regress_d, uint32_t parallel_idx, uint32_t parallel_tot, uint32_t splitx_bound1, uint32_t splitx_bound2, uint32_t ppc_gap, uint32_t sex_missing_pheno, uint32_t update_sex_col, uint32_t hwe_modifier, uint32_t min_ac, uint32_t max_ac, uint32_t genome_modifier, double genome_min_pi_hat, double genome_max_pi_hat, Homozyg_info* homozyg_ptr, Cluster_info* cluster_ptr, uint32_t neighbor_n1, uint32_t neighbor_n2, Set_info* sip, Ld_info* ldip, Epi_info* epi_ip, Clump_info* clump_ip, Rel_info* relip, Score_info* sc_ip, uint32_t recode_modifier, uint32_t allelexxxx, uint32_t merge_type, uint32_t sample_sort, 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 write_var_range_ct, uint32_t covar_modifier, Range_list* covar_range_list_ptr, uint32_t write_covar_modifier, uint32_t write_covar_dummy_max_categories, uint32_t dupvar_modifier, uint32_t mwithin_col, uint32_t model_modifier, uint32_t model_cell_ct, uint32_t model_mperm_val, uint32_t glm_modifier, double glm_vif_thresh, uint32_t glm_xchr_model, uint32_t glm_mperm_val, Range_list* parameters_range_list_ptr, Range_list* tests_range_list_ptr, double ci_size, double pfilter, double output_min_p, uint32_t mtest_adjust, double adjust_lambda, uint32_t gxe_mcovar, Aperm_info* apip, uint32_t mperm_save, uintptr_t ibs_test_perms, uint32_t perm_batch_size, double lasso_h2, double lasso_minlambda, Range_list* lasso_select_covars_range_list_ptr, uint32_t testmiss_modifier, uint32_t testmiss_mperm_val, uint32_t permphe_ct, Ll_str** file_delete_list_ptr) {
FILE* bedfile = NULL;
FILE* phenofile = NULL;
uintptr_t unfiltered_marker_ct = 0;
uintptr_t* marker_exclude = NULL;
uintptr_t marker_exclude_ct = 0;
uintptr_t marker_ct = 0;
uintptr_t max_marker_id_len = 0;
// set_allele_freqs = .bed set bit frequency in middle of loading process, A2
// allele frequency later.
double* set_allele_freqs = NULL;
uintptr_t topsize = 0;
uintptr_t unfiltered_sample_ct = 0;
uintptr_t unfiltered_sample_ct4 = 0;
uintptr_t unfiltered_sample_ctl = 0;
uintptr_t* sample_exclude = NULL;
uintptr_t sample_exclude_ct = 0;
uintptr_t sample_ct = 0;
uint32_t* sample_sort_map = NULL;
uintptr_t* founder_info = NULL;
uintptr_t* sex_nm = NULL;
uintptr_t* sex_male = NULL;
uint32_t genome_skip_write = (cluster_ptr->ppc != 0.0) && (!(calculation_type & CALC_GENOME)) && (!read_genome_fname);
uint32_t marker_pos_needed = are_marker_pos_needed(calculation_type, misc_flags, cm_map_fname, sip->fname, min_bp_space, genome_skip_write, ldip->modifier, epi_ip->modifier, cluster_ptr->modifier);
uint32_t marker_cms_needed = are_marker_cms_needed(calculation_type, cm_map_fname, update_cm);
uint32_t marker_alleles_needed = are_marker_alleles_needed(calculation_type, freqname, homozyg_ptr, a1alleles, a2alleles, ldip->modifier, (filter_flags / FILTER_SNPS_ONLY) & 1, clump_ip->modifier, cluster_ptr->modifier);
// add other nchrobs subscribers later
uint32_t nchrobs_needed = (calculation_type & CALC_GENOME) && freqname;
uint32_t uii = 0;
int64_t llxx = 0;
uint32_t nonfounders = (misc_flags / MISC_NONFOUNDERS) & 1;
uint32_t pheno_all = pheno_modifier & PHENO_ALL;
char* marker_ids = NULL;
uint32_t* marker_id_htable = NULL;
uint32_t marker_id_htable_size = 0;
double* marker_cms = NULL;
// marker_allele_ptrs[2 * i] is id of A1 (usually minor) allele at marker i
// marker_allele_ptrs[2 * i + 1] is id of A2 allele
// Single-character allele names point to g_one_char_strs[]; otherwise
// string allocation occurs on the heap.
char** marker_allele_ptrs = NULL;
uintptr_t max_marker_allele_len = 2; // includes trailing null
uintptr_t* marker_reverse = NULL;
int32_t retval = 0;
uint32_t map_is_unsorted = 0;
uint32_t map_cols = 3;
uint32_t affection = 0;
uint32_t gender_unk_ct = 0;
uintptr_t* pheno_nm = NULL;
uintptr_t* pheno_nm_datagen = NULL; // --make-bed/--recode/--write-covar only
uintptr_t* orig_pheno_nm = NULL; // --all-pheno + --pheno-merge
uintptr_t* pheno_c = NULL;
uintptr_t* orig_pheno_c = NULL;
uintptr_t* geno_excl_bitfield = NULL;
uintptr_t* ac_excl_bitfield = NULL;
double* pheno_d = NULL;
double* orig_pheno_d = NULL;
char* sample_ids = NULL;
uintptr_t max_sample_id_len = 4;
char* paternal_ids = NULL;
uintptr_t max_paternal_id_len = 2;
char* maternal_ids = NULL;
uintptr_t max_maternal_id_len = 2;
unsigned char* wkspace_mark = NULL;
uintptr_t cluster_ct = 0;
uint32_t* cluster_map = NULL; // unfiltered sample IDs
// index for cluster_map, length (cluster_ct + 1)
// cluster_starts[n+1] - cluster_starts[n] = length of cluster n (0-based)
uint32_t* cluster_starts = NULL;
char* cluster_ids = NULL;
uintptr_t max_cluster_id_len = 2;
double* mds_plot_dmatrix_copy = NULL;
uintptr_t* cluster_merge_prevented = NULL;
double* cluster_sorted_ibs = NULL;
char* cptr = NULL;
uint64_t dists_alloc = 0;
double missing_phenod = (double)missing_pheno;
double ci_zt = 0.0;
uintptr_t bed_offset = 3;
uint32_t* marker_pos = NULL;
uint32_t hh_exists = 0;
uint32_t pheno_ctrl_ct = 0;
uintptr_t covar_ct = 0;
char* covar_names = NULL;
uintptr_t max_covar_name_len = 0;
uintptr_t* covar_nm = NULL;
double* covar_d = NULL;
uintptr_t* gxe_covar_nm = NULL;
uintptr_t* gxe_covar_c = NULL;
uintptr_t* pca_sample_exclude = NULL;
uintptr_t pca_sample_ct = 0;
uintptr_t ulii = 0;
uint32_t pheno_nm_ct = 0;
uint32_t plink_maxsnp = 0;
uint32_t plink_maxfid = 0;
uint32_t plink_maxiid = 0;
uint32_t max_bim_linelen = 0;
unsigned char* wkspace_mark2 = NULL;
unsigned char* wkspace_mark_precluster = NULL;
unsigned char* wkspace_mark_postcluster = NULL;
uint32_t* nchrobs = NULL;
int32_t* hwe_lls = NULL;
int32_t* hwe_lhs = NULL;
int32_t* hwe_hhs = NULL;
int32_t* hwe_ll_cases = NULL;
int32_t* hwe_lh_cases = NULL;
int32_t* hwe_hh_cases = NULL;
int32_t* hwe_ll_allfs = NULL;
int32_t* hwe_lh_allfs = NULL;
int32_t* hwe_hh_allfs = NULL;
int32_t* hwe_hapl_allfs = NULL;
int32_t* hwe_haph_allfs = NULL;
pthread_t threads[MAX_THREADS];
uint32_t* uiptr;
double* rel_ibc;
uintptr_t uljj;
uint32_t ujj;
uint32_t ukk;
char* outname_end2;
int32_t ii;
int64_t llyy;
int64_t llzz;
uint32_t sample_male_ct;
uint32_t sample_f_ct;
uint32_t sample_f_male_ct;
Pedigree_rel_info pri;
uintptr_t marker_uidx;
if ((cm_map_fname || update_cm) && (!marker_cms_needed)) {
LOGPRINTF("Error: --%s results would never be used. (Did you forget --make-bed?)\n", cm_map_fname? "cm-map" : "update-cm");
goto plink_ret_INVALID_CMDLINE;
} else if (update_map && (!marker_pos_needed)) {
logprint("Error: --update-map results would never be used. (Did you forget --make-bed?)\n");
goto plink_ret_INVALID_CMDLINE;
}
if (ci_size != 0.0) {
ci_zt = ltqnorm(1 - (1 - ci_size) / 2);
}
if (relip->modifier & REL_CALC_COV) {
relip->ibc_type = -1;
}
// famname[0] is nonzero iff we're not in the --merge-list special case
if ((calculation_type & CALC_MAKE_BED) && famname[0]) {
#ifdef _WIN32
uii = GetFullPathName(bedname, FNAMESIZE, tbuf, NULL);
if ((!uii) || (uii > FNAMESIZE))
#else
if (!realpath(bedname, tbuf))
#endif
{
uii = strlen(bedname);
if ((uii > 8) && ((!memcmp(&(bedname[uii - 8]), ".bed.bed", 8)) || (!memcmp(&(bedname[uii - 8]), ".bim.bed", 8)) || (!memcmp(&(bedname[uii - 8]), ".fam.bed", 8)))) {
LOGPRINTFWW("Error: Failed to open %s. (--bfile expects a filename *prefix*; '.bed', '.bim', and '.fam' are automatically appended.)\n", bedname);
} else {
LOGPRINTFWW(errstr_fopen, bedname);
}
goto plink_ret_OPEN_FAIL;
}
memcpy(outname_end, ".bed", 5);
// if file doesn't exist, realpath returns NULL on Linux instead of what
// the path would be.
#ifdef _WIN32
uii = GetFullPathName(outname, FNAMESIZE, &(tbuf[FNAMESIZE + 64]), NULL);
if (uii && (uii <= FNAMESIZE) && (!strcmp(tbuf, &(tbuf[FNAMESIZE + 64]))))
#else
cptr = realpath(outname, &(tbuf[FNAMESIZE + 64]));
if (cptr && (!strcmp(tbuf, &(tbuf[FNAMESIZE + 64]))))
#endif
{
logprint("Note: --make-bed input and output filenames match. Appending '~' to input\nfilenames.\n");
uii = strlen(bedname);
memcpy(tbuf, bedname, uii + 1);
memcpy(&(bedname[uii]), "~", 2);
if (rename(tbuf, bedname)) {
logprint("Error: Failed to append '~' to input .bed filename.\n");
goto plink_ret_OPEN_FAIL;
}
uii = strlen(bimname);
memcpy(tbuf, bimname, uii + 1);
memcpy(&(bimname[uii]), "~", 2);
if (rename(tbuf, bimname)) {
logprint("Error: Failed to append '~' to input .bim filename.\n");
goto plink_ret_OPEN_FAIL;
}
uii = strlen(famname);
memcpy(tbuf, famname, uii + 1);
memcpy(&(famname[uii]), "~", 2);
if (rename(tbuf, famname)) {
logprint("Error: Failed to append '~' to input .fam filename.\n");
goto plink_ret_OPEN_FAIL;
}
}
}
if (calculation_type & CALC_MERGE) {
if (((fam_cols & FAM_COL_13456) != FAM_COL_13456) || (misc_flags & MISC_AFFECTION_01) || (missing_pheno != -9)) {
logprint("Error: --merge/--bmerge/--merge-list cannot be used with an irregularly\nformatted reference fileset (--no-fid, --no-parents, --no-sex, --no-pheno,\n--1, --missing-pheno). Use --make-bed first.\n");
goto plink_ret_INVALID_CMDLINE;
}
// Only append -merge to the filename stem if --make-bed or --recode lgen
// is specified.
ulii = bed_suffix_conflict(calculation_type, recode_modifier);
if (ulii) {
memcpy(outname_end, "-merge", 7);
}
retval = merge_datasets(bedname, bimname, famname, outname, ulii? &(outname_end[6]) : outname_end, mergename1, mergename2, mergename3, sample_sort_fname, calculation_type, merge_type, sample_sort, misc_flags, chrom_info_ptr);
if (retval || (!(calculation_type & (~CALC_MERGE)))) {
goto plink_ret_1;
}
uljj = (uintptr_t)(outname_end - outname) + (ulii? 6 : 0);
memcpy(memcpya(bedname, outname, uljj), ".bed", 5);
memcpy(memcpya(famname, bedname, uljj), ".fam", 5);
memcpy(memcpya(bimname, bedname, uljj), ".bim", 5);
if ((calculation_type & CALC_MAKE_BED) && ulii) {
if (push_ll_str(file_delete_list_ptr, bedname) || push_ll_str(file_delete_list_ptr, famname) || push_ll_str(file_delete_list_ptr, bimname)) {
goto plink_ret_NOMEM;
}
}
}
// don't use fopen_checked() here, since we want to customize the error
// message.
if (bedname[0]) {
bedfile = fopen(bedname, "rb");
if (!bedfile) {
uii = strlen(bedname);
if ((uii > 8) && ((!memcmp(&(bedname[uii - 8]), ".bed.bed", 8)) || (!memcmp(&(bedname[uii - 8]), ".bim.bed", 8)) || (!memcmp(&(bedname[uii - 8]), ".fam.bed", 8)))) {
LOGPRINTFWW("Error: Failed to open %s. (--bfile expects a filename *prefix*; '.bed', '.bim', and '.fam' are automatically appended.)\n", bedname);
} else {
LOGPRINTFWW(errstr_fopen, bedname);
}
goto plink_ret_OPEN_FAIL;
}
}
// load .bim, count markers, filter chromosomes
if (bimname[0]) {
if (update_name) {
ulii = 0;
retval = scan_max_strlen(update_name->fname, update_name->colid, update_name->colx, update_name->skip, update_name->skipchar, &max_marker_id_len, &ulii);
if (retval) {
goto plink_ret_1;
}
if (ulii > 80) {
// only warn on long new marker ID, since if there's a long old marker ID
// and no long new one, it's reasonable to infer that the user is fixing
// the problem, so we shouldn't spam them.
logprint("Warning: Unusually long new variant ID(s) in --update-name file. Double-check\nyour file and command-line parameters, and consider changing your naming\nscheme if you encounter memory problems.\n");
}
if (ulii > max_marker_id_len) {
max_marker_id_len = ulii;
}
}
if (!marker_alleles_needed) {
allelexxxx = 0;
}
retval = load_bim(bimname, &map_cols, &unfiltered_marker_ct, &marker_exclude_ct, &max_marker_id_len, &marker_exclude, &set_allele_freqs, nchrobs_needed? (&nchrobs) : NULL, &marker_allele_ptrs, &max_marker_allele_len, &marker_ids, missing_mid_template, new_id_max_allele_len, missing_marker_id_match, chrom_info_ptr, &marker_cms, &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, marker_pos_needed, marker_cms_needed, marker_alleles_needed, ((!(calculation_type & (~(CALC_MAKE_BED | CALC_MAKE_BIM | CALC_MAKE_FAM)))) && (mind_thresh == 1.0) && (geno_thresh == 1.0) && (hwe_thresh == 0.0) && (!update_map) && (!freqname))? NULL : "make-bed", ".bim file", &max_bim_linelen);
if (retval) {
goto plink_ret_1;
}
}
// load .fam, count samples
if (famname[0]) {
uii = fam_cols & FAM_COL_6;
if (uii && phenoname) {
uii = (pheno_modifier & PHENO_MERGE) && (!makepheno_str);
}
if (!uii) {
pheno_modifier &= ~PHENO_MERGE; // nothing to merge
}
if (update_ids_fname) {
ulii = 0;
retval = scan_max_fam_indiv_strlen(update_ids_fname, 3, &max_sample_id_len);
if (retval) {
goto plink_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 plink_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 plink_ret_1;
}
unfiltered_sample_ct4 = (unfiltered_sample_ct + 3) / 4;
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 plink_ret_NOMEM;
}
}
if ((pheno_modifier & PHENO_MERGE) && pheno_all) {
if (aligned_malloc(&orig_pheno_nm, unfiltered_sample_ctl * sizeof(intptr_t))) {
goto plink_ret_NOMEM;
}
memcpy(orig_pheno_nm, pheno_nm, unfiltered_sample_ctl * sizeof(intptr_t));
if (pheno_c) {
if (aligned_malloc(&orig_pheno_c, unfiltered_sample_ctl * sizeof(intptr_t))) {
goto plink_ret_NOMEM;
}
memcpy(orig_pheno_c, pheno_c, unfiltered_sample_ctl * sizeof(intptr_t));
} else if (pheno_d) {
orig_pheno_d = (double*)malloc(unfiltered_sample_ct * sizeof(double));
if (!orig_pheno_d) {
goto plink_ret_NOMEM;
}
memcpy(orig_pheno_d, pheno_d, unfiltered_sample_ct * sizeof(double));
}
}
count_genders(sex_nm, sex_male, unfiltered_sample_ct, sample_exclude, &uii, &ujj, &gender_unk_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 plink_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 plink_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(&cptr, &uiptr, unfiltered_sample_ct, sample_exclude, 0, sample_ids, max_sample_id_len, 0, 0, strcmp_deref);
if (retval) {
goto plink_ret_1;
}
if (makepheno_str) {
retval = makepheno_load(phenofile, makepheno_str, unfiltered_sample_ct, cptr, max_sample_id_len, uiptr, pheno_nm, &pheno_c);
if (retval) {
goto plink_ret_1;
}
} else if (phenofile) {
retval = load_pheno(phenofile, unfiltered_sample_ct, 0, cptr, max_sample_id_len, uiptr, 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 plink_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 plink_ret_1;
}
}
wkspace_reset(wkspace_mark);
}
if (pheno_c) {
/*
if (calculation_type & (CALC_REGRESS_PCS | CALC_REGRESS_PCS_DISTANCE)) {
sprintf(logbuf, "Error: --regress-pcs%s requires a scalar phenotype.\n", (calculation_type & CALC_REGRESS_PCS_DISTANCE)? "-distance" : "");
goto plink_ret_INVALID_CMDLINE_2;
*/
if (calculation_type & (CALC_REGRESS_REL | CALC_REGRESS_DISTANCE | CALC_UNRELATED_HERITABILITY | CALC_GXE)) {
if (calculation_type & CALC_REGRESS_REL) {
logprint("Error: --regress-rel calculation requires a scalar phenotype.\n");
} else if (calculation_type & CALC_REGRESS_DISTANCE) {
logprint("Error: --regress-distance calculation requires a scalar phenotype.\n");
} else if (calculation_type & CALC_UNRELATED_HERITABILITY) {
logprint("Error: --unrelated-heritability requires a scalar phenotype.\n");
}
goto plink_ret_INVALID_CMDLINE;
}
} else {
if (calculation_type & CALC_CLUSTER) {
if (cluster_ptr->modifier & CLUSTER_CC) {
logprint("Error: --cc requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
} else if ((cluster_ptr->max_cases != 0xffffffffU) || (cluster_ptr->max_ctrls != 0xffffffffU)) {
logprint("Error: --mcc requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
}
} else if ((calculation_type & CALC_EPI) && (epi_ip->modifier & EPI_FAST)) {
logprint("Error: --fast-epistasis requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
} else if (calculation_type & (CALC_IBS_TEST | CALC_GROUPDIST | CALC_FLIPSCAN)) {
if (calculation_type & (CALC_IBS_TEST | CALC_GROUPDIST)) {
logprint("Error: --ibs-test and --groupdist calculations require a case/control\nphenotype.\n");
} else if (calculation_type & CALC_FLIPSCAN) {
logprint("Error: --flip-scan requires a case/control phenotype.\n");
}
goto plink_ret_INVALID_CMDLINE;
} else if ((calculation_type & CALC_RECODE) && (recode_modifier & (RECODE_HV | RECODE_HV_1CHR))) {
logprint("Error: --recode HV{-1chr} requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
} else if ((calculation_type & CALC_FST) && (misc_flags & MISC_FST_CC)) {
logprint("Error: '--fst case-control' requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
}
}
if (!pheno_all) {
if (loop_assoc_fname || (!pheno_d)) {
if ((calculation_type & CALC_GLM) && (!(glm_modifier & GLM_LOGISTIC))) {
logprint("Error: --linear without --all-pheno requires a scalar phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
} else if (calculation_type & CALC_QFAM) {
logprint("Error: QFAM test requires a scalar phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
}
} else if (!pheno_c) {
if ((calculation_type & CALC_MODEL) && (!(model_modifier & MODEL_ASSOC))) {
logprint("Error: --model requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
} else if ((calculation_type & CALC_GLM) && (glm_modifier & GLM_LOGISTIC)) {
logprint("Error: --logistic without --all-pheno requires a case/control phenotype.\n");
goto plink_ret_INVALID_CMDLINE;
} else if (calculation_type & (CALC_CMH | CALC_HOMOG | CALC_TESTMISS | CALC_TDT | CALC_DFAM)) {
if (calculation_type & CALC_CMH) {
logprint("Error: --mh and --mh2 require a case/control phenotype.\n");
} else if (calculation_type & CALC_HOMOG) {
logprint("Error: --homog requires a case/control phenotype.\n");
} else if (calculation_type & CALC_TESTMISS) {
logprint("Error: --test-missing requires a case/control phenotype.\n");
} else if (calculation_type & CALC_TDT) {
logprint("Error: --tdt requires a case/control phenotype.\n");
} else {
logprint("Error: --dfam requires a case/control phenotype.\n");
}
goto plink_ret_INVALID_CMDLINE;
}
}
}
}
if (cm_map_fname) {
// need sorted bps, but not marker IDs
if (map_is_unsorted & UNSORTED_BP) {
logprint("Error: --cm-map requires a sorted .bim file. Retry this command after using\n--make-bed to sort your data.\n");
goto plink_ret_INVALID_FORMAT;
}
retval = apply_cm_map(cm_map_fname, cm_map_chrname, unfiltered_marker_ct, marker_exclude, marker_pos, marker_cms, chrom_info_ptr);
if (retval) {
goto plink_ret_1;
}
}
uii = update_cm || update_map || update_name || (marker_alleles_needed && (update_alleles_fname || (flip_fname && (!flip_subset_fname)))) || filter_attrib_fname || qual_filter;
if (uii || extractname || excludename) {
// only permit duplicate marker IDs for --extract/--exclude
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 plink_ret_1;
}
if (update_cm) {
retval = update_marker_cms(update_cm, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_cms);
if (retval) {
goto plink_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 plink_ret_1;
}
if (update_alleles_fname || (marker_alleles_needed && flip_fname && (!flip_subset_fname)) || 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 (marker_alleles_needed) {
if (update_alleles_fname) {
retval = update_marker_alleles(update_alleles_fname, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, marker_allele_ptrs, &max_marker_allele_len, outname, outname_end);
if (retval) {
goto plink_ret_1;
}
}
if (flip_fname && (!flip_subset_fname)) {
retval = flip_strand(flip_fname, marker_id_htable, marker_id_htable_size, marker_ids, max_marker_id_len, unfiltered_marker_ct, marker_exclude, marker_allele_ptrs);
if (retval) {
goto plink_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 plink_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 plink_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 plink_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 plink_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 plink_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 plink_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 plink_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 plink_ret_1;
}
}
wkspace_reset(wkspace_mark);
}
if (allelexxxx) {
allelexxxx_recode(allelexxxx, marker_allele_ptrs, unfiltered_marker_ct, marker_exclude, unfiltered_marker_ct - marker_exclude_ct);
}
if (thin_keep_prob != 1.0) {
if (random_thin_markers(thin_keep_prob, unfiltered_marker_ct, marker_exclude, &marker_exclude_ct)) {
goto plink_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 plink_ret_1;
}
}
if (bedfile) {
if (fseeko(bedfile, 0, SEEK_END)) {
goto plink_ret_READ_FAIL;
}
llxx = ftello(bedfile);
if (!llxx) {
logprint("Error: Empty .bed file.\n");
goto plink_ret_INVALID_FORMAT;
}
rewind(bedfile);
uii = fread(tbuf, 1, 3, bedfile);
llyy = ((uint64_t)unfiltered_sample_ct4) * unfiltered_marker_ct;
llzz = ((uint64_t)unfiltered_sample_ct) * ((unfiltered_marker_ct + 3) / 4);
if ((uii == 3) && (!memcmp(tbuf, "l\x1b\x01", 3))) {
llyy += 3;
} else if ((uii == 3) && (!memcmp(tbuf, "l\x1b", 2))) {
// v1.00 sample-major
llyy = llzz + 3;
bed_offset = 2;
} else if (uii && (*tbuf == '\x01')) {
// v0.99 SNP-major
llyy += 1;
bed_offset = 1;
} else if (uii && (!(*tbuf))) {
// v0.99 sample-major
llyy = llzz + 1;
bed_offset = 2;
} else {
// pre-v0.99, sample-major, no header bytes
llyy = llzz;
if (llxx != llyy) {
// probably not PLINK-format at all, so give this error instead of
// "invalid file size"
logprint("Error: Invalid header bytes in .bed file.\n");
goto plink_ret_INVALID_FORMAT;
}
bed_offset = 2;
}
if (llxx != llyy) {
if ((*tbuf == '#') || ((uii == 3) && (!memcmp(tbuf, "chr", 3)))) {
logprint("Error: Invalid header bytes in PLINK 1 .bed file. (Is this a UCSC Genome\nBrowser BED file instead?)\n");
goto plink_ret_INVALID_FORMAT;
} else {
sprintf(logbuf, "Error: Invalid .bed file size (expected %" PRId64 " bytes).\n", llyy);
goto plink_ret_INVALID_FORMAT_2;
}
}
if (bed_offset == 2) {
strcpy(outname_end, ".bed.vmaj"); // not really temporary
logprint("Sample-major .bed file detected. Transposing...\n");
fclose(bedfile);
retval = sample_major_to_snp_major(bedname, outname, unfiltered_marker_ct, unfiltered_sample_ct, llxx);
if (retval) {
goto plink_ret_1;
}
LOGPRINTFWW("Variant-major .bed written to %s .\n", outname);
strcpy(bedname, outname);
if (fopen_checked(&bedfile, bedname, "rb")) {
goto plink_ret_OPEN_FAIL;
}
bed_offset = 3;
}
}
if (update_ids_fname || update_parents_fname || update_sex_fname || keepname || keepfamname || removename || removefamname || filter_attrib_sample_fname || om_ip->marker_fname || filtername) {
wkspace_mark = wkspace_base;
retval = sort_item_ids(&cptr, &uiptr, unfiltered_sample_ct, sample_exclude, sample_exclude_ct, sample_ids, max_sample_id_len, 0, 0, strcmp_deref);
if (retval) {
goto plink_ret_1;
}
ulii = unfiltered_sample_ct - sample_exclude_ct;
if (update_ids_fname) {
retval = update_sample_ids(update_ids_fname, cptr, ulii, max_sample_id_len, uiptr, sample_ids);
if (retval) {
goto plink_ret_1;
}
wkspace_reset(wkspace_base);
retval = sort_item_ids(&cptr, &uiptr, unfiltered_sample_ct, sample_exclude, sample_exclude_ct, sample_ids, max_sample_id_len, 0, 0, strcmp_deref);
if (retval) {
goto plink_ret_1;
}
} else {
if (update_parents_fname) {
retval = update_sample_parents(update_parents_fname, cptr, ulii, max_sample_id_len, uiptr, paternal_ids, max_paternal_id_len, maternal_ids, max_maternal_id_len, founder_info);
if (retval) {
goto plink_ret_1;
}
}
if (update_sex_fname) {
retval = update_sample_sexes(update_sex_fname, update_sex_col, cptr, ulii, max_sample_id_len, uiptr, sex_nm, sex_male);
if (retval) {
goto plink_ret_1;
}
}
}
if (keepfamname) {
retval = keep_or_remove(keepfamname, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 2);
if (retval) {
goto plink_ret_1;
}
}
if (keepname) {
retval = keep_or_remove(keepname, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 0);
if (retval) {
goto plink_ret_1;
}
}
if (removefamname) {
retval = keep_or_remove(removefamname, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 3);
if (retval) {
goto plink_ret_1;
}
}
if (removename) {
retval = keep_or_remove(removename, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, 1);
if (retval) {
goto plink_ret_1;
}
}
if (filter_attrib_sample_fname) {
retval = filter_attrib_sample(filter_attrib_sample_fname, filter_attrib_sample_liststr, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct);
if (retval) {
goto plink_ret_1;
}
}
if (om_ip->marker_fname) {
// would rather do this with pre-sorted markers, but that might break
// order-of-operations assumptions in existing pipelines
retval = load_oblig_missing(bedfile, bed_offset, unfiltered_marker_ct, marker_exclude, marker_exclude_ct, marker_ids, max_marker_id_len, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, sex_male, chrom_info_ptr, om_ip);
if (retval) {
goto plink_ret_1;
}
}
if (filtername) {
if (!mfilter_col) {
mfilter_col = 1;
}
retval = filter_samples_file(filtername, cptr, ulii, max_sample_id_len, uiptr, unfiltered_sample_ct, sample_exclude, &sample_exclude_ct, filtervals_flattened, mfilter_col);
if (retval) {
goto plink_ret_1;
}
}
wkspace_reset(wkspace_mark);
}
if (famname[0]) {
if (gender_unk_ct && (!(sex_missing_pheno & ALLOW_NO_SEX))) {