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

//
// ANALYSE_MT:ALIGN_AND_CALL_MT
//

process {

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:BWAMEM2_MEM_MT' {
        ext.when = { !params.skip_mt_analysis && !(params.analysis_type == "wes") && params.aligner == "bwamem2" }
        ext.args = { "-M -K 100000000 -R ${meta.read_group}" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:SENTIEON_BWAMEM_MT' {
        ext.args = { "-M -K 10000000 -R ${meta.read_group}" }
        ext.when = { !params.skip_mt_analysis && !(params.analysis_type == "wes") && params.aligner == "sentieon" }
        ext.prefix = { "${meta.id}.sorted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:GATK4_MERGEBAMALIGNMENT_MT' {
        ext.args = '--CREATE_INDEX true --MAX_GAPS -1  --SORT_ORDER queryname --INCLUDE_SECONDARY_ALIGNMENTS false --PAIRED_RUN false --VALIDATION_STRINGENCY LENIENT'
        ext.prefix = { "${meta.id}_merged" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:PICARD_ADDORREPLACEREADGROUPS_MT' {
        ext.args = { [
            "--VALIDATION_STRINGENCY LENIENT",
            "--RGLB lib",
            "--RGPL ILLUMINA",
            "--RGPU barcode",
            "--RGSM ${meta.id}"
        ].join(' ' ).trim() }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:PICARD_MARKDUPLICATES_MT' {
        ext.args = '--VALIDATION_STRINGENCY LENIENT --CREATE_INDEX true'
        ext.prefix = { "${meta.id}_markduplicates" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:SAMTOOLS_SORT_MT' {
        ext.prefix = { "${meta.id}_sorted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:MT_DELETION' {
        ext.args = '-s --insert-size 16000'
        publishDir = [
            path: { "${params.outdir}/mt_sv" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:GATK4_MUTECT2_MT' {
        ext.args = '--mitochondria-mode TRUE'
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT:GATK4_FILTERMUTECTCALLS_MT' {
        ext.prefix = { "${meta.id}_filtered" }
    }
}

//
// ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT
//

process {

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:BWAMEM2_MEM_MT' {
        ext.when = { !params.skip_mt_analysis && !(params.analysis_type == "wes") && params.aligner == "bwamem2" }
        ext.args = { "-M -K 100000000 -R ${meta.read_group}" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:SENTIEON_BWAMEM_MT' {
        ext.args = { "-M -K 10000000 -R ${meta.read_group}" }
        ext.when = { !params.skip_mt_analysis && !(params.analysis_type == "wes") && params.aligner == "sentieon" }
        ext.prefix = { "${meta.id}.sorted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:GATK4_MERGEBAMALIGNMENT_MT' {
        ext.args = '--CREATE_INDEX true --MAX_GAPS -1  --SORT_ORDER queryname --INCLUDE_SECONDARY_ALIGNMENTS false --PAIRED_RUN false --VALIDATION_STRINGENCY LENIENT'
        ext.prefix = { "${meta.id}_merged_shifted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:PICARD_ADDORREPLACEREADGROUPS_MT' {
        ext.args =  { [
            "--VALIDATION_STRINGENCY LENIENT",
            "--RGLB lib",
            "--RGPL ${params.platform}",
            "--RGPU barcode",
            "--RGSM ${meta.id}"
        ].join(' ' ).trim() }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:PICARD_MARKDUPLICATES_MT' {
        ext.args = '--VALIDATION_STRINGENCY LENIENT --CREATE_INDEX true'
        ext.prefix = { "${meta.id}_markduplicates_shifted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:SAMTOOLS_SORT_MT' {
        ext.prefix = { "${meta.id}_sorted_shifted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:GATK4_MUTECT2_MT' {
        ext.args = '--mitochondria-mode TRUE'
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:HAPLOCHECK_MT' {
        ext.prefix = { "${meta.id}_shifted" }
    }

    withName: '.*ANALYSE_MT:ALIGN_AND_CALL_MT_SHIFT:GATK4_FILTERMUTECTCALLS_MT' {
        ext.prefix = { "${meta.id}_filtered_shifted" }
    }

}
