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

    withName: FASTQC {
        ext.args = '--quiet'
    }

    withName: 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,zip}"
            ],
            [
                path: { "${params.outdir}/trimgalore" },
                mode: params.publish_dir_mode,
                pattern: "*.txt"
            ]
        ]
    }

    withName: SEQTK_MERGEPE {
        ext.prefix = { "${meta.id}.processed" }
    }

    withName: BBMAP_BBNORM {
        ext.args = { [
            "target=${params.bbnorm_target}",
            "min=${params.bbnorm_min}",
        ].join(' ').trim() }
        publishDir = [
            [
                path : { "${params.outdir}/bbmap/bbnorm" },
                mode: params.publish_dir_mode,
                pattern: "*.log"
            ],
            [
                path : { "${params.outdir}/bbmap/bbnorm/"},
                mode: params.publish_dir_mode,
                pattern: "*.fastq.gz",
                enabled: params.save_bbnorm_fastq
            ]
        ]
    }

    withName: BBMAP_BBDUK {
        publishDir = [
            [
                path : { "${params.outdir}/bbmap/bbduk/"},
                pattern: '*.fastq.gz',
                mode: params.publish_dir_mode,
                enabled: params.save_bbduk_fastq
            ],
            [
                path : { "${params.outdir}/bbmap/bbduk/"},
                pattern: '*.bbduk.log',
                mode: params.publish_dir_mode
            ]
        ]
    }

    withName: MEGAHIT_INTERLEAVED {
        publishDir = [
            path: { "${params.outdir}/megahit" },
            mode: params.publish_dir_mode,
            pattern: '**/{megahit_assembly.contigs.fa.gz,megahit_assembly.log}'
        ]
    }

    withName: WRITESPADESYAML {
        publishDir = [
            path: { "${params.outdir}/spades" },
            mode: params.publish_dir_mode,
            pattern: '*.yaml'
        ]
    }

    withName: SPADES {
        ext.args = { "--${params.spades_flavor}" }
        publishDir = [
            path: { "${params.outdir}/spades" },
            mode: params.publish_dir_mode,
            pattern: '*.{gz,log}'
        ]
    }

    withName: FORMATSPADES {
        publishDir = [
            path: { "${params.outdir}/spades" },
            mode: params.publish_dir_mode,
            pattern: '*.fa.gz',
            enabled: params.save_formatspades
        ]
    }

    withName: SEQTK_SEQ_CONTIG_FILTER {
        ext.args = { "-L ${params.min_contig_length}" }
    }

    withName: BBMAP_ALIGN {
        ext.args = { "trimreaddescriptions=t pigz=t minid=${params.bbmap_minid}" }
        publishDir = [
            [
                path: { "${params.outdir}/bbmap/bbmap/" },
                mode: params.publish_dir_mode,
                pattern: "*.bam",
                enabled: params.save_bam
            ],
            [
                path: { "${params.outdir}/bbmap/bbmap" },
                mode: params.publish_dir_mode,
                pattern: "*.log"
            ]
        ]
    }

    withName: SAMTOOLS_SORT {
        ext.prefix = { "${meta.id}.sorted" }
    }

    withName: ".*SAMTOOLS.*" {
        publishDir = [
            [
                pattern: "*.bam",
                path: { "${params.outdir}/samtools/" },
                mode: params.publish_dir_mode

            ],
            [
                pattern: "*.bam.bai",
                path: { "${params.outdir}/samtools/" },
                mode: params.publish_dir_mode
            ],
            [
                pattern: "*.{flagstat,idxstats}",
                path: { "${params.outdir}/samtools/" },
                mode: params.publish_dir_mode,
                enabled: params.save_samtools,
                saveAs: { filename -> "${params.assembler ?: params.user_assembly_name}.${filename}" }
            ]
        ]
    }

    withName: PRODIGAL {
        ext.args = { params.prodigal_trainingfile ? "-t $params.prodigal_trainingfile" : "-p meta" }
        publishDir = [
            path: { "${params.outdir}/prodigal" },
            mode: params.publish_dir_mode,
            pattern: "*.gz"
        ]
    }

    withName: FORMAT_PRODIGAL_GFF {
        publishDir = [
            path: { "${params.outdir}/prodigal" },
            mode: params.publish_dir_mode,
            pattern: "*.gz"
        ]
    }

    withName: TRANSDECODER {
        publishDir = [
            path: { "${params.outdir}/transdecoder" },
            mode: params.publish_dir_mode,
            pattern: "*transdecoder/*.transdecoder.{bed,cds,gff3,pep}",
            saveAs: { filename -> filename },
            enabled: false
        ]
    }

    withName: 'PIGZ_TRANSDECODER_.*' {
        publishDir = [
            path: { "${params.outdir}/transdecoder" },
            mode: params.publish_dir_mode,
            pattern: "*.transdecoder.{bed,cds,gff3,pep}.gz",
            saveAs: { filename -> filename }
        ]
    }

    withName: 'PROKKA' {
        ext.args = '--prodigal --metagenome'
    }

    withName: 'FAA_CAT' {
        ext.prefix = 'prokka.faa.gz'
        publishDir = [
            path: { "${params.outdir}/prokka" },
            mode: params.publish_dir_mode,
            pattern: "*.gz"
        ]
    }

    withName: 'FFN_CAT' {
        ext.prefix = 'prokka.ffn.gz'
        publishDir = [
            path: { "${params.outdir}/prokka" },
            mode: params.publish_dir_mode,
            pattern: "*.gz"
        ]
    }

    withName: 'GFF_CAT' {
        ext.prefix = 'prokka.gff.gz'
        publishDir = [
            path: { "${params.outdir}/prokka" },
            mode: params.publish_dir_mode,
            pattern: "*.gz"
        ]
    }

    withName: PROKKAGFF2TSV {
        publishDir = [
            path: { "${params.outdir}/summary_tables" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: "*.prokka-annotations.tsv.gz"
        ]
    }

    withName: '.*:FEATURECOUNTS_CDS' {
        ext.args = '-g ID -t CDS -F gtf'
        publishDir = [
            path: { "${params.outdir}/featurecounts" },
            mode: params.publish_dir_mode,
            pattern: "*.featureCounts.*"
        ]
    }

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

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

    withName: 'SEQTK_HMMHITFAAS' {
        publishDir = [
            path: { "${params.outdir}/hmmer" },
            mode: params.publish_dir_mode,
            pattern: "hits/*.faa.gz"
        ]
    }

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

    withName: EGGNOG_DOWNLOAD {
        storeDir = { "${params.eggnog_dbpath}" }
    }

    withName: EGGNOG_MAPPER {
        publishDir = [
            [
                path: { "${params.outdir}/eggnog" },
                mode: params.publish_dir_mode,
                pattern: "*.emapper.*"
            ],
            [
                path: { "${params.outdir}/summary_tables" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*.emapper.tsv.gz"
            ]
        ]
    }

    withName: KOFAMSCAN_DOWNLOAD {
        storeDir = { "${params.kofam_dir}" }
    }

    withName: KOFAMSCAN_SCAN {
        publishDir = [
            [
                path: { "${params.outdir}/summary_tables/" },
                pattern: "kofamscan.tsv.gz",
                mode: params.publish_dir_mode,
                saveAs: {
                    filename ->
                        filename.equals('versions.yml') ?
                            null :
                            "${params.assembler ?: params.user_assembly_name}.${params.orf_caller ?: params.user_orfs_name}.${filename}"
                }
            ],
            [
                path: { "${params.outdir}/kofamscan/" },
                pattern: "kofamscan_output.tsv.gz",
                mode: params.publish_dir_mode,
                saveAs: { filename -> "${params.assembler ?: params.user_assembly_name}.${params.orf_caller ?: params.user_orfs_name}.${filename}" }
            ]
        ]
    }

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

    withName: EUKULELE_SEARCH {
        publishDir = [
            path: { "${params.outdir}/eukulele" },
            mode: params.publish_dir_mode
        ]
        ext.args = { params.eukulele_method ? "-m ${params.eukulele_method}" : '' }
    }

    withName: DIAMOND_TAXONOMY {
        publishDir = [
            path: { "${params.outdir}/diamond_taxonomy/" },
            mode: params.publish_dir_mode,
            enabled: true
        ]
        ext.args = { "--top ${params.diamond_top} --compress 1" }
    }

    withName: TAXONKIT_LINEAGE {
        publishDir = [
            enabled: false
        ]
        ext.args = { "--taxid-field 2" }
    }

    withName: PIGZ_DIAMOND_LINEAGE {
        publishDir = [
            path: { "${params.outdir}/diamond_taxonomy/" },
            mode: params.publish_dir_mode,
            enabled: true
        ]
    }

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

    withName: FORMAT_DIAMOND_TAX_TAXDUMP {
        publishDir = [
            path: { "${params.outdir}/summary_tables/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
        errorStrategy: 'ignore'
    }

    withName: FORMAT_EUKULELE_TAX {
        publishDir = [
            path: { "${params.outdir}/summary_tables" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv.gz',
            saveAs: {
                filename ->
                    filename.equals('versions.yml') ?
                        null :
                        "${params.assembler ?: params.user_assembly_name}.${params.orf_caller ?: params.user_orfs_name}.${params.eukulele_db ?: 'userdb'}.eukulele.taxonomy.tsv.gz"
            }
        ]
    }

    withName: SUM_EUKULELE_TAXONOMY {
        ext.prefix = { "${params.eukulele_db ?: 'userdb'}" }
    }

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

    withName: TRANSRATE {
        publishDir = [
            path: { "${params.outdir}/transrate" },
            mode: params.publish_dir_mode,
            pattern: "*"
        ]
    }

    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 }
        ]
    }

    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 }
        ]
    }

}
