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

// HAPLOTYPECALLER

process {

    withName: 'GATK4_HAPLOTYPECALLER' {
        ext.args         = { params.joint_germline ? "-ERC GVCF" : "" }
        ext.prefix       = { meta.num_intervals <= 1 ? ( params.joint_germline ? "${meta.id}.haplotypecaller.g" : "${meta.id}.haplotypecaller" ) : ( params.joint_germline ? "${meta.id}.haplotypecaller.${intervals.simpleName}.g" :"${meta.id}.haplotypecaller.${intervals.simpleName}" ) }
        ext.when         = { params.tools && params.tools.split(',').contains('haplotypecaller') }
        publishDir       = [
            enabled: !params.joint_germline,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/variant_calling/"},
            pattern: "*{vcf.gz,vcf.gz.tbi}",
            saveAs: { meta.num_intervals > 1 ? null : "haplotypecaller/${meta.id}/${it}" }
        ]
    }

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

    withName: 'CNNSCOREVARIANTS' {
        publishDir       = [
            // Otherwise it gets published
            enabled: false
        ]
    }

    withName: 'FILTERVARIANTTRANCHES' {
        ext.prefix       = {"${meta.id}.haplotypecaller"}
        ext.args         = { "--info-key CNN_1D" }
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/variant_calling/haplotypecaller/${meta.id}/"},
            pattern: "*{vcf.gz,vcf.gz.tbi}"
        ]
    }

    withName: 'GATK4_GENOMICSDBIMPORT' {
        ext.args         = { "--genomicsdb-shared-posixfs-optimizations true --bypass-feature-reader "}
        ext.prefix       = { meta.num_intervals > 1 ? "${meta.intervals_name}.joint" : "joint" }
        ext.when         = { params.tools && params.tools.split(',').contains('haplotypecaller') && params.joint_germline && !params.no_intervals}
    }

    withName: 'GATK4_GENOTYPEGVCFS' {
        ext.prefix       = { meta.num_intervals > 1 ? meta.intervals_name : "joint_germline" }
    }

    if (params.tools && params.tools.contains('haplotypecaller') && params.joint_germline){
        withName: 'NFCORE_SAREK:SAREK:GERMLINE_VARIANT_CALLING:RUN_HAPLOTYPECALLER:JOINT_GERMLINE:BCFTOOLS_SORT' {
            ext.prefix       = { meta.num_intervals <= 1 ? "${meta.id}.joint.sort" : "${vcf.minus("vcf.gz")}.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}"
        ]
    }
}
