comparison withinvariation-26603602a823/mixomics_multilevel.r @ 0:5086ad0c0992 draft default tip

Uploaded v0.4
author yguitton
date Fri, 05 May 2017 05:04:36 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5086ad0c0992
1 #!/usr/bin/env Rscript
2
3 ###############################################################################
4 #
5 # mixOmics multilevel function
6 #
7 # This script is written specifically for the mixOmics web-interface
8 # using the Galaxy system.
9 #
10 # R-Package: mixOmics
11 #
12 # Version: 1.2.3
13 #
14 # Author (wrapper): Xin-Yi Chua (xinyi.chua@qfab.org)
15 # Author (mixOmics.multilevel): Benoit Liquet, Kim-Anh Le Cao
16 # Author (warpper & .r adaptation for workflow4metabolomics.org): Yann GUITTON
17 #
18 # Expected parameters from the commandline
19 # input files:
20 # dataMatrix
21 # sampleMetadata
22 # params:
23 # respL (respL for one level & respL1 & respL2 for 2 levels)
24 # trans (need log2 or log10 transformation made before withinVar)
25 # scaling
26 # centering
27 # output files:
28 # dataMatrix_out (after withinVariation correction )
29 # result (Robject)
30 ################################################################################
31
32 #Redirect all stdout to the log file
33 log_file=file("multilevel.log", open = "wt")
34 sink(log_file)
35 sink(log_file, type = "output")
36
37 #remove rgl warning
38 options(rgl.useNULL = TRUE)
39
40 # ----- PACKAGE -----
41 cat("\tPACKAGE INFO\n")
42
43 pkgs=c("mixOmics","batch","pcaMethods")
44 for(pkg in pkgs) {
45 suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
46 cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
47 }
48
49
50 source_local <- function(fname) {
51 argv <- commandArgs(trailingOnly = FALSE)
52 base_dir <- dirname(substring(argv[grep("--file=", argv)], 8))
53 source(paste(base_dir, fname, sep="/"))
54 }
55
56
57
58 #load transformation function
59 source_local("transformation_script.R")
60 # source("transformation_script.R")
61 print("first loadings OK")
62
63
64 listArguments = parseCommandArgs(evaluate=FALSE) #interpretation of arguments given in command line as an R list of objects
65 print(listArguments)
66
67 ## libraries
68 ##----------
69
70 cat('\n\nRunning mixomics_multilevel.r\n');
71
72 options(warn=-1);
73 ##suppressPackageStartupMessages(library(mixOmics)); #not needed?
74
75
76 ## constants
77 ##----------
78
79 modNamC <- "Multilevel" ## module name
80
81 topEnvC <- environment()
82 flgC <- "\n"
83
84 ## functions
85 ##----------For manual input of function
86 ##--end function
87
88 flgF <- function(tesC,
89 envC = topEnvC,
90 txtC = NA) { ## management of warning and error messages
91
92 tesL <- eval(parse(text = tesC), envir = envC)
93
94 if(!tesL) {
95
96 sink(NULL)
97 stpTxtC <- ifelse(is.na(txtC),
98 paste0(tesC, " is FALSE"),
99 txtC)
100
101 stop(stpTxtC,
102 call. = FALSE)
103
104 }
105
106 } ## flgF
107
108
109 ## log file
110 ##---------
111
112
113 cat("\nStart of the '", modNamC, "' Galaxy module call: ",
114 format(Sys.time(), "%a %d %b %Y %X"), "\n", sep="")
115
116
117 ## arguments
118 ##----------
119
120 ## loading files and checks
121 xMN <- t(as.matrix(read.table(listArguments[["dataMatrix_in"]],
122 check.names = FALSE,
123 header = TRUE,
124 row.names = 1,
125 sep = "\t")))
126
127 samDF <- read.table(listArguments[["sampleMetadata_in"]],
128 check.names = FALSE,
129 header = TRUE,
130 row.names = 1,
131 sep = "\t")
132 flgF("identical(rownames(xMN), rownames(samDF))", txtC = "Sample names (or number) in the data matrix (first row) and sample metadata (first column) are not identical; use the 'Check Format' module in the 'Quality Control' section")
133
134 ##Here Add transformation scripts if trans<>none
135 if (listArguments[["transfo"]]=="go"){
136 cat("\n Start transformation with trans=",listArguments[["trans"]]," scale=",listArguments[["scale"]]," center=",listArguments[["center"]],"\n", sep="")
137 if (listArguments[["trans"]]!="none"){
138 metC <- listArguments[["trans"]]
139 xMN <- transformF(datMN = xMN, ## dataMatrix
140 metC = metC) ## transformation method
141 }
142 if (listArguments[["center"]]=="true"){
143 listArguments[["center"]]<-TRUE
144 }else{
145 listArguments[["center"]]<-FALSE
146 }
147
148 xMN<-prep(xMN, scale=listArguments[["scale"]],center=listArguments[["center"]])
149 }
150
151
152 ##end tranformation
153
154 if (listArguments[["respL2"]]!="NULL"){
155 cat("\n\nMultilevel (two levels)\n");
156 flgF("((listArguments[['respL1']] %in% colnames(samDF)) || (listArguments[['respL2']] %in% colnames(samDF)))", txtC = paste("Level argument (",listArguments[['respL2']]," ,",listArguments[['respL1']], ") must be one of the column names (first row) of your sample metadata", sep = ""))
157
158 tryCatch({
159 result <- withinVariation(xMN, design=samDF[,c(listArguments[["repmeasure"]],listArguments[["respL1"]],listArguments[["respL2"]])]);
160 }, error = function(err) {
161 stop(paste("There was an error when trying to run the Multilevel (two levels) function.\n\n",err));
162 });
163 } else {
164 cat("\n\nMultilevel (one level)\n");
165 flgF("(listArguments[['respL']] %in% colnames(samDF))", txtC = paste("Level argument (",listArguments[['respL']],") must be one of the column names (first row) of your sample metadata", sep = ""))
166
167 tryCatch({
168 result <- withinVariation(xMN, design=samDF[,c(listArguments[["repmeasure"]], listArguments[["respL"]])]);
169 }, error = function(err) {
170 stop(paste("There was an error when trying to run the Multilevel (one level) function.\n\n",err));
171 });
172 }
173
174
175 ##saving
176
177 if (exists("result")) {
178 ## writing output files
179 cat("\n\nWriting output files\n\n");
180 ## transpose matrix
181
182 datDF <- cbind.data.frame(dataMatrix = colnames(xMN),
183 as.data.frame(t(result)))
184 write.table(datDF,
185 file = "dataMatrix_out.tsv",
186 quote = FALSE,
187 row.names = FALSE,
188 sep = "\t")
189
190 tryCatch({
191 save(result, file="multilevel.RData");
192 }, warning = function(w) {
193 print(paste("Warning: ", w));
194 }, error = function(err) {
195 stop(paste("ERROR saving result RData object:", err));
196 });
197 }
198
199 ## ending
200 ##-------
201
202 cat("\nEnd of the '", modNamC, "' Galaxy module call: ",
203 format(Sys.time(), "%a %d %b %Y %X"), "\n", sep = "")
204
205 sink()
206
207 # options(stringsAsFactors = strAsFacL)
208
209
210 rm(list = ls())