/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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|GATK4SPARK_APPLYBQSR' {
        ext.prefix       = { meta.num_intervals <= 1 ? "${meta.id}.recal" : "${meta.id}_${intervals.simpleName}.recal" }
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/" },
            pattern: "*cram",
            saveAs: { !params.save_output_as_bam ? meta.num_intervals > 1 ? null : "recalibrated/${meta.id}/${it}" : null }
        ]
    }

    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       = [
                    mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/recalibrated/${meta.id}/" },
                pattern: "*cram",
                saveAs: { !params.save_output_as_bam ? it : null }
        ]
        }

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

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