# Buggy code
def kaiju_stats_table(self):
    """Take the parsed stats from the Kaiju reports and add them to the
    basic stats table at the top of the report"""
    headers = {}
    general_data = {}
    taxo_ranks = self.kaiju_data.keys()

    # print only phylum rank in general table.
    if len(taxo_ranks) >= 1 and "phylum" in taxo_ranks:
        general_data = self.kaiju_data["phylum"]
        general_taxo_rank = "Phylum"
    else:
        general_data = self.kaiju_data[sorted(self.kaiju_data.keys())[0]]
        general_taxo_rank = sorted(self.kaiju_data.keys())[0].capitalize()

    headers["percentage_assigned"] = {
        "title": "% Reads assigned {}".format(general_taxo_rank),
        "description": "Percentage of reads assigned at {} rank".format(
            general_taxo_rank
        ),
        "min": 0,
        "max": 100,
        "suffix": "%",
        "scale": "RdYlGn",
    }
    headers["assigned"] = {
        "title": "{} Reads assigned {} ".format(
            config.read_count_prefix, general_taxo_rank
        ),
        "description": "Number of reads assigned ({})  at {} rank".format(
            config.read_count_desc, general_taxo_rank
        ),
        "modify": lambda x: x * config.read_count_multiplier,
        "scale": "Blues",
    }
    self.general_stats_addcols(general_data, headers)


# Traceback (AttributeError)
[INFO   ]         multiqc : This is MultiQC v1.9
[INFO   ]         multiqc : Template    : default
[INFO   ]         multiqc : Searching   : /mnt/home/juriski/projects/scratch/MiSeq_run_200529/results/kaiju1/taxa/genus
[INFO   ]           kaiju : Found 1 reports
[ERROR  ]         multiqc : Oops! The 'kaiju' MultiQC module broke...
Please copy the following traceback and report it at https://github.com/ewels/MultiQC/issues
If possible, please include a log file that triggers the error - the last file found was:
genus/Li-2020-14538-3.kaiju.genus.tsv
============================================================
Module kaiju raised an exception: Traceback (most recent call last):
File "/home/juriski/.conda/envs/multiqc/lib/python3.6/site-packages/multiqc/multiqc.py", line 569, in run
output = mod()
File "/home/juriski/.conda/envs/multiqc/lib/python3.6/site-packages/multiqc/modules/kaiju/kaiju.py", line 64, in __init__
self.kaiju_stats_table()
File "/home/juriski/.conda/envs/multiqc/lib/python3.6/site-packages/multiqc/modules/kaiju/kaiju.py", line 105, in kaiju_stats_table
general_data = self.kaiju_data[self.kaiju_data.keys().sorted()[0]]
AttributeError: 'dict_keys' object has no attribute 'sorted'
============================================================
[WARNING]         multiqc : No analysis results found. Cleaning up..
[INFO   ]         multiqc : MultiQC complete

# Fixed code
def kaiju_stats_table(self):
    """Take the parsed stats from the Kaiju reports and add them to the
    basic stats table at the top of the report"""
    headers = {}
    general_data = {}
    taxo_ranks = self.kaiju_data.keys()

    # print only phylum rank in general table.
    if len(taxo_ranks) >= 1 and "phylum" in taxo_ranks:
        general_data = self.kaiju_data["phylum"]
        general_taxo_rank = "Phylum"
    else:
        general_data = self.kaiju_data[sorted(self.kaiju_data)[0]]
        general_taxo_rank = sorted(self.kaiju_data)[0].capitalize()

    headers["percentage_assigned"] = {
        "title": "% Reads assigned {}".format(general_taxo_rank),
        "description": "Percentage of reads assigned at {} rank".format(
            general_taxo_rank
        ),
        "min": 0,
        "max": 100,
        "suffix": "%",
        "scale": "RdYlGn",
    }
    headers["assigned"] = {
        "title": "{} Reads assigned {} ".format(
            config.read_count_prefix, general_taxo_rank
        ),
        "description": "Number of reads assigned ({})  at {} rank".format(
            config.read_count_desc, general_taxo_rank
        ),
        "modify": lambda x: x * config.read_count_multiplier,
        "scale": "Blues",
    }
    self.general_stats_addcols(general_data, headers)