comparison xcms_xcmsSet.r @ 15:b62808a2a008 draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit 9f72e947d9c241d11221cad561f3525d27231857
author lecorguille
date Tue, 18 Sep 2018 16:07:36 -0400
parents
children 87df3692c68d
comparison
equal deleted inserted replaced
14:363cce459fff 15:b62808a2a008
1 #!/usr/bin/env Rscript
2
3 # ----- LOG FILE -----
4 log_file <- file("log.txt", open="wt")
5 sink(log_file)
6 sink(log_file, type = "output")
7
8
9 # ----- PACKAGE -----
10 cat("\tSESSION INFO\n")
11
12 #Import the different functions
13 source_local <- function(fname){ argv <- commandArgs(trailingOnly=FALSE); base_dir <- dirname(substring(argv[grep("--file=", argv)], 8)); source(paste(base_dir, fname, sep="/")) }
14 source_local("lib.r")
15 source_local("lib-xcms3.x.x.r")
16
17 pkgs <- c("xcms","batch")
18 loadAndDisplayPackages(pkgs)
19 cat("\n\n");
20
21
22 # ----- ARGUMENTS -----
23 cat("\tARGUMENTS INFO\n")
24 args <- parseCommandArgs(evaluate = FALSE) #interpretation of arguments given in command line as an R list of objects
25 write.table(as.matrix(args), col.names=F, quote=F, sep='\t')
26
27 cat("\n\n")
28
29
30 # ----- PROCESSING INFILE -----
31 cat("\tARGUMENTS PROCESSING INFO\n")
32
33 #saving the commun parameters
34 BPPARAM <- MulticoreParam(1)
35 if (!is.null(args$BPPARAM)){
36 BPPARAM <- MulticoreParam(args$BPPARAM)
37 }
38 register(BPPARAM)
39
40 #saving the specific parameters
41 if (!is.null(args$filterAcquisitionNum)) filterAcquisitionNumParam <- args$filterAcquisitionNum
42 if (!is.null(args$filterRt)) filterRtParam <- args$filterRt
43 if (!is.null(args$filterMz)) filterMzParam <- args$filterMz
44
45 method <- args$method
46
47 if (!is.null(args$roiList)){
48 cat("\t\troiList provided\n")
49 args$roiList <- list(getDataFrameFromFile(args$roiList))
50 print(args$roiList)
51 }
52
53 cat("\n\n")
54
55 # ----- INFILE PROCESSING -----
56 cat("\tINFILE PROCESSING INFO\n")
57
58 #image is an .RData file necessary to use xset variable given by previous tools
59 load(args$image)
60 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")
61
62 # Handle infiles
63 if (!exists("singlefile")) singlefile <- NULL
64 if (!exists("zipfile")) zipfile <- NULL
65 rawFilePath <- getRawfilePathFromArguments(singlefile, zipfile, args)
66 zipfile <- rawFilePath$zipfile
67 singlefile <- rawFilePath$singlefile
68 directory <- retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)
69
70
71 cat("\n\n")
72
73
74 # ----- MAIN PROCESSING INFO -----
75 cat("\tMAIN PROCESSING INFO\n")
76
77
78 cat("\t\tCOMPUTE\n")
79
80 ## Get the full path to the files
81 files <- getMSFiles(directory)
82
83 cat("\t\t\tApply filter[s] (if asked)\n")
84 if (exists("filterAcquisitionNumParam")) raw_data <- filterAcquisitionNum(raw_data, filterAcquisitionNumParam[1]:filterAcquisitionNumParam[2])
85 if (exists("filterRtParam")) raw_data <- filterRt(raw_data, filterRtParam)
86 if (exists("filterMzParam")) raw_data <- filterMz(raw_data, filterMzParam)
87 #Apply this filter only if file contain MS and MSn
88 if(length(unique(msLevel(raw_data)))!= 1){
89 raw_data <- filterMsLevel(raw_data,msLevel=1)
90 }
91
92 cat("\t\t\tChromatographic peak detection\n")
93 # clear the arguement list to remove unexpected key/value as singlefile_galaxyPath or method ...
94 args <- args[names(args) %in% slotNames(do.call(paste0(method,"Param"), list()))]
95
96 findChromPeaksParam <- do.call(paste0(method,"Param"), args)
97 print(findChromPeaksParam)
98 xdata <- findChromPeaks(raw_data, param=findChromPeaksParam)
99
100 # Check if there are no peaks
101 if (nrow(chromPeaks(xdata)) == 0) stop("No peaks were detected. You should review your settings")
102
103 # Transform the files absolute pathways into relative pathways
104 xdata@processingData@files <- sub(paste(getwd(), "/", sep="") , "", xdata@processingData@files)
105
106 # Create a sampleMetada file
107 sampleNamesList <- getSampleMetadata(xdata=xdata, sampleMetadataOutput="sampleMetadata.tsv")
108
109 cat("\t\t\tCompute and Store TIC and BPI\n")
110 chromTIC = chromatogram(xdata, aggregationFun = "sum")
111 chromBPI = chromatogram(xdata, aggregationFun = "max")
112
113 cat("\n\n")
114
115 # ----- EXPORT -----
116
117 cat("\tXCMSnExp OBJECT INFO\n")
118 print(xdata)
119 cat("\n\n")
120
121 cat("\txcmsSet OBJECT INFO\n")
122 # Get the legacy xcmsSet object
123 xset <- getxcmsSetObject(xdata)
124 print(xset)
125 cat("\n\n")
126
127 #saving R data in .Rdata file to save the variables used in the present tool
128 objects2save <- c("xdata", "zipfile", "singlefile", "md5sumList", "sampleNamesList", "chromTIC", "chromBPI")
129 save(list=objects2save[objects2save %in% ls()], file="xcmsSet.RData")
130
131
132 cat("\tDONE\n")