annotate metaMS_cmd_interface.r @ 48:26b93438f30e

small fix in mass list of CSV files
author pieter.lukasse@wur.nl
date Wed, 19 Nov 2014 15:11:45 +0100
parents 664ccd5f7cf8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
1 ## read args:
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
2 args <- commandArgs(TRUE)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
3 ## the constructed DB, e.g. "E:/Rworkspace/metaMS/data/LCDBtest.RData"
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
4 args.constructedDB <- args[1]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
5 ## data files, e.g. "E:/Rworkspace/metaMS/data/data.zip" (with e.g. .CDF files) and unzip output dir, e.g. "E:/"
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
6 args.dataZip <- args[2]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
7 args.zipExtrDir <- paste(args[2],"dir/")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
8 ## settings file, e.g. "E:/Rworkspace/metaMS/data/settings.r", should contain assignment to an object named "customMetaMSsettings"
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
9 args.settings <- args[3]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
10
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
11 ## output file names, e.g. "E:/Rworkspace/metaMS/data/out.txt"
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
12 args.outAnnotationTable <- args[4]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
13 args.outLogFile <- args[5]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
14 args.xsetOut <- args[6]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
15
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
16 ## report files
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
17 args.htmlReportFile <- args[7]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
18 args.htmlReportFile.files_path <- args[8]
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
19
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
20 # Send all STDERR to STDOUT using sink() see http://mazamascience.com/WorkingWithData/?p=888
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
21 msg <- file(args.outLogFile, open="wt")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
22 sink(msg, type="message")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
23 sink(msg, type="output")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
24
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
25 cat("\nSettings used===============:\n")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
26 cat(readChar(args.settings, 1e5))
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
27
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
28
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
29 tryCatch(
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
30 {
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
31 library(metaMS)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
32
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
33 ## load the constructed DB :
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
34 tempEnv <- new.env()
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
35 testDB <- load(args.constructedDB, envir=tempEnv)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
36
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
37 ## load the data files from a zip file
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
38 files <- unzip(args.dataZip, exdir=args.zipExtrDir)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
39
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
40 ## load settings "script" into "customMetaMSsettings"
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
41 source(args.settings, local=tempEnv)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
42 message(paste(" loaded : ", args.settings))
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
43
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
44 # Just to highlight: if you want to use more than one
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
45 # trigger runLC:
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
46 LC <- runLC(files, settings = tempEnv[["customMetaMSsettings"]], DB = tempEnv[[testDB[1]]]$DB, nSlaves=20, returnXset = TRUE)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
47
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
48 # write out runLC annotation results:
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
49 write.table(LC$Annotation$annotation.table, args.outAnnotationTable, sep="\t", row.names=FALSE)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
50
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
51 # the used constructed DB (write to log):
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
52 cat("\nConstructed DB info===============:\n")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
53 str(tempEnv[[testDB[1]]]$Info)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
54 cat("\nConstructed DB table===============:\n")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
55 write.table(tempEnv[[testDB[1]]]$DB, args.outLogFile, append=TRUE, row.names=FALSE)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
56 write.table(tempEnv[[testDB[1]]]$Reftable, args.outLogFile, sep="\t", append=TRUE, row.names=FALSE)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
57 # save xset as rdata:
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
58 xsetData <- LC$xset@xcmsSet
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
59 saveRDS(xsetData, file=args.xsetOut)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
60
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
61 message("\nGenerating report.........")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
62 # report
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
63 dir.create(file.path(args.htmlReportFile.files_path), showWarnings = FALSE)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
64 setwd(file.path(args.htmlReportFile.files_path))
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
65 html <- "<html><body><h1>Extracted Ion Chromatograms of groups with more than 3 peaks</h1>"
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
66
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
67 LC$xset@xcmsSet
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
68 gt <- groups(LC$xset@xcmsSet)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
69 colnames(gt)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
70 groupidx1 <- which(gt[,"rtmed"] > 0 & gt[,"rtmed"] < 3000 & gt[,"npeaks"] > 3)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
71 if (length(groupidx1) > 0)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
72 {
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
73 eiccor <- getEIC(LC$xset@xcmsSet, groupidx = c(groupidx1))
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
74 eicraw <- getEIC(LC$xset@xcmsSet, groupidx = c(groupidx1), rt = "raw")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
75 for (i in 1:length(groupidx1))
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
76 {
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
77 figureName <- paste(args.htmlReportFile.files_path, "/figure", i,".png", sep="")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
78 html <- paste(html,"<img src='", "figure", i,".png' />", sep="")
42
664ccd5f7cf8 small fix for png method
pieter.lukasse@wur.nl
parents: 41
diff changeset
79 png( figureName, type="cairo" )
41
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
80 plot(eiccor, LC$xset@xcmsSet, groupidx = i)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
81 devname = dev.off()
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
82 }
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
83 }
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
84
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
85
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
86 html <- paste(html,"</body><html>")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
87 message("finished generating report")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
88 write(html,file=args.htmlReportFile)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
89 # unlink(args.htmlReportFile)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
90 cat("\nWarnings================:\n")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
91 str( warnings() )
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
92 },
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
93 error=function(cond) {
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
94 sink(NULL, type="message") # default setting
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
95 sink(stderr(), type="output")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
96 message("\nERROR: ===========\n")
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
97 print(cond)
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
98 }
e67149fbff20 small changes/improvements;
pieter.lukasse@wur.nl
parents:
diff changeset
99 )