changeset 6:0888f7ef739a draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit 83b80dcd96b379518c2e4ace992affc889d32ca6
author lecorguille
date Fri, 08 Apr 2016 10:38:52 -0400
parents 588b0a7ae4b0
children 451ff602a957
files README.rst abims_xcms_xcmsSet.xml lib.r macros.xml planemo.sh planemo_test.sh test-data/log.txt test-data/sacuri.zip test-data/sacuri_current_root.zip test-data/sacuri_dir_root.log.txt test-data/sacuri_dir_root.zip test-data/sampleMetadata.tsv test-data/xset.BPCs_raw.pdf test-data/xset.RData test-data/xset.TICs_raw.pdf tool_dependencies.xml xcms.r
diffstat 17 files changed, 855 insertions(+), 135 deletions(-) [+]
line wrap: on
line diff
--- a/README.rst	Mon Feb 22 16:25:05 2016 -0500
+++ b/README.rst	Fri Apr 08 10:38:52 2016 -0400
@@ -2,6 +2,10 @@
 Changelog/News
 --------------
 
+**Version 2.0.8 - 06/04/2016**
+
+- TEST: refactoring to pass planemo test using conda dependencies
+
 
 **Version 2.0.7 - 10/02/2016**
 
@@ -16,7 +20,7 @@
 - UPDATE: refactoring to feed the new report tool
 
 
-**Version 2.0.2 - 18/01/2016
+**Version 2.0.2 - 18/01/2016**
 
 - BUGFIX: Some zip files were tag as "corrupt" by R. We have changed the extraction mode to deal with thoses cases.
 
@@ -33,3 +37,13 @@
 - IMPROVEMENT: new datatype/dataset formats (rdata.xcms.raw, rdata.xcms.group, rdata.xcms.retcor ...) will facilitate the sequence of tools and so avoid incompatibility errors.
 
 - IMPROVEMENT: parameter labels have changed to facilitate their reading.
+
+
+Test Status
+-----------
+
+Planemo test using conda: passed
+
+Planemo test using source env.sh: passed
+
+Planemo shed_test : passed
\ No newline at end of file
--- a/abims_xcms_xcmsSet.xml	Mon Feb 22 16:25:05 2016 -0500
+++ b/abims_xcms_xcmsSet.xml	Fri Apr 08 10:38:52 2016 -0400
@@ -1,20 +1,16 @@
-<tool id="abims_xcms_xcmsSet" name="xcms.xcmsSet" version="2.0.7">
+<tool id="abims_xcms_xcmsSet" name="xcms.xcmsSet" version="2.0.8">
     
     <description>Filtration and Peak Identification using xcmsSet function from xcms R package to preprocess LC/MS data for relative quantification and statistical analysis </description>
     
-    <requirements>
-        <requirement type="package" version="3.1.2">R</requirement>
-        <requirement type="binary">Rscript</requirement>
-        <requirement type="package" version="1.44.0">xcms</requirement>
-        <requirement type="package" version="2.2.0">xcms_w4m_script</requirement>
-    </requirements>
-    
-    <stdio>
-        <exit_code range="1:" level="fatal" />
-    </stdio>
+    <macros>
+        <import>macros.xml</import>
+    </macros>
+
+    <expand macro="requirements"/>
+    <expand macro="stdio"/>
     
     <command><![CDATA[
-        xcms.r
+        @COMMAND_XCMS_SCRIPT@
         #if $inputs.input == "lib":
             library $__app__.config.user_library_import_dir/$__user_email__/$inputs.library
         #elif $inputs.input == "zip_file":
@@ -33,16 +29,16 @@
         #if $methods.method == "centWave":
             ppm $methods.ppm
             peakwidth "c($methods.peakwidth)"
-        #if $methods.options_scanrange.option == "show":
+            #if $methods.options_scanrange.option == "show":
                 scanrange "c($methods.options_scanrange.scanrange)"
-        #end if 
-        #if $methods.options_c.option == "show":
-            mzdiff $methods.options_c.mzdiff
-            snthresh $methods.options_c.snthresh
-            integrate $methods.options_c.integrate
-            noise $methods.options_c.noise
-            prefilter "c($methods.options_c.prefilter)"
-        #end if
+            #end if 
+            #if $methods.options_c.option == "show":
+                mzdiff $methods.options_c.mzdiff
+                snthresh $methods.options_c.snthresh
+                integrate $methods.options_c.integrate
+                noise $methods.options_c.noise
+                prefilter "c($methods.options_c.prefilter)"
+            #end if
         #elif $methods.method == "matchedFilter":
             step $methods.step
             fwhm $methods.fwhm
@@ -62,11 +58,7 @@
             scales "c($methods.scales)"
             SNR.method "$methods.SNR_method"
         #end if
-        ;
-        return=\$?;
-        mv log.txt $log;
-        cat $log;
-        sh -c "exit \$return"
+        @COMMAND_LOG_EXIT@
     ]]></command>
     
     <inputs>
@@ -192,25 +184,38 @@
     
     <tests>
         <test>
-            <param name="inputs.input" value="zip_file" />
-            <param name="inputs.zip_file" value="sacuri.zip" />
-            <param name="methods.method" value="matchedFilter" />
-            <param name="methods.step" value="0.01" />
-            <param name="methods.fwhm" value="4" />
-            <param name="methods.options_m.option" value="show" />
-            <param name="methods.options_m.max" value="50" />
-            <param name="methods.options_m.snthresh" value="1" />
-            <param name="methods.options_m.steps" value="2" />
-            <!--<output name="xsetRData" file="xset.RData" />-->
-            <!--<output name="sampleMetadata" file="sampleMetadata.tsv" />-->
-            <!--<output name="ticsRawPdf" file="xset.TICs_raw.pdf" />-->
-            <!--<output name="bpcsRawPdf" file="xset.BPCs_raw.pdf" />-->
+            <param name="inputs|input" value="zip_file" />
+            <param name="inputs|zip_file" value="sacuri_dir_root.zip"  ftype="zip" />
+            <param name="methods|method" value="matchedFilter" />
+            <param name="methods|step" value="0.01" />
+            <param name="methods|fwhm" value="4" />
+            <param name="methods|options_m|option" value="show" />
+            <param name="methods|options_m|max" value="50" />
+            <param name="methods|options_m|snthresh" value="1" />
+            <param name="methods|options_m|steps" value="2" />
             <output name="log">
                 <assert_contents>
