annotate tools/discreteWavelet/execute_dwt_var_perClass.pl @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/perl -w
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 use warnings;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 use IO::Handle;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 use POSIX qw(floor ceil);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 # example: perl execute_dwt_var_perClass.pl hg18_NCNR_10bp_3flanks_deletionHotspot_data_del.txt deletionHotspot 3flanks del
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 $usage = "execute_dwt_var_perClass.pl [TABULAR.in] [TABULAR.out] [TABULAR.out] [PDF.out] \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 die $usage unless @ARGV == 4;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 #get the input arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 my $inputFile = $ARGV[0];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 my $firstOutputFile = $ARGV[1];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 my $secondOutputFile = $ARGV[2];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 my $thirdOutputFile = $ARGV[3];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 open (INPUT, "<", $inputFile) || die("Could not open file $inputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 open (OUTPUT1, ">", $firstOutputFile) || die("Could not open file $firstOutputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 open (OUTPUT2, ">", $secondOutputFile) || die("Could not open file $secondOutputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 open (OUTPUT3, ">", $thirdOutputFile) || die("Could not open file $thirdOutputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 open (ERROR, ">", "error.txt") or die ("Could not open file error.txt \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 #save all error messages into the error file $errorFile using the error file handle ERROR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 STDERR -> fdopen( \*ERROR, "w" ) or die ("Could not direct errors to the error file error.txt \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 # choosing meaningful names for the output files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 $max_dwt = $firstOutputFile;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 $pvalue = $secondOutputFile;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 $pdf = $thirdOutputFile;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 # count the number of columns in the input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 while($buffer = <INPUT>){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 #if ($buffer =~ m/interval/){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 chomp($buffer);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 $buffer =~ s/^#\s*//;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 @contrl = split(/\t/, $buffer);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 last;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 #}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 print "The number of columns in the input file is: " . (@contrl) . "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 print "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 # count the number of motifs in the input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 $count = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 for ($i = 0; $i < @contrl; $i++){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 $count++;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 print "# $contrl[$i]\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 print "The number of motifs in the input file is: $count \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 # check if the number of motifs is not a multiple of 12, and round up is so
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 $count2 = ($count/12);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 if ($count2 =~ m/(\D)/){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 print "the number of motifs is not a multiple of 12 \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 $count2 = ceil($count2);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 print "the number of motifs is a multiple of 12 \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 print "There will be $count2 subfiles\n\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 # split infile into subfiles only 12 motif per file for R plotting
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 for ($x = 1; $x <= $count2; $x++){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 $a = (($x - 1) * 12 + 1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 $b = $x * 12;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if ($x < $count2){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 print "# data.short $x <- data_test[, +c($a:$b)]; \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 print "# data.short $x <- data_test[, +c($a:ncol(data_test)]; \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 print "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 print "There are 4 output files: \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 print "The first output file is a pdf file\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 print "The second output file is a max_dwt file\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 print "The third output file is a pvalues file\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 print "The fourth output file is a test_final_pvalues file\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 # write R script
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 $r_script = "get_dwt_varPermut_getMax.r";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 print "The R file name is: $r_script \n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 open(Rcmd, ">", "$r_script") or die "Cannot open $r_script \n\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 print Rcmd "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 ######################################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 # plot power spectra, i.e. wavelet variance by class
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 # add code to create null bands by permuting the original data series
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 # get class of maximum significant variance per feature
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 # generate plots and table matrix of variance including p-values
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 ######################################################################
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 library(\"Rwave\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 library(\"wavethresh\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 library(\"waveslim\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 options(echo = FALSE)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 # normalize data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 norm <- function(data){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 v <- (data-mean(data))/sd(data);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 if(sum(is.na(v)) >= 1){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 v<-data;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 return(v);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 dwt_var_permut_getMax <- function(data, names, filter = 4, bc = \"symmetric\", method = \"kendall\", wf = \"haar\", boundary = \"reflection\") {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 max_var = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 matrix = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 title = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 final_pvalue = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 short.levels = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 scale = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 print(names);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 par(mfcol = c(length(names), length(names)), mar = c(0, 0, 0, 0), oma = c(4, 3, 3, 2), xaxt = \"s\", cex = 1, las = 1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 short.levels <- wd(data[, 1], filter.number = filter, bc = bc)\$nlevels;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 title <- c(\"motif\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 for (i in 1:short.levels){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 title <- c(title, paste(i, \"var\", sep = \"_\"), paste(i, \"pval\", sep = \"_\"), paste(i, \"test\", sep = \"_\"));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 print(title);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 # normalize the raw data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 data<-apply(data,2,norm);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 for(i in 1:length(names)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 for(j in 1:length(names)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136 temp = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 results = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 wave1.dwt = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 out = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 out <- vector(length = length(title));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 temp <- vector(length = short.levels);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 if(i < j) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 plot(temp, type = \"n\", axes = FALSE, xlab = NA, ylab = NA);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 box(col = \"grey\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 grid(ny = 0, nx = NULL);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 if (i > j){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 plot(temp, type = \"n\", axes = FALSE, xlab = NA, ylab = NA);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 box(col = \"grey\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 grid(ny = 0, nx = NULL);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 wave1.dwt <- dwt(data[, i], wf = wf, short.levels, boundary = boundary);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 temp_row = (short.levels + 1 ) * -1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 temp_col = 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 temp <- wave.variance(wave1.dwt)[temp_row, temp_col];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 #permutations code :
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 feature1 = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 null = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 var_25 = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 var_975 = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 med = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 feature1 = data[, i];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 for (k in 1:1000) {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 nk_1 = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 null.levels = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 var = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 null_wave1 = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 nk_1 = sample(feature1, length(feature1), replace = FALSE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 null.levels <- wd(nk_1, filter.number = filter, bc = bc)\$nlevels;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 var <- vector(length = length(null.levels));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 null_wave1 <- dwt(nk_1, wf = wf, short.levels, boundary = boundary);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 var<- wave.variance(null_wave1)[-8, 1];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 null= rbind(null, var);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 null <- apply(null, 2, sort, na.last = TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 var_25 <- null[25, ];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 var_975 <- null[975, ];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 med <- (apply(null, 2, median, na.rm = TRUE));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 # plot
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 results <- cbind(temp, var_25, var_975);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 matplot(results, type = \"b\", pch = \"*\", lty = 1, col = c(1, 2, 2), axes = F);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 # get pvalues by comparison to null distribution
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 out <- (names[i]);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 for (m in 1:length(temp)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 print(paste(\"scale\", m, sep = \" \"));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 print(paste(\"var\", temp[m], sep = \" \"));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 print(paste(\"med\", med[m], sep = \" \"));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 pv = tail = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 out <- c(out, format(temp[m], digits = 3));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 if (temp[m] >= med[m]){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 # R tail test
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 print(\"R\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 tail <- \"R\";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 pv <- (length(which(null[, m] >= temp[m])))/(length(na.exclude(null[, m])));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 if (temp[m] < med[m]){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 # L tail test
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 print(\"L\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 tail <- \"L\";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 pv <- (length(which(null[, m] <= temp[m])))/(length(na.exclude(null[, m])));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 out <- c(out, pv);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 print(pv);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 out <- c(out, tail);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 final_pvalue <-rbind(final_pvalue, out);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 # get variances outside null bands by comparing temp to null
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 ## temp stores variance for each scale, and null stores permuted variances for null bands
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 for (n in 1:length(temp)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 if (temp[n] <= var_975[n]){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 temp[n] <- NA;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 } else {
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 temp[n] <- temp[n];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 matrix <- rbind(matrix, temp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 # labels
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 if (i == 1){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 mtext(names[j], side = 2, line = 0.5, las = 3, cex = 0.25);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 if (j == 1){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 mtext(names[i], side = 3, line = 0.5, cex = 0.25);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 if (j == length(names)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 axis(1, at = (1:short.levels), las = 3, cex.axis = 0.5);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 colnames(final_pvalue) <- title;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 #write.table(final_pvalue, file = \"test_final_pvalue.txt\", sep = \"\\t\", quote = FALSE, row.names = FALSE, append = TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 # get maximum variance larger than expectation by comparison to null bands
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 varnames <- vector();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 for(i in 1:length(names)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 name1 = paste(names[i], \"var\", sep = \"_\")
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 varnames <- c(varnames, name1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 rownames(matrix) <- varnames;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 colnames(matrix) <- (1:short.levels);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 max_var <- names;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 scale <- vector(length = length(names));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 for (x in 1:nrow(matrix)){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 if (length(which.max(matrix[x, ])) == 0){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 scale[x] <- NA;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 scale[x] <- colnames(matrix)[which.max(matrix[x, ])];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 max_var <- cbind(max_var, scale);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 write.table(max_var, file = \"$max_dwt\", sep = \"\\t\", quote = FALSE, row.names = FALSE, append = TRUE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 return(final_pvalue);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 }\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 print Rcmd "
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 # execute
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 # read in data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 data_test = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 data_test <- read.delim(\"$inputFile\");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 pdf(file = \"$pdf\", width = 11, height = 8);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 # loop to read and execute on all $count2 subfiles
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 final = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 for (x in 1:$count2){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 sub = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 sub_names = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284 a = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 b = NULL;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287 a = ((x - 1) * 12 + 1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 b = x * 12;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 if (x < $count2){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 sub <- data_test[, +c(a:b)];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292 sub_names <- colnames(data_test)[a:b];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293 final <- rbind(final, dwt_var_permut_getMax(sub, sub_names));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295 else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 sub <- data_test[, +c(a:ncol(data_test))];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 sub_names <- colnames(data_test)[a:ncol(data_test)];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298 final <- rbind(final, dwt_var_permut_getMax(sub, sub_names));
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303 dev.off();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 write.table(final, file = \"$pvalue\", sep = \"\\t\", quote = FALSE, row.names = FALSE);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 #eof\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309 close Rcmd;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 system("echo \"wavelet ANOVA started on \`hostname\` at \`date\`\"\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312 system("R --no-restore --no-save --no-readline < $r_script > $r_script.out");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 system("echo \"wavelet ANOVA ended on \`hostname\` at \`date\`\"\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 #close the input and output and error files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316 close(ERROR);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 close(OUTPUT3);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 close(OUTPUT2);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 close(OUTPUT1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320 close(INPUT);