comparison spectra_plots.xml @ 13:c5aa9fca18c6 draft

"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cardinal commit 39bd480e8813fa7a96b640150365577a69885d17-dirty"
author galaxyp
date Sun, 29 Nov 2020 23:41:23 +0000
parents 10566783d718
children 1693b2126f30
comparison
equal deleted inserted replaced
12:10566783d718 13:c5aa9fca18c6
1 <tool id="cardinal_spectra_plots" name="MSI plot spectra" version="@VERSION@.1"> 1 <tool id="cardinal_spectra_plots" name="MSI plot spectra" version="@VERSION@.2">
2 <description> 2 <description>
3 mass spectrometry imaging mass spectra plots 3 mass spectrometry imaging mass spectra plots
4 </description> 4 </description>
5 <macros> 5 <macros>
6 <import>macros.xml</import> 6 <import>macros.xml</import>
64 print("tabular_pixel") 64 print("tabular_pixel")
65 65
66 ## read and extract x,y, optional annotation information 66 ## read and extract x,y, optional annotation information
67 spectra_tabular = read.delim("$pixel_conditional.pixel_file", header = $pixel_conditional.tabular_pixel_header, stringsAsFactors = FALSE) 67 spectra_tabular = read.delim("$pixel_conditional.pixel_file", header = $pixel_conditional.tabular_pixel_header, stringsAsFactors = FALSE)
68 68
69 #if $pixel_conditional.column_pixel_annotation: 69 #if str($pixel_conditional.single_or_overlaid.plot_type) == 'overlaid_plots':
70 70
71 spectra_input = spectra_tabular[,c($pixel_conditional.column_pixel_x, $pixel_conditional.column_pixel_y, $pixel_conditional.column_pixel_annotation)] 71 spectra_input = spectra_tabular[,c($pixel_conditional.column_pixel_x, $pixel_conditional.column_pixel_y, $pixel_conditional.single_or_overlaid.column_pixel_annotation)]
72 colnames(spectra_input) = c("x", "y", "annotation") 72 colnames(spectra_input) = c("x", "y", "annotation")
73 73 spectra_input\$annotation = as.character(spectra_input\$annotation)
74 input_pixels = paste(spectra_input[,1], spectra_input[,2], sep="_") 74
75 dataset_pixels = paste(coord(msidata)\$x, coord(msidata)\$y, sep="_") 75 msidata_coordinates = data.frame(coord(msidata)\$x, coord(msidata)\$y)
76 pixelsofinterest = dataset_pixels %in% input_pixels 76 colnames(msidata_coordinates) = c("x", "y")
77 msidata = msidata[,pixelsofinterest] 77 merged_annotation = merge(msidata_coordinates, spectra_input, by=c("x", "y"))
78
79 ## merge with coordinate information of msidata
80 msidata_coordinates = data.frame(coord(msidata)\$x, coord(msidata)\$y, c(1:ncol(msidata)))
81 colnames(msidata_coordinates) = c("x", "y", "pixel_index")
82 merged_annotation = merge(msidata_coordinates, spectra_input, by=c("x", "y"), all.x=TRUE)
83 merged_annotation[is.na(merged_annotation)] = "NA" 78 merged_annotation[is.na(merged_annotation)] = "NA"
84 merged_annotation = merged_annotation[order(merged_annotation\$pixel_index),] 79
85 msidata\$annotation = factor(merged_annotation[,4], levels = unique(as.character(merged_annotation[,4]))) ## keep the right order 80 merged_annotation = merged_annotation[order(merged_annotation\$annotation),]
81 msidata\$annotation = factor(merged_annotation\$annotation)
82
86 83
87 ## overview plot over annotated samples 84 ## overview plot over annotated samples
88 number_combined = length(levels(msidata\$annotation)) 85 number_combined = length(levels(msidata\$annotation))
89 86
90 ## the more annotation groups a file has the smaller will be the legend 87 ## the more annotation groups a file has the smaller will be the legend
98 legend_size = 7 95 legend_size = 7
99 }else{ 96 }else{
100 legend_size = 6 97 legend_size = 6
101 } 98 }
102 99
103 position_df = data.frame(coord(msidata)\$x, coord(msidata)\$y, as.factor(msidata\$annotation)) 100 ## colours selection:
101
102 #if str($pixel_conditional.single_or_overlaid.colour_conditional.colour_type) == "manual_colour"
103 #set $color_string = ','.join(['"%s"' % $color.annotation_color for $color in $pixel_conditional.single_or_overlaid.colour_conditional.colours])
104 colourvector = c($color_string)
105
106 #elif str($pixel_conditional.single_or_overlaid.colour_conditional.colour_type) == "colourpalette"
107
108 number_levels = (length(levels(msidata\$annotation)))
109
110 colourvector = noquote($pixel_conditional.single_or_overlaid.colour_conditional.palettes)(number_levels)
111
112 #end if
113
114 position_df = merged_annotation
104 colnames(position_df) = c("x", "y", "sample_name") 115 colnames(position_df) = c("x", "y", "sample_name")
105 116
106 combine_plot = ggplot(position_df, aes(x=x, y=y, fill=sample_name))+ 117 combine_plot = ggplot(position_df, aes(x=x, y=y, fill=sample_name))+
107 geom_tile(height = 1, width=1)+ 118 geom_tile(height = 1, width=1)+
108 coord_fixed()+ 119 coord_fixed()+
109 ggtitle("Spatial orientation of annotations")+ 120 ggtitle("Spatial orientation of annotations")+
110 theme_bw()+ 121 theme_bw()+
111 theme(plot.title = element_text(hjust = 0.5))+ 122 theme(plot.title = element_text(hjust = 0.5))+
112 theme(text=element_text(family="ArialMT", face="bold", size=12))+ 123 theme(text=element_text(family="ArialMT", face="bold", size=12))+
113 theme(legend.position="bottom",legend.direction="vertical")+ 124 theme(legend.position="bottom",legend.direction="vertical")+
114 theme(legend.key.size = unit(0.2, "line"), legend.text = element_text(size = 6))+ 125 theme(legend.key.size = unit(0.2, "line"), legend.text = element_text(size = 10))+
115 guides(fill=guide_legend(ncol=4,byrow=TRUE)) 126 guides(fill=guide_legend(ncol=4,byrow=TRUE))+
116 127 scale_discrete_manual(aesthetics = c("colour", "fill"), values = colourvector)
117 coord_labels = aggregate(cbind(x,y)~sample_name, data=position_df, mean, na.rm=TRUE, na.action="na.pass") 128
118 coord_labels\$file_number = 1:length(levels(position_df\$sample_name)) 129
130 coord_labels = aggregate(cbind(x,y)~sample_name, data=position_df, mean, na.rm=TRUE, na.action="na.pass")
131 coord_labels\$file_number = 1:length(unique(position_df\$sample_name))
119 132
120 for(file_count in 1:nrow(coord_labels)) 133 for(file_count in 1:nrow(coord_labels))
121 {combine_plot = combine_plot + annotate("text",x=coord_labels[file_count,"x"], 134 {combine_plot = combine_plot + annotate("text",x=coord_labels[file_count,"x"],
122 y=coord_labels[file_count,"y"],label=toString(coord_labels[file_count,4]))} 135 y=coord_labels[file_count,"y"],label=toString(coord_labels[file_count,4]))
136 }
123 137
124 print(combine_plot) 138 print(combine_plot)
125 139
126 ## print legend only for less than 10 samples 140 ## print legend only for less than 10 samples
127 if (length(levels(msidata\$annotation)) < 10){ 141 if (length(levels(msidata\$annotation)) < 10){
128 key_legend = TRUE 142 key_legend = TRUE
129 }else{key_legend = FALSE} 143 }else{key_legend = FALSE}
130 144
131 #if $fullmz: 145 #if $fullmz:
132 ## plot single tabular mz, average per annotation 146 ## plot single tabular mz, average per annotation
133 print(plot(msidata, run="infile", pixel.groups=msidata\$annotation, key=key_legend, col=hue_pal()(length(levels(msidata\$annotation))),superpose=TRUE, strip=FALSE, grid=$grid_variable), main="Average spectrum per group") 147 print(plot(msidata, run="infile", pixel.groups=msidata\$annotation, key=key_legend,superpose=TRUE, strip=FALSE, grid=$grid_variable, col = colourvector), main="Average spectrum per group")
134 #end if 148 #end if
135 149
136 pixeldf = data.frame(table(msidata\$annotation)) 150 pixeldf = data.frame(table(msidata\$annotation))
137 colnames(pixeldf) = c("sample name", "number of pixels") 151 colnames(pixeldf) = c("sample name", "number of pixels")
138 152
163 return(features(msidata, mz=max(mz(msidata)))) 177 return(features(msidata, mz=max(mz(msidata))))
164 } 178 }
165 ) 179 )
166 180
167 ## plot single tabular mz, average per annotation, manual zoom 181 ## plot single tabular mz, average per annotation, manual zoom
168 print(plot(msidata[minmasspixel:maxmasspixel,], run="infile", strip=FALSE, 182
183 print(plot(msidata[minmasspixel:maxmasspixel,], run="infile", strip=FALSE, col = colourvector,
169 xlim= c($token.xlimmin,$token.xlimmax),pixel.groups=msidata\$annotation, grid = $grid_variable, 184 xlim= c($token.xlimmin,$token.xlimmax),pixel.groups=msidata\$annotation, grid = $grid_variable,
170 key=key_legend,col=hue_pal()(length(levels(msidata\$annotation))), superpose=TRUE, main="Average spectrum per group")) 185 key=key_legend, superpose=TRUE, main="Average spectrum per group"))
171 #end for 186 #end for
172 187
173 #elif str($mz_range.mz_range_options) == "tabular_mz": 188 #elif str($mz_range.mz_range_options) == "tabular_mz":
174 189
175 input_mz = read.delim("$mz_range.mz_file", header = $mz_range.tabular_header, stringsAsFactors = FALSE) 190 input_mz = read.delim("$mz_range.mz_file", header = $mz_range.tabular_header, stringsAsFactors = FALSE)
184 minmasspixel = features(msidata, mz=xlimmin) 199 minmasspixel = features(msidata, mz=xlimmin)
185 maxmasspixel = features(msidata, mz=xlimmax) 200 maxmasspixel = features(msidata, mz=xlimmax)
186 201
187 ## plot single tabular mz, average per annotation, tabular zoom 202 ## plot single tabular mz, average per annotation, tabular zoom
188 print(plot(msidata[minmasspixel:maxmasspixel,], run="infile", strip=FALSE, main="Average spectrum per group", 203 print(plot(msidata[minmasspixel:maxmasspixel,], run="infile", strip=FALSE, main="Average spectrum per group",
189 xlim= c(xlimmin,xlimmax),pixel.groups=msidata\$annotation, grid = $grid_variable, 204 xlim= c(xlimmin,xlimmax),pixel.groups=msidata\$annotation, grid = $grid_variable, col = colourvector,
190 key=key_legend,col=hue_pal()(length(levels(msidata\$annotation))), superpose=TRUE)) 205 key=key_legend, superpose=TRUE))
206
191 } 207 }
192 } 208 }
193 209
194 #end if 210 #end if
195 211 #elif str($pixel_conditional.single_or_overlaid.plot_type) == 'separate_plots':
196 #elif str($pixel_conditional.column_pixel_annotation)== "None": 212
197 213
198 spectra_tabular = spectra_tabular[,c($pixel_conditional.column_pixel_x, $pixel_conditional.column_pixel_y)] 214 spectra_tabular = spectra_tabular[,c($pixel_conditional.column_pixel_x, $pixel_conditional.column_pixel_y)]
199 colnames(spectra_tabular) = c("x", "y") 215 colnames(spectra_tabular) = c("x", "y")
200 216
201 dataset_pixels = data.frame(coord(msidata)\$x, coord(msidata)\$y) 217 dataset_pixels = data.frame(coord(msidata)\$x, coord(msidata)\$y)
202 colnames(dataset_pixels) = c("x", "y") 218 colnames(dataset_pixels) = c("x", "y")
203 spectra_input = merge(dataset_pixels, spectra_tabular, by=c("x", "y")) 219 spectra_input = merge(dataset_pixels, spectra_tabular, by=c("x", "y"))
204 false_input = merge(spectra_tabular, spectra_input, by=c("x", "y")) 220 false_input = merge(spectra_tabular, spectra_input, by=c("x", "y"))
259 maxmasspixel = maxmasspixel+1 275 maxmasspixel = maxmasspixel+1
260 } 276 }
261 } 277 }
262 278
263 ## print single tabular mz; manual zoom 279 ## print single tabular mz; manual zoom
264 print(plot(msidata[minmasspixel:maxmasspixel,], grid = $grid_variable, coord=list(x=x_coord, y=y_coord), key=TRUE, xlim= c($token.xlimmin,$token.xlimmax), col="black")) 280
281 tryCatch(
282 {
283 print(plot(msidata[minmasspixel:maxmasspixel,], grid = $grid_variable, coord=list(x=x_coord, y=y_coord), key=TRUE, xlim= c($token.xlimmin,$token.xlimmax), col="black"))
284 }
285 ,
286 error=function(cond) {
287 ## if there are no intensities > 0 in the chosen plotting window, write a warning
288 text(0.5,0.5,labels = c(paste("No peaks in the spectrum with the coordinates x =", x_coord, ", y =", y_coord, sep=" ")))
289 }
290 )
265 291
266 #end for 292 #end for
267 293
268 #elif str($mz_range.mz_range_options) == "tabular_mz": 294 #elif str($mz_range.mz_range_options) == "tabular_mz":
269 295
454 </configfiles> 480 </configfiles>
455 <inputs> 481 <inputs>
456 <expand macro="reading_msidata"/> 482 <expand macro="reading_msidata"/>
457 <expand macro="pdf_filename"/> 483 <expand macro="pdf_filename"/>
458 <conditional name="pixel_conditional"> 484 <conditional name="pixel_conditional">
459 <param name="pixel_type" type="select" label="Choose spectra (pixel) and/or add spectra annotations"> 485 <param name="pixel_type" type="select" label="Choose spectra">
460 <option value="all_pixel" selected="True" >All spectra</option> 486 <option value="all_pixel" selected="True" >Plot mean spectra based on all spectra</option>
461 <option value="tabular_pixel">Single spectra</option> 487 <option value="tabular_pixel">Plot single spectra (separate or overlaid)</option>
462 </param> 488 </param>
463 <when value="tabular_pixel"> 489 <when value="tabular_pixel">
464 <param name="pixel_file" type="data" format="tabular" label="Load tabular file with pixel coordinates" 490 <param name="pixel_file" type="data" format="tabular" label="Load tabular file with pixel coordinates"
465 help="Two or three columns: x values, y values, optionally annotations"/> 491 help="Two or three columns: x values, y values, optionally annotations"/>
466 <param name="column_pixel_x" data_ref="pixel_file" label="Column with x values" type="data_column"/> 492 <param name="column_pixel_x" data_ref="pixel_file" label="Column with x values" type="data_column"/>
467 <param name="column_pixel_y" data_ref="pixel_file" label="Column with y values" type="data_column"/> 493 <param name="column_pixel_y" data_ref="pixel_file" label="Column with y values" type="data_column"/>
468 <param name="column_pixel_annotation" data_ref="pixel_file" optional="True" label="Column with annotations" type="data_column"/>
469 <param name="tabular_pixel_header" type="boolean" label="Tabular files contain a header line" truevalue="TRUE" falsevalue="FALSE"/> 494 <param name="tabular_pixel_header" type="boolean" label="Tabular files contain a header line" truevalue="TRUE" falsevalue="FALSE"/>
495 <conditional name="single_or_overlaid">
496 <param name="plot_type" type="select" label="Separate plot per spectrum or overlaid plot with average spectra per annotation group">
497 <option value="separate_plots" selected="True" >Separate spectra plots</option>
498 <option value="overlaid_plots">Overlaid spectra plots</option>
499 </param>
500 <when value="separate_plots"/>
501 <when value="overlaid_plots">
502 <param name="column_pixel_annotation" data_ref="pixel_file" label="Select column with annotations" type="data_column"/>
503 <conditional name="colour_conditional">
504 <param name="colour_type" type="select" label="Choose a colour scheme">
505 <option value="colourpalette" selected="True" >Colour palette</option>
506 <option value="manual_colour">Manual selection</option>
507 </param>
508 <when value="manual_colour">
509 <repeat name="colours" title="Colours for the plots" min="1" max="50">
510 <param name="annotation_color" type="color" label="Colours" value="#ff00ff" help="Numbers of colours should be the same as number of components">
511 <sanitizer>
512 <valid initial="string.letters,string.digits">
513 <add value="#" />
514 </valid>
515 </sanitizer>
516 </param>
517 </repeat>
518 </when>
519 <when value="colourpalette">
520 <param name="palettes" type="select" display="radio" label="Select a colourpalette">
521 <option value="hue_pal()" selected="True">hue</option>
522 <option value="rainbow">rainbow</option>
523 <option value="heat.colors">heat colors</option>
524 <option value="terrain.colors">terrain colors</option>
525 <option value="topo.colors">topo colors</option>
526 <option value="cm.colors">cm colors</option>
527 </param>
528 </when>
529 </conditional>
530 </when>
531 </conditional>
470 </when> 532 </when>
471 <when value="all_pixel"> 533 <when value="all_pixel">
472 </when> 534 </when>
473 </conditional> 535 </conditional>
474 <param name="fullmz" type="boolean" label="Plot complete m/z range" checked="True" truevalue="TRUE" falsevalue="FALSE"/> 536 <param name="fullmz" type="boolean" label="Plot complete m/z range" checked="True" truevalue="TRUE" falsevalue="FALSE"/>
552 <conditional name="pixel_conditional"> 614 <conditional name="pixel_conditional">
553 <param name="pixel_type" value="tabular_pixel"/> 615 <param name="pixel_type" value="tabular_pixel"/>
554 <param name="pixel_file" value="annotations.tabular"/> 616 <param name="pixel_file" value="annotations.tabular"/>
555 <param name="column_pixel_x" value="1"/> 617 <param name="column_pixel_x" value="1"/>
556 <param name="column_pixel_y" value="2"/> 618 <param name="column_pixel_y" value="2"/>
557 <param name="column_pixel_annotation" value="4"/>
558 <param name="tabular_pixel_header" value="TRUE"/> 619 <param name="tabular_pixel_header" value="TRUE"/>
620
621 <conditional name="single_or_overlaid">
622 <param name="plot_type" value="overlaid_plots"/>
623 <param name="column_pixel_annotation" value="4"/>
624 <param name="colour_type" value="manual_colour"/>
625 <repeat name="colours">
626 <param name="annotation_color" value="#0000FF"/>
627 </repeat>
628 <repeat name="colours">
629 <param name="annotation_color" value="#00C957"/>
630 </repeat>
631 <repeat name="colours">
632 <param name="annotation_color" value="#B0171F"/>
633 </repeat>
634 </conditional>
559 </conditional> 635 </conditional>
560 <param name="fullmz" value="FALSE"/> 636 <param name="fullmz" value="FALSE"/>
561 <conditional name="mz_range"> 637 <conditional name="mz_range">
562 <param name="mz_range_options" value="manual_mz"/> 638 <param name="mz_range_options" value="manual_mz"/>
563 <repeat name="zoomed_sample"> 639 <repeat name="zoomed_sample">
564 <param name="xlimmin" value="1250"/> 640 <param name="xlimmin" value="1250"/>
565 <param name="xlimmax" value="1270"/> 641 <param name="xlimmax" value="1270"/>
566 </repeat> 642 </repeat>
567 </conditional> 643 </conditional>
568 <param name="grid_variable" value="FALSE"/> 644 <param name="grid_variable" value="FALSE"/>
569 <output name="plots" file="Plot_analyze75_allpixels.pdf" compare="sim_size"/> 645 <output name="plots" file="Plot_analyze75_allpixels.pdf" compare="sim_size"/>
570 </test> 646 </test>
571 <test> 647 <test>
572 <param name="infile" value="3_files_combined.RData" ftype="rdata"/> 648 <param name="infile" value="3_files_combined.RData" ftype="rdata"/>
573 <conditional name="pixel_conditional"> 649 <conditional name="pixel_conditional">
574 <param name="pixel_type" value="all_pixel"/> 650 <param name="pixel_type" value="all_pixel"/>