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

    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: "PIXELATOR.*" {
        publishDir = [
            [
                path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> (filename.endsWith('.log') || filename.equals('versions.yml')) ? null : filename }
            ],
            [
                path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}/logs" },
                mode: params.publish_dir_mode,
                pattern: "*.log"
            ]
        ]

        if (params.pixelator_container) {
            container = params.pixelator_container
        }
    }

    withName: PIXELATOR_LIST_OPTIONS {
        publishDir = [ enabled: false ]
    }

    withName: "CAT_FASTQ" {
        publishDir = [ enabled: false ]
    }

    withName: RENAME_READS {
        publishDir = [
            enabled: false,
            path: { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
        ]
    }


    // use explicit (params.my_option instanceof Integer) checks to avoid issues with 0 evaluating false
    // since most pixelator flags do accept zero as a value

    withName: PIXELATOR_AMPLICON {
        ext.args = {
            [
                "--design ${meta.design}",
            ].join(' ').trim()
        }
    }

    withName: PIXELATOR_QC {
        // Options for pixelator preqc
        ext.args = {
            [
                "--design ${meta.design}",
                (params.trim_front instanceof Integer)? "--trim-front ${params.trim_front}": '',
                (params.trim_tail instanceof Integer) ? "--trim-tail ${params.trim_tail}": '',
                (params.max_length instanceof Integer) ? "--max-length ${params.max_length}": '',
                (params.min_length instanceof Integer) ? "--min-length ${params.min_length}": '',
                (params.max_n_bases instanceof Integer) ? "--max-n-bases ${params.max_n_bases}": '',
                (params.avg_qual instanceof Integer)? "--avg-qual ${params.avg_qual}": '',
                params.dedup ? "--dedup": '',
                params.remove_polyg ? "--remove_polyg": '',
            ].join(' ').trim()
        }

        // Options for pixelator adapterqc
        ext.args2 = { [
                "--design ${meta.design}",
                params.adapterqc_mismatches ? "--mismatches ${params.adapterqc_mismatches}": '',
            ].join(' ').trim()
        }
    }

    withName: PIXELATOR_DEMUX {
        ext.args = {
            [
                "--design ${meta.design}",
                (params.demux_mismatches != null) ? "--mismatches ${params.demux_mismatches}": '',
                (params.demux_min_length instanceof Integer) ? "--mismatches ${params.demux_min_length}": '',
            ].join(' ').trim()
        }
    }

    withName: PIXELATOR_COLLAPSE {
        ext.args = [
            params.markers_ignore ? "--markers_ignore ${params.markers_ignore}":
            params.algorithm ? "--algorithm ${params.algorithm}": '',
            params.max_neighbours ? "--max-neighbours ${params.max_neighbours}": '',
            params.collapse_mismatches ? "--mismatches ${params.collapse_mismatches}": '',
            params.collapse_min_count ? "--min-count ${params.collapse_min_count}": '',
            params.collapse_use_counts ? "--use-counts": '',
        ].join(' ').trim()
    }

    withName: PIXELATOR_GRAPH {
        ext.args = [
            params.multiplet_recovery ? "--multiplet-recovery" : '',
            params.leiden_iterations ? "--leiden-iterations ${params.leiden_iterations}" : '',
            params.graph_min_count ? "--min-count ${params.graph_min_count}" : '',
        ].join(' ').trim()
    }

    withName: PIXELATOR_ANNOTATE {
        ext.args = [
            (params.min_size instanceof Integer) ? "--min-size ${params.min_size}" : '',
            (params.max_size instanceof Integer) ? "--max-size ${params.max_size}" : '',
            params.dynamic_filter ? "--dynamic-filter ${params.dynamic_filter}" : '',
            params.aggregate_calling ? "--aggregate-calling" : '',
        ].join(' ').trim()
    }

    withName: PIXELATOR_ANALYSIS {
        ext.when = { !params.skip_analysis }
        ext.args = [
            params.compute_polarization ? "--compute-polarization" : '',
            params.compute_colocalization ? "--compute-colocalization" : '',
            params.use_full_bipartite ? "--use-full-bipartite " : '',
            params.polarization_normalization ? "--polarization-normalization ${params.polarization_normalization}" : '',
            params.polarization_binarization ? "--polarization-binarization" : '',
            params.colocalization_transformation ? "--colocalization-transformation ${params.colocalization_transformation}" : '',
            (params.colocalization_neighbourhood_size instanceof Integer) ? "--colocalization-neighbourhood-size ${params.colocalization_neighbourhood_size}" : '',
            (params.colocalization_n_permutations instanceof Integer) ? "--colocalization-n-permutations ${params.colocalization_n_permutations}" : '',
            (params.colocalization_min_region_count instanceof Integer) ? "--colocalization-min-region-count ${params.colocalization_min_region_count}" : '',
        ].join(' ').trim()
    }

    withName: PIXELATOR_REPORT {
        ext.when = { !params.skip_report }
    }


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