/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.prefix       = { "${vcf.baseName.minus(".vcf")}_snpEff" }
            ext.args         = '-nodownload -canon -v'
            if (!params.snpeff_cache && !params.download_cache) container = { params.snpeff_genome ? "docker.io/nfcore/snpeff:${params.snpeff_version}.${params.snpeff_genome}" : "docker.io/nfcore/snpeff:${params.snpeff_version}.${params.genome}" }
            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         = { [
                '--everything --filter_common --per_gene --total_length --offline --format vcf',
                (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:/opt/conda/envs/nf-core-vep-${params.vep_version}/share/ensembl-vep-${params.vep_version}-0" : '',
                (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.minus(".vcf")}_VEP.ann" }
            if (!params.vep_cache && !params.download_cache)    container = { params.vep_genome ? "docker.io/nfcore/vep:${params.vep_version}.${params.vep_genome}" : "docker.io/nfcore/vep:${params.vep_version}.${params.genome}" }
            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}"
                ]
            ]
        }
    }

    // SNPEFF THEN VEP
    if (params.tools && params.tools.split(',').contains('merge')) {
        withName: "NFCORE_SAREK:SAREK:VCF_ANNOTATE_ALL:VCF_ANNOTATE_MERGE:ENSEMBLVEP_VEP" {
            // If merge: Output file will have format *_snpEff_VEP.ann.vcf, *_snpEff_VEP.ann.json or *_snpEff_VEP.ann.tab
            ext.prefix       = { "${vcf.baseName.minus(".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'))) {
        withName: "NFCORE_SAREK:SAREK:VCF_ANNOTATE_ALL:.*:(TABIX_BGZIPTABIX|TABIX_TABIX)" {
            ext.prefix       = { input.baseName.minus(".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 }
            ]
        }
    }
}
