From 1e44c26b8d5f90773e27d185644001d7e2b671eb Mon Sep 17 00:00:00 2001 From: natetrux Date: Wed, 14 Aug 2024 19:04:57 -0400 Subject: [PATCH 1/3] cookbooks transfered --- docs/barplot.ipynb | 410 +++++++++++++++ docs/gallery.md | 7 - docs/heatmap.ipynb | 987 +++++++++++++++++++++++++++++++++++ docs/highlights.ipynb | 194 +++++++ docs/ridge-plot.ipynb | 1096 +++++++++++++++++++++++++++++++++++++++ docs/spacing.ipynb | 192 +++++++ mkdocs.yml | 12 +- old_docs/Cookbook.ipynb | 295 ++++++----- 8 files changed, 3040 insertions(+), 153 deletions(-) create mode 100644 docs/barplot.ipynb delete mode 100644 docs/gallery.md create mode 100644 docs/heatmap.ipynb create mode 100644 docs/highlights.ipynb create mode 100644 docs/ridge-plot.ipynb create mode 100644 docs/spacing.ipynb diff --git a/docs/barplot.ipynb b/docs/barplot.ipynb new file mode 100644 index 00000000..c33b38ad --- /dev/null +++ b/docs/barplot.ipynb @@ -0,0 +1,410 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### ToyTree + barplot\n", + "Aligning a tree with data is sometimes easier on one axis versus two. See the (#1) and (#2) for comparison. Here when plotting on one axis the tree coordinates which map to treeheight and the number of tips can be difficult to align with data (e.g., a barplot) since the data values may be much greater than the treeheight. This can be fixed by tranforming the data and the axis labels. The example on a two axes is a bit easier in this case. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r14r15r19r18r16r17r13r5r6r4r2r3r7r8r9r10r11r12050100150200
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import numpy as np\n", + "import toytree\n", + "import toyplot\n", + "\n", + "# generate a random tree and data\n", + "ntips = 20\n", + "rseed = 123456\n", + "rng = np.random.default_rng(rseed)\n", + "\n", + "# get tree and data\n", + "rtre = toytree.rtree.unittree(ntips=ntips, seed=rseed).ladderize()\n", + "randomdata = rng.uniform(20, 200, ntips)\n", + "\n", + "# set up a toyplot Canvas with 2 axes: (x1, x2, y1, y2)\n", + "canvas = toyplot.Canvas(width=375, height=350)\n", + "ax0 = canvas.cartesian(bounds=(50, 200, 50, 300), padding=15, ymin=0, ymax=20)\n", + "ax1 = canvas.cartesian(bounds=(225, 325, 50, 300), padding=15, ymin=0, ymax=20)\n", + "\n", + "# add tree to first axes\n", + "rtre.draw(axes=ax0);\n", + "ax0.show = False\n", + "\n", + "# plot the barplot on the second axes\n", + "# (y-axis is range 0-ntips);\n", + "# (x-axis is bar values transformed to be 0-1)\n", + "# baseline is the space between tipnames and bars\n", + "ax1.bars(\n", + " np.arange(ntips),\n", + " randomdata,\n", + " along='y',\n", + ");\n", + "\n", + "# style axes\n", + "ax1.show = True\n", + "ax1.y.show = False\n", + "ax1.x.ticks.show = True" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/gallery.md b/docs/gallery.md deleted file mode 100644 index 30c2f948..00000000 --- a/docs/gallery.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -section: FAQs -icon: material/android-studio ---- - - -# Coming soon... \ No newline at end of file diff --git a/docs/heatmap.ipynb b/docs/heatmap.ipynb new file mode 100644 index 00000000..3b334595 --- /dev/null +++ b/docs/heatmap.ipynb @@ -0,0 +1,987 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Plot tree with matrix/heatmap" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "import toytree\n", + "import numpy as np\n", + "import toyplot\n", + "\n", + "# load tree with variable name lengths\n", + "tree = toytree.tree(\"https://eaton-lab.org/data/Cyathophora.tre\")\n", + "tree = tree.root(13) #13th node corresponds to przwilenskii outgroup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Method 1: \n", + "The simplest method is to plot the tree and markers on shared coordinate axes. To make it easy to space items on the x-axis I set the tree to be 2X the width of the data (matrix), which allows me to use units of x=1 to space items on the x-axis. Then I generate a canvas and axes by drawing a tree, as usual, and here I add the data as square scatterplot markers with different opacities to represent the (randomly generated) data. \n", + "\n", + "The only tricky thing here is that you need to use `tip_labels_style` to offset the x-location of the tre tip labels, and also to extend the x-axis max domain if the names are long to prevent them from getting cut off. " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[2, 5, 7, 7, 7],\n", + " [6, 2, 2, 4, 2],\n", + " [5, 3, 9, 3, 5],\n", + " [8, 6, 5, 9, 6],\n", + " [5, 6, 7, 2, 4],\n", + " [8, 2, 9, 4, 3],\n", + " [4, 2, 2, 3, 2],\n", + " [9, 2, 1, 4, 8],\n", + " [6, 6, 1, 5, 3],\n", + " [3, 5, 2, 8, 2],\n", + " [7, 9, 1, 5, 1],\n", + " [1, 4, 1, 8, 7],\n", + " [9, 8, 7, 6, 4]])" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# generate some random data for this columns\n", + "spdata = np.random.randint(low=1, high=10, size=(tree.ntips, 5))\n", + "spdata" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides26585849637195236622265948729579211211774392434585867256432832174
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# scale tree\n", + "ctree = tree.mod.edges_scale_to_root_height()\n", + "\n", + "# get canvas and axes with tree plot\n", + "canvas, axes, mark = ctree.draw(\n", + " width=600,\n", + " height=300,\n", + " tip_labels_align=True,\n", + " tip_labels_style={\"-toyplot-anchor-shift\": \"80px\"}\n", + " \n", + ");\n", + "\n", + "# add n columns of data (here random data)\n", + "ncols = 5\n", + "xoffset = 1\n", + "for col in range(5):\n", + " \n", + " # select the column of data\n", + " data = spdata[:, col]\n", + " \n", + " # plot the data column\n", + " axes.scatterplot(\n", + " np.repeat(col, tree.ntips) + xoffset, \n", + " np.arange(tree.ntips),\n", + " marker='s',\n", + " size=10,\n", + " color=\"magenta\", \n", + " opacity=0.1 + data[::-1] / data.max(),\n", + " title=data,\n", + " );\n", + "\n", + "# stretch domain to fit long tip names\n", + "axes.x.domain.max = 40" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Method 2:\n", + "\n", + "Using both a *matrix* and *cartesian* axes in *toyplot*. The key to aligning the two is that matrices have a margin of 50px by default. There aren't as many options to style matrix cells as there are in the option above. Here I used the right-side matrix labels to add and align tip names. " + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(13, 5)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# a random rectangular matrix\n", + "matrix = np.arange(tree.ntips * 5).reshape(tree.ntips, 5)\n", + "matrix.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# create a canvas\n", + "canvas = toyplot.Canvas(width=500, height=350);\n", + "\n", + "# add tree \n", + "axes = canvas.cartesian(bounds=(50, 150, 70, 250))\n", + "tree.draw(axes=axes, tip_labels=False, tip_labels_align=True)\n", + "\n", + "# add matrix\n", + "table = canvas.table(\n", + " rows=13,\n", + " columns=5, \n", + " margin=0,\n", + " bounds=(175, 250, 65, 255),\n", + ")\n", + "\n", + "colormap = toyplot.color.brewer.map(\"BlueRed\")\n", + "\n", + "# apply a color to each cell in the table\n", + "for ridx in range(matrix.shape[0]):\n", + " for cidx in range(matrix.shape[1]):\n", + " cell = table.cells.cell[ridx, cidx]\n", + " cell.style = {\n", + " \"fill\": colormap.colors(matrix[ridx, cidx], 0, 100), \n", + " }\n", + "\n", + "# style the gaps between cells\n", + "table.body.gaps.columns[:] = 3\n", + "table.body.gaps.rows[:] = 3 \n", + "\n", + "# hide axes coordinates\n", + "axes.show = False" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
012340.0000001.0000002.0000003.0000004.00000041478_cyathophylloides5.0000006.0000007.0000008.0000009.00000041954_cyathophylloides10.00000011.00000012.00000013.00000014.00000030686_cyathophylla15.00000016.00000017.00000018.00000019.00000029154_superba20.00000021.00000022.00000023.00000024.00000038362_rex25.00000026.00000027.00000028.00000029.00000039618_rex30.00000031.00000032.00000033.00000034.00000035236_rex35.00000036.00000037.00000038.00000039.00000035855_rex40.00000041.00000042.00000043.00000044.00000040578_rex45.00000046.00000047.00000048.00000049.00000030556_thamno50.00000051.00000052.00000053.00000054.00000033413_thamno55.00000056.00000057.00000058.00000059.00000033588_przewalskii60.00000061.00000062.00000063.00000064.00000032082_przewalskiiTraits
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# create a canvas\n", + "canvas = toyplot.Canvas(width=500, height=350);\n", + "\n", + "# add tree \n", + "axes = canvas.cartesian(bounds=(50, 150, 70, 250))\n", + "tree.draw(axes=axes, tip_labels=False, tip_labels_align=True)\n", + "\n", + "# add matrix\n", + "colormap = toyplot.color.brewer.map(\"BlueRed\")\n", + "table = canvas.matrix(\n", + " (matrix, colormap),\n", + " bounds=(120, 300, 25, 295), \n", + " tshow=True,\n", + " tlabel=\"Traits\",\n", + " lshow=False,\n", + " rshow=True,\n", + " margin=0,\n", + " rlocator=toyplot.locator.Explicit(range(tree.ntips), tree.get_tip_labels()[::-1])\n", + ")\n", + "\n", + "# hide axes coordinates\n", + "axes.show = False" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/highlights.ipynb b/docs/highlights.ipynb new file mode 100644 index 00000000..178660d6 --- /dev/null +++ b/docs/highlights.ipynb @@ -0,0 +1,194 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Colored rectangles to highlight clades \n", + "The easiest way to add colored shapes to a plot is with the Toyplot `.rectangle` or `.fill()` functions of cartesian `axes` objects. For this you simply need to know the coordinates of the area that you wish to fill (See [Coordinates](toytree/drawing-coordinates)). The example below draws two rectangles in the coordinate space and then adds a tree on top of these. You could make more complex polygon shapes using the `fill` function (see Toyplot docs). Remember you can use `axes.show=True` to see the axes coordinates if you need a reminder of how to set the x and y coordinates of the rectangles. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19-1.0-0.50.00.501020
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import toytree\n", + "import toyplot\n", + "\n", + "# generate a random tree\n", + "rtre = toytree.rtree.unittree(20, seed=12345)\n", + "\n", + "# make the canvas and axes\n", + "canvas = toyplot.Canvas(width=250, height=400)\n", + "axes = canvas.cartesian()\n", + "axes.show = True\n", + "\n", + "# draw a rectangle (x1, x2, y1, y2)\n", + "axes.rectangle(\n", + " -0.75, 0.35, -0.5, 4.5, \n", + " opacity=0.25,\n", + " color='yellow',\n", + ")\n", + "\n", + "# draw a rectangle (x1, x2, y1, y2)\n", + "axes.rectangle(\n", + " -0.75, 0.35, 4.5, 8.5, \n", + " opacity=0.25,\n", + " color='magenta',\n", + ")\n", + "\n", + "# add tree to the axes \n", + "rtre.draw(axes=axes);" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/ridge-plot.ipynb b/docs/ridge-plot.ipynb new file mode 100644 index 00000000..5af2844d --- /dev/null +++ b/docs/ridge-plot.ipynb @@ -0,0 +1,1096 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Plot histograms associated with tip trait values (ridge plot)\n", + "You can use the `.hist()` or `.fill()` functions of toytree to plot histograms. Here we will generate and plot a distribution of a data in order from top to bottom so that the histograms overlap in a \"ridge plot\" fashion. An analagous function in `ggtree` seems to have merited an entire publication: https://academic.oup.com/mbe/article/35/12/3041/5142656. " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import toytree\n", + "import toyplot\n", + "import numpy as np\n", + "# we'll use scipy.stats to get prob. density func. of normal dist\n", + "import scipy.stats as sc\n", + "\n", + "# generate a random tree with N tips\n", + "ntips = 40\n", + "tre = toytree.rtree.baltree(ntips)\n", + "\n", + "# generate a distribution between -10 and 10 for each tip in the tree\n", + "points = np.linspace(-10, 10, 50)\n", + "dists = {}\n", + "for tip in tre.get_tip_labels():\n", + " dists[tip] = sc.norm.pdf(points, loc=np.random.randint(-5, 5, 1), scale=2)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r39r38r37r36r35r34r33r32r31r30r29r28r27r26r25r24r23r22r21r20r19r18r17r16r15r14r13r12r11r10r9r8r7r6r5r4r3r2r1r0-10-50510Trait value
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# set up canvas for two panel plot\n", + "canvas = toyplot.Canvas(width=300, height=400)\n", + "\n", + "# add tree to canvas\n", + "ax0 = canvas.cartesian(bounds=(50, 180, 50, 350), ymin=0, ymax=ntips, padding=15)\n", + "tre.draw(axes=ax0, tip_labels=False)\n", + "ax0.show = False\n", + "\n", + "# add histograms to canvas\n", + "ax1 = canvas.cartesian(bounds=(200, 275, 50, 350), ymin=0, ymax=ntips, padding=15)\n", + "\n", + "# iterate from top to bottom (ntips to 0)\n", + "for tip in range(tre.ntips)[::-1]:\n", + " \n", + " # select a color for hist\n", + " color = \"skyblue\"\n", + " \n", + " # get tip name and get hist from dict\n", + " tipname = tre.get_tip_labels()[tip]\n", + " probs = dists[tipname]\n", + " \n", + " # fill histogram with slightly overlapping histograms\n", + " ax1.fill(\n", + " points, probs / probs.max() * 1.25,\n", + " baseline=[tip] * len(points), \n", + " style={\"fill\": color, \"stroke\": \"white\", \"stroke-width\": 0.5},\n", + " title=tipname,\n", + " )\n", + " \n", + " # add horizontal line at base\n", + " ax1.hlines(tip, opacity=0.5, color=\"grey\", style={\"stroke-width\": 0.5})\n", + "\n", + "# hide y axis, show x \n", + "ax1.y.show = False\n", + "ax1.x.label.text = \"Trait value\"\n", + "ax1.x.ticks.show = True" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/spacing.ipynb b/docs/spacing.ipynb new file mode 100644 index 00000000..1942a0e2 --- /dev/null +++ b/docs/spacing.ipynb @@ -0,0 +1,192 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Spacing tree vs. tip names\n", + "\n", + "The ratio of tree to tipnames on a plot is automatically adjusted to try to fit the tip names depending on their font and size, but only to an extent before the tipnames are eventually cutoff. If you want to manually adjust this ratio by squeezing the tree to take up less space this can be done by using the `shrink` parameter, as demonstrated below.\n", + "\n", + "In the plot below I show the x-axis tick marks to highlight where the data are located on the x, and where the domain is by default and when extended. You can see that in both cases the treeheight is between 0 and -1 on the x-axis. But in the latter we extend the max domain from 1.5 to 4 which better accomodates the really long tipnames. Of course you can also increase the `width` of the entire canvas as well to increase spacing." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
ddcdcccaccacdcbacbaabbbbcdcdacbbbdccbdabcdddbcddcacaabcddcbaaccbdcdbaababbcaacaaaacaadcabcababdbdcdadacbacaddddabcdcbcbbcabddbbccdabcbdcbabacdbacccdadcbdaaacdccbdabbcddbbbdbccdcabcaadbadcbddcabdbbcaddbacccccdcd-1.0-0.50.00.51.01.5ddcdcccaccacdcbacbaabbbbcdcdacbbbdccbdabcdddbcddcacaabcddcbaaccbdcdbaababbcaacaaaacaadcabcababdbdcdadacbacaddddabcdcbcbbcabddbbccdabcbdcbabacdbacccdadcbdaaacdccbdabbcddbbbdbccdcabcaadbadcbddcabdbbcaddbacccccdcd-101234
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import numpy as np\n", + "import toytree\n", + "import toyplot\n", + "\n", + "# generate a random tree and data\n", + "ntips = 20\n", + "rseed = 123456\n", + "rtre = toytree.rtree.unittree(ntips=ntips, seed=rseed)\n", + "\n", + "# names of different lengths\n", + "names = [\"\".join(np.random.choice(list(\"abcd\"), i + 1)) for i in range(ntips)]\n", + "\n", + "# make a canvas and coords for two plots\n", + "canvas = toyplot.Canvas(width=600, height=350)\n", + "ax0 = canvas.cartesian(grid=(1, 2, 0), yshow=False)\n", + "ax1 = canvas.cartesian(grid=(1, 2, 1), yshow=False)\n", + "\n", + "# plot the tree with its default spacing for tree and names\n", + "rtre.draw(tip_labels=names, axes=ax0);\n", + "\n", + "# plot the tree on the second axis\n", + "rtre.draw(tip_labels=names, axes=ax1, shrink=500);" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/mkdocs.yml b/mkdocs.yml index 593599f1..87d80b74 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -88,7 +88,7 @@ nav: # - Tree drawing: - Tree Drawing: - draw basics: drawing-basics.ipynb - - draw options: ... + - draw options: draw-options.ipynb - coordinates and axes: drawing-coordinates.ipynb - composite plots: ... - tree styles: tree-styles.ipynb @@ -144,10 +144,12 @@ nav: - FAQs: FAQs.md - # Use this for the gallery: https://smarie.github.io/mkdocs-gallery/ - - Cookbooks: gallery.md - # - barplot - # - heatmap + - Cookbooks: + - ToyTree with barplot: barplot.ipynb + - Spacing tree vs. tip names: spacing.ipynb + - Colored rectangles to highlight clades: highlights.ipynb + - Ridge plot: ridge-plot.ipynb + - ToyTree with matrix/heatmap: heatmap.ipynb # - co-phylogeny # - trait tree # - unrooted trait tree in 2D (PCA) diff --git a/old_docs/Cookbook.ipynb b/old_docs/Cookbook.ipynb index 5a25e233..696b9d9d 100644 --- a/old_docs/Cookbook.ipynb +++ b/old_docs/Cookbook.ipynb @@ -35,7 +35,7 @@ { "data": { "text/html": [ - "
r12r13r14r15r16r9r10r11r8r17r18r6r7r5r19r3r4r2r0r1050100150200
" ] }, @@ -413,16 +413,16 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
cacacbccbbaacdbdcabcbddbbaddddabdacdddcddcbbaabbadcdddcdddbcdcbbdcdadbdcdabbbbbbdccacbdbdaaacbdddadabaddbccbcaaaddabccbcddbbadccacabdbbdabbcaccacddcaaaccbabadaaaccabaabbaacbcbdbcbaacddbcbddcbdccbbcbcaccabdbdcbd-101cacacbccbbaacdbdcabcbddbbaddddabdacdddcddcbbaabbadcdddcdddbcdcbbdcdadbdcdabbbbbbdccacbdbdaaacbdddadabaddbccbcaaaddabccbcddbbadccacabdbbdabbcaccacddcaaaccbabadaaaccabaabbaacbcbdbcbaacddbcbddcbdccbbcbcaccabdbdcbd-1012
" ] }, @@ -573,7 +573,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -582,9 +582,21 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'ToyTree' object has no attribute 'set_node_values'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[5], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m rtre \u001b[38;5;241m=\u001b[39m toytree\u001b[38;5;241m.\u001b[39mrtree\u001b[38;5;241m.\u001b[39munittree(\u001b[38;5;241m10\u001b[39m, seed\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m123\u001b[39m)\n\u001b[1;32m----> 2\u001b[0m rtre \u001b[38;5;241m=\u001b[39m rtre\u001b[38;5;241m.\u001b[39mset_node_values(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmorph\u001b[39m\u001b[38;5;124m\"\u001b[39m, {\u001b[38;5;241m12\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mapple\u001b[39m\u001b[38;5;124m\"\u001b[39m}, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[1;31mAttributeError\u001b[0m: 'ToyTree' object has no attribute 'set_node_values'" + ] + } + ], "source": [ "rtre = toytree.rtree.unittree(10, seed=123)\n", "rtre = rtre.set_node_values(\"morph\", {12: \"apple\"}, \"\")" @@ -592,7 +604,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -601,7 +613,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -610,7 +622,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -779,7 +791,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -800,7 +812,7 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -809,7 +821,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "metadata": { "scrolled": true }, @@ -832,7 +844,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -862,10 +874,8 @@ }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "scrolled": false - }, + "execution_count": null, + "metadata": {}, "outputs": [ { "ename": "TypeError", @@ -919,7 +929,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -931,7 +941,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1162,7 +1172,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "metadata": { "scrolled": true }, @@ -1191,7 +1201,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1223,7 +1233,7 @@ }, { "cell_type": "code", - "execution_count": 130, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -1233,7 +1243,7 @@ }, { "cell_type": "code", - "execution_count": 134, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1315,7 +1325,7 @@ }, { "cell_type": "code", - "execution_count": 132, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1347,7 +1357,7 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1370,7 +1380,7 @@ }, { "cell_type": "code", - "execution_count": 127, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1397,7 +1407,7 @@ }, { "cell_type": "code", - "execution_count": 111, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1417,7 +1427,7 @@ }, { "cell_type": "code", - "execution_count": 96, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1437,7 +1447,7 @@ }, { "cell_type": "code", - "execution_count": 89, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1459,7 +1469,7 @@ }, { "cell_type": "code", - "execution_count": 65, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -1480,7 +1490,7 @@ }, { "cell_type": "code", - "execution_count": 64, + "execution_count": null, "metadata": { "scrolled": true }, @@ -1659,16 +1669,16 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19-1.0-0.50.00.501020
" ] }, @@ -1801,14 +1811,14 @@ "axes.rectangle(\n", " -0.75, 0.35, -0.5, 4.5, \n", " opacity=0.25,\n", - " color=toytree.colors[0],\n", + " color='yellow',\n", ")\n", "\n", "# draw a rectangle (x1, x2, y1, y2)\n", "axes.rectangle(\n", " -0.75, 0.35, 4.5, 8.5, \n", " opacity=0.25,\n", - " color=toytree.colors[1],\n", + " color='magenta',\n", ")\n", "\n", "# add tree to the axes \n", @@ -1825,7 +1835,7 @@ }, { "cell_type": "code", - "execution_count": 198, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ @@ -1834,7 +1844,7 @@ "\n", "# generate a random tree with N tips\n", "ntips = 40\n", - "tre = toytree.rtree.baltree(ntips).mod.node_slider(seed=123)\n", + "tre = toytree.rtree.baltree(ntips)\n", "\n", "# generate a distribution between -10 and 10 for each tip in the tree\n", "points = np.linspace(-10, 10, 50)\n", @@ -1845,13 +1855,13 @@ }, { "cell_type": "code", - "execution_count": 199, + "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
r39r38r37r36r35r34r33r32r31r30r29r28r27r26r25r24r23r22r21r20r19r18r17r16r15r14r13r12r11r10r9r8r7r6r5r4r3r2r1r0-10-50510Trait value
" ] }, @@ -2860,7 +2870,7 @@ "for tip in range(tre.ntips)[::-1]:\n", " \n", " # select a color for hist\n", - " color = toytree.colors[int((tip) / 10)]\n", + " color = \"skyblue\"\n", " \n", " # get tip name and get hist from dict\n", " tipname = tre.get_tip_labels()[tip]\n", @@ -2892,13 +2902,13 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# load tree with variable name lengths\n", "tree = toytree.tree(\"https://eaton-lab.org/data/Cyathophora.tre\")\n", - "tree = tree.root(wildcard=\"prz\")" + "tree = tree.root(13) #13th node corresponds to przwilenskii outgroup" ] }, { @@ -2913,28 +2923,28 @@ }, { "cell_type": "code", - "execution_count": 148, + "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "array([[1, 5, 8, 9, 2],\n", - " [5, 4, 6, 1, 7],\n", - " [7, 7, 5, 7, 4],\n", - " [4, 6, 8, 5, 1],\n", - " [1, 1, 4, 9, 6],\n", - " [5, 8, 1, 1, 9],\n", - " [1, 3, 9, 9, 3],\n", - " [5, 1, 6, 3, 8],\n", - " [1, 7, 1, 8, 5],\n", - " [2, 4, 7, 7, 9],\n", - " [8, 3, 2, 6, 7],\n", - " [6, 1, 1, 9, 4],\n", - " [9, 8, 2, 8, 8]])" + "array([[5, 1, 6, 3, 9],\n", + " [6, 2, 1, 8, 3],\n", + " [2, 9, 9, 1, 7],\n", + " [4, 6, 2, 9, 7],\n", + " [6, 9, 8, 6, 9],\n", + " [9, 8, 4, 6, 9],\n", + " [2, 1, 2, 8, 6],\n", + " [2, 7, 2, 5, 5],\n", + " [8, 4, 9, 9, 3],\n", + " [7, 6, 2, 4, 3],\n", + " [2, 4, 3, 6, 3],\n", + " [2, 9, 4, 9, 1],\n", + " [3, 7, 4, 7, 7]])" ] }, - "execution_count": 148, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -2947,13 +2957,13 @@ }, { "cell_type": "code", - "execution_count": 153, + "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii15741515128695476183174318865841961721291759193876982741693859748
" ] }, @@ -3248,15 +3258,16 @@ } ], "source": [ - "# scale tree to be 2X length of number of matrix cols\n", - "ctree = tree.mod.node_scale_root_height(spdata.shape[1] * 2)\n", + "# scale tree\n", + "ctree = tree.mod.edges_scale_to_root_height()\n", "\n", "# get canvas and axes with tree plot\n", "canvas, axes, mark = ctree.draw(\n", - " width=500,\n", + " width=600,\n", " height=300,\n", " tip_labels_align=True,\n", " tip_labels_style={\"-toyplot-anchor-shift\": \"80px\"}\n", + " \n", ");\n", "\n", "# add n columns of data (here random data)\n", @@ -3273,13 +3284,13 @@ " np.arange(tree.ntips),\n", " marker='s',\n", " size=10,\n", - " color=toytree.colors[col], \n", + " color=\"magenta\", \n", " opacity=0.1 + data[::-1] / data.max(),\n", " title=data,\n", " );\n", "\n", "# stretch domain to fit long tip names\n", - "axes.x.domain.max = 20" + "axes.x.domain.max = 40" ] }, { @@ -3293,7 +3304,7 @@ }, { "cell_type": "code", - "execution_count": 154, + "execution_count": 33, "metadata": {}, "outputs": [ { @@ -3302,7 +3313,7 @@ "(13, 5)" ] }, - "execution_count": 154, + "execution_count": 33, "metadata": {}, "output_type": "execute_result" } @@ -3315,13 +3326,13 @@ }, { "cell_type": "code", - "execution_count": 196, + "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
" ] }, @@ -3583,13 +3594,15 @@ }, { "cell_type": "code", - "execution_count": 116, - "metadata": {}, + "execution_count": 35, + "metadata": { + "scrolled": true + }, "outputs": [ { "data": { "text/html": [ - "
012340.0000001.0000002.0000003.0000004.00000032082_przewalskii5.0000006.0000007.0000008.0000009.00000033588_przewalskii10.00000011.00000012.00000013.00000014.00000029154_superba15.00000016.00000017.00000018.00000019.00000030686_cyathophylla20.00000021.00000022.00000023.00000024.00000041954_cyathophylloides25.00000026.00000027.00000028.00000029.00000041478_cyathophylloides30.00000031.00000032.00000033.00000034.00000033413_thamno35.00000036.00000037.00000038.00000039.00000030556_thamno40.00000041.00000042.00000043.00000044.00000040578_rex45.00000046.00000047.00000048.00000049.00000035855_rex50.00000051.00000052.00000053.00000054.00000035236_rex55.00000056.00000057.00000058.00000059.00000039618_rex60.00000061.00000062.00000063.00000064.00000038362_rexTraits
" ] }, @@ -3843,9 +3856,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "toytree_practice", "language": "python", - "name": "python3" + "name": "toytree_practice" }, "language_info": { "codemirror_mode": { @@ -3857,9 +3870,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.8" + "version": "3.12.4" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } From 492c47174dcb40089726fe5ec9ff664e108797cd Mon Sep 17 00:00:00 2001 From: natetrux Date: Thu, 15 Aug 2024 13:02:43 -0400 Subject: [PATCH 2/3] reorganized draw options --- docs/draw-options.ipynb | 1348 +++++++++++++++++++++++++++++++++++++++ docs/tree-styles.ipynb | 1255 ++++++------------------------------ mkdocs.yml | 4 +- 3 files changed, 1541 insertions(+), 1066 deletions(-) create mode 100644 docs/draw-options.ipynb diff --git a/docs/draw-options.ipynb b/docs/draw-options.ipynb new file mode 100644 index 00000000..e2ef42de --- /dev/null +++ b/docs/draw-options.ipynb @@ -0,0 +1,1348 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Styling toytree drawings\n", + "\n", + "The number of styling available in toytree is enormous and will continue to grow as development on the project continues. If you have a specific styling option that does not appear to be supported please raise a issue on GitHub and we can discuss adding support for it. Below I try to demonstrate the options and usage of each styling option with at least one example. " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "import toytree\n", + "import toyplot\n", + "import numpy as np\n", + "\n", + "# a tree to use for examples\n", + "url = \"https://eaton-lab.org/data/Cyathophora.tre\"\n", + "rtre = toytree.tree(url).root(13) #13th node corresponds to przewalskii outgroup" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Tip label styling" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### tip_labels\n", + "\n", + "**Type:** boolean or list \n", + "**Allowed:** True, False, or list (length of ntips) \n", + "**Default:** True \n", + "**Description:** Shows or hides tip labels (boolean), or replaces tip labels with a user defined list. " + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# hide tip labels\n", + "rtre.draw(tip_labels=False);" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# get tip labels from tree\n", + "tipnames = rtre.get_tip_labels()\n", + "\n", + "# modify list so that html italic tags surround text\n", + "italicnames = [\"{}\".format(i) for i in tipnames]\n", + "\n", + "# enter the list of names to tip_labels\n", + "rtre.draw(tip_labels=italicnames);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### tip_labels_align\n", + "\n", + "**Type:** boolean \n", + "**Allowed:** True, False \n", + "**Default:** False \n", + "**Description:** Add lines connecting tips to tip labels. See [edge_align_style](#edge_align_style) for ways to style the extra tip lines (colors, etc.). " + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(tip_labels_align=True);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### tip_labels_colors\n", + "\n", + "**Type:** (str, tuple, list) \n", + "**Allowed:** Any color values supported in [toyplot colors](https://toyplot.readthedocs.io/en/stable/colors.html#color-values). \n", + "**Default:** `\"#262626\"` (near black) \n", + "**Description:** A single value can be applied to color all tip labels, or a list of values can be entered to apply different colors to different tip labels. The order of colors is applied to tip labels in the same order as `.get_tip_labels()` returns the labels. This order (the plot order) is from the tip located on the zero-axis (e.g., x=0 for right-facing trees) and continuing until the last name. If both `tip_labels_colors` and `tip_labels_style[\"fill\"]` are used `tip_labels_colors` overrides the other. In contrast to the fill style, only this option can be used to apply multiple colors. " + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# use color from favored toytree color scheme\n", + "rtre.draw(\n", + " tip_labels_align=True, \n", + " tip_labels_colors=\"orange\",\n", + ");\n", + "\n", + "# enter a list of colors by name\n", + "rtre.draw(\n", + " tip_labels_align=True, \n", + " tip_labels_colors=(['goldenrod'] * 11) + ([\"mediumseagreen\"] * 2),\n", + ");\n", + "\n", + "# make list of hex color values based on tip labels\n", + "colorlist = [\"#d6557c\" if \"rex\" in tip else \"#5384a3\" for tip in rtre.get_tip_labels()]\n", + "rtre.draw(\n", + " tip_labels_align=True, \n", + " tip_labels_colors=colorlist\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### tip_labels_style\n", + "\n", + "**Type:** dictionary of (mostly) CSS styles. \n", + "**Allowed:** CSS styles accepted by toyplot. For example, you could add \"stroke\": \"red\" to draw a stroke around the text. The defaults provide just the most likely styles. \n", + "**Default:** Defaults are shown below. \n", + "**Description:** 'fill' is the color of the text; 'font-size' should be entered in units of pixels; '-toyplot-anchor-shift' can be used to shift text closer or farther from the tree tips.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(\n", + " tip_labels_style={\n", + " \"fill\": \"#262626\", \n", + " \"font-size\": \"11px\", \n", + " \"-toyplot-anchor-shift\": \"15px\", \n", + " }\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Node labels styling" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_labels\n", + "\n", + "**Type:** boolean, str, or list of strings or ints. \n", + "**Allowed:** True, False, string name of features (e.g., \"idx\", \"support\"), or list that is the same length as the number of nodes. \n", + "**Default:** False \n", + "**Description:** If True then the node index labels are used. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
012345678910111213141516171819202122232438362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# shows node idx labels on all nodes\n", + "rtre.draw(node_labels=True);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# suppreses nodes\n", + "rtre.draw(node_labels=False);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# suppresses node labels, sizes ensures nodes are still shown\n", + "rtre.draw(node_labels=False, node_sizes=10);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# shortcut for 'default' features always present in TreeNodes, suppresses tip nodes.\n", + "rtre.draw(node_labels=\"support\");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0000000000000100961009910010010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# build a list of values in the correct node plot order\n", + "sups = rtre.get_node_values(\"support\", show_root=True, show_tips=True)\n", + "rtre.draw(node_labels=sups);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_labels_style\n", + "\n", + "**Type:** dictionary. \n", + "**Allowed:** CSS values supported for text by toyplot. \n", + "**Default:** fill: near-black, font-size: '11px'. \n", + "**Description:** A dictionary of CSS style options applied to text of node labels. \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
131415161718192021222338362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(\n", + " node_labels='idx',\n", + " node_labels_style={\n", + " \"fill\": \"#262626\",\n", + " \"font-size\": \"8px\",\n", + " }\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Node styling" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_sizes" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(\n", + " node_labels=False,\n", + " node_sizes=10,\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# draw random values to use for node sizes\n", + "np.random.seed(1234)\n", + "sizes = np.random.uniform(5, 15, rtre.nnodes)\n", + "rtre.draw(\n", + " node_labels=False,\n", + " node_sizes=sizes,\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_colors" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# set a single color for all nodes\n", + "rtre.draw(\n", + " node_labels=False,\n", + " node_sizes=10, \n", + " node_colors=toytree.colors[1],\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array(['100', '100', '100', '100', '100', '100', '100', '100', '99',\n", + " '100', '96', '100', '', '', '', '', '', '', '', '', '', '', '', '',\n", + " ''], dtype='38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# get list of sizes and colors in node plot order with tip nodes suppressed\n", + "sizes = [10 if i else 0 for i in rtre.get_node_values('support', 1, 0)]\n", + "colors = ['black' if i=='100' else 'red' for i in rtre.get_node_values('support', 1, 0)]\n", + "\n", + "# enter a lists of values\n", + "rtre.draw(\n", + " node_labels=None,\n", + " node_sizes=sizes, \n", + " node_colors=colors,\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_markers\n", + "\n", + "\n", + "See [toyplot markers](https://toyplot.readthedocs.io/en/stable/markers.html) for available options. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# enter a lists of values\n", + "rtre.draw(\n", + " node_labels=\"support\",\n", + " node_markers=\"o\"\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# enter a lists of values\n", + "rtre.draw(\n", + " height=350,\n", + " node_labels=None,\n", + " node_sizes=[0 if i else 8 for i in rtre.get_node_values(None, 1, 0)],\n", + " node_markers=\"s\",\n", + " node_colors=toytree.colors[1],\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Rectangular markers can be drawn in many dimensions. Designate \"r2x1\" for a box that is twice as wide as it is tall. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# rectangles for nodes\n", + "rtre.draw(\n", + " width=600,\n", + " height=400,\n", + " node_labels=\"support\",\n", + " node_labels_style={\"font-size\": \"11px\"},\n", + " node_markers=\"r2x1.25\",\n", + " node_sizes=12,\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_style" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# the classic \"ape\" style tree plot look\n", + "rtre.draw(\n", + " width=600,\n", + " height=400,\n", + " node_labels=\"support\",\n", + " node_labels_style={\"font-size\": \"10px\"},\n", + " node_markers=\"r2x1.25\",\n", + " node_sizes=12,\n", + " node_style={\n", + " \"fill\": \"lightgrey\", \n", + " \"stroke\": \"black\",\n", + " \"stroke-width\": 0.75,\n", + " }\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### node_hover\n", + "\n", + "Enables interactive hover over nodes so that you can see all features associated with each. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
idx: 0\n", + "dist: 0.0011\n", + "support: 0.0000\n", + "height: 0.0000\n", + "name: 38362_rex0idx: 1\n", + "dist: 0.0010\n", + "support: 0.0000\n", + "height: 0.0001\n", + "name: 39618_rex1idx: 2\n", + "dist: 0.0058\n", + "support: 0.0000\n", + "height: 0.0015\n", + "name: 35236_rex2idx: 3\n", + "dist: 0.0034\n", + "support: 0.0000\n", + "height: 0.0024\n", + "name: 35855_rex3idx: 4\n", + "dist: 0.0034\n", + "support: 0.0000\n", + "height: 0.0024\n", + "name: 40578_rex4idx: 5\n", + "dist: 0.0065\n", + "support: 0.0000\n", + "height: 0.0023\n", + "name: 30556_thamno5idx: 6\n", + "dist: 0.0057\n", + "support: 0.0000\n", + "height: 0.0042\n", + "name: 33413_thamno6idx: 7\n", + "dist: 0.0001\n", + "support: 0.0000\n", + "height: 0.0028\n", + "name: 41478_cyathophylloides7idx: 8\n", + "dist: 0.0001\n", + "support: 0.0000\n", + "height: 0.0027\n", + "name: 41954_cyathophylloides8idx: 9\n", + "dist: 0.0067\n", + "support: 0.0000\n", + "height: 0.0032\n", + "name: 30686_cyathophylla9idx: 10\n", + "dist: 0.0063\n", + "support: 0.0000\n", + "height: 0.0035\n", + "name: 29154_superba10idx: 11\n", + "dist: 0.0025\n", + "support: 0.0000\n", + "height: 0.0127\n", + "name: 33588_przewalskii11idx: 12\n", + "dist: 0.0026\n", + "support: 0.0000\n", + "height: 0.0126\n", + "name: 32082_przewalskii12idx: 13\n", + "dist: 0.0062\n", + "support: 100.0000\n", + "height: 0.0011\n", + "name: 1313idx: 14\n", + "dist: 0.0007\n", + "support: 96.0000\n", + "height: 0.0073\n", + "name: 1414idx: 15\n", + "dist: 0.0022\n", + "support: 100.0000\n", + "height: 0.0058\n", + "name: 1515idx: 16\n", + "dist: 0.0008\n", + "support: 99.0000\n", + "height: 0.0080\n", + "name: 1616idx: 17\n", + "dist: 0.0010\n", + "support: 100.0000\n", + "height: 0.0088\n", + "name: 1717idx: 18\n", + "dist: 0.0094\n", + "support: 100.0000\n", + "height: 0.0028\n", + "name: 2318idx: 19\n", + "dist: 0.0024\n", + "support: 100.0000\n", + "height: 0.0099\n", + "name: 2119idx: 20\n", + "dist: 0.0054\n", + "support: 100.0000\n", + "height: 0.0098\n", + "name: 1820idx: 21\n", + "dist: 0.0030\n", + "support: 100.0000\n", + "height: 0.0122\n", + "name: 2221idx: 22\n", + "dist: 0.0179\n", + "support: 100.0000\n", + "height: 0.0152\n", + "name: 2022idx: 23\n", + "dist: 0.0179\n", + "support: 100.0000\n", + "height: 0.0152\n", + "name: 1923idx: 24\n", + "dist: 1.0000\n", + "support: 100.0000\n", + "height: 0.0331\n", + "name: root2438362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(node_hover=True, node_labels=True, node_sizes=15);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Layout" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### layout\n", + "\n", + "**Type:** str \n", + "**Allowed:** 'circular', 'c', 'right', 'r', 'down', 'd', (coming: 'unrooted', 'up', 'left') \n", + "**Default:** 'r' \n", + "**Description:** The layout orients the direction of tip labels. Options include linear trees facing right, left, up, down, as well as circular and unrooted trees. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ttre = toytree.rtree.coaltree(20, seed=123)\n", + "ttre.draw(\n", + " layout='d', \n", + " tip_labels_align=True,\n", + " node_sizes=[8 if i else 0 for i in ttre.get_node_values()],\n", + " node_style={\"stroke\": \"black\"},\n", + " width=350,\n", + " height=300,\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r39
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ttre = toytree.rtree.unittree(40, seed=123)\n", + "ttre.draw(\n", + " layout='c', \n", + " edge_type='c',\n", + " node_sizes=[8 if i else 0 for i in ttre.get_node_values()],\n", + " node_style={\"stroke\": \"black\"},\n", + " width=400,\n", + " height=400,\n", + ");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r39
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "ttre = toytree.rtree.unittree(40, seed=123)\n", + "ttre.draw(\n", + " layout='c', \n", + " edge_type='p',\n", + " width=400,\n", + " height=400,\n", + ");" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### `todo`\n", + "\n", + "**Type:** \n", + "**Allowed:** \n", + "**Default:** \n", + "**Description:** \n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Aligned Edge Styling" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### edge_align_style\n", + "\n", + "**Type:** \n", + "**Allowed:** \n", + "**Default:** \n", + "**Description:** \n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(\n", + " tip_labels_align=True, \n", + " edge_align_style={\n", + " \"stroke\": \"violet\",\n", + " \"stroke-width\": 1.5,\n", + " \"stroke-dasharray\": \"2,5\" # size of dash, spacing of dashes\n", + " });" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Styles" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(tree_style='n');" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0.00.20.4
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# generate a random coalescent tree and draw in 'coalescent' style\n", + "randomtre = toytree.rtree.coaltree(ntips=10, seed=666)\n", + "randomtre.draw(tree_style='c');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Scalebar\n", + "\n", + "You can add a scalebar to any tree plot by simply adding `scalebar=True`. Alternatively, you can add or customize axes by saving the returned variables from the `.draw()` function and modifying the axes. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii0.000.020.03
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "rtre.draw(scalebar=True);" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/tree-styles.ipynb b/docs/tree-styles.ipynb index e2ef42de..e51fc551 100644 --- a/docs/tree-styles.ipynb +++ b/docs/tree-styles.ipynb @@ -4,1039 +4,19 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Styling toytree drawings\n", - "\n", - "The number of styling available in toytree is enormous and will continue to grow as development on the project continues. If you have a specific styling option that does not appear to be supported please raise a issue on GitHub and we can discuss adding support for it. Below I try to demonstrate the options and usage of each styling option with at least one example. " - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [], - "source": [ - "import toytree\n", - "import toyplot\n", - "import numpy as np\n", - "\n", - "# a tree to use for examples\n", - "url = \"https://eaton-lab.org/data/Cyathophora.tre\"\n", - "rtre = toytree.tree(url).root(13) #13th node corresponds to przewalskii outgroup" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Tip label styling" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### tip_labels\n", - "\n", - "**Type:** boolean or list \n", - "**Allowed:** True, False, or list (length of ntips) \n", - "**Default:** True \n", - "**Description:** Shows or hides tip labels (boolean), or replaces tip labels with a user defined list. " - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# hide tip labels\n", - "rtre.draw(tip_labels=False);" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# get tip labels from tree\n", - "tipnames = rtre.get_tip_labels()\n", - "\n", - "# modify list so that html italic tags surround text\n", - "italicnames = [\"{}\".format(i) for i in tipnames]\n", - "\n", - "# enter the list of names to tip_labels\n", - "rtre.draw(tip_labels=italicnames);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### tip_labels_align\n", - "\n", - "**Type:** boolean \n", - "**Allowed:** True, False \n", - "**Default:** False \n", - "**Description:** Add lines connecting tips to tip labels. See [edge_align_style](#edge_align_style) for ways to style the extra tip lines (colors, etc.). " - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(tip_labels_align=True);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### tip_labels_colors\n", - "\n", - "**Type:** (str, tuple, list) \n", - "**Allowed:** Any color values supported in [toyplot colors](https://toyplot.readthedocs.io/en/stable/colors.html#color-values). \n", - "**Default:** `\"#262626\"` (near black) \n", - "**Description:** A single value can be applied to color all tip labels, or a list of values can be entered to apply different colors to different tip labels. The order of colors is applied to tip labels in the same order as `.get_tip_labels()` returns the labels. This order (the plot order) is from the tip located on the zero-axis (e.g., x=0 for right-facing trees) and continuing until the last name. If both `tip_labels_colors` and `tip_labels_style[\"fill\"]` are used `tip_labels_colors` overrides the other. In contrast to the fill style, only this option can be used to apply multiple colors. " - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
32082_przewalskii33588_przewalskii33413_thamno30556_thamno40578_rex35855_rex35236_rex39618_rex38362_rex29154_superba30686_cyathophylla41954_cyathophylloides41478_cyathophylloides
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# use color from favored toytree color scheme\n", - "rtre.draw(\n", - " tip_labels_align=True, \n", - " tip_labels_colors=\"orange\",\n", - ");\n", - "\n", - "# enter a list of colors by name\n", - "rtre.draw(\n", - " tip_labels_align=True, \n", - " tip_labels_colors=(['goldenrod'] * 11) + ([\"mediumseagreen\"] * 2),\n", - ");\n", - "\n", - "# make list of hex color values based on tip labels\n", - "colorlist = [\"#d6557c\" if \"rex\" in tip else \"#5384a3\" for tip in rtre.get_tip_labels()]\n", - "rtre.draw(\n", - " tip_labels_align=True, \n", - " tip_labels_colors=colorlist\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### tip_labels_style\n", - "\n", - "**Type:** dictionary of (mostly) CSS styles. \n", - "**Allowed:** CSS styles accepted by toyplot. For example, you could add \"stroke\": \"red\" to draw a stroke around the text. The defaults provide just the most likely styles. \n", - "**Default:** Defaults are shown below. \n", - "**Description:** 'fill' is the color of the text; 'font-size' should be entered in units of pixels; '-toyplot-anchor-shift' can be used to shift text closer or farther from the tree tips.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(\n", - " tip_labels_style={\n", - " \"fill\": \"#262626\", \n", - " \"font-size\": \"11px\", \n", - " \"-toyplot-anchor-shift\": \"15px\", \n", - " }\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Node labels styling" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_labels\n", - "\n", - "**Type:** boolean, str, or list of strings or ints. \n", - "**Allowed:** True, False, string name of features (e.g., \"idx\", \"support\"), or list that is the same length as the number of nodes. \n", - "**Default:** False \n", - "**Description:** If True then the node index labels are used. \n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
012345678910111213141516171819202122232438362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# shows node idx labels on all nodes\n", - "rtre.draw(node_labels=True);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# suppreses nodes\n", - "rtre.draw(node_labels=False);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# suppresses node labels, sizes ensures nodes are still shown\n", - "rtre.draw(node_labels=False, node_sizes=10);" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# shortcut for 'default' features always present in TreeNodes, suppresses tip nodes.\n", - "rtre.draw(node_labels=\"support\");" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
0000000000000100961009910010010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# build a list of values in the correct node plot order\n", - "sups = rtre.get_node_values(\"support\", show_root=True, show_tips=True)\n", - "rtre.draw(node_labels=sups);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_labels_style\n", - "\n", - "**Type:** dictionary. \n", - "**Allowed:** CSS values supported for text by toyplot. \n", - "**Default:** fill: near-black, font-size: '11px'. \n", - "**Description:** A dictionary of CSS style options applied to text of node labels. \n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
131415161718192021222338362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(\n", - " node_labels='idx',\n", - " node_labels_style={\n", - " \"fill\": \"#262626\",\n", - " \"font-size\": \"8px\",\n", - " }\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Node styling" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_sizes" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(\n", - " node_labels=False,\n", - " node_sizes=10,\n", - ");" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# draw random values to use for node sizes\n", - "np.random.seed(1234)\n", - "sizes = np.random.uniform(5, 15, rtre.nnodes)\n", - "rtre.draw(\n", - " node_labels=False,\n", - " node_sizes=sizes,\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_colors" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# set a single color for all nodes\n", - "rtre.draw(\n", - " node_labels=False,\n", - " node_sizes=10, \n", - " node_colors=toytree.colors[1],\n", - ");" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array(['100', '100', '100', '100', '100', '100', '100', '100', '99',\n", - " '100', '96', '100', '', '', '', '', '', '', '', '', '', '', '', '',\n", - " ''], dtype='38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# get list of sizes and colors in node plot order with tip nodes suppressed\n", - "sizes = [10 if i else 0 for i in rtre.get_node_values('support', 1, 0)]\n", - "colors = ['black' if i=='100' else 'red' for i in rtre.get_node_values('support', 1, 0)]\n", - "\n", - "# enter a lists of values\n", - "rtre.draw(\n", - " node_labels=None,\n", - " node_sizes=sizes, \n", - " node_colors=colors,\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_markers\n", - "\n", - "\n", - "See [toyplot markers](https://toyplot.readthedocs.io/en/stable/markers.html) for available options. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# enter a lists of values\n", - "rtre.draw(\n", - " node_labels=\"support\",\n", - " node_markers=\"o\"\n", - ");" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# enter a lists of values\n", - "rtre.draw(\n", - " height=350,\n", - " node_labels=None,\n", - " node_sizes=[0 if i else 8 for i in rtre.get_node_values(None, 1, 0)],\n", - " node_markers=\"s\",\n", - " node_colors=toytree.colors[1],\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Rectangular markers can be drawn in many dimensions. Designate \"r2x1\" for a box that is twice as wide as it is tall. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# rectangles for nodes\n", - "rtre.draw(\n", - " width=600,\n", - " height=400,\n", - " node_labels=\"support\",\n", - " node_labels_style={\"font-size\": \"11px\"},\n", - " node_markers=\"r2x1.25\",\n", - " node_sizes=12,\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_style" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
100961009910010010010010010010038362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "# the classic \"ape\" style tree plot look\n", - "rtre.draw(\n", - " width=600,\n", - " height=400,\n", - " node_labels=\"support\",\n", - " node_labels_style={\"font-size\": \"10px\"},\n", - " node_markers=\"r2x1.25\",\n", - " node_sizes=12,\n", - " node_style={\n", - " \"fill\": \"lightgrey\", \n", - " \"stroke\": \"black\",\n", - " \"stroke-width\": 0.75,\n", - " }\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### node_hover\n", - "\n", - "Enables interactive hover over nodes so that you can see all features associated with each. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
idx: 0\n", - "dist: 0.0011\n", - "support: 0.0000\n", - "height: 0.0000\n", - "name: 38362_rex0idx: 1\n", - "dist: 0.0010\n", - "support: 0.0000\n", - "height: 0.0001\n", - "name: 39618_rex1idx: 2\n", - "dist: 0.0058\n", - "support: 0.0000\n", - "height: 0.0015\n", - "name: 35236_rex2idx: 3\n", - "dist: 0.0034\n", - "support: 0.0000\n", - "height: 0.0024\n", - "name: 35855_rex3idx: 4\n", - "dist: 0.0034\n", - "support: 0.0000\n", - "height: 0.0024\n", - "name: 40578_rex4idx: 5\n", - "dist: 0.0065\n", - "support: 0.0000\n", - "height: 0.0023\n", - "name: 30556_thamno5idx: 6\n", - "dist: 0.0057\n", - "support: 0.0000\n", - "height: 0.0042\n", - "name: 33413_thamno6idx: 7\n", - "dist: 0.0001\n", - "support: 0.0000\n", - "height: 0.0028\n", - "name: 41478_cyathophylloides7idx: 8\n", - "dist: 0.0001\n", - "support: 0.0000\n", - "height: 0.0027\n", - "name: 41954_cyathophylloides8idx: 9\n", - "dist: 0.0067\n", - "support: 0.0000\n", - "height: 0.0032\n", - "name: 30686_cyathophylla9idx: 10\n", - "dist: 0.0063\n", - "support: 0.0000\n", - "height: 0.0035\n", - "name: 29154_superba10idx: 11\n", - "dist: 0.0025\n", - "support: 0.0000\n", - "height: 0.0127\n", - "name: 33588_przewalskii11idx: 12\n", - "dist: 0.0026\n", - "support: 0.0000\n", - "height: 0.0126\n", - "name: 32082_przewalskii12idx: 13\n", - "dist: 0.0062\n", - "support: 100.0000\n", - "height: 0.0011\n", - "name: 1313idx: 14\n", - "dist: 0.0007\n", - "support: 96.0000\n", - "height: 0.0073\n", - "name: 1414idx: 15\n", - "dist: 0.0022\n", - "support: 100.0000\n", - "height: 0.0058\n", - "name: 1515idx: 16\n", - "dist: 0.0008\n", - "support: 99.0000\n", - "height: 0.0080\n", - "name: 1616idx: 17\n", - "dist: 0.0010\n", - "support: 100.0000\n", - "height: 0.0088\n", - "name: 1717idx: 18\n", - "dist: 0.0094\n", - "support: 100.0000\n", - "height: 0.0028\n", - "name: 2318idx: 19\n", - "dist: 0.0024\n", - "support: 100.0000\n", - "height: 0.0099\n", - "name: 2119idx: 20\n", - "dist: 0.0054\n", - "support: 100.0000\n", - "height: 0.0098\n", - "name: 1820idx: 21\n", - "dist: 0.0030\n", - "support: 100.0000\n", - "height: 0.0122\n", - "name: 2221idx: 22\n", - "dist: 0.0179\n", - "support: 100.0000\n", - "height: 0.0152\n", - "name: 2022idx: 23\n", - "dist: 0.0179\n", - "support: 100.0000\n", - "height: 0.0152\n", - "name: 1923idx: 24\n", - "dist: 1.0000\n", - "support: 100.0000\n", - "height: 0.0331\n", - "name: root2438362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(node_hover=True, node_labels=True, node_sizes=15);" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Layout" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### layout\n", - "\n", - "**Type:** str \n", - "**Allowed:** 'circular', 'c', 'right', 'r', 'down', 'd', (coming: 'unrooted', 'up', 'left') \n", - "**Default:** 'r' \n", - "**Description:** The layout orients the direction of tip labels. Options include linear trees facing right, left, up, down, as well as circular and unrooted trees. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "ttre = toytree.rtree.coaltree(20, seed=123)\n", - "ttre.draw(\n", - " layout='d', \n", - " tip_labels_align=True,\n", - " node_sizes=[8 if i else 0 for i in ttre.get_node_values()],\n", - " node_style={\"stroke\": \"black\"},\n", - " width=350,\n", - " height=300,\n", - ");" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r39
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "ttre = toytree.rtree.unittree(40, seed=123)\n", - "ttre.draw(\n", - " layout='c', \n", - " edge_type='c',\n", - " node_sizes=[8 if i else 0 for i in ttre.get_node_values()],\n", - " node_style={\"stroke\": \"black\"},\n", - " width=400,\n", - " height=400,\n", - ");" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r39
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "ttre = toytree.rtree.unittree(40, seed=123)\n", - "ttre.draw(\n", - " layout='c', \n", - " edge_type='p',\n", - " width=400,\n", - " height=400,\n", - ");" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### `todo`\n", - "\n", - "**Type:** \n", - "**Allowed:** \n", - "**Default:** \n", - "**Description:** \n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Aligned Edge Styling" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### edge_align_style\n", - "\n", - "**Type:** \n", - "**Allowed:** \n", - "**Default:** \n", - "**Description:** \n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(\n", - " tip_labels_align=True, \n", - " edge_align_style={\n", - " \"stroke\": \"violet\",\n", - " \"stroke-width\": 1.5,\n", - " \"stroke-dasharray\": \"2,5\" # size of dash, spacing of dashes\n", - " });" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Styles" + "## Builtin Tree Styles\n", + "There are innumerous ways to style ToyTree drawings by combining different arguments to the `.draw` function. As a convenience, we also provide a number of pre-built tree styles that represent collections of style arguments that can be set using a single command." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii
" @@ -1044,24 +24,14 @@ }, "metadata": {}, "output_type": "display_data" - } - ], - "source": [ - "rtre.draw(tree_style='n');" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ + }, { "data": { "text/html": [ - "
0.00.20.4
" ] }, "metadata": {}, "output_type": "display_data" - } - ], - "source": [ - "# generate a random coalescent tree and draw in 'coalescent' style\n", - "randomtre = toytree.rtree.coaltree(ntips=10, seed=666)\n", - "randomtre.draw(tree_style='c');" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Scalebar\n", - "\n", - "You can add a scalebar to any tree plot by simply adding `scalebar=True`. Alternatively, you can add or customize axes by saving the returned variables from the `.draw()` function and modifying the axes. " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [ + }, + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, { "data": { "text/html": [ - "
38362_rex39618_rex35236_rex35855_rex40578_rex30556_thamno33413_thamno41478_cyathophylloides41954_cyathophylloides30686_cyathophylla29154_superba33588_przewalskii32082_przewalskii0.000.020.03
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import toytree\n", + "tree = toytree.rtree.baltree(ntips=10)\n", + "# drawing with pre-built tree_styles\n", + "tree.draw(tree_style='s'); # simple-style\n", + "tree.draw(tree_style='c'); # coalescent-style\n", + "tree.draw(tree_style='r'); # R-style\n", + "tree.draw(tree_style='p'); # population-style\n", + "tree.draw(tree_style='d'); # dark-style\n", + "tree.draw(tree_style='o'); # umlaut-style" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can think of setting a `tree_style` as changing the baseline style on top of which additional drawing arguments can still be added. For example, here we select the \"dark\" tree style, which changes the default edge and tip colors, and on top of this we can still specify a change to the layout, node marker type, node size, and/or other drawing options." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9
" ] }, @@ -1320,7 +342,112 @@ } ], "source": [ - "rtre.draw(scalebar=True);" + "# tree_style is a baseline on top of which additional styles can be added\n", + "tree.draw(tree_style='d', layout='d', node_markers=\"s\", node_sizes=8, height=250);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In order to see the base settings for a particular builtin tree style, you can call an empty object as shown below. " + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{\n", + "tree_style: 'c',\n", + "height: None,\n", + "width: None,\n", + "layout: 'd',\n", + "edge_type: 'c',\n", + "edge_colors: None,\n", + "edge_widths: None,\n", + "node_mask: False,\n", + "node_colors: None,\n", + "node_sizes: 7,\n", + "node_markers: 'o',\n", + "node_hover: None,\n", + "node_labels: False,\n", + "node_as_edge_data: False,\n", + "tip_labels: True,\n", + "tip_labels_colors: None,\n", + "tip_labels_angles: None,\n", + "tip_labels_align: None,\n", + "edge_style: {\n", + " stroke: (0.145, 0.145, 0.145, 1.0),\n", + " stroke_width: 2,\n", + " stroke_opacity: None,\n", + " stroke_linecap: 'round',\n", + " stroke_dasharray: None,\n", + "},\n", + "node_style: {\n", + " fill: (0.4, 0.7607843137254902, 0.6470588235294118, 1.0),\n", + " fill_opacity: None,\n", + " stroke: (0.145, 0.145, 0.145, 1.0),\n", + " stroke_width: 1.5,\n", + " stroke_opacity: None,\n", + "},\n", + "node_labels_style: {\n", + " fill: (0.145, 0.145, 0.145, 1.0),\n", + " fill_opacity: 1.0,\n", + " font_size: 9,\n", + " font_weight: 300,\n", + " font_family: 'Helvetica',\n", + " anchor_shift: 0,\n", + " baseline_shift: 0,\n", + " text_anchor: 'middle',\n", + "},\n", + "tip_labels_style: {\n", + " fill: (0.145, 0.145, 0.145, 1.0),\n", + " fill_opacity: None,\n", + " font_size: 12,\n", + " font_weight: 300,\n", + " font_family: 'Helvetica',\n", + " anchor_shift: 12,\n", + " baseline_shift: 0,\n", + " text_anchor: 'start',\n", + "},\n", + "edge_align_style: {\n", + " stroke: (0.66, 0.66, 0.66, 1),\n", + " stroke_width: 2,\n", + " stroke_opacity: 0.75,\n", + " stroke_linecap: 'round',\n", + " stroke_dasharray: '2,4',\n", + "},\n", + "use_edge_lengths: True,\n", + "scale_bar: True,\n", + "padding: 15.0,\n", + "xbaseline: 0.0,\n", + "ybaseline: 0.0,\n", + "shrink: 0.0,\n", + "admixture_edges: None,\n", + "}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# S: simple-style\n", + "# C: coalescent-style\n", + "# R: R-style\n", + "# P: population-style\n", + "# D: dark-style\n", + "# O: umlaut-style\n", + "\n", + "#retrieving Tree Style C (coalescent)\n", + "from toytree.style.src.style_types import TreeStyleC\n", + "\n", + "TreeStyleC()" ] } ], diff --git a/mkdocs.yml b/mkdocs.yml index 87d80b74..7c4d5621 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -90,13 +90,13 @@ nav: - draw basics: drawing-basics.ipynb - draw options: draw-options.ipynb - coordinates and axes: drawing-coordinates.ipynb - - composite plots: ... - - tree styles: tree-styles.ipynb + - builtin tree styles: tree-styles.ipynb - save/export canvas: rendering.ipynb - feature range-mapping: range-mapping.ipynb - feature color-mapping: color-mapping.ipynb - multitree tree grids: drawing-tree-grids.ipynb - multitree cloud trees: drawing-cloud-trees.ipynb + # - composite plots # - Modifying axes style: ... - Consensus Trees: ... From 873187780e7470cc5457a93b7987ba2aebfb211f Mon Sep 17 00:00:00 2001 From: natetrux Date: Fri, 16 Aug 2024 12:53:51 -0400 Subject: [PATCH 3/3] rtree docs, added error for odd baltree --- docs/random-trees.ipynb | 1175 +++++++++++++++++++++++++++++++++++ mkdocs.yml | 2 +- toytree/rtree/_src/rtree.py | 4 + 3 files changed, 1180 insertions(+), 1 deletion(-) create mode 100644 docs/random-trees.ipynb diff --git a/docs/random-trees.ipynb b/docs/random-trees.ipynb new file mode 100644 index 00000000..413f2c83 --- /dev/null +++ b/docs/random-trees.ipynb @@ -0,0 +1,1175 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Random Tree Generators \n", + "\n", + "\n", + "The ._rtree_ subpackage allows the user to quickly generate random topologies in a variety of methods. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simple rtree \n", + "\n", + "The function `rtree.rtree()` is the fastest method provided to generate a random topology, and works by iteratively bifurcating a random tip (starting from a single root) until there are the desired number of tips provided by the `ntips=` argument. " + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0123456789101112131415161718r0r1r2r3r4r5r6r7r8r9
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import toytree\n", + "\n", + "random_tree = toytree.rtree.rtree(ntips=10)\n", + "random_tree.draw('s');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note: by default, the tips are ordered by name value as seen above. This can be changed using the setting `random_names=True`" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0123456789101112131415161718r0r3r5r4r6r2r9r7r1r8
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "toytree.rtree.rtree(ntips=10, random_names=True).draw('s');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Additionally, reproduceable results can be attained using an rng seed as shown below." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
0123456789101112131415161718r0r1r2r3r4r5r6r7r8r9
" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/html": [ + "
0123456789101112131415161718r0r1r2r3r4r5r6r7r8r9
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "toytree.rtree.rtree(ntips=10, seed=123).draw('s');\n", + "toytree.rtree.rtree(ntips=10, seed=123).draw('s');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit rtree \n", + "\n", + "This variation, `rtree.unittree()`, generates an ultrametric tree where all internal edges are of equal distance. External edges are extended to make the tree ultrametric. The total tree height can be scaled to any arbitrary height, and other Nodes are scaled proportionately." + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
idx: 0\n", + "dist: 0.6\n", + "support: nan\n", + "height: 0\n", + "name: r00idx: 1\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r11idx: 2\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r22idx: 3\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r33idx: 4\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r44idx: 5\n", + "dist: 0.8\n", + "support: nan\n", + "height: 0\n", + "name: r55idx: 6\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r66idx: 7\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r77idx: 8\n", + "dist: 0.6\n", + "support: nan\n", + "height: 0\n", + "name: r88idx: 9\n", + "dist: 0.8\n", + "support: nan\n", + "height: 0\n", + "name: r99idx: 10\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.2\n", + "name: 10idx: 11\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.2\n", + "name: 11idx: 12\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 12idx: 13\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.6\n", + "name: 13idx: 14\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.8\n", + "name: 14idx: 15\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 15idx: 16\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.6\n", + "name: 16idx: 17\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.8\n", + "name: 17idx: 18\n", + "dist: 0\n", + "support: nan\n", + "height: 1\n", + "name: 18r0r1r2r3r4r5r6r7r8r9543210
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "unit_tree = toytree.rtree.unittree(10)\n", + "unit_tree.draw(scale_bar=True, ts='s', node_hover=True);\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Imbalanced rtree \n", + "\n", + "One extreme possibility for a topography is a fully imbalanced tree, generated using `rtree.imbtree()`, in which each internal node has one child bifurcating and one child that is a tip. This creates a ladder-like shape as seen below." + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
idx: 0\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0\n", + "name: r00idx: 1\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0\n", + "name: r11idx: 2\n", + "dist: 0.222222222222\n", + "support: nan\n", + "height: 0\n", + "name: r22idx: 3\n", + "dist: 0.333333333333\n", + "support: nan\n", + "height: 0\n", + "name: r33idx: 4\n", + "dist: 0.444444444444\n", + "support: nan\n", + "height: 0\n", + "name: r44idx: 5\n", + "dist: 0.555555555556\n", + "support: nan\n", + "height: 0\n", + "name: r55idx: 6\n", + "dist: 0.666666666667\n", + "support: nan\n", + "height: 0\n", + "name: r66idx: 7\n", + "dist: 0.777777777778\n", + "support: nan\n", + "height: 0\n", + "name: r77idx: 8\n", + "dist: 0.888888888889\n", + "support: nan\n", + "height: 0\n", + "name: r88idx: 9\n", + "dist: 1\n", + "support: nan\n", + "height: 0\n", + "name: r99idx: 10\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.111111111111\n", + "name: 10idx: 11\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.222222222222\n", + "name: 11idx: 12\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.333333333333\n", + "name: 12idx: 13\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.444444444444\n", + "name: 13idx: 14\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.555555555556\n", + "name: 14idx: 15\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.666666666667\n", + "name: 15idx: 16\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.777777777778\n", + "name: 16idx: 17\n", + "dist: 0.111111111111\n", + "support: nan\n", + "height: 0.888888888889\n", + "name: 17idx: 18\n", + "dist: 0\n", + "support: nan\n", + "height: 1\n", + "name: 18r0r1r2r3r4r5r6r7r8r99630
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "unit_tree = toytree.rtree.imbtree(10)\n", + "unit_tree.draw(scale_bar=True, ts='s', node_hover=True);\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Balanced rtree \n", + "\n", + "Conversely, the other extreme topographical possibility is that of a completely balanced tree. `rtree.baltree()` generates a balanced tree, so the only real random part of this generator is the tip names if `random_names = True`. The number of tips must be even to generate a fully balanced tree. " + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
idx: 0\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r130idx: 1\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r31idx: 2\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r72idx: 3\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r93idx: 4\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r194idx: 5\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r115idx: 6\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r66idx: 7\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r187idx: 8\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r28idx: 9\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r179idx: 10\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r810idx: 11\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r511idx: 12\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r112idx: 13\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r1513idx: 14\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r1614idx: 15\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r1215idx: 16\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r1016idx: 17\n", + "dist: 0.4\n", + "support: nan\n", + "height: 0\n", + "name: r1417idx: 18\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r018idx: 19\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0\n", + "name: r419idx: 20\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 20idx: 21\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.2\n", + "name: 21idx: 22\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 22idx: 23\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.6\n", + "name: 23idx: 24\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 24idx: 25\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.2\n", + "name: 25idx: 26\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 26idx: 27\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.6\n", + "name: 27idx: 28\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.8\n", + "name: 28idx: 29\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 29idx: 30\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.2\n", + "name: 30idx: 31\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 31idx: 32\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.6\n", + "name: 32idx: 33\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 33idx: 34\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.2\n", + "name: 34idx: 35\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.4\n", + "name: 35idx: 36\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.6\n", + "name: 36idx: 37\n", + "dist: 0.2\n", + "support: nan\n", + "height: 0.8\n", + "name: 37idx: 38\n", + "dist: 0\n", + "support: nan\n", + "height: 1\n", + "name: 38r13r3r7r9r19r11r6r18r2r17r8r5r1r15r16r12r10r14r0r4543210
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "unit_tree = toytree.rtree.baltree(20, random_names=True)\n", + "unit_tree.draw(scale_bar=True, ts='s', node_hover=True);" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Birth/death rtree\n", + "\n", + "The function `rtree.bdtree()` generates a parametric birth/death tree. This means that birth or death events are randomly sampled beginning with a single ancestor until a stopping criterion is reached (either `stop='taxa'` or `stop='time'`). If the tree goes extinct, it is restarted. The waiting time to the next speciation event is exponential with rate equal to the parameter `b=`, and the same goes for extinction events with parameter `d=`. The events do not happen in parallel, so the waiting type for the next event regardless of type is exponential with rate $b+d$, and the probability that it is a speciation event is $\\frac{b}{b+d}$ and the probability that it is an extinction event is $\\frac{d}{b+d}$" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "time 7.863758\n", + "ntips 40.000000\n", + "b 74.000000\n", + "d 35.000000\n", + "b/d 1.472973\n", + "resets 0.000000\n", + "dtype: float64\n" + ] + }, + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r390246
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "tree = toytree.rtree.bdtree(ntips=40, b=1.0, d=0.5, verbose=True)\n", + "tree.draw('c');" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Coalescent rtree \n", + "\n", + "Finally, the function `rtree.coaltree()` generates a random ToyTree under the n-coalescent model. Waiting times between coalescent events under this model follow the distribution: \n", + "$$\n", + "\\frac{4N}{k*(k-1)}\n", + "$$\n", + "Where $k$ is the number of samples at present time and $N$ is the diploid effective population size $N_e$ \n", + "\n", + "A tree is constructed by randomly sampling waiting times from an exponential distribution at each value of k from k to 1, and randomly joining Nodes at each coalescent interval. Note that the expected root height is $4N$. \n", + "\n", + "\n", + "The number of tips (usually ntips) is given by `k=`, which represents the number of samples at present time, and the diploid effective population size is given by `N=`." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
r0r1r2r3r4r5r6r7r8r9r10r11r12r13r14r15r16r17r18r19r20r21r22r23r24r25r26r27r28r29r30r31r32r33r34r35r36r37r38r3901020
" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "tree = toytree.rtree.coaltree(k=40, N=10)\n", + "tree.draw('c');" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "toytree_practice", + "language": "python", + "name": "toytree_practice" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/mkdocs.yml b/mkdocs.yml index 7c4d5621..e53ab305 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -132,7 +132,7 @@ nav: - .annotate - add to drawings: ... - - .rtree - random trees: ... + - .rtree - random trees: random-trees.ipynb # - networks: # - drawing admixture edges: ... diff --git a/toytree/rtree/_src/rtree.py b/toytree/rtree/_src/rtree.py index b977a0aa..65af1235 100644 --- a/toytree/rtree/_src/rtree.py +++ b/toytree/rtree/_src/rtree.py @@ -194,6 +194,10 @@ def baltree( # each node as the node distance from the root, to evenly create # splits. 'dists' are later overwritten by extend_tips_to_align. + #check that ntips is even, if not raise an error + if ntips%2 != 0: + raise ToytreeError("Number of tips must be even to make a balanced tree!") + # get a root Node and keep track of nancestors root = Node()