<tool id="crossmap_vcf" name="CrossMap VCF" version="@WRAPPER_VERSION@">
    <description>Convert genome coordinates or annotation files between genome assemblies</description>
    <macros>
        <import>macros.xml</import>
    </macros>
    <expand macro="requirements"/>
    <expand macro="stdio"/>
    <expand macro="version_command"/>

    <command detect_errors="aggressive"><![CDATA[
#set $input_file = str($seq_source.input)

CrossMap.py vcf

#if $seq_source.index_source_s == "cached"
    ## This is the 2nd dbkey, and the corresponding value has to be looked up
    "${filter(lambda x: str( x[1] ) == str($seq_source.chain_source.input_chain ), $__app__.tool_data_tables['liftOver'].get_fields())[0][2] }"
#else
    '${seq_source.chain_source.input_chain}'
#end if

'${input_file}'
'${seq_source.input_fasta}'

output
    ]]></command>

    <inputs>
        <conditional name="seq_source">
            <param name="index_source_s" type="select" label="Source for Input Data">
                <option value="cached">Local data (in galaxy)</option>
                <option value="history">From History</option>
            </param>
            <when value="cached">
                <param name="input" type="data" format="vcf" label="VCF file">
                    <!--
                    <validator type="unspecified_build"/>
                    -->
                    <!-- Gives error in tests
                    <validator type="dataset_metadata_in_file" filename="liftOver.loc" metadata_name="dbkey" metadata_column="0" message="LiftOver mapping (chain file) is not available for the specified build."/>
                    -->
                </param>
                <param name="input_fasta" type="select" label="Lift Over To (FASTA file)" help="The FASTA file must be on the same build (dbkey) as the LiftOver chain file">
                    <options from_data_table="all_fasta"/>
                </param>

                <expand macro="chain" />
            </when>

            <when value="history">
                <param name="input" type="data" format="vcf" label="VCF file"/>
                <param name="input_fasta" type="data" format="fasta" label="Full genome FASTA file"/>

                <expand macro="chain" />
            </when>
        </conditional>
    </inputs>

    <outputs>
        <data name="output" format="vcf" label="${tool.name} on ${on_string}" from_work_dir="output" />
        <data name="output_unmapped" format="vcf" label="${tool.name} (unmapped) on ${on_string}" from_work_dir="output.unmap" />
    </outputs>

    <tests>
        <test>
            <param name="index_source_s" value="history"/>
            <param name="input" value="test_vcf_01_input.vcf" ftype="vcf"/>
            <param name="index_source" value="history"/>
            <param name="input_fasta" value="test_vcf_01.fasta" ftype="fasta"/>
            <param name="input_chain" value="test_vcf_01.over.chain" ftype="csv"/>

            <output name="output">
                <assert_contents>
                    <has_text text="##fileformat=VCFv4.2"/>
                    <has_text text="##liftOverProgram=CrossMap(https://sourceforge.net/projects/crossmap/)"/>
                    <has_text text="##new_reference_genome="/>
                    <has_text text="#CHROM"/>
                    <has_text_matching expression="2.*?rs11449.*?PASS"/>
                    <has_text_matching expression="2.*?rs84825.*?PASS"/>
                </assert_contents>
            </output>
            <output name="output_unmapped" file="test_vcf_01_output.vcf.unmap"/>
        </test>
    </tests>
    <help><![CDATA[
@HELP_GENERAL@

VCF
---

VCF (variant call format) is a flexible and extendable line-oriented
text format developed by the 1000 Genome Project. It is useful for
representing single nucleotide variants, indels, copy number
variants, and structural variants. Chromosomes, coordinates, and
reference alleles are updated to a new assembly, and all the other
fields are not changed.

Notes:

- Genome coordinates and reference allele will be updated to target assembly.
- Reference genome is genome sequence of target assembly.
- If the reference genome sequence file (../database/genome/hg18.fa) was
  not indexed, CrossMap will automatically indexed it (only the first time
  you run CrossMap).
- In the output VCF file, whether the chromosome IDs contain "chr" or not
  depends on the format of the input VCF file.
    ]]></help>

    <citations>
        <citation type="doi">10.1093/bioinformatics/btt730</citation>
    </citations>
</tool>
