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

    withName: 'NFCORE_SAREK:SAREK:(BAM_MARKDUPLICATES|BAM_MARKDUPLICATES_SPARK):CRAM_QC_MOSDEPTH_SAMTOOLS:SAMTOOLS_STATS' {
        ext.prefix = { "${meta.id}.md.cram" }
        ext.when   = { !(params.skip_tools && params.skip_tools.split(',').contains('samtools')) }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reports/samtools/${meta.id}" },
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'BAM_TO_CRAM_MAPPING' {
        ext.prefix = { "${meta.id}.sorted" }
        // 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')) &&
                !(params.tools && params.tools.split(',').contains('sentieon_dedup'))
            )
        publishDir = [
            // Never publish if BAM only should be published
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/mapped/${meta.id}/" },
            pattern: "*{cram,crai}",
            saveAs: { !params.save_output_as_bam ? it : null }
        ]
    }

    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 = [
            [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
                pattern: "*{cram,crai}",
                saveAs: { !params.save_output_as_bam ? it : null }
            ],
            [
                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: 'GATK4SPARK_MARKDUPLICATES' {
        containerOptions = ''
        ext.args   = { '--remove-sequencing-duplicates false -VS LENIENT' }
        ext.prefix = { "${meta.id}.md.cram" }
        publishDir = [
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
            pattern: "*{cram,crai}",
            saveAs: { !params.save_output_as_bam ? it : null }
        ]
    }

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

    withName: 'NFCORE_SAREK:SAREK:CRAM_TO_BAM' {
        ext.when   = { params.save_output_as_bam }
        if (params.tools && params.tools.split(',').contains('sentieon_dedup')) {
            ext.prefix = { "${meta.id}.dedup" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/sentieon_dedup/${meta.id}/" },
                pattern: "*{dedup.bam,dedup.bam.bai}",
                saveAs: { params.save_output_as_bam ? it : null }
            ]
        } else {
            ext.prefix = { "${meta.id}.md" }
            publishDir = [
                mode: params.publish_dir_mode,
                path: { "${params.outdir}/preprocessing/markduplicates/${meta.id}/" },
                pattern: "*{md.bam,md.bam.bai}",
                saveAs: { params.save_output_as_bam ? it : null }
            ]
        }
    }
}
