/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: 'copy',
        enabled: true,
        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
    ]

    // nanoseq.nf includes
    withName: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: 'copy',
            enabled: true,
            pattern: '*_versions.yml'
        ]
    }
    withName: GET_TEST_DATA {
        publishDir = [
            path: { "${params.outdir}/test-dataset" },
            mode: 'copy',
            enabled: false,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    // INPUT_CHECK
    withName: SAMPLESHEET_CHECK {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: 'copy',
            enabled: true,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
}

if (!params.skip_demultiplexing) {
    process {
        withName: QCAT {
            publishDir = [
                path: { "${params.outdir}/qcat" },
                mode: 'copy',
                enabled: true,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (params.run_nanolyse) {
    withName: GET_NANOLYSE_FASTA {
        publishDir = [
            path: { "${params.outdir}/nanolyse" },
            mode: 'copy',
            enabled: false,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }
    process {
        withName: NANOLYSE {
            publishDir = [
                path: { "${params.outdir}/nanolyse" },
                mode: 'copy',
                enabled: true,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (!params.skip_qc) {
    if (!params.skip_nanoplot) {
        process {
            withName: NANOPLOT {
                publishDir = [
                    path: { "${params.outdir}/nanoplot" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
    if (!params.skip_fastqc) {
        process {
            withName: FASTQC {
                publishDir = [
                    path: { "${params.outdir}/fastqc" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}

if (!params.skip_alignment) {
    process {
        withName: GTF2BED {
            publishDir = [
                path: { "${params.outdir}/genome" },
                mode: 'copy',
                enabled: false,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
        withName: GET_CHROM_SIZES {
            publishDir = [
                path: { "${params.outdir}/genome" },
                mode: 'copy',
                enabled: false,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
        withName: SAMTOOLS_FAIDX {
            publishDir = [
                path: { "${params.outdir}/genome" },
                mode: 'copy',
                enabled: false,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
    if (params.aligner == "graphmap2") {
        process {
            withName: GRAPHMAP2_INDEX {
                publishDir = [
                    path: { "${params.outdir}/graphmap2" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: GRAPHMAP2_ALIGN {
                publishDir = [
                    path: { "${params.outdir}/graphmap2" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_VIEW_BAM {
                publishDir = [
                    path: { "${params.outdir}/graphmap2" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_SORT {
                ext.prefix = { "${meta.id}.sorted" }
                publishDir = [
                    path: { "${params.outdir}/graphmap2" },
                    mode: 'copy',
                    enabled: true,
                    pattern: "*.sorted.bam",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_INDEX {
                publishDir = [
                    path: { "${params.outdir}/graphmap2" },
                    mode: 'copy',
                    enabled: true,
                    pattern: "*.bai",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_SORT_INDEX {
                publishDir = [
                    path: { "${params.outdir}/graphmap2" },
                    mode: 'copy',
                    enabled: true,
                    pattern: "*{'.sorted.bam','.bai'}",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_STATS {
                publishDir = [
                    path: { "${params.outdir}/graphmap2/samtools_stats" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_IDXSTATS {
                publishDir = [
                    path: { "${params.outdir}/graphmap2/samtools_stats" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_FLAGSTAT {
                publishDir = [
                    path: { "${params.outdir}/graphmap2/samtools_stats" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        if (!params.skip_bigbed) {
            process {
                withName: BEDTOOLS_BAMBED {
                    publishDir = [
                        path: { "${params.outdir}/graphmap2/bigbed" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*.bed12",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
                withName: UCSC_BED12TOBIGBED {
                    publishDir = [
                        path: { "${params.outdir}/graphmap2/bigbed" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*.bigBed",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
        }
        if (!params.skip_bigwig) {
            process {
                withName: BEDTOOLS_GENOMECOV {
                    publishDir = [
                        path: { "${params.outdir}/graphmap2/bigwig" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*bedGraph",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
            process {
                withName: UCSC_BEDGRAPHTOBIGWIG {
                    publishDir = [
                        path: { "${params.outdir}/graphmap2/bigwig" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*bigWig",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
        }
    }
    if (params.aligner == "minimap2") {
        process {
            withName: MINIMAP2_INDEX {
                publishDir = [
                    path: { "${params.outdir}/minimap2" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: MINIMAP2_ALIGN {
                publishDir = [
                    path: { "${params.outdir}/minimap2" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_VIEW_BAM {
                publishDir = [
                    path: { "${params.outdir}/minimap2" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_SORT {
                ext.prefix = { "${meta.id}.sorted" }
                publishDir = [
                    path: { "${params.outdir}/minimap2" },
                    mode: 'copy',
                    enabled: true,
                    pattern: "*.sorted.bam",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_INDEX {
                publishDir = [
                    path: { "${params.outdir}/minimap2" },
                    mode: 'copy',
                    enabled: true,
                    pattern: "*.bai",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_SORT_INDEX {
                publishDir = [
                    path: { "${params.outdir}/minimap2" },
                    mode: 'copy',
                    enabled: true,
                    pattern: "*.{sorted.bam,sorted.bam.bai}",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_STATS {
                publishDir = [
                    path: { "${params.outdir}/minimap2/samtools_stats" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_IDXSTATS {
                publishDir = [
                    path: { "${params.outdir}/minimap2/samtools_stats" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SAMTOOLS_FLAGSTAT {
                publishDir = [
                    path: { "${params.outdir}/minimap2/samtools_stats" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        if (!params.skip_bigbed) {
            process {
                withName: BEDTOOLS_BAMBED {
                    publishDir = [
                        path: { "${params.outdir}/minimap2/bigbed" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*.bed12",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
                withName: UCSC_BED12TOBIGBED {
                    publishDir = [
                        path: { "${params.outdir}/minimap2/bigbed" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*.bigBed",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
        }
        if (!params.skip_bigwig) {
            process {
                withName: BEDTOOLS_GENOMECOV {
                    publishDir = [
                        path: { "${params.outdir}/minimap2/bigwig" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*bedGraph",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
            process {
                withName: UCSC_BEDGRAPHTOBIGWIG {
                    publishDir = [
                        path: { "${params.outdir}/minimap2/bigwig" },
                        mode: 'copy',
                        enabled: true,
                        pattern: "*.bigWig",
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
        }
    }
}

if (params.call_variants) {
    if (params.variant_caller == 'medaka') {
        process {
            withName: MEDAKA_VARIANT {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/medaka_variant" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: MEDAKA_BGZIP_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/medaka_variant" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: MEDAKA_TABIX_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/medaka_variant" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
    if (params.variant_caller == 'deepvariant') {
        process {
            withName: DEEPVARIANT {
                ext.args         = { "--model_type WGS" }
                publishDir = [
                    path: { "${params.outdir}/variant_calling/deepvariant" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: DEEPVARIANT_TABIX_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/deepvariant" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: DEEPVARIANT_TABIX_GVCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/deepvariant" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
    if (params.variant_caller == 'pepper_margin_deepvariant') {
        process {
            withName: PEPPER_MARGIN_DEEPVARIANT {
                ext.args         = { "--ont_r9_guppy5_sup" }
                publishDir = [
                    path: { "${params.outdir}/variant_calling/margin_pepper_deepvariant" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
    if (params.structural_variant_caller == 'sniffles') {
        process {
            withName: SNIFFLES {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/sniffles" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: SNIFFLES_SORT_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/sniffles" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: SNIFFLES_TABIX_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/sniffles" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
    if (params.structural_variant_caller == 'cutesv') {
        process {
            withName: CUTESV {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/cutesv" },
                    mode: 'copy',
                    enabled: false,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: CUTESV_SORT_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/cutesv" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        process {
            withName: CUTESV_TABIX_VCF {
                publishDir = [
                    path: { "${params.outdir}/variant_calling/cutesv" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}

if (!params.skip_quantification) {
    if (params.quantification_method == "bambu") {
        process {
            withName: BAMBU {
                publishDir = [
                    path: { "${params.outdir}/bambu" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        if (!params.skip_differential_analysis) {
            process {
                withName: DESEQ2 {
                    publishDir = [
                        path: { "${params.outdir}/bambu/deseq2" },
                        mode: 'copy',
                        enabled: true,
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
                withName: DEXSEQ {
                    publishDir = [
                        path: { "${params.outdir}/bambu/dexseq" },
                        mode: 'copy',
                        enabled: true,
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
        }
    }
    if (params.quantification_method == "stringtie2") {
        process {
            withName: STRINGTIE2 {
                publishDir = [
                    path: { "${params.outdir}/stringtie2" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: SUBREAD_FEATURECOUNTS {
                publishDir = [
                    path: { "${params.outdir}/stringtie2/featureCounts" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
        if (!params.skip_differential_analysis) {
            process {
                withName: DESEQ2 {
                    publishDir = [
                        path: { "${params.outdir}/bambu/deseq2" },
                        mode: 'copy',
                        enabled: true,
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
                withName: DEXSEQ {
                    publishDir = [
                        path: { "${params.outdir}/bambu/dexseq" },
                        mode: 'copy',
                        enabled: true,
                        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                    ]
                }
            }
        }
    }
}

if (!params.skip_fusion_analysis) {
    process {
        withName: GET_JAFFAL_REF {
            publishDir = [
                path: { "${params.outdir}/jaffal" },
                mode: 'copy',
                enabled: false,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
        withName: UNTAR {
            publishDir = [
                path: { "${params.outdir}/jaffal" },
                mode: 'copy',
                enabled: false,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
        withName: JAFFAL {
            publishDir = [
                path: { "${params.outdir}/jaffal" },
                mode: 'copy',
                enabled: true,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }

}

if (!params.skip_modification_analysis) {
    process {
        withName: NANOPOLISH_INDEX_EVENTALIGN {
            publishDir = [
                path: { "${params.outdir}/rna_modifications/nanopolish" },
                mode: 'copy',
                enabled: true,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
    if (!params.skip_xpore) {
        process {
            withName: XPORE_DATAPREP {
                publishDir = [
                    path: { "${params.outdir}/rna_modifications/xpore/dataprep" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: XPORE_DIFFMOD {
                publishDir = [
                    path: { "${params.outdir}/rna_modifications/xpore/diffmod" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
    if (!params.skip_m6anet) {
        process {
            withName: M6ANET_DATAPREP {
                publishDir = [
                    path: { "${params.outdir}/rna_modifications/m6anet/dataprep" },
                    mode: 'copy',
                    enabled: true,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
            withName: M6ANET_INFERENCE {
                publishDir = [
                    path: { "${params.outdir}/rna_modifications/m6anet/inference" },
                    mode: 'copy',
                    enabled: true,
                    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.skip_alignment? '' : "/${params.aligner}"
                    ].join('') },
                mode: 'copy',
                enabled: true,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }

}
