/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Config file for defining DSL2 per module options and publishing paths
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Available keys to override module options:
        ext.args            = Additional arguments appended to command in module.
        ext.args2           = Second set of arguments appended to command in module (multi-tool modules).
        ext.args3           = Third set of arguments appended to command in module (multi-tool modules).
        ext.prefix          = File name prefix for output files.
        ext.when            = Conditional clause
----------------------------------------------------------------------------------------
*/

//
// SNV annotation options
//

process {

    if (!params.skip_snv_annotation) {
        withName: '.*ANNOTATE_GENOME_SNVS:BCFTOOLS_ROH' {
            ext.args = { "--samples ${meta.probands.unique().first()} --skip-indels " }
            ext.prefix = { "${meta.id}_roh" }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:RHOCALL_ANNOTATE' {
            ext.args = { "--v14 " }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:ZIP_TABIX_ROHCALL' {
            ext.prefix = { "${input.simpleName}" }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:VCFANNO' {
            ext.prefix = { "${vcf.simpleName}_vcfanno" }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:UPD_SITES' {
            ext.prefix = { "${vcf.simpleName}_upd_sites" }
            ext.args = {"--af-tag GNOMADAF --proband ${meta.upd_child} --mother ${meta.mother} --father ${meta.father} sites"}
        }

        withName: '.*ANNOTATE_GENOME_SNVS:UPD_REGIONS' {
            ext.prefix = { "${vcf.simpleName}_upd_regions" }
            ext.args = {"--af-tag GNOMADAF --proband ${meta.upd_child} --mother ${meta.mother} --father ${meta.father} regions --min-size 5 --min-sites 1"}
            ext.when = { !(workflow.profile.tokenize(',').intersect(['test', 'test_one_sample']).size() >= 1) || workflow.stubRun }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:CHROMOGRAPH_SITES' {
            ext.prefix = { "${sites.simpleName}_chromograph" }
            ext.args = { "--euploid" }
            tag =  {"${meta7.id}"}
            publishDir = [
                path: { "${params.outdir}/annotate_snv/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: '.*ANNOTATE_GENOME_SNVS:CHROMOGRAPH_REGIONS' {
            ext.prefix = { "${regions.simpleName}_chromograph" }
            ext.args = { '--euploid' }
            ext.when = { !(workflow.profile.tokenize(',').intersect(['test', 'test_one_sample', 'test_sentieon']).size() >= 1) || workflow.stubRun }
            tag =  {"${meta6.id}"}
            publishDir = [
                path: { "${params.outdir}/annotate_snv/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: '.*ANNOTATE_GENOME_SNVS:ZIP_TABIX_VCFANNO' {
            ext.prefix = { "${input.simpleName}" }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:BCFTOOLS_VIEW' {
            ext.prefix = { "${vcf.simpleName}_filter" }
            ext.args = { '--output-type z --exclude "INFO/GNOMADAF > 0.70 | INFO/GNOMADAF_popmax > 0.70" ' }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:GATK4_SELECTVARIANTS' {
            ext.prefix = { "${vcf.simpleName}_${intervals.simpleName}" }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:ENSEMBLVEP_SNV' {
            ext.prefix = { "${meta.prefix}_vep_${meta.scatterid}" }
            ext.args = { [
                '--dir_plugins vep_cache/Plugins',
                '--plugin LoFtool,LoFtool_scores.txt',
                '--plugin pLI,pLI_values.txt',
                '--plugin SpliceAI,snv=spliceai_21_scores_raw_snv_-v1.3-.vcf.gz,indel=spliceai_21_scores_raw_snv_-v1.3-.vcf.gz',
                '--distance 5000',
                '--buffer_size 20000',
                '--format vcf --max_sv_size 248956422',
                '--appris --biotype --cache --canonical --ccds --compress_output bgzip',
                '--domains --exclude_predicted --force_overwrite',
                '--hgvs --humdiv --no_progress --numbers',
                '--merged --polyphen p --protein --offline --regulatory --sift p --symbol --tsl',
                '--uniprot --vcf'
            ].join(' ') }
        }

        withName: '.*ANNOTATE_GENOME_SNVS:BCFTOOLS_CONCAT' {
            ext.prefix = { "${meta.prefix}_vep" }
            publishDir = [
                path: { "${params.outdir}/annotate_snv/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: '.*ANNOTATE_GENOME_SNVS:TABIX_BCFTOOLS_CONCAT' {
            publishDir = [
                path: { "${params.outdir}/annotate_snv/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}
