# Buggy code
def plot_basic_hist(samples, file_type, **plot_args):
    """Create line graph plot for basic histogram data for 'file_type'.

    The 'samples' parameter could be from the bbmap mod_data dictionary:
    samples = bbmap.MultiqcModule.mod_data[file_type]
    """

    sumy = sum(
        [
            int(samples[sample]["data"][x][0])
            for sample in samples
            for x in samples[sample]["data"]
        ]
    )

    cutoff = sumy * 0.999
    all_x = set()
    for item in sorted(chain(*[samples[sample]["data"].items() for sample in samples])):
        all_x.add(item[0])
        cutoff -= item[1][0]
        if cutoff < 0:
            xmax = item[0]
            break

    data = {
        sample: {
            x: samples[sample]["data"][x][0] if x in samples[sample]["data"] else 0
            for x in all_x
        }
        for sample in samples
    }

    plot_params = {
        "id": "bbmap-" + file_type + "_plot",
        "title": "BBTools: " + plot_args["plot_title"],
        "xmax": xmax,
    }
    plot_params.update(plot_args["plot_params"])
    plot = linegraph.plot(data, plot_params)

    return plot


# Traceback (UnboundLocalError)
[INFO   ]         multiqc : This is MultiQC v1.7
[INFO   ]         multiqc : Template    : default
[INFO   ]         multiqc : Searching 'qc/'
Searching 252 files..  [####################################]  100%
[INFO   ]          preseq : Found 24 reports
[INFO   ]           rseqc : Found 12 read_distribution reports
[INFO   ]           rseqc : Found 12 read_duplication reports
[INFO   ]           rseqc : Found 12 infer_experiment reports
[INFO   ]        samtools : Found 12 flagstat reports
[INFO   ]           bbmap : Found 12 reports
[ERROR  ]         multiqc : Oops! The 'bbmap' 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:
qc/trimstats/41_DMSO_rep_2_S12_R1_001.refstats.txt
============================================================
Module bbmap raised an exception: Traceback (most recent call last):
File "/Users/magr0763/.local/bin/multiqc", line 440, in multiqc
output = mod()
File "/Users/magr0763/.local/lib/python3.6/site-packages/multiqc/modules/bbmap/bbmap.py", line 62, in __init__
plot = self.plot(file_type)
File "/Users/magr0763/.local/lib/python3.6/site-packages/multiqc/modules/bbmap/bbmap.py", line 155, in plot
plot_params=plot_params)
File "/Users/magr0763/.local/lib/python3.6/site-packages/multiqc/modules/bbmap/plot_basic_hist.py", line 37, in plot_basic_hist
'xmax': xmax
UnboundLocalError: local variable 'xmax' referenced before assignment
============================================================
[INFO   ]          fastqc : Found 24 reports
[INFO   ]         multiqc : Compressing plot data

# Fixed code
def plot_basic_hist(samples, file_type, **plot_args):
    """Create line graph plot for basic histogram data for 'file_type'.

    The 'samples' parameter could be from the bbmap mod_data dictionary:
    samples = bbmap.MultiqcModule.mod_data[file_type]
    """

    sumy = sum(
        [
            int(samples[sample]["data"][x][0])
            for sample in samples
            for x in samples[sample]["data"]
        ]
    )

    cutoff = sumy * 0.999
    all_x = set()
    for item in sorted(chain(*[samples[sample]["data"].items() for sample in samples])):
        all_x.add(item[0])
        cutoff -= item[1][0]
        if cutoff < 0:
            xmax = item[0]
            break
    else:
        xmax = max(all_x)

    data = {
        sample: {
            x: samples[sample]["data"][x][0] if x in samples[sample]["data"] else 0
            for x in all_x
        }
        for sample in samples
    }

    plot_params = {
        "id": "bbmap-" + file_type + "_plot",
        "title": "BBTools: " + plot_args["plot_title"],
        "xmax": xmax,
    }
    plot_params.update(plot_args["plot_params"])
    plot = linegraph.plot(data, plot_params)

    return plot