<tool id="ncbi_fcs_gx" name="NCBI FCS GX" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@">
    <description>detects contamination from foreign organisms in genome sequences</description>
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="biotools"/>
    <expand macro="requirements"/>
    <command detect_errors="exit_code"><![CDATA[
#if $mode.mode_selector == "screen"
    #set manifest_pathname = $mode.config_tag.fields.local_manifest
    #if $mode.config_tag.fields.use_source_manifest == "1"
        #set manifest_pathname = $mode.config_tag.fields.source_manifest
    #end if
    GX_DB_DIR="\${TMPDIR}/$mode.config_tag" &&
    ## skip checksumming the database if the db_state_file exists
    if [ ! -e "\${GX_DB_DIR}/sync-files-completed.txt" ]; then
        ## copy data to local storage
        mkdir -p "\${GX_DB_DIR}" &&
        sync_files.py get --mft '$manifest_pathname' --dir "\${GX_DB_DIR}" > sync_files.log 2>&1 ||
        { cat sync_files.log >&2 ; false ; } &&
        touch "\${GX_DB_DIR}/sync-files-completed.txt";
    fi &&
    ## run gx
    GX_NUM_CORES=\${GALAXY_SLOTS:-2} run_gx.py
    #if $mode.config_tag.fields.phone_home == "1"
        --phone-home-label '$mode.config_tag.fields.phone_home_label'
    #end if
        --fasta '$mode.fasta'
    #if $mode.id.id_selector == "gx_div"
        --tax-id '1'
        --div '$mode.id.div'
    #else
        --tax-id '$mode.id.tax_id'
    #end if
    #if $mode.species != ""
        --species '$mode.species'
    #end if
        --split-fasta '$mode.screen_adv.split_fasta'
    #if $mode.screen_adv.div
        --div '$mode.screen_adv.div'
    #end if
    #if $mode.screen_adv.ignore_same_kingdom
        --ignore-same-kingdom
    #end if
        --gx-db "\${GX_DB_DIR}"
        --out-basename output
        --action-report true
        --generate-logfile false
#elif $mode.mode_selector == "clean"
    ## run gx
    GX_NUM_CORES=\${GALAXY_SLOTS:-2} gx clean-genome
        --input '$mode.input'
        --action-report '$mode.action_report'
        --contam-fasta-out 'contam.fa'
        --min-seq-len '$mode.min_seq_len'
        --output 'clean.fa'
#end if
    ]]></command>
    <environment_variables>
        <environment_variable name="GX_ALIGN_EXCLUDE_TAXA">$getVar('mode.screen_adv.gx_align_exclude_taxa', '')</environment_variable>
        <environment_variable name="GX_EXTRA_CONTAM_DIVS"><![CDATA[#echo ','.join($getVar('mode.screen_adv.gx_extra_contam_divs', []))]]></environment_variable>
    </environment_variables>
    <inputs>
        <conditional name="mode">
            <param name="mode_selector" type="select" label="Choose the mode">
                <option value="screen" selected="true">Screen genome</option>
                <option value="clean">Clean genome</option>
            </param>
            <when value="screen">
                <!-- value, description, source_manifest, use_source_manifest, phone_home, phone_home_label, local_manifest -->
                <param name="config_tag" type="select" label="Database">
                    <options from_data_table="ncbi_fcs_gx_databases_ext">
                        <filter type="sort_by" column="1"/>
                    </options>
                    <validator message="No database is available" type="no_options"/>
                </param>
                <param argument="--fasta" type="data" format="fasta" label="Input file (Fasta file)" help="To detect contamination from foreign organisms, a genome assembly in a fasta file."/>
                <conditional name="id">
                    <param name="id_selector" type="select" label="Taxonomy entry">
                        <option value="gx_div" selected="true">GX Division</option>
                        <option value="ncbi_tax">NCBI Taxonomic identifier</option>
                    </param>
                    <when value="gx_div">
                        <!-- value, tag, description -->
                        <param argument="--div" type="select" optional="true">
                            <options from_data_table="ncbi_fcs_gx_divisions">
                                <filter type="param_value" ref="config_tag" column="1" />
                                <filter type="sort_by" column="2" />
                            </options>
                            <validator message="No GX Divisions are available" type="no_options"/>
                        </param>
                    </when>
                    <when value="ncbi_tax">
                        <!-- https://www.ncbi.nlm.nih.gov/taxonomy -->
                        <param argument="--tax-id" type="text" label="Taxonomic identifier" help="The appropriate tax-id for your genome assembly.  The appropriate tax-id for an organism can be retrieved from the NCBI Taxonomy website."/>
                    </when>
                </conditional>
                <param argument="--species" type="text" optional="true" label="Species binomial name"/>
                <section name="screen_adv" title="Advanced options">
                    <!-- comma separated list of taxa to ignore in GX_ALIGN_EXCLUDE_TAXA environment variable -->
                    <param name="gx_align_exclude_taxa" type="text" value="" optional="true" label="Taxonomic identifier(s) to exclude" help="Multiple tax-ids may be provided as a comma-separated list.">
                        <validator type="regex" message="comma separated integers">^\s*\d+\s*(,\s*\d+\s*)*$</validator>
                        <sanitizer invalid_char="">
                            <valid initial="string.digits">
                                <add value=","/>
                            </valid>
                        </sanitizer>
                    </param>
                    <!-- value, tag, description -->
                    <param name="gx_extra_contam_divs" type="select" multiple="true" optional="true" label="Additional contaminants to identify" help="Multiple gx-divisions may be selected.">
                        <options from_data_table="ncbi_fcs_gx_divisions">
                            <filter type="param_value" ref="config_tag" column="1" />
                            <filter type="sort_by" column="2" />
                        </options>
                        <validator message="No GX Divisions are available" type="no_options"/>
                    </param>
                    <param argument="--split-fasta" type="boolean" checked="true" optional="true" label="Split fasta sequences on N-runs of length at least 10"/>
                    <param argument="--div" type="text" value="" optional="true" label="BLAST-div of the tax-id" help="from 'NCBI BLAST name' on taxon Info page"/>
                    <param argument="--ignore-same-kingdom" type="boolean" checked="false" optional="true" label="Ignore same-kingdom contamination"/>
                </section>
            </when>
            <when value="clean">
                <param argument="--input" type="data" format="fasta" label="Input file (Fasta file)" help="To detect contamination from foreign organisms, a genome assembly in a fasta file."/>
                <param argument="--action-report" type="data" format="tabular" label="Select Action report"/>
                <param argument="--min-seq-len" type="integer" value="200" label="Minimumm sequence length to keep"/>
            </when>
        </conditional>
    </inputs>
    <outputs>
        <!-- mode == screen -->
        <data name="taxonomy_report" format="tabular" from_work_dir="output.taxonomy.rpt" label="${tool.name} on ${on_string}: Taxonomy report">
            <filter>mode['mode_selector'] == 'screen'</filter>
        </data>
        <data name="action_report" format="tabular" from_work_dir="output.fcs_gx_report.txt" label="${tool.name} on ${on_string}: Action report">
            <filter>mode['mode_selector'] == 'screen'</filter>
        </data>
        <!-- mode == clean -->
        <data name="contam_fasta" format="fasta" from_work_dir="contam.fa" label="${tool.name} on ${on_string}: Fasta for EXCLUDE entries">
            <filter>mode['mode_selector'] == 'clean'</filter>
        </data>
        <data name="clean_fasta" format="fasta" from_work_dir="clean.fa" metadata_source="mode.input" label="${tool.name} on ${on_string}: Cleaned Fasta">
            <filter>mode['mode_selector'] == 'clean'</filter>
        </data>
    </outputs>
    <tests>
        <test expect_num_outputs="2">
            <conditional name="mode">
                <param name="mode_selector" value="screen"/>
                <param name="config_tag" value="test" />
                <param name="fasta" value="fcsgx_test.fa.gz" ftype="fasta"/>
                <conditional name="id">
                    <param name="id_selector" value="ncbi_tax"/>
                    <param name="tax_id" value="6973"/>
                </conditional>
                <section name="screen_adv"/>
            </conditional>
            <output name="taxonomy_report" file="output.taxonomy.rpt" compare="diff" lines_diff="2" />
            <output name="action_report" file="output.fcs_gx_report.txt" compare="diff" lines_diff="2" />
        </test>
        <test expect_num_outputs="2">
            <conditional name="mode">
                <param name="mode_selector" value="clean"/>
                <param name="input" value="fcsgx_test.fa.gz" ftype="fasta"/>
                <param name="action_report" value="output.fcs_gx_report.txt" ftype="tabular"/>
            </conditional>
            <output name="contam_fasta" decompress="true" file="output.contam.fa.gz" ftype="fasta" />
            <output name="clean_fasta" decompress="true" file="output.clean.fa.gz" ftype="fasta" />
        </test>
    </tests>
    <help><![CDATA[
    FCS-GX detects contamination from foreign organisms in genome sequences using the genome cross-species aligner (GX). The FCS-GX executable retrieves a Docker or Singularity container and runs a pipeline to align sequences to a large database of NCBI genomes through modified k-mer seeds and assign a most likely taxonomic division.

    FCS-GX classifies sequences as contaminant when their taxonomic assignment is different from the user provided taxonomic identifier. A contamination summary provides an overview of observed contaminant divisions, counts, and total sizes, and an action report provides details and recommended actions for each problematic sequence.

    https://github.com/ncbi/fcs/wiki/FCS-GX
    ]]></help>
    <expand macro="citations"/>
</tool>
