<?xml version="1.0"?>
<tool id="idpqonvertEmbedder" name="idpEmbedder" version="@VERSION@.1">
    <description>Embed human/mouse gene metadata into IDPicker files</description>
    <macros>
        <import>macros.xml</import>
        <xml name="IsobaricQuantitationOptions" token_tolerance="0.015">
            <param argument="-ReporterIonMzTolerance" type="float" min="0.000001" value="@TOLERANCE@" label="Tolerance window to search for iTRAQ/TMT reporter ions" help="The most intense ion in the tolerance window is used as the reporter ion intensity." />
            <param name="ReporterIonMzTolerance_units" type="select" label="Tolerance units">
                <option value="ppm">Parts per million</option>
                <option value="daltons" selected="true">Daltons</option>
            </param>
            <param argument="-NormalizeReporterIons" type="boolean" value="false" label="Normalize reporter ion channels to match 1:1 ratio?" help="This option will adjust the intensity of each channel so that the total intensity of each channel across an entire source file is the same." />
        </xml>
    </macros>
    <expand macro="requirements" />
    <stdio>
        <exit_code range="1:" level="fatal" description="Job Failed" />
        <regex match="\nError:.*\n" source="both" level="fatal" />
    </stdio>
    <command>
<![CDATA[
        #set $input_name = $input.display_name
        cp '$input' output &&
        ln -s output output.idpDB &&
        ln -s \$(dirname \$(which idpQonvert))/gene2protein.db3 gene2protein.db3 &&

        #if $use_raw_data_condition.use_raw_data
            #for $i in $use_raw_data_condition.input_raw
                ln -s '${i.file_name}' '${i.display_name}' &&
            #end for
        #end if

        idpQonvert
            -EmbedOnly true
            -EmbedGeneMetadata $EmbedGeneMetadata
            #if $use_raw_data_condition.use_raw_data
                -EmbedSpectrumScanTimes $use_raw_data_condition.EmbedSpectrumScanTimes
                -EmbedSpectrumSources $use_raw_data_condition.EmbedSpectrumSources

                #set $qm = $use_raw_data_condition.quantitation_method_condition.QuantitationMethod
                -QuantitationMethod $qm
                #if $qm != "None" and $qm != "LabelFree"
                    -ReporterIonMzTolerance '$use_raw_data_condition.quantitation_method_condition.ReporterIonMzTolerance $use_raw_data_condition.quantitation_method_condition.ReporterIonMzTolerance_units'
                    -NormalizeReporterIons $use_raw_data_condition.quantitation_method_condition.NormalizeReporterIons
                #end if
            #end if
            output.idpDB
]]>
    </command>
    <inputs>
        <param name="input" type="data" format="idpdb" label="Input idpDB file" />
        <param argument="-EmbedGeneMetadata" type="boolean" value="true" label="Embed Gene Metadata?" help="Allows gene-centric analysis in IDPicker. The gene metadata embedding only works for human/mouse proteins from a RefSeq database. For best results, run idpQonvert with RefSeq no matter what database was used for the search: idpQonvert always remap the peptides anyway. Non-human/mouse proteins and any unmappable human/mouse proteins will get gene ids like &quot;Unmapped_&lt;protein accession&gt;&quot;. This option, run by itself, is very fast." />
        <conditional name="use_raw_data_condition">
            <param name="use_raw_data" type="boolean" label="Do you have the raw data (spectra)?" help="More embed options are available if the raw spectra are available." />
            <when value="false"></when>
            <when value="true">
                <param name="input_raw" type="data" format="mzml,mzxml,mgf,ms2,mz5" label="Input raw MS files" multiple="true" />
                <param argument="-EmbedSpectrumSources" type="boolean" value="false" label="Embed Spectrum Sources?" help="Allows visualizing peptide-spectrum-matches without downloading the raw data. Embedding spectra will greatly increase the size of the database, even though only spectra that passed the import FDR filter will be included. This option can take a LONG time to run." />
                <param argument="-EmbedSpectrumScanTimes" type="boolean" value="false" label="Embed Spectrum Scan Times?" help="If the pepXML/mzIdentML file did not contain scan time (retention time) information, this will look up that information in the raw data. This option, run by itself, will take some time (it has to open every raw file)." />
                <conditional name="quantitation_method_condition">
                    <param argument="-QuantitationMethod" type="select" label="Quantitation Method" help="Enables quantitation methods other than spectral counting. For isobaric isotope labelling quantitation (iTRAQ/TMT) or intensity-based label-free quantitation (XIC), select the appropriate QuantitationMethod here. You have to keep iTRAQ/TMT and label-free data separate since you can only specify a single QuantitationMethod for the entire assembly. Like embedding spectrum sources, this option can take a LONG time to run, although not quite as long.">
                        <option value="None" selected="true">None</option>
                        <option value="LabelFree">Label-free (XIC)</option>
                        <option value="ITRAQ4plex">iTRAQ 4-plex</option>
                        <option value="ITRAQ8plex">iTRAQ 8-plex</option>
                        <option value="TMT2plex">TMT 2-plex</option>
                        <option value="TMT6plex">TMT 6-plex</option>
                        <option value="TMT10plex">TMT 10-plex</option>
                        <option value="TMT11plex">TMT 11-plex</option>
                        <option value="TMTpro16plex">TMTpro 16-plex</option>
                    </param>
                    <when value="ITRAQ4plex"><expand macro="IsobaricQuantitationOptions" /></when>
                    <when value="ITRAQ8plex"><expand macro="IsobaricQuantitationOptions" /></when>
                    <when value="TMT2plex"><expand macro="IsobaricQuantitationOptions" /></when>
                    <when value="TMT6plex"><expand macro="IsobaricQuantitationOptions" /></when>
                    <when value="TMT10plex"><expand macro="IsobaricQuantitationOptions" tolerance="0.003" /></when>
                    <when value="TMT11plex"><expand macro="IsobaricQuantitationOptions" tolerance="0.003" /></when>
                    <when value="TMTpro16plex"><expand macro="IsobaricQuantitationOptions" tolerance="0.003" /></when>

                    <when value="None"></when>
                    <when value="LabelFree"></when>
                </conditional>
            </when>
        </conditional>
    </inputs>
    <outputs>
        <data format="idpdb" name="output" from_work_dir="output" />
    </outputs>
    <tests>
        <test>
            <param name="input" value="201208-378803.idpDB" />
            <param name="EmbedGeneMetadata" value="false" />
            <output name="output" file="201208-378803.idpDB" compare="sim_size" delta="500000" />
        </test>
        <test>
            <param name="input" value="201208-378803.idpDB" />
            <param name="EmbedGeneMetadata" value="true" />
            <output name="output" file="201208-378803-embeddedGenes.idpDB" compare="sim_size" delta="500000" />
        </test>
        <test>
            <param name="input" value="201208-378803.idpDB" />
            <param name="EmbedGeneMetadata" value="true" />
            <param name="use_raw_data" value="true" /> 
            <param name="input_raw" value="input/201208-378803.mzML" />
            <param name="EmbedSpectrumSources" value="false" />
            <param name="EmbedSpectrumScanTimes" value="true" />
            <param name="QuantitationMethod" value="None" />
            <output name="output" file="201208-378803-embeddedGenesAndScanTimes.idpDB" compare="sim_size" delta="500000" />
        </test>
        <test>
            <param name="input" value="201208-378803.idpDB" />
            <param name="EmbedGeneMetadata" value="true" />
            <param name="use_raw_data" value="true" /> 
            <param name="input_raw" value="input/201208-378803.mzML" />
            <param name="EmbedSpectrumSources" value="true" />
            <param name="EmbedSpectrumScanTimes" value="false" />
            <param name="QuantitationMethod" value="None" />
            <output name="output" file="201208-378803-embeddedGenesAndSpectra.idpDB" compare="sim_size" delta="500000" />
        </test>
        <test>
            <param name="input" value="201208-378803.idpDB" />
            <param name="EmbedGeneMetadata" value="true" />
            <param name="use_raw_data" value="true" /> 
            <param name="input_raw" value="input/201208-378803.mzML" />
            <param name="EmbedSpectrumSources" value="false" />
            <param name="EmbedSpectrumScanTimes" value="true" />
            <param name="QuantitationMethod" value="ITRAQ4plex" />
            <param name="ReporterIonMzTolerance" value="10" />
            <param name="ReporterIonMzTolerance_units" value="ppm" />
            <param name="NormalizeReporterIons" value="true" />
            <output name="output" file="201208-378803-embeddedGenesAndQuantitation.idpDB" compare="sim_size" delta="500000" />
        </test>
    </tests>
    <help>
<![CDATA[
**What it does**

Embeds optional data and metadata into the IDPicker 3 idpDB files. This should be run after all idpDB files have been merged together (e.g. after idpAssemble).
]]>
    </help>
    <citations>
        <citation type="doi">10.1021/pr900360j</citation>
        <citation type="bibtex">@misc{toolsGalaxyP, author = {Chilton, J, Chambers MC, et al.}, title = {Galaxy Proteomics Tools}, publisher = {GitHub}, journal = {GitHub repository},
                                      year = {2015}, url = {https://github.com/galaxyproteomics/tools-galaxyp}}</citation> <!-- TODO: fix substitution of commit ", commit = {$sha1$}" -->
    </citations>
</tool>
