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

//       Let modules.config to store module configurations that share both shortreads and longreads
process {

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

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

    withName: 'PORECHOP_PORECHOP' {
        ext.args = ''
        ext.prefix = { "${meta.id}.porechop" }
        publishDir = [
            path: { "${params.outdir}/trimming/longreads" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

    withName: 'CANU' {
        ext.args = {
            [ params.canu_args ? "${params.canu_args}" : '',
            "merylMemory=${task.memory.toGiga()}G",
            "merylThreads=$task.cpus",
            "hapThreads=${task.cpus}",
            "batMemory=${task.memory.toGiga()}G",
            "redMemory=${task.memory.toGiga()}G",
            "redThreads=${task.cpus}",
            "oeaMemory=${task.memory.toGiga()}G",
            "oeaThreads=${task.cpus}",
            "corMemory=${task.memory.toGiga()}G",
            "corThreads=${task.cpus}"
            ].join(' ').trim()
        }
        publishDir = [
            path: { "${params.outdir}/Canu" },
            mode: params.publish_dir_mode,
            pattern: "*.{contigs.fasta.gz,report}",
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'MINIMAP2_ALIGN' {
        ext.args = '-x ava-ont'
    }

    withName: 'MINIMAP2_CONSENSUS' {
        ext.args = '-x map-ont'
    }

    withName: 'MINIMAP2_POLISH' {
        ext.args = '-x map-ont'
    }

    withName: 'MINIASM' {
        ext.args = ''
        publishDir = [
            path: { "${params.outdir}/Miniasm" },
            mode: params.publish_dir_mode,
            pattern: '*.fasta.gz',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'DRAGONFLYE' {
        ext.args = {
            if ( meta.gsize && !meta.gsize.equals('NA') ){
                if ( !params.dragonflye_args.contains("--gsize") ) {
                    "--gsize ${meta.gsize} ${params.dragonflye_args}"
                } else {
                    params.dragonflye_args ?: ''
                }
            } else {
                params.dragonflye_args ?: ''
            }
        }
        publishDir = [
            path: { "${params.outdir}/Dragonflye" },
            mode: params.publish_dir_mode,
            pattern: "*.{fa,log}",
            saveAs: { filename ->
                if (filename.equals('versions.yml')) {
                    null
                } else if (filename.endsWith('.log')) {
                    "${meta.id}.${filename}"
                } else {
                    filename
                }
            }
        ]
    }

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

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

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

    withName: 'KRAKEN2' {
        ext.args = ''
        publishDir = [
            path: { "${params.outdir}/Kraken2" },
            mode: params.publish_dir_mode,
            pattern: "*report.txt",
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'KRAKEN2_LONG' {
        ext.args = ''
        publishDir = [
            path: { "${params.outdir}/kraken2" },
            mode: params.publish_dir_mode,
            pattern: "*report.txt",
            saveAs: { filename ->
                if (filename.equals('versions.yml')) {
                    null
                } else {
                    "${filename.replaceFirst('\\..+$', '')}_longreads${filename.substring(filename.lastIndexOf('.'))}"
                }
            }
        ]
    }

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

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

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

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

if (!params.skip_fastqc) {
    process {
        withName: '.*:.*:FASTQ_TRIM_FASTP_FASTQC:FASTQC_RAW' {
            ext.args = '--quiet'
            publishDir = [
                path: { "${params.outdir}/FastQC/raw" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}
if (!params.skip_fastp) {
    process {
        withName: '.*:.*:FASTQ_TRIM_FASTP_FASTQC:FASTP' {
            ext.args = ''
            publishDir = [
                [
                    path: { "${params.outdir}/trimming/shortreads" },
                    mode: params.publish_dir_mode,
                    pattern: "*.fastp.fastq.gz",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ],
                [
                    path: { "${params.outdir}/trimming/shortreads/json_html" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{json,html}"
                ],
                [
                    path: { "${params.outdir}/trimming/shortreads/log" },
                    mode: params.publish_dir_mode,
                    pattern: "*.log"
                ],
                [
                    path: { "${params.outdir}/trimming/shortreads" },
                    mode: params.publish_dir_mode,
                    pattern: "*.fail.fastq.gz",
                    enabled: params.save_trimmed_fail
                ]
            ]
        }
    }
    if (!params.skip_fastqc) {
        process {
            withName: '.*:.*:FASTQ_TRIM_FASTP_FASTQC:FASTQC_TRIM' {
                ext.args = '--quiet'
                publishDir = [
                    path: { "${params.outdir}/FastQC/trim" },
                    mode: params.publish_dir_mode,
                    pattern: "*.{json,html}",
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
                ]
            }
        }
    }
}

if (params.annotation_tool == 'bakta') {
    if (params.baktadb_download == true) {
        process {
            withName: '.*:.*:BAKTA_DBDOWNLOAD_RUN:BAKTA_BAKTADBDOWNLOAD' {
                ext.args = params.baktadb_download_args ? params.baktadb_download_args : ''
            }
        }
    }
    process {
        withName: '.*:.*:BAKTA_DBDOWNLOAD_RUN:BAKTA_BAKTA' {
            ext.args    = ''
            publishDir  = [
                path: { "${params.outdir}/Bakta/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}
