annotate metaMS/xcms_get_alignment_eic.r @ 25:9f03c8587d6b draft default tip

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