/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: 'FASTQC_PRE_CONVERSION' {
        ext.args        = '--quiet'
        ext.prefix      = { "${meta.id}.pre_conversion" }
        ext.when        = { !params.no_read_QC }
    }

    withName: 'FASTQC_POST_CONVERSION' {
        ext.args        = '--quiet'
        ext.prefix      = { "${meta.id}.post_conversion" }
        ext.when        = { !params.no_read_QC }
    }

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

    withName: 'SAMTOOLS_COLLATEFASTQ_SINGLE_END' {
        ext.args         = { params.samtools_collate_fast ? "-f -r " + params.reads_in_memory : "" }
        ext.args2        = '-N'
        publishDir       = [
            path: { "${params.outdir}/reads" },
            mode: params.publish_dir_mode,
            pattern: '*{other.fq.gz}'
        ]
    }

    withName: 'COLLATE_FASTQ_MAP' {
        ext.args         = { params.samtools_collate_fast ? "-f -r " + params.reads_in_memory : "" }
        ext.args2        = '-N'
        ext.prefix       = {"${meta.id}.mapped"}
        publishDir       = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'COLLATE_FASTQ_UNMAP' {
        ext.args          = { params.samtools_collate_fast ? "-f -r " + params.reads_in_memory : "" }
        ext.args2         = '-N'
        ext.prefix        = { "${meta.id}.unmapped" }
        publishDir        = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_VIEW_MAP_MAP' {
        ext.args           = '-b -f1 -F12'
        ext.prefix         = { "${meta.id}.map_map" }
        publishDir         = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_FLAGSTAT' {
        ext.when = { !params.no_stats }
        publishDir         = [
            path: { "${params.outdir}/samtools" },
            mode: params.publish_dir_mode,
            pattern: '*.flagstat'
            ]
    }

    withName: 'SAMTOOLS_IDXSTATS' {
        ext.when = { !params.no_stats }
        publishDir         = [
            path: { "${params.outdir}/samtools" },
            mode: params.publish_dir_mode,
                pattern: '*.idxstats'
        ]
    }

    withName: 'SAMTOOLS_STATS' {
        ext.when = { !params.no_stats }
        publishDir         = [
            path: { "${params.outdir}/samtools" },
            mode: params.publish_dir_mode,
            pattern: '*.stats'
        ]
    }

    withName: 'SAMTOOLS_INDEX' {
        publishDir         = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_FAIDX' {
        publishDir          = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }


    withName: 'SAMTOOLS_VIEW_MAP_UNMAP' {
        ext.args            = '-b -f8 -F260'
        ext.prefix          = { "${meta.id}.map_unmap" }
        publishDir          = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_VIEW_UNMAP_MAP' {
        ext.args             = '-b -f4 -F264'
        ext.prefix           = { "${meta.id}.unmap_map" }
        publishDir           = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_VIEW_UNMAP_UNMAP' {
        ext.args             = '-b -f12 -F256'
        ext.prefix           = { "${meta.id}.unmap_unmap" }
        publishDir           = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_MERGE_UNMAP' {
        ext.prefix           = { "${meta.id}.merged_unmap" }
        publishDir           = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'CAT_FASTQ' {
        publishDir          = [
            path: { "${params.outdir}/reads" },
            mode: params.publish_dir_mode,
            pattern: '*.fastq.gz',
            saveAs: { filename -> filename.indexOf(".fastq.gz") > 0 ? filename : null },
            enabled: true
        ]
    }

    withName: 'CHECK_IF_PAIRED_END'{
        publishDir          = [
            //specify to avoid publishing, overwritten otherwise
            enabled: false
        ]
    }

    withName: 'SAMTOOLS_CHR'{
        ext.args            = "-hb"
        ext.args2           = "${params.chr}"
        ext.prefix          = { params.chr.split(' |-|:').size() > 1 ? "${meta.id}."+(params.chr.split(' |-|:').join('_')) : "${meta.id}."+(params.chr) }
    }

}
