# Buggy code
def add_general_stats(self):
    data = {
        key: {
            "yieldQ30": self.bcl2fastq_bysample[key]["yieldQ30"],
            "total": self.bcl2fastq_bysample[key]["total"],
            "perfectPercent": "{0:.1f}".format(
                float(
                    100.0
                    * self.bcl2fastq_bysample[key]["perfectIndex"]
                    / self.bcl2fastq_bysample[key]["total"]
                )
            ),
            "trimmedPercent": self.bcl2fastq_bysample[key]["percent_trimmed"],
        }
        for key in self.bcl2fastq_bysample.keys()
    }
    headers = OrderedDict()
    headers["total"] = {
        "title": "{} Clusters".format(config.read_count_prefix),
        "description": "Total number of reads for this sample as determined by bcl2fastq demultiplexing ({})".format(
            config.read_count_desc
        ),
        "scale": "Blues",
        "shared_key": "read_count",
    }
    headers["yieldQ30"] = {
        "title": "{} Yield &ge; Q30".format(config.base_count_prefix),
        "description": "Number of bases with a Phred score of 30 or higher ({})".format(
            config.base_count_desc
        ),
        "scale": "Greens",
        "shared_key": "base_count",
    }
    headers["perfectPercent"] = {
        "title": "% Perfect Index",
        "description": "Percent of reads with perfect index (0 mismatches)",
        "max": 100,
        "min": 0,
        "scale": "RdYlGn",
        "suffix": "%",
    }
    headers["trimmedPercent"] = {
        "title": "% Bases trimmed",
        "description": "Percent of bases trimmed",
        "max": 100,
        "min": 0,
        "scale": "Reds",
        "suffix": "%",
        "hidden": True if all(data[s]["trimmedPercent"] == 0 for s in data) else False,
    }
    self.general_stats_addcols(data, headers)


# Traceback (ZeroDivisionError)
multiqc --outdir x .
[INFO   ]         multiqc : This is MultiQC v1.5
[INFO   ]         multiqc : Template    : default
[INFO   ]         multiqc : Searching '.'
Searching 770 files..  [####################################]  100%
[ERROR  ]         multiqc : Oops! The 'bcl2fastq' 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:
./Stats/Stats.json
============================================================
Module bcl2fastq raised an exception: Traceback (most recent call last):
File "/bioseqfs/home/sequencing/opt/miniconda2/envs/sequencing/bin/multiqc", line 442, in multiqc
output = mod()
File "/bioseqfs/home/sequencing/opt/miniconda2/envs/sequencing/lib/python2.7/site-packages/multiqc/modules/bcl2fastq/bcl2fastq.py", line 44, in __init__
self.add_general_stats()
File "/bioseqfs/home/sequencing/opt/miniconda2/envs/sequencing/lib/python2.7/site-packages/multiqc/modules/bcl2fastq/bcl2fastq.py", line 275, in add_general_stats
} for key in self.bcl2fastq_bysample.keys()
File "/bioseqfs/home/sequencing/opt/miniconda2/envs/sequencing/lib/python2.7/site-packages/multiqc/modules/bcl2fastq/bcl2fastq.py", line 275, in <dictcomp>
} for key in self.bcl2fastq_bysample.keys()
ZeroDivisionError: float division by zero
============================================================
[WARNING]         multiqc : No analysis results found. Cleaning up..
[INFO   ]         multiqc : MultiQC complete

# Fixed code
def add_general_stats(self):
    data = {}
    for key in self.bcl2fastq_bysample.keys():
        try:
            perfectPercent = float(
                100.0
                * self.bcl2fastq_bysample[key]["perfectIndex"]
                / self.bcl2fastq_bysample[key]["total"]
            )
        except ZeroDivisionError:
            perfectPercent = 0
        data[key] = {
            "yieldQ30": self.bcl2fastq_bysample[key]["yieldQ30"],
            "total": self.bcl2fastq_bysample[key]["total"],
            "perfectPercent": "{0:.1f}".format(perfectPercent),
            "trimmedPercent": self.bcl2fastq_bysample[key]["percent_trimmed"],
        }
    headers = OrderedDict()
    headers["total"] = {
        "title": "{} Clusters".format(config.read_count_prefix),
        "description": "Total number of reads for this sample as determined by bcl2fastq demultiplexing ({})".format(
            config.read_count_desc
        ),
        "scale": "Blues",
        "shared_key": "read_count",
    }
    headers["yieldQ30"] = {
        "title": "{} Yield &ge; Q30".format(config.base_count_prefix),
        "description": "Number of bases with a Phred score of 30 or higher ({})".format(
            config.base_count_desc
        ),
        "scale": "Greens",
        "shared_key": "base_count",
    }
    headers["perfectPercent"] = {
        "title": "% Perfect Index",
        "description": "Percent of reads with perfect index (0 mismatches)",
        "max": 100,
        "min": 0,
        "scale": "RdYlGn",
        "suffix": "%",
    }
    headers["trimmedPercent"] = {
        "title": "% Bases trimmed",
        "description": "Percent of bases trimmed",
        "max": 100,
        "min": 0,
        "scale": "Reds",
        "suffix": "%",
        "hidden": True if all(data[s]["trimmedPercent"] == 0 for s in data) else False,
    }
    self.general_stats_addcols(data, headers)