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

//
// General configuration options
//

process {

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

    withName: 'CUSTOM_DUMPSOFTWAREVERSIONS' {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml'
        ]
    }
}

//
// Read QC options
//

process {
    withName: '.*RAREDISEASE:FASTQC' {
        ext.args = '--quiet'
        publishDir = [
            path: { "${params.outdir}/fastqc/${meta.id}" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }
}

//
// Remove mitochondrial variants
//

process {
    withName: '.*RAREDISEASE:GATK4_SELECTVARIANTS' {
        ext.args   = "--exclude-intervals ${params.mito_name}"
        ext.prefix = { "${meta.id}_nomito" }
        ext.when   = { !params.skip_snv_annotation }
        publishDir = [
            enabled: !params.skip_mt_analysis,
            path: { "${params.outdir}/call_snv" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
}

//
// Merge mitochondrial and genomic vcfs
//

process {
    withName: '.*RAREDISEASE:GATK4_MERGEVCFS' {
        ext.prefix = { "${meta.id}_mito_genome_merged" }
    }
}

//
// SENTIEON_TNSCOPE_MT_CALL
//

process {
    withName: '.*SENTIEON_TNSCOPE' {
        ext.args = { (params.genome == "GRCh37") ? " --interval MT " : "--interval chrM" }
        ext.args2 = " --min_init_normal_lod 0,5 --min_normal_lod 2,0 --min_init_tumor_lod 1,0 --min_tumor_lod 2,8 --trim_soft_clip "
        ext.when = params.variant_caller.equals("sentieon")
    }
}

//
// Smncopynumbercaller options
//

process {
    withName: '.*SMNCOPYNUMBERCALLER' {
        ext.args = {
        (params.genome == 'GRCh37') ? '--genome 37' : '--genome 38'
        }
        ext.prefix = { "${meta.id}" }
        publishDir = [
            path: { "${params.outdir}/smncopynumbercaller" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }
}

//
// Filter out clincal and research variants
//

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

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

//
// Multiqc options
//

process {
    withName: '.*RAREDISEASE:MULTIQC' {
        publishDir = [
            path: { "${params.outdir}/multiqc/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }
}
