/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: CUSTOM_DUMPSOFTWAREVERSIONS {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            mode: params.publish_dir_mode,
            pattern: '*_versions.yml'
        ]
    }

    if (params.subworkflow == "bcellmagic") {

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

        withName: FASTQC {
            ext.args = '--quiet'
        }

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

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

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

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

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

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

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

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

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

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

        withName: PRESTO_ASSEMBLEPAIRS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/08-assemble-pairs/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--coord presto --rc tail --1f CONSCOUNT PRCONS --2f CONSCOUNT PRCONS'
            ext.args2 = '-f ID BARCODE SEQCOUNT PRIMER PRCOUNT PRCONS PRFREQ CONSCOUNT LENGTH OVERLAP ERROR PVALUE'
        }

        withName: PRESTO_ASSEMBLEPAIRS_SANS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/01-assemble-pairs/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--rc tail'
            ext.args2 = '-f ID SEQCOUNT PRIMER PRCOUNT PRFREQ LENGTH OVERLAP ERROR PVALUE'
        }

        withName: PRESTO_PARSEHEADERS_COLLAPSE_UMI {
            publishDir = [
                enabled: false
            ]
            ext.subcommand = 'collapse'
            ext.args = '-f CONSCOUNT --act min'
        }

        withName: PRESTO_PARSEHEADERS_PRIMERS_UMI {
            publishDir = [
                enabled: false
            ]
            ext.args = 'PRCONS PRCONS'
        }

        withName: PRESTO_PARSEHEADERS_PRIMERS_SANS_UMI {
            publishDir = [
                enabled: false
            ]
            ext.args = 'PRIMER PRIMER'
        }

        withName: PRESTO_PARSEHEADERS_METADATA {
            publishDir = [
                enabled: false
            ]
            ext.args = '-f sample_id subject_id species pcr_target_locus'
        }

        withName: PRESTO_COLLAPSESEQ_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/09-collapseseq/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '-n 20 --inner --uf PRCONS --cf CONSCOUNT --act sum'
            ext.args2 = '-f HEADER DUPCOUNT'
        }

        withName: PRESTO_COLLAPSESEQ_SANS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/04-collapseseq/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '-n 20 --inner'
            ext.args2 = '-f HEADER DUPCOUNT'
        }

        withName: PRESTO_SPLITSEQ_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/10-splitseq/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '-f CONSCOUNT --num 2'
        }

        withName: PRESTO_SPLITSEQ_SANS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/05-splitseq/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '-f DUPCOUNT --num 2'
        }

        withName: CHANGEO_ASSIGNGENES {
            publishDir = [
                path: { "${params.outdir}/changeo/01-assign-genes/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--format blast'
        }

        withName: CHANGEO_MAKEDB {
            publishDir = [
                path: { "${params.outdir}/changeo/02-make-db/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--regions default --format airr'
        }

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

        withName: CHANGEO_PARSEDB_SELECT {
            publishDir = [
                path: { "${params.outdir}/changeo/04-parsedb-select/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '-f v_call j_call -u "IG[HLK]" --regex --logic all'
            ext.args2 = '-f v_call j_call -u "TR" --regex --logic all'
        }

        withName: CHANGEO_CONVERTDB_FASTA {
            publishDir = [
                path: { "${params.outdir}/changeo/05-convertdb-fasta/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--if sequence_id --sf sequence_alignment --mf v_call dupcount'
        }

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

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

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

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

        withName: CHANGEO_BUILDTREES {
            publishDir = [
                path: { "${params.outdir}/changeo/08-build-trees/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--igphyml --collapse'
        }

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

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

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

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

// REVEAL modules

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

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

        withName: CHANGEO_ASSIGNGENES_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/assign_genes/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--format blast'
        }

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

        withName: CHANGEO_CONVERTDB_FASTA_FROM_AIRR {
            publishDir = [
                path: { "${params.outdir}/changeo/convert-db/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = '--if sequence_id --sf sequence --mf cell_id consensus_count duplicate_count c_call c_cigar c_sequence_start c_sequence_end'
        }

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

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

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

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

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

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

        withName: DEFINE_CLONES {
            publishDir = [
                path: { "${params.outdir}/changeo/define_clones" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
            ext.args = ['outname':'', 'model':'hierarchical', 'method':'nt', 'linkage':'single']
        }

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

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

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

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

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

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

}
