/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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 }
    ]

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

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

    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: GUNZIP {
        publishDir = [
            enabled: false
        ]
    }

    withName: BIOAWK {
        ext.args = "-c fastx \'{print \">\" \$name ORS length(\$seq)}\'"
        publishDir = [
            path: { "${params.outdir}/" },
            mode: params.publish_dir_mode,
            enabled: false,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${meta.it}.fa" }
    }

    withName: PROKKA {
        publishDir = [
            path: { "${params.outdir}/annotation/prokka/}" },
            mode: params.publish_dir_mode,
            enabled: params.save_annotations,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            "--kingdom ${params.annotation_prokka_kingdom}",
            "--gcode ${params.annotation_prokka_gcode}",
            "--mincontiglen ${params.annotation_prokka_mincontiglen}",
            "--evalue ${params.annotation_prokka_evalue}",
            "--coverage ${params.annotation_prokka_coverage}",
            "--centre ${params.annotation_prokka_centre}",
            "--locustag ${params.annotation_prokka_locustag}",
            params.annotation_prokka_singlemode ? '' : '--metagenome' ,
            params.annotation_prokka_cdsrnaolap ? '--cdsrnaolap' : '',
            params.annotation_prokka_rawproduct ? '--rawproduct' : '',
            params.annotation_prokka_rnammer ? '--rnammer' : '',
            params.annotation_prokka_compliant ? '--compliant' : '',
            params.annotation_prokka_addgenes ? '--addgenes' : ''
        ].join(' ').trim()
    }

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

    withName: BAKTA_BAKTA {
        publishDir = [
            path: { "${params.outdir}/annotation/bakta" },
            mode: params.publish_dir_mode,
            enabled: params.save_annotations,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            "--min-contig-length ${params.annotation_bakta_mincontiglen}",
            "--translation-table ${params.annotation_bakta_translationtable}",
            "--gram ${params.annotation_bakta_gram}",
            params.annotation_bakta_complete ? '--complete' : '',
            params.annotation_bakta_renamecontigheaders ? '' : '--keep-contig-headers',
            params.annotation_bakta_compliant ? '--compliant' : '',
            params.annotation_bakta_skiptrna ? '--skip-trna' : '',
            params.annotation_bakta_skiptmrna ? '--skip-tmrna' : '',
            params.annotation_bakta_skiprrna ? '--skip-rrna' : '',
            params.annotation_bakta_skipncrna ? '--skip-ncrna' : '',
            params.annotation_bakta_skipncrnaregion ? '--skip-ncrna-region' : '',
            params.annotation_bakta_skipcrispr ? '--skip-crispr' : '',
            params.annotation_bakta_skipcds ? '--skip-cds' : '',
            params.annotation_bakta_skippseudo ? '--skip-pseudo' : '',
            params.annotation_bakta_skipsorf ? '--skip-sorf' : '',
            params.annotation_bakta_skipgap ? '--skip-gap' : '',
            params.annotation_bakta_skipori ? '--skip-ori' : ''
        ].join(' ').trim()
    }

    withName: PRODIGAL_GFF {
        publishDir = [
            path: { "${params.outdir}/annotation/prodigal/${meta.id}" },
            mode: params.publish_dir_mode,
            enabled: params.save_annotations,
            pattern: "*.{faa,fna,gff}",
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            params.annotation_prodigal_singlemode ? "-p single" : "-p meta",
            params.annotation_prodigal_closed ? "-c" : "",
            params.annotation_prodigal_forcenonsd ? "-n" : "",
            "-g ${params.annotation_prodigal_transtable}"
        ].join(' ').trim()
    }

        withName: PRODIGAL_GBK {
        publishDir = [
            path: { "${params.outdir}/annotation/prodigal/${meta.id}" },
            mode: params.publish_dir_mode,
            enabled: params.save_annotations,
            pattern: "*.gbk",
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            params.annotation_prodigal_singlemode ? "-p single" : "-p meta",
            params.annotation_prodigal_closed ? "-c" : "",
            params.annotation_prodigal_forcenonsd ? "-n" : "",
            "-g ${params.annotation_prodigal_transtable}"
        ].join(' ').trim()
    }

    withName: ABRICATE_RUN {
        publishDir = [
            path: { "${params.outdir}/arg/abricate/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args =  [
            "--db ${params.arg_abricate_db}",
            "--minid ${params.arg_abricate_minid}",
            "--mincov ${params.arg_abricate_mincov}"
        ].join(' ').trim()
    }

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

    withName: AMRFINDERPLUS_RUN {
        publishDir = [
            path: { "${params.outdir}/arg/amrfinderplus/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            "--ident_min ${params.arg_amrfinderplus_identmin}",
            "--coverage_min ${params.arg_amrfinderplus_coveragemin}",
            "--translation_table ${params.arg_amrfinderplus_translationtable}",
            params.arg_amrfinderplus_plus ? '--plus' : '',
            params.arg_amrfinderplus_name ? '--name ${meta.id}' : ''
        ].join(' ').trim()
    }

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

    withName: DEEPARG_PREDICT {
        publishDir = [
            path: { "${params.outdir}/arg/deeparg/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            "--type prot",
            "--min-prob ${params.arg_deeparg_minprob}",
            "--arg-alignment-identity ${params.arg_deeparg_alignmentidentity}",
            "--arg-alignment-evalue ${params.arg_deeparg_alignmentevalue}",
            "--arg-alignment-overlap ${params.arg_deeparg_alignmentoverlap}",
            "--arg-num-alignments-per-entry ${params.arg_deeparg_numalignmentsperentry}"
        ].join(' ').trim()
    }

    withName: FARGENE {
        publishDir = [
            [
                path: { "${params.outdir}/arg/fargene/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*.log"
            ],
            [
                path: { "${params.outdir}/arg/fargene/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*/results_summary.txt"
            ],
            [
                path: { "${params.outdir}/arg/fargene/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*/{predictedGenes,retrievedFragments}/*"
            ],
            [
                path: { "${params.outdir}/arg/fargene/${meta.id}/" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*/{hmmsearchresults,tmpdir}/*",
                enabled: params.arg_fargene_savetmpfiles
            ]
        ]
        ext.prefix = { "${meta.hmm_class}" }
        ext.args =  { "--hmm-model ${params.arg_fargene_hmmmodel} --logfile ${meta.id}-${meta.hmm_class}.log --min-orf-length ${params.arg_fargene_minorflength} --score ${params.arg_fargene_score} --translation-format ${params.arg_fargene_translationformat}" }
        ext.args = params.arg_fargene_orffinder ? '--orf-finder' : ''
    }

    withName: RGI_MAIN {
        publishDir = [
            [
                path: { "${params.outdir}/arg/rgi/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*.{json,txt}"
                ],
            [
                path: { "${params.outdir}/arg/rgi/${meta.id}/" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                pattern: "*temp*",
                enabled: params.arg_rgi_savetmpfiles
            ]
        ]
        ext.args = [
            "--alignment_tool ${params.arg_rgi_alignmenttool}",
            "--data ${params.arg_rgi_data}",
            params.arg_rgi_includeloose ? '--include_loose' : '',
            params.arg_rgi_excludenudge ? '--exclude_nudge' : '',
            params.arg_rgi_lowquality ? '--low_quality' : ''
        ].join(' ').trim()
    }

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

    withName: AMPLIFY_PREDICT {
        publishDir = [
            path: { "${params.outdir}/amp/amplify/${meta.id}/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: AMP_HMMER_HMMSEARCH {
        label = { "${meta.id}_${meta.hmm_id}" }
        publishDir = [
            path: { "${params.outdir}/amp/hmmer_hmmsearch/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${meta.id}_${meta.hmm_id}" }
    }

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

    withName: BGC_HMMER_HMMSEARCH {
        label = { "${meta.id}_${meta.hmm_id}" }
        publishDir = [
            path: { "${params.outdir}/bgc/hmmer_hmmsearch/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${meta.id}_${meta.hmm_id}" }
    }

    withName: ANTISMASH_ANTISMASHLITE {
        publishDir = [
            path: { "${params.outdir}/bgc/antismash" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args   = [
            params.bgc_antismash_cbgeneral ? '--cb-general' : '',
            params.bgc_antismash_cbknownclusters ? '--cb-knownclusters' : '',
            params.bgc_antismash_smcogtrees ? '--smcog-trees' : '',
            params.bgc_antismash_ccmibig ? '--cc-mibig' : '',
            params.bgc_antismash_cbsubclusters ? '--cb-subclusters' : '',
            "--allow-long-headers",
            "--minlength ${params.bgc_antismash_contigminlength}",
            "--hmmdetection-strictness ${params.bgc_antismash_hmmdetectionstrictness}",
            "--taxon ${params.bgc_antismash_taxon}",
        ].join(' ').trim()
    }

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

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

    withName: DEEPBGC_PIPELINE {
        publishDir = [
            path: { "${params.outdir}/bgc/deepbgc/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            "--score ${params.bgc_deepbgc_score}",
            params.bgc_deepbgc_prodigalsinglemode ? '' : '--prodigal-meta-mode',
            "--merge-max-protein-gap ${params.bgc_deepbgc_mergemaxproteingap}",
            "--merge-max-nucl-gap ${params.bgc_deepbgc_mergemaxnuclgap}",
            "--min-nucl ${params.bgc_deepbgc_minnucl}",
            "--min-proteins ${params.bgc_deepbgc_minproteins}",
            "--min-domains ${params.bgc_deepbgc_mindomains}",
            "--min-bio-domains ${params.bgc_deepbgc_minbiodomains}",
            "--classifier-score ${params.bgc_deepbgc_classifierscore}"
        ].join(' ').trim()
    }

    withName: GECCO_RUN {
        publishDir = [
            path: { "${params.outdir}/bgc/gecco/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = [
            "--cds ${params.bgc_gecco_cds}",
            "--threshold ${params.bgc_gecco_threshold}",
            "--p-filter ${params.bgc_gecco_pfilter}",
            "--edge-distance ${params.bgc_gecco_edgedistance}",
            params.bgc_gecco_mask ? '--mask' : ''
        ].join(' ').trim()
    }

    withName: HAMRONIZATION_ABRICATE {
        publishDir = [
            path: { "${params.outdir}/arg/hamronization/abricate" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${report}.abricate" }
    }

    withName: HAMRONIZATION_AMRFINDERPLUS {
        publishDir = [
            path: { "${params.outdir}/arg/hamronization/amrfinderplus" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${report}.amrfinderplus" }
    }

    withName: HAMRONIZATION_DEEPARG {
        publishDir = [
            path: { "${params.outdir}/arg/hamronization/deeparg" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${report}.deeparg" }
    }

    withName: HAMRONIZATION_RGI {
        publishDir = [
            path: { "${params.outdir}/arg/hamronization/rgi" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${report}.rgi" }
    }

    withName: HAMRONIZATION_FARGENE {
        publishDir = [
            path: { "${params.outdir}/arg/hamronization/fargene" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${meta.id}_${report}.fargene" }
    }

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

    withName: AMPCOMBI {
        publishDir = [
            path: { "${params.outdir}/reports/ampcombi" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        // Have to use a custom `ext` due to deep nested quotes
        ext.tooldict = '{"ampir":"ampir.tsv", "amplify":".tsv", "macrel":".prediction", "neubi":"neubi.fasta", "hmmer_hmmsearch":".txt"}'
        ext.args =  "--tooldict '${ext.tooldict}' --cutoff ${params.amp_ampcombi_cutoff}"
    }

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