If you need more control over your tree images, you can program how the tree is rendered using ETE (Python language)
A very very simple example that associates node features with node size and color would look like this:
from ete2 import Tree, faces
import colorsys
import random
def hls2hex(h, l, s):
''' Converts from HLS to RGB '''
return '#%02x%02x%02x' %tuple(map(lambda x: int(x*255),
colorsys.hls_to_rgb(h, l, s)))
def get_color(h=None, s=0.5, l=0.5):
''' Returns a RGB color code with the specific Hue, Saturation and
lightness. Otherwise, it returns a random color'''
if not h:
h = random.random()
return hls2hex(h, l, s)
def my_layout(node):
""" This function set the rules that control how nodes are drawn"""
value = tax2size.getnode.name, None)
if value is not None:
# generate a gradient on color lightness for 0.3 to 0.8. A
# assume value is in the (0,1) range.
gradient = 0.3 + (float(value)/100)/2
# Set node size and color based on the dictionary of user
# values
color = get_color(h=0.9, l=gradient)
node.img_style["size"] = 20 + value
node.img_style["fgcolor"] = color
node.img_style["line_type"] = 1
# Text labels can also be added to the image
faces.add_face_to_node(faces.TextFace(" (%s) " %node.name), \
node=node, column=0, aligned=True)
faces.add_face_to_node(faces.TextFace("value: %s" %value), \
node=node, column=1, aligned=True)
else:
node.img_style["size"] = 0
# Loads the example tree
t = Tree("(((9606,9844),99287),(9823,9913));")
# Loads the example values associated to leaf nodes
tax2size = {
"9606": 100, "9823": 80, "9844": 60, "9913": 40, "99287": 20 }
t.show(my_layout)
t.render("gradient.png", my_layout)
fullsize
That's neat. Thanks jhc !
Cool module. and nice color choice.
jhc,great idea. I would like to do something similar only start with the Tree-of-Life and assign abunce values. However I can't seem to open the NCBI Taxonomy trees (http://itol.embl.de/other_trees.shtml) with ETE2. I would love to copy your general method but using the entire taxonomical dataset.