Mercurial > repos > galaxyp > cardinal_spectra_plots
view spectra_plots.xml @ 12:10566783d718 draft
"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cardinal commit e499c9124d3fd85a7fc47b95c206ce91a5e3678c-dirty"
author | galaxyp |
---|---|
date | Tue, 03 Nov 2020 23:11:11 +0000 |
parents | 0b7e00e5b9d2 |
children | c5aa9fca18c6 |
line wrap: on
line source
<tool id="cardinal_spectra_plots" name="MSI plot spectra" version="@VERSION@.1"> <description> mass spectrometry imaging mass spectra plots </description> <macros> <import>macros.xml</import> </macros> <expand macro="requirements"> <requirement type="package" version="3.3.2">r-ggplot2</requirement> <requirement type="package" version="2.3">r-gridextra</requirement> <requirement type="package" version="1.1.1">r-scales</requirement> </expand> <command detect_errors="exit_code"> <![CDATA[ @INPUT_LINKING@ cat '${MSI_mzplots}' && Rscript '${MSI_mzplots}' ]]> </command> <configfiles> <configfile name="MSI_mzplots"><![CDATA[ ################################# load libraries and read file ################# library(Cardinal) library(gridExtra) library(ggplot2) library(scales) @READING_MSIDATA_FULLY_COMPATIBLE@ @DATA_PROPERTIES@ ######################################## PDF ################################### ################################################################################ ################################################################################ pdf("mzplots.pdf", fonts = "Times", pointsize = 12) plot(0,type='n',axes=FALSE,ann=FALSE) #if not $filename: #set $filename = $infile.display_name #end if title(main=paste0("Mass spectra for file: \n\n","$filename")) ############################# I) numbers ###################################### ############################################################################### grid.table(property_df, rows= NULL) ## run only if mz and pixels are > 0 if (ncol(msidata)>0 & nrow(msidata) >0){ pixeldf = data.frame(matrix(ncol = 2, nrow=0)) ############################# single pixel ################################ ########################################################################### #if str( $pixel_conditional.pixel_type) == 'tabular_pixel': print("tabular_pixel") ## read and extract x,y, optional annotation information spectra_tabular = read.delim("$pixel_conditional.pixel_file", header = $pixel_conditional.tabular_pixel_header, stringsAsFactors = FALSE) #if $pixel_conditional.column_pixel_annotation: spectra_input = spectra_tabular[,c($pixel_conditional.column_pixel_x, $pixel_conditional.column_pixel_y, $pixel_conditional.column_pixel_annotation)] colnames(spectra_input) = c("x", "y", "annotation") input_pixels = paste(spectra_input[,1], spectra_input[,2], sep="_") dataset_pixels = paste(coord(msidata)\$x, coord(msidata)\$y, sep="_") pixelsofinterest = dataset_pixels %in% input_pixels msidata = msidata[,pixelsofinterest] ## merge with coordinate information of msidata msidata_coordinates = data.frame(coord(msidata)\$x, coord(msidata)\$y, c(1:ncol(msidata))) colnames(msidata_coordinates) = c("x", "y", "pixel_index") merged_annotation = merge(msidata_coordinates, spectra_input, by=c("x", "y"), all.x=TRUE) merged_annotation[is.na(merged_annotation)] = "NA" merged_annotation = merged_annotation[order(merged_annotation\$pixel_index),] msidata\$annotation = factor(merged_annotation[,4], levels = unique(as.character(merged_annotation[,4]))) ## keep the right order ## overview plot over annotated samples number_combined = length(levels(msidata\$annotation)) ## the more annotation groups a file has the smaller will be the legend if (number_combined<20){ legend_size = 10 }else if (number_combined>20 && number_combined<40){ legend_size = 9 }else if (number_combined>40 && number_combined<60){ legend_size = 8 }else if (number_combined>60 && number_combined<100){ legend_size = 7 }else{ legend_size = 6 } position_df = data.frame(coord(msidata)\$x, coord(msidata)\$y, as.factor(msidata\$annotation)) colnames(position_df) = c("x", "y", "sample_name") combine_plot = ggplot(position_df, aes(x=x, y=y, fill=sample_name))+ geom_tile(height = 1, width=1)+ coord_fixed()+ ggtitle("Spatial orientation of annotations")+ theme_bw()+ theme(plot.title = element_text(hjust = 0.5))+ theme(text=element_text(family="ArialMT", face="bold", size=12))+ theme(legend.position="bottom",legend.direction="vertical")+ theme(legend.key.size = unit(0.2, "line"), legend.text = element_text(size = 6))+ guides(fill=guide_legend(ncol=4,byrow=TRUE)) coord_labels = aggregate(cbind(x,y)~sample_name, data=position_df, mean, na.rm=TRUE, na.action="na.pass") coord_labels\$file_number = 1:length(levels(position_df\$sample_name)) for(file_count in 1:nrow(coord_labels)) {combine_plot = combine_plot + annotate("text",x=coord_labels[file_count,"x"], y=coord_labels[file_count,"y"],label=toString(coord_labels[file_count,4]))} print(combine_plot) ## print legend only for less than 10 samples if (length(levels(msidata\$annotation)) < 10){ key_legend = TRUE }else{key_legend = FALSE} #if $fullmz: ## plot single tabular mz, average per annotation 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") #end if pixeldf = data.frame(table(msidata\$annotation)) colnames(pixeldf) = c("sample name", "number of pixels") ##################### II) Sample: plot zoom-in mass spectrum ########## #if str($mz_range.mz_range_options) == "manual_mz": #for $token in $mz_range.zoomed_sample: minmasspixel = tryCatch( { features(msidata, mz=$token.xlimmin) }, warning=function(cond) { ## if xlimmin is outside mz range, use min mz value return(features(msidata, mz=min(mz(msidata)))) } ) maxmasspixel = tryCatch( { features(msidata, mz=$token.xlimmax) }, warning=function(cond) { ## if xlimax is outside mz range, use max mz value return(features(msidata, mz=max(mz(msidata)))) } ) ## plot single tabular mz, average per annotation, manual zoom print(plot(msidata[minmasspixel:maxmasspixel,], run="infile", strip=FALSE, xlim= c($token.xlimmin,$token.xlimmax),pixel.groups=msidata\$annotation, grid = $grid_variable, key=key_legend,col=hue_pal()(length(levels(msidata\$annotation))), superpose=TRUE, main="Average spectrum per group")) #end for #elif str($mz_range.mz_range_options) == "tabular_mz": input_mz = read.delim("$mz_range.mz_file", header = $mz_range.tabular_header, stringsAsFactors = FALSE) input_mz_features = input_mz[,c($mz_range.column_mz)] for (feature_count in input_mz_features){ if (feature_count>min(mz(msidata))&feature_count<max(mz(msidata))){ xlimmin = feature_count-$mz_range.minus_mz_tab xlimmax = feature_count+$mz_range.plus_mz_tab minmasspixel = features(msidata, mz=xlimmin) maxmasspixel = features(msidata, mz=xlimmax) ## plot single tabular mz, average per annotation, tabular zoom print(plot(msidata[minmasspixel:maxmasspixel,], run="infile", strip=FALSE, main="Average spectrum per group", xlim= c(xlimmin,xlimmax),pixel.groups=msidata\$annotation, grid = $grid_variable, key=key_legend,col=hue_pal()(length(levels(msidata\$annotation))), superpose=TRUE)) } } #end if #elif str($pixel_conditional.column_pixel_annotation)== "None": spectra_tabular = spectra_tabular[,c($pixel_conditional.column_pixel_x, $pixel_conditional.column_pixel_y)] colnames(spectra_tabular) = c("x", "y") dataset_pixels = data.frame(coord(msidata)\$x, coord(msidata)\$y) colnames(dataset_pixels) = c("x", "y") spectra_input = merge(dataset_pixels, spectra_tabular, by=c("x", "y")) false_input = merge(spectra_tabular, spectra_input, by=c("x", "y")) ## for loop for every valid spectrum (pixel) for (pixel_count in 1:nrow(spectra_input)){ x_coord = spectra_input[pixel_count,1] y_coord = spectra_input[pixel_count,2] ##################### I) Sample: plot full mass spectrum ############## #if $fullmz: ## plot full mz for single tabular mz print(plot(msidata, coord=list(x=x_coord, y=y_coord), key=TRUE, grid = $grid_variable, col="black")) #end if pixelname = paste0("x = ", x_coord,", ", "y = ", y_coord) input_pixels = paste(x_coord, y_coord, sep="_") dataset_pixels = paste(coord(msidata)\$x, coord(msidata)\$y, sep="_") pixelisvalid = as.character(input_pixels %in% dataset_pixels) pixeldf = rbind(pixeldf, cbind(pixelname, pixelisvalid)) ##################### II) Sample: plot zoom-in mass spectrum ########## #if str($mz_range.mz_range_options) == "manual_mz": #for $token in $mz_range.zoomed_sample: minmasspixel = tryCatch( { features(msidata, mz=$token.xlimmin) }, warning=function(cond) { ## if xlimmin is outside mz range, use min mz value return(features(msidata, mz=min(mz(msidata)))) } ) maxmasspixel = tryCatch( { features(msidata, mz=$token.xlimmax) }, warning=function(cond) { ## if xlimax is outside mz range, use max mz value return(features(msidata, mz=max(mz(msidata)))) } ) ## same min and max not possible for plot if (minmasspixel == maxmasspixel){ if (maxmasspixel == nrow(msidata)){ minmasspixel = minmasspixel-1 }else{ maxmasspixel = maxmasspixel+1 } } ## print single tabular mz; manual zoom 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")) #end for #elif str($mz_range.mz_range_options) == "tabular_mz": input_mz = read.delim("$mz_range.mz_file", header = $mz_range.tabular_header, stringsAsFactors = FALSE) input_mz_features = input_mz[,c($mz_range.column_mz)] for (feature_count in input_mz_features){ if (feature_count>min(mz(msidata))&feature_count<max(mz(msidata))){ xlimmin = feature_count-$mz_range.minus_mz_tab xlimmax = feature_count+$mz_range.plus_mz_tab minmasspixel = tryCatch( { features(msidata, mz=xlimmin) }, warning=function(cond) { ## if xlimmin is outside mz range, use min mz value return(features(msidata, mz=min(mz(msidata)))) } ) maxmasspixel = tryCatch( { maxmasspixel = features(msidata, mz=xlimmax) }, warning=function(cond) { ## if xlimax is outside mz range, use max mz value return(features(msidata, mz=max(mz(msidata)))) } ) ## same min and max not possible for plot if (minmasspixel == maxmasspixel){ if (maxmasspixel == nrow(msidata)){ minmasspixel = minmasspixel-1 }else{ maxmasspixel = maxmasspixel+1 } } ## print single tabular mz; tabular zoom print(plot(msidata[minmasspixel:maxmasspixel,], grid = $grid_variable, coord=list(x=x_coord, y=y_coord), key=TRUE, xlim= c(xlimmin,xlimmax), col="black")) } } #end if } colnames(pixeldf) = c("pixel coordinates", "coordinates were found in this file") #end if ############################# all pixel ################################ ########################################################################### #elif str( $pixel_conditional.pixel_type) == 'all_pixel': print("all pixels") ##################### I) Sample: plot full mass spectrum ############## #if $fullmz: print(plot(msidata, run="infile", key=TRUE, strip=FALSE, main="Average spectrum", grid = $grid_variable, col="black")) #end if ##################### II) Sample: plot zoom-in mass spectrum ########## #if str($mz_range.mz_range_options) == "manual_mz": #for $token in $mz_range.zoomed_sample: minmasspixel = tryCatch( { features(msidata, mz=$token.xlimmin) }, warning=function(cond) { ## if xlimmin is outside mz range, use min mz value return(features(msidata, mz=min(mz(msidata)))) } ) maxmasspixel = tryCatch( { features(msidata, mz=$token.xlimmax) }, warning=function(cond) { ## if xlimax is outside mz range, use max mz value return(features(msidata, mz=max(mz(msidata)))) } ) ## same min and max not possible for plot if (minmasspixel == maxmasspixel){ if (maxmasspixel == nrow(msidata)){ minmasspixel = minmasspixel-1 }else{ maxmasspixel = maxmasspixel+1 } } print(plot(msidata[minmasspixel:maxmasspixel,], grid = $grid_variable, main="Average spectrum", run = "infile", strip=FALSE, key=TRUE, xlim= c($token.xlimmin,$token.xlimmax), col="black")) #end for #elif str($mz_range.mz_range_options) == "tabular_mz": input_mz = read.delim("$mz_range.mz_file", header = $mz_range.tabular_header, stringsAsFactors = FALSE) input_mz_features = input_mz[,c($mz_range.column_mz)] for (feature_count in input_mz_features){ if (feature_count>min(mz(msidata))&feature_count<max(mz(msidata))){ xlimmin = feature_count-$mz_range.minus_mz_tab xlimmax = feature_count+$mz_range.plus_mz_tab minmasspixel = tryCatch( { features(msidata, mz=xlimmin) }, warning=function(cond) { ## if xlimmin is outside mz range, use min mz value return(features(msidata, mz=min(mz(msidata)))) } ) maxmasspixel = tryCatch( { maxmasspixel = features(msidata, mz=xlimmax) }, warning=function(cond) { ## if xlimax is outside mz range, use max mz value return(features(msidata, mz=max(mz(msidata)))) } ) print(plot(msidata[minmasspixel:maxmasspixel,], grid = $grid_variable, run="infile", key=TRUE, strip=FALSE, main="Average spectrum", xlim= c(xlimmin,xlimmax), col="black")) } } #end if pixeldf = data.frame("$filename", ncol(msidata)) colnames(pixeldf) = c("sample name", "number of pixels") #end if ############################# pixel table ###################################### ############################################################################### ### overview table of pixels or samples: plot(0,type='n',axes=FALSE,ann=FALSE) title(main="Overview of chosen pixel:") ### for more than 20 annotation groups print only 20 samples per page: if (is.null(levels(msidata\$annotation))){ grid.table(pixeldf, rows= NULL) }else if (length(levels(msidata\$annotation)) <= 20){ grid.table(pixeldf, rows= NULL) }else{ grid.table(pixeldf[1:20,], rows= NULL) mincount = 21 maxcount = 40 for (count20 in 1:(ceiling(nrow(pixeldf)/20)-1)){ plot(0,type='n',axes=FALSE,ann=FALSE) if (maxcount <= nrow(pixeldf)){ grid.table(pixeldf[mincount:maxcount,], rows= NULL) mincount = mincount+20 maxcount = maxcount+20 }else{### stop last page with last sample otherwise NA in table grid.table(pixeldf[mincount:nrow(pixeldf),], rows= NULL)} } } dev.off() }else{ print("Inputfile has no intensities > 0") dev.off() } ]]></configfile> </configfiles> <inputs> <expand macro="reading_msidata"/> <expand macro="pdf_filename"/> <conditional name="pixel_conditional"> <param name="pixel_type" type="select" label="Choose spectra (pixel) and/or add spectra annotations"> <option value="all_pixel" selected="True" >All spectra</option> <option value="tabular_pixel">Single spectra</option> </param> <when value="tabular_pixel"> <param name="pixel_file" type="data" format="tabular" label="Load tabular file with pixel coordinates" help="Two or three columns: x values, y values, optionally annotations"/> <param name="column_pixel_x" data_ref="pixel_file" label="Column with x values" type="data_column"/> <param name="column_pixel_y" data_ref="pixel_file" label="Column with y values" type="data_column"/> <param name="column_pixel_annotation" data_ref="pixel_file" optional="True" label="Column with annotations" type="data_column"/> <param name="tabular_pixel_header" type="boolean" label="Tabular files contain a header line" truevalue="TRUE" falsevalue="FALSE"/> </when> <when value="all_pixel"> </when> </conditional> <param name="fullmz" type="boolean" label="Plot complete m/z range" checked="True" truevalue="TRUE" falsevalue="FALSE"/> <conditional name="mz_range"> <param name="mz_range_options" type="select" label="zoomed in m/z range"> <option value="none_mz" selected="True">no m/z range</option> <option value="manual_mz" >Manual input</option> <option value="tabular_mz">Tabular file</option> </param> <when value="none_mz"/> <when value="tabular_mz"> <param name="minus_mz_tab" type="float" value="" label="m/z value to subtract from m/z values in tabular file = lower m/z boundary"/> <param name="plus_mz_tab" type="float" value="" label="m/z value to add to m/z values in tabular file = upper m/z boundary"/> <param name="mz_file" type="data" format="tabular" label="Load tabular file with m/z values"/> <param name="column_mz" data_ref="mz_file" label="Column with mz values" type="data_column"/> <param name="tabular_header" type="boolean" label="Tabular files contain a header line" truevalue="TRUE" falsevalue="FALSE"/> </when> <when value="manual_mz"> <repeat name="zoomed_sample" title="Define min m/z and max m/z for the plotting window" min="1"> <param name="xlimmin" type="float" value="" label="lower m/z boundary for plotting window"/> <param name="xlimmax" type="float" value="" label="upper m/z boundary for plotting window"/> </repeat> </when> </conditional> <param name="grid_variable" type="boolean" label="Add grid to plot" truevalue="TRUE" falsevalue="FALSE"/> </inputs> <outputs> <data format="pdf" name="plots" from_work_dir="mzplots.pdf" label="${tool.name} on ${on_string}:results"/> </outputs> <tests> <test> <expand macro="infile_imzml"/> <conditional name="pixel_conditional"> <param name="pixel_type" value="tabular_pixel"/> <param name="pixel_file" value="spectra_1_pixel.tabular"/> <param name="column_pixel_x" value="1"/> <param name="column_pixel_y" value="2"/> <param name="tabular_pixel_header" value="FALSE"/> </conditional> <param name="fullmz" value="TRUE"/> <conditional name="mz_range"> <param name="mz_range_options" value="manual_mz"/> <repeat name="zoomed_sample"> <param name="xlimmin" value="310"/> <param name="xlimmax" value="320"/> </repeat> <repeat name="zoomed_sample"> <param name="xlimmin" value="350"/> <param name="xlimmax" value="400"/> </repeat> <repeat name="zoomed_sample"> <param name="xlimmin" value="360"/> <param name="xlimmax" value="360.8"/> </repeat> </conditional> <param name="grid_variable" value="TRUE"/> <output name="plots" file="Plot_imzml.pdf" compare="sim_size"/> </test> <test> <expand macro="infile_analyze75"/> <conditional name="pixel_conditional"> <param name="pixel_type" value="tabular_pixel"/> <param name="pixel_file" value="spectra_1_pixel.tabular"/> <param name="column_pixel_x" value="1"/> <param name="column_pixel_y" value="2"/> <param name="tabular_pixel_header" value="TRUE"/> </conditional> <param name="fullmz" value="TRUE"/> <conditional name="mz_range"> <param name="mz_range_options" value="manual_mz"/> <repeat name="zoomed_sample"> <param name="xlimmin" value="840"/> <param name="xlimmax" value="850"/> </repeat> </conditional> <param name="grid_variable" value="FALSE"/> <output name="plots" file="Plot_analyze75.pdf" compare="sim_size"/> </test> <test> <expand macro="infile_analyze75"/> <conditional name="pixel_conditional"> <param name="pixel_type" value="tabular_pixel"/> <param name="pixel_file" value="annotations.tabular"/> <param name="column_pixel_x" value="1"/> <param name="column_pixel_y" value="2"/> <param name="column_pixel_annotation" value="4"/> <param name="tabular_pixel_header" value="TRUE"/> </conditional> <param name="fullmz" value="FALSE"/> <conditional name="mz_range"> <param name="mz_range_options" value="manual_mz"/> <repeat name="zoomed_sample"> <param name="xlimmin" value="1250"/> <param name="xlimmax" value="1270"/> </repeat> </conditional> <param name="grid_variable" value="FALSE"/> <output name="plots" file="Plot_analyze75_allpixels.pdf" compare="sim_size"/> </test> <test> <param name="infile" value="3_files_combined.RData" ftype="rdata"/> <conditional name="pixel_conditional"> <param name="pixel_type" value="all_pixel"/> </conditional> <param name="fullmz" value="TRUE"/> <conditional name="mz_range"> <param name="mz_range_options" value="manual_mz"/> <repeat name="zoomed_sample"> <param name="xlimmin" value="350"/> <param name="xlimmax" value="360"/> </repeat> </conditional> <param name="grid_variable" value="FALSE"/> <output name="plots" file="Plot_rdata.pdf" compare="sim_size"/> </test> <test> <expand macro="processed_infile_imzml"/> <conditional name="processed_cond"> <param name="processed_file" value="processed"/> <param name="accuracy" value="100"/> <param name="units" value="ppm"/> </conditional> <conditional name="pixel_conditional"> <param name="pixel_type" value="all_pixel"/> </conditional> <param name="fullmz" value="FALSE"/> <conditional name="mz_range"> <param name="mz_range_options" value="manual_mz"/> <repeat name="zoomed_sample"> <param name="xlimmin" value="350"/> <param name="xlimmax" value="350.5"/> </repeat> <repeat name="zoomed_sample"> <param name="xlimmin" value="600"/> <param name="xlimmax" value="650"/> </repeat> </conditional> <param name="grid_variable" value="TRUE"/> <output name="plots" file="Plot_processed.pdf" compare="sim_size"/> </test> <test> <param name="infile" value="" ftype="imzml"> <composite_data value="preprocessing_results1.imzml"/> <composite_data value="preprocessing_results1.ibd"/> </param> <param name="centroids" value="TRUE"/> <conditional name="pixel_conditional"> <param name="pixel_type" value="all_pixel"/> </conditional> <param name="fullmz" value="TRUE"/> <conditional name="mz_range"> <param name="mz_range_options" value="manual_mz"/> <repeat name="zoomed_sample"> <param name="xlimmin" value="328"/> <param name="xlimmax" value="330"/> </repeat> <repeat name="zoomed_sample"> <param name="xlimmin" value="335"/> <param name="xlimmax" value="340"/> </repeat> </conditional> <param name="grid_variable" value="TRUE"/> <output name="plots" file="Plot_picked.pdf" compare="sim_size"/> </test> </tests> <help><![CDATA[ @CARDINAL_DESCRIPTION@ ----- This tool uses the Cardinal plot function to generate (zoomed in) mass spectra plots of mass spectrometry imaging data. @MSIDATA_INPUT_DESCRIPTION@ @SPECTRA_TABULAR_INPUT_DESCRIPTION@ @MZ_TABULAR_INPUT_DESCRIPTION@ **Options** *Choosing spectra* - "All spectra": Plots average mass spectra (mean of all spectra) - "Single spectra": Returns a full mass spectrum plot for each input spectrum, which is defined by its x- and y-coordinates - the annotation column is optional: - without annotation column: plots a separate mass spectrum for every input spectrum - with annotation column: plots average mass spectra for each annotation group in different colours *Choosing m/z axis* - "Plot complete m/z range": Full m/z range is plotted - "zoomed in m/z range": Only a customized m/z range is plotted - "Manual input": Provide the minimum and maximum m/z value to define the m/z axis of the plot - "Tabular input": File with m/z values of interest. Needs manual choice of m/z values to subtract and add from the input m/z values to generate the m/z axis of the plot **Output** - Single Pdf with all mass spectra plots ]]> </help> <expand macro="citations"/> </tool>