comparison xcms_xcmsSet.r @ 32:2bf1cb023c94 draft

planemo upload for repository https://github.com/workflow4metabolomics/xcms commit e384d6dd5f410799ec211f73bca0b5d5d7bc651e
author lecorguille
date Thu, 01 Mar 2018 04:14:39 -0500
parents
children c363b9f1caef
comparison
equal deleted inserted replaced
31:e93153c07be0 32:2bf1cb023c94
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
16 pkgs <- c("xcms","batch")
17 loadAndDisplayPackages(pkgs)
18 cat("\n\n");
19
20
21 # ----- ARGUMENTS -----
22 cat("\tARGUMENTS INFO\n")
23 args <- parseCommandArgs(evaluate = FALSE) #interpretation of arguments given in command line as an R list of objects
24 write.table(as.matrix(args), col.names=F, quote=F, sep='\t')
25
26 cat("\n\n")
27
28
29 # ----- PROCESSING INFILE -----
30 cat("\tARGUMENTS PROCESSING INFO\n")
31
32 #saving the commun parameters
33 BPPARAM <- MulticoreParam(1)
34 if (!is.null(args$BPPARAM)){
35 BPPARAM <- MulticoreParam(args$BPPARAM); args$BPPARAM <- NULL
36 }
37 register(BPPARAM)
38
39 #saving the specific parameters
40 method <- args$method; args$method <- NULL
41
42 cat("\n\n")
43
44 # ----- INFILE PROCESSING -----
45 cat("\tINFILE PROCESSING INFO\n")
46
47 # Handle infiles
48 if (!exists("singlefile")) singlefile <- NULL
49 if (!exists("zipfile")) zipfile <- NULL
50 rawFilePath <- getRawfilePathFromArguments(singlefile, zipfile, args)
51 zipfile <- rawFilePath$zipfile
52 singlefile <- rawFilePath$singlefile
53 args <- rawFilePath$args
54 directory <- retrieveRawfileInTheWorkingDirectory(singlefile, zipfile)
55
56 # Check some character issues
57 md5sumList <- list("origin" = getMd5sum(directory))
58 checkXmlStructure(directory)
59 checkFilesCompatibilityWithXcms(directory)
60
61
62 cat("\n\n")
63
64
65 # ----- MAIN PROCESSING INFO -----
66 cat("\tMAIN PROCESSING INFO\n")
67
68
69 cat("\t\tCOMPUTE\n")
70
71 ## Get the full path to the files
72 files <- getMSFiles(directory)
73
74 cat("\t\t\tCreate a phenodata data.frame\n")
75 s_groups <- sapply(files, function(x) tail(unlist(strsplit(dirname(x),"/")), n=1))
76 s_name <- tools::file_path_sans_ext(basename(files))
77 pd <- data.frame(sample_name=s_name, sample_group=s_groups, stringsAsFactors=FALSE)
78 print(pd)
79
80 cat("\t\t\tLoad Raw Data\n")
81 raw_data <- readMSData(files=files, pdata = new("NAnnotatedDataFrame", pd), mode="onDisk")
82
83 cat("\t\t\tChromatographic peak detection\n")
84 findChromPeaksParam <- do.call(paste0(method,"Param"), args)
85 print(findChromPeaksParam)
86 xdata <- findChromPeaks(raw_data, param=findChromPeaksParam)
87
88 # Check if there are no peaks
89 if (nrow(chromPeaks(xdata)) == 0) stop("No peaks were detected. You should review your settings")
90
91 # Transform the files absolute pathways into relative pathways
92 xdata@processingData@files <- sub(paste(getwd(), "/", sep="") , "", xdata@processingData@files)
93 save.image()
94 # Create a sampleMetada file
95 sampleNamesList <- getSampleMetadata(xdata=xdata, sampleMetadataOutput="sampleMetadata.tsv")
96
97 # Get the legacy xcmsSet object
98 xset <- getxcmsSetObject(xdata)
99
100 cat("\n\n")
101
102
103 # -- TIC --
104 cat("\t\tGET TIC GRAPH\n")
105 #@TODO: one day, use xdata instead of xset to draw the TICs and BPC or a complete other method
106 getTICs(xcmsSet=xset, rt="raw", pdfname="TICs.pdf")
107 getBPCs(xcmsSet=xset, rt="raw", pdfname="BICs.pdf")
108
109 cat("\n\n")
110
111 # ----- EXPORT -----
112
113 cat("\tXCMSnExp OBJECT INFO\n")
114 print(xdata)
115 cat("\n\n")
116
117 cat("\txcmsSet OBJECT INFO\n")
118 print(xset)
119 cat("\n\n")
120
121 #saving R data in .Rdata file to save the variables used in the present tool
122 objects2save <- c("xdata", "zipfile", "singlefile", "md5sumList", "sampleNamesList")
123 save(list=objects2save[objects2save %in% ls()], file="xcmsSet.RData")
124
125
126 cat("\tDONE\n")