/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.
----------------------------------------------------------------------------------------
*/

// MUTECT2

process {
    if (params.tools && params.tools.split(',').contains('mutect2')) {

        withName: 'GATK4_MUTECT2' {
            ext.args   = { params.ignore_soft_clipped_bases ? "--dont-use-soft-clipped-bases true --f1r2-tar-gz ${task.ext.prefix}.f1r2.tar.gz" : "--f1r2-tar-gz ${task.ext.prefix}.f1r2.tar.gz" }
            ext.prefix = { meta.num_intervals <= 1 ? "${meta.id}.mutect2" : "${meta.id}.mutect2.${intervals.simpleName}" }
            ext.when   = { params.tools && params.tools.split(',').contains('mutect2') }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/" },
                pattern: "*{vcf.gz,vcf.gz.tbi,stats}",
                saveAs: { meta.num_intervals > 1 ? null : "mutect2/${meta.id}/${it}" }
            ]
        }

        // PAIR_VARIANT_CALLING
        withName: 'MUTECT2_PAIRED' {
            ext.args   = { params.ignore_soft_clipped_bases ?
                                "--dont-use-soft-clipped-bases true --f1r2-tar-gz ${task.ext.prefix}.f1r2.tar.gz --normal-sample ${meta.patient}_${meta.normal_id}" :
                                "--f1r2-tar-gz ${task.ext.prefix}.f1r2.tar.gz --normal-sample ${meta.patient}_${meta.normal_id}" }
        }

        withName: 'MERGE_MUTECT2.*' {
            ext.prefix = { "${meta.id}.mutect2" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/mutect2/${meta.id}" },
                pattern: "*{vcf.gz,vcf.gz.tbi}"
            ]
        }

        withName: 'FILTERMUTECTCALLS.*' {
            ext.prefix = {"${meta.id}.mutect2.filtered"}
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/mutect2/${meta.id}" },
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'CALCULATECONTAMINATION' {
            ext.args   = { "-tumor-segmentation ${meta.id}.mutect2.segmentation.table" }
            ext.prefix = { "${meta.id}.mutect2" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/mutect2/${meta.id}" },
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'LEARNREADORIENTATIONMODEL' {
            ext.prefix = { "${meta.id}.mutect2.artifactprior" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/mutect2/${meta.id}/" },
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'MERGEMUTECTSTATS' {
            ext.prefix = { "${meta.id}.mutect2" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/mutect2/${meta.id}/" },
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'GATHERPILEUPSUMMARIES.*' {
            ext.prefix = { "${meta.id}.mutect2" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/mutect2/${meta.id}/" },
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'GETPILEUPSUMMARIES.*' {
            ext.prefix = { meta.num_intervals <= 1 ? "${meta.id}.mutect2" : "${meta.id}.mutect2.${intervals.simpleName}" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/" },
                pattern: "*.table",
                saveAs: { meta.num_intervals > 1 ? null : "mutect2/${meta.id}/${it}" }
            ]
        }

        if (params.joint_mutect2) {
            withName: 'CALCULATECONTAMINATION' {
                publishDir = [
                    mode: params.publish_dir_mode,
                    path: { "${params.outdir}/variant_calling/mutect2/${meta.patient}" },
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}
