# Buggy code
def split_data_by_lane_and_sample(self):
    for runId in self.bcl2fastq_data.keys():
        for lane in self.bcl2fastq_data[runId].keys():
            uniqLaneName = self.prepend_runid(runId, lane)
            self.bcl2fastq_bylane[uniqLaneName] = {
                "total": self.bcl2fastq_data[runId][lane]["total"],
                "total_yield": self.bcl2fastq_data[runId][lane]["total_yield"],
                "perfectIndex": self.bcl2fastq_data[runId][lane]["perfectIndex"],
                "undetermined": self.bcl2fastq_data[runId][lane]["samples"]
                .get("undetermined", {})
                .get("total", "NA"),
                "yieldQ30": self.bcl2fastq_data[runId][lane]["yieldQ30"],
                "qscore_sum": self.bcl2fastq_data[runId][lane]["qscore_sum"],
                "percent_Q30": self.bcl2fastq_data[runId][lane]["percent_Q30"],
                "percent_perfectIndex": self.bcl2fastq_data[runId][lane][
                    "percent_perfectIndex"
                ],
                "mean_qscore": self.bcl2fastq_data[runId][lane]["mean_qscore"],
            }
            for sample in self.bcl2fastq_data[runId][lane]["samples"].keys():
                if not sample in self.bcl2fastq_bysample:
                    self.bcl2fastq_bysample[sample] = {
                        "total": 0,
                        "total_yield": 0,
                        "perfectIndex": 0,
                        "yieldQ30": 0,
                        "qscore_sum": 0,
                        "trimmed_bases": 0,
                    }
                if not sample in self.bcl2fastq_bysample_lane:
                    self.bcl2fastq_bysample_lane[sample] = dict()
                self.bcl2fastq_bysample_lane[sample][lane] = self.bcl2fastq_data[runId][
                    lane
                ]["samples"][sample]["total"]
                self.bcl2fastq_bysample[sample]["total"] += self.bcl2fastq_data[runId][
                    lane
                ]["samples"][sample]["total"]
                self.bcl2fastq_bysample[sample]["total_yield"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["total_yield"]
                self.bcl2fastq_bysample[sample]["perfectIndex"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["perfectIndex"]
                self.bcl2fastq_bysample[sample]["yieldQ30"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["yieldQ30"]
                self.bcl2fastq_bysample[sample]["qscore_sum"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["qscore_sum"]
                self.bcl2fastq_bysample[sample]["trimmed_bases"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["trimmed_bases"]

                try:
                    self.bcl2fastq_bysample[sample]["percent_Q30"] = (
                        float(self.bcl2fastq_bysample[sample]["yieldQ30"])
                        / float(self.bcl2fastq_bysample[sample]["total_yield"])
                    ) * 100.0
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["percent_Q30"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = (
                        float(self.bcl2fastq_bysample[sample]["perfectIndex"])
                        / float(self.bcl2fastq_bysample[sample]["total"])
                    ) * 100.0
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = (
                        float(self.bcl2fastq_bysample[sample]["perfectIndex"])
                        / float(self.bcl2fastq_bysample[sample]["total"])
                    ) * 100.0
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["mean_qscore"] = float(
                        self.bcl2fastq_bysample[sample]["qscore_sum"]
                    ) / float(self.bcl2fastq_bysample[sample]["total_yield"])
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["mean_qscore"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["percent_trimmed"] = (
                        float(self.bcl2fastq_bysample[sample]["trimmed_bases"])
                        / float(self.bcl2fastq_bysample[sample]["total_yield"])
                        * 100.0
                    )
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["mean_qscore"] = "NA"
                if sample != "undetermined":
                    if not sample in self.source_files:
                        self.source_files[sample] = []
                    self.source_files[sample].append(
                        self.bcl2fastq_data[runId][lane]["samples"][sample]["filename"]
                    )


# 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 split_data_by_lane_and_sample(self):
    for runId in self.bcl2fastq_data.keys():
        for lane in self.bcl2fastq_data[runId].keys():
            uniqLaneName = self.prepend_runid(runId, lane)
            self.bcl2fastq_bylane[uniqLaneName] = {
                "total": self.bcl2fastq_data[runId][lane]["total"],
                "total_yield": self.bcl2fastq_data[runId][lane]["total_yield"],
                "perfectIndex": self.bcl2fastq_data[runId][lane]["perfectIndex"],
                "undetermined": self.bcl2fastq_data[runId][lane]["samples"]
                .get("undetermined", {})
                .get("total", "NA"),
                "yieldQ30": self.bcl2fastq_data[runId][lane]["yieldQ30"],
                "qscore_sum": self.bcl2fastq_data[runId][lane]["qscore_sum"],
                "percent_Q30": self.bcl2fastq_data[runId][lane]["percent_Q30"],
                "percent_perfectIndex": self.bcl2fastq_data[runId][lane][
                    "percent_perfectIndex"
                ],
                "mean_qscore": self.bcl2fastq_data[runId][lane]["mean_qscore"],
            }
            for sample in self.bcl2fastq_data[runId][lane]["samples"].keys():
                if not sample in self.bcl2fastq_bysample:
                    self.bcl2fastq_bysample[sample] = {
                        "total": 0,
                        "total_yield": 0,
                        "perfectIndex": 0,
                        "yieldQ30": 0,
                        "qscore_sum": 0,
                        "trimmed_bases": 0,
                    }
                if not sample in self.bcl2fastq_bysample_lane:
                    self.bcl2fastq_bysample_lane[sample] = dict()
                self.bcl2fastq_bysample_lane[sample][lane] = self.bcl2fastq_data[runId][
                    lane
                ]["samples"][sample]["total"]
                self.bcl2fastq_bysample[sample]["total"] += self.bcl2fastq_data[runId][
                    lane
                ]["samples"][sample]["total"]
                self.bcl2fastq_bysample[sample]["total_yield"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["total_yield"]
                self.bcl2fastq_bysample[sample]["perfectIndex"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["perfectIndex"]
                self.bcl2fastq_bysample[sample]["yieldQ30"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["yieldQ30"]
                self.bcl2fastq_bysample[sample]["qscore_sum"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["qscore_sum"]
                self.bcl2fastq_bysample[sample]["trimmed_bases"] += self.bcl2fastq_data[
                    runId
                ][lane]["samples"][sample]["trimmed_bases"]

                try:
                    self.bcl2fastq_bysample[sample]["percent_Q30"] = (
                        float(self.bcl2fastq_bysample[sample]["yieldQ30"])
                        / float(self.bcl2fastq_bysample[sample]["total_yield"])
                    ) * 100.0
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["percent_Q30"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = (
                        float(self.bcl2fastq_bysample[sample]["perfectIndex"])
                        / float(self.bcl2fastq_bysample[sample]["total"])
                    ) * 100.0
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["mean_qscore"] = float(
                        self.bcl2fastq_bysample[sample]["qscore_sum"]
                    ) / float(self.bcl2fastq_bysample[sample]["total_yield"])
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["mean_qscore"] = "NA"
                try:
                    self.bcl2fastq_bysample[sample]["percent_trimmed"] = (
                        float(self.bcl2fastq_bysample[sample]["trimmed_bases"])
                        / float(self.bcl2fastq_bysample[sample]["total_yield"])
                        * 100.0
                    )
                except ZeroDivisionError:
                    self.bcl2fastq_bysample[sample]["percent_trimmed"] = "NA"
                if sample != "undetermined":
                    if not sample in self.source_files:
                        self.source_files[sample] = []
                    self.source_files[sample].append(
                        self.bcl2fastq_data[runId][lane]["samples"][sample]["filename"]
                    )