view mz_images.xml @ 7:5e6f8fb2df07 draft

"planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/cardinal commit f986c51abe33c7f622d429a3c4a79ee24b33c1f3"
author galaxyp
date Thu, 23 Apr 2020 08:08:28 -0400
parents 773a24b240e1
children 08ebd8da780c
line wrap: on
line source

<tool id="cardinal_mz_images" name="MSI mz images" version="@VERSION@.0">
    <description>
        mass spectrometry imaging m/z heatmaps
    </description>
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="requirements">
        <requirement type="package" version="2.3">r-gridextra</requirement>
    </expand>
    <command detect_errors="aggressive">
<![CDATA[
        @INPUT_LINKING@
        cat '${MSI_heatmaps}' &&
        Rscript '${MSI_heatmaps}'
]]>
    </command>
    <configfiles>
        <configfile name="MSI_heatmaps"><![CDATA[

################################# load libraries and read file #################

library(Cardinal)
library(gridExtra)
##library(lattice)

@READING_MSIDATA_FULLY_COMPATIBLE@

@DATA_PROPERTIES@

##################################### read and filter input m/z ##############

input_list = read.delim("$calibrant_file", header = $calibrant_header, stringsAsFactors = FALSE)
input_list = input_list[,c($mz_column, $name_column)]

### calculate how many input m/z are valid: 
inputmasses = input_list[input_list[,1]>minmz & input_list[,1]<maxmz,]
inputmz = as.numeric(inputmasses[,1])
inputnames = as.character(inputmasses[,2])


############################## PDF #############################################
################################################################################

pdf("heatmaps.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=paste("\nHeatmap images\n\n", "Filename:\n", "$filename"))

new_row = data.frame(paste0("# valid m/z in \n", "$calibrant_file.display_name"), paste0(length(inputmz), "/", length(input_list[,1])))
colnames(new_row) = c("properties", "values")
property_df = rbind(property_df, new_row)

grid.table(property_df, rows= NULL)

############################# II) images ####################################

### only plot images when file has features and pixels: 
par(mar=c(0,0,0,0), oma=c(0,0,0,1))## margin for image legend
if (ncol(msidata)>0 & nrow(msidata) >0){
    if (length(inputmz) != 0){
        for (mass in 1:length(inputmz)){

                print(image(msidata, mz=inputmz[mass],plusminus = $plusminus_dalton, normalize.image="$normalize_image", contrast.enhance = "$image_contrast", 
                smooth.image = "$image_smoothing", strip=$strip, colorkey=$colorkey, colorscale=$colorscale, ylim= c(maximumy,minimumy)))}


        ## optional svg output with original coordinates
        #if $svg_pixelimage:
            print("svg pixel image")
            ## reverse y axis for svg output = correct order and nice svg image
            coord(msidata)\$y <- max(coord(msidata)\$y) - coord(msidata)\$y + 1
            ## works only with MSImageSet as expected
            msidata = as(msidata, "MSImageSet")

            svg(file="svg_pixel_output.svg", width=maximumx, height=maximumy)
            par(mar=c(0,0,0,0), oma=c(0,0,0,0))## no margin for svg
            print(image(msidata, mz = inputmz[1],strip=FALSE, plusminus = $plusminus_dalton, colorscale=$colorscale, colorkey = FALSE,axes=FALSE, xlab=NA, ylab=NA,contrast.enhance = "$image_contrast", smooth.image = "$image_smoothing", normalize.image="$normalize_image"))
            dev.off()
        #end if

        ## optional overlay all input m/z in one image
        #if str($overlay_cond.overlay_selection) == "yes_overlay":

        #set $color_string = ','.join(['"%s"' % $color.feature_color for $color in $overlay_cond.colours])
        colourvector = c($color_string)
            par(mar=c(0,0,0,0), oma=c(0,0,0,1))## margin for image legend
            print(image(msidata, mz=inputmz, 
                  plusminus = $plusminus_dalton,
                  col=colourvector,
                  contrast.enhance = "$image_contrast", 
                  normalize.image="$normalize_image", 
                  smooth.image = "$image_smoothing",
                  superpose=TRUE, main="overlay of all m/z", 
                  key=FALSE, 
                  ylim= c(maximumy,minimumy)))

             legend("$overlay_cond.legend_position",
                       fill=colourvector,
                      legend=as.numeric(sort(as.character(inputmz))))
        #end if

    } else {print("The input m/z were invalid")}
    dev.off()
}else{
    print("input file has no features or pixels left")
dev.off()
}


    ]]></configfile>
    </configfiles>
    <inputs>
        <expand macro="reading_msidata"/>
        <expand macro="pdf_filename"/>
        <expand macro="reading_2_column_mz_tabular"/>

        <param name="plusminus_dalton" value="0.25" type="float" label="m/z range" help="average of the m/z window will be computed"/>
        <param name="image_contrast" type="select" label="Contrast enhancement" help="The 'histogram' equalization method flatterns the distribution of intensities. The hotspot 'suppression' method uses thresholding to reduce the intensities of hotspots">
            <option value="none" selected="True">none</option>
            <option value="suppression">suppression</option>
            <option value="histogram">histogram</option>
        </param>
        <param name="image_smoothing" type="select" label="Image smoothing" help="The 'gaussian' smoothing method smooths images with a simple gaussian kernel. The 'adaptive' method uses bilateral filtering to preserve edges">
            <option value="none" selected="True">none</option>
            <option value="gaussian">gaussian</option>
            <option value="adaptive">adaptive</option>
        </param>
        <param name="colorscale" type="select" display="radio" label="Colorscale">
            <option value="viridis" selected="True">viridis</option>
            <option value="magma">magma</option>
            <option value="plasma">plasma</option>
            <option value="inferno">inferno</option>
        </param>
        <param name="strip" type="boolean" checked="True" truevalue="TRUE" falsevalue="FALSE" label="Display m/z value in plot"/>
        <param name="colorkey" type="boolean" checked="True" truevalue="TRUE" falsevalue="FALSE" label="Display colorkey in plot"/>
        <param name="normalize_image" type="boolean" truevalue="linear" falsevalue="none" label="Linear normalization of image"/>
        <param name="svg_pixelimage" type="boolean" label="Export first valid m/z from tabular file as SVG"/>
        <conditional name="overlay_cond">
            <param name="overlay_selection" type="select" label="Overlay image">
                <option value="no_overlay" selected="True">no</option>
                <option value="yes_overlay">yes</option>
            </param>
            <when value="no_overlay"/>
            <when value="yes_overlay">
            <repeat name="colours" title="Colours for the plots" min="1" max="50">
                <param name="feature_color" type="color" label="Colours" value="#ff00ff" help="Numbers of colours should be the same as number of m/z in tabular file">
                  <sanitizer>
                    <valid initial="string.letters,string.digits">
                      <add value="#" />
                    </valid>
                  </sanitizer>
                </param>
            </repeat>
            <param name="legend_position" type="select" display="radio" label="Position of legend">
                <option value="topright" selected="True">topright</option>
                <option value="top">top</option>
                <option value="topleft">topleft</option>
                <option value="left">left</option>
                <option value="bottomleft">bottomleft</option>
                <option value="bottom">bottom</option>
                <option value="bottomright">bottomright</option>
                <option value="right">right</option>
            </param>
            </when>
        </conditional>
    </inputs>
    <outputs>
        <data format="pdf" name="plots" from_work_dir="heatmaps.pdf" label="${tool.name} on ${on_string}: results"/>
        <data format="svg" name="svg_output" from_work_dir="svg_pixel_output.svg" label="${tool.name} on ${on_string}: image.svg">
            <filter>svg_pixelimage</filter>
        </data>
    </outputs>
    <tests>
        <test>
            <expand macro="infile_imzml"/>
            <param name="calibrant_file" value="inputpeptides.tabular" ftype="tabular"/>
            <param name="mz_column" value="1"/>
            <param name="name_column" value="2"/>
            <param name="plusminus_dalton" value="0.25"/>
            <param name="filename" value="Testfile_imzml"/>
            <param name="image_contrast" value="histogram"/>
            <param name="strip" value="True"/>
            <param name="colorkey" value="True"/>
            <output name="plots" file="Heatmaps_imzml.pdf" ftype="pdf" compare="sim_size"/>
        </test>
        <test>
            <expand macro="infile_analyze75"/>
            <param name="calibrant_file" value="inputpeptides2.tabular" ftype="tabular"/>
            <param name="mz_column" value="1"/>
            <param name="name_column" value="1"/>
            <param name="plusminus_dalton" value="0.5"/>
            <param name="filename" value="Testfile_analyze75"/>
            <param name="colorscale" value="magma"/>
            <param name="image_smoothing" value="gaussian"/>
            <param name="strip" value="False"/>
            <param name="colorkey" value="True"/>
            <param name="svg_pixelimage" value="True"/>
            <output name="plots" file="Heatmaps_analyze75.pdf" ftype="pdf" compare="sim_size"/>
            <output name="svg_output" file="analyze75.svg" ftype="svg" compare="sim_size"/>
        </test>
        <test>
            <param name="infile" value="preprocessed.RData" ftype="rdata"/>
            <param name="calibrant_file" value="inputpeptides.tabular" ftype="tabular"/>
            <param name="mz_column" value="1"/>
            <param name="name_column" value="2"/>
            <param name="plusminus_dalton" value="0.5"/>
            <param name="strip" value="True"/>
            <param name="colorkey" value="True"/>
            <param name="filename" value="Testfile_rdata"/>
            <output name="plots" file="Heatmaps_rdata.pdf" ftype="pdf" compare="sim_size"/>
        </test>
        <test>
            <param name="infile" value="empty_spectra.rdata" ftype="rdata"/>
            <param name="calibrant_file" value="inputpeptides2.tabular" ftype="tabular"/>
            <param name="mz_column" value="1"/>
            <param name="name_column" value="1"/>
            <param name="plusminus_dalton" value="0.5"/>
            <param name="colorscale" value="plasma"/>
            <param name="strip" value="True"/>
            <param name="colorkey" value="False"/>
            <param name="filename" value="Testfile_rdata"/>
            <output name="plots" file="Heatmaps_LM8_file16.pdf" ftype="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>
            <param name="calibrant_file" value="inputpeptides.tabular" ftype="tabular"/>
            <param name="mz_column" value="1"/>
            <param name="name_column" value="2"/>
            <conditional name="overlay_cond">
                <param name="overlay_selection" value="yes_overlay"/>
                <repeat name="colours">
                    <param name="feature_color" value="#ff00ff"/>
                </repeat>
                <repeat name="colours">
                    <param name="feature_color" value="#1f497d"/>
                </repeat>
                <repeat name="colours">
                    <param name="feature_color" value="#00b050"/>
                </repeat>
                <param name="legend_position" value="topleft"/>
            </conditional>
            <output name="plots" file="Heatmaps_processed.pdf" ftype="pdf" compare="sim_size"/>
        </test>
    </tests>
    <help><![CDATA[

@CARDINAL_DESCRIPTION@

-----

This tool uses the Cardinal image function to plot the intensity distribution of m/z features of mass spectrometry imaging data. 

@MSIDATA_INPUT_DESCRIPTION@

@MZ_2COLS_TABULAR_INPUT_DESCRIPTION@


**Tips** 

- In case tabular file cannot be selected in drop-down menu: Datatype in Galaxy must be tabular otherwise file will not appear in selection window (if Galaxy auto-detection was wrong, datatype can be changed by pressing the pen button (edit attributes))
- The contrast enhance and image smoothing functions require a certain number (about 2%) of m/z features with intensities > 0 across all spectra

**Output**

- Single Pdf with the heatmap images
- optional: svg file with the heatmap of the first valid m/z in the tabular file


    ]]>
    </help>
    <expand macro="citations"/>
</tool>