view msi_ion_images.xml @ 9:9a873d3e2aa9 draft default tip

planemo upload for repository https://github.com/galaxyproteomics/tools-galaxyp/tree/master/tools/msi_ion_images commit 5feaf3d0e0da8cef1241fecc1f4d6f81324594e6
author galaxyp
date Wed, 22 Aug 2018 13:33:23 -0400
parents dd44a98ce32c
children
line wrap: on
line source

<tool id="mass_spectrometry_imaging_ion_images" name="MSI ion images" version="1.10.0.5">
    <description>
        mass spectrometry imaging m/z heatmaps
    </description>
    <requirements>
        <requirement type="package" version="1.10.0">bioconductor-cardinal</requirement>
        <requirement type="package" version="2.2.1">r-gridextra</requirement>
        <requirement type="package" version="0.20-35">r-lattice</requirement>
    </requirements>
    <command detect_errors="aggressive">
<![CDATA[
        #if $infile.ext == 'imzml'
            ln -s '${infile.extra_files_path}/imzml' infile.imzML &&
            ln -s '${infile.extra_files_path}/ibd' infile.ibd &&
        #elif $infile.ext == 'analyze75'
            ln -s '${infile.extra_files_path}/hdr' infile.hdr &&
            ln -s '${infile.extra_files_path}/img' infile.img &&
            ln -s '${infile.extra_files_path}/t2m' infile.t2m &&
        #else
            ln -s $infile infile.RData &&
        #end if
        cat '${MSI_heatmaps}' &&
        Rscript '${MSI_heatmaps}'
]]>
    </command>
    <configfiles>
        <configfile name="MSI_heatmaps"><![CDATA[

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

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

## Read MALDI Imaging dataset


#if $infile.ext == 'imzml'
    #if str($processed_cond.processed_file) == "processed":
        msidata <- readImzML('infile', mass.accuracy=$processed_cond.accuracy, units.accuracy = "$processed_cond.units")
    #else
        msidata <- readImzML('infile', attach.only=TRUE)
    #end if
#elif $infile.ext == 'analyze75'
    msidata = readAnalyze('infile', attach.only=TRUE)
#else
    loadRData <- function(fileName){
    load(fileName)
    get(ls()[ls() != "fileName"])
    }
    msidata = loadRData('infile.RData')
#end if


###################################### file properties in numbers ##############

## Number of features (m/z)
maxfeatures = length(features(msidata))
## Range m/z
minmz = round(min(mz(msidata)), digits=2)
maxmz = round(max(mz(msidata)), digits=2)
## Number of spectra (pixels)
pixelcount = length(pixels(msidata))
## Range x coordinates
minimumx = min(coord(msidata)[,1])
maximumx = max(coord(msidata)[,1])
## Range y coordinates
minimumy = min(coord(msidata)[,2])
maximumy = max(coord(msidata)[,2])
## Range of intensities
minint = round(min(spectra(msidata)[], na.rm=TRUE), digits=2)
maxint = round(max(spectra(msidata)[], na.rm=TRUE), digits=2)
medint = round(median(spectra(msidata)[], na.rm=TRUE), digits=2)
## Number of intensities > 0
npeaks= sum(spectra(msidata)[]>0, na.rm=TRUE)
## Spectra multiplied with m/z (potential number of peaks)
numpeaks = ncol(spectra(msidata)[])*nrow(spectra(msidata)[])
## Percentage of intensities > 0
percpeaks = round(npeaks/numpeaks*100, digits=2)
## Number of empty TICs
TICs = colSums(spectra(msidata)[], na.rm=TRUE) 
NumemptyTIC = sum(TICs == 0)

## Processing informations
processinginfo = processingData(msidata)
centroidedinfo = processinginfo@centroided # TRUE or FALSE

## if TRUE write processinginfo if no write FALSE

## normalization
if (length(processinginfo@normalization) == 0) {
    normalizationinfo='FALSE'
} else {
    normalizationinfo=processinginfo@normalization
}
## smoothing
if (length(processinginfo@smoothing) == 0) {
    smoothinginfo='FALSE'
} else {
    smoothinginfo=processinginfo@smoothing
}
## baseline
if (length(processinginfo@baselineReduction) == 0) {
    baselinereductioninfo='FALSE'
} else {
    baselinereductioninfo=processinginfo@baselineReduction
}
## peak picking
if (length(processinginfo@peakPicking) == 0) {
    peakpickinginfo='FALSE'
} else {
    peakpickinginfo=processinginfo@peakPicking
}

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

input_list = read.delim("$massfile", header = FALSE, stringsAsFactors = FALSE)

### in case input file had only one column with m/z values but not names, duplicate m/z values and use as names:
if (ncol(input_list) == 1){
    input_list = cbind(input_list, input_list)}

### 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"))

############################# I) numbers ####################################

properties = c("Number of m/z features",
               "Range of m/z values",
               "Number of pixels", 
               "Range of x coordinates", 
               "Range of y coordinates",
               "Range of intensities", 
               "Median of intensities",
               "Intensities > 0",
               "Number of zero TICs",
               "Preprocessing", 
               "Normalization", 
               "Smoothing",
               "Baseline reduction",
               "Peak picking",
               "Centroided", 
                paste0("# valid m/z in \n", "$massfile.display_name"))

values = c(paste0(maxfeatures), 
           paste0(minmz, " - ", maxmz), 
           paste0(pixelcount), 
           paste0(minimumx, " - ", maximumx),  
           paste0(minimumy, " - ", maximumy), 
           paste0(minint, " - ", maxint), 
           paste0(medint),
           paste0(percpeaks, " %"), 
           paste0(NumemptyTIC), 
           paste0(" "),
           paste0(normalizationinfo),
           paste0(smoothinginfo),
           paste0(baselinereductioninfo),
           paste0(peakpickinginfo),
           paste0(centroidedinfo), 
           paste0(length(inputmz), "/", length(input_list[,1])))

property_df = data.frame(properties, values)

grid.table(property_df, rows= NULL)

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

### only plot images when file has peaks and valid input m/z: 

if (npeaks > 0){
    if (length(inputmz) != 0){
        for (mass in 1:length(inputmz)){

            ###standard image

            #if str($image_cond.image_type) == "standard_image":
                print("standard image")

                print(image(msidata, mz=inputmz[mass],plusminus = $plusminus_dalton, contrast.enhance = "$image_contrast", 
                smooth.image = "$image_smoothing", strip=$strip, colorkey=$colorkey,ylim= c(maximumy+0.1*maximumy,minimumy-0.1*minimumy),
                main= paste0(inputnames[mass], " (", round(inputmz[mass], digits = 2)," ± ", $plusminus_dalton, " Da)")))}


            ###lattice image

            #elif str($image_cond.image_type) == "lattice_image":
                print("lattice image")

                #if str($strip) =="TRUE": 

                print(image(msidata, mz=inputmz[mass], strip = strip.custom(bg="lightgrey", par.strip.text=list(col="black", cex=.9)),
                lattice=TRUE, plusminus = $plusminus_dalton, contrast.enhance = "$image_contrast", smooth.image = "$image_smoothing", 
                colorkey=$colorkey,ylim= c(maximumy+0.1*maximumy,minimumy-0.1*minimumy),
                main= paste0(inputnames[mass], " (", round(inputmz[mass], digits = 2)," ± ", $plusminus_dalton, " Da)")))}

                #elif str($strip) =="FALSE": 

                print(image(msidata, mz=inputmz[mass], strip = $strip,
                lattice=TRUE, plusminus = $plusminus_dalton, contrast.enhance = "$image_contrast", smooth.image = "$image_smoothing", 
                colorkey=$colorkey,
                main= paste0(inputnames[mass], " (", round(inputmz[mass], digits = 2)," ± ", $plusminus_dalton, " Da)")))}

                #end if

            #end if 

        ## optional svg output with original coordinates
        #if str($svg_cond.svg_pixelimage) == "yes_svg":
            print("svg pixel image")

            svg(file="svg_pixel_output.svg")
            par(mar=c(0,0,0,0))
            image(msidata, mz = inputmz[1],strip=FALSE, ylim=c(maximumy, minimumy), plusminus = $plusminus_dalton,colorkey = FALSE,axes=FALSE, xlab=NA, ylab=NA,contrast.enhance = "$image_contrast", smooth.image = "$image_smoothing")
            dev.off()
        #end if

    } else {print("The input m/z were invalid")}
    dev.off()
}else{
    print("inputfile has no intensities > 0")
dev.off()
}


    ]]></configfile>
    </configfiles>
    <inputs>
        <param name="infile" type="data" format="imzml,rdata,analyze75" label="Inputfile as imzML, Analyze7.5 or Cardinal MSImageSet saved as RData"
            help="Upload composite datatype imzml (ibd+imzML) or analyze75 (hdr+img+t2m) or regular upload .RData (Cardinal MSImageSet)"/>
        <conditional name="processed_cond">
            <param name="processed_file" type="select" label="Is the input file a processed imzML file ">
                <option value="no_processed" selected="True">not a processed imzML</option>
                <option value="processed">processed imzML</option>
            </param>
            <when value="no_processed"/>
            <when value="processed">
                <param name="accuracy" type="float" value="50" label="Mass accuracy to which the m/z values will be binned" help="This should be set to the native accuracy of the mass spectrometer, if known"/>
                <param name="units" display="radio" type="select" label="Unit of the mass accuracy" help="either m/z or ppm">
                    <option value="mz" >mz</option>
                    <option value="ppm" selected="True" >ppm</option>
                </param>
            </when>
        </conditional>
        <param name="filename" type="text" value="" label="Title" help="will appear in the pdf output. If nothing given it will take the dataset name"/>
        <param name="massfile" type="data" format="tabular" label="Tabular file with m/z and names"
            help="first column m/z, second column m/z name, tab separated file"/>
        <param name="image_contrast" type="select" label="Select a contrast enhancement function for the heatmap images" 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="Select an image smoothing function for the heatmap images" 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="plusminus_dalton" value="0.25" type="float" label="M/z range" help="plusminus m/z window"/>
        <param name="strip" type="boolean" checked="True" display="radio" truevalue="TRUE" falsevalue="FALSE" label="Display m/z value in plot"/>
        <param name="colorkey" type="boolean" checked="True" display="radio" truevalue="TRUE" falsevalue="FALSE" label="Display colorkey in plot"/>
        <conditional name="image_cond">
            <param name="image_type" type="select" label="Select the image type">
                <option value="standard_image" selected="True">standard</option>
                <option value="lattice_image">lattice</option>
            </param>
        <when value="standard_image"/>
        <when value="lattice_image"/>
        </conditional>
        <conditional name="svg_cond">
            <param name="svg_pixelimage" type="select" label="Do you want to export a heatmap with the original coordinates as svg image, the first valid m/z from tabular file is used">
                <option value="no_svg" selected="True">No svg image export</option>
                <option value="yes_svg">Export svg image</option>
            </param>
        <when value="no_svg"/>
        <when value="yes_svg"/>
        </conditional>
    </inputs>
    <outputs>
        <data format="pdf" name="plots" from_work_dir="heatmaps.pdf" label = "$infile.display_name heatmaps"/>
        <data format="svg" name="svg_output" from_work_dir="svg_pixel_output.svg" label="$infile.display_name svg">
            <filter>svg_cond['svg_pixelimage'] == 'yes_svg'</filter>
        </data>
    </outputs>
    <tests>
        <test>
            <param name="infile" value="" ftype="imzml">
                <composite_data value="Example_Continuous.imzML"/>
                <composite_data value="Example_Continuous.ibd"/>
            </param>
            <param name="massfile" value="inputpeptides.tabular" ftype="tabular"/>
            <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"/>
            <param name="image_type" value="lattice_image"/>
            <output name="plots" file="Heatmaps_imzml.pdf" compare="sim_size" delta="20000"/>
        </test>
        <test>
            <param name="infile" value="" ftype="analyze75">
                <composite_data value="Analyze75.hdr"/>
                <composite_data value="Analyze75.img"/>
                <composite_data value="Analyze75.t2m"/>
            </param>
            <param name="massfile" value="inputpeptides2.tabular" ftype="tabular"/>
            <param name="plusminus_dalton" value="0.5"/>
            <param name="filename" value="Testfile_analyze75"/>
            <param name="image_smoothing" value="gaussian"/>
            <param name="strip" value="False"/>
            <param name="colorkey" value="True"/>
            <conditional name="svg_cond">
                <param name="svg_pixelimage" value="yes_svg"/>
            </conditional>
            <output name="plots" file="Heatmaps_analyze75.pdf" compare="sim_size" delta="20000"/>
            <output name="svg_output" file="analyze75.svg" compare="sim_size" delta="20000"/>
        </test>
        <test>
            <param name="infile" value="preprocessed.rdata" ftype="rdata"/>
            <param name="massfile" value="inputpeptides.tabular" ftype="tabular"/>
            <param name="plusminus_dalton" value="0.5"/>
            <param name="strip" value="True"/>
            <param name="colorkey" value="True"/>
            <param name="image_type" value="lattice_image"/>
            <param name="filename" value="Testfile_rdata"/>
            <output name="plots" file="Heatmaps_rdata.pdf" compare="sim_size" delta="20000"/>
        </test>
        <test>
            <param name="infile" value="empty_spectra.rdata" ftype="rdata"/>
            <param name="massfile" value="inputpeptides2.tabular" ftype="tabular"/>
            <param name="plusminus_dalton" value="0.5"/>
            <param name="strip" value="True"/>
            <param name="colorkey" value="False"/>
            <param name="filename" value="Testfile_rdata"/>
            <output name="plots" file="Heatmaps_LM8_file16.pdf" compare="sim_size" delta="20000"/>
        </test>
    </tests>
    <help><![CDATA[


Cardinal is an R package that implements statistical & computational tools for analyzing mass spectrometry imaging datasets. `More information on Cardinal <http://cardinalmsi.org//>`_

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

3 types of mass spectrometry imaging data can be used:

- imzml file (upload imzml and ibd file via the "composite" function) `Introduction to the imzml format <https://ms-imaging.org/wp/imzml/>`_
- Analyze7.5 (upload hdr, img and t2m file via the "composite" function)
- Cardinal "MSImageSet" data (with variable name "msidata", saved as .RData)

Tabular file with m/z:

- tab separated file (.tabular), 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 button with the pen (edit attributes))
- first column must contain m/z (separate point numbers by point, not comma)
- optionally a second column with names for the m/z can be provided
- no empty fields or letters are allowed in the first column

Output:

- Pdf with the heatmap images
- optional svg file of a mass-spectrometry imaging m/z heatmap

Troubleshooting:

- no heatmaps are plotted when tabular file doesn't fulfill the criteria described above
- no heatmaps are plotted when the input mass spectrometry imaging file has no intensities > 0
- the contrast enhance and image smoothing functions require a certain number of m/z with intensities > 0 (empirical value > 2% of spectra)
- the standard image function should work for all files while the lattice function works not on every file (nicely)

    ]]>
    </help>
    <citations>
        <citation type="doi">10.1093/bioinformatics/btv146</citation>
    </citations>
</tool>