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

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

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

    withName: 'MOSDEPTH' {
        ext.args = { !params.wes ? "-n --fast-mode --by 500" : ""}
        ext.prefix       = { params.skip_tools && params.skip_tools.split(',').contains('markduplicates') ? "${meta.id}.sorted" : "${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.step == 'mapping' || params.step == 'markduplicates'|| params.step == 'prepare_recalibration'|| params.step == 'recalibrate') && (!(params.skip_tools && params.skip_tools.split(',').contains('baserecalibrator')))) {
        withName: 'NFCORE_SAREK:SAREK:CRAM_QC_RECAL:MOSDEPTH' {
            ext.prefix       = { "${meta.id}.recal" }
        }

        withName: 'NFCORE_SAREK:SAREK: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.when         = { !(params.skip_tools && params.skip_tools.split(',').contains('bcftools')) }
        ext.prefix       = { "${vcf.baseName.minus(".vcf")}" }
        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.when         = { !(params.skip_tools && params.skip_tools.split(',').contains('vcftools')) }
        ext.prefix       = { "${variant_file.baseName.minus(".vcf")}" }
        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"
    }
}
