/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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   = When to run the module.
----------------------------------------------------------------------------------------
*/

// ANNOTATE

process {

    // SNPEFF
    if (params.tools && (params.tools.split(',').contains('snpeff') || params.tools.split(',').contains('merge'))) {
        withName: 'SNPEFF_SNPEFF' {
            ext.args   = { '-nodownload -canon -v' }
            ext.prefix = { vcf.baseName - '.vcf' + '_snpEff' }
            publishDir = [
                [
                    mode: params.publish_dir_mode,
                    path: { "${params.outdir}/reports/snpeff/${meta.variantcaller}/${meta.id}/" },
                    pattern: "*{csv,html,genes.txt}",
                    saveAs: { params.tools.split(',').contains('snpeff') ? it : null }
                ]
            ]
        }
    }

    // VEP
    if (params.tools && (params.tools.split(',').contains('vep') || params.tools.split(',').contains('merge'))) {
        withName: 'ENSEMBLVEP_VEP' {
            ext.args   = { [
                "--stats_file ${vcf.baseName - '.vcf' + '_VEP.ann'}.summary.html",
                (params.vep_dbnsfp && params.dbnsfp && !params.dbnsfp_consequence)    ? "--plugin dbNSFP,${params.dbnsfp.split("/")[-1]},${params.dbnsfp_fields}"                                              : '',
                (params.vep_dbnsfp && params.dbnsfp && params.dbnsfp_consequence)     ? "--plugin dbNSFP,'consequence=${params.dbnsfp_consequence}',${params.dbnsfp.split("/")[-1]},${params.dbnsfp_fields}"   : '',
                (params.vep_loftee)                                                   ? "--plugin LoF,loftee_path:/usr/local/share/ensembl-vep-${params.vep_version}" : '',
                (params.vep_spliceai && params.spliceai_snv && params.spliceai_indel) ? "--plugin SpliceAI,snv=${params.spliceai_snv.split("/")[-1]},indel=${params.spliceai_indel.split("/")[-1]}"            : '',
                (params.vep_spliceregion)                                             ? '--plugin SpliceRegion'                                                                                                : '',
                (params.vep_out_format)                                               ? "--${params.vep_out_format}"                                                                                           : '--vcf',
                (params.vep_custom_args)                                              ?: ''
            ].join(' ').trim() }
            // If just VEP: <vcf prefix>_VEP.ann.vcf
            ext.prefix = { vcf.baseName - '.vcf' + '_VEP.ann' }
            publishDir = [
                [
                    mode: params.publish_dir_mode,
                    path: { "${params.outdir}/reports/EnsemblVEP/${meta.variantcaller}/${meta.id}/" },
                    pattern: "*html"
                ],
                [
                    mode: params.publish_dir_mode,
                    path: { "${params.outdir}/annotation/${meta.variantcaller}/${meta.id}/" },
                    pattern: "*{gz}"
                ]
            ]
        }
    }

    // BCFTOOLS ANNOTATE
    if (params.tools && params.tools.split(',').contains('bcfann')) {
        withName: 'NFCORE_SAREK:SAREK:VCF_ANNOTATE_ALL:VCF_ANNOTATE_BCFTOOLS:BCFTOOLS_ANNOTATE' {
            ext.args   = { '--output-type z' }
            ext.prefix = { input.baseName - '.vcf' + '_BCF.ann' }
            publishDir = [
                    mode: params.publish_dir_mode,
                    path: { "${params.outdir}/annotation/${meta.variantcaller}/${meta.id}/" },
                    pattern: "*{gz}"
                ]
        }
    }

    // SNPEFF THEN VEP
    if (params.tools && params.tools.split(',').contains('merge')) {
        withName: 'NFCORE_SAREK:SAREK:VCF_ANNOTATE_ALL:VCF_ANNOTATE_MERGE:ENSEMBLVEP_VEP' {
            ext.args   = { [
                "--stats_file ${vcf.baseName - '.ann.vcf' + '_VEP.ann'}.summary.html",
                (params.vep_dbnsfp && params.dbnsfp && !params.dbnsfp_consequence)    ? "--plugin dbNSFP,${params.dbnsfp.split("/")[-1]},${params.dbnsfp_fields}"                                              : '',
                (params.vep_dbnsfp && params.dbnsfp && params.dbnsfp_consequence)     ? "--plugin dbNSFP,'consequence=${params.dbnsfp_consequence}',${params.dbnsfp.split("/")[-1]},${params.dbnsfp_fields}"   : '',
                (params.vep_loftee)                                                   ? "--plugin LoF,loftee_path:/usr/local/share/ensembl-vep-${params.vep_version}" : '',
                (params.vep_spliceai && params.spliceai_snv && params.spliceai_indel) ? "--plugin SpliceAI,snv=${params.spliceai_snv.split("/")[-1]},indel=${params.spliceai_indel.split("/")[-1]}"            : '',
                (params.vep_spliceregion)                                             ? '--plugin SpliceRegion'                                                                                                : '',
                (params.vep_out_format)                                               ? "--${params.vep_out_format}"                                                                                           : '--vcf',
                (params.vep_custom_args)                                              ?: ''
            ].join(' ').trim() }
            // If merge: Output file will have format *_snpEff_VEP.ann.vcf, *_snpEff_VEP.ann.json or *_snpEff_VEP.ann.tab
            ext.prefix = { vcf.baseName - '.ann.vcf' + '_VEP.ann' }
        }
    }

    // ALL ANNOTATION TOOLS
    if (params.tools && (params.tools.split(',').contains('snpeff') || params.tools.split(',').contains('vep') || params.tools.split(',').contains('merge') || params.tools.split(',').contains('bcfann'))) {
        withName: 'NFCORE_SAREK:SAREK:VCF_ANNOTATE_ALL:.*:(TABIX_BGZIPTABIX|TABIX_TABIX)' {
            ext.prefix = { input.name - '.vcf' }
            publishDir = [
                    mode: params.publish_dir_mode,
                    path: { "${params.outdir}/annotation/${meta.variantcaller}/${meta.id}/" },
                    pattern: "*{gz.tbi}"
            ]
        }
    }

    if (params.tools && (params.tools.split(',').contains('snpeff') || params.tools.split(',').contains('merge'))) {
        withName: 'NFCORE_SAREK:SAREK:VCF_ANNOTATE_ALL:VCF_ANNOTATE_SNPEFF:TABIX_BGZIPTABIX' {
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/annotation/${meta.variantcaller}/${meta.id}/" },
                pattern: "*{gz,gz.tbi}",
                saveAs: { params.tools.split(',').contains('snpeff') ? it : null }
            ]
        }
    }
}
