/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: 'GFFREAD' {
        ext.args = '-y'
        publishDir = [
            path: { "${params.outdir}/gffread" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'ORTHOFINDER_V2_CAFE' {
        publishDir = [
            path: { "${params.outdir}/orthofinder_cafe" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'ORTHOFINDER_CAFE' {
        cpus = { 8 }
        memory = 100.GB
        errorStrategy = 'retry'
        maxRetries    = 3
        ext.args = {
            def args = []
            if (params.orthofinder_method)   args << "-M ${params.orthofinder_method}"
            if (params.orthofinder_msa_prog) args << "-A ${params.orthofinder_msa_prog}"
            if (params.orthofinder_search)   args << "-S ${params.orthofinder_search}"
            if (params.orthofinder_tree)     args << "-T ${params.orthofinder_tree}"
            return args.join(' ')
        }
        publishDir = [
            path: { "${params.outdir}/orthofinder_cafe" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_RUN' {
        ext.args = { meta.id == 'gamma'            ? '-k 3'    :
                     meta.id == 'gamma_per_family' ? '-p -k 3' : '' }
        publishDir = [
            path: { "${params.outdir}/cafe/${meta.id}" },
            mode: params.publish_dir_mode
        ] 

    }

    withName: 'CAFE_PREP' {
        cpus   = { 8 }
        memory = { 24.GB }
        time   = { 24.h  }
        publishDir = [
            path: { "${params.outdir}/cafe/base" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_MODEL_COMPARE' {
        publishDir = [
            [
                path: { "${params.outdir}/cafe/model_comparison" },
                pattern: "*.{tsv,txt}",
                mode: params.publish_dir_mode
            ],
            [
                path: { "${params.outdir}/cafe/best" },
                pattern: "best_cafe_results",
                mode: params.publish_dir_mode,
                saveAs: { "." }
            ]
        ]
    }

    withName: 'CAFE_RUN_LARGE' {
        cpus   = { 8 }
        memory = { 24.GB }
        time   = { 24.h  }
        publishDir = [
            path: { "${params.outdir}/cafe/large_families" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_GO_RUN' {
        publishDir = [
            path: { "${params.outdir}/cafe_go/${params.go_algo}_cutoff${params.go_cutoff}_type${params.go_type}" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CHROMO_GO' {
        publishDir = [
            path: { "${params.outdir}/chromo_go/${params.go_algo}_cutoff${params.go_cutoff}_type${params.go_type}" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_PLOT' {
        publishDir = [
            path: { "${params.outdir}/cafe_plot" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_PLOT_LARGE' {
        publishDir = [
            path: { "${params.outdir}/cafe_plot_large" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_GO_PREP_LARGE' {
        publishDir = [
            path: { "${params.outdir}/cafe_go_large/${params.go_algo}_cutoff${params.go_cutoff}_type${params.go_type}" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'CAFE_GO_RUN_LARGE' {
        publishDir = [
            path: { "${params.outdir}/cafe_go_large/${params.go_algo}_cutoff${params.go_cutoff}_type${params.go_type}" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'BUSCO_BUSCO' {
        publishDir = [
            path: { "${params.outdir}/busco" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'AGAT_SPKEEPLONGESTISOFORM' {
        publishDir = [
            path: { "${params.outdir}/agat" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'AGAT_SPSTATISTICS' {
        publishDir = [
            path: { "${params.outdir}/agat" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'QUAST' {
        publishDir = [
            path: { "${params.outdir}/quast" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'NCBIGENOMEDOWNLOAD' {
        ext.args          = "--formats fasta,gff -N"
        errorStrategy     = { task.attempt <= 2 ? 'retry' : 'finish' }
        maxRetries        = 2
        maxForks          = 10   // only 10 species downloading at a time
        publishDir = [
            path: { "${params.outdir}/ncbigenomedownload" },
            mode: params.publish_dir_mode
        ]
    }

withName: 'EGGNOGMAPPER' {
    publishDir = [
        path: { "${params.outdir}/eggnogmapper" },
        mode: params.publish_dir_mode
    ]
    ext.args = {
        def args = []

        if (params.eggnog_target_taxa)
            args << "--target_taxa '${params.eggnog_target_taxa}'"

        if (params.eggnog_tax_scope)
            args << "--tax_scope '${params.eggnog_tax_scope}'"

        if (params.eggnog_evalue)
            args << "--evalue ${params.eggnog_evalue}"

        if (params.eggnog_score)
            args << "--score ${params.eggnog_score}"

        if (params.eggnog_pident)
            args << "--pident ${params.eggnog_pident}"

        if (params.eggnog_query_cover)
            args << "--query_cover ${params.eggnog_query_cover}"

        if (params.eggnog_subject_cover)
            args << "--subject_cover ${params.eggnog_subject_cover}"

        return args.join(' ')
    }
}
    
    withName: 'EGGNOG_TO_GO' {
        publishDir = [
            path: { "${params.outdir}/eggnogmapper/go_files" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'OG_ANNOTATION_SUMMARY' {
        publishDir = [
            path: { "${params.outdir}/eggnogmapper" },
            mode: params.publish_dir_mode
        ]
    }

    withName: 'SUMMARIZE_CHROMO_GO' {
        publishDir = [
            path: { "${params.outdir}/chromo_go/${params.go_algo}_cutoff${params.go_cutoff}_type${params.go_type}/summary" },
            mode: params.publish_dir_mode
        ]
    }
}
