annotate execute_dwt_var_perClass.pl @ 0:cb422b6f49d2 draft

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