/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    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.
        ext.when   = When to run the module.
----------------------------------------------------------------------------------------
*/

process {
    withName: 'SAMPLESHEET_CHECK' {
        publishDir = [
            path: { "${params.outdir}/pipeline_info" },
            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'
        ]
    }
}

//
// Input Preparation options
//
process {
    withName: 'CAT_FASTQ' {
        publishDir = [
            path: { "${params.outdir}/fastq" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_merged_fastq
        ]
    }
}

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

}

//
// Quality Control options
//
if(!params.skip_qc) {
    process {
        withName: 'FASTQC' {
            ext.args = "--quiet"
            publishDir = [
                    path: { "${params.outdir}/reports/fastqc" },
                    mode: params.publish_dir_mode,
                    saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                    pattern: "*.{html,zip}"
                ]
        }
    }
}

//
// Trimming options
//
process {
    withName: 'TRIMGALORE' {
        ext.args   = [
            '--fastqc',
            params.trim_nextseq > 0 ? "--nextseq ${params.trim_nextseq}" : ''
        ].join(' ').trim()
        ext.prefix = { "${meta.id}.trimmed" }
        publishDir = [
            [
                path: { "${params.outdir}/reports/trimgalore_fastqc" },
                mode: params.publish_dir_mode,
                pattern: "*.{html,zip}",
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/trimgalore" },
                mode: params.publish_dir_mode,
                pattern: "*.fq.gz",
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: params.save_trimmed
            ],
            [
                path: { "${params.outdir}/trimgalore" },
                mode: params.publish_dir_mode,
                pattern: "*.txt",
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ]
        ]
    }
}

//
// Alignment options
//
process {
    withName: 'BWA_MEM' {
        ext.args = ""
        publishDir = [
            path: { "${params.outdir}/bwa" },
            enabled: false,
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: "*.bam"
        ]
    }
    withName: 'SAMTOOLS_INDEX_BAM' {
        ext.args = ""
        publishDir = [
            path: { "${params.outdir}/bwa" },
            enabled: true,
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: "*.bai"
        ]
    }
    withName: 'SAMTOOLS_SORT_BAM' {
        ext.args = ""
        ext.prefix = { "${meta.id}.sorted" }
        publishDir = [
            path: { "${params.outdir}/bwa" },
            enabled: true,
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: "*sorted.bam"
        ]
    }
}


