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

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

    //
    // Input preparation
    //
    withName: 'NFCORE_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: 'CAT_FASTQ' {
        publishDir = [
            path: { "${params.outdir}/cat_fastq" },
            mode: params.publish_dir_mode,
            pattern: '*.fastq',
            enabled: params.save_merged
        ]
    }

    //
    // FASTQ_FASTQC_UMITOOLS_FASTP
    //
    withName: '.*:FASTQ_FASTQC_UMITOOLS_FASTP: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 == "auto-detect" ?  "" : "--adapter_sequence ${params.three_prime_adapter}"
        ].join(" ").trim()
        publishDir = [
            [
                path: { "${params.outdir}/fastp/on_raw" },
                mode: params.publish_dir_mode,
                pattern: "*.{json,html}"
            ],
            [
                path: { "${params.outdir}/fastp/on_raw/log" },
                mode: params.publish_dir_mode,
                pattern: "*.log"
            ],
            [
                path: { "${params.outdir}/fastp/on_raw" },
                mode: params.publish_dir_mode,
                pattern: "*.fail.fastq.gz",
                enabled: params.save_trimmed_fail
            ]
        ]
    }
    withName: '.*:FASTQ_FASTQC_UMITOOLS_FASTP:FASTQC_RAW' {
        //the prefix is required for multiqc to pickup the files separately from the other fastqc instances
        ext.prefix =  { "${meta.id}.raw" }
        ext.args = '--quiet'
        publishDir = [
            path: { "${params.outdir}/fastqc/raw" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: '.*:FASTQ_FASTQC_UMITOOLS_FASTP:FASTQC_TRIM' {
        ext.prefix =  { "${meta.id}.trim" }
        ext.args = '--quiet'
        publishDir = [
            path: { "${params.outdir}/fastqc/trimmed" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    //
    // FASTP LENGTH FILTER
    //
    withName: 'FASTP_LENGTH_FILTER' {
        ext.args = [ "",
            "--disable_adapter_trimming",
            "--disable_quality_filtering",
            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}" : ""
        ].join(" ").trim()
        publishDir = [
            [
                path: { "${params.outdir}/fastp/on_umi_extracted" },
                mode: params.publish_dir_mode,
                pattern: "*.{json,html}"
            ],
            [
                path: { "${params.outdir}/fastp/on_umi_extracted/log" },
                mode: params.publish_dir_mode,
                pattern: "*.log"
            ],
            [
                path: { "${params.outdir}/fastp/on_umi_extracted" },
                mode: params.publish_dir_mode,
                pattern: "*.fail.fastq.gz",
                enabled: params.save_trimmed_fail
            ]
        ]
    }

    //
    // Make bowtie index
    //
    withName: 'INDEX_GENOME' {
        publishDir = [
            path: { "${params.outdir}/bowtie_index/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    //
    // UMI deduplication
    //

    withName: '.*:UMICOLLAPSE_FASTQ' {
        ext.args = { meta.single_end ? "--algo ${params.umitools_method} --two-pass" : "--method ${params.umitools_method} --two-pass --paired --remove-unpaired --remove-chimeric" }
        ext.prefix = { "${meta.id}.umi_dedup.sorted" }
        publishDir = [
            path: { "${params.outdir}/umi_dedup/bam_deduplicated" },
            mode: params.publish_dir_mode,
            pattern: '*.{bam,fastq.gz}',
            enabled: (
                params.save_umi_intermeds
            )
        ]
    }

    withName: 'UMITOOLS_EXTRACT' {
        ext.args  = [
            params.umitools_extract_method ? "--extract-method=${params.umitools_extract_method}" : '',
            params.umitools_bc_pattern     ? "--bc-pattern='${params.umitools_bc_pattern}'" : '',
        ].join(' ').trim()
        publishDir = [
            [
                path: { "${params.outdir}/umi_dedup/fastq_extracted_umi" },
                mode: params.publish_dir_mode,
                pattern: "*.log"
            ],
            [
                path: { "${params.outdir}/umi_dedup/fastq_extracted_umi" },
                mode: params.publish_dir_mode,
                pattern: "*.fastq.gz",
                enabled: params.save_umi_intermeds
            ]
        ]
    }

    //
    // MIRTRACE QC
    //
    withName: 'MIRTRACE_RUN' {
        publishDir = [
            //"mirtrace" already part of the published folder
            path: { "${params.outdir}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

    //
    // MIRNA_QUANT
    //
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:FORMAT_.*' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/reference" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:PARSE_.*' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/reference" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:INDEX_MATURE' {
        publishDir = [
            path: { "${params.outdir}/bowtie_index/mirna_mature" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:INDEX_HAIRPIN' {
        publishDir = [
            path: { "${params.outdir}/bowtie_index/mirna_hairpin" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:BOWTIE_MAP_MATURE' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/bam/mature" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_aligned_mirna_quant
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:BAM_STATS_MATURE:.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/mirna_quant/bam/mature" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:BOWTIE_MAP_HAIRPIN' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/bam/hairpin" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_aligned_mirna_quant
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:BAM_STATS_HAIRPIN:.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/mirna_quant/bam/hairpin" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'EDGER_QC' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/edger_qc" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'SEQCLUSTER_SEQUENCES' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/seqcluster" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:BOWTIE_MAP_SEQCLUSTER' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/bam/seqcluster" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_aligned_mirna_quant
        ]
    }
    withName: 'MIRTOP_QUANT' {
        publishDir = [
            //mirtop already part of the output folder
            path: { "${params.outdir}/mirna_quant/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRNA_QUANT:TABLE_MERGE' {
        publishDir = [
            path: { "${params.outdir}/mirna_quant/mirtop" },
            mode: params.publish_dir_mode,
            pattern: "*.tsv"
        ]
    }


    //
    // GENOME_QUANT
    //
    withName: 'NFCORE_SMRNASEQ:GENOME_QUANT:BAM_SORT_STATS_SAMTOOLS:SAMTOOLS_.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/genome_quant/bam" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:GENOME_QUANT:BAM_SORT_STATS_SAMTOOLS:BAM_STATS_SAMTOOLS:.*' {
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/genome_quant/bam" },
            mode: params.publish_dir_mode,
            pattern: "*.{stats,flagstat,idxstats}"
        ]
    }
    withName: 'NFCORE_SMRNASEQ:GENOME_QUANT:BOWTIE_MAP_GENOME' {
        publishDir = [
            path: { "${params.outdir}/genome_quant/bam" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_aligned
        ]
    }


    //
    // MIRDEEP
    //
    withName: 'NFCORE_SMRNASEQ:MIRDEEP2:MIRDEEP2_MAPPER' {
        publishDir = [
            path: { "${params.outdir}/mirdeep2/mapper" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    withName: 'NFCORE_SMRNASEQ:MIRDEEP2:MIRDEEP2_RUN' {
        publishDir = [
            path: { "${params.outdir}/mirdeep2/run" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    //
    // reports
    //
    withName: 'CUSTOM_DUMPSOFTWAREVERSIONS' {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml'
        ]
    }
    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 }
        ]
    }
}