-                    <has_text text="object with 9 samples" />
-                    <has_text text="Time range: 0.7-1140 seconds (0-19 minutes)" />
-                    <has_text text="Mass range: 50.0019-999.9863 m/z" />
-                    <has_text text="Peaks: 135846 (about 15094 per sample)" />
+                    <has_text text="object with 4 samples" />
+                    <has_text text="Time range: 0.7-1139.7 seconds (0-19 minutes)" />
+                    <has_text text="Mass range: 50.0021-999.9863 m/z" />
+                    <has_text text="Peaks: 59359 (about 14840 per sample)" />
+                    <has_text text="Peak Groups: 0" />
+                    <has_text text="Sample classes: bio, blank" />
+                </assert_contents>
+            </output>
+        </test> 
+        <test>
+            <param name="inputs|input" value="zip_file" />
+            <param name="inputs|zip_file" value="sacuri_current_root.zip"  ftype="zip" />
+            <param name="methods|method" value="centWave" />
+            <param name="methods|ppm" value="25" />
+            <param name="methods|peakwidth" value="20,50" />
+            <output name="log">
+                <assert_contents>
+                    <has_text text="object with 4 samples" />
+                    <has_text text="Time range: 3.5-1139.2 seconds (0.1-19 minutes)" />
+                    <has_text text="Mass range: 57.9756-593.4086 m/z" />
+                    <has_text text="Peaks: 1535 (about 384 per sample)" />
                     <has_text text="Peak Groups: 0" />
                     <has_text text="Sample classes: bio, blank" />
                 </assert_contents>
@@ -220,17 +225,7 @@
     
     <help><![CDATA[
 
-.. class:: infomark
-
-**Authors**  Colin A. Smith csmith@scripps.edu, Ralf Tautenhahn rtautenh@gmail.com, Steffen Neumann sneumann@ipb-halle.de, Paul Benton hpaul.benton08@imperial.ac.uk and Christopher Conley cjconley@ucdavis.edu 
-
-.. class:: infomark
-
-**Galaxy integration** ABiMS TEAM - UPMC/CNRS - Station biologique de Roscoff and Yann Guitton yann.guitton@univ-nantes.fr - part of Workflow4Metabolomics.org [W4M]
-
- | Contact support@workflow4metabolomics.org for any questions or concerns about the Galaxy implementation of this tool.
-
----------------------------------------------------
+@HELP_AUTHORS@
 
 ============
 Xcms.xcmsSet
@@ -463,6 +458,10 @@
 Changelog/News
 --------------
 
+**Version 2.0.8 - 06/04/2016**
+
+- TEST: refactoring to pass planemo test using conda dependencies
+
 
 **Version 2.0.7 - 10/02/2016**
 
@@ -474,7 +473,7 @@
 
 - UPDATE: refactoring of internal management of inputs/outputs
 
-- UPDATE: refactoring to feed the new report tool
+- TEST: refactoring to feed the new report tool
 
 
 **Version 2.0.2 - 18/01/2016
@@ -497,10 +496,5 @@
 
     ]]></help>
 
-
-    <citations>
-        <citation type="doi">10.1021/ac051437y</citation>
-        <citation type="doi">10.1093/bioinformatics/btu813</citation>
-    </citations>
-
+    <expand macro="citation" />
 </tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib.r	Fri Apr 08 10:38:52 2016 -0400
