annotate metaMS/metaMS_cmd_pick_and_group.r @ 16:fe4682eb938c

small improvement
author pieter.lukasse@wur.nl
date Mon, 23 Mar 2015 08:40:42 +0100
parents 4393f982d18f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
1 ## read args:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
2 args <- commandArgs(TRUE)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
3 ## data files, e.g. "E:/Rworkspace/metaMS/data/data.zip" (with e.g. .CDF files) and unzip output dir, e.g. "E:/"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
4 args.dataZip <- args[1]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
5 args.zipExtrDir <- sub("\\.","_",paste(args[1],"dir", sep=""))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
6 dir.create(file.path(args.zipExtrDir), showWarnings = FALSE, recursive = TRUE)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
7 ## settings file, e.g. "E:/Rworkspace/metaMS/data/settings.r", should contain assignment to an object named "customMetaMSsettings"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
8 args.settings <- args[2]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
9
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
10 ## output file names, e.g. "E:/Rworkspace/metaMS/data/out.txt"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
11 args.outPeakTable <- args[3]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
12 args.xsetOut <- args[4]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
13
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
14 # polarity as explicit parameter:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
15 args.runLC_polarity <- args[5]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
16
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
17 ## report files
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
18 args.htmlReportFile <- args[6]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
19 args.htmlReportFile.files_path <- args[7]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
20
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
21
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
22 if (length(args) == 8)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
23 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
24 args.outLogFile <- args[8]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
25 # suppress messages:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
26 # Send all STDERR to STDOUT using sink() see http://mazamascience.com/WorkingWithData/?p=888
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
27 msg <- file(args.outLogFile, open="wt")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
28 sink(msg, type="message")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
29 sink(msg, type="output")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
30 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
31
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
32 cat("\nSettings used===============:\n")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
33 cat(readChar(args.settings, 1e5))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
34
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
35
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
36 tryCatch(
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
37 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
38 library(metaMS)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
39
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
40 ## load the data files from a zip file
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
41 files <- unzip(args.dataZip, exdir=args.zipExtrDir)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
42
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
43 ## load settings "script" into "customMetaMSsettings"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
44 tempEnv <- new.env()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
45 source(args.settings, local=tempEnv)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
46 message(paste(" loaded : ", args.settings))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
47 allSettings <- tempEnv[["customMetaMSsettings"]]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
48
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
49 # trigger runLC:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
50 LC <- runLC(files, settings = allSettings, polarity=args.runLC_polarity, nSlaves=20, returnXset = TRUE)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
51
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
52 # write out runLC annotation results:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
53 write.table(LC$PeakTable, args.outPeakTable, sep="\t", row.names=FALSE)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
54
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
55 # save xset as rdata:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
56 xsAnnotatePreparedData <- LC$xset
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
57 saveRDS(xsAnnotatePreparedData, file=args.xsetOut)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
58
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
59 message("\nGenerating report.........")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
60 # report
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
61 dir.create(file.path(args.htmlReportFile.files_path), showWarnings = FALSE, recursive = TRUE)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
62 html <- "<html><body><h1>Info on alignment quality </h1>"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
63 # TODO add (nr and mass error) and group size
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
64
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
65 message("\nPlotting figures... ")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
66 figureName <- paste(args.htmlReportFile.files_path, "/figure_retcor.png", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
67 html <- paste(html,"<img src='figure_retcor.png' /><br/>", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
68 png( figureName, type="cairo", width=1100,height=600 )
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
69 retcor(LC$xset@xcmsSet, method="peakgroups", plottype = "mdevden")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
70 html <- paste(html,"<a>*NB: retention time correction plot based on 'peakgroups' option with default settings. This is not the plot matching the exact settings used in the run,
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
71 but just intended to give a rough estimate of the retention time shifts present in the data. A more accurate plot will be available once
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
72 this option is added in metaMS API. </a><br/>", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
73 devname = dev.off()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
74
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
75
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
76 gt <- groups(LC$xset@xcmsSet)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
77 groupidx1 <- which(gt[,"rtmed"] > 0 & gt[,"rtmed"] < 3000 & gt[,"npeaks"] > 3)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
78
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
79 html <- paste(html,"</body><html>")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
80 message("finished generating report")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
81 write(html,file=args.htmlReportFile)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
82 # unlink(args.htmlReportFile)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
83 cat("\nWarnings================:\n")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
84 str( warnings() )
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
85 },
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
86 error=function(cond) {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
87 sink(NULL, type="message") # default setting
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
88 sink(stderr(), type="output")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
89 message("\nERROR: ===========\n")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
90 print(cond)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
91 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
92 )