/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: HMMER_HMMBUILD {
        ext.prefix = { "${meta.id}.ref" }
    }

    withName: HMMER_UNALIGNREF {
        ext.prefix = { "${meta.id}.ref.unaligned" }
        ext.args   = "--gapsym=- afa"
        ext.postprocessing = '| sed "/^>/!s/-//g"'
    }

    withName: HMMER_HMMALIGNREF {
        ext.prefix = { "${meta.id}.ref.hmmalign" }
    }

    withName: HMMER_HMMALIGNQUERY {
        ext.prefix = { "${meta.id}.query.hmmalign" }
    }

    withName: 'HMMER_MASK.*' {
        ext.args   = '--rf-is-mask'
    }

    withName: 'HMMER_MASKQUERY.*' {
        ext.prefix = { "${meta.id}.query.hmmalign" }
    }

    withName: 'HMMER_MASKREF.*' {
        ext.prefix = { "${meta.id}.ref.hmmalign" }
    }

    withName: 'HMMER_AFAFORMATQUERY.*' {
        ext.prefix = { "${meta.id}.query.hmmalign.masked" }
        ext.args   = 'afa'
    }

    withName: 'HMMER_AFAFORMATREF.*' {
        ext.prefix = { "${meta.id}.ref.hmmalign.masked" }
        ext.args   = 'afa'
    }

    withName: 'MAFFT' {
        ext.args = '--keeplength'
    }

    withName: 'EPANG_PLACE' {
        ext.args   = { "--model ${meta.model}" }
    }

    withName: 'GAPPA_GRAFT' {
        ext.prefix = { "${meta.id}.graft" }
    }

    withName: 'GAPPA_ASSIGN' {
        ext.prefix = { "${meta.id}.taxonomy" }
        ext.args   = "--per-query-results --krona --sativa"
        ext.when   = { taxonomy }
    }

    withName: 'GAPPA_HEATTREE' {
        ext.prefix = { "${meta.id}.heattree" }
        ext.args = "--write-nexus-tree --write-phyloxml-tree --write-svg-tree"
    }

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

}
