<?xml version="1.0"?>
<macros>
    <xml name="inputs">
	    <param name="input_file" type="data" format="fasta,fasta.gz,nex" label="Input FASTA or NEXUS file" help="If the input file type is NEXUS and it includes a valid newick tree, that tree will override an uploaded newick tree" />
        <param name="input_nhx" type="data" format="nhx,newick" optional="true" label="Input newick file"/>
    </xml>
    <xml name="substitution">
        <param name="model" type="select" label="Substitution model">
            <option value="GTR">GTR - General time reversible
            model</option>
            <option value="LG">LG - Generalist empirical model from
            Le and Gascuel (2008)</option>
            <option value="HIVBm">HIVBm - Specialist empirical model
            for between-host HIV sequences</option>
            <option value="HIVWm">HIVWm - Specialist empirical model
            for within-host HIV sequences</option>
            <option value="WAG">WAG - Generalist empirical model from
            Whelan and Goldman (2001)</option>
            <option value="JTT">JTT - Generalist empirical model from
            Jones, Taylor, and Thornton (1996)</option>
            <option value="JC69">JC69 - Generalist empirical model
            from with equal exchangeability rates</option>
            <option value="mtMet">mtMet - Specialist empirical model
            for metazoan mitochondrial genomes</option>
            <option value="mtVer">mtVer - Specialist empirical model
            for vertebrate mitochondrial genomes</option>
            <option value="mtInv">mtInv - Specialist empirical model
            for invertebrate mitochondrial genomes</option>
            <option value="gcpREV">gcpREV - Specialist empirical
            model for green plant chloroplast genomes</option>
        </param>
    </xml>

    <xml name="conditional_posteriorEstimationMethod">
        <conditional name="posteriorEstimationMethod">
            <param argument="--method" type="select" label="Posterior estimation method">
                <option value="Variational-Bayes">0-th order Variational Bayes approximation</option>
                <option value="Metropolis-Hastings">Full Metropolis-Hastings MCMC algorithm</option>
                <option value="Collapsed-Gibbs">Collapsed Gibbs sampler</option>
            </param>
            <when value="Variational-Bayes">
            </when>
            <when value="Metropolis-Hastings">
                <expand macro="mcmc_options" />
            </when>
            <when value="Collapsed-Gibbs">
                <expand macro="mcmc_options" />
            </when>
        </conditional>
    </xml>

    <token name="@posteriorEstimationMethod_cmd@">
            #if $posteriorEstimationMethod.method != "Variational-Bayes"
                --chains '$posteriorEstimationMethod.chains'
                --chain-length '$posteriorEstimationMethod.chain_length'
                --burn-in '$posteriorEstimationMethod.samples'
                --samples '$posteriorEstimationMethod.samples_per_chain'
            #end if
    </token>

    <xml name="mcmc_options">
        <param argument="--chains" type="integer" value="5" min="2" max="20" label="Number of MCMC chains" />
        <param argument="--chain-length" name="chain_length" type="integer" value="2000000" min="500000" max="50000000" label="Length of each chain" />
        <param argument="--burn-in" name="samples" type="integer" value="1000000" min="100000" max="1900000" label="Samples to use for burn-in" />
        <param argument="--samples" name="samples_per_chain" type="integer" value="100" min="50" max="1000000" label="Samples to draw from each chain" />
    </xml>

    <xml name="gencode">
        <param name="gencodeid" type="select" label="Genetic code">
            <option value="Universal">Universal code</option>
            <option value="Vertebrate-mtDNA">Vertebrate mitochondrial DNA
            code</option>
            <option value="Yeast-mtDNA">Yeast mitochondrial DNA
            code</option>
            <option value="Mold-Protozoan-mtDNA">Mold, Protozoan and
            Coelenterate mt; Mycloplasma/Spiroplasma</option>
            <option value="Invertebrate-mtDNA">Invertebrate mitochondrial
            DNA code</option>
            <option value="Ciliate-Nuclear">Ciliate, Dasycladacean and
            Hexamita Nuclear code</option>
            <option value="Echinoderm-mtDNA">Echinoderm mitochondrial DNA
            code</option>
            <option value="Euplotid-Nuclear">Euplotid Nuclear
            code</option>
            <option value="Alt-Yeast-Nuclear">Alternative Yeast Nuclear
            code</option>
            <option value="Ascidian-mtDNA">Ascidian mitochondrial DNA
            code</option>
            <option value="Flatworm-mtDNA">Flatworm mitochondrial DNA
            code</option>
            <option value="Blepharisma-Nuclear">Blepharisma Nuclear
            code</option>
        </param>
    </xml>

    <xml name="srv">
        <conditional name="advanced" label="Advanced options">
            <param name="srv_options" type="select" label="Set advanced parameters" help="If these parameters are not set, RELAX will calculate appropriate values at runtime">
                <option value="defaults">Calculate</option>
                <option value="specify">Specify values</option>
            </param>
            <when value="defaults" />
            <when value="specify">
                <param argument="--grid-size" type="integer" value="250" label="Points in the initial distributional guess for likelihood fitting" />
                <param argument="--starting-points" type="integer" value="1" label="Initial random guesses to seed rate values optimization" />
                <param argument="--syn-rates" type="integer" min="1" max="10" value="3" label="Alpha rate classes to include in the model" />
                <param argument="--rates" type="integer" min="2" max="10" value="3" label="Omega rate classes to include in the model" />
                <param argument="--srv" type="boolean" truevalue="Yes" falsevalue="No" label="Include synonymous rate variation" />
                <param name="save_alternative_model" type="boolean" label="Save alternative model fit" />
            </when>
        </conditional>
    </xml>

    <xml name="branches">
        <conditional name="branch_cond">
            <param name="branch_sel" type="select" label="Set of branches to test">
                <option value="All">All branches</option>
                <option value="Internal">Internal branches</option>
                <option value="Leaves">Leaf branches</option>
                <option value="'Unlabeled-branches'">Unlabeled branches</option>
                <option value="specify">Enter a branch label</option>
            </param>
            <when value="specify">
                <param name="branch_label" type="text" value="Test" optional="false" label="Branch label">
                    <sanitizer invalid_char="">
                        <valid initial="default" />
                    </sanitizer>
                </param>
            </when>
            <when value="All" />
            <when value="Internal" />
            <when value="Leaves" />
            <when value="'Unlabeled-branches'" />
        </conditional>
    </xml>
    <xml name="citations">
        <citations>
            <citation type="doi">10.1093/molbev/msz197</citation>
            <yield/>
        </citations>
    </xml>
    <token name="@VERSION@">2.5.31</token>
    <xml name="requirements">
        <requirements>
            <requirement type="package" version="@VERSION@">hyphy</requirement>
            <yield/>
        </requirements>
    </xml>
    <xml name="alternative_model_output">
        <data name="alternative_model" format="hyphy_results.json" from_work_dir="alternative_model.json">
            <filter>advanced['save_alternative_model']</filter>
        </data>
    </xml>
    <token name="@INPUT_TREE@"><![CDATA[
            #if $input_nhx:
                --tree input.nhx
            #end if
        ]]>
    </token>
    <token name="@branch_options@"><![CDATA[
        #if $branch_cond.branch_sel == 'specify':
            --branches '$branch_cond.branch_label'
        #else:
            --branches $branch_cond.branch_sel
        #end if
        ]]>
    </token>
    <token name="@advanced_options@"><![CDATA[
        #if $advanced.srv_options == 'specify':
            --grid-size $advanced.grid_size
            --starting-points $advanced.starting_points
            --syn-rates $advanced.syn_rates
            --rates $advanced.rates
            --srv $advanced.srv
            #if $advanced.save_alternative_model:
                --save-fit alternative_model.json
            #end if
        #end if
    ]]></token>
    <token name="@HYPHYMPI@">\${GALAXY_MPIRUN:-mpirun -mca orte_tmpdir_base "\${TMPDIR:-.}" -np \${GALAXY_SLOTS:-1}} HYPHYMPI</token>
    <token name="@ERRORS@"><![CDATA[
        ; EC=\$? ; [[ -f errors.* ]] && cat errors.* >&2 ; exit \$EC
    ]]></token>
    <token name="@HYPHY_ENVIRONMENT@"><![CDATA[
        export HYPHY=`which hyphy` &&
        export HYPHY_PATH=`dirname \$HYPHY` &&
        export HYPHY_LIB=`readlink -f \$HYPHY_PATH/../share/hyphy` &&]]></token>
    <token name="@HYPHY_INVOCATION@"><![CDATA[
        @HYPHY_ENVIRONMENT@ hyphy LIBPATH=\$HYPHY_LIB
    ]]></token>
    <token name="@SYMLINK_FILES@"><![CDATA[
        #if $input_nhx is not None:
            ln -s '$input_nhx' input.nhx &&
        #end if
        ln -s '$input_file' input.$input_file.extension &&
        #set $input_file = 'input.%s' % $input_file.extension
    ]]></token>
</macros>
