def test_mixed_digestions(tmpdir):
    png_path = os.path.join(str(tmpdir), "test.png")
    enzymes = "EcoRI", "EcoRV", "BamHI", "XhoI"
    mixes = [[e] for e in enzymes] + list(itertools.combinations(enzymes, 2))

    patterns = [
        BandsPattern(
            compute_digestion_bands(example_sequence, mix, linear=True),
            ladder=LADDER_100_to_4k,
            label=" + ".join(mix),
            label_fontdict={
                "rotation": 40,
                "size": 9
            },
        ) for mix in mixes
    ]
    patterns_set = BandsPatternsSet(
        patterns=[LADDER_100_to_4k] + patterns,
        ladder=LADDER_100_to_4k,
        label="Digestion results",
        ladder_ticks=3,
    )
    ax = patterns_set.plot()
    ax.figure.savefig(png_path, bbox_inches="tight", dpi=200)
    assert os.path.exists(png_path)
def test_simple_digestions_matplotlib_plot(tmpdir):
    png_path = os.path.join(str(tmpdir), "test.png")
    patterns = [
        BandsPattern(
            compute_digestion_bands(example_sequence, [enzyme]),
            ladder=LADDER_100_to_4k,
            label=enzyme,
            global_bands_props={"label": "=size"},
        ) for enzyme in ["BamHI", "EcoRI", "EcoRV", "PstI", "SpeI", "XbaI"]
    ]
    patterns_set = BandsPatternsSet(
        patterns=[LADDER_100_to_4k] + patterns,
        ladder=LADDER_100_to_4k,
        label="Digestion results",
        ladder_ticks=3,
    )

    ax = patterns_set.plot()
    ax.figure.savefig(png_path, bbox_inches="tight", dpi=200)
    assert os.path.exists(png_path)
def test_simple_digestions_bokeh_plot(tmpdir):
    patterns = [
        BandsPattern(
            compute_digestion_bands(example_sequence, [enzyme]),
            ladder=LADDER_100_to_4k,
            label=enzyme,
            global_bands_props={"label": "=size"},
        ) for enzyme in ["BamHI", "EcoRI", "EcoRV", "PstI", "SpeI", "XbaI"]
    ]
    patterns_set = BandsPatternsSet(
        patterns=[LADDER_100_to_4k] + patterns,
        ladder=LADDER_100_to_4k,
        label="Digestion results",
        ladder_ticks=3,
    )

    plot = patterns_set.plot_with_bokeh()
    target_file = os.path.join(str(tmpdir), "plot_with_bokeh.html")
    with open(target_file, "w+") as f:
        f.write(file_html(plot, CDN, "Example Sequence"))
    with open(target_file, "r") as f:
        assert len(f.read()) > 9400
"""Prediction of digestion bands sizes in different scenarios.

The following example shows how to compute digestion bands in the case of
a linear fragment, a circular fragment, and a multi-enzymes digestion.
"""

from bandwagon import compute_digestion_bands

# Read the sequence (a string of the form 'ATGTGTGGTA...' etc.)
with open("example_sequence.txt", "r") as f:
    sequence = f.read()

# Compute digestion bands for a linear construct
print(compute_digestion_bands(sequence, ["EcoRI"], linear=True))
# Result >>> [400, 1017, 3583]

# Compute digestion bands for a circular construct
print(compute_digestion_bands(sequence, ["EcoRI"], linear=False))
# Result >>> [1017, 3983]

# Compute digestion bands for an enzymatic mix
print(compute_digestion_bands(sequence, ["EcoRI", "BamHI"]))
# Result >>> [400, 417, 600, 3583]
"""Simple gel simulation with gelsimulation.py.

This example shows how to plot the the digestion patterns produced by different
restriction enzymes on a same DNA sequence.
"""
import itertools
from bandwagon import (BandsPattern, BandsPatternsSet, LADDER_100_to_4k,
                       compute_digestion_bands)

with open("example_sequence.txt", "r") as f:
    sequence = f.read()

enzymes = "EcoRI", "EcoRV", "BamHI", "XhoI"
mixes = [[e] for e in enzymes] + list(itertools.combinations(enzymes, 2))

patterns = [
    BandsPattern(compute_digestion_bands(sequence, mix, linear=True),
                 ladder=LADDER_100_to_4k, label=" + ".join(mix),
                 label_fontdict={"rotation": 40, "size": 9})
    for mix in mixes
]

patterns_set = BandsPatternsSet(patterns=[LADDER_100_to_4k] + patterns,
                                ladder=LADDER_100_to_4k,
                                label="Digestion results", ladder_ticks=3)

ax = patterns_set.plot()
ax.figure.savefig("mixed_digestions.png", bbox_inches="tight", dpi=120)
"""Simple gel simulation with gelsimulation.py.

This example shows how to plot the digestion patterns produced by different
restriction enzymes on a same DNA sequence.
"""

from bandwagon import (BandsPattern, BandsPatternsSet, LADDER_100_to_4k,
                       compute_digestion_bands)

with open("example_sequence.txt", "r") as f:
    sequence = f.read()

patterns = [
    BandsPattern(compute_digestion_bands(sequence, [enzyme], linear=True),
                 ladder=LADDER_100_to_4k, label=enzyme)
    for enzyme in ["BamHI", "EcoRI", "EcoRV", "PstI", "SpeI", "XbaI"]
]
patterns_set = BandsPatternsSet(patterns=[LADDER_100_to_4k] + patterns,
                                ladder=LADDER_100_to_4k,
                                label="Digestion results", ladder_ticks=3)

ax = patterns_set.plot()
ax.figure.savefig("simple_digestions.png", bbox_inches="tight", dpi=120)
def test_compute_digestion_bands(digestion, linear, expected):
    bands = compute_digestion_bands(example_sequence, digestion, linear=linear)
    assert set(expected) == set(bands)