Trees¶
This module provides some facilities for constructing and plotting trees. It is mostly a wrapper around a very limited subset of functions from the R ape package (Analyses of Phylogenetics and Evolution).
R must be installed, the ape R package must be installed, and the Python package rpy2 must be installed, e.g.:
$ apt-get install r-base
$ pip install rpy2
$ R
> install.packages("ape")
See also the examples at:
- anhima.tree.nj(dist_square, labels=None)[source]¶
Wrapper for the ape nj function, which performs the neighbor-joining tree estimation of Saitou and Nei (1987).
Parameters: dist_square : array_like, shape (n_samples, n_samples)
A pairwise distance matrix in square form.
labels : sequence of strings, optional
A sequence of strings to label the tips of the tree. Must be in the same order as rows of the distance matrix.
Returns: An R object of class “phylo”.
See also
- anhima.tree.bionj(dist_square, labels=None)[source]¶
Wrapper for the ape bionj function, which performs the BIONJ algorithm of Gascuel (1997).
Parameters: dist_square : array_like, shape (n_samples, n_samples)
A pairwise distance matrix in square form.
labels : sequence of strings, optional
A sequence of strings to label the tips of the tree. Must be in the same order as rows of the distance matrix.
Returns: An R object of class “phylo”.
See also
- anhima.tree.read_tree(filename, **kwargs)[source]¶
Wrapper for the ape read.tree function, which reads a file which contains one or several trees in parenthetic format known as the Newick or New Hampshire format.
Parameters: filename : string
Name of the file to read.
**kwargs : keyword arguments
All further keyword arguments are passed through to read.tree.
Returns: tree : R object of class “phylo”
If several trees are read in the file, the returned object is of class “multiPhylo”, and is a list of objects of class “phylo”. The name of each tree can be specified by tree.names, or can be read from the file (see details).
- anhima.tree.write_tree(tree, filename=None, **kwargs)[source]¶
Wrapper for the ape write.tree function, which writes in a file a tree in parenthetic format using the Newick (also known as New Hampshire) format.
Parameters: tree : R object of class “phylo”
The tree to be written.
filename : string, optional
The name of the file to write to. If ommitted, write the file to a string and return it.
**kwargs : keyword arguments
All further keyword arguments are passed through to write.tree.
Returns: result : string
A string if filename is None, otherwise no return value.
- anhima.tree.plot_phylo(tree, plot_kwargs=None, add_scale_bar=None, filename=None, width=None, height=None, units=None, res=None, pointsize=None, bg=None, ax=None, imshow_kwargs=None)[source]¶
Wrapper for the ape plot.phylo function, which plots phylogenetic trees. Plotting will use the R png graphics device.
Parameters: tree : R object of class “phylo”
The tree to plot.
plot_kwargs : dict-like, optional
A dictionary of keyword arguments that will be passed through to the ape function plot.phylo(). See the documentation for the ape package for a full list of supported arguments.
add_scale_bar : dict-like, optional
A dictionary of keyword arguments that will be passed through to the ape function add.scale.bar(). See the documentation for the ape package for a full list of supported arguments.
filename : string, optional
File path for the generated PNG image. If None, a temporary file will be used.
width : int or float, optional
Width of the plot in units.
height : int or float, optional
Height of the plot in units.
units : {‘px’, ‘in’, ‘cm’, ‘mm’}, optional
The units in which ‘height’ and ‘width’ are given. Can be ‘px’ (pixels, the default), ‘in’ (inches), ‘cm’ or ‘mm’.
res : int, optional
The nominal resolution in ppi which will be recorded in the bitmap file, if a positive integer. Also used for ‘units’ other than the default, and to convert points to pixels.
pointsize : float, optional
The default pointsize of plotted text, interpreted as big points ( 1/72 inch) at ‘res’ ppi.
bg : color, optional
The background color.
ax : axes, optional
The axes on which to draw. If not provided, a new figure will be created.
imshow_kwargs : dict-like
Additional keyword arguments passed through to imshow().
Returns: ax : axes
The axes on which the plot was drawn.
- anhima.tree.color_edges_by_group_majority(tree, labels, groups, colors, equality_color='gray')[source]¶
Color the edges of a tree according to the majority group membership of the descendant tips.
Parameters: tree : R object of class “phylo”
The tree containing the edges to be colored.
labels : sequence of strings
The tip labels.
groups : sequence of strings
A sequence of strings of the same length as labels, where each item is an identifier for the group to which the corresponding tip belongs.
colors : dict-like
A dictionary mapping groups to colors.
equality_color : string, optional
The color to use in the event of a tie.
Returns: edge_colors : list of strings
A list of colors for the edges of the tree, to be passed into plot_phylo().