-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcio.c
More file actions
6366 lines (5362 loc) · 149 KB
/
Copy pathmcio.c
File metadata and controls
6366 lines (5362 loc) · 149 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 <fcntl.h>
#include <ctype.h>
#include <mcio.h>
#include <xsearch.h>
#include <ranmath.h>
#include "geno.h"
/*! \file mcio.c
*
* \brief Input/Output Library
*/
/* global data */
extern int numchrom;
int usecm = NO; //!< genetic distances are in cMorgans
int plinkinputmode = NO;
static int snprawtab = NO;
static int debug = NO;
extern char *trashdir;
extern int qtmode; //!< user parameter (phenotype is quantitative)
extern int verbose; //!< user parameter (print additional output to stdout)
extern int familynames; //!< user parameter (prepend PLINK family names with colon to individual names)
extern double lp1, lp2;
extern double a1, b1;
extern int packmode; //!< flag - input {is not,is} in packed mode
extern char *packgenos; //!< packed genotype data (packit.h)
extern char *packepath;
extern long packlen; //!< allocated size of packgenos data space
extern long rlen; //!< number of bytes in packgenos space that each SNP's data occupies
extern int malexhet; //!< user parameter (retain het genotype data on male X chromosome)
extern int hashcheck; //!< user parameter (check input file hashes against input data)
extern int outputall;
extern int sevencolumnped;
static int dofreeped = YES;
static int hiresgendis = NO ;
static int memorymap = NO ;
static int transout = NO ;
int tempnum = 0;
int tempfake = 0;
static int *snpord = NULL; //!< snpord[i] == j if and only if snpm[j] is ith SNP in input file
static int numsnpord = 0; //!< current size of array snpord
static int *snporda[3]; //!< Copies of snpord for various data sets (used by mergeit)
static int numsnporda[3]; //!< Number of elements of snporda in use
static int dupcheck = YES ;
static int badpedignore = NO; //!< flag - ignore bad allele symbols in PED file
static int maxgenolinelength = -1;
static int tersemode = NO;
int checksizemode = YES;
int pedignore = YES;
enum outputmodetype outputmode = PACKEDANCESTRYMAP;
static double maxgpos[MAXCH];
static int chrmode = NO;
static int chimpmode = NO;
static int pordercheck = YES;
static int snpordered;
static int isgdis = YES; // no means gsid 0 in input
// fails if packed and out of order
static int familypopnames = NO;
// in .fam output use popnames (egroup)
static int oldsnpformat = NO ;
SNPDATA *tsdpt;
/* local function prototypes */
int getbedgenos (char *gname, SNP ** snpmarkers, Indiv ** indivmarkers,
int numsnps, int numindivs, int nignore);
void freeped ();
static char x2base (int x);
static void gtox (int g, char *cvals, int *p1, int *p2);
int ancval (int x);
static int setskipit (char *sx); // ignore lines in snp, map files
/* ---------------------------------------------------------------------------------------------------- */
void sethiressnp()
{
hiresgendis = YES ;
printf("hiressnp set!\n") ;
}
void
setoldsnpformat()
{
oldsnpformat = YES ;
// for ancestrymap compatibility
}
void
setfamilypopnames (int fpop)
{
familypopnames = fpop;
}
void
clearsnpord ()
{
free (snpord);
snpord = NULL;
numsnpord = 0;
}
void
snpsortit (int **spos, int *indx, int n)
{
long *lkode;
int i, base[3];
base[0] = 1;
base[1] = pow(10.0, 8) ;
base[2] = pow(10.0, 9) ;
ZALLOC (lkode, n, long);
for (i = 0; i < n; i++) {
lkode[i] = lkodeitbb (spos[i], 3, base);
}
lsortit (lkode, indx, n);
free (lkode);
return;
}
int
getsnps (char *snpfname, SNP *** snpmarkpt, double spacing,
char *badsnpname, int *numignore, int numrisks)
{
// returns number of SNPS
// numrisks
/* read file of real SNPS store in temporary structure */
SNPDATA **snpraw, *sdpt;
static SNP **snpmarkers;
SNP *cupt;
int **snppos;
int nreal, nfake, numsnps = 0, i, t, j;
int *snpindx;
double xspace;
int failx = 0;
if (snpfname == NULL)
fatalx ("(getsnps) null snpname\n");
xspace = spacing;
nreal = getsizex (snpfname);
if (nreal <= 0)
fatalx ("no snps found: snpfname: %s\n", snpfname);
ZALLOC (snpraw, nreal, SNPDATA *);
if (snpord == NULL) {
ZALLOC (snpord, nreal, int);
ivclear (snpord, -1, nreal);
numsnpord = nreal;
}
for (i = 0; i < nreal; i++) {
ZALLOC (snpraw[i], 1, SNPDATA);
charclear (snpraw[i]->cchrom, CNULL, 7);
snpraw[i]->inputrow = -1;
snpraw[i]->alleles[0] = '1';
snpraw[i]->alleles[1] = '2';
}
nreal = readsnpdata (snpraw, snpfname);
dobadsnps (snpraw, nreal, badsnpname);
ZALLOC (snppos, nreal, int *);
for (i = 0; i < nreal; i++) {
ZALLOC (snppos[i], 3, int);
}
for (i = 0; i < nreal; i++) {
sdpt = snpraw[i];
snppos[i][0] = sdpt->chrom;
if ((sdpt->ignore) && (plinkinputmode)) {
snppos[i][0] = 99;
if (pordercheck == YES) {
pordercheck = NO;
printf ("PLINK input. No check on SNP order\n");
}
}
t = snppos[i][1] = nnint ((sdpt->gpos) * GDISMUL);
if (isgdis)
snppos[i][1] = 0;
snppos[i][2] = nnint (sdpt->ppos);
// sdpt -> gpos = ((double) t)/ GDISMUL ;
}
/**
for (i=nreal-10; i<nreal; i++) {
printf("zzyy: %d ", i) ; printimat(snppos[i], 1, 3) ;
}
*/
ZALLOC (snpindx, nreal, int);
//snpsortit(snppos, snpindx, nreal) ;
ipsortit (snppos, snpindx, nreal, 3);
snpordered = YES;
for (i = 0; i < nreal; ++i) {
j = snpindx[i];
sdpt = snpraw[j];
//printf("zzz %d %d %s ", i, j, sdpt -> ID) ;
//printimat(snppos[j], 1, 3) ;
if (j != i) {
snpordered = NO;
++failx;
if (failx < 10) {
printf
("snp order check fail; snp list not ordered: %s (processing continues)",
snpfname);
printimat (snppos[i], 1, 3);
printf ("zzz %d %d\n", i, j);
}
}
}
if ((usecm) && (xspace > 0.5)) {
printf ("*** warning fake spacing given in cM\n");
xspace /= 100.0;
}
// get number of fakes
nfake = numfakes (snpraw, snpindx, nreal, xspace);
numsnps = nreal + nfake;
tempnum = numsnps;
tempfake = nfake;
// allocate storage
ZALLOC (snpmarkers, numsnps, SNP *);
for (i = 0; i < numsnps; i++) {
ZALLOC (snpmarkers[i], 1, SNP);
cupt = snpmarkers[i];
clearsnp (cupt);
ZALLOC (cupt->modelscores, numrisks, double);
ZALLOC (cupt->totmodelscores, numrisks, double);
}
tsdpt = snpraw[0];
*snpmarkpt = snpmarkers;
numsnps = loadsnps (snpmarkers, snpraw, snpindx, nreal, xspace, numignore);
/**
for (i=numsnps-10; i<numsnps; i++) {
cupt = snpmarkers[i] ;
printf("zzyy3: %d %d %12.0f\n", i, cupt -> chrom, cupt -> physpos) ;
}
*/
// and free up temporary storage
for (i = 0; i < nreal; i++) {
free (snpraw[i]);
free (snppos[i]);
}
free (snpraw);
free (snppos);
free (snpindx);
/* printf("numsnps: %d\n", numsnps) ; */
/*
if (snpord != NULL) {
printimat(snpord, 1, MIN(100, numsnps)) ;
}
*/
cupt = snpmarkers[0];
if (isnumword (cupt->ID))
printf
("*** warning: first snp %s is number. perhaps you are using .map format\n",
cupt->ID);
cksnpdup(snpmarkers, numsnps) ;
return numsnps;
}
void cksnpdup(SNP **snpmarkers, int numsnps)
{
int *hasha ;
int *inda, k, j1, j2, k1, k2, t ;
SNP *cupt1, *cupt2 ;
int numc = 0 ;
ZALLOC(hasha, numsnps, int) ;
ZALLOC(inda, numsnps, int) ;
for (k=0; k<numsnps; ++k) {
cupt1 = snpmarkers[k] ;
hasha[k] = stringhash(cupt1 -> ID) ;
}
isortit(hasha, inda, numsnps) ;
for (j1 = 0; j1 < numsnps; ++j1) {
for (j2 = j1+1; j2 < numsnps; ++j2) {
if (hasha[j1] != hasha[j2]) break ; // usual case
++numc ;
k1 = inda[j1] ;
k2 = inda[j2] ;
cupt1 = snpmarkers[k1] ;
cupt2 = snpmarkers[k2] ;
t = strcmp(cupt1 -> ID, cupt2 -> ID) ;
if (t==0) {
fatalx("duplicate SNP name found! :: %s\n", cupt1 -> ID) ;
}
}
}
free(hasha) ;
free(inda) ;
// printf("cksnpdup :: %d %d\n", numsnps, numc) ;
}
void freesnps(SNP ***psnpmarkers, int numsnps)
{
SNP **snpm = *psnpmarkers ;
SNP *cupt ;
int k ;
for (k=0; k<numsnps; ++k) {
cupt = snpm[k] ;
freecupt(&cupt) ;
}
free(snpm) ;
*psnpmarkers = NULL ;
}
/* ---------------------------------------------------------------------------------------------------- */
int
getsizex (char *fname)
{
char line[MAXSTR + 1], c;
char *spt[MAXFF], *sx;
int nsplit, num = 0;
int skipit;
int len;
FILE *fff;
openit (fname, &fff, "r");
line[MAXSTR] = '\0';
while (fgets (line, MAXSTR, fff) != NULL) {
nsplit = splitup (line, spt, MAXFF);
if (nsplit == 0)
continue;
sx = spt[0];
skipit = setskipit (sx); // comment line
if (skipit == NO) {
++num;
}
// now flush the rest of the line if necessary.
len = strlen (line);
c = line[len - 1];
if (c != '\n') {
while ((c = fgetc (fff)) != EOF) {
if (c == '\n')
break;
}
}
freeup (spt, nsplit);
continue;
}
fclose (fff);
fflush (stdout);
return num;
}
/* ---------------------------------------------------------------------------------------------------- */
int
ismapfile (char *fname)
{
// PLINK map file ?
// just look at file name (perhaps should look at format)
char *sx;
int len;
len = strlen (fname);
if (len < 4)
return NO;
sx = fname + len - 4;
if (strcmp (sx, ".map") == 0)
return YES;
if (strcmp (sx, ".bim") == 0)
return YES;
if (len < 7)
return NO;
sx = fname + len - 7;
if (strcmp (sx, ".pedsnp") == 0)
return YES;
return NO;
}
/* ---------------------------------------------------------------------------------------------------- */
int
ispedfile (char *fname)
{
// PLINK ped file ?
// just look at file name (perhaps should look at format)
char *sx;
int len;
len = strlen (fname);
if (len < 4)
return NO;
sx = fname + len - 4;
if (strcmp (sx, ".ped") == 0)
return YES;
if (strcmp (sx, ".fam") == 0)
return YES;
if (len < 7)
return NO;
sx = fname + len - 7;
if (strcmp (sx, ".pedind") == 0)
return YES;
return NO;
}
/* ---------------------------------------------------------------------------------------------------- */
int
isbedfile (char *fname)
{
// PLINK ped file ?
// just look at file name (perhaps should look at format)
char *sx;
int len;
len = strlen (fname);
if (len < 4)
return NO;
sx = fname + len - 4;
if (strcmp (sx, ".bed") == 0)
return YES;
return NO;
}
int
isitcram (char *fname)
{
char *sx;
int len;
len = strlen (fname);
if (len < 5)
return NO;
sx = fname + len - 5;
if (strcmp (sx, ".cram") == 0)
return YES;
return NO;
}
/* ---------------------------------------------------------------------------------------------------- */
int
readsnpdata (SNPDATA ** snpraw, char *fname)
{
char line[LONGSTR];
char *spt[MAXFF], *sx;
int nsplit, num = 0, k;
int skipit;
SNPDATA *sdpt;
double maxg = -9999.0;
FILE *fff;
int chrom;
int nbad = 0;
plinkinputmode = NO;
// if this is a PLINK file, call PLINK input routine
if (ismapfile (fname)) {
plinkinputmode = YES;
return readsnpmapdata (snpraw, fname);
}
usecm = NO;
vclear (maxgpos, -9999.0, MAXCH);
openit (fname, &fff, "r");
while (fgets (line, LONGSTR, fff) != NULL) {
nsplit = splitup (line, spt, MAXFF);
if (nsplit == 0)
continue;
sx = spt[0];
skipit = setskipit (sx);
if (skipit == NO) {
if (nsplit < 6)
fatalx ("(readsnpdata) bad line: %s 6 columns required\n", line);
sdpt = snpraw[num];
sdpt->inputrow = num;
if (strlen (spt[0]) >= IDSIZE)
fatalx ("snp ID too long: %s\n", spt[0]);
strcpy (sdpt->ID, spt[0]);
sdpt->chrom = chrom = str2chrom (spt[1]);
strncpy (sdpt->cchrom, spt[1], 6);
if ((chrom >= MAXCH) || (chrom <= 0)) {
if (nbad < 10)
printf ("warning: bad chrom: %s", line);
++nbad;
sdpt->chrom = MIN (chrom, BADCHROM);
sdpt->chrom = MAX (chrom, 0);
sdpt->ignore = YES;
}
// the genetic positions will be converted to Morgans (assumed to be in cM) if and only if
// any genetic position is greater than 100
sdpt->gpos = atof (spt[2]);
if (sdpt->gpos > 100) {
if (sdpt->gpos > 1.0e6)
fatalx ("absurd genetic distance:\n%s\n", line);
if (!usecm) {
printf ("*** warning. genetic distances are in cM not Morgans\n");
printf ("%s\n", line);
}
usecm = YES; // set flag to connvert to Morgans
}
maxgpos[chrom] = MAX (maxgpos[chrom], sdpt->gpos);
maxg = MAX (maxg, maxgpos[chrom]);
setsdpos (sdpt, atoi (spt[3]));
if (oldsnpformat) {
if (nsplit < 8) {
ivzero (sdpt->nn, 4);
if (nsplit == 6) {
sx = spt[4];
sdpt->alleles[0] = toupper (sx[0]);
sx = spt[5];
sdpt->alleles[1] = toupper (sx[0]);
}
}
else { // QUESTION: when does a SNP file have more than seven columns?
for (k = 0; k < 4; k++) {
sdpt->nn[k] = atoi (spt[4 + k]);
}
if (nsplit == 10) {
sx = spt[8];
sdpt->alleles[0] = toupper (sx[0]);
sx = spt[9];
sdpt->alleles[1] = toupper (sx[0]);
}
}
}
else {
sx = spt[4];
sdpt->alleles[0] = toupper (sx[0]);
sx = spt[5];
sdpt->alleles[1] = toupper (sx[0]);
}
++num;
}
freeup (spt, nsplit);
continue;
}
// if all genetic positions are set to zero, set from physical position
if (maxg <= 0.00001) {
isgdis = NO;
printf ("%s: genetic distance set from physical distance\n", fname);
usecm = NO;
for (k = 0; k < num; ++k) {
snpraw[k]->gpos = 1.0e-8 * snpraw[k]->ppos;
}
}
// convert to Morgans
if (usecm) {
for (k = 0; k < num; ++k) {
snpraw[k]->gpos /= 100.0;
}
}
fclose (fff);
return num;
}
/* ---------------------------------------------------------------------------------------------------- */
int
readsnpmapdata (SNPDATA ** snpraw, char *fname)
{
char line[MAXSTR];
char *spt[MAXFF], *sx;
int nsplit, num = 0, k, t;
int skipit, len;
SNPDATA *sdpt;
int nbad = 0;
FILE *fff;
int chrom;
double maxg = -9999.0;
vclear (maxgpos, -9999.0, MAXCH);
openit (fname, &fff, "r");
while (fgets (line, MAXSTR, fff) != NULL) {
nsplit = splitup (line, spt, MAXFF);
if (nsplit == 0)
continue;
sx = spt[0];
skipit = setskipit (sx);
if (skipit == NO) {
if (nsplit < 4)
fatalx ("(readsnpmapdata) bad line: %s\n", line);
sdpt = snpraw[num];
if (strlen (spt[1]) >= IDSIZE)
fatalx ("snp ID too long: %s\n", spt[1]);
strcpy (sdpt->ID, spt[1]);
if (nsplit >= 6) { // alleles in .map file are optional
sx = spt[4];
sdpt->alleles[0] = sx[0];
sx = spt[5];
sdpt->alleles[1] = sx[0];
if (sdpt->alleles[0] == '0')
sdpt->alleles[0] = 'X'; // unknown
if (sdpt->alleles[1] == '0')
sdpt->alleles[1] = 'X';
}
else {
charclear ( sdpt->alleles, CNULL, 2);
}
sx = spt[0];
sdpt->chrom = chrom = str2chrom (sx);
strncpy (sdpt->cchrom, sx, 6);
if ((chrom >= MAXCH) || (chrom <= 0)) {
if (nbad < 10)
printf ("warning (mapfile): bad chrom: %s", line);
++nbad;
sdpt->chrom = MIN (chrom, BADCHROM);
sdpt->chrom = MAX (chrom, 0);
sdpt->chrom = 99;
strcpy (sdpt->cchrom, "99");
sdpt->ignore = YES;
}
// the genetic positions will be converted to Morgans (assumed to be in cM) if and only if
// any genetic position is greater than 100
sdpt->gpos = atof (spt[2]);
if (sdpt->gpos > 100) {
if (sdpt->gpos > 1.0e6)
fatalx ("absurd genetic distance:\n%s\n", line);
if (!usecm) {
printf ("*** warning. genetic distances are in cM not Morgans\n");
printf ("%s\n", line);
}
usecm = YES;
}
maxgpos[chrom] = MAX (maxgpos[chrom], sdpt->gpos);
maxg = MAX (maxg, maxgpos[chrom]);
sdpt->ppos = atof (spt[3]);
if (nsplit < 8) {
ivzero (sdpt->nn, 4);
}
else {
for (k = 0; k < 4; k++) {
sdpt->nn[k] = atoi (spt[4 + k]);
}
}
sdpt->inputrow = num;
// printf("zz %d %d %s %12.0f\n", num, sdpt -> chrom, sdpt -> ID, sdpt -> ppos) ;
++num;
}
freeup (spt, nsplit);
continue;
}
if (maxg <= 0.00001) {
printf ("genetic distance set from physical distance\n");
usecm = NO;
isgdis = NO;
for (k = 0; k < num; ++k) {
snpraw[k]->gpos = 1.0e-8 * snpraw[k]->ppos;
}
}
if (usecm) {
for (k = 0; k < num; ++k) {
snpraw[k]->gpos /= 100.0;
}
}
if (snpord == NULL) {
ZALLOC (snpord, num, int);
ivclear (snpord, -1, num);
numsnpord = num;
}
fclose (fff);
return num;
}
/* ---------------------------------------------------------------------------------------------------- */
int
numfakes (SNPDATA ** snpraw, int *snpindx, int nreal, double spacing)
{
// it seems better for this internal routine
// to use the precomputed values
int nignore, numsnps;
int nfake = 0, i, k, indx;
int num = 0;
SNP *cupt;
SNPDATA *sdpt;
char *sname;
int *sp;
int xc = 0, chrom;
double fakedis, realdis; // gpos for fake marker
double yf, yr;
double physpos;
if (spacing <= 0.0)
fakedis = 1.0e20;
for (k = 0; k < nreal; k++) {
indx = snpindx[k];
sdpt = snpraw[indx];
chrom = sdpt->chrom;
realdis = sdpt->gpos;
if (chrom != xc) {
fakedis = nextmesh (realdis, spacing);
xc = chrom;
}
while (fakedis < realdis) {
fakedis += spacing;
++nfake;
}
}
// nfake is number of multiples of fakedis in chromosome
return nfake;
}
/* ---------------------------------------------------------------------------------------------------- */
double
nextmesh (double val, double spacing)
{
double y;
if (spacing == 0.0)
return 1.0e8;
y = ceil (val / spacing) * spacing;
if (y < val)
y += spacing;
return y;
}
/* ---------------------------------------------------------------------------------------------------- */
/*! \fn int loadsnps(SNP **snpm, SNPDATA **snpraw,
int *snpindx, int nreal, double spacing, int *numignore)
\brief Store raw SNP data in final array of type SNP *
\param snpm Pointer to array of type SNP * in which to store data
\param snpraw Pointer to array of type SNPDATA * in which preliminary data was stored
\param snpindx On entry, kth element of snpindx is index of the kth SNP in snpraw (This is not
the same as the value k itself if the SNPs were out of order in the file.)
\param nreal Number of SNPs stored in snpraw
\param spacing Maximum spacing between SNPs (not relevant to EIGENSOFT)
\param numignore Return number of SNPs to ignore here
*/
int
loadsnps (SNP ** snpm, SNPDATA ** snpraw,
int *snpindx, int nreal, double spacing, int *numignore)
{
// snppos, snpindx could be recalculated but
// it seems better for this internal routine
// to use the precomputed values
// do NOT call externally
int nignore, numsnps;
int nfake = 0, i, k, indx;
int num = 0, tnum;
SNP *cupt = NULL, *lastcupt = NULL, *tcupt;
SNPDATA *sdpt;
char *sname;
int *sp;
int xc = 0, chrom;
double fakedis, realdis, xrealdis; // gpos for fake marker
double yf, yr;
double physpos;
double xl, xr, xmid, al, ar, fraw;
double y;
int nn[2], n0, n1;
int cnum, t;
int inputrow, chimpfudge, xchimpfudge;
int ischimp = NO;
char ss[6];
if (spacing <= 0.0)
fakedis = 1.0e20;
strcpy (ss, "??");
for (k = 0; k < nreal; k++) {
indx = snpindx[k];
sdpt = snpraw[indx];
chrom = sdpt->chrom;
// defensive programming; should not be needed:
if (sdpt->cchrom[0] == CNULL) {
sprintf (sdpt->cchrom, "%d", chrom);
}
sname = sdpt->ID;
realdis = sdpt->gpos;
physpos = sdpt->ppos;
inputrow = sdpt->inputrow;
if (sdpt->chimpfudge)
ischimp = YES;
/**
if (k>(nreal-10)) {
printf("zzyy2b %d %d %12.0f %d\n", k, chrom, physpos, inputrow) ;
}
*/
t = strcmp (ss, sdpt->cchrom);
if (t != 0) {
fakedis = nextmesh (realdis, spacing);
xc = chrom;
cnum = 0;
strcpy (ss, sdpt->cchrom);
}
yf = fakedis;
yr = realdis;
// insert fake SNPs so the distance between SNPs is no greater than spacing
while (fakedis < realdis) {
if (cnum == 0)
break; // first SNP on chromosome
if (sdpt->ignore)
break;
if (nfake >= tempfake)
fatalx (" too many fake markers (bug) %d %d\n", num, nfake);
if (num >= tempnum)
fatalx (" too many markers (bug) %d %d\n", num, nfake);
cupt = snpm[num];
if (cupt == NULL)
fatalx ("bad loadsnps\n");
sprintf (cupt->ID, "fake-%d:%d", xc, nfake);
cupt->estgenpos = cupt->genpos = fakedis;
tcupt = lastcupt;
for (;;) {
xl = tcupt->genpos;
if (xl < fakedis)
break;
tnum = tcupt->markernum;
--tnum;
if (tnum < 0)
fatalx ("verybadbug\n");
tcupt = snpm[tnum];
if (tcupt->chrom != chrom)
fatalx ("badbug\n");
}
al = tcupt->physpos;
xr = realdis;;
ar = physpos;
y = cupt->physpos = interp (xl, xr, fakedis, al, ar);
if (chrom == -199) {
printf
("zzinterp %12.6f %12.6f %12.6f %12.0f %12.0f %12.6f\n", xl,
xr, fakedis, al, ar, y);
}
cupt->markernum = num;
cupt->isfake = YES;
cupt->chrom = xc;
strncpy (cupt->cchrom, ss, 6);
fakedis += spacing;
++num;
++nfake;
}
cupt = snpm[num];
if (cupt == NULL)
fatalx ("bad loadsnps\n");
strcpy (cupt->ID, sname);
sdpt->cuptnum = num;
cupt->estgenpos = cupt->genpos = realdis;
cupt->physpos = physpos;
cupt->markernum = num;
cupt->isfake = NO;
cupt->ignore = sdpt->ignore;
// if ((cupt -> ignore == NO) && (cupt -> isfake == NO))
if ((cupt->isfake = NO)) {
lastcupt = cupt;
++cnum;
}
cupt->isrfake = sdpt->isrfake;
cupt->chrom = xc;
strncpy (cupt->cchrom, ss, 6);
cupt->tagnumber = inputrow; // just used for pedfile
if (inputrow >= 0) {
if (inputrow >= numsnpord)
fatalx ("snpord overflow\n");
snpord[inputrow] = num;
}
n0 = sdpt->nn[0];
n1 = sdpt->nn[1];
fraw = mknn (nn, n0, n1);
copyiarr (nn, cupt->af_nn, 2);
cupt->aftrue = cupt->af_freq = fraw;
cupt->aa_aftrue = cupt->aa_af_freq = fraw;
cupt->alleles[0] = sdpt->alleles[0];
cupt->alleles[1] = sdpt->alleles[1];
n0 = sdpt->nn[2];
n1 = sdpt->nn[3];
fraw = mknn (nn, n0, n1);
copyiarr (nn, cupt->cauc_nn, 2);
cupt->cftrue = cupt->cauc_freq = fraw;
cupt->aa_cftrue = cupt->aa_cauc_freq = fraw;
++num;
}
// now make list of ignored snps used by loadgeno for check
numsnps = num;
for (k = 0; k < nreal; k++) {
indx = snpindx[k];
sdpt = snpraw[indx];
if (sdpt->ignore == NO)
continue;
inputrow = sdpt->inputrow;
chrom = sdpt->chrom;
sname = sdpt->ID;
realdis = sdpt->gpos;
physpos = sdpt->ppos;
cupt = snpm[sdpt->cuptnum];
cupt->tagnumber = inputrow; // just used for pedfile
/*
strncpy(cupt -> ID, sname, IDSIZE-1) ;
cupt -> genpos = realdis ;
cupt -> physpos = physpos ;
cupt -> markernum = num ;
cupt -> isfake = NO ;