Plotting genes with bioplotlib


In [1]:
%matplotlib inline

In [19]:
from copy import copy, deepcopy

from Bio import SeqIO
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.patches import PathPatch
from matplotlib.path import Path
import matplotlib
import matplotlib.gridspec as gridspec

from matplotlib.transforms import Bbox, TransformedBbox, \
     blended_transform_factory

from mpl_toolkits.axes_grid1.inset_locator import BboxPatch, BboxConnector,\
     BboxConnectorPatch

In [31]:
try:
    from importlib import reload
except ImportError:
    pass

from bioplotlib import collections
from bioplotlib import links
from bioplotlib import feature_shapes

reload(collections)
reload(links)
reload(feature_shapes)

from bioplotlib.collections import Feature
from bioplotlib.collections import new_shape

from bioplotlib.feature_shapes import Triangle
from bioplotlib.feature_shapes import Rectangle
from bioplotlib.feature_shapes import Arrow
from bioplotlib.feature_shapes import OpenSemicircle

#from bioplotlib.axis import

In [24]:
fig, ax = plt.subplots()
interval = [(1, 2, -1), (3, 4, -1), (5, 6, -1), (7, 8, -1)][::-1]

f = Feature(
    interval,
    shape=new_shape(Triangle),
    offset=0.5
    )

ax.add_collection(f)

f = Feature(
    interval,
    shape=[
        new_shape(Rectangle, linewidth=0, facecolor="red"),
        new_shape(Arrow, linewidth=0, facecolor="red")
        ],
    between_shape=[
        new_shape(Rectangle, facecolor="black", linewidth=0)
        ],
    offset=2.5,
    )

ax.add_collection(f)

ax.set_ylim([0, 10])
ax.set_xlim([0, 10])
plt.show()



In [ ]:


In [ ]:


In [ ]:


In [25]:
fig, ax = plt.subplots()

shape = Arrow(2, 6, 1, tail_width=0.8, offset=3, by_axis="y")

ax.add_patch(shape)

ax.set_ylim([0, 10])
ax.set_xlim([0, 10])
plt.show()



In [26]:
isinstance(shape, PathPatch)


Out[26]:
False

In [27]:
fig, ax = plt.subplots()

shape = Triangle(2, 5, 1, offset=5)
ax.add_patch(shape)
shape = Triangle(2, 5, -1, offset=4)
ax.add_patch(shape)

shape = Triangle(5, 2, 1, offset=3)
ax.add_patch(shape)
shape = Triangle(5, 2, -1, offset=2)
ax.add_patch(shape)

shape = OpenSemicircle(2, 8, width=2, offset=1, fill=None, linewidth=2)
ax.add_patch(shape)
shape = Triangle(2, 5, offset=0)
ax.add_patch(shape)

ax.set_ylim([0, 10])
ax.set_xlim([0, 10])
plt.show()



In [29]:
seq = SeqIO.read("../bioplotlib/data/Sc-chr01.gb", 'genbank')

In [33]:
fig, ax = plt.subplots()

feature_patches, text_patches = draw_region(seq, 60000, 100000)
for patch in feature_patches:
    ax.add_patch(patch)

#ax.set_ylim([0, 1])
#ax.set_xlim([60000, 100000])
ax.autoscale()
fig.set_size_inches(6, 1.5)
#fig.savefig(pjoin(step35, candidate['id'] + 'gene_structure.svg'))
plt.show()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-33-f4514a4c710f> in <module>()
      1 fig, ax = plt.subplots()
      2 
----> 3 feature_patches, text_patches = draw_region(seq, 60000, 100000)
      4 for patch in feature_patches:
      5     ax.add_patch(patch)

NameError: name 'draw_region' is not defined

In [14]:
shape


Out[14]:
Triangle(start=2, end=5, offset=0, width=1, by_axis=None)

In [15]:
fig = plt.figure()

gs = gridspec.GridSpec(2, 1, hspace=1)
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])

ax1.set_xlim([0, 50000])
ax2.set_xlim([0, 100000])

slink = CrossLink(ax1=ax2, ax2=ax1, y1_range=[0, 1], y2_range=[1, 0], y1_control_point=0.2, alpha=0.4, linewidth=0., zorder=0)
slink(5000, 5000, 30000, 10000)
slink(60000, -20000, 20000, -5000, facecolor='green', y1_control_point=2)
slink(80000, -20000, 25000, -5000, facecolor='green', y1_control_point=2)

slink = CrossLink(ax1=ax2, y1_range=[1, 0], y1_control_point=-2, alpha=0.4, linewidth=0., zorder=0)
slink(1000, 5000, 30000, 10000)

feature_patches, dummy = draw_region(seq, 0, 100000, exon=Triangle(width=1., fill=True, facecolor='white'))
for patch in feature_patches:
    ax1.add_patch(patch)

feature_patches, dummy = draw_region(seq, 0, 100000, exon=Triangle(width=1., fill=True))
for patch in feature_patches:
    ax2.add_patch(patch)

