# 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"][
                    "undetermined"
                ]["total"],
                "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,
                    }
                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]["percent_Q30"] = (
                    float(self.bcl2fastq_bysample[sample]["yieldQ30"])
                    / float(self.bcl2fastq_bysample[sample]["total_yield"])
                ) * 100.0
                self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = (
                    float(self.bcl2fastq_bysample[sample]["perfectIndex"])
                    / float(self.bcl2fastq_bysample[sample]["total"])
                ) * 100.0
                self.bcl2fastq_bysample[sample]["mean_qscore"] = float(
                    self.bcl2fastq_bysample[sample]["qscore_sum"]
                ) / float(self.bcl2fastq_bysample[sample]["total_yield"])
                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 (KeyError)
[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, include a log file that triggers the error)
============================================================
Module bcl2fastq raised an exception: Traceback (most recent call last):
File "/camp/stp/babs/working/software/binaries/Python-2.7.12-foss-2016b_packages/multiqc-1.2-2016b-Python-2.7.12/lib/python2.7/site-packages/multiqc-1.2-py2.7.egg/EGG-INFO/scripts/multiqc", line 411, in multiqc
output = mod()
File "/camp/stp/babs/working/software/binaries/Python-2.7.12-foss-2016b_packages/multiqc-1.2-2016b-Python-2.7.12/lib/python2.7/site-packages/multiqc-1.2-py2.7.egg/multiqc/modules/bcl2fastq/bcl2fastq.py", line 21, in __init__
self.parse_file_as_json(myfile)
File "/camp/stp/babs/working/software/binaries/Python-2.7.12-foss-2016b_packages/multiqc-1.2-2016b-Python-2.7.12/lib/python2.7/site-packages/multiqc-1.2-py2.7.egg/multiqc/modules/bcl2fastq/bcl2fastq.py", line 120, in parse_file_as_json
for indexMetric in demuxResult["IndexMetrics"]:
KeyError: 'IndexMetrics'
============================================================

# 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,
                    }
                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]["percent_Q30"] = (
                    float(self.bcl2fastq_bysample[sample]["yieldQ30"])
                    / float(self.bcl2fastq_bysample[sample]["total_yield"])
                ) * 100.0
                self.bcl2fastq_bysample[sample]["percent_perfectIndex"] = (
                    float(self.bcl2fastq_bysample[sample]["perfectIndex"])
                    / float(self.bcl2fastq_bysample[sample]["total"])
                ) * 100.0
                self.bcl2fastq_bysample[sample]["mean_qscore"] = float(
                    self.bcl2fastq_bysample[sample]["qscore_sum"]
                ) / float(self.bcl2fastq_bysample[sample]["total_yield"])
                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"]
                    )