comparison w4mclassfilter_wrapper.R @ 15:08d4ca8bc6dd draft

"planemo upload for repository https://github.com/HegemanLab/w4mclassfilter_galaxy_wrapper/tree/master commit 9639dde5737c9aa2330bb603c2299345939407cf"
author eschen42
date Thu, 11 Mar 2021 20:46:26 +0000
parents
children
comparison
equal deleted inserted replaced
14:1d36ecf93e67 15:08d4ca8bc6dd
1 #!/usr/bin/env Rscript
2
3 library(batch) ## parseCommandArgs
4
5 ########
6 # MAIN #
7 ########
8
9 argVc <- unlist(parseCommandArgs(evaluate=FALSE))
10
11 ##------------------------------
12 ## Initializing
13 ##------------------------------
14
15 ## options
16 ##--------
17
18 strAsFacL <- options()$stringsAsFactors
19 options(stringsAsFactors = FALSE)
20
21 ## libraries
22 ##----------
23
24 suppressMessages(library(w4mclassfilter))
25
26 expected_version <- "0.98.18"
27 actual_version <- packageVersion("w4mclassfilter")
28 if(packageVersion("w4mclassfilter") < expected_version) {
29 stop(
30 sprintf(
31 "Unrecoverable error: Version %s of the 'w4mclassfilter' R package was loaded instead of expected version %s",
32 actual_version, expected_version
33 )
34 )
35 }
36
37 ## constants
38 ##----------
39
40 modNamC <- "w4mclassfilter" ## module name
41
42 topEnvC <- environment()
43 flgC <- "\n"
44
45 ## functions
46 ##----------
47
48 flgF <- function(tesC,
49 envC = topEnvC,
50 txtC = NA) { ## management of warning and error messages
51
52 tesL <- eval(parse(text = tesC), envir = envC)
53
54 if(!tesL) {
55
56 #sink(NULL)
57 stpTxtC <- ifelse(is.na(txtC),
58 paste0(tesC, " is FALSE"),
59 txtC)
60
61 stop(stpTxtC,
62 call. = FALSE)
63
64 }
65
66 } ## flgF
67
68
69 ## log file
70 ##---------
71
72 my_print <- function(x, ...) { cat(c(x, ...))}
73
74 my_print("\nStart of the '", modNamC, "' Galaxy module call: ",
75 format(Sys.time(), "%a %d %b %Y %X"), "\n", sep="")
76
77 ## arguments
78 ##----------
79
80 # files
81
82 dataMatrix_in <- as.character(argVc["dataMatrix_in"])
83 dataMatrix_out <- as.character(argVc["dataMatrix_out"])
84
85 sampleMetadata_in <- as.character(argVc["sampleMetadata_in"])
86 sampleMetadata_out <- as.character(argVc["sampleMetadata_out"])
87
88 variableMetadata_in <- as.character(argVc["variableMetadata_in"])
89 variableMetadata_out <- as.character(argVc["variableMetadata_out"])
90
91 # other parameters
92
93 transformation <- as.character(argVc["transformation"])
94 my_imputation_label <- as.character(argVc["imputation"])
95 my_imputation_function <- if (my_imputation_label == "zero") {
96 w4m_filter_zero_imputation
97 } else if (my_imputation_label == "center") {
98 w4m_filter_median_imputation
99 } else if (my_imputation_label == "none") {
100 w4m_filter_no_imputation
101 } else {
102 stop(sprintf("Unknown value %s supplied for 'imputation' parameter. Expected one of {zero,center,none}."))
103 }
104 wildcards <- as.logical(argVc["wildcards"])
105 sampleclassNames <- as.character(argVc["sampleclassNames"])
106 sampleclassNames <- strsplit(x = sampleclassNames, split = ",", fixed = TRUE)[[1]]
107 if (wildcards) {
108 sampleclassNames <- gsub("[.]", "[.]", sampleclassNames)
109 sampleclassNames <- utils::glob2rx(sampleclassNames, trim.tail = FALSE)
110 }
111 inclusive <- as.logical(argVc["inclusive"])
112 classnameColumn <- as.character(argVc["classnameColumn"])
113 samplenameColumn <- as.character(argVc["samplenameColumn"])
114
115 order_vrbl <- as.character(argVc["order_vrbl"])
116 centering <- as.character(argVc["centering"])
117 order_smpl <-
118 if (centering == 'centroid' || centering == 'median') {
119 "sampleMetadata"
120 } else {
121 as.character(argVc["order_smpl"])
122 }
123
124 variable_range_filter <- as.character(argVc["variable_range_filter"])
125 variable_range_filter <- strsplit(x = variable_range_filter, split = ",", fixed = TRUE)[[1]]
126
127 ## -----------------------------
128 ## Transformation and imputation
129 ## -----------------------------
130 my_transformation_and_imputation <- if (transformation == "log10") {
131 function(m) {
132 # convert negative intensities to missing values
133 m[m < 0] <- NA
134 if (!is.matrix(m))
135 stop("Cannot transform and impute data - the supplied data is not in matrix form")
136 if (nrow(m) == 0)
137 stop("Cannot transform and impute data - data matrix has no rows")
138 if (ncol(m) == 0)
139 stop("Cannot transform and impute data - data matrix has no columns")
140 suppressWarnings({
141 # suppress warnings here since non-positive values will produce NaN's that will be fixed in the next step
142 m <- log10(m)
143 m[is.na(m)] <- NA
144 })
145 return ( my_imputation_function(m) )
146 }
147 } else if (transformation == "log2") {
148 function(m) {
149 # convert negative intensities to missing values
150 m[m < 0] <- NA
151 if (!is.matrix(m))
152 stop("Cannot transform and impute data - the supplied data is not in matrix form")
153 if (nrow(m) == 0)
154 stop("Cannot transform and impute data - data matrix has no rows")
155 if (ncol(m) == 0)
156 stop("Cannot transform and impute data - data matrix has no columns")
157 suppressWarnings({
158 # suppress warnings here since non-positive values will produce NaN's that will be fixed in the next step
159 m <- log2(m)
160 m[is.na(m)] <- NA
161 })
162 return ( my_imputation_function(m) )
163 }
164 } else {
165 function(m) {
166 # convert negative intensities to missing values
167 m[m < 0] <- NA
168 if (!is.matrix(m))
169 stop("Cannot transform and impute data - the supplied data is not in matrix form")
170 if (nrow(m) == 0)
171 stop("Cannot transform and impute data - data matrix has no rows")
172 if (ncol(m) == 0)
173 stop("Cannot transform and impute data - data matrix has no columns")
174 suppressWarnings({
175 # suppress warnings here since non-positive values will produce NaN's that will be fixed in the next step
176 m[is.na(m)] <- NA
177 })
178 return ( my_imputation_function(m) )
179 }
180 }
181
182 ##------------------------------
183 ## Computation
184 ##------------------------------
185
186 result <- w4m_filter_by_sample_class(
187 dataMatrix_in = dataMatrix_in
188 , sampleMetadata_in = sampleMetadata_in
189 , variableMetadata_in = variableMetadata_in
190 , dataMatrix_out = dataMatrix_out
191 , sampleMetadata_out = sampleMetadata_out
192 , variableMetadata_out = variableMetadata_out
193 , classes = sampleclassNames
194 , include = inclusive
195 , class_column = classnameColumn
196 , samplename_column = samplenameColumn
197 , order_vrbl = order_vrbl
198 , order_smpl = order_smpl
199 , centering = centering
200 , variable_range_filter = variable_range_filter
201 , failure_action = my_print
202 , data_imputation = my_transformation_and_imputation
203 )
204
205 my_print("\nResult of '", modNamC, "' Galaxy module call to 'w4mclassfilter::w4m_filter_by_sample_class' R function: ",
206 as.character(result), "\n", sep = "")
207
208 ##--------
209 ## Closing
210 ##--------
211
212 my_print("\nEnd of '", modNamC, "' Galaxy module call: ",
213 as.character(Sys.time()), "\n", sep = "")
214
215 #sink()
216
217 if (!file.exists(dataMatrix_out)) {
218 print(sprintf("ERROR %s::w4m_filter_by_sample_class - file '%s' was not created", modNamC, dataMatrix_out))
219 }# else { print(sprintf("INFO %s::w4m_filter_by_sample_class - file '%s' was exists", modNamC, dataMatrix_out)) }
220
221 if (!file.exists(variableMetadata_out)) {
222 print(sprintf("ERROR %s::w4m_filter_by_sample_class - file '%s' was not created", modNamC, variableMetadata_out))
223 } # else { print(sprintf("INFO %s::w4m_filter_by_sample_class - file '%s' was exists", modNamC, variableMetadata_out)) }
224
225 if (!file.exists(sampleMetadata_out)) {
226 print(sprintf("ERROR %s::w4m_filter_by_sample_class - file '%s' was not created", modNamC, sampleMetadata_out))
227 } # else { print(sprintf("INFO %s::w4m_filter_by_sample_class - file '%s' was exists", modNamC, sampleMetadata_out)) }
228
229 if( !result ) {
230 stop(sprintf("ERROR %s::w4m_filter_by_sample_class - method failed", modNamC))
231 }
232
233 rm(list = ls())