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

process {

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

    withName: NACHO_QC {
        publishDir = [
            path: { "${params.outdir}/QC/NACHO" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename.indexOf(".png") > 0 ? "png/${filename}" : filename }
        ]
    }

    withName: NACHO_NORMALIZE {
        publishDir = [
            path: { "${params.outdir}/normalized_counts" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = { params.normalization_method ? "--norm_method ${params.normalization_method}" : '' }
    }

    withName: CREATE_ANNOTATED_TABLES {
        publishDir = [
            path: { "${params.outdir}/annotated_tables" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') || filename.contains('_mqc') ? null : filename }
        ]
    }

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

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

    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 }
        ]
    }

}
