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

// https://nfcore.slack.com/archives/C027CM7P08M/p1677521465365689
def generate_wfmash_sparse_map_cmd() {
    def wfmash_sparse_map_cmd = ""
    if (params.wfmash_sparse_map == "auto") {
        def n = params.n_haplotypes
        def x = Math.log(n)/n * 10
        wfmash_sparse_map_frac = 1
        if (x < 1) {
            def wfmash_sparse_map_frac = x
        }
        wfmash_sparse_map_cmd = "-x ${wfmash_sparse_map_frac}"
    } else {
        if (params.wfmash_sparse_map != null) {
            wfmash_sparse_map_cmd = "-x ${params.wfmash_sparse_map}"
        }
    }
    return wfmash_sparse_map_cmd
}

def wfmash_sparse_map_cmd = generate_wfmash_sparse_map_cmd()

def generate_smoothxg_poa_params_cmd() {
    def smoothxg_poa_params_cmd = "-P ${params.smoothxg_poa_params}"
    if (params.smoothxg_poa_params == "asm5") {
        smoothxg_poa_params_cmd = "-P 1,19,39,3,81,1"
    } else if (params.smoothxg_poa_params == "asm10") {
        smoothxg_poa_params_cmd = "-P 1,9,16,2,41,1"
    } else if (params.smoothxg_poa_params == "asm15") {
        smoothxg_poa_params_cmd = "-P 1,7,11,2,33,1"
    } else if (params.smoothxg_poa_params == "asm20") {
        smoothxg_poa_params_cmd = "-P 1,4,6,2,26,1"
    }
    return smoothxg_poa_params_cmd
}

def smoothxg_poa_params_cmd = generate_smoothxg_poa_params_cmd()

