<tool id="frag4feature" name="frag4feature" version="0.0.20">
    <description>
        Assign fragmentation spectra to XCMS features using msPurity
    </description>

    <macros>
        <import>macros.xml</import>
    </macros>

    <expand macro="requirements">
        <!-- would ideally have xcms 3.0.0 but not available in bioconda at the point of tool creation-->
       <!-- <requirement type="package" version="1.46.0" >bioconductor-xcms</requirement>-->
    </expand>

    
 <stdio>
        <exit_code range="1:" />
    </stdio>
    <command interpreter="Rscript"><![CDATA[
        frag4feature.R
            --out_dir=.
            --xset=$xset
            --pa=$pa
            --cores=\${GALAXY_SLOTS:-4}
            #if $file_load_conditional.file_load_select=="yes"
                --mzML_files='
                #for $i in $file_load_conditional.input
                    $i,
                #end for
                '
                --galaxy_names='
                #for $i in $file_load_conditional.input
                    $i.name,
                #end for
                '
            #end if
            #if $use_group
                --use_group
            #end if

            --ppm=$ppm_f4f
            --plim=$plim_f4f
            #if $mostIntense_f4f
                --mostIntense
            #end if
            #if $convert2RawRT_f4f
                --convert2RawRT
            #end if
 	        #if $createDB
                --createDB
            #end if

            #if $grp_peaklist_opt.grp_peaklist_opt=="yes"
                --grp_peaklist=$grp_peaklist
            #end if

    ]]></command>
    <inputs>

        <param type="data" name="xset" label="xcmsSet object"
               help="grouped xcmsSet object saved as 'xset' in an RData file" format="rdata.xcms.raw,rdata.xcms.group,rdata.xcms.retcor,rdata.xcms.fillpeaks,rdata.camera.quick,rdata.camera.positive,rdata.camera.negative,rdata"/>
        <param type="data" name="pa" label="purityA object" format="rdata"
               help="purityA object saved as 'pa' in a RData file (output from assess_purity_msms)"/>

        <param name="mostIntense_f4f" type="boolean" checked="true" label="For matching fragmentation to a feature,
         use most intense peak within isolation window for precursor?"/>

        <param name="use_group" type="boolean" checked="false" label="For matching fragmentation to a feature,
         use the grouped feature range" help="If the MS1 and MS2 are in different files this is option has to be set to true"
         />

        <param name="convert2RawRT_f4f" type="boolean" checked="false" label="Was retention time correction used?"
               help="If retention time correction has been used in XCMS set this to yes"/>

        <param name="ppm_f4f" type="float" label="ppm" value="10"
                    help="ppm tolerance between precursor mz and feature mz"/>

        <param name="plim_f4f" type="float" label="plim" value="0" max="1" min="0"
                    help="min purity of precursor to be included"/>

        <param name="createDB" type="boolean" checked="true" label="Create SQLite database of spcetra?"/>

        <expand macro="grp_peaklist" />

        <expand macro="fileload" />

    </inputs>
    <outputs>
	    <data name="frag4feature_grouped_msms" format="tsv" label="${tool.name} on ${on_string}: tsv"
              from_work_dir="frag4feature.tsv" />
        <data name="frag4feature_rdata" format="rdata" label="${tool.name} on ${on_string}: RData"
              from_work_dir="frag4feature.RData" />
        <data name="frag4feature_sqlite" format="sqlite" label="${tool.name} on ${on_string}: Sqlite"
              from_work_dir="alldata.sqlite" />
    </outputs>
    <tests>
        <test>
            <conditional name="file_load_conditional">
                <param name="file_load_select" value="yes"/>
                <param name="input" >
                    <collection type="list">
                        <element name="LCMS_1.mzML" value="LCMS_1.mzML"/>
                        <element name="LCMS_2.mzML" value="LCMS_2.mzML"/>
                        <element name="LCMSMS_1.mzML" value="LCMSMS_1.mzML"/>
                        <element name="LCMSMS_2.mzML" value="LCMSMS_2.mzML"/>
                    </collection>
                </param>
            </conditional>
            <param name="xset" value="LCMS_1_LCMS_2_LCMSMS_1_LCMSMS_2.xset.group.rdata"/>
            <param name="pa" value="LCMS_1_LCMS_2_LCMSMS_1_LCMSMS_2.assess_purity.rdata"/>
            <output name="frag4feature_grouped_msms" value="frag4feature_output.tsv" >
                
            </output>
        </test>
    </tests>
    
    <help><![CDATA[
=============================================================
Link fragmentation spectra to XCMS features
=============================================================
-----------
Description
-----------

Tool to link XCMS features to fragmentation spectra and create an SQLite database of all the data and connections. 

Please note that the xcmsSet object needs to have been grouped.

The data inputs are:

* A purityA object (generated from assess_purity_msms) saved in an rdata file.
* A xcmsSet grouped object (generated from xcms_group) saved in an rdata file
* [optional] a dataset collection of the mzML files to resubmit

See Bioconductor documentation for more details, function msPurity::frag4feature()

-----------
Outputs
-----------
* frag4feature_rdata: An updated purityA object saved as rdata file with fragmentation-feature links added
* frag4feature_grouped_msms: A flat file of all the XCMS peaks for each grouped feature and the corresponding fragmentation scans
* frag4feature_sqlite: An SQLite database of the data (including fragmentation scans)



    ]]></help>

<expand macro="citations" />

</tool>
