/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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            = Conditional clause
----------------------------------------------------------------------------------------
*/

//
// Genome and reference preparation options
//

process {
    withName: '.*PREPARE_REFERENCES:.*' {
        publishDir = [
            path: { "${params.outdir}/references" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference,
        ]
    }

    withName: '.*PREPARE_REFERENCES:BWAMEM2_INDEX_GENOME' {
        ext.when = {!params.bwamem2 && params.aligner == "bwamem2"}
    }

    withName: '.*PREPARE_REFERENCES:BWAMEM2_INDEX_MT_SHIFT' {
        ext.when = { !(params.analysis_type == "wes") && params.aligner == "bwamem2"}
    }

    withName: '.*PREPARE_REFERENCES:SENTIEON_BWAINDEX_GENOME' {
        ext.when = {!params.bwa && params.aligner == "sentieon"}
    }

    withName: '.*PREPARE_REFERENCES:SENTIEON_BWAINDEX_MT_SHIFT' {
        ext.when = { !(params.analysis_type == "wes") && params.aligner == "sentieon"}
    }

    withName: '.*PREPARE_REFERENCES:BWA_INDEX_MT_SHIFT' {
        ext.when = { !(params.analysis_type == "wes") && params.aligner == "bwa"}
    }

    withName: '.*PREPARE_REFERENCES:BWA_INDEX_GENOME' {
        ext.when = {!params.bwa && (!(params.aligner == "sentieon") || params.aligner == "bwa")}
    }

    withName: '.*PREPARE_REFERENCES:SAMTOOLS_FAIDX_GENOME' {
        ext.when = {!params.fai}
    }

    withName: '.*PREPARE_REFERENCES:RTGTOOLS_FORMAT' {
        ext.when = { !params.sdf && params.run_rtgvcfeval }
    }

    withName: '.*PREPARE_REFERENCES:SAMTOOLS_EXTRACT_MT' {
        ext.args = { " ${params.mito_name} -o ${meta.id}_mt.fa" }
        ext.when = {!params.mt_fasta}
    }

    withName: '.*PREPARE_REFERENCES:SAMTOOLS_FAIDX_MT_SHIFT' {
        ext.when = { !(params.analysis_type == "wes")}
    }

    withName: '.*PREPARE_REFERENCES:GATK_SD' {
        ext.when = {!params.sequence_dictionary}
    }

    withName: '.*PREPARE_REFERENCES:GATK_SHIFTFASTA' {
        ext.args = { "--interval-file-name ${meta.id}_mt" }
    }

    withName: '.*PREPARE_REFERENCES:GATK_SD_MT_SHIFT' {
        ext.when = { !(params.analysis_type == "wes")}
    }

    withName: '.*PREPARE_REFERENCES:GET_CHROM_SIZES' {
        publishDir = [
            enabled: false,
        ]
    }

    withName: '.*PREPARE_REFERENCES:TABIX_DBSNP' {
        ext.when = {params.known_dbsnp && !params.known_dbsnp_tbi}
    }

    withName: '.*PREPARE_REFERENCES:TABIX_GNOMAD_AF' {
        ext.when = {params.gnomad_af && !params.gnomad_af_idx}
        ext.args   = '-s 1 -b 2 -e 2'
    }

    withName: '.*PREPARE_REFERENCES:TABIX_PT' {
        ext.when = { !params.target_bed.equals(null) && params.target_bed.endsWith(".gz") }
    }

    withName: '.*PREPARE_REFERENCES:TABIX_PBT' {
        ext.when = { !params.target_bed.equals(null) && !params.target_bed.endsWith(".gz") }
    }

    withName: '.*PREPARE_REFERENCES:GATK_BILT' {
        ext.when = { !params.target_bed.equals(null) }
        ext.prefix = { "${meta.id}_target" }
    }

    withName: '.*PREPARE_REFERENCES:GATK_ILT' {
        ext.when = { !params.target_bed.equals(null) }
        ext.args = { "--PADDING ${params.bait_padding} -SUBDIVISION_MODE INTERVAL_SUBDIVISION --SCATTER_COUNT 2" }
        publishDir = [
            enabled: false
        ]
    }

    withName: '.*PREPARE_REFERENCES:CAT_CAT_BAIT' {
        ext.when = { !params.target_bed.equals(null) }
        ext.prefix = { "${meta.id}" }
    }

    withName: '.*PREPARE_REFERENCES:UNTAR_VEP_CACHE' {
        ext.when = { (params.vep_cache && params.vep_cache.endsWith("tar.gz")) }
        publishDir = [
            enabled: false
        ]
    }

    withName: '.*PREPARE_REFERENCES:GATK_PREPROCESS_WGS' {
        ext.args =  { "--padding 0 --interval-merging-rule OVERLAPPING_ONLY --exclude-intervals ${params.mito_name}" }
        ext.when = { params.analysis_type.equals("wgs") && !params.readcount_intervals }
    }

    withName: '.*PREPARE_REFERENCES:GATK_PREPROCESS_WES' {
        ext.args   = { "--bin-length 0 --interval-merging-rule OVERLAPPING_ONLY --exclude-intervals ${params.mito_name}" }
        ext.when = { params.analysis_type.equals("wes") && !params.readcount_intervals }
    }

}
