process SISTR {
    tag "$meta.id"
    label 'process_medium'

    conda (params.enable_conda ? "bioconda::sistr_cmd=1.1.1" : null)
    container "${ workflow.containerEngine == 'singularity' && !task.ext.singularity_pull_docker_container ?
        'https://depot.galaxyproject.org/singularity/sistr_cmd:1.1.1--pyh864c0ab_2':
        'quay.io/biocontainers/sistr_cmd:1.1.1--pyh864c0ab_2' }"

    input:
    tuple val(meta), path(fasta)

    output:
    tuple val(meta), path("*.tab")         , emit: tsv
    tuple val(meta), path("*-allele.fasta"), emit: allele_fasta
    tuple val(meta), path("*-allele.json") , emit: allele_json
    tuple val(meta), path("*-cgmlst.csv")  , emit: cgmlst_csv
    path "versions.yml"                    , emit: versions

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

    script:
    def args = task.ext.args ?: ''
    def prefix = task.ext.prefix ?: "${meta.id}"
    def is_compressed = fasta.getName().endsWith(".gz") ? true : false
    def fasta_name = fasta.getName().replace(".gz", "")
    """
    if [ "$is_compressed" == "true" ]; then
        gzip -c -d $fasta > $fasta_name
    fi

    sistr \\
        --qc \\
        $args \\
        --threads $task.cpus \\
        --alleles-output ${prefix}-allele.json \\
        --novel-alleles ${prefix}-allele.fasta \\
        --cgmlst-profiles ${prefix}-cgmlst.csv \\
        --output-prediction ${prefix} \\
        --output-format tab \\
        $fasta_name

    cat <<-END_VERSIONS > versions.yml
    "${task.process}":
        sistr: \$(echo \$(sistr --version 2>&1) | sed 's/^.*sistr_cmd //; s/ .*\$//' )
    END_VERSIONS
    """
}