-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertf.c
More file actions
1717 lines (1430 loc) · 42.4 KB
/
Copy pathconvertf.c
File metadata and controls
1717 lines (1430 loc) · 42.4 KB
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 <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <nicklib.h>
#include <getpars.h>
#include <globals.h>
#include "admutils.h"
#include "mcio.h"
#include "mcmcpars.h"
#include "egsubs.h"
#include "exclude.h"
#include "h2d.h"
#define WVERSION "8650"
/**
reformats files.
pedfile junk (6, 7 cols, ACGT added)
packped (.bed) added input and output
pedfile can now be in random order
tersemode supported
ped -> ancestrymap outputs alleles
various fixups for ped files in computing variance, reference alleles
alleles can (say) be A X if second allele unknown
allele flipped for bed format
huge ascii file trapped now
fastdup added
r2thresh added
killr2 added to convertf (default is NO)
bug in processing C0 SNPS in ped files fixed
outputall added (all indivs, snps output)
genolistname added (list of packed ancestrymap files one per line)
support for enhanced eigenstrat format and genolist can also be enhanced eigenstrat
Chromosome # 1-89 now supported. Improved treatment of funny chromosomes.
MT -> 90
XY -> 91
Various bugfixes and checks for file size
inpack2 no longer needs big memory
hash table lookup for snpindex
check size more carefully
update to mcio.c (snprawindex)
maxmissfrac added
poplistname added
remap (newsnpname) added
flipsnpname, flipreference added
flipsnpname flips genotype values
if flipped we can flip reference allele too if we wish (default YES)
badpedignore. Crazy bases flagged as ignore
remapind (newindivname) added
flipstrandname moves alleles to opposite strand
maxmissing added (counts alleles like smartpca)
minor bug fixed for plink homozygous files
polarize added (force homozygotes to 2 if possible)
if not possible SNP set -> ignore
bigread now set
map files output 23, 24 for X, Y
fastdupthresh, fastdupkill added
remap now allows allele changes when genotypes are killed
zerodistance added
mcio now calls toupper on alleles
bugfix for newsnpname + phasedmode
downsample added (make pseudo homozygotes)
chimpmode added support for chr2a etc
support for snps out of order in packed format
(pordercheck: NO)
remapcheck NO (useful for moving reference sequence)
mcio.c imported from eig 5.0.1
randommode, seed added (randomizes fastdup)
newignore: NO => newsnpname with new snps will have genotypes filled in with unknown (default is don't output)
inddupcheck added (compulsory)
usesamples added => poplistname = NULL)
copyalleles (if newsnpname alleles are copied from old file)
familypopnames (use egroup for family in .fam output
dupcheck contains iter number
minvalpop :: every pop must have at least this number of valids (default not set)
fillmissing: added
better handling of seed
hiresgendis added
O2 version
support for .prob files
wipeoutchrom added
probprintname added
imputeusingpops added
mkhaploid added
forcemiss added
slowdup added
.map output files no longer print alleles (use .pedsnp if wanted)
transpose packed format (TGEN)
memorymapping implemented
transform hets (1 -> 2)
shuffle
randomhetfix
indivlistname added (overrules poplistname)
instem, outstem added
countvalidall added
defaultegroup added (replace ???)
popsizelimit = -1 ;
allgenos added
*/
#define MAXFL 50
#define MAXSTR 512
char *trashdir = "/var/tmp";
int qtmode = NO;
Indiv **indivmarkers, **probindivs, **indm2 ;
int numsnps, numindivs, numprobindivs, numind2 ;
SNP **snpmarkers;
SNP **snpm2;
int zerodistance = NO; // YES => force gdis 0
int downsample = NO; // make pseudo homozygotes
int pordercheck = YES;
int familypopnames = NO;
int hiresgendis = NO ;
int memorymap = NO ;
int transformhets = NO ;
int randomhetfix = NO ;
int shuffle = NO ;
int countvalidall = NO ;
int popsizelimit = -1 ;
int allgenos = NO ;
int nums2;
char *genotypename = NULL;
char *genotypelist = NULL;
char *snpname = NULL;
char *indoutfilename = NULL;
char *snpoutfilename = NULL;
char *genooutfilename = NULL;
char *tgenooutfilename = NULL;
char *indivname = NULL;
char *newindivname = NULL;
char *instem = NULL, *outstem = NULL ;
char *probfilename = NULL;
char *proboutfilename = NULL;
char *probindivname = NULL;
char *probprintname = NULL ;
char *probprintid = NULL ;
char *badsnpname = NULL;
char *xregionname = NULL;
char *deletesnpoutname = NULL;
char *flipsnpname = NULL;
char *flipstrandname = NULL;
int flipreference = YES;
int remapcheck = YES;
char *poplistname = NULL;
char *indivlistname = NULL ;
char *fillmissingpoplistname = NULL ;
int haploidfill = NO ;
char **fpops ;
int nfpops = 0 ;
double makemiss = -1 ;
double r2thresh = -1.0;
double r2genlim = 0.01; // Morgans
double r2physlim = 5.0e6;
double maxmissfrac = 1000.0; // no thresh
int maxmiss = -1; // no thresh
int minvalpop = -1 ;
int killr2 = NO;
int mkdiploid = NO;
int mkhaploid = NO;
int packout = -1;
int tersem = YES;
int randommode = NO;
int seed = 0;
extern enum outputmodetype outputmode;
extern int checksizemode;
char *omode = "packedancestrymap";
extern int packmode;
int ogmode = NO;
int fastdup = NO;
int slowdup = NO;
int fastdupnum = 10;
double fastdupthresh = .75;
double fastdupkill = .75;
char *polarid = NULL;
int polarindex = -1;
int phasedmode = NO;
int badpedignore = NO;
int chimpmode = NO;
int xchrom = -1;
int lopos = -999999999;
int hipos = 999999999;
int minchrom = 1;
int maxchrom = 97;
int wipeoutchrom = -1 ;
int deletedup = YES; // only one marker at a position
char *newsnpname = NULL; // new map
int newignore = YES; // default ignore snps not in old list
int polarcheck = NO;
int copyalleles = NO;
int rmcompress = YES ;
char *defaultegroup = NULL ;
char *usesamples = NULL;
char unknowngender = 'U';
double nhwfilter = -1;
void setomode (enum outputmodetype *outmode, char *omode);
void readcommands (int argc, char **argv);
void remap (SNP ** s1, int nums1, SNP ** s2, int nums2);
void remapind (SNP ** snpmarkers, int numsnps, Indiv ** indivmarkers,
Indiv ** indm2, int numindivs, int numind2);
void pickx (SNP * c1, SNP * c2, SNP ** px1, SNP ** px2);
void dedupit (SNP ** snpmarkers, int numsnps);
void flipsnps (char *fsname, SNP ** snpm, int numsnps, int phasedmode);
void flipstrand (char *fsname, SNP ** snpm, int numsnps);
int mkindh2d (Indiv ** indivmarkers, Indiv *** pindm2, int numindivs);
void remaph2d (SNP ** snpmarkers, int numsnps, Indiv ** indivmarkers,
Indiv ** indm2, int numindivs, int numind2);
void flip1 (SNP * cupt, int phasedmode, int flipreference);
void fixaa (SNP * cupt1, SNP * cupt2);
void fvalg (SNP * cupt, int val);
char cxx (char *c1, char *c2);
void downsamp (SNP * cupt);
void forcemiss (double yprob);
int setsamp (Indiv ** indivmarkers, int numindivs, char *usesamples);
int testmisspop(SNP **snpmarkers, int numsnps, Indiv **indivmarkers, int numindivs, int minvalpops) ;
int fillmiss(SNP **snpmarkers, Indiv **indivmarkers, int numsnps, int numindivs, char **fpops, int nfpops) ;
int fixsnpdistance(SNP **snpm, int numsnps) ;
long loadprobpack(SNP **snpmarkers, Indiv **indivmarkers, int numsnps, int numindivs, char *bigbuff) ;
void doshuffle(SNP **snpm, int numsnps, int numindivs) ;
int
main (int argc, char **argv)
{
int **snppos;
int *snpindx;
char **snpnames, **indnames;
char **eglist;
int lsnplist, lindlist, numeg;
int i, j;
SNP *cupt, *cupt1, *cupt2, *cupt3;
Indiv *indx;
double gpos1, gpos2, cpos1, cpos2, gd, cd, gd100;
double rthresh, zt;
int mpflag, ret, numvalidind, nvalid, numvalidsnps;
int ch1, ch2;
int fmnum, lmnum;
int num, n1, n2;
int nkill = 0;
int t, k, g;
int k1, k2 ;
int nindiv = 0, e, f, lag = 1;
double xc[9], xd[4], xc2[9];
double ychi, zscore, zthresh = 20.0;
double y1, y2, ymem;
int nignore, numrisks = 1;
int *valcount ;
char **genolist;
int numgenolist;
char c1, c2;
int t1, t2, x;
unsigned char *packp, *packp2 ;
long plen, plen2, numx ;
int rl2 = 4 ;
int numout = -1 ;
malexhet = YES; // convertf default is don't change the data
tersem = YES; // no snp counts
int jlast = -1 ;
FILE *probfile = NULL ;
double *pp ;
readcommands (argc, argv);
cputime(0) ;
calcmem(0) ;
printf("## %s version: %s\n", argv[0], WVERSION) ;
if (seed == 0) {
seed = seednum() ;
}
SRAND(seed) ;
if (fastdup) {
randommode = YES;
}
if (randommode)
printf("seed: %d\n", seed) ;
if (chimpmode) {
setchimpmode (YES);
setchr (YES);
}
if (familypopnames) {
setfamilypopnames (YES);
}
if (outputall) memorymap = NO ; // need to read in all data
if (allgenos) memorymap = NO ; // need to read in all data
if (memorymap) setmemorymap(YES) ;
if (instem != NULL) setinfiles(&indivname, &snpname, &genotypename, instem) ;
if (outstem != NULL) setoutfiles(&indoutfilename, &snpoutfilename, &genooutfilename, outstem) ;
if (indivlistname != NULL) poplistname = NULL ;
if (rmcompress == NO) {
fatalx("rmcompress obsolete -- use outputall!\n") ;
}
setomode (&outputmode, omode);
packmode = YES;
settersemode (tersem);
if (hiresgendis) sethiressnp() ;
if (r2thresh > 0.0)
killr2 = YES;
if (badpedignore)
setbadpedignore ();
setpordercheck (pordercheck);
numsnps =
getsnps (snpname, &snpmarkers, 0.0, badsnpname, &nignore, numrisks);
for (i = 0; i < numsnps; i++) {
if (xchrom == -1)
break;
cupt = snpmarkers[i];
if (cupt->chrom != xchrom)
cupt->ignore = YES;
if (cupt->ignore)
continue;
t = nnint (cupt->physpos);
if ((t < lopos) || (t > hipos))
cupt->ignore = YES;
}
nignore = 0;
for (i = 0; i < numsnps; i++) {
cupt = snpmarkers[i];
if (cupt->chrom > maxchrom)
cupt->ignore = YES;
if (cupt->chrom < minchrom)
cupt->ignore = YES;
if (cupt->ignore)
++nignore;
}
if (numsnps == nignore)
fatalx ("no valid snps\n");
numindivs = getindivs (indivname, &indivmarkers);
if (polarid != NULL) {
polarindex = indindex (indivmarkers, numindivs, polarid);
if (polarindex < 0)
fatalx ("polarid %s not found\n", polarid);
}
for (i = 0; i < numindivs; ++i) {
if (defaultegroup == NULL) break ;
indx = indivmarkers[i];
t = strcmp(indx -> egroup, "Ignore") ;
if (t==0) indx -> ignore = YES ;
t = strcmp(indx -> egroup, "???") ;
if (t != 0) continue ;
freestring(&indx -> egroup) ;
indx -> egroup = strdup(defaultegroup) ;
}
ZALLOC (eglist, numindivs, char *);
if (poplistname != NULL) {
numeg = loadlist (eglist, poplistname);
}
else {
numeg = makeeglist(eglist, numindivs, indivmarkers, numindivs) ;
}
if (poplistname != NULL) {
seteglist (indivmarkers, numindivs, poplistname);
for (i = 0; i < numindivs; ++i) {
indx = indivmarkers[i];
if (indx->affstatus == NO)
indx->ignore = YES;
}
}
else {
setstatus (indivmarkers, numindivs, NULL) ;
}
if (popsizelimit>=0) {
setplimit (indivmarkers, numindivs, eglist, numeg, popsizelimit) ;
}
setgk(indivmarkers, numindivs, poplistname, NULL, NULL) ;
inddupcheck (indivmarkers, numindivs);
if (indivlistname != NULL) {
lindlist = numlines(indivlistname) ;
ZALLOC(indnames, lindlist, char *) ;
lindlist = getlist(indivlistname, indnames) ;
for (k=0; k<numindivs; ++k) {
indx = indivmarkers[k] ;
indx -> ignore = YES ;
indx -> gkode = -1 ;
t = indxstring(indnames, lindlist, indx -> ID) ;
if (t>=0) {
indx -> ignore = NO ;
indx -> gkode = 1 ;
}
}
}
if (genotypelist != NULL) {
getgenos_list (genotypelist, snpmarkers, indivmarkers,
numsnps, numindivs, nignore);
}
else {
setgenotypename (&genotypename, indivname);
getgenos (genotypename, snpmarkers, indivmarkers,
numsnps, numindivs, nignore);
}
for (i=0; i< numsnps; ++i) {
if (transformhets == NO) break ;
// het -> countallele
cupt = snpmarkers[i] ;
for (j=0; j<numindivs; ++j) {
g = getgtypes(cupt, j) ;
if (g==1) putgtypes(cupt, j, 2) ;
}
}
for (i=0; i< numsnps; ++i) {
if (randomhetfix == NO) break ;
// het -> random homozygote
cupt = snpmarkers[i] ;
for (j=0; j<numindivs; ++j) {
g = getgtypes(cupt, j) ;
if (g==1) {
g = 2 * ranmod(2) ;
putgtypes(cupt, j, g) ;
}
}
}
// read in probs
numprobindivs = 0 ;
plen = 0 ;
packp = NULL ;
if ((probfilename != NULL) && (probindivname == NULL)) probindivname = indivname ;
if (probindivname != NULL) {
numprobindivs = getindivs (probindivname, &probindivs);
for (j=0; j<numprobindivs; ++j) {
indx = probindivs[j] ;
k = indindex(indivmarkers, numindivs, indx -> ID) ;
indx -> idnum = k ;
if (k<0) {
indx -> ignore = YES ;
printf("*** warning *** ID %s missing in indivs\n", indx -> ID) ;
}
}
plen = numsnps*numprobindivs*rl2 ;
ZALLOC(packp, plen, unsigned char) ;
printf("calling inprobx: hashcheck: %d\n", hashcheck) ;
inprobx (probfilename, snpmarkers, probindivs, numsnps, numprobindivs, (char *) packp) ;
}
if (numprobindivs > 0) {
for (i=0; i<numsnps; i++) {
cupt = snpmarkers[i] ;
cupt -> probbuff = (char *) packp + i*numprobindivs*rl2 ;
ZALLOC(cupt -> diplike, numindivs, double *) ;
for (j=0; j<numindivs; ++j) {
indx = indivmarkers[j] ;
if (indx -> ignore) continue ;
ZALLOC(cupt -> diplike[j], 3, double) ;
}
}
for (i=0; i<numsnps; i++) {
cupt = snpmarkers[i] ;
for (j=0; j<numprobindivs; ++j) {
indx = probindivs[j] ;
if (indx -> ignore) continue ;
k = indx -> idnum ;
if (k<0) continue ;
indx = indivmarkers[k] ;
if (indx -> ignore) continue ;
x = loaddiplike(cupt -> diplike[k], (unsigned char *) cupt -> probbuff + j*rl2) ;
jlast = MAX(j, jlast) ;
}
}
}
if (makemiss > 0) printf("forcing missing with prob: %9.f\n", makemiss) ;
if (newsnpname != NULL) {
numindivs = rmindivs (snpmarkers, numsnps, indivmarkers, numindivs);
// clean up before funky stuff
clearsnpord ();
nums2 = getsnps (newsnpname, &snpm2, 0.0, NULL, &nignore, numrisks);
remap (snpmarkers, numsnps, snpm2, nums2);
snpmarkers = snpm2;
numsnps = nums2;
}
if (newindivname != NULL) {
numind2 = getindivs (newindivname, &indm2);
remapind (snpmarkers, numsnps, indivmarkers, indm2, numindivs, numind2);
indivmarkers = indm2;
numindivs = numind2;
if (polarid != NULL) {
polarindex = indindex (indivmarkers, numindivs, polarid);
}
inddupcheck (indivmarkers, numindivs);
}
if (mkdiploid) {
numindivs = rmindivs (snpmarkers, numsnps, indivmarkers, numindivs);
numind2 = mkindh2d (indivmarkers, &indm2, numindivs);
remaph2d (snpmarkers, numsnps, indivmarkers, indm2, numindivs, numind2);
indivmarkers = indm2;
numindivs = numind2;
}
if (mkhaploid) {
numindivs = rmindivs (snpmarkers, numsnps, indivmarkers, numindivs);
numind2 = mkindd2h (indivmarkers, &indm2, numindivs);
remapd2h (snpmarkers, numsnps, indivmarkers, indm2, numindivs, numind2);
indivmarkers = indm2;
numindivs = numind2;
}
if (deletedup)
dedupit (snpmarkers, numsnps); // only one marker per position
for (i = 0; i < numsnps; i++) {
cupt = snpmarkers[i];
if (zerodistance)
cupt->genpos = 0.0;
c1 = cupt->alleles[0];
c2 = cupt->alleles[1];
t1 = pedval (&c1) % 5;
t2 = pedval (&c2) % 5; // 0 and 5 are no good
if ((t1 == 0) && (t2 > 0))
flip1 (cupt, phasedmode, YES);
}
t = fixsnpdistance(snpmarkers, numsnps) ;
if (t>0) printf("%12d SNP positions adjusted\n", t) ;
if ((proboutfilename != NULL) && (numprobindivs > 0)) {
plen2 = numsnps*numindivs*rl2 ;
ZALLOC(packp2, plen2, unsigned char) ;
numx = loadprobpack(snpmarkers, indivmarkers, numsnps, numindivs, (char *) packp2) ;
}
if (deletedup)
dedupit (snpmarkers, numsnps); // only one marker per position
for (i = 0; i < numsnps; i++) {
cupt = snpmarkers[i];
if (zerodistance)
cupt->genpos = 0.0;
c1 = cupt->alleles[0];
c2 = cupt->alleles[1];
t1 = pedval (&c1) % 5;
t2 = pedval (&c2) % 5; // 0 and 5 are no good
if ((t1 == 0) && (t2 > 0))
flip1 (cupt, phasedmode, YES);
}
flipstrand (flipstrandname, snpmarkers, numsnps);
flipsnps (flipsnpname, snpmarkers, numsnps, phasedmode);
if (polarindex >= 0) {
for (i = 0; i < numsnps; i++) {
cupt = snpmarkers[i];
g = getgtypes (cupt, polarindex);
if (g == 0) {
printf ("polarizing %s", cupt->ID);
printf (" %3d %12.0f", cupt->chrom, cupt->physpos);
printnl ();
fflush (stdout);
flip1 (cupt, NO, YES);
g = getgtypes (cupt, polarindex);
if (g != 2)
fatalx ("badbug\n");
}
if (g != 2)
cupt->ignore = YES;
}
}
for (i = 0; i < numsnps; i++) {
cupt = snpmarkers[i];
if (downsample)
downsamp (cupt);
}
if (countvalidall) {
ZALLOC(valcount, numindivs, int) ;
numvalidgtallind(valcount, snpmarkers, numsnps, numindivs) ;
for (j=0; j<numindivs; ++j) {
indx = indivmarkers[j] ;
if (indx -> ignore) continue ;
printf("valids: %20s %20s %8d\n", indx -> ID, indx-> egroup, valcount[j]) ;
}
}
if (outputall) {
numout = outfiles (snpoutfilename, indoutfilename, genooutfilename,
snpmarkers, indivmarkers, numsnps, numindivs, packout, ogmode);
printf("numsnps output: %d\n", numout) ;
if (proboutfilename != NULL) {
printf("calling outprob\n") ; fflush(stdout) ;
outprobx(proboutfilename, snpmarkers, indivmarkers, numsnps, numindivs, (char *) packp2) ;
printf("PROB file %s written: %ld records\n", proboutfilename, numx) ;
}
printf ("##end of convertf run (outputall mode)\n");
return 0;
}
if (usesamples != NULL) {
poplistname = NULL;
setsamp (indivmarkers, numindivs, usesamples);
}
if (fillmissingpoplistname != NULL) {
ZALLOC(fpops, numindivs, char *) ;
nfpops = loadlist(fpops, fillmissingpoplistname) ;
t = fillmiss(snpmarkers, indivmarkers, numsnps, numindivs, fpops, nfpops) ;
printf("%10d missing genotypes filled\n", t) ;
}
if (rmcompress) {
printf("before compress: snps: %d indivs: %d\n", numsnps, numindivs) ;
numsnps = rmsnps (snpmarkers, numsnps, deletesnpoutname);
numindivs = rmindivs (snpmarkers, numsnps, indivmarkers, numindivs);
printf("after compress: snps: %d indivs: %d\n", numsnps, numindivs) ;
}
fflush(stdout) ;
if (shuffle) doshuffle(snpmarkers, numsnps, numindivs) ;
forcemiss(makemiss) ;
// printf("got here! 2\n") ; fflush(stdout) ;
// force missing on wipeoutchrom. retain snp.
if (wipeoutchrom > 0) printf("wiping out chrom: %d\n", wipeoutchrom) ;
for (i=0; i<numsnps; i++) {
if (wipeoutchrom<0) break ;
cupt = snpmarkers[i] ;
if (cupt -> chrom != wipeoutchrom) continue ;
fvalg(cupt, 999) ; // wipe out
}
if (killr2) {
nkill =
killhir2 (snpmarkers, numsnps, numindivs, r2physlim, r2genlim,
r2thresh);
if (nkill > 0)
printf ("killhir2. number of snps killed: %d\n", nkill);
}
if (nhwfilter > 0) {
hwfilter (snpmarkers, numsnps, numindivs, nhwfilter, deletesnpoutname);
}
if (xregionname) {
excluderegions (xregionname, snpmarkers, numsnps, deletesnpoutname);
}
numvalidind = 0;
cputimes(0, 1) ;
for (i = 0; i < numindivs; ++i) {
indx = indivmarkers[i];
if (indx->ignore)
continue;
if (numvalidgtind (snpmarkers, numsnps, i) == 0) {
printf ("no data for individual: %s\n", indx->ID);
if ((probprintname == NULL) &&(allgenos == NO)) indx->ignore = YES;
}
if (indx->ignore == NO)
++numvalidind;
}
if (maxmiss < 0)
maxmiss = (int) (maxmissfrac * (double) numvalidind + 1);
printf ("numvalidind: %5d maxmiss: %5d\n", numvalidind, maxmiss);
// printf("clock 1: %9.3f\n", cputimes(1, 1)) ;
if ((numvalidind == 0) && (probprintname == NULL))
fatalx ("no samples with valid genotypes!\n");
cputimes(0, 2) ;
t = testmisspop(snpmarkers, numsnps, indivmarkers, numindivs, minvalpop) ;
if (minvalpop>0) printf("minvalpop: deleted %d retained %d\n", t, numsnps-t) ;
// printf("clock 2: %9.3f\n", cputimes(1, 2)) ;
cputimes(0, 3) ;
for (k = 0; k < numsnps; ++k) {
if (allgenos) break ;
if (maxmiss > numvalidind)
break;
cupt = snpmarkers[k];
t = numvalidind - numvalidgtypes (cupt);
if (maxmiss < t) {
cupt->ignore = YES;
}
}
// printf("clock 3: %9.3f\n", cputimes(1, 3)) ;
if (slowdup) {
fastdup = NO ;
fastdupthresh = 0.5 ; fastdupkill = 2.0 ;
printf ("calling slowdupcheck\n");
setfastdupthresh (fastdupthresh, fastdupkill);
for (k1=0; k1<numindivs; ++k1) {
if (indivmarkers[k1] -> ignore) continue ;
for (k2=k1+1; k2<numindivs; ++k2) {
if (indivmarkers[k2] -> ignore) continue ;
slowdupcheck (snpmarkers, indivmarkers, numsnps, k1, k2) ;
}}}
if (fastdup) {
printf ("fastdup set %d\n", fastdupnum);
fflush (stdout);
if (fastdupnum > 0) {
setfastdupnum (fastdupnum);
setfastdupthresh (fastdupthresh, fastdupkill);
printf ("calling fastdupcheck\n");
fflush (stdout);
fastdupcheck (snpmarkers, indivmarkers, numsnps, numindivs);
}
}
if (decim > 0) {
snpdecimate (snpmarkers, numsnps, decim, dmindis, dmaxdis);
}
cputimes(0, 4) ;
printf("calling outfiles\n") ;
fflush(stdout) ;
numout = outfiles (snpoutfilename, indoutfilename, genooutfilename,
snpmarkers, indivmarkers, numsnps, numindivs, packout, ogmode);
if (tgenooutfilename != NULL) {
settrans(YES) ;
outpack (tgenooutfilename, snpmarkers, indivmarkers, numsnps, numindivs);
}
// printf("clock 4: %9.3f\n", cputimes(1, 4)) ;
printf("numsnps output: %d\n", numout) ;
fflush(stdout) ;
/**
x = snpindex(snpmarkers, numsnps, "rs12626123") ;
cupt = snpmarkers[x] ;
printf("zzq2 %s ", cupt -> ID) ;
printmatl(cupt -> diplike[0], 1, 3) ;
*/
if (proboutfilename != NULL) {
if (numprobindivs <= 0) fatalx("proboutfilename set but not probindivname\n") ;
numx = loadprobpack(snpmarkers, indivmarkers, numsnps, numindivs, (char *) packp2) ;
outprobx(proboutfilename, snpmarkers, indivmarkers, numsnps, numindivs, (char *) packp2) ;
printf("PROB file %s written: %ld records\n", proboutfilename, numx) ;
fflush(stdout) ;
}
if (probprintname != NULL) {
if (numprobindivs <= 0) fatalx("probprintname set but not probindivname\n") ;
if (numprobindivs == 1) probprintid = probindivs[0] -> ID ;
if (probprintid == NULL) fatalx("probprintid not set\n") ;
x = indindex(probindivs, numprobindivs, probprintid) ;
if (x<0) fatalx("probprintid: %s not in probindiv file\n", probprintid) ;
indx = probindivs[x] ;
openit(probprintname, &probfile, "w") ;
fprintf(probfile, "## prob for ID: %s file: %s\n", probprintid, indivname) ;
for (i=0; i<numsnps; i++) {
cupt = snpmarkers[i] ;
if (cupt -> ignore) continue ;
fprintf(probfile, "%20s ", cupt -> ID) ;
fprintf(probfile, "%2d ", cupt -> chrom) ;
fprintf(probfile, "%12.0f ", cupt -> physpos) ;
k = indx -> idnum ;
if (k<0) fatalx("badbug\n") ;
pp = cupt -> diplike[k] ;
printmatwxfile(pp, 1, 3, 3, probfile) ;
fprintf(probfile, "\n") ;
}
fclose(probfile) ;
}
ymem = calcmem(1)/1.0e6 ;
printf("##end of convertf: %12.3f seconds cpu %12.3f Mbytes in use\n", cputime(1), ymem) ;
return 0;
}
void
readcommands (int argc, char **argv)
{
int i ;
char *parname = NULL;
phandle *ph;
char str[5000];
char *tempname;
int n;
while ((i = getopt (argc, argv, "p:vV")) != -1) {
switch (i) {
case 'p':
parname = strdup (optarg);
break;
case 'v':
printf ("version: %s\n", WVERSION);
break;
case 'V':
verbose = YES;
break;
case '?':
printf ("Usage: bad params.... \n");
fatalx ("bad params\n");
}
}
if (parname==NULL) {
printf("no parameter file (-p)\n") ;
exit(1) ;
}
pcheck (parname, 'p');
printf ("parameter file: %s\n", parname);
ph = openpars (parname);
dostrsub (ph);
/**
DIR2: /fg/nfiles/admixdata/ms2
SSSS: DIR2/outfiles
genotypename: DIR2/autos_ccshad_fakes
eglistname: DIR2/eurlist
output: eurout
*/
getstring (ph, "genotypename:", &genotypename);
getstring (ph, "genotypelist:", &genotypelist);
getstring (ph, "snpname:", &snpname);
getstring (ph, "indivname:", &indivname);
getstring (ph, "badsnpname:", &badsnpname);
getstring (ph, "flipsnpname:", &flipsnpname);
getstring (ph, "flipstrandname:", &flipstrandname);
getstring (ph, "indoutfilename:", &indoutfilename);
getstring (ph, "indivoutname:", &indoutfilename); /* changed 11/02/06 */
getstring (ph, "snpoutfilename:", &snpoutfilename);
getstring (ph, "snpoutname:", &snpoutfilename); /* changed 11/02/06 */
getstring (ph, "genooutfilename:", &genooutfilename);
getstring (ph, "tgenooutfilename:", &tgenooutfilename);
getstring (ph, "genotypeoutname:", &genooutfilename); /* changed 11/02/06 */
getstring (ph, "tgenotypeoutname:", &tgenooutfilename) ;
getstring (ph, "outputformat:", &omode);
getstring (ph, "outputmode:", &omode);
getstring (ph, "polarize:", &polarid);
getstring (ph, "usesamples:", &usesamples);
getstring (ph, "defaultegroup:", &defaultegroup);
getint (ph, "zerodistance:", &zerodistance);
getint (ph, "memorymap:", &memorymap);
getint (ph, "mmap:", &memorymap);
getint (ph, "checksizemode:", &checksizemode);
getint (ph, "badpedignore:", &badpedignore);
getint (ph, "downsample:", &downsample);
getint (ph, "chimpmode:", &chimpmode);
getint (ph, "pordercheck:", &pordercheck);
getint (ph, "remapcheck:", &remapcheck);
getint (ph, "seed:", &seed);
getint (ph, "randommode:", &randommode);
getint (ph, "familypopnames:", &familypopnames);
getint (ph, "wipeoutchrom:", &wipeoutchrom);
getint (ph, "rmcompress:", &rmcompress);
getint (ph, "allgenos:", &allgenos);
getint (ph, "numchrom:", &numchrom);
getstring (ph, "xregionname:", &xregionname);
getdbl (ph, "hwfilter:", &nhwfilter);
getstring (ph, "deletesnpoutname:", &deletesnpoutname);
getint (ph, "outputgroup:", &ogmode);
getint (ph, "malexhet:", &malexhet);
getint (ph, "nomalexhet:", &malexhet); /* changed 11/02/06 */
getint (ph, "tersemode:", &tersem);
getint (ph, "familynames:", &familynames);
getint (ph, "packout:", &packout); /* now obsolete 11/02/06 */
getint (ph, "decimate:", &decim);
getint (ph, "dmindis:", &dmindis);
getint (ph, "dmaxdis:", &dmaxdis);
getint (ph, "flipreference:", &flipreference);
getint (ph, "fastdup:", &fastdup);
getint (ph, "slowdup:", &slowdup);
getint (ph, "fastdupnum:", &fastdupnum);
getdbl (ph, "fastdupthresh:", &fastdupthresh);
getdbl (ph, "fastdupkill:", &fastdupkill);
getint (ph, "killr2:", &killr2);
getint (ph, "hashcheck:", &hashcheck);
getint (ph, "outputall:", &outputall);
getint (ph, "sevencolumnped:", &sevencolumnped);
getint (ph, "phasedmode:", &phasedmode);
getint (ph, "polarcheck:", &polarcheck);
getint (ph, "copyalleles:", ©alleles);
// we assume with newsnpname we are (A,B) in S1 and (A, B) or (rev(A), rev(B)) in S2
getdbl (ph, "r2thresh:", &r2thresh);
getdbl (ph, "r2genlim:", &r2genlim);
getdbl (ph, "r2physlim:", &r2physlim);
getint (ph, "chrom:", &xchrom);
getint (ph, "lopos:", &lopos);
getint (ph, "hipos:", &hipos);
getint (ph, "minchrom:", &minchrom);
getint (ph, "maxchrom:", &maxchrom);