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

//
// Mobile element variant annotation options
//

process {

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:.*' {
        ext.when = !params.skip_me_annotation
        publishDir = [
            enabled: false
        ]
    }

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:SVDB_QUERY_DB' {
        ext.args = { [
            '--bnd_distance 150',
            '--overlap -1'
        ].join(' ') }
        ext.prefix = { "${meta.id}_me_svdb" }
    }

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:PICARD_SORTVCF' {
        ext.prefix = { "${meta.id}_sortvcf" }
    }

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:ENSEMBLVEP_ME' {
        ext.args   = { [
            '--dir_cache vep_cache',
            '--dir_plugins vep_cache/Plugins',
            '--plugin pLI,pLI_values.txt',
            '--appris --biotype --buffer_size 100 --canonical --cache --ccds',
            '--compress_output bgzip --distance 5000 --domains',
            '--exclude_predicted --force_overwrite --format vcf',
            '--fork 4 --hgvs --humdiv --max_sv_size 248956422 --merged',
            '--no_progress --numbers --per_gene --polyphen p',
            '--protein --offline --regulatory --sift p',
            '--symbol --tsl --uniprot --vcf'
        ].join(' ') }
        ext.prefix = { "${meta.id}_svdbquery_vep" }
    }

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:BCFTOOLS_VIEW_FILTER' {
        // extend filter with arguments such as --exclude 'INFO/swegen_sva_FRQ > 0.1'
        ext.args = { "--apply-filters PASS --output-type z" }
        ext.prefix = { "${meta.id}_filter" }
    }

    withName: '.*:ANNOTATE_MOBILE_ELEMENTS:GENERATE_CLINICAL_SET_ME:ENSEMBLVEP_FILTERVEP' {
        ext.when   = !params.skip_vep_filter
        ext.prefix = { "${meta.id}_me_${meta.set}" }
        ext.args   = { "--filter \"HGNC_ID in ${meta.hgnc_ids.join(',')}\"" }
    }

    withName: '.*:ANNOTATE_MOBILE_ELEMENTS:GENERATE_CLINICAL_SET_ME:TABIX_BGZIP' {
        ext.when   = !params.skip_vep_filter
        ext.prefix = { "${meta.id}_me_${meta.set}" }
    }

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:ANNOTATE_CSQ_PLI_ME:ADD_MOST_SEVERE_CSQ' {
        ext.prefix = { "${meta.id}_me_csq_${meta.set}" }
    }

    withName: '.*ANNOTATE_MOBILE_ELEMENTS:ANNOTATE_CSQ_PLI_ME:ADD_MOST_SEVERE_PLI' {
        ext.prefix = { "${meta.id}_me_pli_${meta.set}" }
    }

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