/*
========================================================================================
    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.
----------------------------------------------------------------------------------------
*/

//
// General configuration options
//

process {
    withName: 'GUNZIP_.*' {
        publishDir = [
            path: { "${params.outdir}/genome" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_reference
        ]
    }

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

    withName: 'MULTIQC_TSV_BARCODE_COUNT|MULTIQC_TSV_GUPPYPLEX_COUNT' {
        publishDir = [
            path: { "${params.outdir}/multiqc/${params.artic_minion_caller}" },
            enabled: false
        ]
    }

    withName: 'ARTIC_GUPPYPLEX' {
        ext.args = params.primer_set_version == 1200 ? '--min-length 250 --max-length 1500' : '--min-length 400 --max-length 700'
        publishDir = [
            path: { "${params.outdir}/guppyplex" },
            enabled: false
        ]
    }

    withName: 'ARTIC_MINION' {
        ext.args = [
            '--normalise 500',
            params.artic_minion_caller  == 'medaka' ? '--medaka' : '',
            params.artic_minion_aligner == 'bwa'    ? '--bwa'    : '--minimap2'
        ].join(' ').trim()
        publishDir = [
            path: { "${params.outdir}/${params.artic_minion_caller}" },
            mode: params.publish_dir_mode,
            pattern: "*.{sorted.bam,sorted.bam.bai,fail.vcf,merged.vcf,primers.vcf,gz,tbi,consensus.fasta}"
        ]
    }

    withName: 'VCFLIB_VCFUNIQ' {
        ext.args = '-f'
        ext.prefix = { "${meta.id}.pass.unique" }
        publishDir = [
            path: { "${params.outdir}/${params.artic_minion_caller}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: '.*:.*:TABIX_TABIX' {
        ext.args = '-p vcf -f'
        publishDir = [
            path: { "${params.outdir}/${params.artic_minion_caller}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

    withName: '.*:.*:.*:SAMTOOLS_VIEW' {
        ext.args = '-b -F 4'
        ext.prefix = { "${meta.id}.mapped.sorted" }
        publishDir = [
            path: { "${params.outdir}/${params.artic_minion_caller}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
        ]
    }

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

    withName: '.*:.*:.*:BAM_STATS_SAMTOOLS:.*' {
        ext.prefix = { "${meta.id}.mapped.sorted" }
        publishDir = [
            path: { "${params.outdir}/${params.artic_minion_caller}/samtools_stats" },
            mode: params.publish_dir_mode,
            pattern: "*.{stats,flagstat,idxstats}"
        ]
    }

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

//
// Optional configuration options
//

if (params.input) {
    process {
        withName: 'MULTIQC_TSV_NO_.*' {
            publishDir = [
                path: { "${params.outdir}/multiqc/${params.artic_minion_caller}" },
                enabled: false
            ]
        }
    }
}

if (params.sequencing_summary && !params.skip_pycoqc) {
    process {
        withName: 'PYCOQC' {
            publishDir = [
                path: { "${params.outdir}/pycoqc" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

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

if (!params.skip_mosdepth) {
    process {
        withName: 'COLLAPSE_PRIMERS' {
            publishDir = [
                path: { "${params.outdir}/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: params.save_reference
            ]
        }

        withName: 'MOSDEPTH_GENOME' {
            ext.args = '--fast-mode'
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/mosdepth/genome" },
                mode: params.publish_dir_mode,
                pattern: "*.summary.txt"
            ]
        }

        withName: 'PLOT_MOSDEPTH_REGIONS_GENOME' {
            ext.args = '--input_suffix .regions.bed.gz'
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/mosdepth/genome" },
                mode: params.publish_dir_mode,
                pattern: "*.{tsv,pdf}"
            ]
        }

        withName: 'MOSDEPTH_AMPLICON' {
            ext.args = '--fast-mode --use-median --thresholds 0,1,10,50,100,500'
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/mosdepth/amplicon" },
                mode: params.publish_dir_mode,
                pattern: "*.summary.txt"
            ]
        }

        withName: 'PLOT_MOSDEPTH_REGIONS_AMPLICON' {
            ext.args = '--input_suffix .regions.bed.gz'
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/mosdepth/amplicon" },
                mode: params.publish_dir_mode,
                pattern: "*.{tsv,pdf}"
            ]
        }
    }
}

if (!params.skip_pangolin) {
    process {
        withName: 'PANGOLIN' {
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/pangolin" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (!params.skip_nextclade) {
    process {
        withName: 'UNTAR' {
            publishDir = [
                path: { "${params.outdir}/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: params.save_reference
            ]
        }

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

        withName: 'NEXTCLADE_RUN' {
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/nextclade" },
                mode: params.publish_dir_mode,
                pattern: "*.csv"
            ]
        }

        withName: 'MULTIQC_TSV_NEXTCLADE' {
            publishDir = [
                path: { "${params.outdir}/multiqc/${params.artic_minion_caller}" },
                enabled: false
            ]
        }
    }
}

if (!params.skip_variants_quast) {
    process {
        withName: 'QUAST' {
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}" },
                mode: params.publish_dir_mode,
                pattern: "quast"
            ]
        }
    }
}

if (!params.skip_snpeff) {
    process {
        withName: 'SNPEFF_BUILD' {
            publishDir = [
                path: { "${params.outdir}/genome" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: params.save_reference
            ]
        }

        withName: 'SNPEFF_ANN' {
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/snpeff" },
                mode: params.publish_dir_mode,
                pattern: "*.{csv,txt,html}"
            ]
        }

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

        withName: '.*:.*:.*:.*:.*:TABIX_TABIX' {
            ext.args = '-p vcf -f'
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/snpeff" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }

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

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

    if (!params.skip_variants_long_table) {
        process {
            withName: 'BCFTOOLS_QUERY' {
                ext.args = [
                    params.artic_minion_caller == 'nanopolish' ? "-H -f '%CHROM\\t%POS\\t%REF\\t%ALT\\t%FILTER\\t%StrandSupport\\n'" : '',
                    params.artic_minion_caller == 'medaka'     ? "-H -f '%CHROM\\t%POS\\t%REF\\t%ALT\\t%FILTER\\t%DP\\t%AC\\n'" : ''
                ].join(' ').trim()
                ext.prefix = { "${meta.id}.bcftools_query" }
                publishDir = [
                    path: { "${params.outdir}/${params.artic_minion_caller}" },
                    enabled: false
                ]
            }

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

if (!params.skip_asciigenome) {
    process {
        withName: 'ASCIIGENOME' {
            publishDir = [
                path: { "${params.outdir}/${params.artic_minion_caller}/asciigenome/${meta.id}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}

if (!params.skip_multiqc) {
    process {
        withName: 'MULTIQC' {
            ext.args = params.multiqc_title ? "--title \"$params.multiqc_title\"" : ''
            publishDir = [
                path: { "${params.outdir}/multiqc/${params.artic_minion_caller}" },
                mode: params.publish_dir_mode,
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        }
    }
}
