/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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: UNTAR {
        ext.prefix = { "${archive.simpleName}" }
        publishDir = [
            path: { "${params.outdir}/untar/databases" },
            mode: params.publish_dir_mode,
            enabled: params.save_untarred_databases
        ]
    }

    withName: FASTQC {
        ext.args = '--quiet'
        ext.prefix = { "${meta.id}_${meta.run_accession}_raw" }
        publishDir = [
            path: { "${params.outdir}/fastqc/raw" },
            mode: params.publish_dir_mode,
            pattern: '*.{html,zip}'
        ]
    }

    withName: FASTQC_PROCESSED {
        ext.args = '--quiet'
        ext.prefix = { "${meta.id}_${meta.run_accession}_processed" }
        publishDir = [
            path: { "${params.outdir}/fastqc/processed" },
            mode: params.publish_dir_mode,
            pattern: '*.{html,zip}'
        ]
    }

    withName: FALCO {
        ext.prefix = { "${meta.id}_${meta.run_accession}_raw_falco" }
        publishDir = [
            path: { "${params.outdir}/falco/raw" },
            mode: params.publish_dir_mode,
            pattern: '*.{html,txt,zip}'
        ]
    }

    withName: FALCO_PROCESSED {
        ext.prefix = { "${meta.id}_${meta.run_accession}_processed_falco" }
        publishDir = [
            path: { "${params.outdir}/falco/processed" },
            mode: params.publish_dir_mode,
            pattern: '*.{html,txt,zip}'
        ]
    }

    withName: FASTP_SINGLE {
        ext.args   = [
            // trimming options
            params.shortread_qc_skipadaptertrim ? "--disable_adapter_trimming" : "",
            params.shortread_qc_adapterlist ? "" : params.shortread_qc_adapter1 ? "--adapter_sequence ${params.shortread_qc_adapter1}" : "",
            // filtering options
            "--length_required ${params.shortread_qc_minlength}",
            (params.perform_shortread_complexityfilter && params.shortread_complexityfilter_tool == 'fastp') ? "--low_complexity_filter --complexity_threshold ${params.shortread_complexityfilter_fastp_threshold}" : '',
            params.shortread_qc_dedup ? "--dedup" : ""
        ].join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/fastp" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_preprocessed_reads
            ],
            [
                path: { "${params.outdir}/fastp" },
                mode: params.publish_dir_mode,
                pattern: '*.{log,html,json}'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                // Don't know why `!` doesn't work here, but `== false` makes it work...
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && !params.perform_shortread_complexityfilter && params.perform_shortread_qc && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: FASTP_PAIRED {
        ext.args   = [
            // collapsing options - option to retain singletons
            params.shortread_qc_includeunmerged ? '--include_unmerged' : '',
            // trimming options
            params.shortread_qc_skipadaptertrim ? "--disable_adapter_trimming" : "",
            params.shortread_qc_adapterlist ? "" : params.shortread_qc_adapter1 ? "--adapter_sequence ${params.shortread_qc_adapter1}" : "",
            params.shortread_qc_adapterlist ? "" : params.shortread_qc_adapter2 ? "--adapter_sequence_r2 ${params.shortread_qc_adapter2}" : "--detect_adapter_for_pe",
            // filtering options
            "--length_required ${params.shortread_qc_minlength}",
            params.perform_shortread_complexityfilter && params.shortread_complexityfilter_tool == 'fastp' ? "--low_complexity_filter --complexity_threshold ${params.shortread_complexityfilter_fastp_threshold}" : '',
            params.shortread_qc_dedup ? "--dedup" : ""
        ].join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/fastp" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_preprocessed_reads
            ],
            [
                path: { "${params.outdir}/fastp" },
                mode: params.publish_dir_mode,
                pattern: '*.{log,html,json}'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: params.shortread_qc_mergepairs ? '*merged.fastq.gz' : '*.fastp.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs:  { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && !params.perform_shortread_complexityfilter && params.perform_shortread_qc && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: ADAPTERREMOVAL_SINGLE {
        ext.args   = [
            // trimming options
            params.shortread_qc_skipadaptertrim ? "--adapter1 ''" : params.shortread_qc_adapterlist ? "" : params.shortread_qc_adapter1 ? "--adapter1 ${params.shortread_qc_adapter1}" : "",
            // filtering options
            "--minlength ${params.shortread_qc_minlength}"
        ].join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/adapterremoval" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_preprocessed_reads
            ],
            [
                path: { "${params.outdir}/adapterremoval" },
                mode: params.publish_dir_mode,
                pattern: '*.settings'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*truncated.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && !params.perform_shortread_complexityfilter && params.perform_shortread_qc && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: ADAPTERREMOVAL_PAIRED {
        ext.args   = [
            // collapsing options
            params.shortread_qc_mergepairs ? "--collapse" : "",
            // trimming options
            params.shortread_qc_skipadaptertrim ? "--adapter1 ''" : params.shortread_qc_adapterlist ? "" : params.shortread_qc_adapter1 ? "--adapter1 ${params.shortread_qc_adapter1}" : "", // adding adapter list happens at module input channel level
            params.shortread_qc_skipadaptertrim ? "--adapter2 ''" : params.shortread_qc_adapterlist ? "" : params.shortread_qc_adapter2 ? "--adapter2 ${params.shortread_qc_adapter2}" : "",
            // filtering options
            "--minlength ${params.shortread_qc_minlength}"
        ].join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/adapterremoval" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_preprocessed_reads
            ],
            [
                path: { "${params.outdir}/adapterremoval" },
                mode: params.publish_dir_mode,
                pattern: '*.settings'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*{truncated.fastq,singleton.truncated}.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && !params.perform_shortread_complexityfilter && params.perform_shortread_qc && !params.shortread_qc_mergepairs && params.save_analysis_ready_fastqs ? it : null}
            ]
        ]
    }

    // AdapterRemoval separate output merging
    withName: CAT_FASTQ {
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && !params.perform_shortread_complexityfilter && params.perform_shortread_qc && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: PORECHOP_PORECHOP {
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/porechop" },
                mode: params.publish_dir_mode,
                pattern: '*_porechopped.fastq.gz',
                enabled: params.save_preprocessed_reads
            ],
            [
                path: { "${params.outdir}/porechop" },
                mode: params.publish_dir_mode,
                pattern: '*.log'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*_porechopped.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_longread_hostremoval && params.longread_qc_skipqualityfilter && !params.longread_qc_skipadaptertrim && params.perform_longread_qc && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: FILTLONG {
        ext.args = [
            "--min_length ${params.longread_qc_qualityfilter_minlength}",
            "--keep_percent ${params.longread_qc_qualityfilter_keeppercent}",
            "--target_bases ${params.longread_qc_qualityfilter_targetbases}"
        ]
        .join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}_filtered" }
        publishDir = [
            [
                path: { "${params.outdir}/filtlong" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_preprocessed_reads
            ],
            [
                path: { "${params.outdir}/filtlong" },
                mode: params.publish_dir_mode,
                pattern: '*.log'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_longread_hostremoval && !params.longread_qc_skipqualityfilter && params.perform_longread_qc && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: BBMAP_BBDUK {
        ext.args =  [
                "entropy=${params.shortread_complexityfilter_entropy}",
                "entropywindow=${params.shortread_complexityfilter_bbduk_windowsize}",
                params.shortread_complexityfilter_bbduk_mask ?  "entropymask=t" : "entropymask=f"
            ].join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/bbduk/" },
                mode: params.publish_dir_mode,
                pattern: '*.{fastq.gz}',
                enabled: params.save_complexityfiltered_reads
            ],
            [
                path: { "${params.outdir}/bbduk/" },
                mode: params.publish_dir_mode,
                pattern: '*.log'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && params.shortread_complexityfilter_tool && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: PRINSEQPLUSPLUS {
        ext.args =  [
                params.shortread_complexityfilter_prinseqplusplus_mode == 'dust' ? "-lc_dust=${params.shortread_complexityfilter_prinseqplusplus_dustscore}" : "-lc_entropy=${params.shortread_complexityfilter_entropy}",
                "-trim_qual_left=0 -trim_qual_left=0 -trim_qual_window=0 -trim_qual_step=0",
            ].join(' ').trim()
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/prinseqplusplus/" },
                mode: params.publish_dir_mode,
                pattern: '*{_good_out.fastq.gz,_good_out_R1.fastq.gz,_good_out_R2.fastq.gz}',
                enabled: params.save_complexityfiltered_reads
            ],
            [
                path: { "${params.outdir}/prinseqplusplus/" },
                mode: params.publish_dir_mode,
                pattern: '*.log'
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*{_good_out.fastq.gz,_good_out_R1.fastq.gz,_good_out_R2.fastq.gz}',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && !params.perform_shortread_hostremoval && params.shortread_complexityfilter_tool && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: BOWTIE2_BUILD {
        publishDir = [
            [
            path: { "${params.outdir}/bowtie2/build" },
            mode: params.publish_dir_mode,
            pattern: 'bowtie2',
            enabled: params.save_hostremoval_index
            ]
        ]
    }

    // Saving unmapped reads as FQ comes via input channel!
    withName: BOWTIE2_ALIGN {
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            [
                path: { "${params.outdir}/bowtie2/align" },
                mode: params.publish_dir_mode,
                pattern: '*.log'
            ],
            [
                path: { "${params.outdir}/bowtie2/align" },
                mode: params.publish_dir_mode,
                pattern: '*.bam',
                enabled: params.save_hostremoval_bam
            ],
            [
                path: { "${params.outdir}/bowtie2/align" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_hostremoval_unmapped
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                enabled: params.perform_shortread_hostremoval,
                pattern: '*.fastq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun ) ) && params.perform_shortread_hostremoval && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: MINIMAP2_INDEX {
        ext.args = '-x map-ont'
        publishDir = [
            path: { "${params.outdir}/minimap2/index" },
            mode: params.publish_dir_mode,
            pattern: '*.mmi',
            enabled: params.save_hostremoval_index
        ]
    }

    withName: MINIMAP2_ALIGN {
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            path: { "${params.outdir}/minimap2/align" },
            mode: params.publish_dir_mode,
            pattern: '*.bam',
            enabled: params.save_hostremoval_bam
        ]
    }

    withName: SAMTOOLS_VIEW {
        ext.args = '-f 4'
        ext.prefix = { "${meta.id}_${meta.run_accession}.unmapped" }
    }

    withName: SAMTOOLS_FASTQ {
        ext.prefix = { "${meta.id}_${meta.run_accession}.unmapped" }
        publishDir = [
            [
                path: { "${params.outdir}/samtools/fastq" },
                mode: params.publish_dir_mode,
                pattern: '*_other.fastq.gz',
                enabled: params.save_hostremoval_unmapped
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*.fq.gz',
                enabled: params.save_analysis_ready_fastqs,
                saveAs: { ( params.perform_runmerging == false || ( params.perform_runmerging && !meta.is_multirun) ) && params.perform_longread_hostremoval && params.save_analysis_ready_fastqs ? it : null }
            ]
        ]
    }

    withName: SAMTOOLS_STATS {
        ext.prefix = { "${meta.id}_${meta.run_accession}" }
        publishDir = [
            path: { "${params.outdir}/samtools/stats" },
            mode: params.publish_dir_mode,
            pattern: '*stats'
        ]
    }

    withName: MERGE_RUNS {
        ext.prefix = { "${meta.id}" }
        publishDir = [
            [
                path: { "${params.outdir}/run_merging/" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.save_runmerged_reads
            ],
            [
                path: { "${params.outdir}/analysis_ready_fastqs" },
                mode: params.publish_dir_mode,
                pattern: '*.fastq.gz',
                enabled: params.perform_runmerging && params.save_analysis_ready_fastqs
            ]
        ]
    }

    withName: MALT_RUN {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params} -m ${params.malt_mode}" }
        // one run with multiple samples, so fix ID to just db name to ensure clean log name
        ext.prefix = { "${meta.db_name}" }
        publishDir = [
            path: { "${params.outdir}/malt/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{rma6,log,sam}'
        ]
    }

    withName: 'MEGAN_RMA2INFO_TSV' {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = "-c2c Taxonomy"
        ext.prefix = { "${meta.id}" }
        publishDir = [
            path: { "${params.outdir}/malt/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt.gz,megan}'
        ]
    }

    withName: KRAKEN2_KRAKEN2 {
        tag = { "${meta.db_name}|${meta.tool}|${meta.id}" }
        ext.args = params.kraken2_save_minimizers ? { "${meta.db_params} --report-minimizer-data" } : { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { meta.tool == "bracken" ? "${meta.id}_${meta.db_name}.bracken" : "${meta.id}_${meta.db_name}.kraken2" } : { meta.tool == "bracken" ? "${meta.id}_${meta.run_accession}_${meta.db_name}.bracken" : "${meta.id}_${meta.run_accession}_${meta.db_name}.kraken2" }
        publishDir = [
            path: { "${params.outdir}/kraken2/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt,fastq.gz}'
        ]
    }

    withName: KRAKEN2_STANDARD_REPORT {
        tag = { "${meta.db_name}|${meta.tool}|${meta.id}" }
        ext.prefix = params.perform_runmerging ? { meta.tool == "bracken" ? "${meta.id}_${meta.db_name}.bracken" : "${meta.id}_${meta.db_name}.kraken2" } : { meta.tool == "bracken" ? "${meta.id}_${meta.run_accession}_${meta.db_name}.bracken" : "${meta.id}_${meta.run_accession}_${meta.db_name}.kraken2" }
        publishDir = [
            path: { "${params.outdir}/kraken2/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.report.txt'
        ]
    }

    withName: BRACKEN_BRACKEN {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.bracken" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.bracken" }
        publishDir = [
            path: { "${params.outdir}/bracken/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*{.tsv,.txt}'
    ]
    }

    withName: BRACKEN_COMBINEBRACKENOUTPUTS {
        ext.prefix = { "bracken_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/bracken/" },
            mode: params.publish_dir_mode,
            pattern: '*.txt'
        ]
    }

    withName: KRAKENTOOLS_COMBINEKREPORTS_KRAKEN {
        ext.prefix = { "kraken2_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/kraken2/" },
            mode: params.publish_dir_mode,
            pattern: '*.txt'
        ]
    }

    withName: KRAKENUNIQ_PRELOADEDKRAKENUNIQ {
        ext.args = { "${meta.db_params}" }
        // one run with multiple samples, so fix ID to just db name to ensure clean log name
        ext.prefix = { "${meta.db_name}.krakenuniq" }
        publishDir = [
            path: { "${params.outdir}/krakenuniq/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt,fasta.gz}'
        ]
    }

    withName: KRAKENTOOLS_KREPORT2KRONA {
        publishDir = [
            enabled: false,
            mode: params.publish_dir_mode,
            pattern: '*.txt'
        ]
    }

    withName: KRONA_CLEANUP {
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}" }
        publishDir = [
            path: { "${params.outdir}/krona/" },
            mode: params.publish_dir_mode,
            pattern: '*.{html}'
        ]
    }

    withName: KRONA_KTIMPORTTEXT {
        ext.prefix = { "${meta.tool}_${meta.id}" }
        publishDir = [
            path: { "${params.outdir}/krona/" },
            mode: params.publish_dir_mode,
            pattern: '*.{html}'
        ]
    }

    withName: 'MEGAN_RMA2INFO_KRONA' {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "--read2class Taxonomy" }
        ext.prefix = { "${meta.id}_${meta.db_name}" }
    }

    withName: KRONA_KTIMPORTTAXONOMY {
        ext.args = "-i"
        ext.prefix = { "${meta.tool}_${meta.id}" }
        publishDir = [
            path: { "${params.outdir}/krona/" },
            mode: params.publish_dir_mode,
            pattern: '*.{html}'
        ]
    }

    withName: METAPHLAN_METAPHLAN {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.metaphlan" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.metaphlan" }
        publishDir = [
            path: { "${params.outdir}/metaphlan/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{biom,txt}'
        ]
    }

    withName: METAPHLAN_MERGEMETAPHLANTABLES {
        ext.prefix = { "metaphlan_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/metaphlan/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt}'
        ]
    }

    withName: CENTRIFUGE_CENTRIFUGE {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.centrifuge" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.centrifuge" }
        publishDir = [
            path: { "${params.outdir}/centrifuge/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt,sam,tab,gz}'
        ]
    }

    withName: CENTRIFUGE_KREPORT {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.centrifuge" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.centrifuge" }
        publishDir = [
            path: { "${params.outdir}/centrifuge/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt}'
        ]
    }

    withName: KRAKENTOOLS_COMBINEKREPORTS_CENTRIFUGE {
        ext.prefix = { "centrifuge_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/centrifuge/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt}'
        ]
    }

    withName: KAIJU_KAIJU {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.kaiju" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.kaiju" }
        publishDir = [
            path: { "${params.outdir}/kaiju/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.tsv'
        ]
    }

    withName: 'KAIJU_KAIJU2TABLE_SINGLE' {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = {[
            params.kaiju_expand_viruses ? "-e" : ""
        ].join(' ').trim() }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.kaijutable" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.kaijutable" }
        publishDir = [
            path: { "${params.outdir}/kaiju/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt}'
        ]
    }

    withName: 'KAIJU_KAIJU2TABLE_COMBINED' {
        ext.prefix = { "kaiju_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/kaiju/" },
            mode: params.publish_dir_mode,
            pattern: '*.{txt}'
        ]
    }

    withName: KAIJU_KAIJU2KRONA {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = '-v -u'
        publishDir = [
            path: { "${params.outdir}/kaiju/" },
            enabled : false
        ]
    }

    withName: DIAMOND_BLASTX {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.diamond" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.diamond" }
        publishDir = [
            path: { "${params.outdir}/diamond/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{blast,xml,txt,daa,sam,tsv,paf,log}'
        ]
    }

    withName: MOTUS_PROFILE {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = {
            [
                params.motus_remove_ncbi_ids ? "" : "-p",
                params.motus_use_relative_abundance ? "" : "-c",
                params.motus_save_mgc_read_counts ?  "-M ${task.ext.prefix}.mgc" : ""
            ].join(',').replaceAll(','," ")
            }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}" }
        publishDir = [
            path: { "${params.outdir}/motus/${meta.db_name}/" },
            mode: params.publish_dir_mode
        ]
    }

    withName: MOTUS_MERGE {
        ext.args = { params.standardisation_motus_generatebiom ? "-B" : "" }
        ext.prefix = { "motus_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/motus/" },
            mode: params.publish_dir_mode
        ]
    }

    withName: KMCP_SEARCH {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.kmcp_search" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.kmcp_search" }
        publishDir = [
            path: { "${params.outdir}/kmcp/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            enabled: params.kmcp_save_search
        ]
    }

    withName: KMCP_PROFILE {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.kmcp" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.kmcp" }
        publishDir = [
            path: { "${params.outdir}/kmcp/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{profile}'
        ]
    }

    withName: GANON_CLASSIFY {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = params.ganon_save_readclassifications ? { "${meta.db_params} --output-all --output-lca --output-unclassified" } : { "${meta.db_params}" }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.ganon" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.ganon" }
        publishDir = [
            path: { "${params.outdir}/ganon/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{tre,rep,lca,all,unc}'
        ]
    }

    withName: GANON_REPORT {
        tag = {"${meta.db_name}|${meta.id}"}
        ext.args = {[
            "--report-type ${params.ganon_report_type}",
            ganon_report_rank != 'default' ? "--ranks ${params.ganon_report_rank}" : "",
            "--top-percentile ${params.ganon_report_toppercentile}",
            "--min-count ${params.ganon_report_mincount}",
            "--max-count ${params.ganon_report_maxcount}"
        ].join(' ').trim() }
        ext.prefix = params.perform_runmerging ? { "${meta.id}_${meta.db_name}.ganon_report" } : { "${meta.id}_${meta.run_accession}_${meta.db_name}.ganon_report" }
        publishDir = [
            path: { "${params.outdir}/ganon/${meta.db_name}/" },
            mode: params.publish_dir_mode,
            pattern: '*.{tre}'
        ]
    }

    withName: GANON_TABLE {
        ext.prefix = { "ganon_${meta.id}_combined_reports" }
        publishDir = [
            path: { "${params.outdir}/ganon/" },
            mode: params.publish_dir_mode,
            pattern: '*.txt'
        ]
    }

    withName: TAXPASTA_MERGE {
        tag = { "${meta.tool}|${meta.id}" }
        ext.args =  {
            [
                "-p ${meta.tool} -o ${meta.tool}_${meta.id}.${params.standardisation_taxpasta_format}",
                params.taxpasta_add_name ?  "--add-name" : "",
                params.taxpasta_add_rank ? "--add-rank" : "",
                params.taxpasta_add_lineage ? "--add-lineage" : "",
                params.taxpasta_add_idlineage ? "--add-id-lineage" : "",
                params.taxpasta_add_ranklineage ? "--add-rank-lineage" : "",
                params.taxpasta_ignore_errors ? "--ignore-errors" : ""
            ].join(' ').trim()
            }
        publishDir = [
            path: { "${params.outdir}/taxpasta/" },
            mode: params.publish_dir_mode,
            pattern: '*.{tsv,csv,arrow,parquet,biom}'
        ]
    }

    withName: TAXPASTA_STANDARDISE {
        tag = { "${meta.tool}|${meta.id}" }
        ext.args =  {
            [
                "-p ${meta.tool} -o ${meta.tool}_${meta.id}.${params.standardisation_taxpasta_format}",
                params.taxpasta_add_name ?  "--add-name" : "",
                params.taxpasta_add_rank ? "--add-rank" : "",
                params.taxpasta_add_lineage ? "--add-lineage" : "",
                params.taxpasta_add_idlineage ? "--add-id-lineage" : ""
            ].join(' ').trim()
            }
        publishDir = [
            path: { "${params.outdir}/taxpasta/" },
            mode: params.publish_dir_mode,
            pattern: '*.{tsv,csv,arrow,parquet,biom}'
        ]
    }

    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 }
        ]
    }

}
