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

//
// Mitochondria alignment options
//

process {

    if (params.aligner.equals("bwamem2")) {
        withName: '.*ALIGN_MT:BWAMEM2_MEM_MT' {
            ext.args = { "-M -K 100000000 -R ${meta.read_group}" }
            ext.when = { !(params.analysis_type.equals("wes")) }
            ext.prefix = { "${meta.id}_sorted" }
        }
    }

    if (params.aligner.equals("bwa")) {
        withName: '.*ALIGN_MT:BWA_MEM_MT' {
            ext.args = { "-M -K 100000000 -R ${meta.read_group}" }
            ext.when = { !(params.analysis_type.equals("wes")) }
            ext.prefix = { "${meta.id}_sorted" }
        }
    }

    if (params.aligner.equals("sentieon")) {
        withName: '.*ALIGN_MT:SENTIEON_BWAMEM_MT' {
            ext.args = { "-M -K 10000000 -R ${meta.read_group}" }
            ext.when = { !(params.analysis_type.equals("wes"))}
            ext.prefix = { "${meta.id}_sorted" }
        }
    }

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

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

    withName: '.*ALIGN_MT:PICARD_MARKDUPLICATES_MT' {
        ext.args = '--TMP_DIR . --VALIDATION_STRINGENCY LENIENT --CREATE_INDEX true'
        ext.prefix = { "${meta.id}_sorted_merged_md" }
    }

    withName: '.*ALIGN_MT:SAMTOOLS_SORT_MT' {
        ext.prefix = { "${meta.id}_sorted_merged_md_sorted" }
    }

}

//
// Shifted mitochondria alignment options
//

process {

    if (params.aligner.equals("bwamem2")) {
        withName: '.*ALIGN_MT_SHIFT:BWAMEM2_MEM_MT' {
            ext.args = { "-M -K 100000000 -R ${meta.read_group}" }
            ext.when = { !(params.analysis_type.equals("wes")) }
            ext.prefix = { "${meta.id}_sorted_shifted" }
        }
    }

    if (params.aligner.equals("sentieon")) {
        withName: '.*ALIGN_MT_SHIFT:SENTIEON_BWAMEM_MT' {
            ext.args = { "-M -K 10000000 -R ${meta.read_group}" }
            ext.when = { !(params.analysis_type.equals("wes"))}
            ext.prefix = { "${meta.id}_sorted_shifted" }
        }
    }

    if (params.aligner.equals("bwa")) {
        withName: '.*ALIGN_MT_SHIFT:BWA_MEM_MT' {
            ext.args = { "-M -K 100000000 -R ${meta.read_group}" }
            ext.when = { !(params.analysis_type.equals("wes")) }
            ext.prefix = { "${meta.id}_sorted_shifted" }
        }
    }

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

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

    withName: '.*ALIGN_MT_SHIFT:PICARD_MARKDUPLICATES_MT' {
        ext.args = '--TMP_DIR . --VALIDATION_STRINGENCY LENIENT --CREATE_INDEX true'
        ext.prefix = { "${meta.id}_sorted_merged_md_shifted" }
    }

    withName: '.*ALIGN_MT_SHIFT:SAMTOOLS_SORT_MT' {
        ext.prefix = { "${meta.id}_sorted_merged_md_sorted_shifted" }
    }

}
