/tools/discreteWavelet/execute_dwt_cor_aVa_perClass.pl

https://bitbucket.org/cistrome/cistrome-harvard/ · Perl · 221 lines · 201 code · 14 blank · 6 comment · 5 complexity · d3ad4f072a2925c11263dfc8d541cd71 MD5 · raw file

  1. #!/usr/bin/perl -w
  2. use warnings;
  3. use IO::Handle;
  4. $usage = "execute_dwt_cor_aVa_perClass.pl [TABULAR.in] [TABULAR.in] [TABULAR.out] [PDF.out] \n";
  5. die $usage unless @ARGV == 4;
  6. #get the input arguments
  7. my $firstInputFile = $ARGV[0];
  8. my $secondInputFile = $ARGV[1];
  9. my $firstOutputFile = $ARGV[2];
  10. my $secondOutputFile = $ARGV[3];
  11. open (INPUT1, "<", $firstInputFile) || die("Could not open file $firstInputFile \n");
  12. open (INPUT2, "<", $secondInputFile) || die("Could not open file $secondInputFile \n");
  13. open (OUTPUT1, ">", $firstOutputFile) || die("Could not open file $firstOutputFile \n");
  14. open (OUTPUT2, ">", $secondOutputFile) || die("Could not open file $secondOutputFile \n");
  15. open (ERROR, ">", "error.txt") or die ("Could not open file error.txt \n");
  16. #save all error messages into the error file $errorFile using the error file handle ERROR
  17. STDERR -> fdopen( \*ERROR, "w" ) or die ("Could not direct errors to the error file error.txt \n");
  18. print "There are two input data files: \n";
  19. print "The input data file is: $firstInputFile \n";
  20. print "The control data file is: $secondInputFile \n";
  21. # IvC test
  22. $test = "cor_aVa";
  23. # construct an R script to implement the IvC test
  24. print "\n";
  25. $r_script = "get_dwt_cor_aVa_test.r";
  26. print "$r_script \n";
  27. open(Rcmd, ">", "$r_script") or die "Cannot open $r_script \n\n";
  28. print Rcmd "
  29. ##################################################################################
  30. # code to do all correlation tests of form: motif(a) vs. motif(a)
  31. # add code to create null bands by permuting the original data series
  32. # generate plots and table matrix of correlation coefficients including p-values
  33. ##################################################################################
  34. library(\"Rwave\");
  35. library(\"wavethresh\");
  36. library(\"waveslim\");
  37. options(echo = FALSE)
  38. # normalize data
  39. norm <- function(data){
  40. v <- (data - mean(data))/sd(data);
  41. if(sum(is.na(v)) >= 1){
  42. v <- data;
  43. }
  44. return(v);
  45. }
  46. dwt_cor <- function(data.short, names.short, data.long, names.long, test, pdf, table, filter = 4, bc = \"symmetric\", method = \"kendall\", wf = \"haar\", boundary = \"reflection\") {
  47. print(test);
  48. print(pdf);
  49. print(table);
  50. pdf(file = pdf);
  51. final_pvalue = NULL;
  52. title = NULL;
  53. short.levels <- wd(data.short[, 1], filter.number = filter, bc = bc)\$nlevels;
  54. title <- c(\"motif\");
  55. for (i in 1:short.levels){
  56. title <- c(title, paste(i, \"cor\", sep = \"_\"), paste(i, \"pval\", sep = \"_\"));
  57. }
  58. print(title);
  59. # normalize the raw data
  60. data.short <- apply(data.short, 2, norm);
  61. data.long <- apply(data.long, 2, norm);
  62. for(i in 1:length(names.short)){
  63. # Kendall Tau
  64. # DWT wavelet correlation function
  65. # include significance to compare
  66. wave1.dwt = wave2.dwt = NULL;
  67. tau.dwt = NULL;
  68. out = NULL;
  69. print(names.short[i]);
  70. print(names.long[i]);
  71. # need exit if not comparing motif(a) vs motif(a)
  72. if (names.short[i] != names.long[i]){
  73. stop(paste(\"motif\", names.short[i], \"is not the same as\", names.long[i], sep = \" \"));
  74. }
  75. else {
  76. wave1.dwt <- dwt(data.short[, i], wf = wf, short.levels, boundary = boundary);
  77. wave2.dwt <- dwt(data.long[, i], wf = wf, short.levels, boundary = boundary);
  78. tau.dwt <- vector(length=short.levels)
  79. #perform cor test on wavelet coefficients per scale
  80. for(level in 1:short.levels){
  81. w1_level = w2_level = NULL;
  82. w1_level <- (wave1.dwt[[level]]);
  83. w2_level <- (wave2.dwt[[level]]);
  84. tau.dwt[level] <- cor.test(w1_level, w2_level, method = method)\$estimate;
  85. }
  86. # CI bands by permutation of time series
  87. feature1 = feature2 = NULL;
  88. feature1 = data.short[, i];
  89. feature2 = data.long[, i];
  90. null = results = med = NULL;
  91. cor_25 = cor_975 = NULL;
  92. for (k in 1:1000) {
  93. nk_1 = nk_2 = NULL;
  94. null.levels = NULL;
  95. cor = NULL;
  96. null_wave1 = null_wave2 = NULL;
  97. nk_1 <- sample(feature1, length(feature1), replace = FALSE);
  98. nk_2 <- sample(feature2, length(feature2), replace = FALSE);
  99. null.levels <- wd(nk_1, filter.number = filter, bc = bc)\$nlevels;
  100. cor <- vector(length = null.levels);
  101. null_wave1 <- dwt(nk_1, wf = wf, short.levels, boundary = boundary);
  102. null_wave2 <- dwt(nk_2, wf = wf, short.levels, boundary = boundary);
  103. for(level in 1:null.levels){
  104. null_level1 = null_level2 = NULL;
  105. null_level1 <- (null_wave1[[level]]);
  106. null_level2 <- (null_wave2[[level]]);
  107. cor[level] <- cor.test(null_level1, null_level2, method = method)\$estimate;
  108. }
  109. null = rbind(null, cor);
  110. }
  111. null <- apply(null, 2, sort, na.last = TRUE);
  112. print(paste(\"NAs\", length(which(is.na(null))), sep = \" \"));
  113. cor_25 <- null[25,];
  114. cor_975 <- null[975,];
  115. med <- (apply(null, 2, median, na.rm = TRUE));
  116. # plot
  117. results <- cbind(tau.dwt, cor_25, cor_975);
  118. matplot(results, type = \"b\", pch = \"*\" , lty = 1, col = c(1, 2, 2), ylim = c(-1, 1), xlab = \"Wavelet Scale\", ylab = \"Wavelet Correlation Kendall's Tau\", main = (paste(test, names.short[i], sep = \" \")), cex.main = 0.75);
  119. abline(h = 0);
  120. # get pvalues by comparison to null distribution
  121. ### modify pval calculation for error type II of T test ####
  122. out <- (names.short[i]);
  123. for (m in 1:length(tau.dwt)){
  124. print(paste(\"scale\", m, sep = \" \"));
  125. print(paste(\"tau\", tau.dwt[m], sep = \" \"));
  126. print(paste(\"med\", med[m], sep = \" \"));
  127. out <- c(out, format(tau.dwt[m], digits = 3));
  128. pv = NULL;
  129. if(is.na(tau.dwt[m])){
  130. pv <- \"NA\";
  131. }
  132. else {
  133. if (tau.dwt[m] >= med[m]){
  134. # R tail test
  135. print(paste(\"R\"));
  136. ### per sv ok to use inequality not strict
  137. pv <- (length(which(null[, m] >= tau.dwt[m])))/(length(na.exclude(null[, m])));
  138. if (tau.dwt[m] == med[m]){
  139. print(\"tau == med\");
  140. print(summary(null[, m]));
  141. }
  142. }
  143. else if (tau.dwt[m] < med[m]){
  144. # L tail test
  145. print(paste(\"L\"));
  146. pv <- (length(which(null[, m] <= tau.dwt[m])))/(length(na.exclude(null[, m])));
  147. }
  148. }
  149. out <- c(out, pv);
  150. print(paste(\"pval\", pv, sep = \" \"));
  151. }
  152. final_pvalue <- rbind(final_pvalue, out);
  153. print(out);
  154. }
  155. }
  156. colnames(final_pvalue) <- title;
  157. write.table(final_pvalue, file = table, sep = \"\\t\", quote = FALSE, row.names = FALSE)
  158. dev.off();
  159. }\n";
  160. print Rcmd "
  161. # execute
  162. # read in data
  163. inputData1 = inputData2 = NULL;
  164. inputData.short1 = inputData.short2 = NULL;
  165. inputDataNames.short1 = inputDataNames.short2 = NULL;
  166. inputData1 <- read.delim(\"$firstInputFile\");
  167. inputData.short1 <- inputData1[, +c(1:ncol(inputData1))];
  168. inputDataNames.short1 <- colnames(inputData.short1);
  169. inputData2 <- read.delim(\"$secondInputFile\");
  170. inputData.short2 <- inputData2[, +c(1:ncol(inputData2))];
  171. inputDataNames.short2 <- colnames(inputData.short2);
  172. # cor test for motif(a) in inputData1 vs motif(a) in inputData2
  173. dwt_cor(inputData.short1, inputDataNames.short1, inputData.short2, inputDataNames.short2, test = \"$test\", pdf = \"$secondOutputFile\", table = \"$firstOutputFile\");
  174. print (\"done with the correlation test\");
  175. #eof\n";
  176. close Rcmd;
  177. system("echo \"wavelet IvC test started on \`hostname\` at \`date\`\"\n");
  178. system("R --no-restore --no-save --no-readline < $r_script > $r_script.out\n");
  179. system("echo \"wavelet IvC test ended on \`hostname\` at \`date\`\"\n");
  180. #close the input and output and error files
  181. close(ERROR);
  182. close(OUTPUT2);
  183. close(OUTPUT1);
  184. close(INPUT2);
  185. close(INPUT1);