comparison execute_dwt_cor_aVa_perClass.R @ 2:b87bbe6bc044 draft default tip

"planemo upload for repository https://github.com/galaxyproject/tools-devteam/tree/master/tools/dwt_cor_ava_perclass commit f929353ffb0623f2218d7dec459c7da62f3b0d24"
author devteam
date Mon, 06 Jul 2020 20:28:54 -0400
parents
children
comparison
equal deleted inserted replaced
1:a0defff5cf89 2:b87bbe6bc044
1 ##################################################################################
2 ## code to do all correlation tests of form: motif(a) vs. motif(a)
3 ## add code to create null bands by permuting the original data series
4 ## generate plots and table matrix of correlation coefficients including p-values
5 ##################################################################################
6 library("wavethresh");
7 library("waveslim");
8
9 options(echo = FALSE)
10
11 ## normalize data
12 norm <- function(data) {
13 v <- (data - mean(data)) / sd(data);
14 if (sum(is.na(v)) >= 1) {
15 v <- data;
16 }
17 return(v);
18 }
19
20 dwt_cor <- function(data_short, names_short, data_long, names_long, test, pdf, table, filter = 4, bc = "symmetric", method = "kendall", wf = "haar", boundary = "reflection") {
21 print(test);
22 print(pdf);
23 print(table);
24
25 pdf(file = pdf);
26 final_pvalue <- NULL;
27 title <- NULL;
28
29 short_levels <- wavethresh::wd(data_short[, 1], filter.number = filter, bc = bc)$nlevels;
30 title <- c("motif");
31 for (i in 1:short_levels) {
32 title <- c(title, paste(i, "cor", sep = "_"), paste(i, "pval", sep = "_"));
33 }
34 print(title);
35
36 ## normalize the raw data
37 data_short <- apply(data_short, 2, norm);
38 data_long <- apply(data_long, 2, norm);
39
40 for (i in seq_len(length(names_short))) {
41 ## Kendall Tau
42 ## DWT wavelet correlation function
43 ## include significance to compare
44 wave1_dwt <- NULL;
45 wave2_dwt <- NULL;
46 tau_dwt <- NULL;
47 out <- NULL;
48
49 print(names_short[i]);
50 print(names_long[i]);
51
52 ## need exit if not comparing motif(a) vs motif(a)
53 if (names_short[i] != names_long[i]) {
54 stop(paste("motif", names_short[i], "is not the same as", names_long[i], sep = " "));
55 }
56 else {
57 wave1_dwt <- waveslim::dwt(data_short[, i], wf = wf, short_levels, boundary = boundary);
58 wave2_dwt <- waveslim::dwt(data_long[, i], wf = wf, short_levels, boundary = boundary);
59 tau_dwt <- vector(length = short_levels)
60
61 ## perform cor test on wavelet coefficients per scale
62 for (level in 1:short_levels) {
63 w1_level <- NULL;
64 w2_level <- NULL;
65 w1_level <- (wave1_dwt[[level]]);
66 w2_level <- (wave2_dwt[[level]]);
67 tau_dwt[level] <- cor.test(w1_level, w2_level, method = method)$estimate;
68 }
69
70 ## CI bands by permutation of time series
71 feature1 <- NULL;
72 feature2 <- NULL;
73 feature1 <- data_short[, i];
74 feature2 <- data_long[, i];
75 null <- NULL;
76 results <- NULL;
77 med <- NULL;
78 cor_25 <- NULL;
79 cor_975 <- NULL;
80
81 for (k in 1:1000) {
82 nk_1 <- NULL;
83 nk_2 <- NULL;
84 null_levels <- NULL;
85 cor <- NULL;
86 null_wave1 <- NULL;
87 null_wave2 <- NULL;
88
89 nk_1 <- sample(feature1, length(feature1), replace = FALSE);
90 nk_2 <- sample(feature2, length(feature2), replace = FALSE);
91 null_levels <- wavethresh::wd(nk_1, filter.number = filter, bc = bc)$nlevels;
92 cor <- vector(length = null_levels);
93 null_wave1 <- waveslim::dwt(nk_1, wf = wf, short_levels, boundary = boundary);
94 null_wave2 <- waveslim::dwt(nk_2, wf = wf, short_levels, boundary = boundary);
95
96 for (level in 1:null_levels) {
97 null_level1 <- NULL;
98 null_level2 <- NULL;
99 null_level1 <- (null_wave1[[level]]);
100 null_level2 <- (null_wave2[[level]]);
101 cor[level] <- cor.test(null_level1, null_level2, method = method)$estimate;
102 }
103 null <- rbind(null, cor);
104 }
105
106 null <- apply(null, 2, sort, na.last = TRUE);
107 print(paste("NAs", length(which(is.na(null))), sep = " "));
108 cor_25 <- null[25, ];
109 cor_975 <- null[975, ];
110 med <- (apply(null, 2, median, na.rm = TRUE));
111
112 ## plot
113 results <- cbind(tau_dwt, cor_25, cor_975);
114 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);
115 abline(h = 0);
116
117 ## get pvalues by comparison to null distribution
118 ### modify pval calculation for error type II of T test ####
119 out <- (names_short[i]);
120 for (m in seq_len(length(tau_dwt))) {
121 print(paste("scale", m, sep = " "));
122 print(paste("tau", tau_dwt[m], sep = " "));
123 print(paste("med", med[m], sep = " "));
124 out <- c(out, format(tau_dwt[m], digits = 3));
125 pv <- NULL;
126 if (is.na(tau_dwt[m])) {
127 pv <- "NA";
128 }
129 else {
130 if (tau_dwt[m] >= med[m]) {
131 ## R tail test
132 print(paste("R"));
133 ### per sv ok to use inequality not strict
134 pv <- (length(which(null[, m] >= tau_dwt[m]))) / (length(na.exclude(null[, m])));
135 if (tau_dwt[m] == med[m]) {
136 print("tau == med");
137 print(summary(null[, m]));
138 }
139 }
140 else if (tau_dwt[m] < med[m]) {
141 ## L tail test
142 print(paste("L"));
143 pv <- (length(which(null[, m] <= tau_dwt[m]))) / (length(na.exclude(null[, m])));
144 }
145 }
146 out <- c(out, pv);
147 print(paste("pval", pv, sep = " "));
148 }
149 final_pvalue <- rbind(final_pvalue, out);
150 print(out);
151 }
152 }
153 colnames(final_pvalue) <- title;
154 write.table(final_pvalue, file = table, sep = "\\t", quote = FALSE, row.names = FALSE)
155 dev.off();
156 }
157 ## execute
158 ## read in data
159 args <- commandArgs(trailingOnly = TRUE)
160
161 input_data1 <- NULL;
162 input_data2 <- NULL;
163 input_data_short1 <- NULL;
164 input_data_short2 <- NULL;
165 input_data_names_short1 <- NULL;
166 input_data_names_short2 <- NULL;
167
168 input_data1 <- read.delim(args[1]);
169 input_data_short1 <- input_data1[, +c(seq_len(ncol(input_data1)))];
170 input_data_names_short1 <- colnames(input_data_short1);
171
172 input_data2 <- read.delim(args[2]);
173 input_data_short2 <- input_data2[, +c(seq_len(ncol(input_data2)))];
174 input_data_names_short2 <- colnames(input_data_short2);
175
176 ## cor test for motif(a) in input_data1 vs motif(a) in input_data2
177 dwt_cor(input_data_short1, input_data_names_short1, input_data_short2, input_data_names_short2, test = "cor_aVa", pdf = args[3], table = args[4]);
178 print("done with the correlation test");