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

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

    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/" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : "mutect2/${meta.id}/${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: 'GATHERPILEUPSUMMARIES_.*' {
        ext.prefix       = { "${meta.id}.mutect2" }
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/variant_calling/mutect2/${meta.tumor_id}_vs_${meta.normal_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}" }
        ]
    }

    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.tumor_id}_vs_${meta.normal_id}/${it}" }
        ]
    }

    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: 'GATK4_MUTECT2' {
        ext.prefix       = { meta.num_intervals <= 1 ? "${meta.id}.mutect2" : "${meta.id}.mutect2.${intervals.simpleName}" }
        ext.when         = { params.tools && params.tools.split(',').contains('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" }
        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
    if (params.tools && params.tools.split(',').contains('mutect2')) {
        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}" }
        }
    }
}
