Skip to content

Commit

Permalink
Add support for giving GenomeDiagram features a URL, useful with SVG …
Browse files Browse the repository at this point in the history
…output from ReportLab 2.4
  • Loading branch information
peterjc committed Feb 23, 2010
1 parent ca87378 commit 2586e84
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
10 changes: 7 additions & 3 deletions Bio/Graphics/GenomeDiagram/_AbstractDrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def draw_box((x1, y1), (x2, y2),
return Polygon([x1, y1, x2, y1, x2, y2, x1, y2],
strokeColor=strokecolor,
fillColor=color,
strokewidth=0)
strokewidth=0,
**kwargs)


def draw_polygon(list_of_points,
color=colors.lightgreen, border=None, colour=None,
Expand Down Expand Up @@ -153,7 +155,8 @@ def draw_polygon(list_of_points,
return Polygon(xy_list,
strokeColor=strokecolor,
fillColor=color,
strokewidth=0)
strokewidth=0,
**kwargs)


def draw_arrow((x1, y1), (x2, y2), color=colors.lightgreen, border=None,
Expand Down Expand Up @@ -222,7 +225,8 @@ def draw_arrow((x1, y1), (x2, y2), color=colors.lightgreen, border=None,
strokeWidth=1,
#default is mitre/miter which can stick out too much:
strokeLineJoin=1, #1=round
fillColor=color)
fillColor=color,
**kwargs)

def angle2trig(theta):
""" angle2trig(angle)
Expand Down
20 changes: 15 additions & 5 deletions Bio/Graphics/GenomeDiagram/_CircularDrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,23 @@ def get_feature_sigil(self, feature, locstart, locend, **kwargs):
draw_methods = {'BOX': self._draw_arc,
'ARROW': self._draw_arc_arrow,
}

# Get sigil for the feature, location dependent on the feature strand
method = draw_methods[feature.sigil]
kwargs['head_length_ratio'] = feature.arrowhead_length
kwargs['shaft_height_ratio'] = feature.arrowshaft_height

# Get sigil for the feature, location dependent on the feature strand
method = draw_methods[feature.sigil]
#Support for clickable links... needs ReportLab 2.4 or later
#which added support for links in SVG output.
if hasattr(feature, "url") :
kwargs["hrefURL"] = feature.url
kwargs["hrefTitle"] = feature.name

if feature.color == colors.white:
border = colors.black
else:
border = feature.color

if feature.strand == 1:
sigil = method(ctr, top, startangle, endangle, feature.color,
border, orientation='right', **kwargs)
Expand All @@ -426,6 +434,7 @@ def get_feature_sigil(self, feature, locstart, locend, **kwargs):
else:
sigil = method(btm, top, startangle, endangle, feature.color,
border, **kwargs)

if feature.label: # Feature needs a label
label = String(0, 0, feature.name.strip(),
fontName=feature.label_font,
Expand Down Expand Up @@ -1043,7 +1052,6 @@ def _draw_arc_arrow(self, inner_radius, outer_radius, startangle, endangle,
x1,y1 = (x0+inner_radius*endsin, y0+inner_radius*endcos)
x2,y2 = (x0+outer_radius*endsin, y0+outer_radius*endcos)
x3,y3 = (x0+middle_radius*startsin, y0+middle_radius*startcos)

#return draw_polygon([(x1,y1),(x2,y2),(x3,y3)], color, border,
# stroke_line_join=1)
return Polygon([x1,y1,x2,y2,x3,y3],
Expand All @@ -1056,7 +1064,8 @@ def _draw_arc_arrow(self, inner_radius, outer_radius, startangle, endangle,
fillColor=color,
#default is mitre/miter which can stick out too much:
strokeLineJoin=1, #1=round
strokewidth=0)
strokewidth=0,
**kwargs)
#Note reportlab counts angles anti-clockwise from the horizontal
#(as in mathematics, e.g. complex numbers and polar coordinates)
#but we use clockwise from the vertical. Also reportlab uses
Expand Down Expand Up @@ -1094,7 +1103,8 @@ def _draw_arc_arrow(self, inner_radius, outer_radius, startangle, endangle,
fillColor=color,
#default is mitre/miter which can stick out too much:
strokeLineJoin=1, #1=round
strokewidth=0)
strokewidth=0,
**kwargs)
#Note reportlab counts angles anti-clockwise from the horizontal
#(as in mathematics, e.g. complex numbers and polar coordinates)
#but we use clockwise from the vertical. Also reportlab uses
Expand Down
7 changes: 7 additions & 0 deletions Bio/Graphics/GenomeDiagram/_LinearDrawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,14 @@ def get_feature_sigil(self, feature, x0, x1, fragment, **kwargs):
kwargs['head_length_ratio'] = feature.arrowhead_length
kwargs['shaft_height_ratio'] = feature.arrowshaft_height

#Support for clickable links... needs ReportLab 2.4 or later
#which added support for links in SVG output.
if hasattr(feature, "url") :
kwargs["hrefURL"] = feature.url
kwargs["hrefTitle"] = feature.name

strand = feature.strand

# Get sigil for the feature, location dependent on the feature strand
if strand == 1:
sigil = method((x0, ctr), (x1, top), color=feature.color,
Expand Down
33 changes: 26 additions & 7 deletions Tests/test_GenomeDiagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ def test_partial_diagram(self):
#Out of frame (too far right)
continue

#This URL should work in SVG output from recent versions
#of ReportLab. You need ReportLab 2.4 or later
try :
url = "http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi"+\
"?db=protein&id=%s" % feature.qualifiers["protein_id"][0]
except KeyError :
url = None

#Note that I am using strings for color names, instead
#of passing in color objects. This should also work!
if len(gds_features) % 2 == 0:
Expand All @@ -500,6 +508,7 @@ def test_partial_diagram(self):
#Checking it can cope with the old UK spelling colour.
#Also show the labels perpendicular to the track.
gds_features.add_feature(feature, colour=color,
url = url,
sigil="ARROW",
label_position = "start",
label_size = 8,
Expand Down Expand Up @@ -568,13 +577,23 @@ def test_diagram_via_methods_pdf(self):
index = genbank_entry.seq.find(site, start=index)
if index == -1 : break
feature = SeqFeature(FeatureLocation(index, index+6), strand=None)
gds_features.add_feature(feature, color=color,
#label_position = "middle",
label_size = 10,
label_color=color,
#label_angle = 90,
name=name,
label=True)

#This URL should work in SVG output from recent versions
#of ReportLab. You need ReportLab 2.4 or later
try :
url = "http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi"+\
"?db=protein&id=%s" % feature.qualifiers["protein_id"][0]
except KeyError :
url = None

gds_features.add_feature(feature, color = color,
url = url,
#label_position = "middle",
label_size = 10,
label_color = color,
#label_angle = 90,
name = name,
label = True)
index += len(site)
del index

Expand Down

0 comments on commit 2586e84

Please sign in to comment.