process NASVAR_PIPELINE {
    tag "$meta.id"
    label 'process_low'

    // TODO nf-core: See section in main README for further information regarding finding and adding container addresses to the section below.
    container 'nasvar:1.1.0'

    input:
    tuple val(meta), path(bam), path(bai)
    path(repeats)
    path(enrichment_bed)
    path(sites)
    path(target_bed)
    tuple path(fasta), path(fasta_index), path(gff)
    path(config)
    path(json_reference)

    output:
    // TODO nf-core: Named file extensions MUST be emitted for ALL output channels
    tuple val(meta), path("*.result.json"), emit: json
    tuple val(meta), path("*.coverage.tsv"), emit: coverage
    tuple val(meta), path("*.maf"), emit: maf
    tuple val(meta), path("*.log"), emit: log
    tuple val(meta), path("*.md"), emit: markdown
    tuple val(meta), path("*.html"), emit: html
    tuple val(meta), path("*.nasvar.zip"), emit: zip
    tuple val(meta), path("*.karyotype*.svg"), emit: karyotype_svg
    tuple val(meta), path("*.coverage.gc_adjusted.tsv"), optional: true, emit: coverage_gc_adjusted
    tuple val(meta), path("*.breakpoints.fa"), optional: true, emit: breakpoints
    tuple val(meta), path("*.slice.bam"), path("*.slice.bam.bai"), optional: true, emit: slice_bam
    tuple val(meta), path("*.slice.fa.gz"), path("*.slice.fa.gz.fai"), optional: true, emit: slice_fa
    tuple val(meta), path("*.gc_vs_coverage*.svg"), optional: true, emit: diagnostic
    tuple val(meta), path("*.chr21_RUNX1_covg.svg"), optional: true, emit: RUNX1_svg
    tuple val("${task.process}"), val('nasvar'), eval("nasvar --version"), topic: versions, emit: versions_nasvar

    when:
    task.ext.when == null || task.ext.when

    script:
    def args = task.ext.args ?: ''
    def prefix = task.ext.prefix ?: "${meta.id}"

    """
    nasvar pipeline \\
        $args \\
        --log-file ${prefix}_nasvar.log \\
        --config $config \\
        --reference $json_reference \\
        $bam \\
        $repeats \\
        $enrichment_bed \\
        $sites \\
        $target_bed \\
        $fasta \\
        $gff \\
        $prefix
    """

    stub:
    def args = task.ext.args ?: ''
    def prefix = task.ext.prefix ?: "${meta.id}"
    """
    echo $args
    
    touch ${prefix}.result.json
    touch ${prefix}.coverage.tsv
    touch ${prefix}.maf
    touch ${prefix}.karyotype.svg
    touch ${prefix}.report.md
    touch ${prefix}.report.html
    touch ${prefix}_nasvar.log
    touch ${prefix}.nasvar.zip

    """
}
