Phylo
Package for creating and manipulating phylogenies
Phylo is a Julia package that provides functionality for generating phylogenetic trees to feed into our Diversity package to calculate phylogenetic diversity (currently on master, accessible via Pkg.checkout()
, but not released). Both are currently under development, so please raise an issue if you find any problems. Currently the package can be used to make trees manually, and to generate random trees using the framework from Distributions
. For instance, to construct a sampler for 5 tip non-ultrametric trees, and then generate a random tree of that type:
julia> using Phylo julia> nu = Nonultrametric(5); julia> tree = rand(nu) NamedTree phylogenetic tree with 9 nodes and 8 branches Leaf names: String["tip 1", "tip 2", "tip 3", "tip 4", "tip 5"]
The code also provides iterators, and filtered iterators over the branches, nodes, branchnames and nodenames of a tree:
julia> collect(nodeiter(tree)) 9-element Array{Phylo.BinaryNode{Int64},1}: [branch 4]-->[leaf node] [branch 5]-->[leaf node] [branch 2]-->[leaf node] [branch 1]-->[leaf node] [branch 8]-->[leaf node] [branch 3]-->[internal node]-->[branches 1 and 2] [branch 6]-->[internal node]-->[branches 3 and 4] [branch 7]-->[internal node]-->[branches 5 and 6] [root node]-->[branches 7 and 8] julia> collect(nodenamefilter(isroot, tree)) 1-element Array{String,1}: "Node 4"
The current main purpose of this package is to provide a framework for phylogenetics to use in our Diversity package, and they will both be adapted as appropriate until both are functioning as required (though they are currently working together reasonably successfully).
However, it can also read newick trees:
julia> using Phylo julia> simpletree = parsenewick("((,Tip:1.0)Internal,)Root;") NamedTree phylogenetic tree with 5 nodes and 4 branches Leaf names: String["Node 2", "Tip", "Node 1"] julia> getbranches(simpletree) Dict{Int64,Phylo.Branch{String}} with 4 entries: 4 => [node "Root"]-->[NaN length branch]-->[node "Node 2"] 2 => [node "Internal"]-->[1.0 length branch]-->[node "Tip"] 3 => [node "Root"]-->[NaN length branch]-->[node "Internal"] 1 => [node "Internal"]-->[NaN length branch]-->[node "Node 1"] julia> open(parsenewick, tree = open(parsenewick, Pkg.dir("Phylo", "test", "h1n1.trees"))) NamedTree phylogenetic tree with 1013 nodes and 1012 branches Leaf names: String["407", "153", "1", "54", "101", "371", "41", "464", "65", "475" … "336", "145", "36", "95", "414", "138", "294", "353", "232", "306"]
And while we wait for me (or kind contributors!) to fill out the other extensive functionality that many phylogenetics packages have in other languages, the other important feature that it offers is a fully(?)-functional interface to R, allowing any existing R library functions to be carried out on julia trees, and trees to be read from disk and written using R helper functions. Naturally the medium-term plan is to fill in as many of these gaps as possible in Julia, and as a result this R interface is not built into the package as it will make RCall (and R) a dependency, which I wanted to avoid. Instead, if you want to use the R interface you need to do it manually, as below:
julia> using RCall julia> include(joinpath(Pkg.dir("Phylo"), "src", "rcall.jl")); R> library(ape)
You can then translate back and forth using NamedTree
contructors on R phylo
objects, and RObject
constructors on julia NamedTree
types to keep them in Julia or @rput
to move the object into R:
julia> rt = rcall(:rtree, 10) RCall.RObject{RCall.VecSxp} Phylogenetic tree with 10 tips and 9 internal nodes. Tip labels: t10, t8, t1, t2, t6, t5, ... Rooted; includes branch lengths. julia> jt = NamedTree(rt) NamedTree phylogenetic tree with 19 nodes and 18 branches Leaf names: String["t10", "t8", "t1", "t2", "t6", "t5", "t3", "t4", "t7", "t9"] julia> @rput rt; julia> @rput jt; # Automatically translates jt back to R R> jt Phylogenetic tree with 10 tips and 9 internal nodes. Tip labels: t10, t8, t1, t2, t6, t5, ... Rooted; includes branch lengths. R> all.equal(rt, jt) # check no damage in translations [1] TRUE
#
Phylo
— Module.
Phylo package
The Phylo
package provides a standard abstract interface to phylogenetic trees, by defining AbstractNode
, AbstractBranch
and AbstractTree
supertypes, and methods to interface to them. It also provides (through the Phylo.API
submodule) methods to (re)define to write your own phylogenetic type in a way that will interact cleanly with other phylogenetic packages. Finally, it provides a simple phylogenetics type.
#
Phylo.BinaryNode
— Type.
BinaryNode{T}(AbstractVector{T}, AbstractVector{T}) <: AbstractNode
A node of strict binary phylogenetic tree
#
Phylo.BinaryTree
— Type.
BinaryTree
Binary phylogenetic tree object with known leaves and per node data
#
Phylo.Branch
— Type.
Branch A directed branch connecting two AbstractNodes of phylogenetic tree
#
Phylo.NamedTree
— Type.
NamedTree
Binary phylogenetic tree object with known leaves
#
Phylo.Nonultrametric
— Type.
Nonultrametric{T <: AbstractTree, RNG <: Sampleable}(n::Int, rng::RNG = Exponential()) Nonultrametric{T <: AbstractTree, RNG <: Sampleable}(tiplabels::Vector{String}, rng::RNG = Exponential())
The sampler for non-ultrametric phylogenetic trees of size n
or with tip labels tiplabels
. Generate random trees by calling rand(). Currently only works for NamedTree
s.
#
Phylo.Ultrametric
— Type.
Ultrametric{T <: AbstractTree, RNG <: Sampleable}(n::Int, rng::RNG = Exponential()) Ultrametric{T <: AbstractTree, RNG <: Sampleable}(tiplabels::Vector{String}, rng::RNG = Exponential())
The sampler for ultrametric phylogenetic trees of size n
or with tip labels tiplabels
. Generate random trees by calling rand(). Currently only works for NamedTree
s.
#
Phylo.addbranch!
— Function.
addbranch!(tree::AbstractTree, source, destination[, length::Float64]; branchname = _newbranchlabel(tree))
Add a branch from source
to destination
on tree
.
#
Phylo.addnode!
— Function.
addnode!(tree::AbstractTree) addnode!(tree::AbstractTree, nodename)
#
Phylo.addnodes!
— Function.
addnodes!(tree::AbstractTree, nodenames::AbstractVector) addnodes!(tree::AbstractTree, count::Integer)
#
Phylo.branch!
— Function.
branch!(tree::AbstractTree, source[, length]) branch!(tree::AbstractTree, source[, length]; destination) branch!(tree::AbstractTree, source[, length]; destination, branchname)
Branch from a source node source
and create a destination node destination
.
#
Phylo.branchfilter
— Method.
branchfilter(filterfn::Function, tree::AbstractTree)
Returns an iterator over the branches of any tree, where the AbstractBranch
is filtered by the function filterfn
.
#
Phylo.branchhistory
— Method.
branchhistory(tree::AbstractTree, node)
Find the branch route between a node on a tree and its root
#
Phylo.branchiter
— Method.
branchiter(tree::AbstractTree)
Returns an iterator over the branches of any tree.
#
Phylo.branchnamefilter
— Method.
branchnamefilter(filterfn::Function, tree::AbstractTree)
Returns an iterator over the names of the branches of any tree, where the AbstractBranch
is filtered by the function filterfn
.
#
Phylo.branchnameiter
— Method.
branchnameiter(tree::AbstractTree)
Returns an iterator over the names of branches of any tree.
#
Phylo.branchnametype
— Method.
branchnametype(::AbstractTree)
Returns type of branch names.
#
Phylo.branchroute
— Method.
branchroute(tree::AbstractTree, node1, node2)
Find the branch route between two nodes on a tree
#
Phylo.branchtype
— Method.
branchtype(tree::AbstractTree)
Returns type of branches in a tree.
#
Phylo.changedst!
— Method.
changedst!(tree::AbstractTree, branchname, destination)
Change the destination node for this node.
#
Phylo.changesrc!
— Method.
changesrc!(tree::AbstractTree, branchname, source)
Change the source node for this branch.
#
Phylo.clearrootheight!
— Method.
clearrootheight(::AbstractTree)
Clears the tree's root height record.
#
Phylo.deletebranch!
— Method.
deletebranch!(tree::AbstractTree, branchname)
Delete the branch branchname
from tree
.
#
Phylo.deletenode!
— Method.
deletenode!(tree::AbstractTree, nodename)
#
Phylo.distance
— Method.
distance(tree::AbstractTree, node1, node2)
Distance between two nodes on a tree
#
Phylo.distances
— Method.
distances(tree::AbstractTree)
Pairwise distances between all leaf nodes on a tree
#
Phylo.dst
— Function.
dst(branch::AbstractBranch) dst(tree::AbstractTree, branchname)
Return the destination node for this branch.
#
Phylo.getancestors
— Method.
getancestors(tree::AbstractTree, nodename)
Return the name of all of the nodes that are ancestral to this node.
#
Phylo.getbranch
— Method.
getbranch(tree::AbstractTree, branchname)
#
Phylo.getbranchnames
— Method.
getbranchnames(tree::AbstractTree)
#
Phylo.getchildren
— Method.
getchildren(tree::AbstractTree, nodename)
Return the name(s) of the child node(s) for this node.
#
Phylo.getdescendants
— Method.
getdescendants(tree::AbstractTree, nodename)
Return the names of all of the nodes that descend from this node.
#
Phylo.getheight
— Method.
getheight(tree::AbstractTree, nodename)
Return the height of the node.
#
Phylo.getinbound
— Function.
getinbound(node::AbstractNode) getinbound(tree::AbstractTree, nodename)
return the name of the inbound branch to this node.
#
Phylo.getleafinfo
— Method.
getleafinfo(::AbstractTree, label)
retrieve the leaf info for a leaf of the tree.
#
Phylo.getleafnames
— Method.
getleafnames(::AbstractTree)
Retrieve the leaf names from the tree.
#
Phylo.getlength
— Function.
getlength(branch::AbstractBranch) getlength(tree::AbstractTree, branchname)
Return the length of this branch.
#
Phylo.getnode
— Method.
getnode(tree::AbstractTree, nodename)
#
Phylo.getnodenames
— Method.
getnodenames(tree::AbstractTree)
#
Phylo.getnoderecord
— Method.
getnoderecord(::AbstractTree, label)
retrieve the node record for a leaf of the tree.
#
Phylo.getoutbounds
— Function.
getoutbounds(node::AbstractNode) getoutbounds(tree::AbstractTree, nodename)
Return the names of the outbound branches from this node.
#
Phylo.getparent
— Method.
getparent(tree::AbstractTree, nodename)
Return the name of the parent node for this node.
#
Phylo.getrootheight
— Method.
getrootheight(tree::AbstractTree)
#
Phylo.hasbranch
— Method.
hasbranch(tree::AbstractTree, branchname)
#
Phylo.hasheight
— Function.
hasheight(tree::AbstractTree, nodename)
Does the node have a height defined?
#
Phylo.hasinbound
— Function.
hasinbound(node::AbstractNode) hasinbound(tree::AbstractTree, nodename)
Does the node have an inbound connection?
#
Phylo.hasinboundspace
— Function.
hasinboundspace(node::AbstractNode) hasinboundspace(tree::AbstractTree, nodename)
Does the node have space for an inbound connection?
#
Phylo.hasnode
— Method.
hasnode(tree::AbstractTree, nodename)
#
Phylo.hasoutboundspace
— Function.
hasoutboundspace(node::AbstractNode) hasoutboundspace(tree::AbstractTree, nodename)
Does the node have space for an[other] outbound connection?
#
Phylo.hasrootheight
— Method.
hasrootheight(tree::AbstractTree)
#
Phylo.heightstoroot
— Method.
heights(tree::AbstractTree)
Height of all of the leaves of the tree above the root
#
Phylo.heighttoroot
— Method.
height(tree::AbstractTree, node)
Height of a node of the tree above the root
#
Phylo.indegree
— Function.
indegree(node::AbstractNode) indegree(tree::AbstractTree, nodename)
#
Phylo.isinternal
— Function.
isinternal(node::AbstractNode) isinternal(tree::AbstractTree, nodename)
#
Phylo.isleaf
— Function.
isleaf(node::AbstractNode) isleaf(tree::AbstractTree, nodename)
#
Phylo.isroot
— Function.
isroot(node::AbstractNode) isroot(tree::AbstractTree, nodename)
#
Phylo.isunattached
— Function.
isunattached(node::AbstractNode) isunattached(tree::AbstractTree, nodename)
#
Phylo.nodefilter
— Method.
nodefilter(filterfn::Function, tree::AbstractTree)
Returns an iterator over the nodes of any tree, where the AbstractNode
is filtered by the function filterfn
.
#
Phylo.nodehistory
— Method.
nodehistory(tree::AbstractTree, node)
Find the node route between a node on a tree and its root
#
Phylo.nodeiter
— Method.
nodeiter(tree::AbstractTree)
Returns an iterator over the nodes of any tree.
#
Phylo.nodenamefilter
— Method.
nodenamefilter(filterfn::Function, tree::AbstractTree)
Returns an iterator over the nodenames of any tree, where the AbstractNode
itself is filtered by the function filterfn
.
#
Phylo.nodenameiter
— Method.
nodenameiter(tree::AbstractTree)
Returns an iterator over the names of the nodes of any tree.
#
Phylo.nodenametype
— Method.
nodenametype(::AbstractTree)
Returns type of node names.
#
Phylo.noderoute
— Method.
noderoute(tree::AbstractTree, node1, node2)
Find the node route between two nodes on a tree
#
Phylo.nodetype
— Method.
nodetype(tree::AbstractTree)
Returns type of nodes in a tree.
#
Phylo.outdegree
— Function.
outdegree(node::AbstractNode) outdegree(tree::AbstractTree, nodename)
#
Phylo.setheight!
— Method.
setheight!(tree::AbstractTree, nodename, height)
Set the height of the node.
#
Phylo.setleafinfo!
— Method.
setleafinfo!(::AbstractTree, label, value)
Set the leaf info for a leaf of the tree.
#
Phylo.setnoderecord!
— Method.
setnoderecord(::AbstractTree, label, value)
Set the node record for a node of the tree.
#
Phylo.setrootheight!
— Method.
setrootheight!(tree::AbstractTree, height)
#
Phylo.src
— Function.
src(branch::AbstractBranch) src(tree::AbstractTree, branchname)
Return the source node for this branch.
#
Phylo.validate
— Method.
validate(tree::AbstractTree)
Phylo
Phylo.API
Phylo.BinaryNode
Phylo.BinaryTree
Phylo.Branch
Phylo.NamedTree
Phylo.Nonultrametric
Phylo.Ultrametric
Phylo.API._addbranch!
Phylo.API._addnode!
Phylo.API._addnodes!
Phylo.API._addoutbound!
Phylo.API._branch!
Phylo.API._branchtype
Phylo.API._deletebranch!
Phylo.API._deletenode!
Phylo.API._deleteoutbound!
Phylo.API._dst
Phylo.API._getbranch
Phylo.API._getbranches
Phylo.API._getbranchnames
Phylo.API._getheight
Phylo.API._getinbound
Phylo.API._getlength
Phylo.API._getnode
Phylo.API._getnodenames
Phylo.API._getnodes
Phylo.API._getoutbounds
Phylo.API._getrootheight
Phylo.API._hasbranch
Phylo.API._hasheight
Phylo.API._hasinbound
Phylo.API._hasinboundspace
Phylo.API._hasnode
Phylo.API._hasoutboundspace
Phylo.API._hasrootheight
Phylo.API._indegree
Phylo.API._isinternal
Phylo.API._isleaf
Phylo.API._isroot
Phylo.API._isunattached
Phylo.API._newbranchlabel
Phylo.API._newnodelabel
Phylo.API._nodetype
Phylo.API._outdegree
Phylo.API._setdst!
Phylo.API._setheight!
Phylo.API._setinbound!
Phylo.API._setrootheight!
Phylo.API._setsrc!
Phylo.API._src
Phylo.API._validate
Phylo.addbranch!
Phylo.addnode!
Phylo.addnodes!
Phylo.branch!
Phylo.branchfilter
Phylo.branchhistory
Phylo.branchiter
Phylo.branchnamefilter
Phylo.branchnameiter
Phylo.branchnametype
Phylo.branchroute
Phylo.branchtype
Phylo.changedst!
Phylo.changesrc!
Phylo.clearrootheight!
Phylo.deletebranch!
Phylo.deletenode!
Phylo.distance
Phylo.distances
Phylo.dst
Phylo.getancestors
Phylo.getbranch
Phylo.getbranchnames
Phylo.getchildren
Phylo.getdescendants
Phylo.getheight
Phylo.getinbound
Phylo.getleafinfo
Phylo.getleafnames
Phylo.getlength
Phylo.getnode
Phylo.getnodenames
Phylo.getnoderecord
Phylo.getoutbounds
Phylo.getparent
Phylo.getrootheight
Phylo.hasbranch
Phylo.hasheight
Phylo.hasinbound
Phylo.hasinboundspace
Phylo.hasnode
Phylo.hasoutboundspace
Phylo.hasrootheight
Phylo.heightstoroot
Phylo.heighttoroot
Phylo.indegree
Phylo.isinternal
Phylo.isleaf
Phylo.isroot
Phylo.isunattached
Phylo.nodefilter
Phylo.nodehistory
Phylo.nodeiter
Phylo.nodenamefilter
Phylo.nodenameiter
Phylo.nodenametype
Phylo.noderoute
Phylo.nodetype
Phylo.outdegree
Phylo.setheight!
Phylo.setleafinfo!
Phylo.setnoderecord!
Phylo.setrootheight!
Phylo.src
Phylo.validate