comparison spectra_plots.xml @ 0:de334e98f1c1 draft

planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cardinal commit 0825a4ccd3ebf4ca8a298326d14f3e7b25ae8415
author galaxyp
date Mon, 01 Oct 2018 01:01:41 -0400
parents
children 45fb4dd8400b
comparison
equal deleted inserted replaced
-1:000000000000 0:de334e98f1c1
1 <tool id="cardinal_spectra_plots" name="MSI plot spectra" version="@VERSION@.0">
2 <description>
3 mass spectrometry imaging mass spectra plots
4 </description>
5 <macros>
6 <import>macros.xml</import>
7 </macros>
8 <expand macro="requirements">
9 <requirement type="package" version="2.2.1">r-gridextra</requirement>
10 <requirement type="package" version="2.2.1">r-ggplot2</requirement>
11 <requirement type="package" version="0.5.0">r-scales</requirement>
12 </expand>
13 <command detect_errors="exit_code">
14 <![CDATA[
15 @INPUT_LINKING@
16 cat '${MSI_mzplots}' &&
17 Rscript '${MSI_mzplots}'
18 ]]>
19 </command>
20 <configfiles>
21 <configfile name="MSI_mzplots"><![CDATA[
22
23 ################################# load libraries and read file #################
24
25 library(Cardinal)
26 library(gridExtra)
27 library(ggplot2)
28 library(scales)
29
30 @READING_MSIDATA@
31
32 @DATA_PROPERTIES@
33
34 ######################################## PDF ###################################
35 ################################################################################
36 ################################################################################
37
38
39 pdf("mzplots.pdf", fonts = "Times", pointsize = 12)
40 plot(0,type='n',axes=FALSE,ann=FALSE)
41 #if not $filename:
42 #set $filename = $infile.display_name
43 #end if
44 title(main=paste0("Mass spectra for file: \n\n","$filename"))
45
46
47 ############################# I) numbers ######################################
48 ###############################################################################
49
50 grid.table(property_df, rows= NULL)
51
52 ## set NA to 0 otherwise plot function will not work
53
54 spectra(msidata)[is.na(spectra(msidata)[])] = 0 ## in case of NA values they will be set to zero
55
56 if (npeaks > 0){
57
58 pixeldf = data.frame(matrix(ncol = 2, nrow=0))
59
60 ############################# single pixel ################################
61 ###########################################################################
62
63 #if str( $pixel_conditional.pixel_type) == 'single_pixel':
64 print("single_pixel")
65
66 #for $chosenpixel in $pixel_conditional.repeatpixel:
67
68 pixelname = paste0("x = ", $chosenpixel.inputx,", ", "y = ", $chosenpixel.inputy)
69 pixelisvalid = as.character(pixelname %in% names(pixels(msidata)))
70 pixeldf = rbind(pixeldf, cbind(pixelname, pixelisvalid))
71
72 ############################# II) control image ####################
73
74 if (pixelisvalid == "TRUE"){
75 print(pixelisvalid)
76
77 image(msidata, mz=$chosenpixel.inputmz, ylim = c(maximumy+(0.2*maximumy),minimumy-1),
78 colorkey=FALSE, plusminus = $chosenpixel.plusminusinDalton,
79 main= paste0("x= ",$chosenpixel.inputx, ", y= ", $chosenpixel.inputy))
80
81 abline(v=$chosenpixel.inputx, col ="$chosenpixel.inputcolour", lty="$chosenpixel.inputtype", lwd=$chosenpixel.inputwidth)
82 abline(h=$chosenpixel.inputy, col ="$chosenpixel.inputcolour", lty="$chosenpixel.inputtype", lwd=$chosenpixel.inputwidth)
83
84 ##################### III) plot full mass spectrum #################
85
86 plot(msidata, coord=list(x=$chosenpixel.inputx, y=$chosenpixel.inputy))
87
88 ##################### IV) plot zoom-in mass spectrum ###############
89
90 #if $chosenpixel.zoomedplot:
91 iData(msidata) <- iData(msidata)[] ## getting back data on disk
92
93 #for $token in $chosenpixel.zoomedplot:
94
95 minmasspixel = features(msidata, mz=$token.xlimmin)
96 maxmasspixel = features(msidata, mz=$token.xlimmax)
97
98 plot(msidata[minmasspixel:maxmasspixel,], coord=list(x=$chosenpixel.inputx, y=$chosenpixel.inputy),
99 xlim= c($token.xlimmin,$token.xlimmax))
100
101 #end for
102 #end if
103 }else{
104 print("The pixel coordinates did not correspond to a real pixel")}
105 #end for
106
107 colnames(pixeldf) = c("pixel coordinates", "coordinates were found in this file")
108
109 ############################# sample pixel ################################
110 ###########################################################################
111
112 #elif str( $pixel_conditional.pixel_type) == 'sample_pixel':
113 print("sample pixels")
114
115 ## optional annotation from tabular file to obtain pixel groups (otherwise all pixels are considered to be one sample)
116
117 #if str($pixel_conditional.tabular_annotation.load_annotation) == 'yes_annotation':
118
119 ## read and extract x,y,annotation information
120 input_tabular = read.delim("$pixel_conditional.tabular_annotation.annotation_file", header = $pixel_conditional.tabular_annotation.tabular_header, stringsAsFactors = FALSE)
121 annotation_input = input_tabular[,c($pixel_conditional.tabular_annotation.column_x, $pixel_conditional.tabular_annotation.column_y, $pixel_conditional.tabular_annotation.column_names)]
122 colnames(annotation_input) = c("x", "y", "annotation")
123
124 ## merge with coordinate information of msidata
125 msidata_coordinates = cbind(coord(msidata)[,1:2], c(1:ncol(msidata)))
126 colnames(msidata_coordinates)[3] = "pixel_index"
127 merged_annotation = merge(msidata_coordinates, annotation_input, by=c("x", "y"), all.x=TRUE)
128 merged_annotation[is.na(merged_annotation)] = "NA"
129 merged_annotation = merged_annotation[order(merged_annotation\$pixel_index),]
130 msidata\$annotation = as.factor(merged_annotation[,4])
131
132 #end if
133
134 ##################### I) Sample: plot full mass spectrum ##############
135
136 ## coloured plot with mean over all spectra with the same annotation, if no annotation is provided all pixels are considered as one sample
137
138 if (!is.null(levels(msidata\$annotation))){
139 print("annotated samples")
140
141 ## overview plot over annotated samples
142 number_combined = length(levels(msidata\$annotation))
143
144 ## the more annotation groups a file has the smaller will be the legend
145 if (number_combined<20){
146 legend_size = 10
147 }else if (number_combined>20 && number_combined<40){
148 legend_size = 9
149 }else if (number_combined>40 && number_combined<60){
150 legend_size = 8
151 }else if (number_combined>60 && number_combined<100){
152 legend_size = 7
153 }else{
154 legend_size = 6
155 }
156
157 position_df = cbind(coord(msidata)[,1:2], as.factor(msidata\$annotation))
158 colnames(position_df)[3] = "sample_name"
159
160 combine_plot = ggplot(position_df, aes(x=x, y=y, fill=sample_name))+
161 geom_tile(height = 1, width=1)+
162 coord_fixed()+
163 ggtitle("Spatial orientation of pixel annotations")+
164 theme_bw()+
165 theme(plot.title = element_text(hjust = 0.5))+
166 theme(text=element_text(family="ArialMT", face="bold", size=12))+
167 theme(legend.position="bottom",legend.direction="vertical")+
168 theme(legend.key.size = unit(0.2, "line"), legend.text = element_text(size = 6))+
169 guides(fill=guide_legend(ncol=4,byrow=TRUE))
170
171 coord_labels = aggregate(cbind(x,y)~sample_name, data=position_df, mean, na.rm=TRUE, na.action="na.pass")
172 coord_labels\$file_number = 1:length(levels(position_df\$sample_name))
173
174 for(file_count in 1:nrow(coord_labels))
175 {combine_plot = combine_plot + annotate("text",x=coord_labels[file_count,"x"],
176 y=coord_labels[file_count,"y"],label=toString(coord_labels[file_count,4]))}
177
178 print(combine_plot)
179
180 ## print legend only for less than 10 samples
181 if (length(levels(msidata\$annotation)) < 10){
182 key_legend = TRUE
183 }else{key_legend = FALSE}
184
185 spectra(msidata)[is.na(spectra(msidata)[])] = 0 ## in case of NA values they will be set to zero
186 plot(msidata, pixel=1:ncol(msidata), pixel.groups=msidata\$annotation, key=key_legend, col=hue_pal()(length(levels(msidata\$annotation))),superpose=TRUE)
187 }else{
188 spectra(msidata)[is.na(spectra(msidata)[])] = 0 ## in case of NA values they will be set to zero
189 plot(msidata, pixel=1:ncol(msidata), key=TRUE)}
190
191 ##################### II) Sample: plot zoom-in mass spectrum ##########
192
193 #if $pixel_conditional.zoomed_sample:
194 iData(msidata) <- iData(msidata)[] ## getting back data on disk
195 #for $token in $pixel_conditional.zoomed_sample:
196 print("zoomed sample pixels")
197
198 minmasspixel = features(msidata, mz=$token.xlimmin)
199 maxmasspixel = features(msidata, mz=$token.xlimmax)
200
201 ## coloured plot with mean over all spectra for annotation group, otherwise only 1 black plot
202 if (!is.null(levels(msidata\$annotation))){
203 print("annotation samples")
204 plot(msidata[minmasspixel:maxmasspixel,], pixel=1:ncol(msidata),
205 xlim= c($token.xlimmin,$token.xlimmax),pixel.groups=msidata\$annotation,
206 key=key_legend,col=hue_pal()(length(levels(msidata\$annotation))), superpose=TRUE)
207 }else{
208 plot(msidata[minmasspixel:maxmasspixel,], pixel=1:ncol(msidata), key=TRUE, xlim= c($token.xlimmin,$token.xlimmax))}
209
210 #end for
211 #end if
212
213 if (!is.null(levels(msidata\$annotation))){
214 pixeldf = data.frame(table(msidata\$annotation))
215 }else{
216 pixeldf = data.frame("$filename", ncol(msidata))}
217 colnames(pixeldf) = c("sample name", "number of pixels")
218
219 #end if
220
221
222 ############################# pixel table ######################################
223 ###############################################################################
224
225
226 ### overview table of pixels or samples:
227 plot(0,type='n',axes=FALSE,ann=FALSE)
228 title(main="Overview of chosen pixel:")
229
230 ### for more than 20 annotation groups print only 20 samples per page:
231 if (is.null(levels(msidata\$annotation))){
232 grid.table(pixeldf, rows= NULL)
233 }else if (length(levels(msidata\$annotation)) <= 20){
234 grid.table(pixeldf, rows= NULL)
235 }else{
236 grid.table(pixeldf[1:20,], rows= NULL)
237 mincount = 21
238 maxcount = 40
239 for (count20 in 1:(ceiling(nrow(pixeldf)/20)-1)){
240 plot(0,type='n',axes=FALSE,ann=FALSE)
241 if (maxcount <= nrow(pixeldf)){
242 grid.table(pixeldf[mincount:maxcount,], rows= NULL)
243 mincount = mincount+20
244 maxcount = maxcount+20
245 }else{### stop last page with last sample otherwise NA in table
246 grid.table(pixeldf[mincount:nrow(pixeldf),], rows= NULL)}
247 }
248 }
249
250 dev.off()
251
252 }else{
253 print("Inputfile has no intensities > 0")
254 dev.off()
255 }
256 ]]></configfile>
257 </configfiles>
258 <inputs>
259 <expand macro="reading_msidata"/>
260 <expand macro="pdf_filename"/>
261 <conditional name="pixel_conditional">
262 <param name="pixel_type" type="select" label="Select if you want to plot the mass spectrum of a single pixel or the average spectrum of all pixels of a sample">
263 <option value="single_pixel" selected="True">Single pixel</option>
264 <option value="sample_pixel">Average spectrum for each sample</option>
265 </param>
266 <when value="single_pixel">
267 <repeat name="repeatpixel" title="Plot mass spectra for pixel of interest" min="1" max="20">
268 <param name="inputx" type="integer" value="" label="x-coordinate of pixel of interest" help="x-value of the pixel of interest"/>
269 <param name="inputy" type="integer" value="" label="y-coordinate of pixel of interest" help="y-value of the pixel of interest"/>
270 <param name="inputmz" type="float" value="1296.7" label="Next parameters are to control heatmap image which will be plotted, define m/z here" help="m/z will be displayed as heatmap and the pixel of interest will be visualized by the intersection of two lines"/>
271 <param name="plusminusinDalton" value="0.25" type="float" label="m/z range for this m/z value" help="plusminus m/z window "/>
272 <param name="inputcolour" type="select" label="Select the colour for the lines at x and y position">
273 <option value="white" selected="True">white</option>
274 <option value="black">black</option>
275 <option value="grey">grey</option>
276 <option value="blue">blue</option>
277 <option value="red">red</option>
278 <option value="green">green</option>
279 </param>
280 <param name="inputtype" type="select" label="Select the line type for the lines at x and y position">
281 <option value="solid" selected="True">solid</option>
282 <option value="dashed">dashed</option>
283 <option value="dotted">dotted</option>
284 <option value="longdash">longdash</option>
285 </param>
286 <param name="inputwidth" type="integer" value="2" label="Select the width of the lines at x and y position"/>
287 <repeat name="zoomedplot" title="Zoomed in plots with m/z min and m/z max to define the plot window" min="0" max="50">
288 <param name="xlimmin" type="integer" value="" label="lower m/z boundary for plotting window" help="minimum m/z for zoomed in window"/>
289 <param name="xlimmax" type="integer" value="" label="upper m/z boundary for plotting window" help="maximum m/z for zoomed in window"/>
290 </repeat>
291 </repeat>
292 </when>
293 <when value="sample_pixel">
294 <conditional name="tabular_annotation">
295 <param name="load_annotation" type="select" label="Use pixel annotation from tabular file for spectra plots">
296 <option value="no_annotation" selected="True">pixels belong into one group only</option>
297 <option value="yes_annotation">group pixels according to annotations</option>
298 </param>
299 <when value="yes_annotation">
300 <expand macro="reading_pixel_annotations"/>
301 </when>
302 <when value="no_annotation"/>
303 </conditional>
304 <repeat name="zoomed_sample" title="Zoomed in plots with m/z min and m/z max to define the plot window" min="0" max="50">
305 <param name="xlimmin" type="integer" value="" label="lower m/z boundary for plotting window" help="minimum m/z for zoomed in window"/>
306 <param name="xlimmax" type="integer" value="" label="upper m/z boundary for plotting window" help="maximum m/z for zoomed in window"/>
307 </repeat>
308 </when>
309 </conditional>
310 </inputs>
311 <outputs>
312 <data format="pdf" name="plots" from_work_dir="mzplots.pdf" label="${tool.name} on ${on_string}"/>
313 </outputs>
314 <tests>
315 <test>
316 <expand macro="infile_imzml"/>
317 <conditional name="pixel_conditional">
318 <param name="pixel_type" value="single_pixel"/>
319 <repeat name="repeatpixel">
320 <param name="plusminusinDalton" value="0.25"/>
321 <param name="inputx" value="3"/>
322 <param name="inputy" value="3"/>
323 <repeat name="zoomedplot">
324 <param name="xlimmin" value="310"/>
325 <param name="xlimmax" value="320"/>
326 </repeat>
327 <repeat name="zoomedplot">
328 <param name="xlimmin" value="350"/>
329 <param name="xlimmax" value="400"/>
330 </repeat>
331 <repeat name="zoomedplot">
332 <param name="xlimmin" value="400"/>
333 <param name="xlimmax" value="420"/>
334 </repeat>
335 </repeat>
336 <repeat name="repeatpixel">
337 <param name="plusminusinDalton" value="0.25"/>
338 <param name="inputx" value="2"/>
339 <param name="inputy" value="2"/>
340 </repeat>
341 <repeat name="repeatpixel">
342 <param name="plusminusinDalton" value="0.25"/>
343 <param name="inputx" value="1"/>
344 <param name="inputy" value="1"/>
345 </repeat>
346 </conditional>
347 <output name="plots" file="Plot_imzml.pdf" compare="sim_size"/>
348 </test>
349 <test>
350 <expand macro="infile_analyze75"/>
351 <conditional name="pixel_conditional">
352 <param name="pixel_type" value="single_pixel"/>
353 <repeat name="repeatpixel">
354 <param name="plusminusinDalton" value="0.25"/>
355 <param name="inputx" value="5"/>
356 <param name="inputy" value="2"/>
357 <repeat name="zoomedplot">
358 <param name="xlimmin" value="840"/>
359 <param name="xlimmax" value="850"/>
360 </repeat>
361 </repeat>
362 <repeat name="repeatpixel">
363 <param name="plusminusinDalton" value="0.25"/>
364 <param name="inputx" value="2"/>
365 <param name="inputy" value="2"/>
366 </repeat>
367 </conditional>
368 <output name="plots" file="Plot_analyze75.pdf" compare="sim_size"/>
369 </test>
370 <test>
371 <expand macro="infile_analyze75"/>
372 <conditional name="pixel_conditional">
373 <param name="pixel_type" value="sample_pixel"/>
374 <conditional name="tabular_annotation">
375
376 <param name="load_annotation" value="yes_annotation"/>
377 <param name="annotation_file" value="annotations.tabular" ftype="tabular"/>
378 <param name="column_x" value="1"/>
379 <param name="column_y" value="2"/>
380 <param name="column_names" value="4"/>
381 <param name="tabular_header" value="TRUE"/>
382
383
384
385 </conditional>
386 <repeat name="zoomed_sample">
387 <param name="xlimmin" value="1250"/>
388 <param name="xlimmax" value="1270"/>
389 </repeat>
390 </conditional>
391 <output name="plots" file="Plot_analyze75_allpixels.pdf" compare="sim_size"/>
392 </test>
393 <test>
394 <param name="infile" value="3_files_combined.RData" ftype="rdata"/>
395 <conditional name="pixel_conditional">
396 <param name="pixel_type" value="sample_pixel"/>
397 <repeat name="zoomed_sample">
398 <param name="xlimmin" value="350"/>
399 <param name="xlimmax" value="360"/>
400 </repeat>
401 </conditional>
402 <output name="plots" file="Plot_rdata.pdf" compare="sim_size"/>
403 </test>
404 <test>
405 <param name="infile" value="empty_spectra.rdata" ftype="rdata"/>
406 <conditional name="pixel_conditional">
407 <param name="pixel_type" value="single_pixel"/>
408 <repeat name="repeatpixel">
409 <param name="plusminusinDalton" value="0.1"/>
410 <param name="inputx" value="1"/>
411 <param name="inputy" value="1"/>
412 </repeat>
413 </conditional>
414 <output name="plots" file="Plot_empty_spectra.pdf" compare="sim_size"/>
415 </test>
416 </tests>
417 <help><![CDATA[
418
419 @CARDINAL_DESCRIPTION@
420
421 -----
422
423 This tool uses the Cardinal plot function to generate (zoomed in) mass spectra plots of mass spectrometry imaging data.
424
425 @MSIDATA_INPUT_DESCRIPTION@
426 @SPECTRA_TABULAR_INPUT_DESCRIPTION@
427
428 **Options**
429
430 - "single pixel": Returns a full mass spectrum plot for one pixel, which is defined by its x- and y-coordinates
431
432 - Enter the x and y coordinates of your pixel of interest
433 - To have a visual control for the selected pixel, a heatmap of a m/z of interest will be drawn. Two intersecting lines will show the pixel location. This procedure requires an m/z of interest together with a m/z range and for the lines the colour and type.
434 - Additionally zoom into mass spectra plots is possible by providing the minimum and maximum m/z value to define the limits of the plot
435
436 - "All pixels of a sample": Returns a full average mass spectrum plot with different colours for each pixel annotation group, without annotations the average of all pixels is plotted
437
438 - Additionally zoom into mass spectra plots is possible by providing the minimum and maximum m/z value to define the limits of the plot
439
440
441 **Tip**
442
443 - Corresponding m/z intensity pairs for single spectra can be obtained with the filtering tool option "ranges for x and y" and afterwards using the imzml tabular exporter tool
444 - Plots will only be drawn for pixels that exist in the MSI data, pixel that are outside the dataset will be "False" in the overview table on the last page of the pdf
445
446
447 **Output**
448
449 - Pdf with the selected mass spectra plots and additional x-y grid plots
450
451
452 ]]>
453 </help>
454 <expand macro="citations"/>
455 </tool>