comparison univariate_wrapper.R @ 0:ab2ee3414e4e draft

planemo upload for repository https://github.com/workflow4metabolomics/univariate.git commit 98e8f4464b2f7321acb010e26e2a1c82fe37096e
author ethevenot
date Tue, 24 Oct 2017 08:57:25 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ab2ee3414e4e
1 #!/usr/bin/env Rscript
2
3 library(batch) ## parseCommandArgs
4
5 # Constants
6 argv <- commandArgs(trailingOnly = FALSE)
7 script.path <- sub("--file=","",argv[grep("--file=",argv)])
8 prog.name <- basename(script.path)
9
10 # Print help
11 if (length(grep('-h', argv)) >0) {
12 cat("Usage:", prog.name,
13 "dataMatrix_in myDataMatrix.tsv",
14 "sampleMetadata_in mySampleData.tsv",
15 "variableMetadata_in myVariableMetadata.tsv",
16 "facC qual",
17 "tesC kruskal",
18 "adjC fdr",
19 "thrN 0.05",
20 "variableMetadata_out myVariableMetadata_out.tsv",
21 "figure figure.pdf",
22 "information information.txt",
23 "\n")
24 quit(status = 0)
25 }
26
27 source_local <- function(fname){
28 argv <- commandArgs(trailingOnly = FALSE)
29 base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
30 source(paste(base_dir, fname, sep="/"))
31 }
32
33 source_local("univariate_script.R")
34
35 argVc <- unlist(parseCommandArgs(evaluate=FALSE))
36
37 ##------------------------------
38 ## Initializing
39 ##------------------------------
40
41 ## options
42 ##--------
43
44 strAsFacL <- options()$stringsAsFactors
45 options(stringsAsFactors = FALSE)
46
47 ## packages
48 ##---------
49
50 library(PMCMR)
51
52 ## constants
53 ##----------
54
55 modNamC <- "Univariate" ## module name
56
57 topEnvC <- environment()
58 flagC <- "\n"
59
60 ## functions
61 ##----------
62
63 flgF <- function(tesC,
64 envC = topEnvC,
65 txtC = NA) { ## management of warning and error messages
66
67 tesL <- eval(parse(text = tesC), envir = envC)
68
69 if(!tesL) {
70
71 sink(NULL)
72 stpTxtC <- ifelse(is.na(txtC),
73 paste0(tesC, " is FALSE"),
74 txtC)
75
76 stop(stpTxtC,
77 call. = FALSE)
78
79 }
80
81 } ## flgF
82
83 ## log file
84 ##---------
85
86 sink(argVc["information"])
87
88 cat("\nStart of the '", modNamC, "' Galaxy module call: ",
89 format(Sys.time(), "%a %d %b %Y %X"), "\n", sep="")
90
91 ## loading
92 ##--------
93
94 datMN <- t(as.matrix(read.table(argVc["dataMatrix_in"],
95 check.names = FALSE,
96 header = TRUE,
97 row.names = 1,
98 sep = "\t")))
99
100 samDF <- read.table(argVc["sampleMetadata_in"],
101 check.names = FALSE,
102 header = TRUE,
103 row.names = 1,
104 sep = "\t")
105
106 varDF <- read.table(argVc["variableMetadata_in"],
107 check.names = FALSE,
108 header = TRUE,
109 row.names = 1,
110 sep = "\t")
111
112 tesC <- argVc["tesC"]
113
114 ## checking
115 ##---------
116
117 flgF("identical(rownames(datMN), rownames(samDF))", txtC = "Column names of the dataMatrix are not identical to the row names of the sampleMetadata; check your data with the 'Check Format' module in the 'Quality Control' section")
118 flgF("identical(colnames(datMN), rownames(varDF))", txtC = "Row names of the dataMatrix are not identical to the row names of the variableMetadata; check your data with the 'Check Format' module in the 'Quality Control' section")
119
120 flgF("argVc['facC'] %in% colnames(samDF)", txtC = paste0("Required factor of interest '", argVc['facC'], "' could not be found in the column names of the sampleMetadata"))
121 flgF("mode(samDF[, argVc['facC']]) %in% c('character', 'numeric')", txtC = paste0("The '", argVc['facC'], "' column of the sampleMetadata should contain either number only, or character only"))
122
123 flgF("!(tesC %in% c('ttest', 'wilcoxon')) || (mode(samDF[, argVc['facC']]) == 'character' && length(unique(samDF[, argVc['facC']])) == 2)", txtC = paste0("For 'ttest' and 'wilcoxon', the chosen factor column ('", argVc['facC'], "') of the sampleMetadata should contain characters with only two different classes"))
124 flgF("!(tesC %in% c('anova', 'kruskal')) || (mode(samDF[, argVc['facC']]) == 'character' && length(unique(samDF[, argVc['facC']])) > 2)", txtC = paste0("For 'anova' and 'kruskal', the chosen factor column ('", argVc['facC'], "') of the sampleMetadata should contain characters with at least three different classes"))
125 flgF("!(tesC %in% c('pearson', 'spearman')) || mode(samDF[, argVc['facC']]) == 'numeric'", txtC = paste0("For 'pearson' and 'spearman', the chosen factor column ('", argVc['facC'], "') of the sampleMetadata should contain numbers only"))
126
127 flgF("argVc['adjC'] %in% c('holm', 'hochberg', 'hommel', 'bonferroni', 'BH', 'BY', 'fdr', 'none')")
128
129 flgF("0 <= as.numeric(argVc['thrN']) && as.numeric(argVc['thrN']) <= 1",
130 txtC = "(corrected) p-value threshold must be between 0 and 1")
131
132
133 ##------------------------------
134 ## Computation
135 ##------------------------------
136
137
138 varDF <- univariateF(datMN = datMN,
139 samDF = samDF,
140 varDF = varDF,
141 facC = argVc["facC"],
142 tesC = tesC,
143 adjC = argVc["adjC"],
144 thrN = as.numeric(argVc["thrN"]),
145 pdfC = argVc["figure"])
146
147
148 ##------------------------------
149 ## Ending
150 ##------------------------------
151
152
153 ## saving
154 ##--------
155
156 varDF <- cbind.data.frame(variableMetadata = rownames(varDF),
157 varDF)
158
159 write.table(varDF,
160 file = argVc["variableMetadata_out"],
161 quote = FALSE,
162 row.names = FALSE,
163 sep = "\t")
164
165 ## closing
166 ##--------
167
168 cat("\nEnd of '", modNamC, "' Galaxy module call: ",
169 as.character(Sys.time()), "\n", sep = "")
170
171 cat("\n\n\n============================================================================")
172 cat("\nAdditional information about the call:\n")
173 cat("\n1) Parameters:\n")
174 print(cbind(value = argVc))
175
176 cat("\n2) Session Info:\n")
177 sessioninfo <- sessionInfo()
178 cat(sessioninfo$R.version$version.string,"\n")
179 cat("Main packages:\n")
180 for (pkg in names(sessioninfo$otherPkgs)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n")
181 cat("Other loaded packages:\n")
182 for (pkg in names(sessioninfo$loadedOnly)) { cat(paste(pkg,packageVersion(pkg)),"\t") }; cat("\n")
183
184 cat("============================================================================\n")
185
186 sink()
187
188 options(stringsAsFactors = strAsFacL)
189
190 rm(list = ls())