-
Notifications
You must be signed in to change notification settings - Fork 2
/
mr_predictor.pl
executable file
·2266 lines (1951 loc) · 80.8 KB
/
mr_predictor.pl
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
#!/usr/bin/perl -w
#mr_predictor.pl
#
#a program to simulate multilocus genotypes of a intermediate phenotype (related to a terminal phenotype), to construct effect those loci have
# on the destination phenotype, given a known relationship between intermediate and destination phenotypes
#
#BF Voight
#created: 01.09.09
#INFO ON PROGRAM
sub VERSION() { "v0.028" } ; #Current version of program
sub MODIFIED() { "08.12.14" } ; #Date of current version
#outline for script
#1. Read in a 'locus' file which contains:
#(1) SNP
#(2) risk and non-risk allele
#(3) Additive Variance term (lnOR Effect sizes, or beta coefficients for additive term)
#(4) SE on Va (-9 if you assume a model without Va (Va = 0 always)
#(5) Dominance Variance term (lnOR effect size, or beta coefficients for domdev term)
#(6) SE on Vd (-9 if you assume a model without Vd (Vd = 0 always)
#(7) frequency of risk allele
#2. Read in phenotype info file which contains:
#(1) phenotype label
#(2) (I)ntermediate or (D)estination phenotype flag
#if (I), then
#(3a) effect size of intermediate to destination (units of beta or ln(OR))
#(4a) SE on effect size
#if (D), then
#(3b) (L)iability or (Q)uantitative model
#(4b) overall prevalence of destination phenotype (liability model) (L) or -9
#3. Modelling: intermediate phenotype
#(a) calculate sigma_r, the variance contributed by the loci that you are simulating.
#sigma_r for a single locus should be:
#
# sig_r = 2*p*q*(a + d(q-p))^2 + (2pqd)^2
#
#(b) calculate sigma_t, which is sig_assumed - sigma_r. in most cases, I'm assuming a model of mu_assumed=0, sig_assumed=1, so this is just = 1-sigma_r
#--make sure you check that sigma_t is not negative (i.e., sigma_r > sigma_assumed).
#4. Modelling: destination phenotype
#(a) figure out if the destination is a quantiative or boolean trait.
#(b1_1) if boolean, calculate the prevalence of the given intermediate trait distribution assuming the relationship of intermediate to destination
#will have to use numerical integration (approximation hack, say).
#if Kp > Kassume, then assert Kp rather than Kass and warn, extra_k = 0;
#if Kp > 1 or Kaassume > 1, die
#if if Kp < Kassume, add the difference in terms to probability of extra_k = Kassume-Kp to liability model.
#(b1_2) model prob(aff) ~ intermediate + other predictors + extra_k.
#(b2_1) if quantiative trait, you can draw model specifically:
#
# Dpheno = N(0,sigma_r2) + sqrt(r2)*Ipheno
#
#where sigma_r2 = 1-r^2, and r^2 is the correlation between Dpheno and Ipheno [drawn from the distribution of correlation]
#5. Simulations
#revision history
## See http://coruscant.itmat.upenn.edu/mr_predictor/version-history/
###for outputting purposes
$| = 1;
########
#MODULES INCLUDED
use Math::Random;
### testing of Math::random module
# @mean = (0,0);
# push @vcovm, [ ( 1, 0.8 ) ];
# push @vcovm, [ ( 0.8, 1 ) ];
#
# push @ref_arr, \@{$vcovm[0]};
# push @ref_arr, \@{$vcovm[1]};
#
# @results = Math::Random::random_multivariate_normal(1000,@mean, @vcovm);
#
# foreach $res (@results) {
# print "$res->[0] $res->[1]\n";
# }
#
# exit();
sub string_numerically { $a cmp $b; }
sub print_header {
my ($filehandle) = @_;
print $filehandle "\n";
print $filehandle "#----------------------------------#\n";
print $filehandle "# mr_predictor # " . VERSION() . " # " . MODIFIED() . " #\n";
print $filehandle "#----------------------------------#\n";
print $filehandle "# (c) Benjamin F. Voight #\n";
print $filehandle "#----------------------------------#\n";
print $filehandle "\n";
}
sub print_string {
my ($string, $out_fh, $log_fh) = @_;
print $out_fh "$string";
print $log_fh "$string";
}
sub print_covars {
my ($ref_sim_covars, $ref_nlabel, $out_fh) = @_;
if ($$ref_nlabel == 1) { #print header
print $out_fh "FID IID";
foreach my $covar (sort string_numerically keys %$ref_sim_covars) {
print $out_fh " $covar";
}
print $out_fh "\n";
}
#print labels
print $out_fh "$$ref_nlabel $$ref_nlabel";
#print covars
foreach my $covar (sort string_numerically keys %$ref_sim_covars) {
print $out_fh " $$ref_sim_covars{$covar}";
}
print $out_fh "\n";
}
sub print_simpheno {
my ($ref_ipheno_order, $ref_dpheno_order, $ref_dis_ipheno, $ref_sim_dpheno_data, $ref_nlabel, $out_fh) = @_;
if ($$ref_nlabel == 1) { #print header
print $out_fh "FID IID";
for (my $p=0; $p<scalar(@{$ref_ipheno_order}); $p++) {
print $out_fh " $$ref_ipheno_order[$p]";
}
for (my $p=0; $p<scalar(@{$ref_dpheno_order}); $p++) {
print $out_fh " $$ref_dpheno_order[$p]";
}
print $out_fh "\n";
}
#print phenotypes
print $out_fh "$$ref_nlabel $$ref_nlabel";
#print intermediate traits first
for (my $p=0; $p<scalar(@{$ref_dis_ipheno}); $p++) {
print $out_fh " $$ref_dis_ipheno[$p]";
}
#do destination next
for (my $p=0; $p<scalar(@{$ref_sim_dpheno_data}); $p++) {
print $out_fh " $$ref_sim_dpheno_data[$p]";
}
print $out_fh "\n";
}
sub print_simverbpheno {
my ($ref_ipheno_order, $ref_dpheno_order, $ref_dis_ipheno, $ref_sim_dpheno_data, $ref_verbpheno, $ref_nlabel, $out_fh) = @_;
if ($$ref_nlabel == 1) { #print header
print $out_fh "FID IID";
for (my $p=0; $p<scalar(@{$ref_ipheno_order}); $p++) {
print $out_fh " $$ref_ipheno_order[$p]";
#print out the env and genetic contributions separately (order is env, genetic)
print $out_fh " vE_@{[$$ref_ipheno_order[$p]]} vG_@{[$$ref_ipheno_order[$p]]}";
}
for (my $p=0; $p<scalar(@{$ref_dpheno_order}); $p++) {
print $out_fh " $$ref_dpheno_order[$p]";
}
print $out_fh "\n";
}
#print phenotypes
print $out_fh "$$ref_nlabel $$ref_nlabel";
#print intermediate traits first
for (my $p=0; $p<scalar(@{$ref_dis_ipheno}); $p++) {
print $out_fh " $$ref_dis_ipheno[$p]";
print $out_fh " $$ref_verbpheno{$$ref_ipheno_order[$p]}[0] $$ref_verbpheno{$$ref_ipheno_order[$p]}[1]";
}
#do destination next
for (my $p=0; $p<scalar(@{$ref_sim_dpheno_data}); $p++) {
print $out_fh " $$ref_sim_dpheno_data[$p]";
}
print $out_fh "\n";
}
sub print_simscore {
my ($ref_ipheno_order, $ref_dpheno_order, $ref_scorestats, $asc_pheno_lab, $asc_pheno_val, $ref_nlabel, $out_fh) = @_;
#note that scores are based on the score data file provided (i.e., expected value), not what is drawn for a given simulation.
if ($$ref_nlabel == 1) { #print header
print $out_fh "FID IID $asc_pheno_lab";
for (my $p=0; $p<scalar(@{$ref_ipheno_order}); $p++) {
print $out_fh " SCORE_@{[$$ref_ipheno_order[$p]]}";
print $out_fh " EXPvG_@{[$$ref_ipheno_order[$p]]}"; #this is the expected addition to the trait due to genetics
}
for (my $p=0; $p<scalar(@{$ref_dpheno_order}); $p++) {
print $out_fh " SCORE_@{[$$ref_dpheno_order[$p]]}";
}
print $out_fh "\n";
}
#print scores
print $out_fh "$$ref_nlabel $$ref_nlabel $asc_pheno_val"; #print the ascertainment phenotype status for ease
#do intermediate traits first
for (my $p=0; $p<scalar(@{$ref_ipheno_order}); $p++) {
if ($$ref_scorestats{$$ref_ipheno_order[$p]}[1] == 0) {
print $out_fh " -9 -9";
} else {
#this is the score normalized for nloci;
printf $out_fh " %1.5f", $$ref_scorestats{$$ref_ipheno_order[$p]}[0]/$$ref_scorestats{$$ref_ipheno_order[$p]}[1];
printf $out_fh " %1.5f", $$ref_scorestats{$$ref_ipheno_order[$p]}[0];
}
}
#do destination next
for (my $p=0; $p<scalar(@{$ref_dpheno_order}); $p++) {
if ($$ref_scorestats{$$ref_dpheno_order[$p]}[1] == 0) {
print $out_fh " -9";
} else {
printf $out_fh " %1.5f", $$ref_scorestats{$$ref_dpheno_order[$p]}[0]/$$ref_scorestats{$$ref_dpheno_order[$p]}[1];
}
}
print $out_fh "\n";
}
sub print_simped {
my ($ref_genodata, $ref_sim_covars, $ref_this_pheno, $ref_nlabel, $out_fh) = @_;
my $snp;
print $out_fh "$$ref_nlabel $$ref_nlabel 0 0 $$ref_sim_covars{'SEX'} $$ref_this_pheno ";
foreach $snp (sort string_numerically keys %$ref_genodata) {
print $out_fh " $$ref_genodata{$snp}[0] $$ref_genodata{$snp}[1]";
}
print $out_fh "\n";
}
sub print_ldstats { #Reports the pairwise r2 (and D) values within each haplotype set.
my ($ref_haplist, $ref_afreqs, $out_fh) = @_;
my ($a1, $b1, $snp_a, $snp_b, $a1_frq, $b1_frq, $a1b1_hapt_frq);
my $D = $rsq = 0;
my @hapalleles = ();
my @snplist = ();
foreach my $snpstring (keys %$ref_haplist) {
@snplist = split '\|', $snpstring;
for (my $i=0; $i<scalar(@snplist); $i++) {
for (my $j=0; $j<$i; $j++) {
$snp_a = $snplist[$i];
$a1 = $$ref_afreqs{$snp_a}[0];
$a1_frq = $$ref_afreqs{$snp_a}[2];
$snp_b = $snplist[$j];
$b1 = $$ref_afreqs{$snp_b}[0];
$b1_frq = $$ref_afreqs{$snp_b}[2];
#now, calculate the a1b1 haplotype freq
$a1b1_hapt_frq = 0;
foreach my $this_hapt (keys %{$ref_haplist->{$snpstring}}) {
@hapalleles = split '', $this_hapt;
if ($hapalleles[$i] =~ m/$a1/ && $hapalleles[$j] =~ m/$b1/) {
$a1b1_hapt_frq += $$ref_haplist{$snpstring}{$this_hapt};
}
#print "$this_hapt $i $j $$ref_haplist{$snpstring}{$this_hapt} $a1b1_hapt_frq\n";
}
$D = sprintf("%1.6f",$a1b1_hapt_frq) - sprintf("%1.6f",($a1_frq*$b1_frq));
$rsq = sprintf("%1.6f",$D**2)/sprintf("%1.6f",($a1_frq * (1-$a1_frq) * $b1_frq * (1-$b1_frq)));
printf $out_fh "$snp_a $snp_b $a1$b1 %f %f\n", $rsq, $D;
}
}
}
}
sub print_ldfreqs { #prints the allele frequencies for haplotype data to file and stores freqs in hash table
my ($ref_haplist, $ref_afreqs, $out_fh) = @_;
my @snplist = ();
my @hapalleles = ();
#Reports the allele frequencies for each SNP, for each haplotype
foreach my $snpstring (keys %$ref_haplist) {
@snplist = split '\|', $snpstring;
foreach my $this_hapt (keys %{$ref_haplist->{$snpstring}}) {
@hapalleles = split '', $this_hapt;
for (my $i=0; $i<scalar(@hapalleles); $i++) {
if (!defined($$ref_afreqs{$snplist[$i]})) {
@{$$ref_afreqs{$snplist[$i]}} = ($hapalleles[$i], -9, $$ref_haplist{$snpstring}{$this_hapt}); #a1 a2 freq(a1)
} else {
if ($hapalleles[$i] !~ m/$$ref_afreqs{$snplist[$i]}[0]/) { #found a new allele
$$ref_afreqs{$snplist[$i]}[1] = $hapalleles[$i];
} else { #a1 allele found. add frequency
$$ref_afreqs{$snplist[$i]}[2] += $$ref_haplist{$snpstring}{$this_hapt};
}
}
}
} #end this haplotype
#print data for this haplotype to frequency file
foreach my $snp (@snplist) {
print $out_fh "$snp $$ref_afreqs{$snp}[0] $$ref_afreqs{$snp}[1] $$ref_afreqs{$snp}[2]\n";
}
}
}
sub check_file_exists {
my ($file, $out_fh, $log_fh) = @_;
my $myprint;
stat($file);
if ( !(-e _) ) {
my $myprint = "ERROR: Can't locate " . $file . ".\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
sub gen_Rseed {
return(int(rand(1000000000))); #random seed feed to R are 1 to a billion
}
sub normal { #This distribution checks out
my ($mu, $sigma) = @_;
if ($sigma < 0) {
print "Improper Variance.\n";
exit();
}
my ($p1, $p2, $p);
do {
$p1 = -1 + 2*rand();
$p2 = -1 + 2*rand();
$p = $p1 * $p1 + $p2 * $p2;
} while ( $p >= 1. );
return $mu + $sigma * $p1 * sqrt(-2 * log ($p) / $p );
}
sub get_phenofile {
my ($file, $ref_phenodata, $ref_covars_exist, $out_fh, $log_fh) = @_;
my ($readline, $myprint);
my @entry;
my $ntot_i = 0;
my $ntot_d = 0;
my $ntot_c = 0;
open PHENO, "<$file" or die "Can't open $file!\n";
$myprint = "Reading in phenotype info from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
while ($readline = <PHENO>) {
@entry = split '\s+', $readline;
if (scalar(@entry) != 4) {
$myprint = "ERROR: Different number of entries found in [ " . $file . " ]. Expected 4, but found " . scalar(@entry) . "\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
if (!defined($$ref_phenodata{$entry[0]}[0])) {
@{$$ref_phenodata{$entry[0]}} = ($entry[1], $entry[2], $entry[3]); #key based on name; store type, trait model and Kp
if ($entry[1] =~ m/i/i) {
$ntot_i++;
} elsif ($entry[1] =~ m/d/i) {
$ntot_d++;
if ($entry[2] !~ m/b/) {
$myprint = "ERROR: improper entry found for type of phenotype for $entry[0] [ $entry[2] ]. Requires 'b'.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
} elsif ($entry[1] =~ m/c/i) {
$ntot_c++;
$$ref_covars_exist = 1; #you found covariates, make sure these data are reported.
if ($entry[2] !~ m/b/) {
$myprint = "ERROR: improper entry found for type of phenotype for $entry[0] [ $entry[2] ]. Requires 'b' ['q' not implemented].\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
} else {
$myprint = "ERROR: improper entry found for type of phenotype for $entry[0] [ $entry[1] ]. Requires 'i', 'c', or 'd'.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
} else {
$myprint = "ERROR: $entry[0] found twice in phenotype file! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
close(PHENO);
$myprint = $ntot_i . " intermediate phenotypes read from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = $ntot_c . " covariates read from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = $ntot_d . " destination phenotypes read from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
#check if prev of sex is defined. If not, set to default
if (!defined($$ref_phenodata{'SEX'}[0])) {
@{$$ref_phenodata{'SEX'}} = ('c', 'b', SEX_PREV());
$myprint = "Prevalence of SEX not defined. Using default ratio of men/women of [ " . SEX_PREV() . " ]\n";
print_string($myprint, $out_fh, $log_fh);
}
}
sub get_iiphenofile {
my ($file, $ref_iiphenodata, $ref_phenolist, $outfix, $flag_specvar, $out_fh, $log_fh) = @_;
my ($readline, $myprint, $n, $exp_tally, $unspecfile);
my @entry;
my $ntot = 0;
#count the number of intermediate phenotypes.
foreach my $pheno (keys %$ref_phenolist) {
if ($$ref_phenolist{$pheno}[0] =~ m/i/i) { #this is an intermediate phenotype
$n++;
}
}
$exp_tally = ($n * ($n-1))/2;
#determine if variances are specified in the file or not
if ($flag_specvar == 0) {
$myprint = "Assuming intermediate traits all have Variance = @{[ DEFAULT_VAR() ]}\n";
print_string($myprint, $out_fh, $log_fh);
foreach my $i (keys %$ref_phenolist) {
$$ref_iiphenodata{$i}{$i} = DEFAULT_VAR();
}
} else {
$myprint = "Obtaining variances for intermediate traits from intermediate trait file.\n";
print_string($myprint, $out_fh, $log_fh);
$exp_tally += $n;
}
if ($exp_tally == 0) { #only one trait specified
$myprint = "One intermediate trait specified (default variance assumed), no phenotype relationships expected/read in from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
foreach my $i (keys %$ref_phenolist) {
$$ref_iiphenodata{$i}{$i} = DEFAULT_VAR();
}
} else { #more than one trait specified; expecting covariance terms (and possibly variance terms)
open PHENO, "<$file" or die "Can't open $file!\n";
#$myprint = "Reading in intermediate->intermediate phenotype relationships from [ " . $file . " ]\n";
#print_string($myprint, $out_fh, $log_fh);
while ($readline = <PHENO>) {
@entry = split '\s+', $readline;
if (!defined($$ref_phenolist{$entry[0]}[0])) {
$myprint = "ERROR: Found unknown phenotype [ $entry[0] ] in relationships file!\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
} elsif (!defined($$ref_phenolist{$entry[1]}[0])) {
$myprint = "ERROR: Found unknown phenotype [ $entry[1] ] in relationships file!\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
$$ref_iiphenodata{$entry[0]}{$entry[1]} = $entry[2];
$$ref_iiphenodata{$entry[1]}{$entry[0]} = $entry[2]; #symmetric
$ntot++;
}
$myprint = "Expecting $exp_tally intermediate phenotype relationship(s) from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = $ntot . " intermediate->intermediate phenotype relationships read from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
if ($exp_tally > $ntot) {
$unspecfile = $outfix . "_unspec.err";
$myprint = "Logging all unspecified relationships that were set to 0 (correlation) or @{[ DEFAULT_VAR() ]} (variance) to [ $unspecfile ]\n";
print_string($myprint, $out_fh, $log_fh);
open UNSPEC, ">$unspecfile" or die "Can't open $unspecfile!\n";
foreach my $i (keys %$ref_phenolist) {
foreach my $j (keys %$ref_phenolist) {
if ($$ref_phenolist{$i}[0] =~ m/i/i && $$ref_phenolist{$j}[0] =~ m/i/i) { #insist on these all being intermediates
if (!defined($$ref_iiphenodata{$i}{$j}) && !defined($$ref_iiphenodata{$j}{$i})) {
print UNSPEC "$i $j\n";
if ($i !~ m/$j/) {
$$ref_iiphenodata{$i}{$j} = 0;
$$ref_iiphenodata{$j}{$i} = 0;
} else {
$$ref_iiphenodata{$i}{$j} = DEFAULT_VAR();
}
} elsif (!defined($$ref_iiphenodata{$i}{$j}) && defined($$ref_iiphenodata{$j}{$i})) {
if ($i !~ m/$j/) {
$$ref_iiphenodata{$i}{$j} = 0;
} else {
$$ref_iiphenodata{$i}{$j} = DEFAULT_VAR();
}
} elsif (defined($$ref_iiphenodata{$i}{$j}) && !defined($$ref_iiphenodata{$j}{$i})) {
if ($i !~ m/$j/) {
$$ref_iiphenodata{$j}{$i} = 0;
} else {
$$ref_iiphenodata{$i}{$j} = DEFAULT_VAR();
}
}
}
}
}
close(UNSPEC);
} elsif ($exp_tally < $ntot) {
$myprint = "ERROR: More intermediate relationships than expected.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
} #END number of results in ii file > 1
return($ntot);
}
sub get_idphenofile {
my ($file, $ref_idphenodata, $ref_phenolist, $ref_asc_pheno, $out_fh, $log_fh) = @_;
my ($readline, $myprint, $gotit);
my @entry;
my $ntot = 0;
my %lookup_check;
open PHENO, "<$file" or die "Can't open $file!\n";
#$myprint = "Reading in intermediate->destination phenotype relationships from [ " . $file . " ]\n";
#print_string($myprint, $out_fh, $log_fh);
while ($readline = <PHENO>) {
@entry = split '\s+', $readline;
if (!defined($$ref_phenolist{$entry[0]}[0])) {
$myprint = "ERROR: Found unknown phenotype [ $entry[0] ] in relationships file!\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
} elsif (!defined($$ref_phenolist{$entry[1]}[0])) {
$myprint = "ERROR: Found unknown phenotype [ $entry[1] ] in relationships file!\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
@{$$ref_idphenodata{$entry[0]}{$entry[1]}} = ($entry[2], $entry[3]); #keyed on D, I; store beta and SE for relationship.
$lookup_check{$entry[1]} = 1; #TRUE if intermediate hooks up with a destination.
$ntot++;
}
$myprint = $ntot . " intermediate->destination phenotype relationships read from [ " . $file . " ]\n";
print_string($myprint, $out_fh, $log_fh);
#Check for specified effect of SEX. if does not exist, define as no effect
if (!defined($$ref_idphenodata{$$ref_asc_pheno}{'SEX'}[0])) {
@{$$ref_idphenodata{$entry[0]}{'SEX'}} = (SEX_EFFECT(), 0);
$myprint = "No effect of sex specified. Assuming Beta = [ " . SEX_EFFECT() . " ]\n";
print_string($myprint, $out_fh, $log_fh);
}
#check if all intermediate phenotypes relate to a destination phenotype. Warn if failure (but don't kill).
for my $i (keys %$ref_phenolist) {
if ($$ref_phenolist{$i}[0] =~ m/i/i) {
if (!defined($lookup_check{$i})) {
$gotit = 0;
} else {
$gotit = 1;
}
} else { #otherwise you are a destination trait, and thus fine.
$gotit = 1;
}
if ($gotit == 0) {
$myprint = "Warning: intermediate phenotype $i does not relate to a destination phenotype!\n";
print_string($myprint, $out_fh, $log_fh);
}
}
}
sub get_infofile {
my ($infofile, $ref_infodata, $ref_ldinfoflag, $out_fh, $log_fh) = @_;
my ($readline, $myprint);
my @entry;
my $ntot = 0;
my $ntot_ld = 0;
my $fail = 0;
open INFO, "<$infofile" or die "Can't open $infofile!\n";
#$myprint = "Reading in SNP base information from [ " . $infofile . " ]\n";
#print_string($myprint, $out_fh, $log_fh);
while ($readline = <INFO>) {
@entry = split '\s+', $readline;
if (scalar(@entry) != 4) {
$myprint = "ERROR: Different number of entries found in [ " . $infofile . " ]. Expected 4, but found " . scalar(@entry) . "\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
#keyed by SNP label, then risk, nrisk, freq
if (!defined($$ref_infodata{$entry[0]}[0])) {
if ($entry[3] !~ m/^[-0-9\.]+$/) {
$myprint = "ERROR: Improper allele frequency specified [ $entry[3] ]. Exiting!\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
if ($entry[3] == -9 && $$ref_ldinfoflag == 0) {
$myprint = "ERROR: Found LD-usage flag [ $entry[3] ] but no LD info file specified. Exiting!\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
if ($entry[3] != -9) {
if ($entry[3] > 1 || $entry[3] < 0) {
$myprint = "ERROR: Improper allele frequency specified [ $entry[3] ]. Exiting!\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
} else {
$ntot_ld++;
}
@{$$ref_infodata{$entry[0]}} = ($entry[1], $entry[2], $entry[3]); #allele frequency here can be -9 given LD specification
$ntot++;
} else {
$myprint = "ERROR: $entry[0] found twice in info file! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
close(INFO);
$myprint = $ntot . " SNPs read from [ " . $infofile . " ]\n";
print_string($myprint, $out_fh, $log_fh);
if ($$ref_ldinfoflag == 1) {
$myprint = "Of these, " . $ntot_ld . " SNPs are expected to have LD relationships.\n";
print_string($myprint, $out_fh, $log_fh);
}
return($ntot);
}
sub get_scorefile {
my ($scorefile, $ref_scoredata, $ref_infodata, $ref_afreqs, $ref_ldinfoflag, $out_fh, $log_fh) = @_;
my ($readline, $myprint);
my @entry;
my $ntot = 0;
open SCORE, "<$scorefile" or die "Can't open $scorefile!\n";
#$myprint = "Reading in SNP score info from [ " . $scorefile . " ]\n";
#print_string($myprint, $out_fh, $log_fh);
while ($readline = <SCORE>) {
@entry = split '\s+', $readline;
if ($entry[0] =~ m/\#/) {
#skip comments.
} else {
if (scalar(@entry) != 6) {
$myprint = "ERROR: Different number of entries found in [ " . $scorefile . " ]. Expected 6, but found " . scalar(@entry) . "\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
} elsif ($$ref_ldinfoflag == 0) { #check info data only
if (!defined($$ref_infodata{$entry[0]}[0]) ) {
$myprint = "ERROR: Entry found in [ " . $scorefile . " ] but not found in the loaded infosheet!\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
} elsif ($$ref_ldinfoflag == 1) { #check loaded info and ldinfo sheets
if (!defined($$ref_infodata{$entry[0]}[0]) && !defined($$ref_afreqs{$entry[0]}[0])) {
$myprint = "ERROR: Entry found in [ " . $scorefile . " ] but not found in the loaded infosheet or ld_infosheet!\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "Last line read: " . $readline . "\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
#keyed by SNP rs number and phenotype; then add_fx, add_fx_se, dom_fx, dom_fx_se
if (!defined($$ref_scoredata{$entry[0]}{$entry[1]}[0])) {
@{$$ref_scoredata{$entry[0]}{$entry[1]}} = ($entry[2], $entry[3], $entry[4], $entry[5]);
$ntot++;
} else {
$myprint = "ERROR: $entry[0] found twice for the same phenotype in score file! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
}
close(SCORE);
$myprint = $ntot . " SNP->Phenotype relationships read from [ " . $scorefile . " ]\n";
print_string($myprint, $out_fh, $log_fh);
return($ntot);
}
sub get_ldinfofile {
my ($ldinfofile, $ref_infodata, $ref_all_haplist, $out_fh, $log_fh) = @_;
my ($snpline, $readline, $myprint, $this_hapt, $snpstring, $t_freq);
my $nhaps = 0;
my @snplist = ();
my @hapalleles = ();
my @hapfreqlist = ();
my @nsnps_thishapt = ();
my %all_snplist;
my %happrobs;
my %allelefreqs;
open LDINFO, "<$ldinfofile" or die "Can't open $ldinfofile!\n";
$myprint = "Reading in SNP haplotype probability information from [ " . $ldinfofile . " ]\n";
print_string($myprint, $out_fh, $log_fh);
while ($snpline = <LDINFO>) {
#data in file is paired (2 lines for each haplotype)
#first line is SNP list, second line is haplotype freqs
$t_freq = 0;
$nhaps++;
chomp($snpline);
@snplist = split '\s+', $snpline;
#make the string of SNPs that follows along with the
for (my $i=0; $i<scalar(@snplist); $i++) {
if ($i==0) {
$snpstring = $snplist[$i];
} else {
$snpstring .= "|" . $snplist[$i];
}
}
if (scalar(@snplist) == 0) {
$myprint = "ERROR: No entries for SNPs found on this line! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
#check to make sure SNPs are represented in the info data file and not duplicated within the LDinfosheet.
foreach my $snp (@snplist) {
if (!defined($$ref_infodata{$snp})) {
$myprint = "ERROR: SNP [ $snp ] listed in ldinfo file was not found in infosheet! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
if (!defined($all_snplist{$snp})) {
$all_snplist{$snp} = 1;
} else {
$myprint = "ERROR: SNP [ $snp ] was already listed in the ldinfo file: You must place all SNPs in LD onto the same haplotype entry! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
#Now parse haplotype frequency entries
if ( !eof(LDINFO) ) { #check if you have reached the end of file
$readline = <LDINFO>;
} else {
$myprint = "ERROR: Expecting haplotype probs but reached end of file. Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
chomp($readline);
@hapfreqs = split '\s+', $readline;
if (scalar(@hapfreqs) == 0) {
$myprint = "ERROR: No haplotype probabilities for SNPs found! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
foreach my $hap (@hapfreqs) {
if ($hap !~ m/([A-Z]+):([0-9\.]+)$/) {
$myprint = "ERROR: [ $hap ] is improperly formatted. Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "LD-infosheet line read: $snpline\n";
print_string($myprint, $out_fh, $log_fh);
exit();
} else {
#check to make sure haplotype entry hasn't already been specified
if (!defined($$ref_all_haplist{$snpstring}{$1})) {
$this_hapt = $1;
$$ref_all_haplist{$snpstring}{$this_hapt} = $2;
#print "$this_hapt $$ref_all_haplist{$snpstring}{$this_hapt}\n";
@hapalleles = split '', $this_hapt;
} else {
$myprint = "ERROR: haplotype [ $1 ] was already listed in given entry: Each entry must be unique! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "LD-infosheet line read: $snpline\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
# check that haplotype size is correct.
if (scalar(@hapalleles) != scalar(@snplist)) {
$myprint = "ERROR: Expecting " . scalar(@snplist) . " alleles in haplotype but found " . scalar(@hapalleles) . ": [ $hap ]. Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "LD-infosheet line read: $snpline\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
# check that alleles found are listed in infosheet
for (my $i=0; $i<scalar(@snplist); $i++) {
if ($$ref_infodata{$snplist[$i]}[0] !~ m/$hapalleles[$i]/ && $$ref_infodata{$snplist[$i]}[1] !~ m/$hapalleles[$i]/) {
$myprint = "ERROR: [ " . $$ref_infodata{$snplist[$i]}[0] . "|" . $$ref_infodata{$snplist[$i]}[1] . " ] alleles not found in haplotype [ $hap ]. Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "LD-infosheet line read: $snpline\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
#build allele frequency info from this haplotype entry
for (my $i=0; $i<scalar(@snplist); $i++) {
if (!defined($allelefreqs{$snplist[$i]}{$hapalleles[$i]})) {
$allelefreqs{$snplist[$i]}{$hapalleles[$i]} = $$ref_all_haplist{$snpstring}{$this_hapt};
} else {
$allelefreqs{$snplist[$i]}{$hapalleles[$i]} += $$ref_all_haplist{$snpstring}{$this_hapt};
}
}
}
#foreach my $z (keys %$ref_all_haplist) {
# print "$z\n";
# foreach my $y (keys %{$ref_all_haplist->{$z}}) {
# print "$y\n";
# }
#}
#exit();
#print "$snpstring\n";
#verify the number of haplotype entries is correct (expected number is 2**n)
@nsnps_thishapt = split '\|', $snpstring;
if (scalar(keys %{$ref_all_haplist->{$snpstring}}) != (2**scalar(@nsnps_thishapt))) {
$myprint = "ERROR: incorrect number of haplotype frequencies found. Expected [ " . (2**scalar(@nsnps_thishapt)) . " ] but found " . scalar(keys %{$ref_all_haplist->{$snpstring}}) . ". Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = "LD-infosheet line read: $snpline\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
#check that the haplotype probs sum to one.
foreach my $hapt (keys %{$ref_all_haplist->{$snpstring}}) {
$t_freq += $$ref_all_haplist{$snpstring}{$hapt};
}
if ( abs($t_freq-1) > FREQ_SUM_TOL() ) {
$myprint = "ERROR: haplotype probabilities do not sum to one: [ $t_freq ]. Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
####Check that the allele frequencies all sum to one for each SNP.
#Don't need to do this actually since I check that the haplotype probs sum to one.
#foreach $snp (keys %allelefreqs) {
# $t_freq = 0;
# foreach $allele (keys %{$allelefreqs{$snp}}) {
# $t_freq += $allelefreqs{$snp}{$allele};
# }
# if ( abs($t_freq-1) > FREQ_SUM_TOL() ) {
# $myprint = "ERROR: allele frequencies for SNP [ $snp ] do not sum to one: [ $t_freq ]. Exiting.\n";
# print_string($myprint, $out_fh, $log_fh);
# exit();
# }
#}
} #END_READSNPfile
close(LDINFO);
#have all SNPs with LD relationships specified in the infosheet been found here? if not, exit
foreach my $snp (keys %$ref_infodata) {
if ($$ref_infodata{$snp}[2] == -9) {
if (!defined($all_snplist{$snp})) {
$myprint = "ERROR: [ $snp ] was listed in infosheet but not found in ldinfosheet! Exiting.\n";
print_string($myprint, $out_fh, $log_fh);
exit();
}
}
}
#Output summary information
$myprint = $nhaps . " haplotype inputs read from [ $ldinfofile ]\n";
print_string($myprint, $out_fh, $log_fh);
$myprint = scalar(keys %all_snplist) . " SNPs in total comprise these haplotype(s).\n";
print_string($myprint, $out_fh, $log_fh);
#relavent data structure output is %ref_all_haplist, keyed by: {snpstring}{hapt} = happrob;
return($nhaps);
}
sub get_ipheno_chunk{
my ($ipheno_chunk, $ref_ipheno_order, $ref_sim_ipheno_data, $out_fh, $log_fh) = @_;
my ($readline, $myprint);
my @entry;
open CHUNK, "<$ipheno_chunk" or die "Can't open $ipheno_chunk!\n";
#$myprint = "Reading in simulated intermediate trait values from [ " . $ipheno_chunk . " ]\n";
#print_string($myprint, $out_fh, $log_fh);
#get the header, reserve the phenotype order
$readline = <CHUNK>; #header;
chomp($readline);
@entry = split '\s+', $readline;
@{$ref_ipheno_order} = @entry;
while ($readline = <CHUNK>) {
chomp($readline);
@entry = split '\s+', $readline;
push @{$ref_sim_ipheno_data}, [ @entry ];
}
close(CHUNK);
}
sub mk_mapfile {
my ($ref_scoredata, $outfix, $out_fh, $log_fh) = @_;
my ($snp, $a, $b);
my $pos = 1000;
open MAP, ">@{[$outfix]}.map" or die "Can't open @{[$outfix]}.map!\n";
$myprint = "Outputting generic map file to [ " . $outfix . ".map ]\n";
print_string($myprint, $out_fh, $log_fh);
foreach $snp (sort string_numerically keys %$ref_scoredata) {
print MAP "1 $snp 0 $pos\n";
$pos += 1000;
}
close(MAP);
}
sub mk_basedata {
my ($ref_scoredata, $ref_basedata, $ref_infodata, $ref_phenolist, $rand_flag, $out_fh, $log_fh) = @_;
my $a_fx = 0;
my $d_fx = 0;
my %phenolist;
my %snplist;
foreach my $snp (keys %$ref_infodata) {
foreach my $pheno (keys %{$$ref_scoredata{$snp}}) {
if (!defined($$ref_scoredata{$snp}{$pheno}[0])) { #no score value for this SNP for this phenotype. set to no effect.
$a_fx = 0;
$d_fx = 0;
} else {
#draw an additive term if applicable.
if ($$ref_scoredata{$snp}{$pheno}[1] != -9) {
if ($rand_flag == 1) { #random draw from fx distribution
$a_fx = normal($$ref_scoredata{$snp}{$pheno}[0], $$ref_scoredata{$snp}{$pheno}[1]);
} else { #do not randomize
$a_fx = $$ref_scoredata{$snp}{$pheno}[0];
}
} else {
$a_fx = 0;
}
if ($$ref_scoredata{$snp}{$pheno}[3] != -9) {
if ($rand_flag == 1) {
$d_fx = normal($$ref_scoredata{$snp}{$pheno}[2], $$ref_scoredata{$snp}{$pheno}[3]);
} else {
$d_fx = $$ref_scoredata{$snp}{$pheno}[2];
}
} else {
$d_fx = 0;
}
}
@{$$ref_basedata{$snp}{$pheno}} = ($a_fx, $d_fx, $$ref_infodata{$snp}[2]);
}
#record the SNP.
$snplist{$snp} = 1;
}
#go through the list and zero out all SNPs and pheno
foreach $snp (keys %snplist) {
foreach $pheno (keys %$ref_phenolist) {
if (!defined($$ref_basedata{$snp}{$pheno}[0])) {
@{$$ref_basedata{$snp}{$pheno}} = (0, 0, $$ref_infodata{$snp}[2]); #retain "-9" flag for ld lookup in sim_genotypes later
}
}
}
}
sub mk_varcovfile {
my ($forRfile, $ref_vcovm, $ref_iiphenodata, $ref_phenolist, $ref_basedata, $ref_cov_adj_matrix, $out_fh, $log_fh) = @_;