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

// PREPARE_GENOME

process {

    withName: 'BWAMEM1_INDEX' {
        ext.when         = { !params.bwa && params.step == "mapping" && params.aligner == "bwa-mem" }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference" },
            pattern: "bwa"
        ]
    }

    withName: 'BWAMEM2_INDEX' {
        ext.when         = { !params.bwamem2 && params.step == "mapping" && params.aligner == "bwa-mem2" }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference" },
            pattern: "bwamem2"
        ]
    }

    withName: 'DRAGMAP_HASHTABLE' {
        ext.when         = { !params.dragmap && params.step == "mapping" && params.aligner == "dragmap" }
        publishDir = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference" },
            pattern: "dragmap"
        ]
    }

    withName: 'GATK4_CREATESEQUENCEDICTIONARY' {
        ext.when         = { !params.dict && params.step != "annotate" && params.step != "controlfreec" }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/dict" },
            pattern: "*dict"
        ]
    }

    withName: 'MSISENSORPRO_SCAN' {
        ext.when         = { params.tools && params.tools.split(',').contains('msisensorpro') }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/msi" },
            pattern: "*list"
        ]
    }

    withName: 'SAMTOOLS_FAIDX' {
        ext.when         = { !params.fasta_fai && params.step != "annotate" }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/fai" },
            pattern: "*fai"
        ]
    }

    withName: 'TABIX_DBSNP' {
        ext.when         = { !params.dbsnp_tbi && params.dbsnp && ((params.step == "mapping" || params.step == "markduplicates" || params.step == "prepare_recalibration") || params.tools && (params.tools.split(',').contains('controlfreec') || params.tools.split(',').contains('haplotypecaller') || params.tools.split(',').contains('mutect2'))) }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/dbsnp" },
            pattern: "*vcf.gz.tbi"
        ]
    }

    withName: 'TABIX_GERMLINE_RESOURCE' {
        ext.when         = { !params.germline_resource_tbi && params.germline_resource && params.tools && params.tools.split(',').contains('mutect2') }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/germline_resource" },
            pattern: "*vcf.gz.tbi"
        ]
    }

    withName: 'TABIX_KNOWN_INDELS' {
        ext.when         = { !params.known_indels_tbi && params.known_indels && (params.step == 'mapping' || params.step == "markduplicates" || params.step == 'prepare_recalibration' || (params.tools && params.tools.split(',').contains('haplotypecaller')) ) }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/known_indels" },
            pattern: "*vcf.gz.tbi"
        ]
    }

    withName: 'TABIX_KNOWN_SNPS' {
        ext.when         = { !params.known_snps_tbi && params.known_snps && (params.step == 'mapping' || params.step == "markduplicates" || params.step == 'prepare_recalibration' || (params.tools && params.tools.split(',').contains('haplotypecaller')) ) }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/known_snps" },
            pattern: "*vcf.gz.tbi"
        ]
    }

    withName: 'TABIX_PON' {
        ext.when         = { !params.pon_tbi && params.pon && params.tools && params.tools.split(',').contains('mutect2') }
        publishDir       = [
            enabled: (params.save_reference || params.build_only_index),
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference/pon" },
            pattern: "*vcf.gz.tbi"
        ]
    }

    withName: 'UNZIP_ALLELES|UNZIP_LOCI|UNZIP_GC|UNZIP_RT' {
        ext.when         = { params.tools && params.tools.split(',').contains('ascat')}
        publishDir       = [
            enabled: false
        ]
    }

    withName: 'UNTAR_CHR_DIR' {
        ext.when         = { params.tools && params.tools.split(',').contains('controlfreec')}
    }
}
