Skip to content

Commit

Permalink
Refactor GenomeDiagram to handle strand inside the sigil code
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjc committed Aug 1, 2012
1 parent 4acc170 commit d9c416b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 32 deletions.
50 changes: 37 additions & 13 deletions Bio/Graphics/GenomeDiagram/_CircularDrawer.py
Expand Up @@ -431,8 +431,8 @@ def get_feature_sigil(self, feature, locstart, locend, **kwargs):
# Distribution dictionary for various ways of drawing the feature
# Each method takes the inner and outer radii, the start and end angle
# subtended at the diagram center, and the color as arguments
draw_methods = {'BOX': self._draw_arc,
'ARROW': self._draw_arc_arrow,
draw_methods = {'BOX': self._draw_sigil_box,
'ARROW': self._draw_sigil_arrow,
}

# Get sigil for the feature, location dependent on the feature strand
Expand All @@ -446,17 +446,8 @@ def get_feature_sigil(self, feature, locstart, locend, **kwargs):
kwargs["hrefURL"] = feature.url
kwargs["hrefTitle"] = feature.name

border = feature.border

if feature.strand == 1:
sigil = method(ctr, top, startangle, endangle, feature.color,
border, orientation='right', **kwargs)
elif feature.strand == -1:
sigil = method(btm, ctr, startangle, endangle, feature.color,
border, orientation='left', **kwargs)
else:
sigil = method(btm, top, startangle, endangle, feature.color,
border, **kwargs)
sigil = method(btm, ctr, top, startangle, endangle, feature.strand,
color=feature.color, border=feature.border, **kwargs)

if feature.label: # Feature needs a label
label = String(0, 0, feature.name.strip(),
Expand Down Expand Up @@ -1053,6 +1044,20 @@ def canvas_angle(self, base):
angle = self.sweep*2*pi*(base-self.start)/self.length
return (angle, cos(angle), sin(angle))

def _draw_sigil_box(self, bottom, center, top,
startangle, endangle, strand,
**kwargs):
"""Draw BOX sigil."""
if strand == 1:
inner_radius = center
outer_radius = top
elif strand == -1:
inner_radius = bottom
outer_radius = center
else:
inner_radius = bottom
outer_radius = top
return self._draw_arc(inner_radius, outer_radius, startangle, endangle, **kwargs)

def _draw_arc(self, inner_radius, outer_radius, startangle, endangle,
color, border=None, colour=None, **kwargs):
Expand Down Expand Up @@ -1211,6 +1216,25 @@ def _draw_arc_poly(self, inner_radius, outer_radius,
strokeLineJoin=1, #1=round
)

def _draw_sigil_arrow(self, bottom, center, top,
startangle, endangle, strand,
**kwargs):
"""Draw ARROW sigil."""
if strand == 1:
inner_radius = center
outer_radius = top
orientation = "right"
elif strand == -1:
inner_radius = bottom
outer_radius = center
orientation = "left"
else:
inner_radius = bottom
outer_radius = top
orientation = "right" #backwards compatibility
return self._draw_arc_arrow(inner_radius, outer_radius, startangle, endangle,
orientation=orientation, **kwargs)

def _draw_arc_arrow(self, inner_radius, outer_radius, startangle, endangle,
color, border=None,
shaft_height_ratio=0.4, head_length_ratio=0.5, orientation='right',
Expand Down
55 changes: 36 additions & 19 deletions Bio/Graphics/GenomeDiagram/_LinearDrawer.py
Expand Up @@ -1010,11 +1010,10 @@ def get_feature_sigil(self, feature, x0, x1, fragment, **kwargs):
1/0

# Distribution dictionary for various ways of drawing the feature
# Each method takes the corners of a containing box and a color
# as argument
draw_methods = {'BOX': draw_box,
'ARROW': draw_arrow,
draw_methods = {'BOX': self._draw_sigil_box,
'ARROW': self._draw_sigil_arrow,
}

method = draw_methods[feature.sigil]
kwargs['head_length_ratio'] = feature.arrowhead_length
kwargs['shaft_height_ratio'] = feature.arrowshaft_height
Expand All @@ -1025,21 +1024,11 @@ def get_feature_sigil(self, feature, x0, x1, fragment, **kwargs):
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,
border=feature.border,
orientation='right', **kwargs)
elif strand == -1:
sigil = method((x1, btm), (x0, ctr), color=feature.color,
border=feature.border,
orientation='left', **kwargs)
else:
sigil = method((x0, btm), (x1, top), color=feature.color,
border=feature.border,
**kwargs)
# Get sigil for the feature, give it the bounding box straddling
# the axis (it decides strand specific placement)
sigil = method(btm, ctr, top, x0, x1, strand=feature.strand,
color=feature.color, border=feature.border,
**kwargs)
if feature.label: # Feature requires a label
label = String(0, 0, feature.name,
fontName=feature.label_font,
Expand Down Expand Up @@ -1351,3 +1340,31 @@ def canvas_location(self, base):
x_offset = 1. * self.pagewidth * base_offset / self.fragment_bases
return fragment, x_offset

def _draw_sigil_box(self, bottom, center, top, x1, x2, strand, **kwargs):
"""Draw BOX sigil."""
if strand == 1:
y1 = center
y2 = top
elif strand == -1:
y1 = bottom
y2 = center
else:
y1 = bottom
y2 = top
return draw_box((x1,y1), (x2,y2), **kwargs)

def _draw_sigil_arrow(self, bottom, center, top, x1, x2, strand, **kwargs):
"Draw ARROW sigil."""
if strand == 1:
y1 = center
y2 = top
orientation = "right"
elif strand== -1:
y1 = bottom
y2 = center
orientation = "left"
else:
y1 = bottom
y2 = top
orientation = "right" #backward compatibility
return draw_arrow((x1,y1), (x2,y2), orientation=orientation, **kwargs)

0 comments on commit d9c416b

Please sign in to comment.