diff xcms.r @ 12:15646e937936 draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit a6f5f18b3d6130f7d7fbb9f2df856838c6217797
author lecorguille
date Fri, 07 Apr 2017 07:35:01 -0400
parents 91311aa08cdc
children
line wrap: on
line diff
--- a/xcms.r	Mon Jan 30 08:52:59 2017 -0500
+++ b/xcms.r	Fri Apr 07 07:35:01 2017 -0400
@@ -15,8 +15,8 @@
 #pkgs=c("xcms","batch")
 pkgs=c("parallel","BiocGenerics", "Biobase", "Rcpp", "mzR", "xcms","snow","batch")
 for(pkg in pkgs) {
-  suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
-  cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
+    suppressPackageStartupMessages( stopifnot( library(pkg, quietly=TRUE, logical.return=TRUE, character.only=TRUE)))
+    cat(pkg,"\t",as.character(packageVersion(pkg)),"\n",sep="")
 }
 source_local <- function(fname){ argv <- commandArgs(trailingOnly = FALSE); base_dir <- dirname(substring(argv[grep("--file=", argv)], 8)); source(paste(base_dir, fname, sep="/")) }
 cat("\n\n");
@@ -38,7 +38,7 @@
 
 #image is an .RData file necessary to use xset variable given by previous tools
 if (!is.null(listArguments[["image"]])){
-  load(listArguments[["image"]]); listArguments[["image"]]=NULL
+    load(listArguments[["image"]]); listArguments[["image"]]=NULL
 }
 
 #Import the different functions
@@ -61,110 +61,67 @@
 
 xsetRdataOutput = paste(thefunction,"RData",sep=".")
 if (!is.null(listArguments[["xsetRdataOutput"]])){
-  xsetRdataOutput = listArguments[["xsetRdataOutput"]]; listArguments[["xsetRdataOutput"]]=NULL
+    xsetRdataOutput = listArguments[["xsetRdataOutput"]]; listArguments[["xsetRdataOutput"]]=NULL
 }
 
 #saving the specific parameters
 rplotspdf = "Rplots.pdf"
 if (!is.null(listArguments[["rplotspdf"]])){
-  rplotspdf = listArguments[["rplotspdf"]]; listArguments[["rplotspdf"]]=NULL
+    rplotspdf = listArguments[["rplotspdf"]]; listArguments[["rplotspdf"]]=NULL
 }
 sampleMetadataOutput = "sampleMetadata.tsv"
 if (!is.null(listArguments[["sampleMetadataOutput"]])){
-  sampleMetadataOutput = listArguments[["sampleMetadataOutput"]]; listArguments[["sampleMetadataOutput"]]=NULL
+    sampleMetadataOutput = listArguments[["sampleMetadataOutput"]]; listArguments[["sampleMetadataOutput"]]=NULL
 }
 variableMetadataOutput = "variableMetadata.tsv"
 if (!is.null(listArguments[["variableMetadataOutput"]])){
-  variableMetadataOutput = listArguments[["variableMetadataOutput"]]; listArguments[["variableMetadataOutput"]]=NULL
+    variableMetadataOutput = listArguments[["variableMetadataOutput"]]; listArguments[["variableMetadataOutput"]]=NULL
 }
 dataMatrixOutput = "dataMatrix.tsv"
 if (!is.null(listArguments[["dataMatrixOutput"]])){
-  dataMatrixOutput = listArguments[["dataMatrixOutput"]]; listArguments[["dataMatrixOutput"]]=NULL
+    dataMatrixOutput = listArguments[["dataMatrixOutput"]]; listArguments[["dataMatrixOutput"]]=NULL
 }
 if (!is.null(listArguments[["convertRTMinute"]])){
-  convertRTMinute = listArguments[["convertRTMinute"]]; listArguments[["convertRTMinute"]]=NULL
+    convertRTMinute = listArguments[["convertRTMinute"]]; listArguments[["convertRTMinute"]]=NULL
 }
 if (!is.null(listArguments[["numDigitsMZ"]])){
-  numDigitsMZ = listArguments[["numDigitsMZ"]]; listArguments[["numDigitsMZ"]]=NULL
+    numDigitsMZ = listArguments[["numDigitsMZ"]]; listArguments[["numDigitsMZ"]]=NULL
 }
 if (!is.null(listArguments[["numDigitsRT"]])){
   numDigitsRT = listArguments[["numDigitsRT"]]; listArguments[["numDigitsRT"]]=NULL
 }
 if (!is.null(listArguments[["intval"]])){
-  intval = listArguments[["intval"]]; listArguments[["intval"]]=NULL
+    intval = listArguments[["intval"]]; listArguments[["intval"]]=NULL
 }
 
 if (thefunction %in% c("xcmsSet","retcor")) {
-  ticspdf = listArguments[["ticspdf"]]; listArguments[["ticspdf"]]=NULL
-  bicspdf = listArguments[["bicspdf"]]; listArguments[["bicspdf"]]=NULL
-}
-
-#necessary to unzip .zip file uploaded to Galaxy
-#thanks to .zip file it's possible to upload many file as the same time conserving the tree hierarchy of directories
-
-
-if (!is.null(listArguments[["zipfile"]])){
-  zipfile= listArguments[["zipfile"]]; listArguments[["zipfile"]]=NULL
-}
-
-if (!is.null(listArguments[["library"]])){
-  directory=listArguments[["library"]]; listArguments[["library"]]=NULL
-  if(!file.exists(directory)){
-    error_message=paste("Cannot access the directory:",directory,". Please verify if the directory exists or not.")
-    print(error_message)
-    stop(error_message)
-  }
+    ticspdf = listArguments[["ticspdf"]]; listArguments[["ticspdf"]]=NULL
+    bicspdf = listArguments[["bicspdf"]]; listArguments[["bicspdf"]]=NULL
 }
 
