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

    withName: FASTQC_POSTASSEMBLY {
        publishDir = [
            path: { "${params.outdir}/fastqc/postassembly" },
        ]
        ext.args = '--quiet'
    }

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

    if (params.subworkflow == "bcellmagic") {
        withName: PRESTO_FILTERSEQ {
            publishDir = [
                path: { "${params.outdir}/presto/01-filterseq/${meta.id}" },
            ]
        }

        withName: PRESTO_FILTERSEQ_POSTASSEMBLY_SANS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/02-filterseq/${meta.id}" },
            ]
        }

        withName: PRESTO_MASKPRIMERS {
            publishDir = [
                path: { "${params.outdir}/presto/02-maskprimers/${meta.id}" },
            ]
        }

        withName: PRESTO_MASKPRIMERS_POSTASSEMBLY_SANS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/03-maskprimers/${meta.id}" },
            ]
        }

        withName: PRESTO_PAIRSEQ {
            publishDir = [
                path: { "${params.outdir}/presto/03-pairseq/${meta.id}" },
            ]
        }

        withName: PRESTO_CLUSTERSETS {
            publishDir = [
                path: { "${params.outdir}/presto/04-clustersets/${meta.id}" },
            ]
        }

        withName: PRESTO_PARSE_CLUSTER {
            publishDir = [
                path: { "${params.outdir}/presto/05-parse-clusters/${meta.id}" },
            ]
        }

        withName: PRESTO_BUILDCONSENSUS {
            publishDir = [
                path: { "${params.outdir}/presto/06-build-consensus/${meta.id}" },
            ]
        }

        withName: PRESTO_POSTCONSENSUS_PAIRSEQ {
            publishDir = [
                path: { "${params.outdir}/presto/07-postconsensus-pairseq/${meta.id}" },
            ]
        }

        withName: PRESTO_ASSEMBLEPAIRS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/08-assemble-pairs/${meta.id}" },
            ]
            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}" },
            ]
            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}" },
            ]
            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}" },
            ]
            ext.args = '-n 20 --inner'
            ext.args2 = '-f HEADER DUPCOUNT'
        }

        withName: PRESTO_SPLITSEQ_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/10-splitseq/${meta.id}" },
            ]
            ext.args = '-f CONSCOUNT --num 2'
        }

        withName: PRESTO_SPLITSEQ_SANS_UMI {
            publishDir = [
                path: { "${params.outdir}/presto/05-splitseq/${meta.id}" },
            ]
            ext.args = '-f DUPCOUNT --num 2'
        }

        withName: CHANGEO_ASSIGNGENES {
            publishDir = [
                path: { "${params.outdir}/changeo/01-assign-genes/${meta.id}" },
            ]
            ext.args = '--format blast'
        }

        withName: CHANGEO_MAKEDB {
            publishDir = [
                path: { "${params.outdir}/changeo/02-make-db/${meta.id}" },
            ]
            ext.args = '--regions default --format airr'
        }

        withName: CHANGEO_PARSEDB_SPLIT {
            publishDir = [
                path: { "${params.outdir}/changeo/03-parsedb-split/${meta.id}" },
            ]
        }

        withName: CHANGEO_PARSEDB_SELECT {
            publishDir = [
                path: { "${params.outdir}/changeo/04-parsedb-select/${meta.id}" },
            ]
            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}" },
            ]
            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}" },
            ]
        }

        withName: SHAZAM_THRESHOLD {
            publishDir = [
                path: { "${params.outdir}/shazam/02-clonal-threshold/${meta.id}" },
            ]
        }

        withName: CHANGEO_DEFINECLONES {
            publishDir = [
                path: { "${params.outdir}/changeo/06-define-clones/${meta.id}" },
            ]
        }

        withName: CHANGEO_CREATEGERMLINES {
            publishDir = [
                path: { "${params.outdir}/changeo/07-create-germlines/${meta.id}" },
            ]
        }

        withName: CHANGEO_BUILDTREES {
            publishDir = [
                path: { "${params.outdir}/changeo/08-build-trees/${meta.id}" },
            ]
            ext.args = '--igphyml --collapse'
        }

        withName: ALAKAZAM_LINEAGE {
            publishDir = [
                path: { "${params.outdir}/lineage-reconstruction/${meta.id}" },
            ]
            ext.args = 'none'
        }

        withName: ALAKAZAM_SHAZAM_REPERTOIRES {
            publishDir = [
                path: { "${params.outdir}/repertoire_analysis" },
            ]
            ext.args = 'none'
        }

        withName: FETCH_DATABASES {
            publishDir = [
                path: { "${params.outdir}/databases" },
                enabled: params.save_databases
            ]
        }

        withName: PARSE_LOGS {
            publishDir = [
                path: { "${params.outdir}/parsed-logs" },
            ]
        }
    }

// REVEAL modules

    if ( params.subworkflow == "reveal" ) {
        withName: IMMCANTATION {
            publishDir = [
                path: { "${params.outdir}/immcantation_version" },
            ]
        }

        withName: CHANGEO_ASSIGNGENES_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/assign_genes/${meta.id}" },
            ]
            ext.args = '--format blast'
        }

        withName: CHANGEO_MAKEDB_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/makedb-igblast/${meta.id}" },
            ]
        }

        withName: CHANGEO_CONVERTDB_FASTA_FROM_AIRR {
            publishDir = [
                path: { "${params.outdir}/changeo/convert-db/${meta.id}" },
            ]
            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}" },
            ]
        }

        withName: FILTER_JUNCTION_MOD3 {
            publishDir = [
                path: { "${params.outdir}/changeo/quality-filter/${meta.id}" },
            ]
        }

        withName: CHANGEO_CREATEGERMLINES_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/chimera-filter/${meta.id}" },
            ]
        }

        withName: SINGLE_CELL_QC {
            publishDir = [
                path: { "${params.outdir}/changeo/single-cell-qc" },
            ]
        }

        withName: ADD_META_TO_TAB {
            publishDir = [
                path: { "${params.outdir}/changeo/metadata-repertoire/${meta.id}" },
            ]
        }

        withName: CHANGEO_PARSEDB_SPLIT_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/parsedb_split/${meta.id}" },
            ]
        }
        withName: COLLAPSE_DUPLICATES {
            publishDir = [
                path: { "${params.outdir}/collapse_duplicates/${meta.id}" },
            ]
        }

        withName: DEFINE_CLONES {
            publishDir = [
                path: { "${params.outdir}/changeo/define_clones" },
            ]
            ext.args = ['outname':'', 'model':'hierarchical', 'method':'nt', 'linkage':'single']
        }

        withName: DETECT_CONTAMINATION_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/detect_contamination/${meta.id}" },
            ]
        }

        withName: DOWSER_LINEAGES {
            publishDir = [
                path: { "${params.outdir}/dowser/lineages" },
            ]
            ext.args = ['build':'igphyml']
        }

        withName: FIND_THRESHOLD {
            publishDir = [
                path: { "${params.outdir}/changeo/find_threshold" },
            ]
        }

        withName: REMOVE_CHIMERIC_REVEAL {
            publishDir = [
                path: { "${params.outdir}/changeo/chimera-filter/${meta.id}" },
            ]
        }

        withName: REPORT_FILE_SIZE {
            publishDir = [
                path: { "${params.outdir}/report_file_size" },
            ]
        }

        withName: VALIDATE_INPUT {
            publishDir = [
                path: { "${params.outdir}/validated_input" },
            ]
        }
    }

}
