annotate waveica_wrapper.R @ 0:2461d20911c9 draft

"planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
author recetox
date Thu, 18 Mar 2021 15:53:38 +0000
parents
children 2bcfd5b450bb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
1 waveica <- function(
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
2 data,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
3 wavelet_filter,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
4 wavelet_length,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
5 k,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
6 t,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
7 t2,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
8 alpha,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
9 exclude_blanks
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
10 ) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
11
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
12 # get input from the Galaxy, preprocess data
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
13 data <- read.csv(data, header = TRUE, row.names = "sample_name")
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
14 data <- preprocess_data(data)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
15
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
16 # remove blanks from dataset
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
17 if (exclude_blanks) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
18 data <- exclude_group(data)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
19 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
20
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
21 # separate data into features, batch and group
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
22 features <- data[, -c(1:4)]
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
23 group <- as.numeric(data$class)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
24 batch <- data$batch
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
25
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
26 # run WaveICA
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
27 normalized_data <- WaveICA::WaveICA(
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
28 data = features,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
29 wf = get_wf(wavelet_filter, wavelet_length),
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
30 batch = batch,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
31 group = group,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
32 K = k,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
33 t = t,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
34 t2 = t2,
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
35 alpha = alpha
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
36 )
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
37
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
38 return(normalized_data)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
39 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
40
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
41
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
42 # Sort data, set numerical values for groups
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
43 preprocess_data <- function(data) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
44 # sort data by injection order
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
45 data <- data[order(data$injectionOrder, decreasing = FALSE), ]
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
46
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
47 data$class[data$class == "blank"] <- 0
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
48 data$class[data$class == "sample"] <- 1
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
49 data$class[data$class == "QC"] <- 2
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
50
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
51 return(data)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
52 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
53
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
54
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
55 # Create appropriate input for R wavelets function
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
56 get_wf <- function(wavelet_filter, wavelet_length) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
57 wf <- paste(wavelet_filter, wavelet_length, sep = "")
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
58
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
59 # exception to the wavelet function
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
60 if (wf == "d2") {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
61 wf <- "haar"
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
62 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
63
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
64 return(wf)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
65 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
66
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
67
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
68 # Exclude blanks from a dataframe
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
69 exclude_group <- function(data) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
70 row_idx_to_exclude <- which(data$class %in% 0)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
71 if (length(row_idx_to_exclude) > 1) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
72 data_without_blanks <- data[-c(row_idx_to_exclude), ]
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
73 msg <- paste("Blank samples have been excluded from the dataframe.\n")
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
74 cat(msg)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
75 return(data_without_blanks)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
76 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
77 else {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
78 return(data)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
79 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
80 }
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
81
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
82
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
83 # Store output of WaveICA in a tsv file
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
84 store_data <- function(normalized_data, output) {
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
85 write.table(normalized_data, file = output, sep = "\t", col.names = NA)
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
86 cat("Normalization has been completed.\n")
2461d20911c9 "planemo upload for repository https://github.com/RECETOX/galaxytools/tree/master/tools/waveica commit 91376ea7a6736351b0cc086ca1bc6c553fdcda97"
recetox
parents:
diff changeset
87 }