process {

    publishDir = [
        path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
        mode: params.publish_dir_mode,
        saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
    ]

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

    withName: WFMASH_MAP_ALIGN {
        ext.args = {
            [
                "-n ${params.n_haplotypes - 1}",
                "-s ${params.wfmash_segment_length}",
                "-p ${params.wfmash_map_pct_id}",
                params.wfmash_merge_segments   ? "-M"                                : "",
                params.wfmash_exclude_delim    ? "-Y ${params.wfmash_exclude_delim}" : "-X",
                params.wfmash_no_splits        ? "-N"                                : "",
                params.wfmash_block_length     ? "-l ${params.wfmash_block_length}"  : "-l ${params.wfmash_segment_length * 5}",
                "-k ${params.wfmash_mash_kmer}",
                "-H ${params.wfmash_mash_kmer_thres}",
                "${wfmash_sparse_map_cmd}",
                params.wfmash_temp_dir         ? "-B ${wfmash_temp_dir}"             : "",
            ].join(" ").trim()
        }
        publishDir = [
            path: { "${params.outdir}/wfmash" },
            mode: params.publish_dir_mode,
            pattern: '*.paf'
        ]
    }

    withName: WFMASH_MAP {
        ext.args = {
            [
                "-n ${params.n_haplotypes - 1}",
                "-s ${params.wfmash_segment_length}",
                "-p ${params.wfmash_map_pct_id}",
                params.wfmash_merge_segments   ? "-M"                                : "",
                params.wfmash_exclude_delim    ? "-Y ${params.wfmash_exclude_delim}" : "-X",
                params.wfmash_no_splits        ? "-N"                                : "",
                params.wfmash_block_length     ? "-l ${params.wfmash_block_length}"  : "-l ${params.wfmash_segment_length * 5}",
                "-k ${params.wfmash_mash_kmer}",
                "-H ${params.wfmash_mash_kmer_thres}",
                "${wfmash_sparse_map_cmd}",
                params.wfmash_temp_dir         ? "-B ${wfmash_temp_dir}"             : "",
                "-m",
            ].join(" ").trim()
        }
        publishDir = [
            path: { "${params.outdir}/wfmash_map" },
            mode: params.publish_dir_mode,
            pattern: '*.paf'
        ]
    }

    withName: WFMASH_MAP_COMMUNITY {
        ext.args = {
            [
                "-n ${params.n_haplotypes - 1}",
                "-s ${params.wfmash_segment_length}",
                "-p ${params.wfmash_map_pct_id}",
                params.wfmash_merge_segments   ? "-M"                                : "",
                params.wfmash_exclude_delim    ? "-Y ${params.wfmash_exclude_delim}" : "-X",
                params.wfmash_no_splits        ? "-N"                                : "",
                params.wfmash_block_length     ? "-l ${params.wfmash_block_length}"  : "-l ${params.wfmash_segment_length * 5}",
                "-k ${params.wfmash_mash_kmer}",
                "-H ${params.wfmash_mash_kmer_thres}",
                "${wfmash_sparse_map_cmd}",
                params.wfmash_temp_dir         ? "-B ${wfmash_temp_dir}"             : "",
                "-m",
            ].join(" ").trim()
        }
        publishDir = [
            path: { "${params.outdir}/wfmash_map_community" },
            mode: params.publish_dir_mode,
            pattern: '*.paf'
        ]
    }

    withName: SPLIT_APPROX_MAPPINGS_IN_CHUNKS {
        ext.args = "${params.wfmash_chunks}"
        publishDir = [
            path: { "${params.outdir}/split_approx_mappings_in_chunks" },
            mode: params.publish_dir_mode,
            pattern: '{*.paf}'
        ]
    }

    withName: WFMASH_ALIGN {
        ext.args = {
            [
                "-n ${params.n_haplotypes - 1}",
                "-s ${params.wfmash_segment_length}",
                "-p ${params.wfmash_map_pct_id}",
                params.wfmash_merge_segments   ? "-M"                                : "",
                params.wfmash_exclude_delim    ? "-Y ${params.wfmash_exclude_delim}" : "-X",
                params.wfmash_no_splits        ? "-N"                                : "",
                params.wfmash_block_length     ? "-l ${params.wfmash_block_length}"  : "-l ${params.wfmash_segment_length * 5}",
                "-k ${params.wfmash_mash_kmer}",
                "-H ${params.wfmash_mash_kmer_thres}",
                "${wfmash_sparse_map_cmd}",
                params.wfmash_temp_dir         ? "-B ${wfmash_temp_dir}"             : "",
                "--invert-filtering",
            ].join(" ").trim()
        }
        publishDir = [
            path: { "${params.outdir}/wfmash_align" },
            mode: params.publish_dir_mode,
            pattern: '*.paf'
        ]
    }

    withName: SEQWISH {
        ext.prefix = {"${meta.id}.seqwish"}
        ext.args = {
            [
                "-k ${params.seqwish_min_match_length}",
                "-f ${params.seqwish_sparse_factor}",
                "-B ${params.seqwish_transclose_batch}",
                "-P",
                params.seqwish_temp_dir        ? "--temp-dir ${params.seqwish_temp_dir}" : ""
            ].join(" ").trim()
        }
        publishDir = [
            path: { "${params.outdir}/seqwish" },
            mode: params.publish_dir_mode,
            pattern: '*.gfa'
        ]
    }

    withName: SMOOTHXG {
        ext.args = {
            [
                params.smoothxg_poa_cpus == 0           ? "-T ${task.cpus}"                : "-T ${params.smoothxg_poa_cpus}",
                "-r ${params.n_haplotypes}",
                "${smoothxg_poa_params_cmd}",
                "-V",
                params.smoothxg_temp_dir                ? "-b ${params.smoothxg_temp_dir}" : "",
                params.smoothxg_keep_intermediate_files ? "-K"                             : "",
                "-X 100",
                "-I ${params.wfmash_map_pct_id / 100.0}",
                "-R ${params.smoothxg_block_ratio_min}",
                "-j ${params.smoothxg_max_path_jump}",
                "-e ${params.smoothxg_max_edge_jump}",
                "-l ${params.smoothxg_poa_length}",
                "-O ${params.smoothxg_poa_padding}",
                "-Y ${params.smoothxg_pad_max_depth * params.n_haplotypes}",
                "-d 0",
                "-D 0",
                params.smoothxg_run_abpoa               ? "-A"                             : "",
                params.smoothxg_run_global_poa          ? "-Z"                             : "",
                params.smoothxg_write_maf               ? "-m ${meta.id}.maf"              : ""
            ].join(" ").trim()
        }
        publishDir = [
            path: { "${params.outdir}/smoothxg" },
            mode: params.publish_dir_mode,
            pattern: '{*.gfa,*.maf}'
        ]
    }

    withName: GFAFFIX {
        publishDir = [
            path: { "${params.outdir}/gfaffix" },
            mode: params.publish_dir_mode,
            pattern: '{*.gfa,*.txt}'
        ]
    }

    withName: ODGI_BUILD {
        ext.args =  "-P"
        publishDir = [
            path: { "${params.outdir}/odgi_build" },
            mode: params.publish_dir_mode,
            pattern: '{*.og}'
        ]
    }

    withName: ODGI_UNCHOP {
        ext.prefix = {"${meta.id}.unchop"}
        ext.args = "-P"
        publishDir = [
            path: { "${params.outdir}/odgi_unchop" },
            mode: params.publish_dir_mode,
            pattern: '{*.og}'
        ]
    }

    withName: ODGI_SORT {
        ext.prefix = {"${meta.id}.unchop.Ygs"}
        ext.args = "-p Ygs -P"
        publishDir = [
            path: { "${params.outdir}/FINAL_ODGI" },
            mode: params.publish_dir_mode,
            pattern: '{*.og}'
        ]
    }

    withName: ODGI_VIEW {
        ext.prefix = {"${meta.id}.unchop.Ygs.view"}
        ext.args = "-P"
        publishDir = [
            path: { "${params.outdir}/FINAL_GFA" },
            mode: params.publish_dir_mode,
            pattern: '{*.gfa}'
        ]
    }

    withName: ODGI_STATS {
        ext.args = "-P --multiqc"
        publishDir = [
            path: { "${params.outdir}/odgi_stats" },
            mode: params.publish_dir_mode,
            pattern: '{*.yaml,*.tsv}'
        ]
    }

    withName: ODGI_VIZ_COLOR {
        ext.prefix = {"${meta.id}_multiqc"}
        ext.args = "-x 1500 -y 500 -a 10 -I ${params.smoothxg_consensus_prefix}"
        publishDir = [
            path: { "${params.outdir}/odgi_viz" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_VIZ_POS {
        ext.prefix = {"${meta.id}_multiqc"}
        ext.args = "-x 1500 -y 500 -a 10 -I ${params.smoothxg_consensus_prefix} -u -d"
        publishDir = [
            path: { "${params.outdir}/odgi_viz" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_VIZ_DEPTH {
        ext.prefix = {"${meta.id}_multiqc"}
        ext.args = "-x 1500 -y 500 -a 10 -I ${params.smoothxg_consensus_prefix} -m"
        publishDir = [
            path: { "${params.outdir}/odgi_viz" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_VIZ_INV {
        ext.prefix = {"${meta.id}_multiqc"}
        ext.args = "-x 1500 -y 500 -a 10 -I ${params.smoothxg_consensus_prefix} -z"
        publishDir = [
            path: { "${params.outdir}/odgi_viz" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_VIZ_UNCALLED {
        ext.prefix = {"${meta.id}_multiqc"}
        ext.args = "-x 1500 -y 500 -a 10 -I ${params.smoothxg_consensus_prefix} -N"
        publishDir = [
            path: { "${params.outdir}/odgi_viz" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_VIZ_COMPR {
        ext.prefix = {"${meta.id}_multiqc"}
        ext.args = "-x 1500 -y 500 -a 10 -I ${params.smoothxg_consensus_prefix} -O"
        publishDir = [
            path: { "${params.outdir}/odgi_viz" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_LAYOUT {
        ext.args = {"-P --out ${meta.id}.lay --tsv ${meta.id}.tsv"}
        publishDir = [
            path: { "${params.outdir}/odgi_layout" },
            mode: params.publish_dir_mode,
            pattern: '{*.lay,*.tsv}'
        ]
    }

    withName: ODGI_DRAW_MULTIQC {
        ext.prefix = {"${meta.id}.draw_multiqc"}
        ext.args = "-C -w 20 -H 1000"
        publishDir = [
            path: { "${params.outdir}/odgi_draw" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: ODGI_DRAW_HEIGHT {
        ext.args = "-H 100"
        publishDir = [
            path: { "${params.outdir}/odgi_draw" },
            mode: params.publish_dir_mode,
            pattern: '{*.png}'
        ]
    }

    withName: VG_DECONSTRUCT {
        publishDir = [
            path: { "${params.outdir}/vg_deconstruct" },
            mode: params.publish_dir_mode,
            pattern: '{*.vcf,*.stats}'
        ]
    }

    withName: PAF2NET {
        publishDir = [
            path: { "${params.outdir}/paf2net" },
            mode: params.publish_dir_mode,
            pattern: '{*.txt}'
        ]
    }

    withName: NET2COMMUNITIES {
        publishDir = [
            path: { "${params.outdir}/net2communities" },
            mode: params.publish_dir_mode,
            pattern: '{*.community.*.txt}'
        ]
    }

    withName: EXTRACT_COMMUNITIES {
        publishDir = [
            path: { "${params.outdir}/extract_communities" },
            mode: params.publish_dir_mode,
            pattern: '{*.community.*.fa}'
        ]
    }

    withName: TABIX_BGZIP {
        publishDir = [
            path: { "${params.outdir}/tabix_bgzip" },
            mode: params.publish_dir_mode,
            pattern: '{*.fa.gz}'
        ]
    }

    withName: SAMTOOLS_FAIDX {
        publishDir = [
            path: { "${params.outdir}/samtools_faidx" },
            mode: params.publish_dir_mode,
            pattern: '{*.fai,*.gzi}'
        ]
    }

    withName: MULTIQC_COMMUNITY {
        ext.args = { "--outdir ${meta.id}_multiqc" }
        publishDir = [
            path: { "${params.outdir}/" },
            mode: params.publish_dir_mode,
            pattern: '{*/*.html,*/*_data, */*_plots}'
        ]
    }

    withName: ODGI_SQUEEZE {
        ext.prefix = {"${meta.id}.squeeze"}
        publishDir = [
            path: { "${params.outdir}/FINAL_ODGI" },
            mode: params.publish_dir_mode,
            pattern: '{*.og}'
        ]
    }

}
