comparison xcms.r @ 10:c81275aee959 draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit a6f5f18b3d6130f7d7fbb9f2df856838c6217797
author lecorguille
date Fri, 07 Apr 2017 07:35:31 -0400
parents 2c1d7df89cf6
children
comparison
equal deleted inserted replaced
9:2c1d7df89cf6 10:c81275aee959
13 # ----- PACKAGE ----- 13 # ----- PACKAGE -----
14 cat("\tPACKAGE INFO\n") 14 cat("\tPACKAGE INFO\n")
15 #pkgs=c("xcms","batch") 15 #pkgs=c("xcms","batch")
16 pkgs=c("parallel","BiocGenerics", "Biobase", "Rcpp", "mzR", "xcms","snow","batch") 16 pkgs=c("parallel","BiocGenerics", "Biobase", "Rcpp", "mzR", "xcms","snow","batch")
17 for(pkg in pkgs) { 17 for(pkg in pkgs) {
18 suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE))) 18 suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
19 cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="") 19 cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
20 } 20 }
21 source_local <- function(fname){ argv <- commandArgs(trailingOnly = FALSE); base_dir <- dirname(substring(argv[grep("--file=", argv)], 8)); source(paste(base_dir, fname, sep="/")) } 21 source_local <- function(fname){ argv <- commandArgs(trailingOnly = FALSE); base_dir <- dirname(substring(argv[grep("--file=", argv)], 8)); source(paste(base_dir, fname, sep="/")) }
22 cat("\n\n"); 22 cat("\n\n");
23 23
24 24
36 # ----- ARGUMENTS PROCESSING ----- 36 # ----- ARGUMENTS PROCESSING -----
37 cat("\tINFILE PROCESSING INFO\n") 37 cat("\tINFILE PROCESSING INFO\n")
38 38
39 #image is an .RData file necessary to use xset variable given by previous tools 39 #image is an .RData file necessary to use xset variable given by previous tools
40 if (!is.null(listArguments[["image"]])){ 40 if (!is.null(listArguments[["image"]])){
41 load(listArguments[["image"]]); listArguments[["image"]]=NULL 41 load(listArguments[["image"]]); listArguments[["image"]]=NULL
42 } 42 }
43 43
44 #Import the different functions 44 #Import the different functions
45 source_local("lib.r") 45 source_local("lib.r")
46 46
59 #saving the commun parameters 59 #saving the commun parameters
60 thefunction = listArguments[["xfunction"]]; listArguments[["xfunction"]]=NULL #delete from the list of arguments 60 thefunction = listArguments[["xfunction"]]; listArguments[["xfunction"]]=NULL #delete from the list of arguments
61 61
62 xsetRdataOutput = paste(thefunction,"RData",sep=".") 62 xsetRdataOutput = paste(thefunction,"RData",sep=".")
63 if (!is.null(listArguments[["xsetRdataOutput"]])){ 63 if (!is.null(listArguments[["xsetRdataOutput"]])){
64 xsetRdataOutput = listArguments[["xsetRdataOutput"]]; listArguments[["xsetRdataOutput"]]=NULL 64 xsetRdataOutput = listArguments[["xsetRdataOutput"]]; listArguments[["xsetRdataOutput"]]=NULL
65 } 65 }
66 66
67 #saving the specific parameters 67 #saving the specific parameters
68 rplotspdf = "Rplots.pdf" 68 rplotspdf = "Rplots.pdf"
69 if (!is.null(listArguments[["rplotspdf"]])){ 69 if (!is.null(listArguments[["rplotspdf"]])){
70 rplotspdf = listArguments[["rplotspdf"]]; listArguments[["rplotspdf"]]=NULL 70 rplotspdf = listArguments[["rplotspdf"]]; listArguments[["rplotspdf"]]=NULL
71 } 71 }
72 sampleMetadataOutput = "sampleMetadata.tsv" 72 sampleMetadataOutput = "sampleMetadata.tsv"
73 if (!is.null(listArguments[["sampleMetadataOutput"]])){ 73 if (!is.null(listArguments[["sampleMetadataOutput"]])){
74 sampleMetadataOutput = listArguments[["sampleMetadataOutput"]]; listArguments[["sampleMetadataOutput"]]=NULL 74 sampleMetadataOutput = listArguments[["sampleMetadataOutput"]]; listArguments[["sampleMetadataOutput"]]=NULL
75 } 75 }
76 variableMetadataOutput = "variableMetadata.tsv" 76 variableMetadataOutput = "variableMetadata.tsv"
77 if (!is.null(listArguments[["variableMetadataOutput"]])){ 77 if (!is.null(listArguments[["variableMetadataOutput"]])){
78 variableMetadataOutput = listArguments[["variableMetadataOutput"]]; listArguments[["variableMetadataOutput"]]=NULL 78 variableMetadataOutput = listArguments[["variableMetadataOutput"]]; listArguments[["variableMetadataOutput"]]=NULL
79 } 79 }
80 dataMatrixOutput = "dataMatrix.tsv" 80 dataMatrixOutput = "dataMatrix.tsv"
81 if (!is.null(listArguments[["dataMatrixOutput"]])){ 81 if (!is.null(listArguments[["dataMatrixOutput"]])){
82 dataMatrixOutput = listArguments[["dataMatrixOutput"]]; listArguments[["dataMatrixOutput"]]=NULL 82 dataMatrixOutput = listArguments[["dataMatrixOutput"]]; listArguments[["dataMatrixOutput"]]=NULL
83 } 83 }
84 if (!is.null(listArguments[["convertRTMinute"]])){ 84 if (!is.null(listArguments[["convertRTMinute"]])){
85 convertRTMinute = listArguments[["convertRTMinute"]]; listArguments[["convertRTMinute"]]=NULL 85 convertRTMinute = listArguments[["convertRTMinute"]]; listArguments[["convertRTMinute"]]=NULL
86 } 86 }
87 if (!is.null(listArguments[["numDigitsMZ"]])){ 87 if (!is.null(listArguments[["numDigitsMZ"]])){
88 numDigitsMZ = listArguments[["numDigitsMZ"]]; listArguments[["numDigitsMZ"]]=NULL 88 numDigitsMZ = listArguments[["numDigitsMZ"]]; listArguments[["numDigitsMZ"]]=NULL
89 } 89 }
90 if (!is.null(listArguments[["numDigitsRT"]])){ 90 if (!is.null(listArguments[["numDigitsRT"]])){
91 numDigitsRT = listArguments[["numDigitsRT"]]; listArguments[["numDigitsRT"]]=NULL 91 numDigitsRT = listArguments[["numDigitsRT"]]; listArguments[["numDigitsRT"]]=NULL
92 } 92 }
93 if (!is.null(listArguments[["intval"]])){ 93 if (!is.null(listArguments[["intval"]])){
94 intval = listArguments[["intval"]]; listArguments[["intval"]]=NULL 94 intval = listArguments[["intval"]]; listArguments[["intval"]]=NULL
95 } 95 }
96 96
97 if (thefunction %in% c("xcmsSet","retcor")) { 97 if (thefunction %in% c("xcmsSet","retcor")) {
98 ticspdf = listArguments[["ticspdf"]]; listArguments[["ticspdf"]]=NULL 98 ticspdf = listArguments[["ticspdf"]]; listArguments[["ticspdf"]]=NULL
99 bicspdf = listArguments[["bicspdf"]]; listArguments[["bicspdf"]]=NULL 99 bicspdf = listArguments[["bicspdf"]]; listArguments[["bicspdf"]]=NULL
100 } 100 }
101 101
102 #necessary to unzip .zip file uploaded to Galaxy 102
103 #thanks to .zip file it's possible to upload many file as the same time conserving the tree hierarchy of directories
104
105
106 if (!is.null(listArguments[["zipfile"]])){
107 zipfile= listArguments[["zipfile"]]; listArguments[["zipfile"]]=NULL
108 }
109
110 if (!is.null(listArguments[["library"]])){
111 directory=listArguments[["library"]]; listArguments[["library"]]=NULL
112 if(!file.exists(directory)){
113 error_message=paste("Cannot access the directory:",directory,". Please verify if the directory exists or not.")
114 print(error_message)
115 stop(error_message)
116 }
117 }
118
119 # We unzip automatically the chromatograms from the zip files.
120 if (thefunction %in% c("xcmsSet","retcor","fillPeaks")) { 103 if (thefunction %in% c("xcmsSet","retcor","fillPeaks")) {
121 if(exists("zipfile") && (zipfile!="")) { 104 if (!exists("singlefile")) singlefile=NULL
122 if(!file.exists(zipfile)){ 105 if (!exists("zipfile")) zipfile=NULL
123 error_message=paste("Cannot access the Zip file:",zipfile,". Please, contact your administrator ... if you have one!") 106 rawFilePath = getRawfilePathFromArguments(singlefile, zipfile, listArguments)
124 print(error_message) 107 zipfile = rawFilePath$zipfile
125 stop(error_message) 108 singlefile = rawFilePath$singlefile
126 } 109 listArguments = rawFilePath$listArguments
127 110 directory = retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)
128 #list all file in the zip file
129 #zip_files=unzip(zipfile,list=T)[,"Name"]
130
131
132 #unzip
133 suppressWarnings(unzip(zipfile, unzip="unzip"))
134
135 #get the directory name
136 filesInZip=unzip(zipfile, list=T);
137 directories=unique(unlist(lapply(strsplit(filesInZip$Name,"/"), function(x) x[1])));
138 directories=directories[!(directories %in% c("__MACOSX")) & file.info(directories)$isdir]
139 directory = "."
140 if (length(directories) == 1) directory = directories
141
142 cat("files_root_directory\t",directory,"\n")
143
144 #
145 md5sumList=list("origin"=getMd5sum(directory)) 111 md5sumList=list("origin"=getMd5sum(directory))
146
147 # Check and fix if there are non ASCII characters. If so, they will be removed from the *mzXML mzML files.
148 # Remove because can create issue with some clean files
149 #@TODO: fix me
150 #if (deleteXmlBadCharacters(directory)) {
151 # md5sumList=list("removalBadCharacters"=getMd5sum(directory))
152 #}
153
154 }
155 } 112 }
156 113
157 #addition of the directory to the list of arguments in the first position 114 #addition of the directory to the list of arguments in the first position
158 if (thefunction == "xcmsSet") { 115 if (thefunction == "xcmsSet") {
159 checkXmlStructure(directory) 116 checkXmlStructure(directory)
160 checkFilesCompatibilityWithXcms(directory) 117 checkFilesCompatibilityWithXcms(directory)
161 listArguments=append(directory, listArguments) 118 listArguments=append(directory, listArguments)
162 } 119 }
163 120
164 121
165 #addition of xset object to the list of arguments in the first position 122 #addition of xset object to the list of arguments in the first position
166 if (exists("xset")){ 123 if (exists("xset")){
167 listArguments=append(list(xset), listArguments) 124 listArguments=append(list(xset), listArguments)
168 } 125 }
169 126
170 cat("\n\n") 127 cat("\n\n")
171
172
173 128
174 129
175 130
176 131
177 # ----- MAIN PROCESSING INFO ----- 132 # ----- MAIN PROCESSING INFO -----
179 134
180 135
181 #Verification of a group step before doing the fillpeaks job. 136 #Verification of a group step before doing the fillpeaks job.
182 137
183 if (thefunction == "fillPeaks") { 138 if (thefunction == "fillPeaks") {
184 res=try(is.null(groupnames(xset))) 139 res=try(is.null(groupnames(xset)))
185 if (class(res) == "try-error"){ 140 if (class(res) == "try-error"){
186 error<-geterrmessage() 141 error<-geterrmessage()
187 write(error, stderr()) 142 write(error, stderr())
188 stop("You must always do a group step after a retcor. Otherwise it won't work for the fillpeaks step") 143 stop("You must always do a group step after a retcor. Otherwise it won't work for the fillpeaks step")
189 } 144 }
190 145
191 } 146 }
192 147
193 #change the default display settings 148 #change the default display settings
194 #dev.new(file="Rplots.pdf", width=16, height=12) 149 #dev.new(file="Rplots.pdf", width=16, height=12)
195 pdf(file=rplotspdf, width=16, height=12) 150 pdf(file=rplotspdf, width=16, height=12)
196 if (thefunction == "group") { 151 if (thefunction == "group") {
197 par(mfrow=c(2,2)) 152 par(mfrow=c(2,2))
198 } 153 }
199 #else if (thefunction == "retcor") { 154 #else if (thefunction == "retcor") {
200 #try to change the legend display 155 #try to change the legend display
201 # par(xpd=NA) 156 # par(xpd=NA)
202 # par(xpd=T, mar=par()$mar+c(0,0,0,4)) 157 # par(xpd=T, mar=par()$mar+c(0,0,0,4))
206 #execution of the function "thefunction" with the parameters given in "listArguments" 161 #execution of the function "thefunction" with the parameters given in "listArguments"
207 162
208 cat("\t\tCOMPUTE\n") 163 cat("\t\tCOMPUTE\n")
209 xset = do.call(thefunction, listArguments) 164 xset = do.call(thefunction, listArguments)
210 165
166 # check if there are no peaks
167 if (nrow(peaks(xset)) == 0) {
168 stop("No peaks were detected. You should review your settings")
169 }
170
211 171
212 cat("\n\n") 172 cat("\n\n")
213 173
214 dev.off() #dev.new(file="Rplots.pdf", width=16, height=12) 174 dev.off() #dev.new(file="Rplots.pdf", width=16, height=12)
215 175
216 if (thefunction == "xcmsSet") { 176 if (thefunction == "xcmsSet") {
217 177
218 #transform the files absolute pathways into relative pathways 178 #transform the files absolute pathways into relative pathways
219 xset@filepaths<-sub(paste(getwd(),"/",sep="") ,"", xset@filepaths) 179 xset@filepaths<-sub(paste(getwd(),"/",sep="") ,"", xset@filepaths)
220 180 if(exists("zipfile") && !is.null(zipfile) && (zipfile!="")) {
221 if(exists("zipfile") && (zipfile!="")) { 181
222 182 #Modify the samples names (erase the path)
223 #Modify the samples names (erase the path) 183 for(i in 1:length(sampnames(xset))){
224 for(i in 1:length(sampnames(xset))){ 184
225 185 sample_name=unlist(strsplit(sampnames(xset)[i], "/"))
226 sample_name=unlist(strsplit(sampnames(xset)[i], "/")) 186 sample_name=sample_name[length(sample_name)]
227 sample_name=sample_name[length(sample_name)] 187 sample_name= unlist(strsplit(sample_name,"[.]"))[1]
228 sample_name= unlist(strsplit(sample_name,"[.]"))[1] 188 sampnames(xset)[i]=sample_name
229 sampnames(xset)[i]=sample_name 189
190 }
230 191
231 } 192 }
232
233 }
234 193
235 } 194 }
236 195
237 # -- TIC -- 196 # -- TIC --
238 if (thefunction == "xcmsSet") { 197 if (thefunction == "xcmsSet") {
239 cat("\t\tGET TIC GRAPH\n") 198 cat("\t\tGET TIC GRAPH\n")
240 sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput) 199 sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput)
241 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw") 200 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw")
242 getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf) 201 getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf)
243 } else if (thefunction == "retcor") { 202 } else if (thefunction == "retcor") {
244 cat("\t\tGET TIC GRAPH\n") 203 cat("\t\tGET TIC GRAPH\n")
245 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected") 204 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected")
246 getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf) 205 getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf)
247 } 206 }
248 207
249 if (thefunction == "fillPeaks") { 208 if ((thefunction == "group" || thefunction == "fillPeaks") && exists("intval")) {
250 cat("\t\tGET THE PEAK LIST\n") 209 getPeaklistW4M(xset,intval,convertRTMinute,numDigitsMZ,numDigitsRT,variableMetadataOutput,dataMatrixOutput)
251 getPeaklistW4M(xset,intval,convertRTMinute,numDigitsMZ,numDigitsRT,variableMetadataOutput,dataMatrixOutput)
252 } 210 }
253 211
254 212
255 cat("\n\n") 213 cat("\n\n")
256 214
260 print(xset) 218 print(xset)
261 #delete the parameters to avoid the passage to the next tool in .RData image 219 #delete the parameters to avoid the passage to the next tool in .RData image
262 220
263 221
264 #saving R data in .Rdata file to save the variables used in the present tool 222 #saving R data in .Rdata file to save the variables used in the present tool
265 objects2save = c("xset","zipfile","listOFlistArguments","md5sumList","sampleNamesList") 223 objects2save = c("xset","zipfile","singlefile","listOFlistArguments","md5sumList","sampleNamesList")
266 save(list=objects2save[objects2save %in% ls()], file=xsetRdataOutput) 224 save(list=objects2save[objects2save %in% ls()], file=xsetRdataOutput)
267 225
268 cat("\n\n") 226 cat("\n\n")
269 227
270 228