process {
    withName: '.*BAM_STATS_SAMTOOLS_RAW.*' {
        publishDir  = [
            path: { "${params.outdir}/reports/samtools_stats/${meta.id}" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

//
// Picard MarkDuplicates and Filtering options
//
process {
    withName: '.*BAM_MARKDUPLICATES_PICARD:PICARD_MARKDUPLICATES' {
        ext.args   = '--ASSUME_SORTED true --REMOVE_DUPLICATES false --VALIDATION_STRINGENCY LENIENT --TMP_DIR tmp'
        ext.prefix = { "${meta.id}.md" }
        publishDir = [
            [
                path: { "${params.outdir}/reports/markduplicates" },
                mode: params.publish_dir_mode,
                pattern: '*metrics.txt',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/markduplicates/bam" },
                mode: params.publish_dir_mode,
                pattern: '*.md.{bam,bai}',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enable: params.save_markduplicates_bam
            ]
        ]
    }

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

    withName: '.*BAM_MARKDUPLICATES_PICARD:BAM_STATS_SAMTOOLS.*' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/reports/samtools_stats/${meta.id}/md/" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            pattern: '*.{stats,flagstat,idxstats}'
        ]
    }
    withName: 'SAMTOOLS_VIEW_FILTER' {
        ext.args = '-h -F 0x0400'
        publishDir  = [
            path: { "${params.outdir}/markduplicates/duplicates_removed" },
            mode: params.publish_dir_mode,
            pattern: '*filtered.{bai,bam}',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
        ext.prefix  = { "${meta.id}.md.filtered" }
    }
    withName: 'SAMTOOLS_SORT_FILTERED' {
        ext.args    = ""
        ext.prefix  = { "${meta.id}.md.filtered.sorted" }
        publishDir  = [
            path: { "${params.outdir}/markduplicates/duplicates_removed" },
            mode: params.publish_dir_mode,
            pattern: '*filtered.sorted.bam',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }

    withName: 'SAMTOOLS_INDEX_FILTERED' {
        ext.args    = ""
        ext.prefix  = { "${meta.id}.md.filtered.sorted" }
        publishDir  = [
            path: { "${params.outdir}/markduplicates/duplicates_removed" },
            mode: params.publish_dir_mode,
            pattern: '*filtered.sorted.bai',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

//
// Circle_finder Options
//
process {
    withName: 'SAMTOOLS_SORT_QNAME_CF' {
        ext.args    = "-n"
        ext.prefix  = { "${meta.id}.qname.sorted" }
        publishDir  = [
            path: { "${params.outdir}/circlefinder/samtools" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_circle_finder_intermediate
        ]
    }
    withName: 'SAMBLASTER' {
        ext.args    = "--ignoreUnmated"
        publishDir  = [
            path: { "${params.outdir}/circlefinder/samblaster" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_circle_finder_intermediate,
        ]
    }
    withName: 'BEDTOOLS_SPLITBAM2BED' {
        ext.prefix  = { "${meta.id}.split" }
        ext.args    = "-cigar"
        publishDir  = [
            path: { "${params.outdir}/circlefinder/split" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_circle_finder_intermediate
        ]
    }
    withName: 'BEDTOOLS_SORTEDBAM2BED' {
        ext.args    = "-cigar"
        ext.prefix  = { "${meta.id}.concordant" }
        publishDir  = [
            path: { "${params.outdir}/circlefinder/concordant" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_circle_finder_intermediate
        ]
    }
    withName: 'CIRCLEFINDER' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/circlefinder" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

//
// CIRCexplorer2 Options
//
process {
    withName: 'CIRCEXPLORER2_PARSE' {
        ext.args    = "-t BWA"
        publishDir  = [
            path: { "${params.outdir}/circexplorer2" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

//
// AmpliconArchitect Options
//
process {
    withName: 'CNVKIT_BATCH' {
        ext.args    = "--method wgs"
        publishDir  = [
            path: { "${params.outdir}/cnvkit" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }

    withName: 'CNVKIT_SEGMENT' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/ampliconarchitect/cnvkit" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
    withName: 'PREPAREAA' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/prepareaa" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
    withName: 'AMPLICONARCHITECT_AMPLICONARCHITECT' {
        time        = '96.h'
        ext.args    = ""
        publishDir  = [
            [
                path: { "${params.outdir}/ampliconarchitect/ampliconarchitect/sv_view" },
                mode: params.publish_dir_mode,
                pattern: '*.{png,pdf}',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/ampliconarchitect/ampliconarchitect/amplicons" },
                mode: params.publish_dir_mode,
                pattern: '*{graph.txt,cycles.txt}',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/ampliconarchitect/ampliconarchitect/logs" },
                mode: params.publish_dir_mode,
                pattern: '*logs.txt',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/ampliconarchitect/ampliconarchitect/cnseg" },
                mode: params.publish_dir_mode,
                pattern: '*cnseg.txt',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/ampliconarchitect/ampliconarchitect/summary" },
                mode: params.publish_dir_mode,
                pattern: '*summary.txt',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],

        ]
    }
    withName: 'AMPLICONCLASSIFIER_AMPLICONCLASSIFIER' {
        ext.args    = "--report_complexity --verbose_classification --plotstyle 'individual'"
        publishDir  = [
            path: { "${params.outdir}/ampliconclassifier/ampliconclassifier" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
    withName: 'AMPLICONCLASSIFIER_AMPLICONSIMILARITY' {
        ext.args    = ""
        publishDir  = [
            [
                path: { "${params.outdir}/ampliconclassifier/ampliconclassifier/input" },
                mode: params.publish_dir_mode,
                pattern: '*.input',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename }
            ],
            [
                path: { "${params.outdir}/ampliconclassifier/ampliconsimilarity/log" },
                mode: params.publish_dir_mode,
                pattern: '*.log',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            ],
            [
                path: { "${params.outdir}/ampliconclassifier/ampliconsimilarity/similarity" },
                mode: params.publish_dir_mode,
                pattern: '*_similarity_scores.tsv',
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            ],
        ]
    }
    withName: 'AMPLICONCLASSIFIER_MAKEINPUT' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/ampliconclassifier/makeinput" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
    withName: 'AMPLICONCLASSIFIER_MAKERESULTSTABLE' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/ampliconclassifier/resultstable" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

//
// Circle-Map Realign & Repeats
//
process {
    withName: 'SAMTOOLS_SORT_QNAME_CM' {
        ext.prefix  = { "${meta.id}.qname.sorted" }
        ext.args    = "-n"
        publishDir  = [
            path: { "${params.outdir}/circlemap/samtools" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }
    withName: 'CIRCLEMAP_READEXTRACTOR' {
        ext.args    = ""
        ext.prefix  = { "${meta.id}.circular_read_candidates" }
        publishDir  = [
            path: { "${params.outdir}/circlemap/readextractor" },
            mode: params.publish_dir_mode,
            pattern: "*.qname.sorted.bam",
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: false
        ]
    }
    withName: 'SAMTOOLS_SORT_RE' {
        ext.prefix  = { "${meta.id}.circular_read_candidates.sorted" }
        ext.args    = ""
        publishDir  = [
                path: { "${params.outdir}/circlemap/readextractor" },
                mode: params.publish_dir_mode,
                pattern: "*.circular_read_candidates.sorted.bam",
                saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
                enabled: params.save_circle_map_intermediate
        ]
    }
    withName: 'SAMTOOLS_INDEX_RE' {
        ext.prefix  = { "${meta.id}.circular_read_candidates.sorted" }
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/circlemap/readextractor" },
            mode: params.publish_dir_mode,
            pattern: "*.circular_read_candidates.sorted.bam.bai",
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_circle_map_intermediate
        ]
    }
    withName: 'CIRCLEMAP_REPEATS' {
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/circlemap/repeats" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
    withName: 'CIRCLEMAP_REALIGN' {
        time        = '96.h'
        ext.args    = ""
        publishDir  = [
            path: { "${params.outdir}/circlemap/realign" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

//
// Unicycler Options
//
process {
    withName: 'UNICYCLER' {
        time        = '96.h'
        ext.args    = "--no_rotate"
        publishDir  = [
            path: { "${params.outdir}/unicycler/unicycler" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_unicycler_intermediate
        ]
    }
    withName: 'SEQTK_SEQ' {
        ext.args    = ""
        ext.prefix  = { "${meta.id}.unicycler" }
        publishDir  = [
            path: { "${params.outdir}/unicycler/seqtk" },
            mode: params.publish_dir_mode,
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_unicycler_intermediate
        ]
    }
    withName: 'GETCIRCULARREADS' {
        ext.args    = ""
        ext.prefix  = { "${meta.id}.unicycler.circular" }
        publishDir  = [
            path: { "${params.outdir}/unicycler/getcircularreads" },
            mode: params.publish_dir_mode,
            pattern: '*.unicycler.circular.fastq.gz',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: params.save_unicycler_intermediate
        ]
    }
    withName: 'MINIMAP2_ALIGN' {
        ext.args    = ""
        ext.prefix  = { "${meta.id}.unicycler.circular.mapped" }
        publishDir  = [
            path: { "${params.outdir}/unicycler/minimap2" },
            mode: params.publish_dir_mode,
            pattern: '*.paf',
            saveAs: { filename -> filename.equals('versions.yml') ? null : filename },
            enabled: true
        ]
    }
}

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