rule prokka:
    """
    Prokka is a software tool to annotate bacterial, archaeal and viral genomes
    quickly and produce standards compliant output files (genbank, gff and so
    on). 

    Required input:
        - __prokka__input: FASTA file of the reference.

    Required output:
        - __prokka__gbk: Genbank file of the reference.

    Required log:
        - __prokka__log: Log file with stderr and stdout of Prokka.

    Required parameters:
        - __prokka__prefix: Prefix name for annotations files.
        - __prokka__outdir: Output directory that contains annotations files.
    
    Required configuration:
        .. code-block:: yaml

            prokka:
                kingdom: Bacteria # Annotation mode. (Archaea|Bacteria|Mitochondria|Viruses)
                genus: "" # Genus name of the assembly.
                species: "" # Species name of the assembly.
                threads: 4 # Number of threads used by Prokka.
                options: ""# Any options recognised by Prokka.

    Reference:
        - https://github.com/tseemann/prokka
    """
    input:
        fasta = __prokka__input
    output:
        gbk = __prokka__gbk
    log:
        l = __prokka__log
    params:
        prefix = __prokka__prefix,
        outdir = __prokka__outdir,
        kingdom = config['prokka']['kingdom'],
        genus = config['prokka']['genus'],
        species = config['prokka']['species'],
        options = config['prokka']['options']
    threads:
        config['prokka']['threads']
    run:
        cmd = "prokka --force --cpus %i --prefix %s --outdir %s %s" % (
            threads, params['prefix'], params['outdir'], params['options']
        )
        if params['kingdom']:
            cmd +=  " --kingdom " + params['kingdom']
        if params['genus']:
            cmd += " --genus " + params['genus']
        if params['species']:
            cmd += " --species " + params['species']
        cmd += " %s > %s 2>&1" % (input['fasta'], log['l'])
        shell(cmd)
