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

process {
    publishDir = [
        mode: params.publish_dir_mode,
        path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
    ]

// QC
    withName: 'FASTQC' {
        ext.args   = { '--quiet' }
        ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('fastqc')) }
        publishDir = [
            [
                path: { "${params.outdir}/reports/fastqc/${meta.id}" },
                mode: params.publish_dir_mode,
                pattern: "*{html,zip}"
            ]
        ]
    }

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

    withName: 'NFCORE_SAREK:SAREK:CRAM_QC_NO_MD:SAMTOOLS_STATS' {
        ext.prefix = { "${meta.id}.sorted.cram" }
        ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('samtools')) }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reports/samtools/${meta.id}" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'NFCORE_SAREK:SAREK:SPRING_DECOMPRESS_.*' {
        ext.prefix = { "${spring.simpleName}" }
        publishDir       = [
            enabled: false
        ]
    }

    withName: 'MOSDEPTH' {
        ext.args   = { !params.wes ? "-n --fast-mode --by 500" : ""}
        ext.prefix = {
            if (params.tools && params.tools.split(',').contains('sentieon_dedup')) {
                "${meta.id}.dedup"
            } else if (params.skip_tools && params.skip_tools.split(',').contains('markduplicates')) {
                "${meta.id}.sorted"
            } else {
                "${meta.id}.md"
            }
        }
        ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('mosdepth')) }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reports/mosdepth/${meta.id}" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    if (!(params.skip_tools && params.skip_tools.split(',').contains('baserecalibrator'))) {
        withName: 'NFCORE_SAREK:SAREK:CRAM_SAMPLEQC:CRAM_QC_RECAL:MOSDEPTH' {
            ext.prefix = { "${meta.id}.recal" }
        }

        withName: 'NFCORE_SAREK:SAREK:CRAM_SAMPLEQC:CRAM_QC_RECAL:SAMTOOLS_STATS' {
            ext.prefix = { "${meta.id}.recal.cram" }
            ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('samtools')) }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/reports/samtools/${meta.id}" },
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }

    // VCF
    withName: 'BCFTOOLS_STATS' {
        ext.prefix = { vcf.baseName - ".vcf" }
        ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('bcftools')) }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reports/bcftools/${meta.variantcaller}/${meta.id}/" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'VCFTOOLS_.*' {
        ext.prefix = { variant_file.baseName - ".vcf" }
        ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('vcftools')) }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reports/vcftools/${meta.variantcaller}/${meta.id}/" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'VCFTOOLS_TSTV_COUNT' {
        ext.args   = { '--TsTv-by-count' }
    }

    withName: 'VCFTOOLS_TSTV_QUAL' {
        ext.args   = { '--TsTv-by-qual' }
    }

    withName: 'VCFTOOLS_SUMMARY' {
        ext.args   = { '--FILTER-summary' }
    }
}
