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

process {

    withName: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml'
        ]
    }

    withName: 'SNAPSHOT_SRES|PRETEXTMAP_HIGHRES|PRETEXTMAP_STANDRD' {
        publishDir = [
            path: { "${params.outdir}/pretext_maps_raw" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: 'PRETEXT_GRAPH' {
        publishDir = [
            path: { "${params.outdir}/pretext_maps_processed" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    // Coverge and repeat, gap, telo
    withName: 'BED2BW_NORMAL|BED2BW_LOG|BEDTOOLS_MERGE_MAX|FINDHALFCOVERAGE|BEDTOOLS_MERGE_MIN|GAP_LENGTH|EXTRACT_TELO' {
        publishDir = [
            path: { "${params.outdir}/accessory_files" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: AVGCOV {
        ext.args    = "-T ./"
    }

    withName: GNU_SORT_A {
        ext.args    = { "-k1,1 -k2,2n" }
        ext.suffix  = { "intersect" }
    }

    withName: GNU_SORT_B {
        ext.args    = { "-k1,1 -k2,2n" }
        ext.suffix  = { "genome" }
    }

    withName: GNU_SORT_C {
        ext.args    = { "-k1,1 -k2,2n" }
        ext.suffix  = { "bins" }
    }

    withName: BEDTOOLS_MAKEWINDOWS {
        ext.args    = { "-w 10000" }
    }

    withName: BEDTOOLS_INTERSECT {
        ext.prefix  = { "${meta.id}_INTERSECT" }
    }

    withName: BEDTOOLS_MAP {
        ext.prefix  = { "${meta.id}_MAPPED" }
        ext.args    = { "-c 4 -o sum" }
    }

    withName: SEQTK_CUTN {
        ext.args    = "-n 1"
    }

    //
    // ACCESSORY_FILES -> LONGREAD_COVERAGE
    //
    withName: ".*:ACCESSORY_FILES:LONGREAD_COVERAGE:SAMTOOLS_VIEW_FILTER_PRIMARY" {
        ext.args        = "-b -hF 256"
        ext.prefix      = { "${meta.id}_view" }
    }

    withName: '.*:LONGREAD_COVERAGE:MINIMAP2_ALIGN' {
        ext.args = { [
            meta.read_type.equals("hifi") ? "-ax map-hifi" : "",
            meta.read_type.equals("clr") ? "-ax map-pb" : "",
            meta.read_type.equals("ont") ? "-ax map-ont" :"",
            meta.read_type.equals("illumina") ? "-ax sr" : "",
            "--cs=short",
            reference.size() > 2.5e9 ? (" -I" + Math.ceil(reference.size()/1e9)+"G") : "",
        ].minus("").join(" ") }
        ext.args2   = { "-T ${meta.id}_tmp" }
        ext.prefix  = { "${meta.id}_alignment_${reference.getName().tokenize('.')[0]}" }
    }

    withName: '.*:.*:.*:LONGREAD_COVERAGE:SAMTOOLS_MERGE' {
        ext.prefix  = { "${meta.id}_merge" }
    }

    withName: '.*:LONGREAD_COVERAGE:BEDTOOLS_GENOMECOV' {
        ext.args    = "-bga -split"
        ext.prefix  = { "${meta.id}_genome2cov" }
    }

    withName: '.*:LONGREAD_COVERAGE:BEDTOOLS_MERGE_MAX' {
        ext.args    = "-d 50"
        ext.prefix  = { "maxdepth" }
    }

    withName: '.*:LONGREAD_COVERAGE:BEDTOOLS_MERGE_MIN' {
        ext.args    = "-d 50"
        ext.prefix  = { "zerodepth" }
    }

    withName: '.*:LONGREAD_COVERAGE:GNU_SORT' {
        ext.args    = "-k1,1 -k2,2n"
        ext.prefix  = { "${meta.id}_sorted" }
    }

    withName: '.*:LONGREAD_COVERAGE:BED2BW_NORMAL' {
        ext.prefix  = 'coverage'
    }

    withName: "FIND_TELOMERE_REGIONS" {
        ext.find_telomere       = "find_telomere"
    }

    withName: "FIND_TELOMERE_WINDOWS" {
        ext.telomere_jar        = "telomere.jar"
        ext.telomere_jvm_params = "-Xms1g -Xmx1g"
    }

    withName: PRETEXTMAP_STANDRD {
        ext.args        = { "${meta.map_order.equals("length") ? "--sortby length": "--sortby nosort" } --mapq 0" }
        ext.prefix      = { "${meta.id}_normal_pi" }
    }

    withName: PRETEXTMAP_HIGHRES {
        ext.args        = { "${meta.map_order.equals("length") ? "--sortby length": "--sortby nosort" } --highRes --mapq 0" }
        ext.prefix      = { "${meta.id}_hr_pi" }
    }

    withName: ".*:PRETEXT_INGEST_SNDRD:PRETEXT_GRAPH" {
        ext.args        = { "--textureBuffer 1G" }
        ext.prefix      = { "${meta.id}_normal" }
    }

    withName: ".*:PRETEXT_INGEST_HIRES:PRETEXT_GRAPH" {
        ext.args        = { "--textureBuffer 1G" }
        ext.prefix      = { "${meta.id}_hr" }
    }

    withName: 'SNAPSHOT_SRES' {
        ext.args = "--sequences '=full' --resolution 1440"
        ext.prefix = { "${meta.id}_normal" }
    }

    withName: ".*:GENERATE_MAPS:HIC_BWAMEM2:CRAM_FILTER_ALIGN_BWAMEM2_FIXMATE_SORT" {
        ext.args        = ""
        ext.args1       = "-F0xB00 -nt"
        ext.args2       = { "-5SPCp -H'${rglines}'" }
        ext.args3       = "-mpu"
        ext.args4       = { "--write-index -l1" }
    }

    withName: ".*:GENERATE_MAPS:HIC_MINIMAP2:CRAM_FILTER_MINIMAP2_FILTER5END_FIXMATE_SORT" {
        ext.args        = ""
        ext.args1       = ""
        ext.args2       = { "-ax sr" }
        ext.args3       = "-mpu"
        ext.args4       = { "--write-index -l1" }
    }

    withName: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml'
        ]
    }

}
