annotate metaMS/xcms_get_mass_eic.r @ 6:4393f982d18f

reorganized sources
author pieter.lukasse@wur.nl
date Thu, 19 Mar 2015 12:22:23 +0100
parents
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 # xset data:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
4 args.xsetData <- args[1]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
5
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
6 args.rtStart <- strtoi(args[2])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
7 args.rtEnd <- strtoi(args[3])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
8
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
9 args.mzStart <- as.double(args[4])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
10 args.mzEnd <- as.double(args[5])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
11 # there are 2 options: specify a mz range or a mz list:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
12 if (args.mzStart < 0)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
13 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
14 args.mzList <- as.double(strsplit(args[6], ",")[[1]])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
15 cat(typeof(as.double(strsplit(args[6], ",")[[1]])))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
16 args.mzTolPpm <- as.double(args[7])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
17 # calculate mzends based on ppm tol:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
18 mzListEnd <- c()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
19 mzListStart <- c()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
20 for (i in 1:length(args.mzList))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
21 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
22 mzEnd <- args.mzList[i] + args.mzList[i]*args.mzTolPpm/1000000.0
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
23 mzStart <- args.mzList[i] - args.mzList[i]*args.mzTolPpm/1000000.0
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
24 mzListEnd <- c(mzListEnd, mzEnd)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
25 mzListStart <- c(mzListStart, mzStart)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
26 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
27 str(mzListStart)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
28 str(mzListEnd)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
29 } else {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
30 mzListEnd <- c(args.mzEnd)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
31 mzListStart <- c(args.mzStart)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
32 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
33
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
34 args.sampleNames <- strsplit(args[8], ",")[[1]]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
35 # trim leading and trailing spaces:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
36 args.sampleNames <- gsub("^\\s+|\\s+$", "", args.sampleNames)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
37
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
38 args.combineSamples <- args[9]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
39 args.rtPlotMode <- args[10]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
40
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
41 ## report files
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
42 args.htmlReportFile <- args[11]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
43 args.htmlReportFile.files_path <- args[12]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
44
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
45
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
46 if (length(args) == 13)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
47 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
48 args.outLogFile <- args[13]
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
49 # suppress messages:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
50 # Send all STDERR to STDOUT using sink() see http://mazamascience.com/WorkingWithData/?p=888
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
51 msg <- file(args.outLogFile, open="wt")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
52 sink(msg, type="message")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
53 sink(msg, type="output")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
54 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
55
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
56 # TODO - add option to do masses in same plot (if given in same line oid) or in separate plots
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
57 # TODO2 - let it run in parallel
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
58
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
59 tryCatch(
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
60 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
61 library(metaMS)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
62
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
63 # load the xset data :
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
64 xsetData <- readRDS(args.xsetData)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
65 # if here to support both scenarios:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
66 if ("xcmsSet" %in% slotNames(xsetData) )
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
67 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
68 xsetData <- xsetData@xcmsSet
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
69 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
70
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
71 # report
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
72 dir.create(file.path(args.htmlReportFile.files_path), showWarnings = FALSE, recursive = TRUE)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
73 message(paste("\nGenerating report.........in ", args.htmlReportFile.files_path))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
74
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
75 html <- "<html><body><h1>Extracted Ion Chromatograms (EIC) matching criteria</h1>"
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
76
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
77 if (args.combineSamples == "No")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
78 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
79 if (length(args.sampleNames) > 1 && length(mzListStart) > 1 && length(args.sampleNames) != length(mzListStart))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
80 stop(paste("The number of sample names should match the number of m/z values in the list. Found ", length(mzListStart),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
81 " masses while ", length(args.sampleNames), " sample names were given."))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
82
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
83 iterSize <- length(args.sampleNames)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
84 # these can be set to 1 or 0 just as a trick to iterate OR not over the items. If the respective list is of length 1, only the first item should be used
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
85 fixSampleIdx <- 1
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
86 fixMzListIdx <- 1
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
87 if (length(args.sampleNames) == 1)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
88 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
89 fixSampleIdx <- 0
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
90 iterSize <- length(mzListStart)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
91 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
92 if (length(mzListStart) == 1)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
93 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
94 fixMzListIdx <- 0
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
95 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
96 lineColors <- rainbow(iterSize)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
97 for (i in 0:(iterSize-1))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
98 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
99 message("\nGetting EIC... ")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
100 eiccor <- getEIC(xsetData,
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
101 mzrange=matrix(c(mzListStart[i*fixMzListIdx+1],mzListEnd[i*fixMzListIdx+1]),nrow=1,ncol=2,byrow=TRUE),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
102 rtrange=matrix(c(args.rtStart,args.rtEnd),nrow=1,ncol=2,byrow=TRUE),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
103 sampleidx=c(args.sampleNames[i*fixSampleIdx+1]), rt=args.rtPlotMode)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
104
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
105 message("\nPlotting figures... ")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
106 figureName <- paste(args.htmlReportFile.files_path, "/figure", i,".png", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
107 html <- paste(html,"<img src='", "figure", i,".png' /><br/>", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
108 png( figureName, type="cairo", width=1100,height=250 )
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
109 #plot(eiccor, col=lineColors[i+1])
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
110 # black is better in this case:
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
111 plot(eiccor)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
112 legend('topright', # places a legend at the appropriate place
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
113 legend=c(args.sampleNames[i*fixSampleIdx+1]), # puts text in the legend
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
114 lty=c(1,1), # gives the legend appropriate symbols (lines)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
115 lwd=c(2.5,2.5))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
116
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
117 devname = dev.off()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
118 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
119
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
120 } else {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
121 for (i in 1:length(mzListStart))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
122 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
123 message("\nGetting EIC... ")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
124 eiccor <- getEIC(xsetData,
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
125 mzrange=matrix(c(mzListStart[i],mzListEnd[i]),nrow=1,ncol=2,byrow=TRUE),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
126 rtrange=matrix(c(args.rtStart,args.rtEnd),nrow=1,ncol=2,byrow=TRUE),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
127 sampleidx=args.sampleNames, rt = args.rtPlotMode)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
128
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
129 #set size, set option (plot per sample, plot per mass)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
130
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
131 message("\nPlotting figures... ")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
132 figureName <- paste(args.htmlReportFile.files_path, "/figure", i,".png", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
133 html <- paste(html,"<img src='", "figure", i,".png' />", sep="")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
134 png( figureName, type="cairo", width=1100,height=450 )
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
135 lineColors <- rainbow(length(args.sampleNames))
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
136 plot(eiccor, col=lineColors)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
137 legend('topright', # places a legend at the appropriate place
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
138 legend=args.sampleNames, # puts text in the legend
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
139 lty=c(1,1), # gives the legend appropriate symbols (lines)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
140 lwd=c(2.5,2.5),
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
141 col=lineColors)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
142 devname = dev.off()
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
143 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
144 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
145 if (args.rtPlotMode == "corrected")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
146 {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
147 html <- paste(html,"<p>*rt values are corrected ones</p></body><html>")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
148 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
149 html <- paste(html,"</body><html>")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
150 message("finished generating report")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
151 write(html,file=args.htmlReportFile)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
152 # unlink(args.htmlReportFile)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
153 cat("\nWarnings================:\n")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
154 str( warnings() )
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
155 },
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
156 error=function(cond) {
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
157 sink(NULL, type="message") # default setting
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
158 sink(stderr(), type="output")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
159 message("\nERROR: ===========\n")
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
160 print(cond)
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
161 }
4393f982d18f reorganized sources
pieter.lukasse@wur.nl
parents:
diff changeset
162 )