/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.baseName}.g" :"${meta.id}.haplotypecaller.${intervals.baseName}" ) }
        ext.when   = { params.tools && params.tools.split(',').contains('haplotypecaller') }
        publishDir = [
            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: '.*:VCF_VARIANT_FILTERING_GATK:FILTERVARIANTTRANCHES' {
        ext.args   = { "--info-key CNN_1D" }
        ext.prefix = { "${meta.id}.haplotypecaller" }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/variant_calling/haplotypecaller/${meta.id}/"},
            pattern: "*{vcf.gz,vcf.gz.tbi}"
        ]
    }

    if (params.tools && params.tools.split(',').contains('haplotypecaller')) {
        withName: 'NFCORE_SAREK:SAREK:BAM_VARIANT_CALLING_GERMLINE_ALL:BAM_VARIANT_CALLING_HAPLOTYPECALLER:BAM_MERGE_INDEX_SAMTOOLS:(MERGE_BAM|INDEX_MERGE_BAM)' {
            ext.prefix = { "${meta.id}.realigned" }
            publishDir = [
                enabled: true,
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/variant_calling/haplotypecaller/${meta.id}/" },
                pattern: "*{bam,bai}"
            ]
        }
    }
}
