-
Notifications
You must be signed in to change notification settings - Fork 5
/
forge.pl
executable file
·1851 lines (1591 loc) · 73.2 KB
/
forge.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
use strict;
use Getopt::Long;
use Pod::Usage;
use PDL;
use PDL::Matrix;
use PDL::GSL::CDF;
use PDL::Primitive;
use PDL::NiceSlice;
use PDL::Stats::Basic;
use PDL::Bad;
use Data::Dumper;
#use Carp qw( confess );
#$SIG{__DIE__} = \&confess;
#$SIG{__WARN__} = \&confess;
# Load local functions
use GWAS_IO;
use GWAS_STATS;
use CovMatrix;
# declare global variables. mainly command line options
our ( $help, $man, $out, $snpmap, $bfile, $assoc, $gene_list,
@genes, $all_genes, $analysis_chr, $report, $spearman,
$affy_to_rsid, @weights_file, $w_header, $v, $lambda,
$print_cor, $pearson_genotypes,$distance,$distance_three_prime,$distance_five_prime, $sample_score,
$ped, $map, $ox_gprobs,$sample_score_self, $w_maf,
$ss_mean, $gc_correction,$g_prob_threshold,
$bgl_gprobs, $flush, $include_gene_type, $exclude_gene_type, $gmt,
$gmt_min_size,$gmt_max_size, $use_ld_as_corr,$mnd_sim_target,
$mnd_sim_max, $mnd_sim_wise_correction_methods, $mnd,$mperm_dump,$asymp,
$low_mem,$g_pareto_dist, $gates_corr, $test_name
);
GetOptions(
'help|h' => \$help,
'man' => \$man,
'ped=s' => \$ped,
'map=s' => \$map,
'bfile=s' => \$bfile,
'out|o=s' => \$out, #name of the output file
'assoc|a=s' => \$assoc,
'gene_list|g=s' => \$gene_list,
'genes=s' => \@genes,
'all_genes' => \$all_genes,
'chr=s' => \$analysis_chr,
'snpmap|m=s@' => \$snpmap,
'report=i' => \$report,
'correlation|cor=s' => \$spearman,
'affy_to_rsid=s' => \$affy_to_rsid,
'verbose|v' => \$v,
'lambda=f' => \$lambda,
'gc_correction' => \$gc_correction,
'print_cor' => \$print_cor,
'pearson_genotypes' => \$pearson_genotypes,
'use_ld' => \$use_ld_as_corr,
'distance|d=i' => \$distance,
'distance_three_prime|three=i' => \$distance_three_prime,
'distance_five_prime|five=i' => \$distance_five_prime,
'sample_score' => \$sample_score,
'weights|w=s' => \@weights_file,
'w_header' => \$w_header,
'ox_gprobs=s' => \$ox_gprobs,
'bgl_gprobs=s' => \$bgl_gprobs,
'g_prob_threshold=f' => \$g_prob_threshold,
'weight_by_maf|w_maf' => \$w_maf,
'ss_mean' => \$ss_mean,
'flush=i' => \$flush,
'gmt=s@' => \$gmt,
'gmt_min_size=i' => \$gmt_min_size,
'gmt_max_size=i' => \$gmt_max_size,
'gene_type|type=s@' => \$include_gene_type,
'exclude_gene_type|exclude_type=s@' => \$exclude_gene_type,
'mnd' => \$mnd,
'mnd_target=i' => \$mnd_sim_target,
'mnd_max=i' => \$mnd_sim_max,
'mnd_methods=s' => \$mnd_sim_wise_correction_methods,
'stats_dump=s' => \$mperm_dump,
'asymp|asymptotic' => \$asymp,
'low_mem' => \$low_mem,
'g_pareto_dist|gpd' => \$g_pareto_dist,
'gates_corr' => \$gates_corr,
'test_name=s' => \$test_name,
) or pod2usage(0);
pod2usage(0) if (defined $help);
pod2usage(-exitstatus => 2, -verbose => 1) if (defined $man);
pod2usage(0) if (not defined $assoc);
my $LOG = new IO::File;
$LOG->open(">$out.log") or print_OUT("I can not open [ $out.log ] to write to",$LOG) and exit(1);
print_OUT("Check http://github.com/inti/FORGE/wiki for updates",$LOG);
print_OUT("LOG file will be written to [ $out.log ]",$LOG);
# define parameters for mnd simulation
defined $mnd_sim_target or $mnd_sim_target = 10;
defined $mnd_sim_max or $mnd_sim_max = 1_000_000;
if (defined $mnd_sim_wise_correction_methods){
$mnd_sim_wise_correction_methods = [ split(/\,/,$mnd_sim_wise_correction_methods) ];
} else { $mnd_sim_wise_correction_methods = [0,1,2,3]; }
if (not defined $asymp) {
$mnd = 1;
} else {
print_OUT("Will calculate gene p-value using asymptotic methods",$LOG);
}
if (defined $mnd){
use PDL::LinearAlgebra qw (mchol);
use Pareto_Distr_Fit qw (Pgpd);
print_OUT("Will run multivariate normal distribution simulations to estimate significance",$LOG);
print_OUT(" '-> max number [ $mnd_sim_max ] or until statistic is seen [ $mnd_sim_target ] times",$LOG);
my @m = ('sidak','fisher','z_fix','z_random');
@m = @m[@$mnd_sim_wise_correction_methods];
print_OUT(" '-> for each gene best p-value will be selected among the methods [ @m ] ",$LOG);
}
# defines min and max size for gene-set analyses
defined $gmt_min_size or $gmt_min_size = 2;
defined $gmt_max_size or $gmt_max_size = 999_999_999;
# flush output every $flush number of genes are ready
defined $flush or $flush = 1000;
# define distance threshold,
if (defined $distance) {
$distance_three_prime = $distance;
$distance_five_prime = $distance;
} elsif ( (defined $distance_three_prime) or (defined $distance_five_prime)){
print_OUT("Max SNP-to-gene distance allowed for 3-prime [ $distance_three_prime ] and 5-prime [ $distance_five_prime ] kb",$LOG);
} else {
$distance = 20;
$distance_three_prime = $distance;
$distance_five_prime = $distance;
print_OUT("Max SNP-to-gene distance allowed [ $distance ] kb",$LOG);
}
# $report is use to specify how often report the advance when reading input files
defined $report or $report = 50_000;
# tell if analysis is restricted to a specific chromosome
defined $analysis_chr and print_OUT("Restricting analysis to chromosome [ $analysis_chr ]",$LOG);
# defined lambda value for Genomic Control correction
defined $lambda or $lambda = 1;
# defined threshold value for genotype probabilities
defined $g_prob_threshold or $g_prob_threshold = 1.0;
# tell if user wants to print the *.correlation file
defined $print_cor and print_OUT("Defined -print_cor: I will print the *.correlation file (it is bulky)",$LOG);
#set output file if not set already.
defined $out or $out = "gene_based_fisher_v041.OUT";
# tell if user wants to correct p-values by genomic control
if ($lambda != 1){ print_OUT("SNP p-value will be corrected with lambda = [ $lambda ]",$LOG);}
if (not defined $test_name){
$test_name = 'ADD';
}
$test_name = uc($test_name);
print_OUT("Will analyse SNPs association restuls of type [ $test_name].",$LOG);
# define option to read genotype probability files
my $geno_probs = undef;
my $geno_probs_format = undef;
# find out which format the file is with the command line options
if (defined $ox_gprobs) {
$geno_probs = $ox_gprobs ;
$geno_probs_format = 'OXFORD';
print_OUT("Genotype probabilities in OXFORD format will be read from [ $geno_probs ]",$LOG);
} elsif (defined $bgl_gprobs) {
$geno_probs = $bgl_gprobs ;
$geno_probs_format = 'BEAGLE';
print_OUT("Genotype probabilities in BEAGLE format will be read from [ $geno_probs ]",$LOG);
}
# generate an index of the genotype probability files
# this speads up the IO
my ($gprobs, $gprobs_index);
if (defined $geno_probs){
$gprobs = IO::File->new();
$gprobs_index = IO::File->new();
my $index_name = "$geno_probs.idx";
$gprobs->open("<$geno_probs") or print_OUT("I can not open genotype probability file [ $geno_probs ]",$LOG) and exit(1);
if (not -e "$geno_probs.idx") {
print_OUT(" '-> Making index for genotype probabilities in [ $index_name ] file",$LOG);
$gprobs_index->open("+>$index_name") or print_OUT("Can't open $index_name for read/write: $!\n",$LOG);
build_index(*$gprobs, *$gprobs_index);
} else {
print_OUT(" '-> Found [ $geno_probs.idx ] file for genotype probabilities",$LOG);
$gprobs_index->open("<$geno_probs.idx") or print_OUT("Can't open $index_name for read/write: $!\n",$LOG) and exit(1);
binmode($gprobs_index);
}
}
if (defined $low_mem){
eval { require PDL::Compression; };
if ($@ ne ""){
print_OUT("You specified -low_mem option. This required the PDL::Compression and seems you do not have it. Check you PDL package, update may be needed.\n",$LOG);
exit(1);
}
}
# print header for output file
my $OUT = new IO::File;
$OUT->open(">$out") or print_OUT("I can not open [ $out ] to write to",$LOG) and exit(1);
# header for MND sampling analysis
if (not defined $mnd){
print $OUT "Ensembl_ID\tHugo_id\tgene_type\tchromosome\tstart\tend";
print $OUT "\tmin_p\tmin_p_SIDAK\tFISHER\tFISHER_chi-square\tFISHER_df";
print $OUT "\tB_fix\tVar_fix\tB_P_fix\tB_random\tVar_random\tB_P_random";
print $OUT "\tI-squared\tQ\tQ_p-value\ttau_squared";
print $OUT "\tn_effect_Galwey\tn_effect_Gao\tn_snps\n";
} else { # header for asymptotic analysis
print $OUT "Ensembl_ID\tHugo_id\tgene_type\tchromosome\tstart\tend";
print $OUT "\tmin_p\tSIM_SIDAK\tSIM_FISHER\tSIM_Z_FIX\tSIM_Z_RANDOM";
print $OUT "\tI-squared\tQ\tQ_p-value\ttau_squared";
print $OUT "\tN_SIM";
print $OUT "\tSEEN_SIDAK\tSEEN_FISHER\tSEEN_Z_FIX\tSEEN_RANDOM";
if (defined $g_pareto_dist){
print $OUT "\tGPD_SIDAK\tGPD_SIDAK_LOW\tGPD_SIDAK_UP";
print $OUT "\tGPD_FISHER\tGPD_FISHER_LOW\tGPD_FISHER_UP";
print $OUT "\tGPD_Z_FIX\tGPD_Z_FIX_LOW\tGPD_Z_FIX_UP";
print $OUT "\tGPD_Z_RANDOM\tGPD_Z_RANDOM_LOW\tGPD_Z_RANDOM_UP";
}
print $OUT "\tn_effect_Galwey\tn_effect_Gao\tn_snps\n";
}
# i will read the gene_list and i will load data for just this genes to speed up.
if ( not defined $all_genes and not defined @genes and not defined $gene_list){
$all_genes = 1;
print_OUT("Note: You did not provide an option for the set of genes to be analyzed. I will analyze all genes covered by the SNP association file. Check documentation for options -genes and -gene_list otherwise",$LOG);
}
if ( not defined $all_genes ) { # in case user want to analyze all genes
if ( not defined @genes ) { # in case user gave a list of genes in the command line
print_OUT("Reading Gene List from [ $gene_list ]",$LOG);
# read file with gene list and store gene names.
open( GL, $gene_list ) or print_OUT("I can not open [ $gene_list ]",$LOG) and exit(1);
@genes = <GL>;
chomp(@genes);
close(GL);
} else {
print_OUT("Read Gene List command line [ @genes ]",$LOG);
}
} else {
print_OUT("Going to analyze all genes on [ @$snpmap ] file.",$LOG);
}
# Now lets going to read the affy id to rsid mapping. This is used to keep all ids in the
# same nomenclature
my %affy_id = ();
if ( defined $affy_to_rsid ) { # if conversion file is defined
print_OUT("Reading AFFY to rsID mapping from [ $affy_to_rsid ]");
open( AFFY, $affy_to_rsid ) or print_OUT("I can not open [ $affy_to_rsid ]",$LOG) and exit(1);
while (my $affy = <AFFY>){
chomp($affy);
my @b = split(/\t+/,$affy);
$affy_id{$b[0]} = $b[1];
}
close(AFFY);
}
# Read file with genetic association results.
print_OUT("Reading association file: [ $assoc ]",$LOG);
# create hash to store SNP information
my $assoc_chis = [] if (defined $gc_correction);
my %assoc_data = ();
open( ASSOC, $assoc ) or print_OUT("I can not open [ $assoc ]",$LOG) and exit(1);
my $line = 0;
my %header = ();
while ( my $a = <ASSOC> ) {
$a =~ s/^\s+//;
$a =~ s/^\t+//;
$a =~ s/\s+/\t/g;
# here I get the header line of the file. With the name of the columns I can use the cols
# SNP and P to extract the information.
my @data = split( /[\s+\t+]/, $a );
if ( $line == 0 ){
%header = %{get_header(\@data)};
$line++;
next;
}
# In case there is not cols with SNP and P names.
exists $header{"SNP"} or print_OUT("Not p-value columns available, these are the headers i found [ " . (keys %header) . " ]",$LOG) and exit(1);
exists $header{"P"} or print_OUT("Not p-value columns available, these are the headers i found [ " . (keys %header) . " ]",$LOG) and exit(1);
# if there is a cols specifying the association test done. Only use result from ADD tests, this is only for compatibility with PLINK
if ( exists $header{"TEST"}){
next if ( $data[$header{"TEST"}] ne $test_name);
}
#if (defined $v ) { print $data[$header{"SNP"}]," ", $data[$header{"P"}],"\n"; }
# if there is an affy id convert it to rsid.
if ( defined $affy_to_rsid ) {
if ($data[$header{"SNP"}] !~ m/^rs/){
if (exists $affy_id{$data[$header{"SNP"}]}){ $data[$header{"SNP"}] = $affy_id{$data[$header{"SNP"}]};}
}
}
# skip if no P-value or p-value equal NA
next if ( $data[$header{"P"}] eq "NA");
next if ( $data[$header{"P"}] eq "");
#generate a pseudo-hash for each snp with the association info
my $A2 = "NA";
my $A1 = "NA";
my $OR = 1;
my $SE = undef;
my $BETA = undef;
my $R2 = undef;
my $STAT = undef;
if (exists $header{"A2"}){ $A2 = $data[$header{"A2"}];}
if (exists $header{"A1"}){ $A1 = $data[$header{"A1"}];}
if (exists $header{"OR"}){
$OR = $data[$header{"OR"}];
if ((exists $header{"L95"}) and (exists $header{"U95"})){
$SE = $header{"U95"} - $header{"L95"};
}
}
if (exists $header{"BETA"}){ $BETA = $data[$header{"BETA"}]; }
if (exists $header{"R2"}){ $R2 = $data[$header{"R2"}]; }
if (exists $header{"STAT"}){ $STAT = $data[$header{"STAT"}]; }
if ((not defined $SE) and (exists $header{"SE"})){ $SE = $data[$header{"SE"}]; }
my $effect = undef;
if (exists $header{"OR"}){
$effect = "or";
} elsif (exists $header{"BETA"}){
$effect = "beta";
} elsif (exists $header{"STAT"}){
$effect = "stat";
}
# do some checking
if (defined $sample_score){
# if there are not direction of effect defined quite analysis and spit and error.
if (not defined $effect){
print_OUT("ERROR: No effect size measure or direction of effect provided. I can not perform Sample Score analysis",$LOG);
print_OUT("ERROR: Please provide an odd-ratio, beta or regression coefficient value under the header OR, BETA or STAT, respectively",$LOG);
exit(1);
}
}
$assoc_data{ $data[$header{"SNP"}] } = {
'pvalue' => 1 ,
'id' => $data[$header{"SNP"}],
'a1' => $A1,
'a2' => $A2,
'or' => $OR,
'se' => $SE,
'beta' => $BETA,
'stat' => $STAT,
'r2' => $R2,
'effect_size_measure' => $effect,
'line' => $line++-1,
};
# correct for genomic control if a lambda > 1 was specified.
if ($lambda == 1) {
push @{ $assoc_chis }, $data[$header{"P"}];
$assoc_data{ $data[$header{"SNP"}] }->{'pvalue'} = $data[$header{"P"}];
} elsif ($lambda > 1) {# transform the p-value on a chi-square, correct it by the inflation factor and transform it again on a p-value
$assoc_data{ $data[$header{"SNP"}] }->{ 'pvalue' }= 1 - gsl_cdf_chisq_P( gsl_cdf_chisq_Pinv( $data[$header{"P"}], 1 )/$lambda, 1 );
} else {
print_OUT("\nPlease check the lambda value is correct\n",$LOG);
exit(1);
}
}
close(ASSOC);
if (scalar keys %assoc_data == 0){
print_OUT("\nNo SNPs with genetic association to used in the analysis\n",$LOG);
exit(1);
}
print_OUT("[ " . scalar (keys %assoc_data) . " ] SNPs with association data",$LOG);
# Genomic Control adjustment
if (defined $gc_correction){
print_OUT("Calculating lambda for genomic control correction",$LOG);
my $gc_lambda = get_lambda_genomic_control($assoc_chis);
print_OUT(" '-> lambda (median) of [ $gc_lambda ]",$LOG);
if ($gc_lambda > 1){
print_OUT(" '-> Applying GC correction",$LOG);
my $assoc_chis = [];
foreach my $snp (keys %assoc_data) {
if ( $assoc_data{ $snp }->{ 'pvalue' } == 1){
push @{ $assoc_chis }, $assoc_data{ $snp }->{ 'pvalue' };
next;
}
my $snp_chi = gsl_cdf_chisq_Pinv ( 1 - $assoc_data{ $snp }->{ 'pvalue' }, 1 );
$snp_chi /= $gc_lambda;
$assoc_data{ $snp }->{ 'pvalue' }= 1 - gsl_cdf_chisq_P( $snp_chi, 1 );
push @{ $assoc_chis }, $assoc_data{ $snp }->{ 'pvalue' };
}
$gc_lambda = get_lambda_genomic_control($assoc_chis);
print_OUT(" '-> After correction the lambda is [ $gc_lambda ]",$LOG);
} else {
print_OUT(" '-> GC correction not applied because lambda is less than 1",$LOG);
}
}
#read snp-to-gene mapping and store in a hash with key equal gene name and value
# an array with the snps in the gene.
my @bim = ();
my @fam = ();
my $ped_map_genotypes;
if (defined $bfile) {
# read the bim file with snp information and fam file with sample information
@bim = @{ read_bim("$bfile.bim",$affy_to_rsid,\%affy_id) };
@fam = @{ read_fam("$bfile.fam") };
print_OUT("[ " . scalar @bim . " ] SNPs and [ " . scalar @fam . " ] samples in genotype file",$LOG);
} elsif (defined $ped and defined $map){
my ($fam_ref,$bim_ref);
($fam_ref,$ped_map_genotypes,$bim_ref) = read_map_and_ped($ped,$map,$affy_to_rsid,\%affy_id);
@fam = @$fam_ref;
@bim = @$bim_ref;
print_OUT("[ " . scalar @bim . " ] SNPs and [ " . scalar @fam . " ] samples in genotype file",$LOG);
} elsif (defined $geno_probs){
print_OUT("Getting list of genotyped SNPs from [ $geno_probs ]",$LOG);
@bim = @{ get_snp_list_from_ox_format($gprobs, $gprobs_index) } if ($geno_probs_format eq 'OXFORD');
@bim = @{ get_snp_list_from_bgl_format($gprobs, $gprobs_index) } if ($geno_probs_format eq 'BEAGLE');
print_OUT("[ " . scalar @bim . " ] SNPs in genotype file",$LOG);
}
my %bim_ids = ();
my $index = 0;
map {
$bim_ids{$_->{snp_id}} = $index;
$index++;
} @bim;
print_OUT("Loading SNP-2-Gene mapping");
for (my $i = 0; $i < scalar @$snpmap; $i++){
if ($snpmap->[$i] =~ m/\#\d+\-\d+\#/) {
print_OUT(" '-> Found [ # ] key on [ $snpmap->[$i] ]. I will generate file names for chromosome interval.");
my ($s,$e) = ($snpmap->[$i] =~ m/\#(\d+)\-(\d+)\#/);
push @{$snpmap}, @{ make_file_name_array_interval($snpmap->[$i],$s,$e) };
splice(@$snpmap,$i,1);
}
if ($snpmap->[$i] =~ m/\#/) {
print_OUT(" '-> Found [ # ] key on [ $snpmap->[$i] ]. I will generate file names for 26 chromosomes.");
push @{$snpmap}, @{ make_file_name_array($snpmap->[$i]) };
splice(@$snpmap,$i,1);
}
}
my %gene = ();
my $gene_counter = 0;
my %snp_to_gene = ();
my %ids_map = ();
foreach my $snp_gene_mapping_file (@$snpmap){
if (not -e $snp_gene_mapping_file){
print_OUT(" '-> File [ $snp_gene_mapping_file ] does not exist, moving on to next file",$LOG);
next;
}
open( MAP, $snp_gene_mapping_file ) or print_OUT("Can not open [ $snp_gene_mapping_file ] file",$LOG) and exit(1);
print_OUT(" '-> Reading [ $snp_gene_mapping_file ]",$LOG);
while ( my $read = <MAP> ) {
chomp($read);
# the line is separate in gene info and snps. the section are separated by a tab.
my ($chr,$start,$end,$gene_strand,$ensembl,$hugo,$gene_status,$gene_type,$description,@m) = split(/\t+/,$read);
next if (scalar @m == 0);
# print "$chr,$start,$end,$gene_strand,$ensembl,$hugo,$gene_status,$gene_type,";
# getc;
#check if gene was in the list of genes i want to analyze
unless ( defined $all_genes ) {
next unless ( ( grep $_ eq $hugo, @genes ) or ( grep $_ eq $ensembl, @genes ) );
}
if (defined $analysis_chr){
next if ($analysis_chr ne $chr);
}
my @first_snp_n_fields = split(/\:/,$m[0]);
if (4 != scalar @first_snp_n_fields){ $description .= splice(@m,0,1); }
# get all mapped snps within the distance threshold,
my @mapped_snps = ();
foreach my $s (@m) {
my ($id,$pos,$allele,$strand) = split(/\:/,$s);
next if (not defined $id);
if (( $pos >= $start) and ($pos <= $end)){
push @mapped_snps, $id;
} else {
# $distance_three_prime $distance_five_prime
if ($gene_strand < 0){
# gene is on reverse strand: <----- start <- end, 3' <- 5'
if ( ( abs ($pos - $start) <= $distance_three_prime*1_000 ) or ( abs ($pos - $end) <= $distance_five_prime*1_000 )) { push @mapped_snps, $id; }
} elsif ($gene_strand > 0) {
# gene is on reverse strand: -----> start -> end, 5' -> 3'
if ( ( abs ($pos - $start) <= $distance_five_prime*1_000 ) or ( abs ($pos - $end) <= $distance_three_prime*1_000 )) { push @mapped_snps, $id; }
}
}
}
next if (scalar @mapped_snps == 0);
# create a pseudo-hash with the gene info
$gene{$ensembl} = {
'hugo' => $hugo,
'ensembl' => $ensembl,
'chr' => $chr,
'start' => $start,
'end' => $end,
'gene_type' => $gene_type,
'snps' => [],
'minp' => -9,
'genotypes' => null,
'geno_mat_rows' => [],
'cor' => null,
'weights' => null,
'pvalues' => [],
'effect_size' => undef,
'effect_size_se' => undef,
'gene_status' => $gene_status,
'desc' => $description,
'counter' => $gene_counter,
};
# go over mapped snps and change convert affy ids to rsid.
# and make a non-redundant set.
my %nr_snps = ();
foreach my $s (@mapped_snps) {
if ( defined $affy_to_rsid ) {
if ($s !~ m/^rs/){
if (exists $affy_id{$s}){ $s = $affy_id{$s};}
}
}
# exclude snps not in the association file nor in the bim file
next unless ( exists $assoc_data{$s} );
if (not defined $mperm_dump){
next unless ( exists $bim_ids{$s});
}
$nr_snps{$s} = "";
}
@mapped_snps = keys %nr_snps;
# go over the snps mapped to the gene and check if they are in the map
# and association files. If so, store the min p-value for the gene.
# if any of the snps is in the files the remove the gene from the analysis.
foreach my $s (@mapped_snps) {
if (defined $v ){ print_OUT("Mapping [ $s ] to [ $ensembl ]",$LOG);}
next if ( grep $_ eq $s, @{ $gene{$ensembl}->{snps} } );
push @{ $snp_to_gene{$s} }, $ensembl;
push @{ $gene{$ensembl}->{snps} }, $s;
if ( $gene{$ensembl}->{minp} == -9) {
$gene{$ensembl}->{minp} = $assoc_data{$s}->{pvalue};
} elsif ( $assoc_data{$s}->{pvalue} < $gene{$ensembl}->{minp} ) {
$gene{$ensembl}->{minp} = $assoc_data{$s}->{pvalue};
}
}
# remove gene if none of its snps is in the analysis.
if ( scalar @{ $gene{$ensembl}->{snps} } == 0 ) {
delete( $gene{$ensembl} );
} else {
if (defined $v ){ print_OUT("Gene $ensembl $hugo included in the analysis with [ " . scalar @{ $gene{$ensembl}->{snps} } . " ] mapped SNPs",$LOG); }
$ids_map{$hugo} = $ensembl;
}
}
close(MAP);
}
print_OUT(" '->[ " . scalar (keys %gene) . " ] Genes read from SNP-2-Gene Mapping files",$LOG);
print_OUT(" '->[ " . scalar (keys %snp_to_gene) . " ] SNPs mapped to Genes and with association results will be analyzed",$LOG);
# complain if there is genes ledt for analysis
if (scalar keys %gene == 0){
print_OUT("No genes mapped",$LOG);
exit(1);
}
# read gene-set definition file
if (defined $gmt){
my $total_gene_sets=0;
print_OUT("Reading gene-set definitions",$LOG);
foreach my $gene_set_file (@$gmt){
print_OUT(" '-> Reading [ $gene_set_file ]",$LOG);
open (GMT,$gene_set_file) or print_OUT("I can not open [ $gene_set_file ] to read from.",$LOG) and die $!;
while (my $line = <GMT>){
chomp($line);
my ( $p_name, $p_desc, @p_genes ) = split( /\t+/, $line );
my @gene_with_snp = ();
# loop over the genes and check if the ids match with any with SNPs
foreach my $gn (@p_genes) {
if ( $gn =~ m/\// ) {
$gn =~ s/\s+//g;
my @genes = split( /\/{1,}/, $gn );
map {
if ( exists $ids_map{$_} ){
push @gene_with_snp, $ids_map{$_};
} elsif ( exists $gene{$_} ){
push @gene_with_snp, $_;
} else {
next;
}
} @genes;
} else {
if ( exists $ids_map{$gn} ){
push @gene_with_snp, $ids_map{$gn};
} elsif ( exists $gene{$gn} ){
push @gene_with_snp, $gn;
} else {
next;
}
}
}
my %tmp = ();
map { $tmp{$_} = ""; } @gene_with_snp;
@gene_with_snp = keys %tmp;
# skip gene-set if does not complain with size constrains
next if (scalar @gene_with_snp < $gmt_min_size);
next if (scalar @gene_with_snp > $gmt_max_size);
%tmp = ();
my ($chrs,$starts,$ends) = "";
foreach my $g (@gene_with_snp) {
$chrs .= "$gene{$g}->{chr},";
$starts .= "$gene{$g}->{start},";
$ends .= "$gene{$g}->{end},";
foreach my $s (@{ $gene{$g}->{snps} }){
$tmp{$s} = "";
}
}
my @p_snps = keys %tmp;
next if (scalar @p_snps < 2);
map { push @{ $snp_to_gene{ $_ } }, $p_name; } @p_snps;
$gene{$p_name} = {
'chr' => $chrs,
'start' => $starts,
'end' => $ends,
'gene_type' => 'gene_set',
'snps' => [@p_snps],
'minp' => -9,
'genotypes' => null,
'geno_mat_rows' => [],
'cor' => null,
'weights' => null,
'pvalues' => [],
'effect_size' => undef,
'effect_size_se' => undef,
'gene_status' => 'KNOWN',
'ensembl' => $p_name,
'hugo' => $p_desc,
'desc' => $p_desc,
'name' => $p_name,
'genes' => [@gene_with_snp],
};
$total_gene_sets++;
}
}
print_OUT(" '-> Just read [ $total_gene_sets ] gene-sets with mapped genes with size [ $gmt_min_size ] and [ $gmt_max_size ]",$LOG);
}
# exclude genes if gene type were specified
if (defined $exclude_gene_type or defined $include_gene_type){
print_OUT("Going to filter genes based on user defined options",$LOG);
if (defined $exclude_gene_type){
print_OUT(" '-> Will exclude gene-types [ @$exclude_gene_type ]",$LOG);
}
if (defined $include_gene_type){
print_OUT(" '-> Will include gene-types [ @$include_gene_type ]",$LOG);
}
foreach my $gn (keys %gene){
# apply filter by gene type
if (defined $exclude_gene_type){
delete ($gene{$gn}) if ( grep $_ eq $gene{$gn}->{gene_type} , @$exclude_gene_type );
}
if (defined $include_gene_type){
if (exists $gene{$gn}) {
delete ($gene{$gn}) if (not grep $_ eq $gene{$gn}->{gene_type}, @$include_gene_type );
}
}
}
}
print_OUT("Will analyse [ " . scalar (keys %gene) . " ] genes",$LOG);
# start a hash to store the SNP-to-SNP correlation values
my %correlation = ();
# if provided get the SNP-to-SNP correlation values from a tab separated file with 3 cols:snp1 snp2 correlatio_value
if (defined $spearman){
print_OUT("Reading SNP correlation from [ $spearman ]",$LOG);
open( SPRMN, $spearman ) or print_OUT("I cannot open [ $spearman ]",$LOG) and exit(1);
while (my $ln = <SPRMN>){
chomp($ln);
my @a = split(/\s+/,$ln);
# take the square of the correlation if they are r2 and user wants to use r values
$correlation{$a[0]}{$a[1]} = $a[2];
$correlation{$a[1]}{$a[0]} = $a[2];
# set self correlation to 1
$correlation{$a[0]}{$a[0]} = 1;
$correlation{$a[1]}{$a[1]} = 1;
}
}
# start output file and print its header
print_OUT("Output file will be written to [ $out ]",$LOG);
# create a variable that will store a ref to a hash with the weights
my $weights = {};
if (defined @weights_file){
print_OUT("Starting to read SNP weigths",$LOG);
# create hash refs to store the name of the weight categories
my $w_classes = {};
my $w_counter = 0; # category counter, in case the file has not a header.
# loop over the files and read the weights
foreach my $w_f (@weights_file){
($weights,$w_counter,$w_classes) = read_weight_file($w_f,$w_counter,$w_classes,$weights, $w_header,\%snp_to_gene);
}
print_OUT(" '-> [ " . scalar (keys %{$weights}) . " ] weights read",$LOG);
# make a single weight vector for each snp
foreach my $snp (keys %{$weights}){
# get the snp weights sorted by category name
my @tmp_all_w = map { $weights->{$snp}{$_}; } sort {$a cmp $b} keys %{ $w_classes };
# make piddle
$weights->{$snp} = [@tmp_all_w];
}
}
#start count to report advance
my $count = 0;
print_OUT("Starting to Calculate gene p-values",$LOG);
# if there are more than 100 genes change the $report variable in order to report every ~ 10 % of genes.
unless (scalar keys %gene < 100){
$report = int((scalar keys %gene)/100 + 0.5)*10;
}
my $N_bytes_to_encode_snp = (scalar @fam)/4; # four genotypes per byte
my $bed = new IO::File;
if (defined $bfile) {
print_OUT("Reading genotypes from [ $bfile.bed ]",$LOG);
# open genotype file
$bed->open("<$bfile.bed") or print_OUT("I can not open binary PLINK file [ $bfile ]",$LOG) and exit(1);
binmode($bed); # set file type to binary
# check if the file is a PLINK file in the proper format by checking the first 3 bytes
my ($buffer,$n_bytes);
my $plink_bfile_signature = "";
read $bed, $plink_bfile_signature, 3;
if (unpack("B24",$plink_bfile_signature) ne '011011000001101100000001'){
print_OUT("Binary file is not in SNP-major format, please check you files\n",$LOG);
exit(1);
} else { print_OUT("Binary file is on SNP-major format",$LOG); }
# calculate how many bytes are needed to encode a SNP
# each byte has 8 bits with information for 4 genotypes
# if not exact round it up
if (($N_bytes_to_encode_snp - int($N_bytes_to_encode_snp)) != 0 ){ $N_bytes_to_encode_snp = int($N_bytes_to_encode_snp) + 1;}
}
# the genotype stack will store SNP genotypes if the SNP was mapped to more than 1 gene.
# the SNP genotype will be deleted after is no longer needed.
# this reduces the IO and speeds up
my %snp_genotype_stack = ();
foreach my $gn (sort { $gene{$a}->{counter} <=> $gene{$b}->{counter} } keys %gene){
# GET SNP GENOTYPE MATRIX
if (defined $geno_probs) {
my $snp_list = [];
my $lines = [];
foreach my $mapped_snp (@{$gene{$gn}->{snps}}){
next if (not exists $assoc_data{ $bim[$bim_ids{$mapped_snp}]->{snp_id} } );
if (defined $v){ print_OUT("Adding SNP [ $bim[ $bim_ids{$mapped_snp} ]->{snp_id} ] to genotypes of $gn",$LOG); }
push @{$snp_list}, $mapped_snp;
push @{$gene{$gn}->{geno_mat_rows}}, $mapped_snp;
push @{$gene{$gn}->{pvalues}}, $assoc_data{ $mapped_snp }->{pvalue};
push @{$lines}, $bim_ids{$mapped_snp} + 1;
}
my ($p_mat,$d_mat) = extract_genotypes_for_snp_list($snp_list,$lines,$g_prob_threshold,$geno_probs_format,$gprobs,$gprobs_index);
$gene{$gn}->{genotypes} = $d_mat;
# check the range of the values. if the range is 0 then the SNP is monomorphic and shoudl be dropped
my ($min,$max,$min_d,$max_d)= $gene{$gn}->{genotypes}->minmaximum;
my $non_zero_variance_index = which(($max - $min) != 0);
# check if any SNPs needs to be dropped
if ($non_zero_variance_index->isempty()){
print_OUT(" [ $gn ] All SNPs are monomorphic, going to next gene",$LOG) if (defined $v);
next;
}
my $old_size = scalar @{ $gene{$gn}->{geno_mat_rows} };
my $new_size = scalar list $non_zero_variance_index;
if ( $old_size != $new_size){
$gene{$gn}->{genotypes} = $gene{$gn}->{genotypes}->(,$non_zero_variance_index);
$gene{$gn}->{geno_mat_rows} = [@{$gene{$gn}->{geno_mat_rows}}[$non_zero_variance_index->flat->list] ];
$gene{$gn}->{pvalues} = [@{$gene{$gn}->{pvalues}}[$non_zero_variance_index->flat->list] ];
if (defined $v){
my $diff = $old_size - $new_size;
print_OUT("Dropping [ $diff ] monomorphic SNPs",$LOG);
}
}
} elsif (defined $bfile) {
my $matrix;
# loop over the snps mapped to the gene
foreach my $mapped_snp (@{$gene{$gn}->{snps}}){
# skip if it does not have association information
next if (not exists $assoc_data{ $bim[$bim_ids{$mapped_snp}]->{snp_id} } );
if (defined $v){ print_OUT("Adding SNP [ $bim[ $bim_ids{$mapped_snp} ]->{snp_id} ] to genotypes of $gn",$LOG); }
if (exists $snp_genotype_stack{$mapped_snp}) {
if (defined $v){
print_OUT(" '-> SNP [ $mapped_snp] already read",$LOG);
}
push @{ $matrix }, $snp_genotype_stack{$mapped_snp};
} else {
# because we know the index of the SNP in the genotype file we know on which byte its information starts
my $snp_byte_start = $N_bytes_to_encode_snp*$bim_ids{$mapped_snp};
# here i extract the actual genotypes
my @snp_genotypes = @{ extract_binary_genotypes(scalar @fam,$N_bytes_to_encode_snp,$snp_byte_start,$bed) };
# store the genotypes.
# if a snp does not use the 8 bits of a byte the rest of the bits are fill with missing values
# here i extract the number of genotypes corresponding to the number of samples
my $maf = get_maf([@snp_genotypes[0..scalar @fam - 1]] ); # check the maf of the SNP
next if ($maf == 0 or $maf ==1); # go to next if it is monomorphic
push @{ $matrix }, [@snp_genotypes[0..scalar @fam - 1]];
$snp_genotype_stack{$mapped_snp} = [@snp_genotypes[0..scalar @fam - 1]];
}
# add snp id to matrix row names
push @{ $gene{$gn}->{geno_mat_rows} }, $mapped_snp;
# store the p-value of the snp
push @{ $gene{$gn}->{pvalues} }, $assoc_data{ $mapped_snp }->{pvalue};
my $effect_measure = $assoc_data{ $mapped_snp }->{effect_size_measure};
if (defined $effect_measure){
if ($effect_measure eq 'or'){
push @{ $gene{$gn}->{effect_size} }, log $assoc_data{ $mapped_snp }->{$effect_measure};
} else {
push @{ $gene{$gn}->{effect_size} }, $assoc_data{ $mapped_snp }->{$effect_measure};
}
if (defined $assoc_data{ $mapped_snp }->{se}){
if ($effect_measure eq 'or'){
#push @{ $gene{$gn}->{effect_size_se} }, abs log $assoc_data{ $bim[ $bim_ids{$mapped_snp} ]->{snp_id} }->{se};
push @{ $gene{$gn}->{effect_size_se} }, $assoc_data{ $mapped_snp }->{se};
} else {
push @{ $gene{$gn}->{effect_size_se} }, $assoc_data{ $mapped_snp }->{se};
}
}
}
}
# generate the genotype matrix as a PDL piddle
$gene{$gn}->{genotypes} = pdl $matrix;
} elsif (defined $ped and defined $map){
my $genotypes = pdl @{ $ped_map_genotypes };
foreach (my $index_snp = 0; $index_snp < scalar @bim; $index_snp++){
# store SNP genotypes only if it has association data
if (exists $assoc_data{ ${ $bim[$index_snp] }{snp_id} } ){
# for every gene mapped to this SNP, push inside the genotype matrix this SNP genotypes.
foreach my $gn (@{ $snp_to_gene{${ $bim[$index_snp] }{snp_id} } }){
if (defined $v){ print_OUT("Adding SNP [ ${ $bim[$index_snp] }{snp_id} ] to genotypes of $gn",$LOG); }
next if ( grep $_ eq ${ $bim[$index_snp] }{snp_id} , @{ $gene{$gn}->{geno_mat_rows} } );
my $maf = get_maf([ $genotypes(,$index_snp)->list ] ); # check the maf of the SNP
next if ($maf == 0 or $maf ==1); # go to next if it is monomorphic
# add the genotypes to the genotype matrix
$gene{$gn}->{genotypes} = $gene{$gn}->{genotypes}->glue(1,$genotypes(,$index_snp));
push @{ $gene{$gn}->{geno_mat_rows} }, ${ $bim[$index_snp] }{snp_id};
push @{ $gene{$gn}->{pvalues} }, $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{pvalue};
my $effect_measure = $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{effect_size_measure};
if (defined $effect_measure){
if ($effect_measure eq 'or'){
push @{ $gene{$gn}->{effect_size} }, log $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{$effect_measure};
} else {
push @{ $gene{$gn}->{effect_size} }, $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{$effect_measure};
}
if (defined $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{se}){
if ($effect_measure eq 'or'){
#push @{ $gene{$gn}->{effect_size_se} }, abs log $assoc_data{ $bim[ $bim_ids{$mapped_snp} ]->{snp_id} }->{se};
push @{ $gene{$gn}->{effect_size_se} }, $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{se};
} else {
push @{ $gene{$gn}->{effect_size_se} }, $assoc_data{ ${ $bim[$index_snp] }{snp_id} }->{se};
}
}
}
}
}
}
} elsif (defined $mperm_dump) {
print_OUT("Reading SNP statistics from [ $mperm_dump ]",$LOG);
my $null_stats = extract_stats_from_mperm_dump_all_files($mperm_dump);
my $matrix = [];
my $indexes = [];
foreach my $mapped_snp (@{$gene{$gn}->{snps}}){
next if (not exists $assoc_data{ $mapped_snp } );
push @{ $indexes}, $assoc_data{ $mapped_snp }->{line};
# add snp id to matrix row names
push @{ $gene{$gn}->{geno_mat_rows} }, $mapped_snp;
# store the p-value of the snp
push @{ $gene{$gn}->{pvalues} }, $assoc_data{ $mapped_snp }->{pvalue};
my $effect_measure = $assoc_data{ $mapped_snp }->{effect_size_measure};
if (defined $effect_measure){
if ($effect_measure eq 'or'){
push @{ $gene{$gn}->{effect_size} }, log $assoc_data{ $mapped_snp }->{$effect_measure};
} else {
push @{ $gene{$gn}->{effect_size} }, $assoc_data{ $mapped_snp }->{$effect_measure};
}
if (defined $assoc_data{ $mapped_snp }->{se}){
if ($effect_measure eq 'or'){
#push @{ $gene{$gn}->{effect_size_se} }, abs log $assoc_data{ $bim[ $bim_ids{$mapped_snp} ]->{snp_id} }->{se};
push @{ $gene{$gn}->{effect_size_se} }, $assoc_data{ $mapped_snp }->{se};
} else {
push @{ $gene{$gn}->{effect_size_se} }, $assoc_data{ $mapped_snp }->{se};
}
}
}
}
$indexes = flat pdl $indexes;
# generate the genotype matrix as a PDL piddle
$gene{$gn}->{genotypes} = $null_stats->($indexes,)->transpose;
} else {
print_OUT("WARNING: Gene p-values will be calculated with the precomputed correlation only. If correlation for some SNPs pairs are missing you may get wrong results, please check your inputs for completeness",$LOG);
}
if (defined $low_mem){
$gene{$gn}->{genotypes} = 1_000*$gene{$gn}->{genotypes};
( $gene{$gn}->{genotypes}, $gene{$gn}->{asize}) = $gene{$gn}->{genotypes}->short->rice_compress();
}
$count++;
&report_advance($count,$report," '-> Gene genotypes read");
}
if (defined $bfile) {
$bed->close();
} elsif (defined $geno_probs){
$gprobs->close();
}
print_OUT("Starting to calculate gene p-values",$LOG);
$count = 0;
%snp_genotype_stack = ();
foreach my $gn (sort { $gene{$a}->{counter} <=> $gene{$b}->{counter} } keys %gene){
if (defined $low_mem){
$gene{$gn}->{genotypes} = $gene{$gn}->{genotypes}->rice_expand($gene{$gn}->{asize});
$gene{$gn}->{genotypes} /= 1000;
$gene{$gn}->{genotypes} = float $gene{$gn}->{genotypes};
}
# Calculate the genotypes correlation matrix
my $more_corrs = "";
($gene{$gn}->{cor},$gene{$gn}->{cor_ld_r},$more_corrs) = deal_with_correlations($gene{$gn},\%correlation,$use_ld_as_corr);
%correlation = (%correlation,%{$more_corrs});
if (defined $gates_corr){
$gene{$gn}->{cor} = gates_p_sham_ld_to_pvalue_correlation( $gene{$gn}->{cor} );
#$gene{$gn}->{cor_ld_r} = gates_p_sham_ld_to_pvalue_correlation( $gene{$gn}->{cor_ld_r} );
}
# Calculate the weights for the gene
$gene{$gn}->{weights} = deal_with_weights(\@weights_file,$gene{$gn},$w_maf,$weights);
if (defined $geno_probs) {
# Calculate average max genotype prob and use it as weitghs
$gene{$gn}->{weights} *= daverage $gene{$gn}->{genotypes};
$gene{$gn}->{weights} /= $gene{$gn}->{weights}->sumover;
}
# CALCULATE GENE P-VALUES
my $z_based_p = z_based_gene_pvalues($gene{$gn},$mnd);
if (ref($z_based_p) ne 'HASH' and $z_based_p == -9){
$z_based_p = {
'B_stouffer_fix' => "NA",
'B_stouffer_random' => "NA",
'B_fix' => "NA",
'B_random' => "NA",
'V_fix' => "NA",
'V_random' => "NA",
'Chi_fix' => "NA",
'Chi_random' => "NA",
'Z_P_fix' => "NA",
'Z_P_random' => "NA",
'Q' => "NA",
'Q_P' => "NA",
'I2' => "NA",
'tau_squared' => "NA",
'N' => scalar @{ $gene{$gn}->{geno_mat_rows} },
};
}
my $pvalue_based_p = gene_pvalue($gn);
# check which analysis strategy to follow
if (not defined $mnd){ # asymptotic
print $OUT join "\t",($gene{$gn}->{ensembl},$gene{$gn}->{hugo},$gene{$gn}->{gene_type},$gene{$gn}->{chr},$gene{$gn}->{start},$gene{$gn}->{end},
$gene{$gn}->{pvalues}->min,
$pvalue_based_p->{sidak_min_p},
$pvalue_based_p->{fisher},
$pvalue_based_p->{fisher_chi},
$pvalue_based_p->{fisher_df},
$z_based_p->{'B_fix'},
$z_based_p->{'V_fix'},
$z_based_p->{'Z_P_fix'},
$z_based_p->{'B_random'},
$z_based_p->{'V_random'},
$z_based_p->{'Z_P_random'},
$z_based_p->{'I2'},
$z_based_p->{'Q'},
$z_based_p->{'Q_P'},
$z_based_p->{'tau_squared'},
$pvalue_based_p->{Meff_Galwey},
$pvalue_based_p->{Meff_gao},
scalar @{ $gene{$gn}->{geno_mat_rows} });
print $OUT "\n";