/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: ORIENT_REFERENCE {
        ext.prefix = { params.reference_fasta ? "${reference.baseName}" : "${meta.id}_reference" }
        publishDir = [
            path: { "${params.outdir}/preprocessing/sequences" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }

    withName: CAT_FASTQ {
        publishDir = [
            path: { "${params.outdir}/preprocessing/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: PEAR {
        publishDir = [
            path: { "${params.outdir}/preprocessing/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

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

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

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

    withName: FASTQC {
        ext.args = '--quiet'
        publishDir = [
            path: { "${params.outdir}/preprocessing/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

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

    }

    withName: MAGECK_MLE {
        publishDir       = [
            path: { "${params.outdir}/mageck/mle/${meta.id}/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
        ext.args   =
            params.crisprcleanr ? '--norm-method none' : ''

    }

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

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

    withName: CUTADAPT {
        ext.args = '-g file:overrepresented.fasta -N'
        publishDir = [
            path: { "${params.outdir}/preprocessing/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: SEQTK_SEQ_MASK {
        ext.args = '-q 20 -L 80 -n N'
        publishDir = [
            path: { "${params.outdir}/preprocessing/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: EXTRACT_UMIS {
        ext.args = '--max-error 3 --adapter-length 250 --fwd-context ""'
    }

    withName: VSEARCH_CLUSTER {
        ext.args = "--minseqlength ${params.vsearch_minseqlength} --maxseqlength ${params.vsearch_maxseqlength} --qmask none --clusterout_sort --gapopen 0E/5I --gapext 0E/2I --mismatch -8 --match 6 --iddef 0 --minwordmatches 0 --qmask none --id ${params.vsearch_id}"
        ext.args2 = '--cluster_fast'
        ext.args3 = '--clusters'
    }


    withName: VSEARCH_SORT {
        ext.args = '--topn 1'
        ext.prefix = { "${fasta.baseName}_top" }
    }

    withName: PREPROCESSING_SUMMARY {
        publishDir = [
            path: { "${params.outdir}/summary/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }

    withName: MINIMAP2_ALIGN_UMI_1 {
        ext.args = '-x map-ont'
        ext.prefix = { "${reads.baseName}_cycle1" }
        publishDir = [
            path: { "${params.outdir}/minimap2_umi" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: MINIMAP2_ALIGN_UMI_2 {
        ext.args = '-x map-ont'
        ext.prefix = { "${reads.baseName}_cycle2" }
        publishDir = [
            path: { "${params.outdir}/minimap2_umi" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: RACON_1 {
        ext.args = '-t 4 -m 8 -x -6 -g -8 -w 500 --no-trimming'
        ext.prefix = { "${reads.baseName}_cycle1" }
        publishDir = [
            path: { "${params.outdir}/racon_umi" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: RACON_2 {
        ext.args = '-t 4 -m 8 -x -6 -g -8 -w 500 --no-trimming'
        ext.prefix = { "${reads.baseName}_cycle2" }
        publishDir = [
            path: { "${params.outdir}/racon_umi" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: MEDAKA {
        ext.args = '-m r941_min_high_g303'
        ext.prefix = { "${reads.baseName}_medakaConsensus" }
    }

    withName: SEQTK_SEQ_FATOFQ {
        ext.args = '-F "#"'
        publishDir = [
            path: { "${params.outdir}/preprocessing/UMI" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }

    withName: CLUSTERING_SUMMARY {
        publishDir = [
            path: { "${params.outdir}/summary/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }

    withName: MINIMAP2_ALIGN_ORIGINAL {
        ext.args = '-A 29 -B 17 -O 25 -E 2'
    }

    withName: ALIGNMENT_SUMMARY {
        publishDir = [
            path: { "${params.outdir}/summary/${task.process.tokenize(':')[-1].tokenize('_')[0].toLowerCase()}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

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

    withName: MINIMAP2_ALIGN_TEMPLATE {
        ext.args = '-A 29 -B 17 -O 25 -E 2'
        publishDir = [
            path: { "${params.outdir}/preprocessing/sequences" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: CIGAR_PARSER {
        ext.args = '--cut_site=-3'
    }

    withName: CRISPRSEQ_PLOTTER {
        ext.args = '--cut_site=-3'
        publishDir = [
            path: { "${params.outdir}/plots" },
            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: 'MULTIQC' {
        ext.args   = params.multiqc_title ? "--title \"$params.multiqc_title\"" : ''
        publishDir = [
            path: { "${params.outdir}/multiqc" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

}
