annotate xcms_get_alignment_eic.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 # shows all alignment results in a rt region
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
2 ## read args:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
3 args <- commandArgs(TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
4 # xset data:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
5 args.xsetData <- args[1]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
6
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
7 args.rtStart <- strtoi(args[2])
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
8 args.rtEnd <- strtoi(args[3])
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
9
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
10 # limit max diff to 600 and minNrSamples to at least 2 :
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
11 if (args.rtEnd - args.rtStart > 600)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
12 stop("The RT window should be <= 600")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
13
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
14 args.minNrSamples <- strtoi(args[4]) #min nr samples
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
15 if (args.minNrSamples < 2)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
16 stop("Set 'Minimum number of samples' to 2 or higher")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
17
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
18
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
19 args.sampleNames <- strsplit(args[5], ",")[[1]]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
20 # trim leading and trailing spaces:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
21 args.sampleNames <- gsub("^\\s+|\\s+$", "", args.sampleNames)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
22
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
23 ## report files
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
24 args.htmlReportFile <- args[6]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
25 args.htmlReportFile.files_path <- args[7]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
26
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
27
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
28 if (length(args) == 8)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
29 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
30 args.outLogFile <- args[8]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
31 # suppress messages:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
32 # Send all STDERR to STDOUT using sink() see http://mazamascience.com/WorkingWithData/?p=888
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
33 msg <- file(args.outLogFile, open="wt")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
34 sink(msg, type="message")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
35 sink(msg, type="output")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
36 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
37
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
38
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
39
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
40 tryCatch(
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
41 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
42 library(metaMS)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
43
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
44 # load the xset data :
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
45 xsetData <- readRDS(args.xsetData)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
46 # if here to support both scenarios:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
47 if ("xcmsSet" %in% slotNames(xsetData) )
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
48 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
49 xsetData <- xsetData@xcmsSet
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
50 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
51
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
52 # report
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
53 dir.create(file.path(args.htmlReportFile.files_path), showWarnings = FALSE, recursive = TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
54 message(paste("\nGenerating report.........in ", args.htmlReportFile.files_path))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
55
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
56 write(paste("<html><body><h1>Extracted Ion Chromatograms (EIC) of alignments with peaks in ",args.minNrSamples, " or more samples</h1>"),
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
57 file=args.htmlReportFile)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
58
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
59 gt <- groups(xsetData)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
60 message("\nParsed groups... ")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
61 groupidx1 <- which(gt[,"rtmed"] > args.rtStart & gt[,"rtmed"] < args.rtEnd & gt[,"npeaks"] >= args.minNrSamples) # this should be only on samples selected
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
62
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
63 if (length(groupidx1) > 0)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
64 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
65 message("\nGetting EIC... ")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
66 eiccor <- getEIC(xsetData, groupidx = c(groupidx1), sampleidx=args.sampleNames)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
67 #eicraw <- getEIC(xsetData, groupidx = c(groupidx1), sampleidx=args.sampleNames, rt = "raw")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
68
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
69 #sampleNamesIdx <- which(sampnames(LC$xset@xcmsSet) %in% args.sampleNames, arr.ind = TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
70 #or (from bioconductor code for getEIC): NB: this is assuming sample indexes used in data are ordered after the order of sample names!!
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
71 sampNames <- sampnames(xsetData)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
72 sampleNamesIdx <- match( args.sampleNames, sampNames)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
73 message(paste("Samples: ", sampleNamesIdx))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
74
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
75 #TODO html <- paste(html, "<table><tbody>")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
76 message(paste("\nPlotting figures... "))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
77
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
78 #get the mz list (interestingly, this [,"mz"] is relatively slow):
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
79 peakMzList <- xsetData@peaks[,"mz"]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
80 peakSampleList <- xsetData@peaks[,"sample"]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
81 #signal to noise list:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
82 peakSnList <- xsetData@peaks[,"sn"]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
83
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
84 message(paste("Total nr of peaks: ", length(peakMzList)))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
85
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
86 for (i in 1:length(groupidx1))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
87 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
88 groupMembers <- xsetData@groupidx[[groupidx1[i]]]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
89
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
90 groupMzList <- ""
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
91 groupSampleList <- ""
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
92 finalGroupSize <- 0
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
93
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
94 for (j in 1:length(groupMembers))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
95 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
96 #get only the peaks from the selected samples:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
97 memberSample <- peakSampleList[groupMembers[j]]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
98 memberSn <- peakSnList[groupMembers[j]]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
99 if (!is.na(memberSn) && memberSample %in% sampleNamesIdx)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
100 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
101 message(paste("Group: ", groupidx1[i], " / Member sample: ", memberSample))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
102 memberMz <- peakMzList[groupMembers[j]]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
103
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
104
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
105 if (finalGroupSize == 0)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
106 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
107 groupMzList <- memberMz
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
108 groupSampleList <- sampNames[memberSample]
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
109 } else {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
110 groupMzList <- paste(groupMzList,",",memberMz, sep="")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
111 groupSampleList <- paste(groupSampleList,",",sampNames[memberSample], sep="")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
112 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
113
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
114 finalGroupSize <- finalGroupSize +1
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
115 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
116 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
117 # here we do the real check on group size and only the groups that have enough
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
118 # members with signal to noise > 0 will be plotted here:
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
119 if (finalGroupSize >= args.minNrSamples)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
120 {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
121 message(paste("Plotting figure ",i, " of max ", length(groupidx1)," figures... "))
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
122
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
123 figureName <- paste(args.htmlReportFile.files_path, "/figure", i,".png", sep="")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
124 write(paste("<img src='", "figure", i,".png' />", sep="") ,
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
125 file=args.htmlReportFile, append=TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
126
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
127 png( figureName, type="cairo", width=800 )
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
128 plot(eiccor, xsetData, groupidx = i)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
129 devname = dev.off()
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
130
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
131 write(paste("<p>Alignment id: [", groupidx1[i], "]. m/z list of peaks in alignment with signal/noise <> NA:<br/>", groupMzList,"</p>", sep="") ,
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
132 file=args.htmlReportFile, append=TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
133 write(paste("<p>m/z values found in the following samples respectively: <br/>", groupSampleList,"</p>", sep="") ,
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
134 file=args.htmlReportFile, append=TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
135 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
136 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
137
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
138 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
139
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
140 write("</body><html>",
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
141 file=args.htmlReportFile, append=TRUE)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
142 message("finished generating report")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
143 # unlink(args.htmlReportFile)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
144 cat("\nWarnings================:\n")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
145 str( warnings() )
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
146 },
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
147 error=function(cond) {
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
148 sink(NULL, type="message") # default setting
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
149 sink(stderr(), type="output")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
150 message("\nERROR: ===========\n")
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
151 print(cond)
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
152 }
dffc38727496 initial commit
pieter.lukasse@wur.nl
parents:
diff changeset
153 )