# Buggy code
def parse_peddy_csv(self, f, pattern):
    """Parse csv output from peddy"""
    parsed_data = dict()
    headers = None
    s_name_idx = None
    for l in f["f"].splitlines():
        s = l.split(",")
        if headers is None:
            headers = s
            try:
                s_name_idx = [headers.index("sample_id")]
            except ValueError:
                try:
                    s_name_idx = [headers.index("sample_a"), headers.index("sample_b")]
                except ValueError:
                    log.warn(
                        "Could not find sample name in Peddy output: {}".format(f["fn"])
                    )
                    return None
        else:
            s_name = "-".join([s[idx] for idx in s_name_idx])
            parsed_data[s_name] = dict()
            for i, v in enumerate(s):
                if i not in s_name_idx:
                    if headers[i] == "error" and pattern == "sex_check":
                        v = "True" if v == "False" else "False"
                    try:
                        # add the pattern as a suffix to key
                        parsed_data[s_name][headers[i] + "_" + pattern] = float(v)
                    except ValueError:
                        # add the pattern as a suffix to key
                        parsed_data[s_name][headers[i] + "_" + pattern] = v
    if len(parsed_data) == 0:
        return None
    return parsed_data


# 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 parse_peddy_csv(self, f, pattern):
    """Parse csv output from peddy"""
    parsed_data = dict()
    headers = None
    s_name_idx = None
    for l in f["f"].splitlines():
        s = l.split(",")
        if headers is None:
            headers = s
            try:
                s_name_idx = [headers.index("sample_id")]
            except ValueError:
                try:
                    s_name_idx = [headers.index("sample_a"), headers.index("sample_b")]
                except ValueError:
                    log.warn(
                        "Could not find sample name in Peddy output: {}".format(f["fn"])
                    )
                    return None
        else:
            s_name = "-".join([s[idx] for idx in s_name_idx])
            s_name = self.clean_s_name(s_name, f["root"])
            parsed_data[s_name] = dict()
            for i, v in enumerate(s):
                if i not in s_name_idx:
                    if headers[i] == "error" and pattern == "sex_check":
                        v = "True" if v == "False" else "False"
                    try:
                        # add the pattern as a suffix to key
                        parsed_data[s_name][headers[i] + "_" + pattern] = float(v)
                    except ValueError:
                        # add the pattern as a suffix to key
                        parsed_data[s_name][headers[i] + "_" + pattern] = v
    if len(parsed_data) == 0:
        return None
    return parsed_data