comparison xcms.r @ 5:b9a87af62223 draft

planemo upload commit 5a5b747865d7fb76f711bce2d9ce2a0f82a2a374-dirty
author lecorguille
date Wed, 06 Apr 2016 03:22:15 -0400
parents
children 60f6c208ca16
comparison
equal deleted inserted replaced
4:4509e9dd03e4 5:b9a87af62223
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 == "xcmsSet" || thefunction == "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 == "xcmsSet" || thefunction == "retcor" || thefunction == "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 #get the directory name
115 filesInZIp=unzip(zipfile, list=T);
116 directories=unique(unlist(lapply(strsplit(filesInZIp$Name,"/"), function(x) x[1])));
117 directories=directories[!(directories %in% c("__MACOSX")) & file.info(directories)$isdir]
118 if (length(directories) == 1) directory=directories else directory=".";
119
120 #unzip
121 suppressWarnings(unzip(zipfile, unzip="unzip"))
122
123 #
124 md5sumList=list("origin"=getMd5sum(directory))
125
126 # Check and fix if there are non ASCII characters. If so, they will be removed from the *mzXML mzML files.
127 # Remove because can create issue with some clean files
128 #@TODO: fix me
129 #if (deleteXmlBadCharacters(directory)) {
130 # md5sumList=list("removalBadCharacters"=getMd5sum(directory))
131 #}
132
133 }
134 }
135
136 #addition of the directory to the list of arguments in the first position
137 if (thefunction == "xcmsSet") {
138 checkXmlStructure(directory)
139 checkFilesCompatibilityWithXcms(directory)
140 listArguments=append(directory, listArguments)
141 }
142
143
144 #addition of xset object to the list of arguments in the first position
145 if (exists("xset")){
146 listArguments=append(list(xset), listArguments)
147 }
148
149 cat("\n\n")
150
151
152
153
154
155
156 # ----- MAIN PROCESSING INFO -----
157 cat("\tMAIN PROCESSING INFO\n")
158
159
160 #Verification of a group step before doing the fillpeaks job.
161
162 if (thefunction == "fillPeaks") {
163 res=try(is.null(groupnames(xset)))
164 if (class(res) == "try-error"){
165 error<-geterrmessage()
166 write(error, stderr())
167 stop("You must always do a group step after a retcor. Otherwise it won't work for the fillpeaks step")
168 }
169
170 }
171
172 #change the default display settings
173 #dev.new(file="Rplots.pdf", width=16, height=12)
174 pdf(file=rplotspdf, width=16, height=12)
175 if (thefunction == "group") {
176 par(mfrow=c(2,2))
177 }
178 #else if (thefunction == "retcor") {
179 #try to change the legend display
180 # par(xpd=NA)
181 # par(xpd=T, mar=par()$mar+c(0,0,0,4))
182 #}
183
184
185 #execution of the function "thefunction" with the parameters given in "listArguments"
186 xset = do.call(thefunction, listArguments)
187
188
189 cat("\n\n")
190
191 dev.off() #dev.new(file="Rplots.pdf", width=16, height=12)
192
193 if (thefunction == "xcmsSet") {
194
195 #transform the files absolute pathways into relative pathways
196 xset@filepaths<-sub("^.*/database/job_working_directory/[0123456789]+/[0123456789]+/" ,"", xset@filepaths)
197 xset@filepaths<-sub("^.*/database/jobs/[0123456789]+/[0123456789]+/" ,"", xset@filepaths)
198 if(exists("zipfile") && (zipfile!="")) {
199
200 #Modify the samples names (erase the path)
201 for(i in 1:length(sampnames(xset))){
202
203 sample_name=unlist(strsplit(sampnames(xset)[i], "/"))
204 sample_name=sample_name[length(sample_name)]
205 sample_name= unlist(strsplit(sample_name,"[.]"))[1]
206 sampnames(xset)[i]=sample_name
207
208 }
209
210 }
211
212 }
213
214 # -- TIC --
215 if (thefunction == "xcmsSet") {
216 sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput)
217 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw")
218 getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf)
219 } else if (thefunction == "retcor") {
220 getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected")
221 getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf)
222 }
223
224 cat("\n\n")
225
226
227 # ----- EXPORT -----
228
229 cat("\tXSET OBJECT INFO\n")
230 print(xset)
231 #delete the parameters to avoid the passage to the next tool in .RData image
232
233
234 #saving R data in .Rdata file to save the variables used in the present tool
235 objects2save = c("xset","zipfile","listOFlistArguments","md5sumList","sampleNamesList")
236 save(list=objects2save[objects2save %in% ls()], file=xsetRdataOutput)
237
238 cat("\n\n")
239
240
241 cat("\tDONE\n")
242