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

// MARKDUPLICATES

process {

    withName: 'CRAM_TO_BAM' {
        ext.args         = "-b"
    }

    withName: 'BAM_TO_CRAM' {
        // BAM provided for step Markduplicates either run through MD or Convert -> then saved as sorted.cram (convert) or md.cram (md directly)
        // BAM files provided for step prepare_recal are converted and run through BQSR -> then saved as md.cram
        // BAM files provided for step recal are converted and run through BQSR II -> then saved as md.cram
        ext.args         = "-C"
        ext.prefix       = { "${meta.id}.converted" }
        publishDir       = [
            enabled: !params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/converted/${meta.id}" },
            pattern: "*{cram,crai}"
        ]
    }

    withName: 'BAM_TO_CRAM_MAPPING' {
        // Run only when mapping should be saved as CRAM or when no MD is done
        ext.when         = (params.save_mapped && !params.save_output_as_bam)  || (params.skip_tools && params.skip_tools.split(',').contains('markduplicates'))
        ext.prefix       = { "${meta.id}.sorted" }
        publishDir       = [
            // Never publish if BAM only should be published
            enabled: !params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/mapped/${meta.id}/" },
            pattern: "*{cram,crai}"
        ]
    }

    withName: 'GATK4_ESTIMATELIBRARYCOMPLEXITY' {
        ext.prefix       = { "${meta.id}.md.cram" }
        ext.when         = { !(params.skip_tools && params.skip_tools.split(',').contains('markduplicates_report')) }
        publishDir       = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reports/markduplicates/${meta.id}" },
            pattern: "*metrics"
        ]
    }

    withName: 'GATK4_MARKDUPLICATES' {
        ext.args         = '-REMOVE_DUPLICATES false -VALIDATION_STRINGENCY LENIENT'
        ext.prefix       = { "${meta.id}.md.cram" }
        ext.when         = { !(params.skip_tools && params.skip_tools.split(',').contains('markduplicates')) }
        publishDir       = [
            [
                enabled: !params.save_output_as_bam,
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
                pattern: "*{cram,crai}"
            ],
            [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/reports/" },
                pattern: "*metrics",
                saveAs: { !(params.skip_tools && params.skip_tools.split(',').contains('markduplicates_report')) ? "markduplicates/${meta.id}/${it}" : null}
            ]
        ]
    }

    withName: 'GATK4_MARKDUPLICATES_SPARK' {
        ext.args         = '--remove-sequencing-duplicates false -VS LENIENT'
        ext.prefix       = { "${meta.id}.md.cram" }
        publishDir       = [
            enabled: !params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
            pattern: "*{cram,crai}"
        ]
    }

    withName: 'INDEX_MARKDUPLICATES' {
        publishDir       = [
            enabled: !params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
            pattern: "*{cram,crai}"
        ]
    }


    withName: 'NFCORE_SAREK:SAREK:CRAM_TO_BAM' {
        ext.prefix       = { "${meta.id}.md" }
        ext.when         = { params.save_output_as_bam }
        publishDir       = [
            enabled: params.save_output_as_bam,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
            pattern: "*{md.bam,md.bam.bai}"
        ]
    }
}
