Toytree is a Python tree plotting library that uses the modern and minimalist graphics library Toyplot to generate high quality figures that can be displayed directly within jupyter-notebooks or exported in a variety of formats. Find the full documentation at http://toytree.readthedocs.io.
## conda install toytree -c eaton-lab
Import toytree, toyplot, and the numerical library numpy for generating data. If you installed toytree with conda then all of these modules will be available.
import toytree
import toyplot
import numpy
toytree.__version__
'0.0.10'
Toytree can parse a wide variety of tree formats in newick or extended newick format, where trees are represented as a set of nested parentheses with additional metadata linked to nodes.
## an example newick string with edge lengths and support values
newick = """
((apple:2,orange:4)100:2,(((tomato:2,eggplant:1)
100:2,pepper:3)90:1,tomatillo:2)100:1);
"""
## create a toytree object from the newick string
tre = toytree.tree(newick)
## A toytree object in memory
tre
<toytree.tree.Toytree at 0x7f0869e54050>
To draw a tree from a Toytree object simply call its .draw()
function which will generate a Canvas and a set of axes on which the plot is embedded. The Canvas is represented in HTML which allows it to be displayed seemlessly in a jupyter-notebook, and to look crisp on the web, as in this notebook presentation.
tre.draw(width=200);
A fun advantage to generating figures in HTML is that you can easily add interactive features to your plots. In the example below I use the magic argument node_labels=True
to diplay markers on nodes with an interactive hover feature. Hover your cursor over a node to see all of the features associated with it. Node features are parsed from the newick string, or can be added later.
tre.draw(width=200, node_labels=True);
Besides the magic command node_values=True
you can also set specific node features to be displayed on the tree. Or you can access node values to create custom lists to use as arguments to modify nodes colors or sizes. This is all controlled through arguments to the draw()
function.
## get list of support values in node-order
supports = tre.get_node_values("support", show_root=False, show_tips=False)
## get list of node colors in node-order
colors = [tre.colors[0] if i==100 else tre.colors[1] for i in supports]
## draw with node supports and some styling
tre.draw(
width=300,
node_labels=supports,
node_color=colors,
node_size=20,
node_style={"stroke": "none"}
);
Toyplot uses a stripped-down version of the TreeNode object from the ete3 library to represent Tree objects in memory as a hierarchical structure that is easy to traverse and manipulate. This makes it easy to store and access meta-data on a tree in a programmatic way. See the ete documentation for many helpful tips on editing trees. In this example we add a new feature to each node and write the new tree string in extended newick format.
## a simple tree
tre = toytree.tree("(((a,b),c),d);")
## add a new feature to every node
for node in tre.tree.traverse():
node.add_feature("new_feature", 100)
## write the tree string in NHX format with features
nhx = tre.tree.write(features=["new_feature", "dist", "support"], format=0)
## print the newick string
nhx
'(d:1[&&NHX:new_feature=100:dist=1.0:support=1.0],(c:1[&&NHX:new_feature=100:dist=1.0:support=1.0],(a:1[&&NHX:new_feature=100:dist=1.0:support=1.0],b:1[&&NHX:new_feature=100:dist=1.0:support=1.0])1:1[&&NHX:new_feature=100:dist=1.0:support=1.0])1:1[&&NHX:new_feature=100:dist=1.0:support=1.0]);'
Viewing large trees is a bit easier with toytree than with alternative graphics libraries because the images can be easily viewed in a browser. Here we read in a large tree and display it. Scroll down to view the entire tree.
## load a newick string from a file path
newick = "/home/deren/Dropbox/Viburnum_t65_min4"
tre = toytree.tree(newick)
## draw it
tre.draw(
width=500,
height=900,
node_labels=tre.get_node_values("support"),
);
It can be informative to visualize a distribution of trees, such as a posterior distribution of species trees, and a cloud tree diagram which is easy to do with toyplot since opacity is an easy variable to modify. Here we create a MultiTree object which holds a list of Toytrees in memory, and call the draw_cloudtree()
function.
## a file with trees sampled from a posterior dist.
mcmcfile = "/home/deren/Documents/ipyrad/tests/analysis-bpp/copy-d10-r0.mcmc.txt"
## use 'slice' operations to subsample every 10th tree from first 5000
mtrees = toytree.multitree(mcmcfile, treeslice=(0, 5000, 10))
## plot a cloud tree
mtrees.draw_cloudtree(
width=350,
height=300,
use_edge_lengths=True,
orient='right',
edge_style={
"opacity": 0.025,
"stroke": mtrees.colors[0]},
tip_labels_style={
"-toyplot-anchor-shift": "10px"},
);