view xcms_xcmsSet.r @ 20:41cf6f56d1b6 draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit 37b0a6a7686f701e4bf00db97ae2c1b82cd6e989
author lecorguille
date Fri, 09 Nov 2018 15:10:13 -0500
parents b62808a2a008
children 87df3692c68d
line wrap: on
line source

#!/usr/bin/env Rscript

# ----- LOG FILE -----
log_file <- file("log.txt", open="wt")
sink(log_file)
sink(log_file, type = "output")


# ----- PACKAGE -----
cat("\tSESSION INFO\n")

#Import the different functions
source_local <- function(fname){ argv <- commandArgs(trailingOnly=FALSE); base_dir <- dirname(substring(argv[grep("--file=", argv)], 8)); source(paste(base_dir, fname, sep="/")) }
source_local("lib.r")
source_local("lib-xcms3.x.x.r")

pkgs <- c("xcms","batch")
loadAndDisplayPackages(pkgs)
cat("\n\n");


# ----- ARGUMENTS -----
cat("\tARGUMENTS INFO\n")
args <- parseCommandArgs(evaluate = FALSE) #interpretation of arguments given in command line as an R list of objects
write.table(as.matrix(args), col.names=F, quote=F, sep='\t')

cat("\n\n")


# ----- PROCESSING INFILE -----
cat("\tARGUMENTS PROCESSING INFO\n")

#saving the commun parameters
BPPARAM <- MulticoreParam(1)
if (!is.null(args$BPPARAM)){
    BPPARAM <- MulticoreParam(args$BPPARAM)
}
register(BPPARAM)

#saving the specific parameters
if (!is.null(args$filterAcquisitionNum)) filterAcquisitionNumParam <- args$filterAcquisitionNum
if (!is.null(args$filterRt)) filterRtParam <- args$filterRt
if (!is.null(args$filterMz)) filterMzParam <- args$filterMz

method <- args$method

if (!is.null(args$roiList)){
    cat("\t\troiList provided\n")
    args$roiList <- list(getDataFrameFromFile(args$roiList))
    print(args$roiList)
}

cat("\n\n")

# ----- INFILE PROCESSING -----
cat("\tINFILE PROCESSING INFO\n")

#image is an .RData file necessary to use xset variable given by previous tools
load(args$image)
if (!exists("raw_data")) stop("\n\nERROR: The RData doesn't contain any object called 'raw_data' which is provided by the tool: MSnbase readMSData")

# Handle infiles
if (!exists("singlefile")) singlefile <- NULL
if (!exists("zipfile")) zipfile <- NULL
rawFilePath <- getRawfilePathFromArguments(singlefile, zipfile, args)
zipfile <- rawFilePath$zipfile
singlefile <- rawFilePath$singlefile
directory <- retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)


cat("\n\n")


# ----- MAIN PROCESSING INFO -----
cat("\tMAIN PROCESSING INFO\n")


cat("\t\tCOMPUTE\n")

## Get the full path to the files
files <- getMSFiles(directory)

cat("\t\t\tApply filter[s] (if asked)\n")
if (exists("filterAcquisitionNumParam"))  raw_data <- filterAcquisitionNum(raw_data, filterAcquisitionNumParam[1]:filterAcquisitionNumParam[2])
if (exists("filterRtParam")) raw_data <- filterRt(raw_data, filterRtParam)
if (exists("filterMzParam")) raw_data <- filterMz(raw_data, filterMzParam)
#Apply this filter only if file contain MS and MSn
if(length(unique(msLevel(raw_data)))!= 1){
	raw_data <- filterMsLevel(raw_data,msLevel=1)
}

cat("\t\t\tChromatographic peak detection\n")
# clear the arguement list to remove unexpected key/value as singlefile_galaxyPath or method ...
args <- args[names(args) %in% slotNames(do.call(paste0(method,"Param"), list()))]

findChromPeaksParam <- do.call(paste0(method,"Param"), args)
print(findChromPeaksParam)
xdata <- findChromPeaks(raw_data, param=findChromPeaksParam)

# Check if there are no peaks
if (nrow(chromPeaks(xdata)) == 0) stop("No peaks were detected. You should review your settings")

# Transform the files absolute pathways into relative pathways
xdata@processingData@files <- sub(paste(getwd(), "/", sep="") , "", xdata@processingData@files)

# Create a sampleMetada file
sampleNamesList <- getSampleMetadata(xdata=xdata, sampleMetadataOutput="sampleMetadata.tsv")

cat("\t\t\tCompute and Store TIC and BPI\n")
chromTIC = chromatogram(xdata, aggregationFun = "sum")
chromBPI = chromatogram(xdata, aggregationFun = "max")

cat("\n\n")

# ----- EXPORT -----

cat("\tXCMSnExp OBJECT INFO\n")
print(xdata)
cat("\n\n")

cat("\txcmsSet OBJECT INFO\n")
# Get the legacy xcmsSet object
xset <- getxcmsSetObject(xdata)
print(xset)
cat("\n\n")

#saving R data in .Rdata file to save the variables used in the present tool
objects2save <- c("xdata", "zipfile", "singlefile", "md5sumList", "sampleNamesList", "chromTIC", "chromBPI")
save(list=objects2save[objects2save %in% ls()], file="xcmsSet.RData")


cat("\tDONE\n")