/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: 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'
        ]
    }
    withName: 'MTX_TO_H5AD|CONCAT_H5AD|MTX_TO_SEURAT' {
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/mtx_conversions" },
            mode: params.publish_dir_mode
        ]
    }
    withName: 'GTF_GENE_FILTER' {
        publishDir = [
            path: { "${params.outdir}/gtf_filter" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }
}

if(params.aligner == "cellranger") {
    process {
        withName: CELLRANGER_MKGTF {
            publishDir = [
                path: "${params.outdir}/${params.aligner}/mkgtf",
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = "--attribute=gene_biotype:protein_coding --attribute=gene_biotype:lncRNA --attribute=gene_biotype:pseudogene"
        }
        withName: CELLRANGER_MKREF {
            publishDir = [
                path: "${params.outdir}/${params.aligner}/mkref",
                mode: params.publish_dir_mode
            ]
        }
        withName: CELLRANGER_COUNT {
            publishDir = [
                path: "${params.outdir}/${params.aligner}/count",
                mode: params.publish_dir_mode
            ]
            ext.args = {meta.expected_cells ? "--expect-cells ${meta.expected_cells}" : ''}
        }
    }
}

if(params.aligner == "universc") {
    process {
        publishDir = { "${params.outdir}/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" }

        withName: CELLRANGER_MKGTF {
            publishDir = [
                path: "${params.outdir}/cellranger/mkgtf",
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = "--attribute=gene_biotype:protein_coding --attribute=gene_biotype:lncRNA --attribute=gene_biotype:pseudogene"
            container = "nf-core/universc:1.2.5.1"
        }
        withName: CELLRANGER_MKREF {
            publishDir = [
                path: "${params.outdir}/cellranger/mkref",
                mode: params.publish_dir_mode
            ]
            container = "nf-core/universc:1.2.5.1"
        }
        withName: UNIVERSC {
            publishDir = [
                path: "${params.outdir}/universc",
                mode: params.publish_dir_mode
            ]
        }
    }
}

if (params.aligner == "alevin") {
    process {
        withName: GFFREAD_TXP2GENE {
            ext.args = "--table transcript_id,gene_id"
            ext.prefix = { "${gff.baseName}_gffread" }
        }
        withName: 'SIMPLEAF_INDEX' {
            publishDir = [
                path: { "${params.outdir}/${params.aligner}" },
                mode: params.publish_dir_mode,
                enabled: params.save_reference
            ]
            ext.args = { "--rlen ${params.simpleaf_rlen}" }
        }
        withName: 'SIMPLEAF_QUANT' {
            publishDir = [
                path: { "${params.outdir}/${params.aligner}" },
                mode: params.publish_dir_mode
            ]
            ext.args = "-r cr-like"
        }
    }
}

if (params.aligner == "star") {
    process {
        withName: STAR_ALIGN {
            ext.args = "--readFilesCommand zcat --runDirPerm All_RWX --outWigType bedGraph --twopassMode Basic --outSAMtype BAM SortedByCoordinate"
        }
        withName: STAR_GENOMEGENERATE {
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/genome_generate" },
                mode: params.publish_dir_mode,
                enabled: params.save_reference
            ]
        }
        withName: STAR_ALIGN {
            publishDir = [
                path: { "${params.outdir}/${params.aligner}/${meta.id}" },
                mode: params.publish_dir_mode
            ]
        }
    }
}

if (params.aligner == 'kallisto') {
    process {
        withName: KALLISTOBUSTOOLS_REF {
            publishDir = [
                path: { "${params.outdir}/${params.aligner}" },
                mode: params.publish_dir_mode,
                enabled: params.save_reference
            ]
        }
        withName: KALLISTOBUSTOOLS_COUNT {
            publishDir = [
                path: { "${params.outdir}/${params.aligner}" },
                mode: params.publish_dir_mode
            ]
            ext.args = "--workflow ${params.kb_workflow}"
        }
    }
}
