Repository 'dwt_ivc_all'
hg clone https://toolshed.g2.bx.psu.edu/repos/devteam/dwt_ivc_all

Changeset 1:506ae7b0d85d (2020-07-06)
Previous changeset 0:0b89b03ad760 (2014-01-27)
Commit message:
"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/dwt_ivc_all commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
modified:
execute_dwt_IvC_all.xml
added:
execute_dwt_IvC_all.R
test-data/in1.tsv
test-data/in2.tsv
test-data/out2.pdf
removed:
execute_dwt_IvC_all.pl
b
diff -r 0b89b03ad760 -r 506ae7b0d85d execute_dwt_IvC_all.R
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/execute_dwt_IvC_all.R Mon Jul 06 20:31:56 2020 -0400
[
@@ -0,0 +1,163 @@
+###########################################################################################
+## code to do wavelet Indel vs. Control
+## signal is the difference I-C; function is second moment i.e. variance from zero not mean
+## to perform wavelet transf. of signal, scale-by-scale analysis of the function
+## create null bands by permuting the original data series
+## generate plots and table matrix of correlation coefficients including p-values
+############################################################################################
+library("wavethresh");
+library("waveslim");
+
+options(echo = FALSE)
+
+## normalize data
+norm <- function(data) {
+    v <- (data - mean(data)) / sd(data);
+    if (sum(is.na(v)) >= 1) {
+        v <- data;
+    }
+    return(v);
+}
+
+dwt_cor <- function(data_short, names_short, data_long, names_long, test, pdf, table, filter = 4, bc = "symmetric", wf = "haar", boundary = "reflection") {
+    print(test);
+    print(pdf);
+    print(table);
+
+    pdf(file = pdf);
+    final_pvalue <- NULL;
+    title <- NULL;
+
+    short_levels <- wavethresh::wd(data_short[, 1], filter.number = filter, bc = bc)$nlevels;
+    title <- c("motif");
+    for (i in 1:short_levels) {
+        title <- c(title, paste(i, "moment2", sep = "_"), paste(i, "pval", sep = "_"), paste(i, "test", sep = "_"));
+    }
+    print(title);
+
+    ## loop to compare a vs a
+    for (i in seq_len(length(names_short))) {
+        wave1_dwt <- NULL;
+        m2_dwt <- NULL;
+        diff <- NULL;
+        var_dwt <- NULL;
+        out <- NULL;
+        out <- vector(length = length(title));
+
+        print(names_short[i]);
+        print(names_long[i]);
+
+        ## need exit if not comparing motif(a) vs motif(a)
+        if (names_short[i] != names_long[i]) {
+            stop(paste("motif", names_short[i], "is not the same as", names_long[i], sep = " "));
+        }
+        else {
+            ## signal is the difference I-C data sets
+            diff <- data_short[, i] - data_long[, i];
+
+            ## normalize the signal
+            diff <- norm(diff);
+
+            ## function is 2nd moment
+            ## 2nd moment m_j = 1/N[sum_N(W_j + V_J)^2] = 1/N sum_N(W_j)^2 + (X_bar)^2
+            wave1_dwt <- waveslim::dwt(diff, wf = wf, short_levels, boundary = boundary);
+            var_dwt <- waveslim::wave.variance(wave1_dwt);
+            m2_dwt <- vector(length = short_levels)
+            for (level in 1:short_levels) {
+                m2_dwt[level] <- var_dwt[level, 1] + (mean(diff)^2);
+            }
+
+            ## CI bands by permutation of time series
+            feature1 <- NULL;
+            feature2 <- NULL;
+            feature1 <- data_short[, i];
+            feature2 <- data_long[, i];
+            null <- NULL;
+            results <- NULL;
+            med <- NULL;
+            m2_25 <- NULL;
+            m2_975 <- NULL;
+
+            for (k in 1:1000) {
+                nk_1 <- NULL;
+                nk_2 <- NULL;
+                m2_null <- NULL;
+                var_null <- NULL;
+                null_levels <- NULL;
+                null_wave1 <- NULL;
+                null_diff <- NULL;
+                nk_1 <- sample(feature1, length(feature1), replace = FALSE);
+                nk_2 <- sample(feature2, length(feature2), replace = FALSE);
+                null_levels <- wavethresh::wd(nk_1, filter.number = filter, bc = bc)$nlevels;
+                null_diff <- nk_1 - nk_2;
+                null_diff <- norm(null_diff);
+                null_wave1 <- waveslim::dwt(null_diff, wf = wf, short_levels, boundary = boundary);
+                var_null <- waveslim::wave.variance(null_wave1);
+                m2_null <- vector(length = null_levels);
+                for (level in 1:null_levels) {
+                    m2_null[level] <- var_null[level, 1] + (mean(null_diff)^2);
+                }
+                null <- rbind(null, m2_null);
+            }
+
+            null <- apply(null, 2, sort, na.last = TRUE);
+            m2_25 <- null[25, ];
+            m2_975 <- null[975, ];
+            med <- apply(null, 2, median, na.rm = TRUE);
+
+            ## plot
+            results <- cbind(m2_dwt, m2_25, m2_975);
+            matplot(results, type = "b", pch = "*", lty = 1, col = c(1, 2, 2), xlab = "Wavelet Scale", ylab = c("Wavelet 2nd Moment", test), main = (names_short[i]), cex.main = 0.75);
+            abline(h = 1);
+
+            ## get pvalues by comparison to null distribution
+            out <- c(names_short[i]);
+            for (m in seq_len(length(m2_dwt))) {
+                print(paste("scale", m, sep = " "));
+                print(paste("m2", m2_dwt[m], sep = " "));
+                print(paste("median", med[m], sep = " "));
+                out <- c(out, format(m2_dwt[m], digits = 4));
+                pv <- NULL;
+                if (is.na(m2_dwt[m])) {
+                    pv <- "NA";
+                }
+                else {
+                    if (m2_dwt[m] >= med[m]) {
+                        ## R tail test
+                        tail <- "R";
+                        pv <- (length(which(null[, m] >= m2_dwt[m]))) / (length(na.exclude(null[, m])));
+                    }
+                    else{
+                        if (m2_dwt[m] < med[m]) {
+                            ## L tail test
+                            tail <- "L";
+                            pv <- (length(which(null[, m] <= m2_dwt[m]))) / (length(na.exclude(null[, m])));
+                        }
+                    }
+                }
+                out <- c(out, pv);
+                print(pv);
+                out <- c(out, tail);
+            }
+            final_pvalue <- rbind(final_pvalue, out);
+            print(out);
+        }
+    }
+
+    colnames(final_pvalue) <- title;
+    write.table(final_pvalue, file = table, sep = "\t", quote = FALSE, row.names = FALSE);
+    dev.off();
+}
+## execute
+## read in data
+args <- commandArgs(trailingOnly = TRUE)
+
+input_data <- read.delim(args[1]);
+input_data_names <- colnames(input_data);
+
+control_data <- read.delim(args[2]);
+control_data_names <- colnames(control_data);
+
+## call the test function to implement IvC test
+dwt_cor(input_data, input_data_names, control_data, control_data_names, test = "IvC", pdf = args[3], table = args[4]);
+print("done with the correlation test");
b
diff -r 0b89b03ad760 -r 506ae7b0d85d execute_dwt_IvC_all.pl
--- a/execute_dwt_IvC_all.pl Mon Jan 27 09:25:56 2014 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
[
b'@@ -1,210 +0,0 @@\n-#!/usr/bin/perl -w\n-use warnings;\n-use IO::Handle;\n-\n-$usage = "execute_dwt_IvC_all.pl [TABULAR.in] [TABULAR.in] [TABULAR.out] [PDF.out]  \\n";\n-die $usage unless @ARGV == 4;\n-\n-#get the input arguments\n-my $firstInputFile = $ARGV[0];\n-my $secondInputFile = $ARGV[1];\n-my $firstOutputFile = $ARGV[2];\n-my $secondOutputFile = $ARGV[3];\n-\n-open (INPUT1, "<", $firstInputFile) || die("Could not open file $firstInputFile \\n");\n-open (INPUT2, "<", $secondInputFile) || die("Could not open file $secondInputFile \\n");\n-open (OUTPUT1, ">", $firstOutputFile) || die("Could not open file $firstOutputFile \\n");\n-open (OUTPUT2, ">", $secondOutputFile) || die("Could not open file $secondOutputFile \\n");\n-open (ERROR,  ">", "error.txt")  or die ("Could not open file error.txt \\n");\n-\n-#save all error messages into the error file $errorFile using the error file handle ERROR\n-STDERR -> fdopen( \\*ERROR,  "w" ) or die ("Could not direct errors to the error file error.txt \\n");\n-\n-\n-print "There are two input data files: \\n";\n-print "The input data file is: $firstInputFile \\n";\n-print "The control data file is: $secondInputFile \\n";\n-\n-# IvC test\n-$test = "IvC";\n-\n-# construct an R script to implement the IvC test\n-print "\\n";\n-\n-$r_script = "get_dwt_IvC_test.r"; \n-print "$r_script \\n";\n-\n-# R script\n-open(Rcmd, ">", "$r_script") or die "Cannot open $r_script \\n\\n";\n-print Rcmd "\n-        ###########################################################################################\n-        # code to do wavelet Indel vs. Control\n-        # signal is the difference I-C; function is second moment i.e. variance from zero not mean\n-        # to perform wavelet transf. of signal, scale-by-scale analysis of the function \n-        # create null bands by permuting the original data series\n-        # generate plots and table matrix of correlation coefficients including p-values\n-        ############################################################################################\n-        library(\\"Rwave\\");\n-        library(\\"wavethresh\\");\n-        library(\\"waveslim\\");\n-        \n-        options(echo = FALSE)\n-        \n-        # normalize data\n-        norm <- function(data){\n-            v <- (data - mean(data))/sd(data);\n-            if(sum(is.na(v)) >= 1){\n-                v <- data;\n-            }\n-            return(v);\n-        }\n-        \n-        dwt_cor <- function(data.short, names.short, data.long, names.long, test, pdf, table, filter = 4, bc = \\"symmetric\\", wf = \\"haar\\", boundary = \\"reflection\\") {\n-            print(test);\n-            print(pdf);\n-            print(table);\n-            \n-            pdf(file = pdf);\n-            final_pvalue = NULL;\n-            title = NULL;\n-                \n-            short.levels <- wd(data.short[, 1], filter.number = filter, bc = bc)\\$nlevels;\n-            title <- c(\\"motif\\");\n-            for (i in 1:short.levels){\n-            \ttitle <- c(title, paste(i, \\"moment2\\", sep = \\"_\\"), paste(i, \\"pval\\", sep = \\"_\\"), paste(i, \\"test\\", sep = \\"_\\"));\n-            }\n-            print(title);\n-        \n-            # loop to compare a vs a\n-            for(i in 1:length(names.short)){\n-        \t\twave1.dwt = NULL;\n-        \t\tm2.dwt = diff = var.dwt = NULL;\n-        \t\tout = NULL;\n-                out <- vector(length = length(title));\n-        \n-        \t\tprint(names.short[i]);\n-        \t\tprint(names.long[i]);\n-                        \n-        \t\t# need exit if not comparing motif(a) vs motif(a)\n-        \t\tif (names.short[i] != names.long[i]){\n-                \tstop(paste(\\"motif\\", names.short[i], \\"is not the same as\\", names.long[i], sep = \\" \\"));\n-        \t\t}\n-        \t\telse {\n-                \t# signal is the difference I-C data sets\n-                    diff<-data.short[,i]-data.long[,i];\n-        \n-                    # normalize the signal\n-                    diff<-norm(diff);\n-        \n-                    # function is 2nd moment\n-                    # 2nd moment m_j = 1/N[s'..b'LSE);\n-                \t\tnk_2 <- sample(feature2, length(feature2), replace = FALSE);\n-                \t\tnull.levels <- wd(nk_1, filter.number = filter, bc = bc)\\$nlevels;\n-                \t\tnull_diff <- nk_1-nk_2;\n-                \t\tnull_diff <- norm(null_diff);\n-                \t\tnull_wave1 <- dwt(null_diff, wf = wf, short.levels, boundary = boundary);\n-                        var_null <- wave.variance(null_wave1);\n-                \t\tm2_null <- vector(length = null.levels);\n-                \t\tfor(level in 1:null.levels){\n-                        \tm2_null[level] <- var_null[level, 1] + (mean(null_diff)^2);\n-                \t\t}\n-                \t\tnull= rbind(null, m2_null);\n-            \t\t}\n-                \n-            \t\tnull <- apply(null, 2, sort, na.last = TRUE);\n-            \t\tm2_25 <- null[25,];\n-            \t\tm2_975 <- null[975,];\n-            \t\tmed <- apply(null, 2, median, na.rm = TRUE);\n-\n-            \t\t# plot\n-            \t\tresults <- cbind(m2.dwt, m2_25, m2_975);\n-            \t\tmatplot(results, type = \\"b\\", pch = \\"*\\", lty = 1, col = c(1, 2, 2), xlab = \\"Wavelet Scale\\", ylab = c(\\"Wavelet 2nd Moment\\", test), main = (names.short[i]), cex.main = 0.75);\n-            \t\tabline(h = 1);\n-\n-            \t\t# get pvalues by comparison to null distribution\n-            \t\tout <- c(names.short[i]);\n-            \t\tfor (m in 1:length(m2.dwt)){\n-                    \tprint(paste(\\"scale\\", m, sep = \\" \\"));\n-                        print(paste(\\"m2\\", m2.dwt[m], sep = \\" \\"));\n-                        print(paste(\\"median\\", med[m], sep = \\" \\"));\n-                        out <- c(out, format(m2.dwt[m], digits = 4));\t\n-                        pv = NULL;\n-                        if(is.na(m2.dwt[m])){\n-                        \tpv <- \\"NA\\"; \n-                        } \n-                        else {\n-                        \tif (m2.dwt[m] >= med[m]){\n-                            \t# R tail test\n-                                tail <- \\"R\\";\n-                                pv <- (length(which(null[, m] >= m2.dwt[m])))/(length(na.exclude(null[, m])));\n-                            }\n-                            else{\n-                                if (m2.dwt[m] < med[m]){\n-                                \t# L tail test\n-                                    tail <- \\"L\\";\n-                                    pv <- (length(which(null[, m] <= m2.dwt[m])))/(length(na.exclude(null[, m])));\n-                                }\n-                            }\n-                        }\n-                        out <- c(out, pv);\n-                        print(pv);  \n-                        out <- c(out, tail);\n-                    }\n-                    final_pvalue <-rbind(final_pvalue, out);\n-                    print(out);\n-                }\n-            }\n-            \n-            colnames(final_pvalue) <- title;\n-            write.table(final_pvalue, file = table, sep = \\"\\\\t\\", quote = FALSE, row.names = FALSE);\n-            dev.off();\n-        }\\n";\n-\n-print Rcmd "\n-        # execute\n-        # read in data \n-        \n-        inputData <- read.delim(\\"$firstInputFile\\");\n-        inputDataNames <- colnames(inputData);\n-        \n-        controlData <- read.delim(\\"$secondInputFile\\");\n-        controlDataNames <- colnames(controlData);\n-        \n-        # call the test function to implement IvC test\n-        dwt_cor(inputData, inputDataNames, controlData, controlDataNames, test = \\"$test\\", pdf = \\"$secondOutputFile\\", table = \\"$firstOutputFile\\");\n-        print (\\"done with the correlation test\\");\n-\\n";\n-\n-print Rcmd "#eof\\n";\n-\n-close Rcmd;\n-\n-system("echo \\"wavelet IvC test started on \\`hostname\\` at \\`date\\`\\"\\n");\n-system("R --no-restore --no-save --no-readline < $r_script > $r_script.out\\n");\n-system("echo \\"wavelet IvC test ended on \\`hostname\\` at \\`date\\`\\"\\n");\n-\n-#close the input and output and error files\n-close(ERROR);\n-close(OUTPUT2);\n-close(OUTPUT1);\n-close(INPUT2);\n-close(INPUT1);\n\\ No newline at end of file\n'
b
diff -r 0b89b03ad760 -r 506ae7b0d85d execute_dwt_IvC_all.xml
--- a/execute_dwt_IvC_all.xml Mon Jan 27 09:25:56 2014 -0500
+++ b/execute_dwt_IvC_all.xml Mon Jul 06 20:31:56 2020 -0400
b
@@ -1,20 +1,35 @@
-<tool id="compute_p-values_second_moments_feature_occurrences_between_two_datasets_using_discrete_wavelet_transfom" name="Compute P-values and Second Moments for Feature Occurrences" version="1.0.0">
+<tool id="compute_p-values_second_moments_feature_occurrences_between_two_datasets_using_discrete_wavelet_transfom" name="Compute P-values and Second Moments for Feature Occurrences" version="1.0.1">
   <description>between two datasets using Discrete Wavelet Transfoms</description>
-  
-  <command interpreter="perl">
-   execute_dwt_IvC_all.pl $inputFile1 $inputFile2 $outputFile1 $outputFile2
+  <requirements>
+    <requirement type="package" version="1.7.5">r-waveslim</requirement>
+    <requirement type="package" version="4.6.8">r-wavethresh</requirement>
+  </requirements>
+  <command detect_errors="exit_code">
+    Rscript --vanilla '$__tool_directory__/execute_dwt_IvC_all.R'
+      '$inputFile1'
+      '$inputFile2'
+      '$outputFile2'
+      '$outputFile1'
   </command>
-
   <inputs>
-   <param format="tabular" name="inputFile1" type="data" label="Select the first input file"/>
-   <param format="tabular" name="inputFile2" type="data" label="Select the second input file"/>
+    <param format="tabular" name="inputFile1" type="data" label="Select the first input file"/>
+    <param format="tabular" name="inputFile2" type="data" label="Select the second input file"/>
   </inputs>
-  
   <outputs>
-    <data format="tabular" name="outputFile1"/> 
-    <data format="pdf" name="outputFile2"/>
+    <data format="tabular" name="outputFile1" label="${tool.name} on ${on_string}: statistics"/> 
+    <data format="pdf" name="outputFile2" label="${tool.name} on ${on_string}: pdf"/>
   </outputs>
-  
+  <tests>
+    <test>
+      <param ftype="tabular" name="inputFile1" value="in1.tsv"/>
+      <param ftype="tabular" name="inputFile2" value="in2.tsv"/>
+      <output name="outputFile1" ftype="tabular">
+        <assert_contents><has_line_matching expression="^motif\t1_moment2.*"/></assert_contents>
+        <assert_contents><has_line_matching expression="^translinTarget.*" /></assert_contents>
+      </output>
+      <output name="outputFile2" ftype="pdf" file="out2.pdf" compare="sim_size"/>
+    </test>
+  </tests>
   <help> 
 
 .. class:: infomark
b
diff -r 0b89b03ad760 -r 506ae7b0d85d test-data/in1.tsv
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/in1.tsv Mon Jul 06 20:31:56 2020 -0400
b
@@ -0,0 +1,17 @@
+deletionHoptspot insertionHoptspot dnaPolPauseFrameshift topoisomeraseCleavageSite translinTarget
+269 366 330 238 1129
+239 328 327 283 1188
+254 351 358 297 1151
+262 371 355 256 1107
+254 361 352 234 1192
+265 354 367 240 1182
+255 359 333 235 1217
+271 389 387 272 1241
+240 305 341 249 1159
+272 351 337 257 1169
+275 351 337 233 1158
+305 331 361 253 1172
+277 341 343 253 1113
+266 362 355 267 1162
+235 326 329 241 1230
+254 335 360 251 1172
b
diff -r 0b89b03ad760 -r 506ae7b0d85d test-data/in2.tsv
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/in2.tsv Mon Jul 06 20:31:56 2020 -0400
b
@@ -0,0 +1,17 @@
+deletionHoptspot insertionHoptspot dnaPolPauseFrameshift topoisomeraseCleavageSite translinTarget
+104 146 142 113 478
+89 146 151 94 495
+100 176 151 88 435
+96 163 128 114 468
+99 138 144 91 513
+112 126 162 106 468
+86 127 145 83 491
+104 145 171 110 496
+91 121 147 104 469
+103 141 145 98 458
+92 134 142 117 468
+97 146 145 107 471
+115 121 136 109 470
+113 135 138 101 491
+111 150 138 102 451
+94 128 151 138 481
b
diff -r 0b89b03ad760 -r 506ae7b0d85d test-data/out2.pdf
b
Binary file test-data/out2.pdf has changed