-# We unzip automatically the chromatograms from the zip files.
+
 if (thefunction %in% c("xcmsSet","retcor","fillPeaks"))  {
-  if(exists("zipfile") && (zipfile!="")) {
-    if(!file.exists(zipfile)){
-      error_message=paste("Cannot access the Zip file:",zipfile,". Please, contact your administrator ... if you have one!")
-      print(error_message)
-      stop(error_message)
-    }
-
-    #list all file in the zip file
-    #zip_files=unzip(zipfile,list=T)[,"Name"]
-
-
-    #unzip
-    suppressWarnings(unzip(zipfile, unzip="unzip"))
-
-    #get the directory name
-    filesInZip=unzip(zipfile, list=T);
-    directories=unique(unlist(lapply(strsplit(filesInZip$Name,"/"), function(x) x[1])));
-    directories=directories[!(directories %in% c("__MACOSX")) & file.info(directories)$isdir]
-    directory = "."
-    if (length(directories) == 1) directory = directories
-
-    cat("files_root_directory\t",directory,"\n")
-
-    #
+    if (!exists("singlefile")) singlefile=NULL
+    if (!exists("zipfile")) zipfile=NULL
+    rawFilePath = getRawfilePathFromArguments(singlefile, zipfile, listArguments)
+    zipfile = rawFilePath$zipfile
+    singlefile = rawFilePath$singlefile
+    listArguments = rawFilePath$listArguments
+    directory = retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)
     md5sumList=list("origin"=getMd5sum(directory))
-
-    # Check and fix if there are non ASCII characters. If so, they will be removed from the *mzXML mzML files.
-    # Remove because can create issue with some clean files
-    #@TODO: fix me
-    #if (deleteXmlBadCharacters(directory)) {
-    #  md5sumList=list("removalBadCharacters"=getMd5sum(directory))
-    #}
-
-  }
 }
 
 #addition of the directory to the list of arguments in the first position
 if (thefunction == "xcmsSet") {
-  checkXmlStructure(directory)
-  checkFilesCompatibilityWithXcms(directory)
-  listArguments=append(directory, listArguments)
+    checkXmlStructure(directory)
+    checkFilesCompatibilityWithXcms(directory)
+    listArguments=append(directory, listArguments)
 }
 
 
 #addition of xset object to the list of arguments in the first position
 if (exists("xset")){
-  listArguments=append(list(xset), listArguments)
+    listArguments=append(list(xset), listArguments)
 }
 
 cat("\n\n")
@@ -172,8 +129,6 @@
 
 
 
-
-
 # ----- MAIN PROCESSING INFO -----
 cat("\tMAIN PROCESSING INFO\n")
 
