annotate compute_q_values.pl @ 3:d1521b82b9fe draft

planemo upload commit 33927a87ba2eee9bf0ecdd376a66241b17b3d734
author devteam
date Tue, 13 Oct 2015 12:15:36 -0400
parents b617b4b3d913
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
1 # A program to compute the q-values based on the p-values of multiple simultaneous tests.
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
2 # The q-valules are computed using a specific R package created by John Storey called "qvalue".
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
3 # The input is a TABULAR format file consisting of one column only that represents the p-values
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
4 # of multiple simultaneous tests, one line for every p-value.
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
5 # The first output is a TABULAR format file consisting of one column only that represents the q-values
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
6 # corresponding to p-values, one line for every q-value.
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
7 # the second output is a TABULAR format file consisting of three pages: the first page represents
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
8 # the p-values histogram, the second page represents the q-values histogram, and the third page represents
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
9 # the four Q-plots as introduced in the "qvalue" package manual.
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
10
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
11 use strict;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
12 use warnings;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
13 use IO::Handle;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
14 use File::Temp qw/ tempfile tempdir /;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
15 my $tdir = tempdir( CLEANUP => 0 );
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
16
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
17 # check to make sure having correct input and output files
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
18 my $usage = "usage: compute_q_values.pl [TABULAR.in] [lambda] [pi0_method] [fdr_level] [robust] [TABULAR.out] [PDF.out] \n";
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
19 die $usage unless @ARGV == 7;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
20
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
21 #get the input arguments
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
22 my $p_valuesInputFile = $ARGV[0];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
23 my $lambdaValue = $ARGV[1];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
24 my $pi0_method = $ARGV[2];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
25 my $fdr_level = $ARGV[3];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
26 my $robustValue = $ARGV[4];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
27 my $q_valuesOutputFile = $ARGV[5];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
28 my $p_q_values_histograms_QPlotsFile = $ARGV[6];
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
29
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
30 if($lambdaValue =~ /sequence/){
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
31 $lambdaValue = "seq(0, 0.95, 0.05)";
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
32 }
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
33
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
34 #open the input files
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
35 open (INPUT, "<", $p_valuesInputFile) || die("Could not open file $p_valuesInputFile \n");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
36 open (OUTPUT1, ">", $q_valuesOutputFile) || die("Could not open file $q_valuesOutputFile \n");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
37 open (OUTPUT2, ">", $p_q_values_histograms_QPlotsFile) || die("Could not open file $p_q_values_histograms_QPlotsFile \n");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
38 #open (ERROR, ">", "error.txt") or die ("Could not open file error.txt \n");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
39
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
40 #save all error messages into the error file $errorFile using the error file handle ERROR
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
41 #STDERR -> fdopen( \*ERROR, "w" ) or die ("Could not direct errors to the error file error.txt \n");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
42
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
43 #warn "Hello Error File \n";
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
44
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
45 #variable to store the name of the R script file
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
46 my $r_script;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
47
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
48 # R script to implement the calcualtion of q-values based on multiple simultaneous tests p-values
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
49 # construct an R script file and save it in a temp directory
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
50 chdir $tdir;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
51 $r_script = "q_values_computation.r";
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
52
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
53 open(Rcmd,">", $r_script) or die "Cannot open $r_script \n\n";
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
54 print Rcmd "
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
55 #options(show.error.messages = FALSE);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
56
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
57 #load necessary packages
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
58 suppressPackageStartupMessages(library(tcltk));
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
59 library(qvalue);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
60
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
61 #read the p-values of the multiple simultaneous tests from the input file $p_valuesInputFile
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
62 p <- scan(\"$p_valuesInputFile\", quiet = TRUE);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
63
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
64 #compute the q-values that correspond to the p-values of the multiple simultaneous tests
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
65 qobj <- qvalue(p, pi0.meth = \"$pi0_method\", lambda = $lambdaValue, fdr.level = $fdr_level, robust = $robustValue);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
66 #qobj <- qvalue(p, pi0.meth = \"smoother\", lambda = seq(0, 0.95, 0.05), fdr.level = 0.05);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
67 #qobj <- qvalue(p, pi0.meth = \"bootstrap\", fdr.level = 0.05);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
68
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
69 #draw the p-values histogram, the q-values histogram, and the four Q-plots
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
70 # and save them on multiple pages of the output file $p_q_values_histograms_QPlotsFile
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
71 pdf(file = \"$p_q_values_histograms_QPlotsFile\", width = 6.25, height = 6, family = \"Times\", pointsize = 12, onefile = TRUE)
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
72 hist(qobj\$pvalues);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
73 #dev.off();
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
74
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
75 hist(qobj\$qvalues);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
76 #dev.off();
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
77
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
78 qplot(qobj);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
79 dev.off();
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
80
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
81 #save the q-values in the output file $q_valuesOutputFile
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
82 qobj\$pi0 <- signif(qobj\$pi0,digits=6)
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
83 qwrite(qobj, filename=\"$q_valuesOutputFile\");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
84
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
85 #options(show.error.messages = TRUE);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
86 #eof\n";
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
87 close Rcmd;
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
88
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
89 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
90
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
91 #close the input and output and error files
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
92 #close(ERROR);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
93 close(OUTPUT2);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
94 close(OUTPUT1);
b617b4b3d913 Uploaded
devteam
parents:
diff changeset
95 close(INPUT);