annotate metaMS_cmd_pick_and_group.r @ 5:41f122255d14

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