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

// MAPPING

process {

    if (params.step == 'mapping') {
        withName: "BWAMEM1_MEM" {
            ext.when         = { params.aligner == "bwa-mem" }
        }

        withName: "BWAMEM2_MEM" {
            ext.when         = { params.aligner == "bwa-mem2" }
        }

        withName: "DRAGMAP_ALIGN" {
            ext.when         = { params.aligner == "dragmap" }
            ext.args         = { "--RGSM ${meta.patient}_${meta.sample} --RGID ${meta.read_group}" }
        }

        withName: "(BWAMEM.*_MEM|DRAGMAP_ALIGN)" {
            // Markduplicates Spark NEEDS name-sorted reads or runtime goes through the roof
            // However if it's skipped, reads need to be coordinate-sorted
            // Only name sort if Spark for Markduplicates + duplicate marking is not skipped
            ext.args2        = { params.use_gatk_spark && params.use_gatk_spark.contains('markduplicates') && (!params.skip_tools || (params.skip_tools && !params.skip_tools.split(',').contains('markduplicates'))) ? '-n' : '' }
            ext.prefix       = { params.split_fastq > 1 ? "${meta.id}".concat('.').concat(reads.get(0).name.tokenize('.')[0]) : "${meta.id}.sorted" }
            publishDir       = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/" },
                pattern: "*bam",
                // Only save if (save_mapped OR (no_markduplicates AND save_output_as_bam)) AND only a single BAM file per sample
                saveAs: { (params.save_output_as_bam && (params.save_mapped || params.skip_tools && params.skip_tools.split(',').contains('markduplicates'))) && (meta.size * meta.numLanes == 1) ? "mapped/${meta.id}/${it}" : null }
            ]
        }

        withName: "BWAMEM.*_MEM" {
            // Using -B 3 for tumor samples
            ext.args         = { meta.status == 1 ? "-K 100000000 -Y -B 3 -R ${meta.read_group}" : "-K 100000000 -Y -R ${meta.read_group}" }
        }
    }

    withName: 'MERGE_BAM|INDEX_MERGE_BAM' {
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/" },
            pattern: "*{bam,bai}",
            // Only save if (save_output_as_bam AND (no_markduplicates OR save_mapped ))
            saveAs: { (params.save_output_as_bam && (params.save_mapped || params.skip_tools && params.skip_tools.split(',').contains('markduplicates'))) ? "mapped/${meta.id}/${it}" : null }
        ]
    }

    withName: 'MERGE_BAM' {
        ext.prefix = { "${meta.id}.sorted" }
    }
}
