/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: ARRIBA {
        publishDir = [
            path: { "${params.outdir}/arriba" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.prefix =  { "${meta.id}.arriba" }
    }

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

    withName: ARRIBA_VISUALISATION {
        ext.when         = { !params.fusioninspector_only && (params.starfusion || params.all) }
        publishDir = [
            path: { "${params.outdir}/arriba_visualisation" },
            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: ENSEMBL_DOWNLOAD {
        publishDir = [
            path: { "${params.genomes_base}/ensembl" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: FASTP {
        ext.args = params.trim_tail ? "--trim_tail1 ${params.trim_tail}" : ''
    }

    withName: FASTQC {
        ext.args = '--quiet'
        ext.when         = { !params.skip_qc }
    }

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

    withName: FUSIONCATCHER {
        ext.args = "--limitSjdbInsertNsj ${params.fusioncatcher_limitSjdbInsertNsj}"
    }

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

    withName: FUSIONINSPECTOR {
        ext.when = { !params.skip_vis }
        ext.args = { params.fusioninspector_limitSjdbInsertNsj != 1000000 ? "--STAR_xtra_params \"--limitSjdbInsertNsj ${params.fusioninspector_limitSjdbInsertNsj}\"" : '' }

    }

    withName: FUSIONREPORT {
        ext.when         = { !params.skip_vis }
        ext.args         = "--export csv"
        ext.args2        = { params.fusionreport_filter ? "--tool-cutoff 2" : "--tool-cutoff 1"}
        publishDir = [
            path: { "${params.outdir}/fusionreport/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: FUSIONREPORT_DOWNLOAD {
        ext.args        = { params.qiagen ? "--qiagen" : "" }
        publishDir = [
            path: { "${params.genomes_base}/fusion_report_db" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

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

    withName: KALLISTO_INDEX {
        ext.args = '-k 31'
        publishDir = [
            path: { "${params.genomes_base}/pizzly" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }
    withName: MEGAFUSION {
        ext.when = {!params.fusioninspector_only}
    }



    withName: MULTIQC {
        ext.when         = { !params.skip_qc }
    }

    withName: PICARD_COLLECTRNASEQMETRICS {
        ext.when         = { !params.skip_qc && !params.fusioninspector_only && (params.starfusion || params.all) }

    }

    withName: PICARD_MARKDUPLICATES {
        ext.when         = { !params.skip_qc && !params.fusioninspector_only && (params.starfusion || params.all) }
    }

    withName: PIZZLY {
        ext.args = "-k 31 --align-score 2 --insert-size 400 --cache index.cache.txt"
        publishDir = [
            path: { "${params.outdir}/pizzly" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }

    withName: QUALIMAP_RNASEQ {
        ext.when         = { !params.skip_qc && !params.fusioninspector_only && (params.starfusion || params.all)}
    }

    withName: REFORMAT {
        ext.args = "forcetrimright=75"
        ext.args2 = "forcetrimleft=75"
    }

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

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

    withName: SAMTOOLS_INDEX_FOR_QC {
        ext.when         = { !params.skip_qc && !params.fusioninspector_only && (params.starfusion || params.all)}
        publishDir = [
            path: { "${params.outdir}/samtools_index_for_qc" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

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

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

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

    withName: SAMTOOLS_VIEW_FOR_ARRIBA {
        ext.args = { "--output-fmt cram" }
        publishDir = [
            path: { "${params.outdir}/cram_arriba" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: SAMTOOLS_VIEW_FOR_SQUID_CHIMERIC {
        ext.prefix = { "${meta.id}_chimeric" }
        ext.args = { "--output-fmt bam" }
        publishDir = [
            path: { "${params.outdir}/samtools_view_for_squid_chimeric" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: SAMTOOLS_VIEW_FOR_SQUID_CRAM {
        ext.args = { "--output-fmt cram" }
        publishDir = [
            path: { "${params.outdir}/cram_squid" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: SAMTOOLS_VIEW_FOR_SQUID_CRAM_CHIMERIC {
        ext.args = { "--output-fmt cram" }
        ext.prefix = { "${meta.id}_chimeric" }
        publishDir = [
            path: { "${params.outdir}/cram_squid" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: SAMTOOLS_VIEW_FOR_STARFUSION {
        ext.args = { "--output-fmt cram" }
        publishDir = [
            path: { "${params.outdir}/cram_starfusion" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: STAR_FOR_ARRIBA {
        publishDir = [
            path: { "${params.outdir}/star_for_arriba" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
        ext.args = '--readFilesCommand zcat \
        --outSAMtype BAM Unsorted \
        --outSAMunmapped Within \
        --outBAMcompression 0 \
        --outFilterMultimapNmax 50 \
        --peOverlapNbasesMin 10 \
        --alignSplicedMateMapLminOverLmate 0.5 \
        --alignSJstitchMismatchNmax 5 -1 5 5 \
        --chimSegmentMin 10 \
        --chimOutType WithinBAM HardClip \
        --chimJunctionOverhangMin 10 \
        --chimScoreDropMax 30 \
        --chimScoreJunctionNonGTAG 0 \
        --chimScoreSeparation 1 \
        --chimSegmentReadGapMax 3 \
        --chimMultimapNmax 50'
    }

    withName: STAR_FOR_SQUID {
        publishDir = [
            path: { "${params.outdir}/star_for_squid" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
        ext.args = '--twopassMode Basic \
        --chimOutType SeparateSAMold \
        --chimSegmentMin 20 \
        --chimJunctionOverhangMin 12 \
        --alignSJDBoverhangMin 10 \
        --outReadsUnmapped Fastx \
        --outSAMstrandField intronMotif \
        --outSAMtype BAM SortedByCoordinate \
        --readFilesCommand zcat'
    }

    withName: STAR_FOR_STARFUSION {
        publishDir = [
            path: { "${params.outdir}/star_for_starfusion" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
        ext.args = '--twopassMode Basic \
        --outReadsUnmapped None \
        --readFilesCommand zcat \
        --outSAMtype BAM SortedByCoordinate \
        --outSAMstrandField intronMotif \
        --outSAMunmapped Within \
        --chimSegmentMin 12 \
        --chimJunctionOverhangMin 8 \
        --chimOutJunctionFormat 1 \
        --alignSJDBoverhangMin 10 \
        --alignMatesGapMax 100000 \
        --alignIntronMax 100000 \
        --alignSJstitchMismatchNmax 5 -1 5 5 \
        --chimMultimapScoreRange 3 \
        --chimScoreJunctionNonGTAG -4 \
        --chimMultimapNmax 20 \
        --chimNonchimScoreDropMin 10 \
        --peOverlapNbasesMin 12 \
        --peOverlapMMp 0.1 \
        --alignInsertionFlush Right \
        --alignSplicedMateMapLminOverLmate 0 \
        --alignSplicedMateMapLmin 30 \
        --chimOutType Junctions'
    }

    withName: STAR_GENOMEGENERATE {
        ext.args = "--sjdbOverhang ${params.read_length - 1}"
        cpus   = { check_max( 24    * task.attempt, 'cpus'    ) }
        memory = { check_max( 100.GB * task.attempt, 'memory'  ) }
        time   = { check_max( 2.d  * task.attempt, 'time'    ) }
        publishDir = [
            path: { "${params.genomes_base}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }

    withName: STARFUSION_BUILD {
        cpus   = { check_max( 24    * task.attempt, 'cpus'    ) }
        memory = { check_max( 100.GB * task.attempt, 'memory'  ) }
        time   = { check_max( 2.d  * task.attempt, 'time'    ) }
        publishDir = [
            path: { "${params.genomes_base}/starfusion" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }

    withName: STARFUSION_DOWNLOAD {
        cpus   = { check_max( 2    * task.attempt, 'cpus'    ) }
        memory = { check_max( 24.GB * task.attempt, 'memory'  ) }
        time   = { check_max( 6.h  * task.attempt, 'time'    ) }
        publishDir = [
            path: { "${params.genomes_base}/starfusion" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
        ]
    }

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