/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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   = When to run the module.
----------------------------------------------------------------------------------------
*/

// JOINT_GERMLINE

process {

    withName: 'GATK4_GENOMICSDBIMPORT' {
        ext.args         = { "--genomicsdb-shared-posixfs-optimizations true --bypass-feature-reader "}
        ext.prefix       = { "${meta.intervals_name}.joint" }
        publishDir = [
            enabled: false
        ]
    }

    withName: 'GATK4_GENOTYPEGVCFS' {
        ext.prefix       = { meta.intervals_name }
        publishDir = [
            enabled: false
        ]
    }

    if (params.tools && params.tools.contains('haplotypecaller') && params.joint_germline) {
        withName: 'NFCORE_SAREK:SAREK:BAM_VARIANT_CALLING_GERMLINE_ALL:BAM_JOINT_CALLING_GERMLINE_GATK:BCFTOOLS_SORT' {
            ext.prefix       = { "${vcf.baseName.minus("vcf")}sort" }
            publishDir       = [
                enabled: false
            ]
        }
    }

    withName: 'MERGE_GENOTYPEGVCFS' {
        ext.prefix       = "joint_germline"
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/variant_calling/haplotypecaller/joint_variant_calling/" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: "*{vcf.gz,vcf.gz.tbi}"
        ]
    }

    withName: 'VARIANTRECALIBRATOR_INDEL' {
        ext.prefix = { "${meta.id}_INDEL" }
        ext.args = "-an QD -an MQRankSum -an ReadPosRankSum -an FS -an SOR -an DP  -mode INDEL"
        publishDir = [
            enabled: false
        ]
    }

    withName: 'VARIANTRECALIBRATOR_SNP' {
        ext.prefix = { "${meta.id}_SNP" }
        ext.args = "-an QD -an MQ -an MQRankSum -an ReadPosRankSum -an FS -an SOR -mode SNP"
        publishDir = [
            enabled: false
        ]
    }

    withName: 'GATK4_APPLYVQSR_SNP' {
        ext.prefix = { "${meta.id}_SNP" }
        ext.args = '--truth-sensitivity-filter-level 99.9 -mode SNP'
    }

    withName: 'GATK4_APPLYVQSR_INDEL' {
        ext.prefix    = { "${meta.id}_INDEL" }
        ext.args      = '--truth-sensitivity-filter-level 99.9 -mode INDEL'
    }

    withName: 'MERGE_VQSR' {
        ext.prefix       = "joint_germline_recalibrated"
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/variant_calling/haplotypecaller/joint_variant_calling/"},
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: "*{vcf.gz,vcf.gz.tbi}"
        ]
    }
}
