Toytree: a minimalist Python tree plotting library

What is Toytree?

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.

Installation

Toyplot can be easily installed using the conda software package manager with the command below.

In [1]:
## conda install toytree -c eaton-lab

To get started let's import a few modules

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.

In [2]:
import toytree
import toyplot
import numpy

Loading/parsing trees

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.

In [3]:
## 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)
In [4]:
## A toytree object in memory
tre
Out[4]:
<toytree.tree.Toytree at 0x7f0869e54050>

Simple tree plotting

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.

In [5]:
tre.draw(width=200);
appleorangetomatillopeppertomatoeggplant

Interactive features

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.

In [6]:
tre.draw(width=200, node_labels=True);
appleorangetomatillopeppertomatoeggplantidx: 0 name: 0 dist: 0 support: 1idx: 1 name: 1 dist: 2 support: 100idx: 2 name: 2 dist: 1 support: 100idx: 3 name: 3 dist: 1 support: 90idx: 4 name: 4 dist: 2 support: 100idx: 5 name: apple dist: 2 support: 1idx: 6 name: orange dist: 4 support: 1idx: 7 name: tomatillo dist: 2 support: 1idx: 8 name: pepper dist: 3 support: 1idx: 9 name: tomato dist: 2 support: 1idx: 10 name: eggplant dist: 1 support: 1

Displaying node features

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.

In [7]:
## 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"}
    );
appleorangetomatillopeppertomatoeggplant10010090100

More on features and parsing trees

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.

In [11]:
## 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
Out[11]:
'(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

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.

In [12]:
## 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"),
    );
clemensiae_DRY6_PWS_2135urceolatum_MJD_Japan_8taiwanianum_TW1_KFC_1952nervosum_C4_PWS_2298lantanoides_D15_Beartown_2sympodiale_D18_KFC_1932furcatum_combinedpunctatum_D19_PWS_2097cassinoides_ELS2lentago_ELS85rufidulum_ELS25prunifolium_ELS57lantana_combinedrhytidophyllumveitchiimacrocephalum_D2_WC_284carlesii_D1_BP_001bitchuense_combinedamplificatum_D3_SAN_156003lutescens_D35_PWS_2077plicatum_C1_MJDJP_12hanceanum_D4_PWS_2195sieboldii_AA_616_6Bodoratissimum_combinedhenryi_D22_WC_272erubescens_RCW36suspensum_C5_MJD_111711farreri_RCW21grandiflorum_ERAD11_Wendyfoetens_ERAD10tinus_D33_WC_277davidii_D32_WC_269propinquum_DRY4_WC_276cinnamomifolium_PWS2105Xrecognitum_AA_1471_83Bdentatum_ELS4sulcatum_D9_MEX_003acutifolium_DRY3_MEX_006triphyllum_D13_PWS_1783jamesonii_D12_PWS_1636opulus_D6_WC_250sargentii_RCW19coriaceum_combinedcylindricum_DRY1_WC_268sambucina_D20_PWS_2100beccarii_combinedXvernicosum_D21_PWS_2123glaberrimum_D34_PWS_2323orientale_DRY2_MJD_GEORGIAacerfolium_ELS88foetidum_D24_KFC_1942betulifoliummuhalla_D28_WC_274integrifolium_D25_KFC_1946japonicum_D26_WC_273wrightii_D31_MJDJP_1parvifolium_D29_KFC_1953dilatatum_ELS45erosum_D23_MJDJP_4tashiori_D30_TET_YAHformosanum_C7_JH_2007luzonicum_D27_9M_2005setigerumsempervirens_combinedanamensis_C6_PWS_2094100100100100100100100100100100831001001001001001001001001001001001001009810010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010010093100100100100

Displaying cloudtrees

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.

In [19]:
## 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"},
    );