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

//
// General configuration options
//

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: 'NFCORE_SMRNASEQ:SMRNASEQ:INPUT_CHECK: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'
        ]
    }
}

//
// Genome preparation options
//

process {
    withName: 'CAT_FASTQ' {
        publishDir = [
            path: { "${params.outdir}/fastq" },
            mode: params.publish_dir_mode,
            pattern: '*.fastq',
            // enabled: params.save_merged_fastq //TODO ? implement save_merged_fastq param
        ]
    }
    withName: 'INDEX_GENOME' {
        publishDir = [
            path: { "${params.outdir}/index" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:MIRNA_QUANT:FORMAT_.*' {
        publishDir = [
            path: { "${params.outdir}/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'EDGER_QC' {
        publishDir = [
            path: { "${params.outdir}/edger" },
            mode: params.publish_dir_mode,
            enabled: true
        ]
    }
}

//
// Read QC and trimming options
//
process {
    withName: 'MIRTRACE_RUN' {
        publishDir = [
            path: { "${params.outdir}/mirtrace/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
}

if (!(params.skip_fastqc)) {
    process {
        withName: '.*:FASTQC_FASTP:FASTQC_.*' {
            ext.args = '--quiet'
        }
    }
}

if (!params.skip_fastp) {
    process {
        withName: 'FASTP' {
                    ext.args = [ "",
            params.trim_fastq              ? "" : "--disable_adapter_trimming",
            params.clip_r1 > 0             ? "--trim_front1 ${params.clip_r1}" : "", // Remove bp from the 5' end of read 1.
            params.three_prime_clip_r1 > 0 ? "--trim_tail1 ${params.three_prime_clip_r1}" : "", // Remove bp from the 3' end of read 1 AFTER adapter/quality trimming has been performed.
            params.fastp_min_length > 0    ? "-l ${params.fastp_min_length}" : "",
            params.fastp_max_length > 0    ? "--max_len1 ${params.fastp_max_length}" : "",
            params.three_prime_adapter == null ?  '' : "--adapter_sequence ${params.three_prime_adapter}"
        ].join(" ").trim()
            publishDir = [
                [
                    path: { "${params.outdir}/fastp" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{json,html}"
                ],
                [
                    path: { "${params.outdir}/fastp/log" },
                    mode: params.publish_dir_mode,
                    pattern: "*.log"
                ],
                [
                    path: { "${params.outdir}/fastp" },
                    mode: params.publish_dir_mode,
                    pattern: "*.fail.fastq.gz",
                    enabled: params.save_trimmed_fail
                ]
            ]
        }

    }

    if (!params.skip_fastqc) {
        process {
            withName: '.*:.*:FASTQC_FASTP:FASTQC_TRIM' {
                ext.args = '--quiet'
                publishDir = [
                    path: { "${params.outdir}/fastqc/trim" },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}

//
// Quantification
//

process {
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:MIRNA_QUANT:PARSE_.*' {
    publishDir = [
            path: { "${params.outdir}/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:MIRNA_QUANT:BAM_STATS_.*:SAMTOOLS_SORT' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/samtools" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:MIRNA_QUANT:BAM_STATS_.*:BAM_STATS_SAMTOOLS:.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/samtools/samtools_stats" },
            mode: params.publish_dir_mode,
            pattern: "*.{stats,flagstat,idxstats}"
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:MIRNA_QUANT:BOWTIE_MAP_.*' {
        publishDir = [
            path: { "${params.outdir}/unmapped/fastq" },
            mode: params.publish_dir_mode,
            pattern: "unmapped/*.gz"
        ]
    }
    withName: 'SEQCLUSTER_SEQUENCES' {
        publishDir = [
            path: { "${params.outdir}/seqcluster" },
            enabled: false
        ]
    }
    withName: 'MIRTOP_QUANT' {
        publishDir = [
            path: { "${params.outdir}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:MIRNA_QUANT:TABLE_MERGE' {
        publishDir = [
            path: { "${params.outdir}/mirtop" },
            mode: params.publish_dir_mode,
            pattern: "*.tsv"
        ]
    }
    withName: 'EDGER_QC' {
        publishDir = [
            path: { "${params.outdir}/edger" },
            mode: params.publish_dir_mode,
            enabled: true
        ]
    }
}

process {
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:GENOME_QUANT:BAM_SORT_STATS_SAMTOOLS:SAMTOOLS_.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/samtools" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:GENOME_QUANT:BAM_SORT_STATS_SAMTOOLS:BAM_STATS_SAMTOOLS:.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/samtools/samtools_stats" },
            mode: params.publish_dir_mode,
            pattern: "*.{stats,flagstat,idxstats}"
        ]
    }
    withName: 'NFCORE_SMRNASEQ:SMRNASEQ:GENOME_QUANT:BOWTIE_MAP_.*' {
        publishDir = [
            path: { "${params.outdir}/unmapped/fastq" },
            mode: params.publish_dir_mode,
            pattern: "unmapped/*.gz"
        ]
    }
}

if (!params.skip_mirdeep) {
    process {
        withName: 'MIRDEEP2_MAPPER' {
            publishDir = [
                path: { "${params.outdir}/mirdeep" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (!params.skip_multiqc) {
    process {
        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 }
            ]
        }
    }
}