@@ -0,0 +1,400 @@
+# lib.r version="2.0.1"
+#Authors ABiMS TEAM
+#Lib.r for Galaxy Workflow4Metabo
+#version 2.2
+#Based on lib.r 2.1
+#Modifications made by Guitton Yann 
+#correct bug in Base Peak Chromatogram (BPC) option, not only TIC when scanrange used in xcmsSet
+#Note if scanrange is used a warning is prompted in R console but do not stop PDF generation
+
+
+
+
+#@author Y. Guitton
+getBPC <- function(file,rtcor=NULL, ...) {
+  object <- xcmsRaw(file)
+  sel <- profRange(object, ...)
+  cbind(if (is.null(rtcor)) object@scantime[sel$scanidx] else rtcor ,xcms:::colMax(object@env$profile[sel$massidx,sel$scanidx,drop=FALSE]))
+  #plotChrom(xcmsRaw(file), base=T)
+}
+
+#@author Y. Guitton
+getBPCs <- function (xcmsSet=NULL, pdfname="BPCs.pdf",rt=c("raw","corrected"), scanrange=NULL) {
+  cat("Creating BIC pdf...\n")
+
+  if (is.null(xcmsSet)) {
+    cat("Enter an xcmsSet \n")
+    stop()
+  } else {
+    files <- filepaths(xcmsSet)
+  }
+
+  class<-as.vector(levels(xcmsSet@phenoData[,1])) #sometime phenoData have more than 1 column use first as class
+
+  classnames<-vector("list",length(class))
+  for (i in 1:length(class)){
+    classnames[[i]]<-which( xcmsSet@phenoData[,1]==class[i])
+  }
+
+  N <- dim(phenoData(xcmsSet))[1]
+
+  TIC <- vector("list",N)
+
+
+  for (j in 1:N) {
+
+    TIC[[j]] <- getBPC(files[j])
+    #good for raw 
+    # seems strange for corrected
+    #errors if scanrange used in xcmsSetgeneration
+    if (!is.null(xcmsSet) && rt == "corrected")
+    rtcor <- xcmsSet@rt$corrected[[j]] else
+    rtcor <- NULL
+    
+    TIC[[j]] <- getBPC(files[j],rtcor=rtcor)
+    # TIC[[j]][,1]<-rtcor
+  }
+
+
+
+  pdf(pdfname,w=16,h=10)
+  cols <- rainbow(N)
+  lty = 1:N
+  pch = 1:N
+  #search for max x and max y in BPCs
+  xlim = range(sapply(TIC, function(x) range(x[,1])))
+  ylim = range(sapply(TIC, function(x) range(x[,2])))
+  ylim = c(-ylim[2], ylim[2])
+
+
+  ##plot start
+  
+  if (length(class)>2){
+    for (k in 1:(length(class)-1)){
+      for (l in (k+1):length(class)){
+        #print(paste(class[k],"vs",class[l],sep=" ")) 
+        plot(0, 0, type="n", xlim = xlim/60, ylim = ylim, main = paste("Base Peak Chromatograms \n","BPCs_",class[k]," vs ",class[l], sep=""), xlab = "Retention Time (min)", ylab = "BPC")
+        colvect<-NULL
+        for (j in 1:length(classnames[[k]])) {
+          tic <- TIC[[classnames[[k]][j]]]
+          # points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
+          points(tic[,1]/60, tic[,2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type="l")
+          colvect<-append(colvect,cols[classnames[[k]][j]])
+        }
+        for (j in 1:length(classnames[[l]])) {
+          # i=class2names[j]
+          tic <- TIC[[classnames[[l]][j]]]
+          points(tic[,1]/60, -tic[,2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type="l")
+          colvect<-append(colvect,cols[classnames[[l]][j]])
+        }
+        legend("topright",paste(basename(files[c(classnames[[k]],classnames[[l]])])), col = colvect, lty = lty, pch = pch)
+      }
+    }
+  }#end if length >2
+
+  if (length(class)==2){
+    k=1
+    l=2
+    colvect<-NULL
+    plot(0, 0, type="n", xlim = xlim/60, ylim = ylim, main = paste("Base Peak Chromatograms \n","BPCs_",class[k],"vs",class[l], sep=""), xlab = "Retention Time (min)", ylab = "BPC")
+
+    for (j in 1:length(classnames[[k]])) {
+
+      tic <- TIC[[classnames[[k]][j]]]
+      # points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
+      points(tic[,1]/60, tic[,2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type="l")
+      colvect<-append(colvect,cols[classnames[[k]][j]])
+    }
+    for (j in 1:length(classnames[[l]])) {
+      # i=class2names[j]
+      tic <- TIC[[classnames[[l]][j]]]
+      points(tic[,1]/60, -tic[,2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type="l")
+      colvect<-append(colvect,cols[classnames[[l]][j]])
+    }
+    legend("topright",paste(basename(files[c(classnames[[k]],classnames[[l]])])), col = colvect, lty = lty, pch = pch)
+
+  }#end length ==2
+
+  dev.off() #pdf(pdfname,w=16,h=10)
+
+  invisible(TIC)
+}
+
+
+
+#@author Y. Guitton
+getTIC <- function(file,rtcor=NULL) {
+  object <- xcmsRaw(file)
+  cbind(if (is.null(rtcor)) object@scantime else rtcor, rawEIC(object,mzrange=range(object@env$mz))$intensity)
+}
+
+##
+##  overlay TIC from all files in current folder or from xcmsSet, create pdf
+##
+#@author Y. Guitton
+getTICs <- function(xcmsSet=NULL,files=NULL, pdfname="TICs.pdf",rt=c("raw","corrected")) {
+  cat("Creating TIC pdf...\n")
+
+  if (is.null(xcmsSet)) {
+    filepattern <- c("[Cc][Dd][Ff]", "[Nn][Cc]", "([Mm][Zz])?[Xx][Mm][Ll]", "[Mm][Zz][Dd][Aa][Tt][Aa]", "[Mm][Zz][Mm][Ll]")
+    filepattern <- paste(paste("\\.", filepattern, "$", sep = ""), collapse = "|")
+    if (is.null(files))
+      files <- getwd()
+    info <- file.info(files)
+    listed <- list.files(files[info$isdir], pattern = filepattern, recursive = TRUE, full.names = TRUE)
+    files <- c(files[!info$isdir], listed)
+  } else {
+    files <- filepaths(xcmsSet)
+  }
+
+  class<-as.vector(levels(xcmsSet@phenoData[,1])) #sometime phenoData have more than 1 column use first as class
+
+  classnames<-vector("list",length(class))
+  for (i in 1:length(class)){
+    classnames[[i]]<-which( xcmsSet@phenoData[,1]==class[i])
+  }
+  
+  N <- length(files)
+  TIC <- vector("list",N)
+
+  for (i in 1:N) {
+    if (!is.null(xcmsSet) && rt == "corrected")
+      rtcor <- xcmsSet@rt$corrected[[i]] else
+    rtcor <- NULL
+    TIC[[i]] <- getTIC(files[i],rtcor=rtcor)
+  }
+
+  pdf(pdfname,w=16,h=10)
+  cols <- rainbow(N)
+  lty = 1:N
+  pch = 1:N
+  #search for max x and max y in TICs
+  xlim = range(sapply(TIC, function(x) range(x[,1])))
+  ylim = range(sapply(TIC, function(x) range(x[,2])))
+  ylim = c(-ylim[2], ylim[2])
+
+
+  ##plot start
+  if (length(class)>2){
+    for (k in 1:(length(class)-1)){
+      for (l in (k+1):length(class)){
+        #print(paste(class[k],"vs",class[l],sep=" ")) 
+        plot(0, 0, type="n", xlim = xlim/60, ylim = ylim, main = paste("Total Ion Chromatograms \n","TICs_",class[k]," vs ",class[l], sep=""), xlab = "Retention Time (min)", ylab = "TIC")
+        colvect<-NULL
+        for (j in 1:length(classnames[[k]])) {
+
+          tic <- TIC[[classnames[[k]][j]]]
+          # points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
+          points(tic[,1]/60, tic[,2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type="l")
+          colvect<-append(colvect,cols[classnames[[k]][j]])
+        }
+        for (j in 1:length(classnames[[l]])) {
+          # i=class2names[j]
+          tic <- TIC[[classnames[[l]][j]]]
+          points(tic[,1]/60, -tic[,2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type="l")
+          colvect<-append(colvect,cols[classnames[[l]][j]])
+        }
+        legend("topright",paste(basename(files[c(classnames[[k]],classnames[[l]])])), col = colvect, lty = lty, pch = pch)
+      }
+    }
+  }#end if length >2
+  if (length(class)==2){
+    k=1
+    l=2
+
+    plot(0, 0, type="n", xlim = xlim/60, ylim = ylim, main = paste("Total Ion Chromatograms \n","TICs_",class[k],"vs",class[l], sep=""), xlab = "Retention Time (min)", ylab = "TIC")
+    colvect<-NULL
+    for (j in 1:length(classnames[[k]])) {
+      tic <- TIC[[classnames[[k]][j]]]
+      # points(tic[,1]/60, tic[,2], col = cols[i], pch = pch[i], type="l")
+      points(tic[,1]/60, tic[,2], col = cols[classnames[[k]][j]], pch = pch[classnames[[k]][j]], type="l")
+      colvect<-append(colvect,cols[classnames[[k]][j]])
+    }
+    for (j in 1:length(classnames[[l]])) {
+      # i=class2names[j]
+      tic <- TIC[[classnames[[l]][j]]]
+      points(tic[,1]/60, -tic[,2], col = cols[classnames[[l]][j]], pch = pch[classnames[[l]][j]], type="l")
+      colvect<-append(colvect,cols[classnames[[l]][j]])
+    }
+    legend("topright",paste(basename(files[c(classnames[[k]],classnames[[l]])])), col = colvect, lty = lty, pch = pch)
+
+  }#end length ==2
+  dev.off() #pdf(pdfname,w=16,h=10)
+
+  invisible(TIC)
+}
+
+
+
+##
+##  Get the polarities from all the samples of a condition
+#@author Misharl Monsoor misharl.monsoor@sb-roscoff.fr ABiMS TEAM
+#@author Gildas Le Corguille lecorguille@sb-roscoff.fr ABiMS TEAM
+getSampleMetadata <- function(xcmsSet=NULL, sampleMetadataOutput="sampleMetadata.tsv") {
+  cat("Creating the sampleMetadata file...\n")
+
+  #Create the sampleMetada dataframe
+  sampleMetadata=xset@phenoData
+  sampleNamesOrigin=rownames(sampleMetadata)
+  sampleNamesMakeNames=make.names(sampleNamesOrigin)
+    
+  if (any(duplicated(sampleNamesMakeNames))) {
+    write("\n\nERROR: Usually, R has trouble to deal with special characters in its column names, so it rename them using make.names().\nIn your case, at least two columns after the renaming obtain the same name, thus XCMS will collapse those columns per name.", stderr())
+    for (sampleName in sampleNamesOrigin) {
+      write(paste(sampleName,"\t->\t",make.names(sampleName)),stderr())
+    }
+    stop("\n\nERROR: One or more of your files will not be import by xcmsSet. It may due to bad characters in their filenames.")
+  }
+
+  if (!all(sampleNamesOrigin == sampleNamesMakeNames)) {
+    cat("\n\nWARNING: Usually, R has trouble to deal with special characters in its column names, so it rename them using make.names()\nIn your case, one or more sample names will be renamed in the sampleMetadata and dataMatrix files:\n")
+    for (sampleName in sampleNamesOrigin) {
+      cat(paste(sampleName,"\t->\t",make.names(sampleName),"\n"))
+    }
+  }
+
+  sampleMetadata$sampleMetadata=sampleNamesMakeNames
+  sampleMetadata=cbind(sampleMetadata["sampleMetadata"],sampleMetadata["class"]) #Reorder columns
+  rownames(sampleMetadata)=NULL
+
+  #Create a list of files name in the current directory
+  list_files=xset@filepaths
+  #For each sample file, the following actions are done
+  for (file in list_files){
+    #Check if the file is in the CDF format
+    if (!mzR:::netCDFIsFile(file)){
+
+      # If the column isn't exist, with add one filled with NA
+      if (is.null(sampleMetadata$polarity)) sampleMetadata$polarity=NA
+
+      #Create a simple xcmsRaw object for each sample
+      xcmsRaw=xcmsRaw(file)
+      #Extract the polarity (a list of polarities)
+      polarity=xcmsRaw@polarity
+      #Verify if all the scans have the same polarity
+      uniq_list=unique(polarity)
+      if (length(uniq_list)>1){
+        polarity="mixed"
+      } else {
+        polarity=as.character(uniq_list)
+      }
+      #Transforms the character to obtain only the sample name
+      filename=basename(file)
+      library(tools)
+      samplename=file_path_sans_ext(filename)
+
+      #Set the polarity attribute
+      sampleMetadata$polarity[sampleMetadata$sampleMetadata==samplename]=polarity
+      
+      #Delete xcmsRaw object because it creates a bug for the fillpeaks step
+      rm(xcmsRaw)
+    }
+
+  }
+
+  write.table(sampleMetadata, sep="\t", quote=FALSE, row.names=FALSE, file=sampleMetadataOutput)
+
+  return(list("sampleNamesOrigin"=sampleNamesOrigin,"sampleNamesMakeNames"=sampleNamesMakeNames))
+
+}
+
+
+##
+## This function check if xcms will found all the files
+##
+#@author Gildas Le Corguille lecorguille@sb-roscoff.fr ABiMS TEAM
+checkFilesCompatibilityWithXcms <- function(directory) {
+  cat("Checking files filenames compatibilities with xmcs...\n")
+  # WHAT XCMS WILL FIND
+  filepattern <- c("[Cc][Dd][Ff]", "[Nn][Cc]", "([Mm][Zz])?[Xx][Mm][Ll]","[Mm][Zz][Dd][Aa][Tt][Aa]", "[Mm][Zz][Mm][Ll]")
+  filepattern <- paste(paste("\\.", filepattern, "$", sep = ""),collapse = "|")
+  info <- file.info(directory)
+  listed <- list.files(directory[info$isdir], pattern = filepattern,recursive = TRUE, full.names = TRUE)
+  files <- c(directory[!info$isdir], listed)
+  files_abs <- file.path(getwd(), files)
+  exists <- file.exists(files_abs)
+  files[exists] <- files_abs[exists]
+  files[exists] <- sub("//","/",files[exists])
+
+  # WHAT IS ON THE FILESYSTEM
+  filesystem_filepaths=system(paste("find $PWD/",directory," -not -name '\\.*' -not -path '*conda-env*' -type f -name \"*\"", sep=""), intern=T)
+  filesystem_filepaths=filesystem_filepaths[grep(filepattern, filesystem_filepaths, perl=T)]
+
+  # COMPARISON
+  if (!is.na(table(filesystem_filepaths %in% files)["FALSE"])) { 
+    write("\n\nERROR: List of the files which will not be imported by xcmsSet",stderr())
+    write(filesystem_filepaths[!(filesystem_filepaths %in% files)],stderr())
+    stop("\n\nERROR: One or more of your files will not be import by xcmsSet. It may due to bad characters in their filenames.")
+
+  }
+}
+
+
+
+##
+## This function check if XML contains special caracters. It also checks integrity and completness.
+##
+#@author Misharl Monsoor misharl.monsoor@sb-roscoff.fr ABiMS TEAM
+checkXmlStructure <- function (directory) {
+  cat("Checking XML structure...\n")
+
+  cmd=paste("IFS=$'\n'; for xml in $(find",directory,"-not -name '\\.*' -not -path '*conda-env*' -type f -iname '*.*ml*'); do if [ $(xmllint --nonet --noout \"$xml\" 2> /dev/null; echo $?) -gt 0 ]; then echo $xml;fi; done;")
+  capture=system(cmd,intern=TRUE)
+
+  if (length(capture)>0){
+    #message=paste("The following mzXML or mzML file is incorrect, please check these files first:",capture)
+    write("\n\nERROR: The following mzXML or mzML file(s) are incorrect, please check these files first:", stderr())
+    write(capture, stderr())
+    stop("ERROR: xcmsSet cannot continue with incorrect mzXML or mzML files")
+  }
+   
+}
+
+
+##
+## This function check if XML contain special characters
+##
+#@author Misharl Monsoor misharl.monsoor@sb-roscoff.fr ABiMS TEAM
+deleteXmlBadCharacters<- function (directory) {
+  cat("Checking Non ASCII characters in the XML...\n")
+
+  processed=F
+  l=system( paste("find",directory, "-not -name '\\.*' -not -path '*conda-env*' -type f -iname '*.*ml*'"),intern=TRUE) 
+  for (i in l){
+    cmd=paste("LC_ALL=C grep '[^ -~]' \"",i,"\"",sep="")
+    capture=suppressWarnings(system(cmd,intern=TRUE))
+    if (length(capture)>0){
+      cmd=paste("perl -i -pe 's/[^[:ascii:]]//g;'",i)
+      print( paste("WARNING: Non ASCII characters have been removed from the ",i,"file") )
+      c=system(cmd,intern=TRUE)
+      capture=""
+      processed=T 
+    }
+  }
+  if (processed) cat("\n\n")
+  return(processed)
+}
+
+
+## 
+## This function will compute MD5 checksum to check the data integrity
+##
+#@author Gildas Le Corguille lecorguille@sb-roscoff.fr
+getMd5sum <- function (directory) {
+  cat("Compute md5 checksum...\n")
+  # WHAT XCMS WILL FIND
+  filepattern <- c("[Cc][Dd][Ff]", "[Nn][Cc]", "([Mm][Zz])?[Xx][Mm][Ll]","[Mm][Zz][Dd][Aa][Tt][Aa]", "[Mm][Zz][Mm][Ll]")
+  filepattern <- paste(paste("\\.", filepattern, "$", sep = ""),collapse = "|")
+  info <- file.info(directory)
+  listed <- list.files(directory[info$isdir], pattern = filepattern,recursive = TRUE, full.names = TRUE)
+  files <- c(directory[!info$isdir], listed)
+  exists <- file.exists(files)
+  files <- files[exists]
+
+  library(tools)
+
+  #cat("\n\n")
+
+  return(as.matrix(md5sum(files)))
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/macros.xml	Fri Apr 08 10:38:52 2016 -0400
@@ -0,0 +1,51 @@
+<?xml version="1.0"?>
+<macros>
+    <xml name="requirements">
+        <requirements>
+            <requirement type="package" version="3.1.2">R</requirement>
+	    <requirement type="package" version="0.4_1">r-snow</requirement>
+            <requirement type="package" version="1.44.0">bioconductor-xcms</requirement>
+	    <requirement type="package" version="1.1_4">r-batch</requirement>
+        </requirements>
+    </xml>
+    <xml name="stdio">
+        <stdio>
+            <exit_code range="1" level="fatal" />
+        </stdio>
+    </xml>
+
+    <token name="@COMMAND_XCMS_SCRIPT@">
+        LANG=C Rscript $__tool_directory__/xcms.r
+    </token>
+
+    <token name="@COMMAND_LOG_EXIT@">
+        ;
+        return=\$?;
+        mv log.txt $log;
+        cat $log;
+        sh -c "exit \$return"
+    </token>
+
+    <token name="@HELP_AUTHORS@">
+.. class:: infomark
+
+**Authors**  Colin A. Smith csmith@scripps.edu, Ralf Tautenhahn rtautenh@gmail.com, Steffen Neumann sneumann@ipb-halle.de, Paul Benton hpaul.benton08@imperial.ac.uk and Christopher Conley cjconley@ucdavis.edu 
+
+.. class:: infomark
+
+**Galaxy integration** ABiMS TEAM - UPMC/CNRS - Station biologique de Roscoff and Yann Guitton yann.guitton@oniris-nantes.fr - part of Workflow4Metabolomics.org [W4M]
+
+ | Contact support@workflow4metabolomics.org for any questions or concerns about the Galaxy implementation of this tool.
+
+---------------------------------------------------
+
+    </token>
+
+
+    <xml name="citation">
+        <citations>
+            <citation type="doi">10.1021/ac051437y</citation>
+            <citation type="doi">10.1093/bioinformatics/btu813</citation>
+        </citations>
+    </xml>
+</macros>
--- a/planemo.sh	Mon Feb 22 16:25:05 2016 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-planemo shed_init -f --name=xcms_xcms_set --owner=lecorguille --description="[W4M][GC-MS] XCMS R Package - Preprocessing - peaks calling in NetCDF/mzXML files" --homepage_url="http://workflow4metabolomics.org" --long_description="Part of the W4M project: http://workflow4metabolomics.org\n\nXCMS: http://www.bioconductor.org/packages/release/bioc/html/xcms.html\n\nFiltration and Peak Identification using xcmsSet function from xcms R package to preprocess LC/MS data for relative quantification and statistical analysis\n\nBEWARE: this tool don't come with its script. You will need to install the dedicated package_xcms_w4m_script too" --category="Metabolomics"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/planemo_test.sh	Fri Apr 08 10:38:52 2016 -0400
@@ -0,0 +1,18 @@
+planemo conda_init
+planemo conda_install .
+planemo test --install_galaxy --conda_dependency_resolution --galaxy_branch "dev"
+
+#All 2 test(s) executed passed.
+#abims_xcms_xcmsSet[0]: passed
+#abims_xcms_xcmsSet[1]: passed
+
+
+
+
+source /w/galaxy/dev/shed_tools_tool_dependency_dir/R/3.1.2/iuc/package_r_3_1_2/1ca39eb16186/env.sh
+source /w/galaxy/dev/shed_tools_tool_dependency_dir/bioconductor-xcms/1.44.0/lecorguille/package_bioconductor_xcms_1_44_0/0c38f7d43e08/env.sh
+planemo test --install_galaxy --galaxy_branch "dev"
+
+#All 2 test(s) executed passed.
+#abims_xcms_xcmsSet[0]: passed
+#abims_xcms_xcmsSet[1]: passed
\ No newline at end of file
--- a/test-data/log.txt	Mon Feb 22 16:25:05 2016 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-	PACKAGE INFO
-parallel	3.1.1
-BiocGenerics	0.12.1
-Biobase	2.26.0
-Rcpp	0.11.5
-mzR	2.0.0
-igraph	0.7.1
-xcms	1.42.0
-snow	0.3.13
-batch	1.1.4
-
-
-	ARGUMENTS INFO
-zipfile	/w/galaxy/galaxy4metabolomics/galaxy-dist/database/files/041/dataset_41794.dat
-xfunction	xcmsSet
-nSlaves	8
-method	matchedFilter
-step	0.01
-fwhm	4
-max	50
-snthresh	1
-steps	2
-
-
-	INFILE PROCESSING INFO
-
-
-	MAIN PROCESSING INFO
-Starting snow cluster with 8 local sockets.
-Detecting features in file # 1 : HU_neg_028.mzXML 
-Detecting features in file # 2 : HU_neg_060.mzXML 
-Detecting features in file # 3 : HU_neg_051.mzXML 
-Detecting features in file # 4 : HU_neg_017.mzXML 
-Detecting features in file # 5 : HU_neg_034.mzXML 
-Detecting features in file # 6 : Blanc09.mzXML 
-Detecting features in file # 7 : Blanc06.mzXML 
-Detecting features in file # 8 : Blanc12.mzXML 
-Detecting features in file # 9 : Blanc04.mzXML 
-
-
-	XSET OBJECT INFO
-An "xcmsSet" object with 9 samples
-
-Time range: 0.7-1140 seconds (0-19 minutes)
-Mass range: 50.0019-999.9863 m/z
-Peaks: 135846 (about 15094 per sample)
-Peak Groups: 0 
-Sample classes: bio, blank 
-
-Profile settings: method = bin
-                  step = 0.01
-
-Memory usage: 13.8 MB
-
-
-	DONE
Binary file test-data/sacuri.zip has changed
Binary file test-data/sacuri_current_root.zip has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/sacuri_dir_root.log.txt	Fri Apr 08 10:38:52 2016 -0400
@@ -0,0 +1,67 @@
+	PACKAGE INFO
+parallel	3.2.2
+BiocGenerics	0.16.1
+Biobase	2.30.0
+Rcpp	0.12.2
+mzR	2.4.1
+xcms	1.44.0
+snow	0.4.1
+batch	1.1.4
+
+
+	ARGUMENTS INFO
+zipfile	/tmp/tmpAmhDSv/files/000/dataset_1.dat
+xfunction	xcmsSet
+xsetRdataOutput	/tmp/tmpAmhDSv/files/000/dataset_2.dat
+sampleMetadataOutput	/tmp/tmpAmhDSv/files/000/dataset_3.dat
+ticspdf	/tmp/tmpAmhDSv/files/000/dataset_4.dat
+bicspdf	/tmp/tmpAmhDSv/files/000/dataset_5.dat
+nSlaves	1
+method	matchedFilter
+step	0.01
+fwhm	4
+max	50
+snthresh	1
+steps	2
+
+
+	INFILE PROCESSING INFO
+
+
+	ARGUMENTS PROCESSING INFO
+files_root_directory	 sacuri 
+Compute md5 checksum...
+Checking XML structure...
+Checking files filenames compatibilities with xmcs...
+
+
+	MAIN PROCESSING INFO
+55:579 60:1264 65:1988 70:2602 75:3287 80:3894 85:4629 90:5499 95:6174 100:6672 105:7194 110:7737 115:8205 120:8673 125:9117 130:9532 135:9973 140:10562 145:10945 150:11380 155:11788 160:12163 165:12477 170:12835 175:13276 180:13632 185:14142 190:14489 195:15022 200:15360 205:15716 210:15992 215:16297 220:16669 225:16895 230:17227 235:17487 240:17657 245:17956 250:18238 255:18464 260:18779 265:18969 270:19194 275:19503 280:19798 285:20041 290:20282 295:20498 300:20673 305:21027 310:21220 315:21403 320:21881 325:22431 330:22630 335:22775 340:22969 345:23144 350:23282 355:23425 360:24001 365:24131 370:24242 375:24362 380:24475 385:24580 390:24763 395:25006 400:25190 405:25308 410:25446 415:25555 420:25648 425:25761 430:25962 435:26057 440:26172 445:26260 450:26343 455:26414 460:26608 465:26720 470:26827 475:26979 480:27048 485:27126 490:27218 495:27272 500:27341 505:27433 510:27487 515:27535 520:27600 525:27758 530:27820 535:27896 540:27940 545:28085 550:28141 555:28298 560:28711 565:28732 570:28760 575:28792 580:28822 585:28995 590:29019 595:29059 600:29090 605:29119 610:29151 615:29182 620:29202 625:29238 630:29266 635:29294 640:29317 645:29472 650:29494 655:29599 660:29628 665:29781 670:29812 675:29868 680:29895 685:29923 690:29953 695:29973 700:30032 705:30048 710:30069 715:30089 720:30107 725:30128 730:30144 735:30168 740:30286 745:30312 750:30331 755:30369 760:30402 765:30420 770:30438 775:30584 780:30620 785:30647 790:30666 795:30682 800:30701 805:30712 810:30785 815:30801 820:30816 825:30831 830:30945 835:30979 840:31004 845:31102 850:31114 855:31132 860:31147 865:31166 870:31175 875:31205 880:31227 885:31253 890:31264 895:31280 900:31298 905:31314 910:31325 915:31337 920:31351 925:31370 930:31389 935:31405 940:31416 945:31442 950:31453 955:31470 960:31486 965:31680 970:31794 975:31803 980:31811 985:31836 990:32026 995:32126 1000:32142 
+55:826 60:1707 65:2578 70:3373 75:4271 80:5020 85:5907 90:6893 95:7729 100:8434 105:9094 110:9712 115:10298 120:10911 125:11430 130:12005 135:12573 140:13234 145:13750 150:14314 155:14841 160:15333 165:15894 170:16323 175:16847 180:17319 185:17867 190:18337 195:18957 200:19402 205:19925 210:20275 215:20746 220:21271 225:21607 230:22062 235:22473 240:22748 245:23187 250:23559 255:23845 260:24284 265:24628 270:24973 275:25315 280:25726 285:26019 290:26327 295:26632 300:26886 305:27300 310:27579 315:27880 320:28450 325:29107 330:29377 335:29609 340:29860 345:30141 350:30369 355:30586 360:31214 365:31446 370:31656 375:31860 380:32067 385:32210 390:32451 395:32690 400:32909 405:33093 410:33312 415:33488 420:33687 425:33837 430:34074 435:34229 440:34403 445:34541 450:34702 455:34798 460:35015 465:35207 470:35390 475:35528 480:35633 485:35754 490:35880 495:35964 500:36076 505:36165 510:36251 515:36336 520:36439 525:36588 530:36699 535:36810 540:36882 545:37060 550:37124 555:37263 560:37718 565:37789 570:37829 575:37878 580:37915 585:38016 590:38054 595:38102 600:38137 605:38188 610:38237 615:38277 620:38312 625:38359 630:38387 635:38413 640:38450 645:38558 650:38590 655:38714 660:38749 665:38864 670:38914 675:38967 680:39003 685:39038 690:39073 695:39096 700:39199 705:39227 710:39256 715:39279 720:39302 725:39334 730:39358 735:39388 740:39513 745:39537 750:39557 755:39615 760:39647 765:39674 770:39714 775:39861 780:39903 785:39937 790:39956 795:39976 800:40003 805:40036 810:40102 815:40121 820:40143 825:40173 830:40202 835:40258 840:40376 845:40468 850:40488 855:40510 860:40524 865:40542 870:40565 875:40593 880:40607 885:40620 890:40642 895:40669 900:40709 905:40727 910:40749 915:40770 920:40795 925:40815 930:40831 935:40859 940:40881 945:40895 950:40909 955:40927 960:40955 965:41172 970:41292 975:41313 980:41336 985:41355 990:41481 995:41632 1000:41652 
+55:514 60:1053 65:1583 70:2125 75:2648 80:3151 85:3818 90:4547 95:5274 100:5669 105:6164 110:6556 115:6922 120:7401 125:7782 130:8159 135:8514 140:9002 145:9296 150:9632 155:9960 160:10258 165:10541 170:10816 175:11141 180:11384 185:11827 190:12048 195:12370 200:12608 205:12929 210:13142 215:13337 220:13642 225:13802 230:14032 235:14204 240:14349 245:14614 250:14773 255:14935 260:15217 265:15344 270:15512 275:15763 280:15966 285:16095 290:16255 295:16456 300:16589 305:16806 310:16956 315:17091 320:17476 325:17978 330:18165 335:18251 340:18393 345:18517 350:18606 355:18739 360:19210 365:19275 370:19358 375:19420 380:19510 385:19577 390:19668 395:19833 400:19955 405:20040 410:20154 415:20244 420:20323 425:20414 430:20577 435:20639 440:20724 445:20785 450:20835 455:20897 460:21073 465:21160 470:21235 475:21428 480:21486 485:21538 490:21599 495:21640 500:21685 505:21738 510:21773 515:21798 520:21863 525:22005 530:22044 535:22121 540:22162 545:22286 550:22340 555:22493 560:22814 565:22842 570:22869 575:22895 580:22927 585:23067 590:23090 595:23119 600:23134 605:23172 610:23197 615:23223 620:23242 625:23275 630:23304 635:23314 640:23339 645:23465 650:23490 655:23600 660:23619 665:23756 670:23781 675:23849 680:23870 685:23895 690:23910 695:23929 700:23992 705:24015 710:24026 715:24044 720:24068 725:24089 730:24122 735:24146 740:24242 745:24259 750:24280 755:24307 760:24330 765:24352 770:24373 775:24493 780:24514 785:24551 790:24563 795:24584 800:24605 805:24637 810:24684 815:24704 820:24715 825:24723 830:24736 835:24763 840:24854 845:24938 850:24950 855:24968 860:24983 865:25000 870:25019 875:25030 880:25046 885:25067 890:25084 895:25100 900:25119 905:25132 910:25141 915:25153 920:25170 925:25184 930:25199 935:25211 940:25224 945:25240 950:25251 955:25266 960:25278 965:25437 970:25548 975:25560 980:25568 985:25580 990:25784 995:25822 
+55:568 60:1154 65:1793 70:2433 75:3064 80:3635 85:4363 90:5039 95:5627 100:6077 105:6641 110:7124 115:7603 120:8076 125:8439 130:8836 135:9206 140:9628 145:9963 150:10288 155:10623 160:10937 165:11304 170:11676 175:12092 180:12358 185:12768 190:13022 195:13435 200:13746 205:14079 210:14229 215:14412 220:14688 225:14901 230:15155 235:15314 240:15475 245:15750 250:15938 255:16115 260:16396 265:16512 270:16704 275:16893 280:17127 285:17336 290:17484 295:17668 300:17788 305:18044 310:18177 315:18318 320:18785 325:19242 330:19399 335:19491 340:19646 345:19785 350:19886 355:19994 360:20481 365:20574 370:20675 375:20743 380:20827 385:20903 390:21012 395:21202 400:21349 405:21433 410:21527 415:21590 420:21649 425:21741 430:21888 435:21942 440:21998 445:22067 450:22121 455:22169 460:22293 465:22371 470:22476 475:22629 480:22666 485:22706 490:22765 495:22820 500:22881 505:22934 510:22988 515:23039 520:23087 525:23185 530:23226 535:23293 540:23332 545:23463 550:23513 555:23674 560:24058 565:24076 570:24095 575:24132 580:24156 585:24262 590:24278 595:24313 600:24336 605:24360 610:24396 615:24421 620:24459 625:24479 630:24498 635:24521 640:24542 645:24692 650:24712 655:24831 660:24853 665:24976 670:24991 675:25048 680:25063 685:25079 690:25095 695:25114 700:25163 705:25184 710:25202 715:25213 720:25227 725:25250 730:25276 735:25289 740:25384 745:25400 750:25416 755:25458 760:25483 765:25508 770:25530 775:25654 780:25671 785:25692 790:25715 795:25742 800:25752 805:25763 810:25832 815:25845 820:25862 825:25875 830:25890 835:25905 840:26020 845:26094 850:26108 855:26134 860:26150 865:26163 870:26174 875:26185 880:26200 885:26212 890:26225 895:26236 900:26250 905:26262 910:26279 915:26287 920:26299 925:26317 930:26325 935:26339 940:26356 945:26367 950:26381 955:26392 960:26404 965:26592 970:26701 975:26720 980:26743 985:26759 990:26969 995:26990 
+
+
+null device 
+          1 
+Creating the sampleMetadata file...
+Creating TIC pdf...
+Creating BIC pdf...
+
+
+	XSET OBJECT INFO
+An "xcmsSet" object with 4 samples
+
+Time range: 0.7-1139.7 seconds (0-19 minutes)
+Mass range: 50.0021-999.9863 m/z
+Peaks: 59359 (about 14840 per sample)
+Peak Groups: 0 
+Sample classes: bio, blank 
+
+Profile settings: method = bin
+                  step = 0.01
+
+Memory usage: 6.02 MB
+
+
+	DONE
Binary file test-data/sacuri_dir_root.zip has changed
--- a/test-data/sampleMetadata.tsv	Mon Feb 22 16:25:05 2016 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-sampleMetadata	class	polarity
-HU_neg_028	bio	negative
-HU_neg_060	bio	negative
-HU_neg_051	bio	negative
-HU_neg_017	bio	negative
-HU_neg_034	bio	negative
-Blanc09	blank	negative
-Blanc06	blank	negative
-Blanc12	blank	negative
-Blanc04	blank	negative
Binary file test-data/xset.BPCs_raw.pdf has changed
Binary file test-data/xset.RData has changed
Binary file test-data/xset.TICs_raw.pdf has changed
--- a/tool_dependencies.xml	Mon Feb 22 16:25:05 2016 -0500
+++ b/tool_dependencies.xml	Fri Apr 08 10:38:52 2016 -0400
@@ -1,12 +1,9 @@
 <?xml version="1.0"?>
 <tool_dependency>
     <package name="R" version="3.1.2">
-        <repository changeset_revision="c987143177d4" name="package_r_3_1_2" owner="iuc" toolshed="https://toolshed.g2.bx.psu.edu" />
+        <repository changeset_revision="4d2fd1413b56" name="package_r_3_1_2" owner="iuc" toolshed="https://toolshed.g2.bx.psu.edu" />
     </package>
-    <package name="xcms" version="1.44.0">
-        <repository changeset_revision="4443617bdd85" name="package_r_xcms_1_44_0" owner="lecorguille" toolshed="https://toolshed.g2.bx.psu.edu" />
-    </package>
-    <package name="xcms_w4m_script" version="2.2.0">
-        <repository changeset_revision="115cf2b43a3c" name="package_xcms_w4m_script_2_2_0" owner="lecorguille" toolshed="https://toolshed.g2.bx.psu.edu" />
+    <package name="bioconductor-xcms" version="1.44.0">
+        <repository changeset_revision="58ebb405a3d6" name="package_bioconductor_xcms_1_44_0" owner="lecorguille" toolshed="https://toolshed.g2.bx.psu.edu" />
     </package>
 </tool_dependency>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xcms.r	Fri Apr 08 10:38:52 2016 -0400
@@ -0,0 +1,246 @@
+#!/usr/bin/env Rscript
+# xcms.r version="2.2.0"
+#Authors ABIMS TEAM
+#BPC Addition from Y.guitton
+
+
+# ----- LOG FILE -----
+log_file=file("log.txt", open = "wt")
+sink(log_file)
+sink(log_file, type = "output")
+
+
+# ----- PACKAGE -----
+cat("\tPACKAGE INFO\n")
+#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="")
+}
+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"); 
+
+
+
+
+
+# ----- ARGUMENTS -----
+cat("\tARGUMENTS INFO\n")
+listArguments = parseCommandArgs(evaluate=FALSE) #interpretation of arguments given in command line as an R list of objects
+write.table(as.matrix(listArguments), col.names=F, quote=F, sep='\t')
+
+cat("\n\n");
+
+
+# ----- ARGUMENTS PROCESSING -----
+cat("\tINFILE PROCESSING INFO\n")
+
+#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
+}
+
+#Import the different functions
+source_local("lib.r")
+
+cat("\n\n")
+
+#Import the different functions
+
+# ----- PROCESSING INFILE -----
+cat("\tARGUMENTS PROCESSING INFO\n")
+
+# Save arguments to generate a report
+if (!exists("listOFlistArguments")) listOFlistArguments=list()
+listOFlistArguments[[paste(format(Sys.time(), "%y%m%d-%H:%M:%S_"),listArguments[["xfunction"]],sep="")]] = listArguments
+
+
+#saving the commun parameters
+thefunction = listArguments[["xfunction"]]; listArguments[["xfunction"]]=NULL #delete from the list of arguments
+
+xsetRdataOutput = paste(thefunction,"RData",sep=".")
+if (!is.null(listArguments[["xsetRdataOutput"]])){
+  xsetRdataOutput = listArguments[["xsetRdataOutput"]]; listArguments[["xsetRdataOutput"]]=NULL
+}
+
+rplotspdf = "Rplots.pdf"
+if (!is.null(listArguments[["rplotspdf"]])){
+  rplotspdf = listArguments[["rplotspdf"]]; listArguments[["rplotspdf"]]=NULL
+}
+
+sampleMetadataOutput = "sampleMetadata.tsv"
+if (!is.null(listArguments[["sampleMetadataOutput"]])){
+  sampleMetadataOutput = listArguments[["sampleMetadataOutput"]]; listArguments[["sampleMetadataOutput"]]=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)
+  }
+}
+
+# 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")
+
+    # 
+    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)
+}
+
+
+#addition of xset object to the list of arguments in the first position
+if (exists("xset")){
+  listArguments=append(list(xset), listArguments)
+}
+
+cat("\n\n")
+
+
+
+
+
+
+# ----- MAIN PROCESSING INFO -----
+cat("\tMAIN PROCESSING INFO\n")
+
+
+#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")
+  }
+
+}
+
+#change the default display settings
+#dev.new(file="Rplots.pdf", width=16, height=12)
+pdf(file=rplotspdf, width=16, height=12)
+if (thefunction == "group") {
+  par(mfrow=c(2,2))
+}
+#else if (thefunction == "retcor") {
+#try to change the legend display
+#     par(xpd=NA)
+#     par(xpd=T, mar=par()$mar+c(0,0,0,4))
+#}
+
+
+#execution of the function "thefunction" with the parameters given in "listArguments"
+xset = do.call(thefunction, listArguments)
+
+
+cat("\n\n")
+
+dev.off() #dev.new(file="Rplots.pdf", width=16, height=12)
+
+if (thefunction  == "xcmsSet") {
+
+  #transform the files absolute pathways into relative pathways
+  xset@filepaths<-sub(paste(getwd(),"/",sep="") ,"", xset@filepaths)
+
+  if(exists("zipfile") && (zipfile!="")) {
+    
+    #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
+
+    }
+
+  }
+
+}
+
+# -- TIC --
+if (thefunction == "xcmsSet") {
+  sampleNamesList = getSampleMetadata(xcmsSet=xset, sampleMetadataOutput=sampleMetadataOutput)
+  getTICs(xcmsSet=xset, pdfname=ticspdf,rt="raw")
+  getBPCs(xcmsSet=xset,rt="raw",pdfname=bicspdf)
+} else if (thefunction == "retcor") {
+  getTICs(xcmsSet=xset, pdfname=ticspdf,rt="corrected")
+  getBPCs(xcmsSet=xset,rt="corrected",pdfname=bicspdf)
+}
+
+cat("\n\n")
+
+
+# ----- EXPORT -----
+
+cat("\tXSET OBJECT INFO\n")
+print(xset)
+#delete the parameters to avoid the passage to the next tool in .RData image
+
+
+#saving R data in .Rdata file to save the variables used in the present tool
+objects2save = c("xset","zipfile","listOFlistArguments","md5sumList","sampleNamesList")
+save(list=objects2save[objects2save %in% ls()], file=xsetRdataOutput)
+
+cat("\n\n")
+
+
+cat("\tDONE\n")
+