@@ -181,12 +136,12 @@
 #Verification of a group step before doing the fillpeaks job.
 
 if (thefunction == "fillPeaks") {
-  res=try(is.null(groupnames(xset)))
-  if (class(res) == "try-error"){
-    error<-geterrmessage()
-    write(error, stderr())
-    stop("You must always do a group step after a retcor. Otherwise it won't work for the fillpeaks step")
-  }
+    res=try(is.null(groupnames(xset)))
+    if (class(res) == "try-error"){
+        error<-geterrmessage()
+        write(error, stderr())
+        stop("You must always do a group step after a retcor. Otherwise it won't work for the fillpeaks step")
+    }
 
 }
 
@@ -194,7 +149,7 @@
 #dev.new(file="Rplots.pdf", width=16, height=12)
 pdf(file=rplotspdf, width=16, height=12)
 if (thefunction == "group") {
-  par(mfrow=c(2,2))
+    par(mfrow=c(2,2))
 }
 #else if (thefunction == "retcor") {
 #try to change the legend display
@@ -208,6 +163,11 @@
 cat("\t\tCOMPUTE\n")
 xset = do.call(thefunction, listArguments)
 
+# check if there are no peaks
+if (nrow(peaks(xset)) == 0) {
+    stop("No peaks were detected. You should review your settings")
+}
+
 
 cat("\n\n")
 
@@ -215,40 +175,38 @@
 
 if (thefunction  == "xcmsSet") {
 
-  #transform the files absolute pathways into relative pathways
-  xset@filepaths<-sub(paste(getwd(),"/",sep="") ,"", xset@filepaths)
+    #transform the files absolute pathways into relative pathways
+    xset@filepaths<-sub(paste(getwd(),"/",sep="") ,"", xset@filepaths)
+    if(exists("zipfile") && !is.null(zipfile) && (zipfile!="")) {
 
-  if(exists("zipfile") && (zipfile!="")) {
+        #Modify the samples names (erase the path)
+        for(i in 1:length(sampnames(xset))){
 
-    #Modify the samples names (erase the path)
-    for(i in 1:length(sampnames(xset))){
+            sample_name=unlist(strsplit(sampnames(xset)[i], "/"))
+            sample_name=sample_name[length(sample_name)]
+            sample_name= unlist(strsplit(sample_name,"[.]"))[1]
+            sampnames(xset)[i]=sample_name
 
-      sample_name=unlist(strsplit(sampnames(xset)[i], "/"))
-      sample_name=sample_name[length(sample_name)]
-      sample_name= unlist(strsplit(sample_name,"[.]"))[1]
-      sampnames(xset)[i]=sample_name
+        }
 
     }
 
-  }
-
 }
 
 # -- TIC --
 if (thefunction == "xcmsSet") {
-  cat("\t\tGET TIC GRAPH\n")
-  sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput)
-  getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw")
-  getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf)
+    cat("\t\tGET TIC GRAPH\n")
+    sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput)
+    getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw")
+    getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf)
 } else if (thefunction == "retcor") {
-  cat("\t\tGET TIC GRAPH\n")
-  getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected")
-  getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf)
+    cat("\t\tGET TIC GRAPH\n")
+    getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected")
+    getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf)
 }
 
-if (thefunction == "fillPeaks") {
-  cat("\t\tGET THE PEAK LIST\n")
-  getPeaklistW4M(xset,intval,convertRTMinute,numDigitsMZ,numDigitsRT,variableMetadataOutput,dataMatrixOutput)
+if ((thefunction == "group" || thefunction == "fillPeaks") && exists("intval")) {
+    getPeaklistW4M(xset,intval,convertRTMinute,numDigitsMZ,numDigitsRT,variableMetadataOutput,dataMatrixOutput)
 }
 
 
@@ -262,7 +220,7 @@
 
 
 #saving R data in .Rdata file to save the variables used in the present tool
-objects2save = c("xset","zipfile","listOFlistArguments","md5sumList","sampleNamesList")
+objects2save = c("xset","zipfile","singlefile","listOFlistArguments","md5sumList","sampleNamesList")
 save(list=objects2save[objects2save %in% ls()], file=xsetRdataOutput)
 
 cat("\n\n")