# Buggy code
def _run_delly(bam_files, chrom, ref_file, work_dir, items):
    """Run delly, calling structural variations for the specified type."""
    batch = sshared.get_cur_batch(items)
    ext = "-%s-svs" % batch if batch else "-svs"
    out_file = os.path.join(
        work_dir,
        "%s%s-%s.bcf"
        % (os.path.splitext(os.path.basename(bam_files[0]))[0], ext, chrom),
    )
    final_file = "%s.vcf.gz" % (utils.splitext_plus(out_file)[0])
    cores = min(
        utils.get_in(items[0], ("config", "algorithm", "num_cores"), 1), len(bam_files)
    )
    if not utils.file_exists(out_file) and not utils.file_exists(final_file):
        with file_transaction(items[0], out_file) as tx_out_file:
            if sshared.has_variant_regions(items, out_file, chrom):
                exclude = ["-x", _delly_exclude_file(items, out_file, chrom)]
                cmd = (
                    ["delly", "call", "-g", ref_file, "-o", tx_out_file]
                    + exclude
                    + bam_files
                )
                multi_cmd = "export OMP_NUM_THREADS=%s && export LC_ALL=C && " % cores
                try:
                    do.run(multi_cmd + " ".join(cmd), "delly structural variant")
                except subprocess.CalledProcessError as msg:
                    # Small input samples, write an empty vcf
                    if (
                        "Sample has not enough data to estimate library parameters"
                        in str(msg)
                    ):
                        pass
                    # delly returns an error exit code if there are no variants
                    elif "No structural variants found" not in str(msg):
                        raise
    return [_bgzip_and_clean(out_file, items)]


# Traceback (UnicodeDecodeError)
Traceback (most recent call last):
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/distributed/ipythontasks.py", line 52, in _setup_logging
yield config
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/distributed/ipythontasks.py", line 447, in detect_sv
return ipython.zip_args(apply(structural.detect_sv, *args))
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/distributed/ipythontasks.py", line 80, in apply
return object(*args, **kwargs)
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/__init__.py", line 205, in detect_sv
for svdata in caller_fn(items):
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/cnvkit.py", line 57, in run
return _cnvkit_by_type(items, background)
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/cnvkit.py", line 67, in _cnvkit_by_type
return _run_cnvkit_single(items[0])
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/cnvkit.py", line 110, in _run_cnvkit_single
return _associate_cnvkit_out(ckouts, [data])
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/cnvkit.py", line 90, in _associate_cnvkit_out
ckout = _add_plots_to_output(ckout, data)
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/cnvkit.py", line 640, in _add_plots_to_output
scatter = _add_scatter_plot(out, data)
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/cnvkit.py", line 710, in _add_scatter_plot
priority_bed = plot._prioritize_plot_regions(pybedtools.BedTool(priority_bed), data, os.path.dirname(out_file))
File "/opt/bcbio/anaconda/lib/python3.6/site-packages/bcbio/structural/plot.py", line 91, in _prioritize_plot_regions
for r in region_bt:
File "pybedtools/cbedtools.pyx", line 754, in pybedtools.cbedtools.IntervalIterator.__next__
File "/opt/bcbio/anaconda/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 749: ordinal not in range(128)

# Fixed code
def _run_delly(bam_files, chrom, ref_file, work_dir, items):
    """Run delly, calling structural variations for the specified type."""
    batch = sshared.get_cur_batch(items)
    ext = "-%s-svs" % batch if batch else "-svs"
    out_file = os.path.join(
        work_dir,
        "%s%s-%s.bcf"
        % (os.path.splitext(os.path.basename(bam_files[0]))[0], ext, chrom),
    )
    final_file = "%s.vcf.gz" % (utils.splitext_plus(out_file)[0])
    cores = min(
        utils.get_in(items[0], ("config", "algorithm", "num_cores"), 1), len(bam_files)
    )
    if not utils.file_exists(out_file) and not utils.file_exists(final_file):
        with file_transaction(items[0], out_file) as tx_out_file:
            if sshared.has_variant_regions(items, out_file, chrom):
                exclude = ["-x", _delly_exclude_file(items, out_file, chrom)]
                cmd = (
                    ["delly", "call", "-g", ref_file, "-o", tx_out_file]
                    + exclude
                    + bam_files
                )
                locale_to_use = utils.get_locale()
                multi_cmd = "export OMP_NUM_THREADS=%s && export LC_ALL=%s && " % (
                    cores,
                    locale_to_use,
                )
                try:
                    do.run(multi_cmd + " ".join(cmd), "delly structural variant")
                except subprocess.CalledProcessError as msg:
                    # Small input samples, write an empty vcf
                    if (
                        "Sample has not enough data to estimate library parameters"
                        in str(msg)
                    ):
                        pass
                    # delly returns an error exit code if there are no variants
                    elif "No structural variants found" not in str(msg):
                        raise
    return [_bgzip_and_clean(out_file, items)]