comparison xcms.r @ 4:2db1d1d0f131 draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit 83b80dcd96b379518c2e4ace992affc889d32ca6
author lecorguille
date Fri, 08 Apr 2016 10:39:09 -0400
parents
children 2c1d7df89cf6
comparison
equal deleted inserted replaced
3:98e33cdf0eb1 4:2db1d1d0f131
1 #!/usr/bin/env Rscript
2 # xcms.r version="2.2.0"
3 #Authors ABIMS TEAM
4 #BPC Addition from Y.guitton
5
6
7 # ----- LOG FILE -----
8 log_file=file("log.txt", open = "wt")
9 sink(log_file)
10 sink(log_file, type = "output")
11
12
13 # ----- PACKAGE -----
14 cat("\tPACKAGE INFO\n")
15 #pkgs=c("xcms","batch")
16 pkgs=c("parallel","BiocGenerics", "Biobase", "Rcpp", "mzR", "xcms","snow","batch")
17 for(pkg in pkgs) {
18 suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
19 cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
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="/")) }
22 cat("\n\n");
23
24
25
26
27
28 # ----- ARGUMENTS -----
29 cat("\tARGUMENTS INFO\n")
30 listArguments = parseCommandArgs(evaluate=FALSE) #interpretation of arguments given in command line as an R list of objects
31 write.table(as.matrix(listArguments), col.names=F, quote=F, sep='\t')
32
33 cat("\n\n");
34
35
36 # ----- ARGUMENTS PROCESSING -----
37 cat("\tINFILE PROCESSING INFO\n")
38
39 #image is an .RData file necessary to use xset variable given by previous tools
40 if (!is.null(listArguments[["image"]])){
41 load(listArguments[["image"]]); listArguments[["image"]]=NULL
42 }
43
44 #Import the different functions
45 source_local("lib.r")
46
47 cat("\n\n")
48
49 #Import the different functions
50
51 # ----- PROCESSING INFILE -----
52 cat("\tARGUMENTS PROCESSING INFO\n")
53
54 # Save arguments to generate a report
55 if (!exists("listOFlistArguments")) listOFlistArguments=list()
56 listOFlistArguments[[paste(format(Sys.time(), "%y%m%d-%H:%M:%S_"),listArguments[["xfunction"]],sep="")]] = listArguments
57
58
59 #saving the commun parameters
60 thefunction = listArguments[["xfunction"]]; listArguments[["xfunction"]]=NULL #delete from the list of arguments
61
62 xsetRdataOutput = paste(thefunction,"RData",sep=".")
63 if (!is.null(listArguments[["xsetRdataOutput"]])){
64 xsetRdataOutput = listArguments[["xsetRdataOutput"]]; listArguments[["xsetRdataOutput"]]=NULL
65 }
66
67 rplotspdf = "Rplots.pdf"
68 if (!is.null(listArguments[["rplotspdf"]])){
69 rplotspdf = listArguments[["rplotspdf"]]; listArguments[["rplotspdf"]]=NULL
70 }
71
72 sampleMetadataOutput = "sampleMetadata.tsv"
73 if (!is.null(listArguments[["sampleMetadataOutput"]])){
74 sampleMetadataOutput = listArguments[["sampleMetadataOutput"]]; listArguments[["sampleMetadataOutput"]]=NULL
75 }
76
77
78
79
80 if (thefunction %in% c("xcmsSet","retcor")) {
81 ticspdf = listArguments[["ticspdf"]]; listArguments[["ticspdf"]]=NULL
82 bicspdf = listArguments[["bicspdf"]]; listArguments[["bicspdf"]]=NULL
83 }
84
85 #necessary to unzip .zip file uploaded to Galaxy
86 #thanks to .zip file it's possible to upload many file as the same time conserving the tree hierarchy of directories
87
88
89 if (!is.null(listArguments[["zipfile"]])){
90 zipfile= listArguments[["zipfile"]]; listArguments[["zipfile"]]=NULL
91 }
92
93 if (!is.null(listArguments[["library"]])){
94 directory=listArguments[["library"]]; listArguments[["library"]]=NULL
95 if(!file.exists(directory)){
96 error_message=paste("Cannot access the directory:",directory,". Please verify if the directory exists or not.")
97 print(error_message)
98 stop(error_message)
99 }
100 }
101
102 # We unzip automatically the chromatograms from the zip files.
103 if (thefunction %in% c("xcmsSet","retcor","fillPeaks")) {
104 if(exists("zipfile") && (zipfile!="")) {
105 if(!file.exists(zipfile)){
106 error_message=paste("Cannot access the Zip file:",zipfile,". Please, contact your administrator ... if you have one!")
107 print(error_message)
108 stop(error_message)
109 }
110
111 #list all file in the zip file
112 #zip_files=unzip(zipfile,list=T)[,"Name"]
113
114
115 #unzip
116 suppressWarnings(unzip(zipfile, unzip="unzip"))
117
118 #get the directory name
119 filesInZip=unzip(zipfile, list=T);
120 directories=unique(unlist(lapply(strsplit(filesInZip$Name,"/"), function(x) x[1])));
121 directories=directories[!(directories %in% c("__MACOSX")) & file.info(directories)$isdir]
122 directory = "."
123 if (length(directories) == 1) directory = directories
124
125 cat("files_root_directory\t",directory,"\n")
126
127 #
128 md5sumList=list("origin"=getMd5sum(directory))
129
130 # Check and fix if there are non ASCII characters. If so, they will be removed from the *mzXML mzML files.
131 # Remove because can create issue with some clean files
132 #@TODO: fix me
133 #if (deleteXmlBadCharacters(directory)) {
134 # md5sumList=list("removalBadCharacters"=getMd5sum(directory))
135 #}
136
137 }
138 }
139
140 #addition of the directory to the list of arguments in the first position
141 if (thefunction == "xcmsSet") {
142 checkXmlStructure(directory)
143 checkFilesCompatibilityWithXcms(directory)
144 listArguments=append(directory, listArguments)
145 }
146
147
148 #addition of xset object to the list of arguments in the first position
149 if (exists("xset")){
150 listArguments=append(list(xset), listArguments)
151 }
152
153 cat("\n\n")
154
155
156
157
158
159
160 # ----- MAIN PROCESSING INFO -----
161 cat("\tMAIN PROCESSING INFO\n")
162
163
164 #Verification of a group step before doing the fillpeaks job.
165
166 if (thefunction == "fillPeaks") {
167 res=try(is.null(groupnames(xset)))
168 if (class(res) == "try-error"){
169 error<-geterrmessage()
170 write(error, stderr())
171 stop("You must always do a group step after a retcor. Otherwise it won't work for the fillpeaks step")
172 }
173
174 }
175
176 #change the default display settings
177 #dev.new(file="Rplots.pdf", width=16, height=12)
178 pdf(file=rplotspdf, width=16, height=12)
179 if (thefunction == "group") {
180 par(mfrow=c(2,2))
181 }
182 #else if (thefunction == "retcor") {
183 #try to change the legend display
184 # par(xpd=NA)
185 # par(xpd=T, mar=par()$mar+c(0,0,0,4))
186 #}
187
188
189 #execution of the function "thefunction" with the parameters given in "listArguments"
190 xset = do.call(thefunction, listArguments)
191
192
193 cat("\n\n")
194
195 dev.off() #dev.new(file="Rplots.pdf", width=16, height=12)
196
197 if (thefunction == "xcmsSet") {
198
199 #transform the files absolute pathways into relative pathways
200 xset@filepaths<-sub(paste(getwd(),"/",sep="") ,"", xset@filepaths)
201
202 if(exists("zipfile") && (zipfile!="")) {
203
204 #Modify the samples names (erase the path)
205 for(i in 1:length(sampnames(xset))){
206
207 sample_name=unlist(strsplit(sampnames(xset)[i], "/"))
208 sample_name=sample_name[length(sample_name)]
209 sample_name= unlist(strsplit(sample_name,"[.]"))[1]
210 sampnames(xset)[i]=sample_name
211
212 }
213
214 }
215
216 }
217
218 # -- TIC --
219 if (thefunction == "xcmsSet") {
220 sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput)
221 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw")
222 getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf)
223 } else if (thefunction == "retcor") {
224 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected")
225 getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf)
226 }
227
228 cat("\n\n")
229
230
231 # ----- EXPORT -----
232
233 cat("\tXSET OBJECT INFO\n")
234 print(xset)
235 #delete the parameters to avoid the passage to the next tool in .RData image
236
237
238 #saving R data in .Rdata file to save the variables used in the present tool
239 objects2save = c("xset","zipfile","listOFlistArguments","md5sumList","sampleNamesList")
240 save(list=objects2save[objects2save %in% ls()], file=xsetRdataOutput)
241
242 cat("\n\n")
243
244
245 cat("\tDONE\n")
246