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

    withName: DOWNLOAD_MGNIFY_GENOMES_REFERENCE_DBS {
        publishDir = [
            path: { "${params.reference_dbs}/${params.biome}" },
            mode: params.publish_dir_mode,
        ]
    }

    withName: DOWNLOAD_DRAM_DB {
        publishDir = [
            path: { "${params.reference_dbs}/" },
            mode: params.publish_dir_mode,
        ]
    }

    withName: DOWNLOAD_HUMAN_PHIX_BWAMEM2_INDEX {
        publishDir = [
            path: { "${params.decontamination_indexes}/" },
            mode: params.publish_dir_mode,
        ]
    }

    withName: FASTP {
        publishDir = [
            path: { "${params.outdir}/quality_control/fastp" },
            mode: params.publish_dir_mode,
        ]
    }

    withName: FASTQC {
        ext.args   = '--quiet'
        publishDir = [
            path: { "${params.outdir}/quality_control/decont_fastqc" },
            mode: params.publish_dir_mode,
            pattern: '*.html',
        ]
    }

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

    withName: SOURMASH_GATHER {
        publishDir = [
            path: { "${params.outdir}/mapping/sourmash" },
            mode: params.publish_dir_mode,
            pattern: '*.csv.gz',
        ]
    }

    withName: ALIGN_BWAMEM2 {
        cpus       = { 12 * task.attempt }
        memory     = {
            def size = meta.single_end ? reads.size() : reads[0].size()
            // Files bigger than 700.MB require more memory
            size > 700000000 ? 90.GB : 72.GB
        }
        time       = { 16.h * task.attempt }
        publishDir = [
            path: { "${params.outdir}/mapping/bwamem2" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: POSTPROC_SOURMASHTAXO {
        publishDir = [
            path: { "${params.outdir}/taxonomy_tables" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: POSTPROC_BWATAXO {
        publishDir = [
            path: { "${params.outdir}/taxonomy_tables" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: POSTPROC_FUNCTIONSPRED {
        cpus    = 1
        memory = { 24.GB * task.attempt }
        publishDir = [
            path: { "${params.outdir}/function_tables" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: DRAM_DISTILL {
        cpus   = 1
        memory = { 24.GB * task.attempt }
        publishDir = [
            [
                path: { "${params.outdir}/dram_results" },
                mode: params.publish_dir_mode,
                pattern: '*_species_*',
            ],
            [
                path: { "${params.outdir}/integrated_annotation" },
                mode: params.publish_dir_mode,
                pattern: '*_community_*',
            ],
        ]
    }

    withName: KEGG_COMPLETENESS {
        publishDir = [
            path: { "${params.outdir}/kegg_completeness" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: KEGG_SPECIES {
        publishDir = [
            path: { "${params.outdir}/kegg_completeness" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: POSTPROC_INTEGRATOR {
        publishDir = [
            path: { "${params.outdir}/integrated_annotation" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv',
        ]
    }

    withName: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml',
        ]
    }
}
