<tool id="mspurity_dimspredictpuritysingle" name="msPurity.dimsPredictPuritySingle" version="@TOOL_VERSION@+galaxy@GALAXY_TOOL_VERSION@">
    <description>Calculate the anticipated precursor ion purity from a DIMS dataset.
    </description>
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="requirements" />

    <stdio>
        <exit_code range="1:" />
    </stdio>
    <command interpreter="Rscript"><![CDATA[
        dimsPredictPuritySingle.R
            #if $mzML_data.format == "mzML_file"
                --mzML_file $mzML_data.source
            #elif $mzML_data.format == "library"
                --mzML_file $__app__.config.user_library_import_dir/$__user_email__/$mzML_data.source
            #end if
            --peaks_file=$peaks_file
            --out_dir=.
            --minOffset=$minoffset
            --maxOffset=$maxoffset
            --ppm=$ppm
            --iwNorm=$iw_norm
            --ilim=$ilim
            #if $sim
                --sim
            #end if
            #if $remove_NAs
                --remove_nas
            #end if
            #if $dimspy_usage.usage == "dimspy"
                --dimspy
                --file_num_dimspy $dimspy_usage.file_num
            #end if
            #if $isotopes.isotopes == "exclude_default":
                --exclude_isotopes
            #elif $isotopes.isotopes == "user"
                --exclude_isotopes
                --isotope_matrix = $isotopes.im
            #end if
    ]]></command>
    <inputs>

        <param type="data" name="peaks_file" format="tsv, tabular"
                help="tsv or tabular file with one column containing the mz values (column header should be either
                      mz)"/>

        <conditional name="mzML_data">
            <param name="format" type="select" label="Choose the source for the dataset" >
                <option value="mzML_file" selected="true">.mzML files to check purity from</option>
                <option value="library">Library directory name (to be used with dimspy workflows) or path to
                                        to an individual .mzML file</option>
            </param>
            <when value="mzML_file">
                <param name="source" type="data" format="mzml" label="Single *.mzML" argument="--mzML_file" >
                    <validator type="empty_field" />
                </param>
            </when>
            <when value="library">
                <param name="source" type="text"
                       size="40" label="Library directory containing *.mzml files
                                                                 or path to an individual *.mzML"
                       argument="--mzML_file">
                    <validator type="empty_field" />
                </param>
            </when>
        </conditional>

        <expand macro="offsets" />
        <expand macro="general_params" />


        <param name="ppm" type="float" label="ppm" value="10"
                    help="ppm tolerance to compare between mz values"/>

        <param name="minoffset" type="float" label="minoffset" value="0.5"
                    help="Offset to the 'left' for the precursor range e.g. if precursor of interest is
                    100.0 then the range would be from 999.5 to 100.0"/>

        <param name="remove_NAs" type="boolean" label="Remove rows where mz value is NA or NaN?" help=""/>

        <param name="sim" type="boolean" label="SIM-Stitch experiment?" help=""/>

        <conditional name="dimspy_usage">
            <param name="usage" type="select" label="dimspy peak matrix text file usage?">
                <option value="no_dimspy" selected="true">dimspy not used to prepare the mz text file</option>
                <option value="dimspy">dimspy used to prepare mz file</option>
            </param>
            <when value="no_dimspy">
            </when>
            <when value="dimspy">
                <param name="file_num" type="integer" label="File number" value="1"
                    help="Choose the file number from the dimspy matrix to use to calculate the precursor ion
                          purity metric (order based on column order). This file will then be looked for in the
                          library folder to calculate the metric.
                         "/>
            </when>
        </conditional>

    </inputs>
    <outputs>
	    <data name="dimsPredictPuritySingle_output"  format="tsv" label="${tool.name} on ${on_string}"
              from_work_dir="dimsPredictPuritySingle_output.tsv" />
    </outputs>
    <tests>
        <test>
            <param name="mzML_data|format" value="mzML_file"   />
            <param name="mzML_data|source" value="dimsPredictPuritySingle_full_scan.mzML" />
            <param name="remove_NAs" value="True" />
            <param name="dimspy_usage|usage" value="dimspy" />
            <param name="peaks_file" value="dimsPredictPuritySingle_input_dimspy_peakmatrix.tsv" />
            <output name="dimsPredictPuritySingle_output" value="dimsPredictPuritySingle_output.tsv" />
        </test>
    </tests>



    <help><![CDATA[

=============================================================
Calculate anticipated precursor ion purity from DI-MS dataset
=============================================================
-----------
Description
-----------

Tool to calculate the anticipated precursor ion purity of selected precursor based on a prior DI-MS dataset. The dataset
can either be in the form of multiple 'full scans' or a SIM-Stitch dataset. See the Bioconductor documentation for more
details, function msPurity::dimsPredictPurity()

--------------
Output example
--------------
Output consists of the mz column (along with any other columns that were in the original mz file). The median and
mean calculated purity. The standard deviation (sdPurity), coefficient of variation (relative standard deviation)
cvPurity, the standard error of the purity (sdePurity) and the median number of peaks within the isolation window
(medianPeakNum)

============= ============= ============= ================ ================ ================ ================
mz            medianPurity  meanPurity    sdPurity         cvPurity	        sdePurity        medianPeakNum
============= ============= ============= ================ ================ ================ ================
50.20428      0.39          0.39          0.0007           0.19             0.0005           3
------------- ------------- ------------- ---------------- ---------------- ---------------- ----------------
56.91206      0.01          0.01          0.0002           4.53             0.0001           12
------------- ------------- ------------- ---------------- ---------------- ---------------- ----------------
62.02906      0.14          0.13          0.0014           22.63            0.0009           7
------------- ------------- ------------- ---------------- ---------------- ---------------- ----------------
75.07431      0.93          0.94          0.019            37.87            0.0134           3
============= ============= ============= ================ ================ ================ ================

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