ax1.patch.set_fill(False)
r = copy(ax2.patch)
r.set_facecolor('red')
r.set_zorder(0)
ax2.patch.set_fill(False)
fig.patches.insert(0, r)
plt.show()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-98f9f2367ac2> in <module>()
      9 
     10 slink = CrossLink(ax1=ax2, ax2=ax1, y1_range=[0, 1], y2_range=[1, 0], y1_control_point=0.2, alpha=0.4, linewidth=0., zorder=0)
---> 11 slink(5000, 5000, 30000, 10000)
     12 slink(60000, -20000, 20000, -5000, facecolor='green', y1_control_point=2)
     13 slink(80000, -20000, 25000, -5000, facecolor='green', y1_control_point=2)

TypeError: __call__() takes 1 positional argument but 5 were given

In [23]:
fig = plt.figure()

gs = gridspec.GridSpec(2, 1, hspace=1)
rgs = gridspec.GridSpecFromSubplotSpec(
    1, 3,
    subplot_spec=gs[0],
    width_ratios=[1, 2, 1],
    )
qgs = gridspec.GridSpecFromSubplotSpec(
    1, 2,
    subplot_spec=gs[1],
    width_ratios=[1, 1],
    )
ax1 = fig.add_subplot(rgs[1])
ax2 = fig.add_subplot(qgs[0])
ax3 = fig.add_subplot(qgs[1])

ax1.set_xlim([0, 100000])
ax2.set_xlim([0, 50000])
ax3.set_xlim([50000, 100000])

feature_patches, dummy = draw_region(seq, 0, 100000, exon=Triangle(width=1., fill=True, facecolor='white'))
for patch in feature_patches:
    ax1.add_patch(patch)
feature_patches, dummy = draw_region(seq, 0, 100000, exon=Triangle(width=1., fill=True))
for patch in feature_patches:
    ax2.add_patch(patch)
feature_patches, dummy = draw_region(seq, 0, 100000, exon=Triangle(width=1., fill=True))
for patch in feature_patches:
    ax3.add_patch(patch)


slink = CrossLink(ax1=ax2, ax2=ax1, y1_range=[0, 1], y2_range=[1, 0], y1_control_point=0, alpha=0.4, linewidth=0., zorder=0)
slink(0, 49000, 0, 49000, facecolor='green')

slink = CrossLink(ax1=ax3, ax2=ax1, y1_range=[0, 1], y2_range=[1, 0], y1_control_point=0, alpha=0.4, linewidth=0., zorder=0)
slink(51000, 49000, 100000, -49000, facecolor='green')

slink = CrossLink(ax1=ax2, y1_range=[1, 0], y1_control_point=-2, alpha=0.4, linewidth=0., zorder=0)
slink(1000, 5000, 30000, 10000)

slink = CrossLink(ax1=ax2, ax2=ax3, y1_range=[1, 0], y1_control_point=-2, alpha=0.4, linewidth=0., zorder=0)
slink(20000, 5000, 60000, 10000)

slink = CrossLink(ax1=ax1, y1_range=[0, 1], y1_control_point=2, alpha=0.4, linewidth=0., zorder=0)
slink(20000, 5000, 60000, 10000)


ax1.patch.set_fill(False)
ax2.patch.set_fill(False)
ax3.patch.set_fill(False)

plt.show()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-23-5e8ade92ede7> in <module>()
     20 ax3.set_xlim([50000, 100000])
     21 
---> 22 feature_patches, dummy = draw_region(seq, 0, 100000, exon=Triangle(width=1., fill=True, facecolor='white'))
     23 for patch in feature_patches:
     24     ax1.add_patch(patch)

/home/darcy/bin/bioplotlib/bioplotlib/draw_wrappers.py in draw_region(seq, start, end, intron_threshold, exon, intron, other_shapes, names_to_print)
    208 
    209                 for e in exons:
--> 210                     verts, codes = exon(e[0], 0., e[1])
    211                     p = Path(verts, codes)
    212                     feature_patches.append(

TypeError: 'PathPatch' object is not iterable

In [31]:
fig, ax = plt.subplots()

f = Feature(blocks=[(2, 3), (3, 4)], shapes=[Triangle(width=1, offset=-0.5, facecolor = "none")])
path, props = f.draw()

ax.add_collection(PathCollection(path, **props))

ax.set_ylim([0, 1])
ax.set_xlim([0, 10])
plt.show()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-b7f1295434e9> in <module>()
      2 
      3 f = Feature(blocks=[(2, 3), (3, 4)], shapes=[Triangle(width=1, offset=-0.5, facecolor = "none")])
----> 4 path, props = f.draw()
      5 
      6 ax.add_collection(PathCollection(path, **props))

/home/darcy/bin/bioplotlib/bioplotlib/collections.py in draw(self)
    120             properties = self.__extend_dict(
    121                 properties,
--> 122                 self.shapes[shape_pos].properties
    123                 )
    124 

/home/darcy/bin/bioplotlib/bioplotlib/collections.py in __extend_dict(self, old_dict, new_dict)
     91                 length = len(v)
     92 
---> 93         for key, val in new_dict:
     94             if key not in old_dict:
     95                 old_dict[key] = [None] * length

ValueError: too many values to unpack (expected 2)

In [33]:
from builtins import super


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-33-17a7c9aeaaa3> in <module>()
----> 1 from builtins import super

ImportError: No module named builtins