/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: PBCCS {
        publishDir = [
            path: { "${params.outdir}/01_PBCCS" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = " --min-rq ${params.rq}"
    }

    withName: LIMA {
        publishDir = [
            path: { "${params.outdir}/02_LIMA" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = "--isoseq --peek-guess"
        ext.prefix = { "${meta.id}_flnc" }
    }

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

    withName: BAMTOOLS_CONVERT {
        publishDir = [
            path: { "${params.outdir}/04_BAMTOOLS_CONVERT" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = "-format fasta"
    }

    withName: GSTAMA_POLYACLEANUP {
        publishDir = [
            path: { "${params.outdir}/05_GSTAMA_POLYACLEANUP" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${meta.id}_tama" }
    }

    withName: MINIMAP2_ALIGN {
        publishDir = [
            path: { "${params.outdir}/06_MINIMAP2" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = "-x splice:hq -uf --secondary=no -a"
    }

    withName: GUNZIP {
        publishDir = [
            path: { "${params.outdir}/06.1_GUNZIP" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix = { "${meta.id}_tama" }
    }

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

    withName: ULTRA_ALIGN {
        publishDir = [
            path: { "${params.outdir}/06.2_ULTRA" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = "--isoseq"
    }

    if (params.capped == true) {
        withName: GSTAMA_COLLAPSE {
            publishDir = [
                path: { "${params.outdir}/07_GSTAMA_COLLAPSE" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = "-x capped -b BAM -a ${params.five_prime} -m ${params.splice_junction} -z ${params.three_prime}"
        }
    } else {
        withName: GSTAMA_COLLAPSE {
            publishDir = [
                path: { "${params.outdir}/07_GSTAMA_COLLAPSE" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = "-x no_cap -b BAM -a ${params.five_prime} -m ${params.splice_junction} -z ${params.three_prime}"
        }
    }

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

    withName: GSTAMA_MERGE {
        publishDir = [
            path: { "${params.outdir}/09_GSTAMA_MERGE" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args = "-a ${params.five_prime} -m ${params.splice_junction} -z ${params.three_prime}"
    }

    withName: SAMPLESHEET_CHECK {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            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'
        ]
    }

}
