/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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_CHIPSEQ:CHIPSEQ: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: 'GUNZIP_.*' {
        publishDir = [
            path: { "${params.outdir}/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

    withName: 'UNTAR_.*' {
        ext.args2 = '--no-same-owner'
        publishDir = [
            path: { "${params.outdir}/genome/index" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

    withName: 'BWA_INDEX|BOWTIE2_BUILD|STAR_GENOMEGENERATE' {
        publishDir = [
            path: { "${params.outdir}/genome/index" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

    withName: 'UNTAR_CHROMAP_INDEX|CHROMAP_INDEX' {
        publishDir = [
            path: { "${params.outdir}/genome/index/chromap" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

    withName: 'GFFREAD' {
        ext.args   = '--keep-exon-attrs -F -T'
        publishDir = [
            path: { "${params.outdir}/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

    withName: 'GTF2BED' {
        publishDir = [
            path: { "${params.outdir}/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

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

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

//
// Read QC and trimming options
//

if (!(params.skip_fastqc || params.skip_qc)) {
    process {
        withName: '.*:FASTQC_TRIMGALORE:FASTQC' {
            ext.args = '--quiet'
            publishDir = [
                [
                    path: { "${params.outdir}/fastqc" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{html}"
                ],
                [
                    path: { "${params.outdir}/fastqc/zips" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{zip}"
                ]
            ]
        }
    }
}

if (!params.skip_trimming) {
    process {
        withName: '.*:FASTQC_TRIMGALORE:TRIMGALORE' {
            ext.args   = [
                '--fastqc',
                params.trim_nextseq > 0 ? "--nextseq ${params.trim_nextseq}" : ''
            ].join(' ').trim()
            publishDir = [
                [
                    path: { "${params.outdir}/trimgalore/fastqc" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{html}"
                ],
                [
                    path: { "${params.outdir}/trimgalore/fastqc/zips" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{zip}"
                ],
                [
                    path: { "${params.outdir}/trimgalore/logs" },
                    mode: params.publish_dir_mode,
                    pattern: "*.txt"
                ],
                [
                    path: { "${params.outdir}/trimgalore" },
                    mode: params.publish_dir_mode,
                    pattern: "*.fq.gz",
                    enabled: params.save_trimmed
                ]
            ]
        }
    }
}

process {
    withName: 'NFCORE_CHIPSEQ:CHIPSEQ:ALIGN_.*:BAM_SORT_SAMTOOLS:SAMTOOLS_SORT' {
        ext.prefix = { "${meta.id}.Lb.sorted" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/library" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_align_intermeds
        ]
    }

    withName: 'NFCORE_CHIPSEQ:CHIPSEQ:ALIGN_.*:BAM_SORT_SAMTOOLS:SAMTOOLS_INDEX' {
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/library" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_align_intermeds
        ]
    }

    withName: 'NFCORE_CHIPSEQ:CHIPSEQ:ALIGN_.*:BAM_SORT_SAMTOOLS:BAM_STATS_SAMTOOLS:SAMTOOLS_.*' {
        ext.prefix = { "${meta.id}.Lb.sorted.bam" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/library/samtools_stats/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_align_intermeds
        ]
    }
}

if (params.aligner == 'bwa') {
    process {
        withName: 'BWA_MEM' {
            ext.args   = { [
                            '-M',
                            params.bwa_min_score ? " -T ${params.bwa_min_score}" : '',
                            meta.read_group ? "-R ${meta.read_group}": ''
                        ].join(' ').trim() }
            ext.args2  = '-bhS -F 0x0100 -O BAM'
            ext.prefix = { "${meta.id}.Lb" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/library" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: false
            ]
        }
    }
}

if (params.aligner == 'bowtie2') {
    process {
        withName: 'BOWTIE2_ALIGN' {
            ext.args   = ''
            ext.prefix = { "${meta.id}.Lb" }
            publishDir = [
                [
                    path: { "${params.outdir}/${params.aligner}/library" },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                    enabled: false
                ],
                [
                    path: { "${params.outdir}/${params.aligner}/library/unmapped" },
                    mode: params.publish_dir_mode,
                    pattern: '*.fastq.gz',
                    enabled: params.save_unaligned
                ]
            ]
        }
    }
}

if (params.aligner == 'chromap') {
    process {
        withName: CHROMAP_INDEX {
            ext.args   = ''
            publishDir = [
                path: { "${params.outdir}/genome/${params.aligner}/index" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
        withName: CHROMAP_CHROMAP {
            ext.args   = '-l 2000 --low-mem --SAM'
            ext.prefix = { "${meta.id}.Lb" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/library" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: false
            ]
        }
    }
}

if (params.aligner == 'star') {
    process {
        withName: '.*:ALIGN_STAR:STAR_ALIGN' {
            ext.args   = [
                '--runMode alignReads',
                '--alignIntronMax 1',
                '--alignEndsType EndToEnd',
                '--outSAMtype BAM Unsorted',
                '--readFilesCommand zcat',
                '--runRNGseed 0',
                '--outSAMattributes NH HI AS NM MD',
                params.save_unaligned ? '--outReadsUnmapped Fastx' : ''
            ].join(' ').trim()
            publishDir = [
                [
                    path: { "${params.outdir}/${params.aligner}/library/log" },
                    mode: params.publish_dir_mode,
                    pattern: '*.{out,tab}'
                ],
                [
                    path: { "${params.outdir}/${params.aligner}/library" },
                    mode: params.publish_dir_mode,
                    pattern: '*.bam',
                    enabled: false
                ],
                [
                    path: { "${params.outdir}/${params.aligner}/library/unmapped" },
                    mode: params.publish_dir_mode,
                    pattern: '*.fastq.gz',
                    enabled: params.save_unaligned
                ]
            ]
        }
    }
}

process {
    withName: 'PICARD_MERGESAMFILES' {
        ext.args   = '--SORT_ORDER coordinate --VALIDATION_STRINGENCY LENIENT --TMP_DIR tmp'
        ext.prefix = { "${meta.id}.mLb.sorted" }
        publishDir = [ enabled: false ]
    }

    withName: '.*:MARK_DUPLICATES_PICARD:PICARD_MARKDUPLICATES' {
        ext.args   = '--ASSUME_SORTED true --REMOVE_DUPLICATES false --VALIDATION_STRINGENCY LENIENT --TMP_DIR tmp'
        ext.prefix = { "${meta.id}.mLb.mkD.sorted" }
        publishDir = [
            [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary/picard_metrics" },
                mode: params.publish_dir_mode,
                pattern: '*.metrics.txt'
            ],
            [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary" },
                mode: params.publish_dir_mode,
                pattern: '*.bam',
                enabled: params.save_align_intermeds
            ]
        ]
    }

    withName: '.*:MARK_DUPLICATES_PICARD:SAMTOOLS_INDEX' {
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary" },
            mode: params.publish_dir_mode,
            pattern: '*.{bai,csi}',
            enabled: params.save_align_intermeds
        ]
    }

    withName: '.*:MARK_DUPLICATES_PICARD:BAM_STATS_SAMTOOLS:.*' {
        ext.prefix = { "${meta.id}.mLb.mkD.sorted.bam" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary/samtools_stats" },
            mode: params.publish_dir_mode,
            pattern: '*.{stats,flagstat,idxstats}'
        ]
    }

    // Should only be published when paired end data is used and save_align_intermeds is true
    withName: 'BAM_FILTER' {
        ext.prefix = { meta.single_end ?  "${meta.id}.mLb.noPublish" : "${meta.id}.mLb.flT.sorted" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary" },
            mode: params.publish_dir_mode,
            pattern: '*.mLb.flT.sorted.bam',
            enabled: params.save_align_intermeds
        ]
    }

    withName: 'BAM_REMOVE_ORPHANS' {
        ext.args   = '--only_fr_pairs'
        ext.prefix = { "${meta.id}.mLb.clN" }
        publishDir = [ enabled: false ]
    }

    withName: 'NFCORE_CHIPSEQ:CHIPSEQ:FILTER_BAM_BAMTOOLS:BAM_SORT_SAMTOOLS:SAMTOOLS_SORT' {
        ext.prefix = { "${meta.id}.mLb.clN.sorted" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary" },
            mode: params.publish_dir_mode,
            pattern: '*.bam'
        ]
    }

    withName: 'NFCORE_CHIPSEQ:CHIPSEQ:FILTER_BAM_BAMTOOLS:BAM_SORT_SAMTOOLS:SAMTOOLS_INDEX' {
        ext.prefix = { "${meta.id}.mLb.clN.sorted" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary" },
            mode: params.publish_dir_mode,
            pattern: '*.{bai,csi}'
        ]
    }

    withName: 'NFCORE_CHIPSEQ:CHIPSEQ:FILTER_BAM_BAMTOOLS:BAM_SORT_SAMTOOLS:BAM_STATS_SAMTOOLS:.*' {
        ext.prefix = { "${meta.id}.mLb.clN.sorted.bam" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary/samtools_stats" },
            mode: params.publish_dir_mode,
            pattern: "*.{stats,flagstat,idxstats}"
        ]
    }

    withName: 'PHANTOMPEAKQUALTOOLS' {
        ext.args2  = { "-p=$task.cpus" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary/phantompeakqualtools" },
            mode: params.publish_dir_mode,
            pattern: "*.{out,pdf}"
        ]
    }

    withName: 'MULTIQC_CUSTOM_PHANTOMPEAKQUALTOOLS' {
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary/phantompeakqualtools" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'BEDTOOLS_GENOMECOV' {
        ext.prefix = { "${meta.id}" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary/bigwig/scale" },
            mode: params.publish_dir_mode,
            pattern: "*.txt"
        ]
    }

    withName: 'UCSC_BEDGRAPHTOBIGWIG' {
        ext.prefix = { "${meta.id}" }
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mergedLibrary/bigwig" },
            mode: params.publish_dir_mode,
            pattern: "*.bigWig"
        ]
    }
}

if (!params.skip_picard_metrics) {
    process {
        withName: 'PICARD_COLLECTMULTIPLEMETRICS' {
            ext.args   = '--VALIDATION_STRINGENCY LENIENT --TMP_DIR tmp'
            ext.prefix = { "${meta.id}.mLb.clN.sorted" }
            publishDir = [
                [
                    path: { "${params.outdir}/${params.aligner}/mergedLibrary/picard_metrics" },
                    mode: params.publish_dir_mode,
                    pattern: "*_metrics"
                ],
                [
                    path: { "${params.outdir}/${params.aligner}/mergedLibrary/picard_metrics/pdf" },
                    mode: params.publish_dir_mode,
                    pattern: "*.pdf"
                ]
            ]
        }
    }
}

if (!params.skip_preseq) {
    process {
        withName: 'PRESEQ_LCEXTRAP' {
            ext.args   = '-verbose -bam -seed 1'
            ext.prefix = { "${meta.id}.mLb.clN" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary/preseq" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (!params.skip_plot_profile) {
    process {
        withName: 'DEEPTOOLS_COMPUTEMATRIX' {
            ext.args   = 'scale-regions --regionBodyLength 1000 --beforeRegionStartLength 3000 --afterRegionStartLength 3000 --skipZeros --smartLabels'
            ext.prefix = { "${meta.id}.mLb.clN" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary/deepTools/plotProfile" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'DEEPTOOLS_PLOTPROFILE' {
            ext.prefix = { "${meta.id}.mLb.clN" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary/deepTools/plotProfile" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'DEEPTOOLS_PLOTHEATMAP' {
            ext.prefix = { "${meta.id}.mLb.clN" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary/deepTools/plotProfile" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

process {
    withName: 'KHMER_UNIQUEKMERS' {
        publishDir = [ enabled: false ]
    }
}

if (!params.skip_plot_fingerprint) {
    process {
        withName: 'DEEPTOOLS_PLOTFINGERPRINT' {
            ext.args   = { [
                '--skipZeros',
                "--numberOfSamples $params.fingerprint_bins",
                "--labels $meta.id $meta.control"
            ].join(' ').trim() }
            ext.prefix = { "${meta.id}.mLb.clN" }
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/mergedLibrary/deepTools/plotFingerprint" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

process {
    withName: 'MACS2_CALLPEAK' {
        ext.args   = [
            '--keep-dup all',
            params.narrow_peak          ? '' : "--broad --broad-cutoff ${params.broad_cutoff}",
            params.save_macs_pileup     ? '--bdg --SPMR' : '',
            params.macs_fdr             ? "--qvalue ${params.macs_fdr}" : '',
            params.macs_pvalue          ? "--pvalue ${params.macs_pvalue}" : '',
            params.aligner == "chromap" ? "--format BAM" : ''
        ].join(' ').trim()
        publishDir = [
            path: { [
                "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                params.narrow_peak? '/narrowPeak' : '/broadPeak'
            ].join('') },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'FRIP_SCORE' {
        ext.args   = '-bed -c -f 0.20'
        publishDir = [
            path: { [
                "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                params.narrow_peak? '/narrowPeak' : '/broadPeak',
                '/qc'
            ].join('') },
            enabled: false
        ]
    }

    withName: 'MULTIQC_CUSTOM_PEAKS' {
        publishDir = [
            path: { [
                "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                params.narrow_peak? '/narrowPeak' : '/broadPeak',
                '/qc'
            ].join('') },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
}

if (!params.skip_peak_annotation) {
    process {
        withName: 'HOMER_ANNOTATEPEAKS_MACS2' {
            ext.args   = '-gid'
            ext.prefix = { "${meta.id}_peaks" }
            publishDir = [
                path: { [
                    "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                    params.narrow_peak? '/narrowPeak' : '/broadPeak'
                ].join('') },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }

    if (!params.skip_peak_qc) {
        process {
            withName: 'PLOT_MACS2_QC' {
                ext.args   = '-o ./ -p macs2_peak'
                publishDir = [
                    path: { [
                        "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                        params.narrow_peak? '/narrowPeak' : '/broadPeak',
                        '/qc'
                    ].join('') },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }

            withName: 'PLOT_HOMER_ANNOTATEPEAKS' {
                ext.args   = '-o ./'
                ext.prefix = 'macs2_annotatePeaks'
                publishDir = [
                    path: { [
                        "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                        params.narrow_peak? '/narrowPeak' : '/broadPeak',
                        '/qc'
                    ].join('') },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}

if (!params.skip_consensus_peaks) {
    process {
        withName: 'MACS2_CONSENSUS' {
            ext.when   = { meta.multiple_groups || meta.replicates_exist }
            ext.prefix = { "${meta.id}.consensus_peaks" }
            publishDir = [
                path: { [
                    "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                    params.narrow_peak? '/narrowPeak' : '/broadPeak',
                    '/consensus',
                    "/${meta.id}"
                ].join('') },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: 'SUBREAD_FEATURECOUNTS'  {
            ext.args   = '-F SAF -O --fracOverlap 0.2'
            ext.prefix = { "${meta.id}.consensus_peaks" }
            publishDir = [
                path: { [
                    "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                    params.narrow_peak? '/narrowPeak' : '/broadPeak',
                    '/consensus',
                    "/${meta.id}"
                ].join('') },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }

    if (!params.skip_peak_annotation) {
        process {
            withName: 'HOMER_ANNOTATEPEAKS_CONSENSUS' {
                ext.args   = '-gid'
                ext.prefix = { "${meta.id}.consensus_peaks" }
                publishDir = [
                    path: { [
                        "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                        params.narrow_peak? '/narrowPeak' : '/broadPeak',
                        '/consensus',
                        "/${meta.id}"
                    ].join('') },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }

            withName: 'ANNOTATE_BOOLEAN_PEAKS' {
                ext.prefix = { "${meta.id}.consensus_peaks" }
                publishDir = [
                    path: { [
                        "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                        params.narrow_peak? '/narrowPeak' : '/broadPeak',
                        '/consensus',
                        "/${meta.id}"
                    ].join('') },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }

    if (!params.skip_deseq2_qc) {
        process {
            withName: DESEQ2_QC {
                ext.when   = { meta.multiple_groups && meta.replicates_exist }
                ext.args   = [
                    '--id_col 1',
                    '--sample_suffix \'.mLb.clN.sorted.bam\'',
                    '--count_col 7',
                    params.deseq2_vst ? '--vst TRUE' : ''
                ].join(' ').trim()
                ext.prefix = { "${meta.id}.consensus_peaks" }
                publishDir = [
                    path: { [
                        "${params.outdir}/${params.aligner}/mergedLibrary/macs2",
                        params.narrow_peak? '/narrowPeak' : '/broadPeak',
                        '/consensus',
                        "/${meta.id}",
                        '/deseq2'
                    ].join('') },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}

if (!params.skip_igv) {
    process {
        withName: 'IGV' {
            publishDir = [
                    path: { [
                        "${params.outdir}/igv",
                        params.narrow_peak? '/narrowPeak' : '/broadPeak'
                    ].join('') },
                    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",
                    params.narrow_peak? '/narrowPeak' : '/broadPeak'
                ].join('') },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}
