<tool id="spectral_matching" name="spectral_matching" version="0.0.16">
    <macros>
        <import>macros.xml</import>
    </macros>

    <expand macro="requirements">
    </expand>

    <description>
        Perform spectral matching to spectral libraries using dot product cosine on a MS/MS dataset
    </description>
    <stdio>
        <exit_code range="1:" />
    </stdio>
    <command interpreter="Rscript"><![CDATA[
        spectral_matching.R
            --out_dir=.
            --target_db_pth=$target_db
            --library_db_pth=$library_db
            --ra_thres_l=$advanced.ra_thres_l
            --ra_thres_t=$advanced.ra_thres_t
            --cores=\${GALAXY_SLOTS:-4}
            --pol=$pol
            --score_thres=$score_thres
            --ppm_tol_prod=$ppm_tol_prod
            --ppm_tol_prec=$ppm_tol_prec
            --instrument_types=$instrument_types
            --library_sources=$library_sources
            #if $advanced.topncond.topnbool:
                --topn=$topn
            #end if
            #if $advanced.scan_ids:
              --scan_ids='
                #for $i, $j in enumerate($advanced.scan_ids)
                    $j.scan_ids,
                #end for
                '
            #end if
            #if $create_new_database:
              --create_new_database
            #end if



    ]]></command>
    <inputs>

        <param type="data" name="target_db" label="SQLite DB of target spectra" format="sqlite"
               help="SQLite DB of target spectra. (e.g. generated from frag4feature tool)"/>

        <param type="data" name="library_db" label="SQLite DB of library spectra" format="sqlite"
               help="SQLite database of library (reference) spectra,
               As a temporary quick solution. The library can be used from this dropbox link
               https://www.dropbox.com/s/csxj6pairsgd8fe/library_spectra.db?dl=1
               A more official location can be found here: https://bioconductor.org/packages/release/data/experiment/src/contrib/msPurityData_1.6.0.tar.gz
                "/>


        <param name="pol" type="select" label="polarity" >
                <option value="positive" selected="true">Positive</option>
                <option value="negative"  >negative</option>
        </param>

        <param name="ppm_tol_prod" type="float" value="10"
                    help="Parts per million tolerance to match product mz values"/>

        <param name="ppm_tol_prec" type="float" value="5"
                    help="Parts per million tolerance to match precursor mz values"/>

        <param name="score_thres" type="float" min="0" max="1" value="0.6"
                    help="Dot product cosine score threshold"/>

        <param name="instrument_types" type="select"  multiple="true" >
            <option value="APCI-ITFT">APCI-ITFT</option>
            <option value="CE-ESI-TOF" selected="true">CE-ESI-TOF</option>
            <option value="CI-B">CI-B</option>
            <option value="EI-B">EI-B</option>
            <option value="EI-EBEB">EI-EBEB</option>
            <option value="ESI-ITFT" selected="true">ESI-ITFT</option>
            <option value="ESI-ITTOF" selected="true">ESI-ITTOF</option>
            <option value="ESI-QTOF" selected="true">ESI-QTOF</option>
            <option value="FAB-B">FAB-B</option>
            <option value="FAB-BE-MS">FAB-BE-MS</option>
            <option value="FAB-EB">FAB-EB</option>
            <option value="FAB-EBEB">FAB-EBEB</option>
            <option value="FI-B">FI-B</option>
            <option value="GC-EI-Q">GC-EI-Q</option>
            <option value="GC-EI-QQ">GC-EI-QQ</option>
            <option value="GC-EI-TOF">GC-EI-TOF</option>
            <option value="LC-APCI-QTOF">LC-APCI-QTOF</option>
            <option value="LC-APPI-QQ">LC-APPI-QQ</option>
            <option value="LC-ESI-IT" selected="true">LC-ESI-IT</option>
            <option value="LC-ESI-ITFT" selected="true">LC-ESI-ITFT</option>
            <option value="LC-ESI-ITTOF" selected="true">LC-ESI-ITTOF</option>
            <option value="LC-ESI-Q">LC-ESI-Q</option>
            <option value="LC-ESI-QFT" selected="true" >LC-ESI-QFT</option>
            <option value="LC-ESI-QIT" selected="true">LC-ESI-QIT</option>
            <option value="LC-ESI-QQ" selected="true">LC-ESI-QQ</option>
            <option value="LC-ESI-QTOF" selected="true">LC-ESI-QTOF</option>
            <option value="LC-ESI-TOF" selected="true">LC-ESI-TOF</option>
            <option value="MALDI-QIT">MALDI-QIT</option>
            <option value="MALDI-TOF">MALDI-TOF</option>
            <option value="ALDI-TOFTOF">ALDI-TOFTOF</option>
        </param>

        <param name="library_sources" type="select"  multiple="true" >
            <option value="lipidblast">LipidBlast</option>
            <option value="massbank" selected="true">MassBank</option>
            <option value="GNPS">GNPS</option>
        </param>
        <param name="create_new_database" type="boolean" checked="true" label="Create a new database for the results?"
               help="A copy will be made of the input SQLite target database and the results will be added to this copy.
                     When False, the input SQLite database will be updated  with the matching results. Use False
                     if you want to reduce storage space being used."/>

        <section name="advanced" title="advanced" expanded="False">
            <conditional name="topncond">
                <param name="topnbool" type="boolean" label="Only use the top n spectral matching hits?"/>
                <when value="false">
                </when>
                <when value="true">
                     <param name="topn" type="integer" value="10" help="Only use top n matches"/>
                </when>
            </conditional>

            <param name="ra_thres_l" type="float" value="0"
               help="Relative abundance threshold for library spectra"/>

            <param name="ra_thres_t" type="float" value="2"
               help="Relative abundance threshold for target spectra (e.g. spectral data from users MS files)"/>

            <repeat name="scan_ids" title="Specific scan ids">
                <param name="scan_ids" type="integer" value=""/>
            </repeat>


        </section>

    </inputs>
    <outputs>



	    <data name="sqlite_results" format="sqlite" label="${tool.name} on ${on_string}: SQLite results"
              from_work_dir="results.sqlite" visible="true">
            <filter>create_new_database is True</filter>
        </data>
        <data name="scan_hits" format="tsv" label="${tool.name} on ${on_string}: scan hits"
              from_work_dir="scan_hits.tsv" visible="true"/>
        <data name="xcms_hits" format="tsv" label="${tool.name} on ${on_string}: xcms hits"
              from_work_dir="xcms_hits.tsv" visible="true"/>
    </outputs>
    <tests>
        <!--<test>-->

            <!--<param name="target_db" value="alldata.sqlite" />-->
            <!--<param name="library_db" value="https://www.dropbox.com/s/csxj6pairsgd8fe/library_spectra.db?dl=0"/>-->
            <!--<output name="xcms_hits" value="spectra_matching_xcms_hits.tsv" />-->
            <!--<output name="scan_hits" value="spectra_matching_scan_hits.tsv" />-->

        <!--</test>-->
    </tests>
    <expand macro="citations" />
    <help><![CDATA[
=============================================================
Spectral matching
=============================================================
-----------
Description
-----------

Tool to perform spectral matching of MS/MS scans against library spectra

The data inputs are:

* library_db: An SQLite database of library ms/ms spectra (e.g. massbank, lipidsearch)
* target_db: An SQLite database of the target ms/ms spectra (e.g. the data collected for an experiment)

The library spectra can be downloaded from:

* https://www.dropbox.com/s/csxj6pairsgd8fe/library_spectra.db?dl=1
* (more official location) https://bioconductor.org/packages/release/data/experiment/src/contrib/msPurityData_1.6.0.tar.gz

The target_db can be generated from the frag4feature galaxy tool but will accept any SQLite database in the same format.

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

-----------
Outputs
-----------
If the 'create_new_db', a copy of the target_db will be created that will now including spectral matching results
spectra. Otherwise the original SQLite database will be updpated

* scan_hits: All spectral matching hits for every scan
* xcms_hits: All spectral matching hits that can be linked to an XCMS grouped feature
* sqlite_results (if create_new_db==True): SQLite database of all data including spectral matching results


    ]]></help>
</tool>
