# Buggy code
def peddy_sex_check_plot(self):
    data = {}
    sex_index = {"female": 0, "male": 1, "unknown": 2}

    for s_name, d in self.peddy_data.items():
        if "sex_het_ratio" in d and "ped_sex_sex_check" in d:
            data[s_name] = {
                "x": sex_index.get(d["ped_sex_sex_check"], 2),
                "y": d["sex_het_ratio"],
            }

    pconfig = {
        "id": "peddy_sex_check_plot",
        "title": "Peddy: Sex Check",
        "xlab": "Sex From Ped",
        "ylab": "Sex Het Ratio",
        "categories": ["Female", "Male", "Unknown"],
    }

    self.add_section(
        name="Sex Check",
        description="Predicted sex against heterozygosity ratio",
        helptext="""
            Higher values of Sex Het Ratio suggests the sample is female, low values suggest male.

            See [the main peddy documentation](http://peddy.readthedocs.io/en/latest/#sex-check) for more details about the `het_check` command.
            """,
        anchor="peddy-sexcheck-plot",
        plot=scatter.plot(data, pconfig),
    )


# Traceback (KeyError)
[INFO   ]         multiqc : This is MultiQC v1.7
[INFO   ]         multiqc : Template    : default
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/multiqc/report/metrics/target_info.yaml'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/contamination/ts_bcbio-verifybamid.failed'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Per_base_N_content.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Per_base_sequence_content.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Per_base_sequence_quality.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Per_sequence_GC_content.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Per_sequence_quality_scores.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Sequence_Duplication_Levels.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/Sequence_Length_Distribution.tsv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/fastqc_data.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/fastqc_report.html'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/fastqc/ts_bcbio.zip'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.background_pca.json'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.het_check.csv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.html'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.ped_check.csv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.ped_check.rel-difference.csv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.peddy.ped'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.sex_check.csv'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/samtools/ts_bcbio-idxstats.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/samtools/ts_bcbio.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/variants/ts_bcbio.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/variants/ts_bcbio_bcftools_stats.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/variants/ts_bcbio_bcftools_stats_germline.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/viral/ts_bcbio-gdc-viral-completeness.txt'
[INFO   ]         multiqc : Searching '/home/ubuntu/naumenko/sample_b2/work/qc/multiqc/report/metrics/ts_bcbio_bcbio.txt'
[INFO   ]           bcbio : Found 1 reports
/bcbio/anaconda/lib/python3.6/site-packages/multiqc_bcbio/bcbio.py:77: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
add_project_info(yaml.load(target_infos[0]['f']))
[INFO   ]        samtools : Found 1 idxstats reports
[ERROR  ]         multiqc : Oops! The 'peddy' 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:
/home/ubuntu/naumenko/sample_b2/work/qc/ts_bcbio/peddy/ts_bcbio.sex_check.csv
============================================================
Module peddy raised an exception: Traceback (most recent call last):
File "/bcbio/anaconda/lib/python3.6/site-packages/multiqc/modules/peddy/peddy.py", line 58, in __init__
self.peddy_data[s_name].update(parsed_data[s_name])
KeyError: 'ts'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/bcbio/tools/bin/multiqc", line 440, in multiqc
output = mod()
File "/bcbio/anaconda/lib/python3.6/site-packages/multiqc/modules/peddy/peddy.py", line 60, in __init__
self.peddy_data[s_name] = parsed_data[s_name]
KeyError: 'ts'
============================================================
[INFO   ]        bcftools : Found 1 stats reports
[INFO   ]        bcftools : Found 1 stats reports
[INFO   ]          fastqc : Found 1 reports
[INFO   ]         multiqc : Compressing plot data
[INFO   ]         multiqc : Report      : ../../bcbiotx/tmp1n5rgkh7/multiqc_report.html
[INFO   ]         multiqc : Data        : ../../bcbiotx/tmp1n5rgkh7/multiqc_data
[INFO   ]         multiqc : MultiQC complete
' returned non-zero exit status 1.

# Fixed code
def peddy_sex_check_plot(self):
    data = {}
    sex_index = {"female": 0, "male": 1, "unknown": 2}

    for s_name, d in self.peddy_data.items():
        if "sex_het_ratio" in d and "ped_sex_sex_check" in d:
            data[s_name] = {
                "x": sex_index.get(d["ped_sex_sex_check"], 2),
                "y": d["sex_het_ratio"],
            }

    pconfig = {
        "id": "peddy_sex_check_plot",
        "title": "Peddy: Sex Check",
        "xlab": "Sex From Ped",
        "ylab": "Sex Het Ratio",
        "categories": ["Female", "Male", "Unknown"],
    }

    if len(data) > 0:
        self.add_section(
            name="Sex Check",
            description="Predicted sex against heterozygosity ratio",
            helptext="""
                Higher values of Sex Het Ratio suggests the sample is female, low values suggest male.

                See [the main peddy documentation](http://peddy.readthedocs.io/en/latest/#sex-check) for more details about the `het_check` command.
                """,
            anchor="peddy-sexcheck-plot",
            plot=scatter.plot(data, pconfig),
        )