Skip to content

Commit

Permalink
Merge branch adding label_colors arg to Phylo.draw
Browse files Browse the repository at this point in the history
See GitHub pull request #672 and branch  'phylo-draw-tip-colors'
of https://github.com/bamueh/biopython
  • Loading branch information
peterjc committed Nov 24, 2015
2 parents 8a4fcbd + 47a6821 commit be114b6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Bio/Phylo/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def draw_clade(clade, startcol):

def draw(tree, label_func=str, do_show=True, show_confidence=True,
# For power users
axes=None, branch_labels=None, *args, **kwargs):
axes=None, branch_labels=None, label_colors=None, *args, **kwargs):
"""Plot the given tree using matplotlib (or pylab).
The graphic is a rooted tree, drawn with roughly the same algorithm as
Expand Down Expand Up @@ -322,6 +322,10 @@ def draw(tree, label_func=str, do_show=True, show_confidence=True,
But if you would like to alter the formatting of confidence values,
or label the branches with something other than confidence, then use
this option.
label_colors : dict or callable
A function or a dictionary specifying the color of the tip label.
If the tip label can't be found in the dict or label_colors is
None, the label will be shown in black.
"""

try:
Expand Down Expand Up @@ -365,6 +369,20 @@ def format_branch_label(clade):
"branch_labels must be either a dict or a callable (function)"
format_branch_label = branch_labels

# options for displaying label colors.
if label_colors:
if callable(label_colors):
def get_label_color(label):
return label_colors(label)
else:
# label_colors is presumed to be a dict
def get_label_color(label):
return label_colors.get(label, 'black')
else:
def get_label_color(label):
# if label_colors is not specified, use black
return 'black'

# Layout

def get_x_positions(tree):
Expand Down Expand Up @@ -446,7 +464,8 @@ def draw_clade(clade, x_start, color, lw):
label = label_func(clade)
if label not in (None, clade.__class__.__name__):
axes.text(x_here, y_here, ' %s' %
label, verticalalignment='center')
label, verticalalignment='center',
color=get_label_color(label))
# Add label above the branch (optional)
conf_label = format_branch_label(clade)
if conf_label:
Expand Down
30 changes: 30 additions & 0 deletions Tests/test_Phylo_depend.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,36 @@ def test_draw(self):
Phylo.draw(apaf, do_show=False, branch_labels={apaf.root: 'Root'})
Phylo.draw(apaf, do_show=False, branch_labels=lambda c: c.branch_length)

def test_draw_with_label_colors_dict(self):
"""Run the tree layout algorithm with a label_colors argument passed in
as a dictionary. Don't display tree."""
pyplot.ioff() # Turn off interactive display
dollo = Phylo.read(EX_DOLLO, 'phyloxml')
apaf = Phylo.read(EX_APAF, 'phyloxml')
label_colors_dollo = {
'f_50': 'red',
'f_34': 'blue',
}
label_colors_apaf = {
'22_MOUSE': 'red',
'18_NEMVE': 'blue',
}
Phylo.draw(dollo, label_colors=label_colors_dollo, do_show=False)
Phylo.draw(apaf, label_colors=label_colors_apaf, do_show=False)

def test_draw_with_label_colors_callable(self):
"""Run the tree layout algorithm with a label_colors argument passed in
as a callable. Don't display tree."""
pyplot.ioff() # Turn off interactive display
dollo = Phylo.read(EX_DOLLO, 'phyloxml')
apaf = Phylo.read(EX_APAF, 'phyloxml')

label_colors_dollo = lambda label: 'r' if label == 'f_50' else 'k'
label_colors_apaf = lambda label: 'r'

Phylo.draw(dollo, label_colors=label_colors_dollo, do_show=False)
Phylo.draw(apaf, label_colors=label_colors_apaf, do_show=False)

def test_draw_ascii(self):
"""Tree to Graph conversion."""
handle = StringIO()
Expand Down

0 comments on commit be114b6

Please sign in to comment.