<macros>
    <token name="@TOOL_VERSION@">3.4.3</token>
    <token name="@VERSION_SUFFIX@">0</token>

    <xml name="requirements">
        <requirements>
            <requirement type="package" version="@TOOL_VERSION@">bioconductor-cardinal</requirement>
            <requirement type="package" version="2.3">r-gridextra</requirement>
            <requirement type="package" version="3.5.1">r-ggplot2</requirement>
            <requirement type="package" version="0.14.1">r-maldiquantforeign</requirement>
            <requirement type="package" version="1.22.2">r-maldiquant</requirement>
            <requirement type="package" version="3.50.0">bioconductor-sva</requirement>
            <requirement type="package" version="1.1.0.1">r-randomcolor</requirement>
            <requirement type="package" version="1.1_3">r-rcolorbrewer</requirement>
            <requirement type="package" version="2.23_24">r-kernsmooth</requirement>
            <requirement type="package" version="1.3.0">r-scales</requirement>
            <requirement type="package" version="1.0.12">r-pheatmap</requirement>
            <yield/>
        </requirements>
    </xml>

    <xml name="print_version">
        <version_command><![CDATA[
echo $(R --version | grep version | grep -v GNU)", Cardinal version" $(R --vanilla --slave -e "library(Cardinal); cat(sessionInfo()\$otherPkgs\$Cardinal\$Version)" 2> /dev/null | grep -v -i "WARNING: ")
        ]]></version_command>
    </xml>

    <token name="@INPUT_LINKING@"><![CDATA[
        #if $infile.ext == 'imzml'
            cp '${infile.extra_files_path}/imzml' infile.imzML &&
            cp '${infile.extra_files_path}/ibd' infile.ibd &&
        #elif $infile.ext == 'analyze75'
            cp '${infile.extra_files_path}/hdr' infile.hdr &&
            cp '${infile.extra_files_path}/img' infile.img &&
            cp '${infile.extra_files_path}/t2m' infile.t2m &&
        #else
            ln -s $infile infile.RData &&
        #end if
    ]]></token>


    <token name="@READING_MSIDATA@"><![CDATA[
        ## importing MSI data files

            ## read RData files (MSI and other data) independent of filename
            loadRData <- function(fileName){
            load(fileName)
            get(ls()[ls() != "fileName"])
            }


        #if $infile.ext == 'imzml'
            #if str($processed_cond.processed_file) == "processed":
                msidata <- readImzML('infile', resolution=$processed_cond.accuracy, attach.only=TRUE, units = "$processed_cond.units")
                centroided(msidata) = $centroids
            #else
                msidata <- readImzML('infile')
                centroided(msidata) = $centroids
            #end if
        #elif $infile.ext == 'analyze75'
            msidata = readAnalyze('infile', attach.only=TRUE)
            centroided(msidata) = $centroids
        #else
            msidata = loadRData('infile.RData')
        #end if

    ]]></token>

    <token name="@DATA_PROPERTIES@"><![CDATA[
        ## Number of features (mz)
        maxfeatures = length(features(msidata))
        ## Range mz
        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])


        properties = c("Number of m/z features",
                       "Range of m/z values",
                       "Number of pixels", 
                       "Range of x coordinates", 
                       "Range of y coordinates")

        values = c(paste0(maxfeatures), 
                   paste0(minmz, " - ", maxmz), 
                   paste0(pixelcount), 
                   paste0(minimumx, " - ", maximumx),
                   paste0(minimumy, " - ", maximumy))

        property_df = data.frame(properties, values)
    ]]></token>

    <token name="@READING_MSIDATA_FULLY_COMPATIBLE@"><![CDATA[
        ## importing MSI data files

        #if $infile.ext == 'imzml'
            #if str($processed_cond.processed_file) == "processed":
                msidata <- readImzML('infile', resolution=$processed_cond.accuracy, units = "$processed_cond.units", attach.only=TRUE)
                centroided(msidata) = $centroids
            #else
                msidata <- readImzML('infile')
                centroided(msidata) = $centroids
            #end if
        #elif $infile.ext == 'analyze75'
            msidata = readAnalyze('infile', attach.only=TRUE)
            centroided(msidata) = $centroids
        #else
            ## function to read RData files independent of filename
            loadRData <- function(fileName){
            load(fileName)
            get(ls()[ls() != "fileName"])
            }
            msidata = loadRData('infile.RData')
            msidata = as(msidata, "MSImagingExperiment")
            run(msidata) = "infile"
        #end if

    ]]></token>

    <token name="@DATA_PROPERTIES_INRAM@"><![CDATA[ 
########################### QC numbers ########################
## including intensity calculations which need data in RAM
	int_matrix = as.matrix(spectra(msidata)) ## only load once into RAM, then re-use
	## Number of NA in spectra matrix
        NAcount = sum(is.na(int_matrix))
	## replace NA with zero to calculate data properties based on intensity matrix, no change in msidata
	int_matrix[is.na(int_matrix)] <- 0


        ## Number of features (mz)
        maxfeatures = length(features(msidata))
        ## Range mz
        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(int_matrix), digits=2)
        maxint = round(max(int_matrix), digits=2)
        ## Number of intensities > 0, for if conditions
        npeaks= sum(int_matrix>0)
        ## Number of NA in spectra matrix
        infcount = sum(is.infinite(int_matrix))
        ## Number of duplicated coordinates
        dupl_coord = sum(duplicated(coord(msidata)))
        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", 
                       "Number of NA intensities",
                       "Number of Inf intensities", 
                       "Number of duplicated coordinates")

        values = c(paste0(maxfeatures), 
                   paste0(minmz, " - ", maxmz), 
                   paste0(pixelcount), 
                   paste0(minimumx, " - ", maximumx),  
                   paste0(minimumy, " - ", maximumy), 
                   paste0(minint, " - ", maxint), 
                   paste0(NAcount), 
                   paste0(infcount), 
                   paste0(dupl_coord))

        property_df = data.frame(properties, values)
    ]]></token>

    <token name="@CARDINAL_DESCRIPTION@"><![CDATA[
        Cardinal is an R package that implements statistical & computational tools for analyzing mass spectrometry imaging datasets.
        `More information on Cardinal <http://cardinalmsi.org/>`_
    ]]></token>
    <token name="@MSIDATA_INPUT_DESCRIPTION@"><![CDATA[
        **Input data**

        - MSI data: 3 types of input data can be used:

            - imzml file (upload imzml and ibd file via the "composite" function) `Introduction to the imzml format <https://ms-imaging.org/imzml/>`_
            - Analyze7.5 (upload hdr, img and t2m file via the "composite" function)
            - Cardinal "MSImageSet" or "MSImagingExperiment" saved as .RData
    ]]></token>
    <token name="@MZ_TABULAR_INPUT_DESCRIPTION@"><![CDATA[
        - Optional tabular file with m/z values: 

            - One column with numeric m/z values (without empty fields or letters)
            - The file is allowed to have any column names as header (in this case set "Tabular file contains a header line" to yes)
            - m/z features outside the m/z range of the input file are ignored 


                ::
            
                            m/z    
                          100.0    
                          100.01   
                          100.02   
                           ...
                           ...    

    ]]></token>
    <token name="@MZ_2COLS_TABULAR_INPUT_DESCRIPTION@"><![CDATA[
        - Tabular file with m/z values: 

            - One column with numeric m/z values (without empty fields or letters), another column with names for the m/z (m/z column can also be used as name)
            - The file is allowed to have any column names as header (in this case set "Tabular file contains a header line" to yes)
            - m/z features outside the m/z range of the input file are ignored 


                ::
             
                            m/z        name
                          100.0      analyte1
                          100.01     analyte2
                          100.02     analyte3
                           ...
                           ...    

    ]]></token>
    <token name="@SPECTRA_TABULAR_INPUT_DESCRIPTION@"><![CDATA[
        - Optional file with pixel coordinates and annotation:

            - Tabular file: One column with x values, one column with y values and one column with annotations
            - The file is allowed to have any column names as header (in this case set "Tabular file contains a header line" to yes)
            - Pixel with coordinates outside the coordinates of the input file are ignored


                ::
                
                      x_coord     y_coord    annotation
                        1            1        healthy
                        2            1        healthy
                        3            1        disease
                        ...
                        ...
    ]]></token>

    <xml name="reading_msidata">
        <param name="infile" type="data" format="imzml,rdata,analyze75"
               label="MSI data"
                help="Input file as imzML (composite upload), Analyze7.5 (composite upload) or Cardinal MSImageSet saved as RData (regular upload)"/>
        <param name="centroids" type="boolean" label="Centroided input" help="Choose Yes if peak detection has already been done." truevalue="TRUE" falsevalue="FALSE"/>
        <conditional name="processed_cond">
            <param name="processed_file" type="select" label="Processed imzML file" help="Choose no if your input is an Analyze7.5 or continuous imzML file">
                <option value="no_processed" selected="True">no</option>
                <option value="processed">yes</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"/>
                <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>
    </xml>

    <xml name="pdf_filename">
        <param name="filename" type="text" value="" label="Title" help="Will appear in the pdf output, if nothing given it will take the dataset name">
            <sanitizer invalid_char="">
                <valid initial="string.ascii_letters,string.digits">
                    <add value="_"/>
                    <add value=" "/>
                </valid>
            </sanitizer>
        </param>
    </xml>

    <xml name="sanitizer_multiple_digits">
        <sanitizer invalid_char="">
            <valid initial="string.digits">
                <add value=":" />
                <add value="," />
            </valid>
        </sanitizer>
    </xml>

    <xml name="reading_1_column_mz_tabular" token_label="Tabular file with m/z features">
        <param name="mz_tabular" type="data" format="tabular" label="@LABEL@" help="Only numeric m/z values are allowed"/>
        <param name="feature_column" data_ref="mz_tabular" label="Column with features" type="data_column"/>
        <param name="feature_header" type="boolean" label="Tabular file contains a header line" truevalue="TRUE" falsevalue="FALSE"/>
    </xml>

    <xml name="reading_2_column_mz_tabular" token_optional="false">
        <param name="calibrant_file" type="data" optional="@OPTIONAL@" format="tabular"
            label="m/z of interest (e.g. internal Calibrants)" help="one column with m/z values, optional second column with names (m/z values can also be selected as name)"/>
        <param name="mz_column" data_ref="calibrant_file" optional="@OPTIONAL@" label="Column with m/z values" type="data_column"/>
        <param name="name_column" data_ref="calibrant_file" optional="@OPTIONAL@" label="Column with name of m/z values" type="data_column"/>
        <param name="calibrant_header" type="boolean" optional="@OPTIONAL@" label="Tabular file contains a header line" truevalue="TRUE" falsevalue="FALSE"/>
    </xml>

    <xml name="reading_pixel_annotations">
                <param name="annotation_file" type="data" format="tabular" label="Tabular file with pixel coordinates and annotation"
                help="Tabular file with three columns: x values, y values and pixel annotations"/>
                <param name="column_x" data_ref="annotation_file" label="Column with x values" type="data_column"/>
                <param name="column_y" data_ref="annotation_file" label="Column with y values" type="data_column"/>
                <param name="column_names" data_ref="annotation_file" label="Column with pixel annotations" type="data_column"/>
                <param name="tabular_header" type="boolean" label="Tabular file contains a header line" truevalue="TRUE" falsevalue="FALSE"/>
    </xml>

    <xml name="citations">
        <citations>
            <citation type="doi">10.1093/bioinformatics/btv146</citation>
            <citation type="doi">10.1093/gigascience/giz143</citation>
        </citations>
    </xml>
    <xml name="infile_analyze75">
        <param name="infile" value="" ftype="analyze75">
            <composite_data value="Analyze75.hdr"/>
            <composite_data value="Analyze75.img"/>
            <composite_data value="Analyze75.t2m"/>
        </param>
    </xml>
    <xml name="infile_imzml">
        <param name="infile" value="" ftype="imzml">
            <composite_data value="Example_Continuous.imzML"/>
            <composite_data value="Example_Continuous.ibd"/>
        </param>
    </xml>
    <xml name="processed_infile_imzml">
        <param name="infile" value="" ftype="imzml">
            <composite_data value="Example_Processed.imzML"/>
            <composite_data value="Example_Processed.ibd"/>
        </param>
    </xml>
</macros>
