/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.
        ext.when            = Conditional clause
----------------------------------------------------------------------------------------
*/

//
// Score and rank structural variants
//

process {
    if (!params.skip_sv_annotation) {
        withName: '.*RANK_VARIANTS_SV:.*' {
            publishDir = [
                enabled: false
            ]
        }

        withName: '.*RANK_VARIANTS_SV:GENMOD_ANNOTATE' {
            ext.prefix = { "${meta.id}_sv_genmod_annotate_${meta.set}" }
            ext.args = { [
                '--annotate_regions',
                params.genome.equals('GRCh37') ? '--genome-build 37' : '--genome-build 38'
                ].join(' ') }
        }

        withName: '.*RANK_VARIANTS_SV:GENMOD_MODELS' {
            ext.prefix = { "${meta.id}_sv_genmod_models_${meta.set}" }
            ext.args = " --whole_gene "
        }

        withName: '.*RANK_VARIANTS_SV:GENMOD_SCORE' {
            ext.prefix = { "${meta.id}_sv_genmod_score_${meta.set}" }
            ext.args = " --rank_results "
        }

        withName: '.*RANK_VARIANTS_SV:GENMOD_COMPOUND' {
            ext.prefix = { "${meta.id}_sv_genmod_compound_${meta.set}" }
        }

        withName: '.*RANK_VARIANTS_SV:BCFTOOLS_SORT' {
            ext.args = "--output-type z"
            ext.prefix = { "${meta.id}_sv_ranked_${meta.set}" }
            publishDir = [
                path: { "${params.outdir}/rank_and_filter" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

        withName: '.*RANK_VARIANTS_SV:TABIX_BGZIP' {
            ext.when = false
        }

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

//
// Score and rank SNVs
//

process {
    if (!params.skip_snv_annotation) {
        withName: '.*RANK_VARIANTS_SNV:.*' {
            ext.when = !params.skip_snv_annotation
            publishDir = [
                enabled: false
            ]
        }

        withName: '.*RANK_VARIANTS_SNV:GENMOD_ANNOTATE' {
            ext.prefix = { "${meta.id}_snv_genmod_annotate_${meta.set}" }
            ext.args = { [
                '--annotate_regions',
                params.genome.equals('GRCh37') ? '--genome-build 37' : '--genome-build 38'
                ].join(' ') }
        }

        withName: '.*RANK_VARIANTS_SNV:GENMOD_MODELS' {
            ext.prefix = { "${meta.id}_snv_genmod_models_${meta.set}" }
            ext.args = " --whole_gene "
        }

        withName: '.*RANK_VARIANTS_SNV:GENMOD_SCORE' {
            ext.prefix = { "${meta.id}_snv_genmod_score_${meta.set}" }
            ext.args = " --rank_results "
        }

        withName: '.*RANK_VARIANTS_SNV:GENMOD_COMPOUND' {
            ext.prefix = { "${meta.id}_snv_genmod_compound_${meta.set}" }
        }

        withName: '.*RANK_VARIANTS_SNV:BCFTOOLS_SORT' {
            ext.when = false
        }

        withName: '.*RANK_VARIANTS_SNV:TABIX_BGZIP' {
            ext.prefix = { "${meta.id}_snv_ranked_${meta.set}" }
            publishDir = [
                path: { "${params.outdir}/rank_and_filter" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

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

//
// Score and rank MT SNVs
//

process {
    if (!params.skip_mt_annotation) {
        withName: '.*RANK_VARIANTS_MT:.*' {
            ext.when = !params.skip_mt_annotation
            publishDir = [
                enabled: false
            ]
        }

        withName: '.*RANK_VARIANTS_MT:GENMOD_ANNOTATE' {
            ext.prefix = { "${meta.id}_mt_genmod_annotate_${meta.set}" }
            ext.args = { [
                '--annotate_regions',
                params.genome.equals('GRCh37') ? '--genome-build 37' : '--genome-build 38'
                ].join(' ') }
        }

        withName: '.*RANK_VARIANTS_MT:GENMOD_MODELS' {
            ext.prefix = { "${meta.id}_mt_genmod_models_${meta.set}" }
            ext.args = " --whole_gene "
        }

        withName: '.*RANK_VARIANTS_MT:GENMOD_SCORE' {
            ext.prefix = { "${meta.id}_mt_genmod_score_${meta.set}" }
            ext.args = " --rank_results "
        }

        withName: '.*RANK_VARIANTS_MT:GENMOD_COMPOUND' {
            ext.prefix = { "${meta.id}_mt_genmod_compound_${meta.set}" }
        }

        withName: '.*RANK_VARIANTS_MT:BCFTOOLS_SORT' {
            ext.when = false
        }

        withName: '.*RANK_VARIANTS_MT:TABIX_BGZIP' {
            ext.prefix = { "${meta.id}_mt_ranked_${meta.set}" }
            publishDir = [
                path: { "${params.outdir}/rank_and_filter" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

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