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

// RECALIBRATE

process {

    withName: 'GATK4_APPLYBQSR|GATK4_APPLYBQSR_SPARK' {
        ext.prefix       = { meta.num_intervals <= 1 ? "${meta.id}.recal" : "${meta.id}_${intervals.simpleName}.recal" }
        publishDir       = [
            enabled: !params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/" },
            pattern: "*cram",
            saveAs: { meta.num_intervals > 1 ? null : "recalibrated/${meta.id}/${it}" }
        ]
    }

    if ((params.step == 'mapping' || params.step == 'markduplicates'|| params.step == 'prepare_recalibration'|| params.step == 'recalibrate') && (!(params.skip_tools && params.skip_tools.split(',').contains('baserecalibrator')))) {
        withName: 'NFCORE_SAREK:SAREK:(BAM_APPLYBQSR|BAM_APPLYBQSR_SPARK):CRAM_MERGE_INDEX_SAMTOOLS:MERGE_CRAM' {
            ext.prefix       = { "${meta.id}.recal" }
            ext.when         = { meta.num_intervals > 1 }
            publishDir       = [
                enabled: !params.save_output_as_bam,
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/recalibrated/${meta.id}/" },
                pattern: "*cram"
            ]
        }

        withName: 'NFCORE_SAREK:SAREK:(BAM_APPLYBQSR|BAM_APPLYBQSR_SPARK):CRAM_MERGE_INDEX_SAMTOOLS:INDEX_CRAM' {
            publishDir       = [
                enabled: !params.save_output_as_bam,
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/recalibrated/${meta.id}/" },
                pattern: "*{recal.cram,recal.cram.crai}"
            ]
        }
    }

    withName: 'CRAM_TO_BAM_RECAL' {
        ext.prefix       = { "${meta.id}.recal" }
        ext.when         = { params.save_output_as_bam}
        publishDir       = [
            enabled: params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/recalibrated/${meta.id}/" },
            pattern: "*{recal.bam,recal.bam.bai}"
        ]
    }
}
