<tool id="mspurity_purityx" name="msPurity.purityX" version="@TOOL_VERSION@+galaxy@GALAXY_TOOL_VERSION@">
        <description>Calculate the anticipated precursor ion purity from a LC-MS XCMS dataset.
    </description>
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="requirements" />
    <command detect_errors="exit_code"><![CDATA[
        Rscript '$__tool_directory__/purityX.R'
            --xset_path='$xset'
            --out_dir='.'
            --minOffset=$minoffset
            --maxOffset=$maxoffset
            --singleFile=$singleFile
            --purityType=$purityType
            --cores=\${GALAXY_SLOTS:-4}
            #if $xgroups.xgroups == "choose"
                --xgroups='$xgroups.xgroups_value'
            #end if
            --camera_xcms=$camera_xcms
            #if $camera_xcms == "camera"
                --rdata_name=xa
            #else
                --rdata_name=xset
            #end if
            --iwNorm=$iw_norm
            --ilim=$ilim
            #if $isotopes.isotopes == "exclude_default"
                --exclude_isotopes
            #elif $isotopes.isotopes == "user"
                --exclude_isotopes
                --isotope_matrix='$isotopes.im'
            #end if
            #if $file_load_conditional.file_load_select == 'yes'
                --galaxy_files='
                #for $i in $file_load_conditional.input
                    $i,
                #end for
                '
            #end if
            #if $file_load_conditional.file_load_select == 'yes'
                --files='
                #for $i in $file_load_conditional.input
                    $i.name,
                #end for
                '
            #end if

            #if $choose_class_cond.choose_class== 'choose'
                --choose_class=$choose_class_cond.class
            #end if
            #if $ignore_files_cond.ignore_files_choice== 'choose'
                --ignore_files=$ignore_files_cond.ignore_files
            #end if

            #if $raw_rt_columns
                --rtraw_columns
            #end if


    ]]></command>
    <inputs>

        <param argument="--xset" type="data" format="rdata"
                help="xcmsSet object saved as an RData file"/>
        <expand macro="offsets" />
        <expand macro="general_params" />
        <param argument="--singleFile" type="integer" label="singleFile" value="0"
                    help="If just a single file for purity is to be calculated (rather than the grouped XCMS peaks).
                    Uses the index of the files in xcmsSet object. If zero this is ignored"/>

        <conditional name="choose_class_cond">
            <param name="choose_class" type="select" label="Choose class of XCMS set object to perform calculation on?" >
                <option value="choose">Choose class</option>
                <option value="all" selected="true">calculate average across all classes</option>
            </param>
            <when value="all">
            </when>
            <when value="choose">
                <param argument="--class" type="text" label="Choose class"
                    help="Comma separated string of classes (found in the phenoData slot of xcmsSet object
                    i.e. xset@phenoData). An average will across all the choosen classes." />
            </when>
        </conditional>

        <conditional name="ignore_files_cond">
            <param name="ignore_files_choice" type="select" label="Ignore files?" >
                <option value="choose">Choose files to ignore from calculation</option>
                <option value="none" selected="true" >Include all files of selected classes in calculation</option>
            </param>
            <when value="none">
            </when>
            <when value="choose">
               <param argument="--ignore_files" type="text" label="ignore files"
               help="Comma separated string of filenames to ignore (do not include the directory) eg. blank1.mzML,blank2.mzML,sample4.mzML"  />
            </when>
        </conditional>


        <conditional name="xgroups">
            <param name="xgroups" type="select" label="xgroups" >
                <option value="choose">Choose XCMS grouped features to calculate precursor ion purity</option>
                <option value="all" selected="true">Calculate precursor ion purity for all XCMS grouped features</option>
            </param>
            <when value="choose">
                <param name="xgroups_value" type="text" label="xgroup ids"
                    help="Comma separated string of xcms groups to perform prediction on eg. 1,2,3,4"  />
            </when>
            <when value="all"/>
        </conditional>


        <param argument="--purityType" type="select" label="Method of averaging purity"
               help="Area and average used for the purity predictions. FWHM='Full width half maximum', FW='Full width'">
                <option value="purityFWHMmedian" selected="true" >FWHM median</option>
                <option value="purityFWHMmean" >FWHM mean</option>
                <option value="purityFWmedian" >FW median</option>
                <option value="purityFWmean" >FW mean</option>
        </param>

        <expand macro="camera_xcms" />

        <param argument="--raw_rt_columns" type="boolean" label="Has the raw retention time been tracked within the peaks?"
               help="Only applicable when using retention time correction with obiwarp. When obiwarp is used
                     the retention time correction tracking is slightly different to the other correction methods. To ensure
                     correct tracking with obiwarp an additional tool (track_rt_raw) should be performed prior to
                     any retention time correction."/>

        <expand macro="fileload" />

    </inputs>
    <outputs>
	    <data name="purityX_tsv" format="tsv" label="${tool.name} on ${on_string}: tsv"
              from_work_dir="purityX.tsv" />
        <data name="purityX_rdata" format="rdata" label="${tool.name} on ${on_string}: RData"
              from_work_dir="purityX.rdata" />
    </outputs>
    <tests>
        <test>
            <conditional name="file_load_conditional">
                <param name="file_load_select" value="yes"/>
                <param name="input" >
                    <collection type="list">
                        <element name="LCMSMS_2.mzML" value="LCMSMS_2.mzML"/>
                        <element name="LCMSMS_1.mzML" value="LCMSMS_1.mzML"/>
                        <element name="LCMS_2.mzML" value="LCMS_2.mzML"/>
                        <element name="LCMS_1.mzML" value="LCMS_1.mzML"/>
                    </collection>
                </param>
            </conditional>
            <conditional name="xgroups">
                <param name="xgroups" value="choose"/>
                <param name="xgroups_value" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"/>
            </conditional>
            <param name="iw_norm" value="QE5" />
            <param name="xset" value="xset_group_LCMS_1_LCMS_2_LCMSMS_1_LCMSMS_2.RData"/>
            <output name="purityX_tsv" value="purityX_output.tsv" />
            <output name="purityX_rdata" value="purityX_output.Rdata" ftype="rdata" compare="sim_size"/>
        </test>
    </tests>
    <help><![CDATA[

===================================================================
Calculate anticipated precursor ion purity from LC-MS XCMS dataset
===================================================================
-----------
Description
-----------

Tool to calculate the anticipated precursor ion purity of selected precursor based on a prior LC-MS dataset. Based on
XCMS determined features

--------------
Output example
--------------
Output consists of the mz and intensity of the XCMS features (id based on the XCMS group id) and various
metrics regarding the anticipated precursor ion purity.

============= ============= ============= ================ ================ ================ ================ ================ ================
grpid         mean          median        sd               stde	            RSD              pknm             i                mz
============= ============= ============= ================ ================ ================ ================ ================ ================
1             1.00          1.00          0.0000           0.0000           0.0000           1                5404920          100.0758
------------- ------------- ------------- ---------------- ---------------- ---------------- ---------------- ---------------- ----------------
2             0.59          0.57          0.3016           0.1508           50.570           3                8793845          101.0084
------------- ------------- ------------- ---------------- ---------------- ---------------- ---------------- ---------------- ----------------
3             0.08          0.07          0.0784           0.0164           29.643           4                1526502          100.9781
------------- ------------- ------------- ---------------- ---------------- ---------------- ---------------- ---------------- ----------------
4             1.00          1.00          0.0000           0.0000           0.0000           1                42513139         102.0914
============= ============= ============= ================ ================ ================ ================ ================ ================



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