/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: FASTQC {
        ext.args = '--quiet'
    }

    withName: FASTP {
        ext.prefix = { "${meta.id}.trimmed" }
    }

    withName: GFFREAD {
        ext.args = "--keep-exon-attrs -F -T"
    }

    withName: BWA_MEM {
        publishDir = [
            [
                path: { "${params.outdir}/${params.aligner}/log" },
                mode: 'copy',
                pattern: "out"
            ],
            [
                path: { "${params.outdir}/${params.aligner}/log" },
                mode: 'copy',
                pattern: "tab"
            ]
        ]
    }

    withName: 'DRAGMAP_HASHTABLE' {
        ext.when         = { !params.dragmap && params.aligner == "dragmap" }
        publishDir = [
            enabled: params.save_reference,
            mode: params.publish_dir_mode,
            path: { "${params.outdir}/reference" },
            pattern: "dragmap"
        ]
    }

    withName: ".*DRAGMAP_ALIGN" {
        ext.when         = { params.aligner == "dragmap" }
        ext.args         = { "--RGSM ${meta.read_group}" }
    }

    if(params.with_umi) {
        withName: '.*:BAM_DEDUP_STATS_SAMTOOLS_UMITOOLS:UMITOOLS_DEDUP' {
            ext.args = { [
                meta.single_end ? '' : '--unpaired-reads=discard --chimeric-pairs=discard',
            ].join(' ').trim() }
            ext.prefix = { "${meta.id}.umi_dedup.sorted" }
        }

        withName: '.*:BAM_DEDUP_STATS_SAMTOOLS_UMITOOLS:SAMTOOLS_INDEX' {
            ext.prefix = { "${meta.id}.umi_dedup.sorted" }
        }

        if(params.umitools_dedup_stats) {
            withName: '.*:BAM_DEDUP_STATS_SAMTOOLS_UMITOOLS:BAM_STATS_SAMTOOLS:.*' {
                ext.prefix = { "${meta.id}.umi_dedup.sorted.bam" }
            }
        }
    }

    withName: SAMTOOLS_SORT {
        ext.prefix = { "${meta.id}.sorted" }
    }

    withName: PRESEQ_CCURVE {
        ext.args = "-B"
    }

    withName: PRESEQ_LCEXTRAP {
        ext.args = "-B"
    }

    withName: BEDTOOLS_GENOMECOV_PLUS {
        ext.args = "-bg -strand +"
        ext.prefix = { "${meta.id}.plus" }
    }

    withName: BEDTOOLS_GENOMECOV_MINUS {
        ext.args = "-bg -strand -"
        ext.prefix = { "${meta.id}.minus" }
    }

    withName: DEEPTOOLS_BAMCOVERAGE_PLUS {
        ext.args = "--filterRNAstrand forward"
        ext.prefix = { "${meta.id}.plus.bigWig" }
    }

    withName: DEEPTOOLS_BAMCOVERAGE_MINUS {
        ext.args = "--filterRNAstrand reverse"
        ext.prefix = { "${meta.id}.minus.bigWig" }
    }

    withName: SUBREAD_FEATURECOUNTS_GENE {
        ext.args = "-B -C"
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/featurecounts/gene" },
            mode: 'copy',
        ]
    }

    withName: SUBREAD_FEATURECOUNTS_PREDICTED {
        ext.prefix = { "${meta.id}-group_${annotation.baseName}-transcripts" }
        ext.args = '-F "SAF"'
        publishDir = [
            path: { "${params.outdir}/${params.aligner}/featurecounts/predicted" },
            mode: 'copy',
        ]
    }

    withName: HOMER_FINDPEAKS {
        // TODO Handle other assays
        ext.args = "-style groseq"
    }

    withName: HOMER_MAKETAGDIRECTORY {
        ext.args = "-checkGC"
    }

    withName: HOMER_MAKEUCSCFILE {
        ext.args = "-strand separate"
    }

    withName: PINTS_CALLER {
        ext.args = { "--exp-type $params.assay_type" }
        // FIXME The singularity image doesn't exist on biocontainers yet
        // https://github.com/hyulab/PINTS/issues/3
        ext.singularity_pull_docker_container = true
    }

    withName: BEDTOOLS_SORT {
        ext.prefix = { "${meta.id}_sorted" }
    }

    withName: BEDTOOLS_MERGE {
        ext.args = "-header"
        ext.prefix = { "${meta.id}_merged" }
    }

    withName: BEDTOOLS_INTERSECT_FILTER {
        ext.prefix = { "${meta.id}_filtered" }
        ext.args = { params.no_overlap ? "-v": "" }
    }

}
