Basics
This section provides an overview over the basic functionalities offered.
When building / modifying trees with methods like add_child! or insert_node!, it might be necessarry to run initialize_tree! or update_tree! to ensure the nodes remain /are initialized.
WARNING: Do not run initialize_tree! after changing a tree, if you rely on the num field of the nodes to identify them. The same applies to the method number_nodes that is called in initalize_tree.
Basic Tree Functionalities
MCPhyloTree.add_child!
— Methodfunction add_child!(mother_node::N, child::N, child_position::Int64)::Nothing where N <: GeneralNode
This function adds a child to the mother node. The arity of the mother node is increased by 1
and the root status of the child is set to False
.
mother_node
: Node to add a child to.child
: Node to add to mother_node.children.child_position
: index at which to add the new child node.
MCPhyloTree.add_child!
— Methodadd_child!(mother_node::N, child::N)::Nothing where N <: GeneralNode
This function adds a child to the mother node. The arity of the mother node is increased by 1
and the root status of the child is set to False
.
mother_node
: Node to add a child to.child
: Node to add to mother_node.children.
MCPhyloTree.check_binary
— Methodcheck_binary(root::AbstractNode)::Bool
checks to see if given tree is binary; returns true if properly formatted and false otherwise
MCPhyloTree.check_leafsets
— Methodcheck_leafsets(trees::Vector{T})::Nothing where T<:AbstractNode
Checks if an array of trees shares the same leafset (based on the leaf names)
MCPhyloTree.delete_node!
— Methoddelete_node!(node::T)::Nothing where T<:AbstractNode
This functions deletes node from a tree and assigns all its children to its mother node.
node
: Node to be deleted.
MCPhyloTree.force_ultrametric!
— Methodforce_ultrametric!(root::T) where T<:AbstractNode
Force an ultrametric version of the tree.
MCPhyloTree.get_branchlength_vector
— Methodget_branchlength_vector(root::N)::Vector{T} where {N <:AbstractNode, T<:Real}
Get the vector of branch lengths of the tree.
MCPhyloTree.get_branchlength_vector
— Methodget_branchlength_vector(root::N, out_vec::Vector{T}) where {N<:AbstractNode, T<:Real}
Do post order traversal to retrieve a vector of branch lengths.
MCPhyloTree.get_mother
— Methodget_mother(node::T)::T where T<:AbstractNode
This function gets the mother of node
. It does so by looking for the respective binary representation of the mother node.
MCPhyloTree.get_sister
— Methodget_sister(node::T)::T where T<:AbstractNode
This function gets the sister of node
. It does so by looking for the respective binary representation of the sister.
MCPhyloTree.get_sum_seperate_length!
— Methodget_sum_seperate_length!(post_order::Vector{T})::Vector{Float64} where T<:AbstractNode
This function gets the sum of the branch lengths of the internal branches and the branches leading to the leave nodes.
MCPhyloTree.initialize_tree!
— Methodinitialize_tree!(root::GeneralNode; height::Bool=true)
This function initializes a tree, i.e. numbers its nodes and sets the binary + height fields.
MCPhyloTree.insert_node!
— Methodinsert_node!(mother::T, children::Vector{T})::T where T<:AbstractNode
This function inserts a node into a tree after a mother node and gains a subset of the mother's children as its children.
Returns the inserted node.
mother
: Node under which to add the newly-inserted node.children
: Children of node referenced by "mother" to reassign as children of the newly-inserted node.
MCPhyloTree.node_age
— Methodfunction node_age(node<:AbstractNode)::Float64
Calculates the age of a node. If the tree is ultrametric then the node age is identical to the node height. It is calculated by subtracting the path length between the node & the root from the height of the root. This represents the age of the node, assuming the leaf farthest from the root has a node age of 0, and the root node is the 'oldest' node.
MCPhyloTree.node_depth
— Methodnode_depth(node::T)::Int64 where T<:AbstractNode
Calculate the depth of a node.
MCPhyloTree.node_height
— Methodfunction node_height(root::T)::Nothing where T<:AbstractNode
Calculate the height of a node.
MCPhyloTree.number_nodes!
— Methodnumber_nodes!(root::T)::Nothing where T<:AbstractNode
This function assigns a unique, sequential number to each node. Leaves are numbered first in alphabetical order.
MCPhyloTree.path_length
— Methodpath_length(ancestor::T, descendant::T)::Float64 where T<:AbstractNode
Note: The function assumes there is an ancestral relationship between the two nodes.
This function calculates the length of the path separating the ancestor from the offspring node. The function follows the path specified through the binary description of the node.
MCPhyloTree.random_node
— Methodrandom_node(root::T)::T where T<:AbstractNode
This function returns a random node from the tree.
MCPhyloTree.remove_child!
— Methodremove_child!(mother_node::N, left::Bool)::N where N <: GeneralNode
This function removes a child from the list of nodes which are daughters of this node. An input of "True" removes the left child, while "False" removes the right child.
Returns the removed node.
mother_node
: Node from which to remove a child.left
: boolean value determining which child of mother_node to remove.
MCPhyloTree.remove_child!
— Methodremove_child!(mother_node::N, child::N)::N where N<:AbstractNode
This function removes a child from the list of nodes which are daughters of this node.
The removed node is returned.
mother_node
: Node from which to remove a child.child
: specific Node to remove. This node has to be a child ofmother_node
.
MCPhyloTree.set_binary!
— Methodset_binary!(root::T) where T <: GeneralNode
Assign a binary representation to each node, which specifies the path from the root to this node via the binary representation of the node. A left turn is a 1 in binary and a right turn a 0.
MCPhyloTree.set_branchlength_vector!
— Methodset_branchlength_vector!(root::N, blenvec::Array{T}) where {N<:AbstractNode, T<:Real}
This function sets the branch lengths of a tree to the values specified in blenvec.
MCPhyloTree.tree_height
— Methodtree_height(root::T)::Float64 where T<:AbstractNode
This function calculates the tree height.
MCPhyloTree.tree_length
— Methodtree_length(root::T)::Float64 where T<:AbstractNode
This function calculates the tree length.
MCPhyloTree.tree_length
— Methodtree_length(root::T, tl::Float64)::Float64 where T<:AbstractNode
This function does the internal tree length recursion
MCPhyloTree.update_tree!
— Methodupdate_tree!(root::GeneralNode)
This function can be used to recompute the tree's binary and height values. This might be necessary after adding/moving/removing nodes.
Tree Pruning
MCPhyloTree.prune_tree!
— Methodprune_tree!(root::T, node_names::Vector{String})::Nothing where T<:AbstractNode
In-place version of prune_tree.
root
: root Node of tree to prune.node_names
: vector of strings, used to specify nodes to remove.
MCPhyloTree.prune_tree!
— Methodprune_tree!(root::T, node_names::Vector{T})::Nothing where T<:AbstractNode
In-place version of prune_tree.
root
: root node of tree to prune.node_names
: vector of Node objects to be removed from tree.
MCPhyloTree.prune_tree
— Methodprune_tree(root::T, node_names::Vector{String})::T where T<:AbstractNode
This function removes specific nodes, including their descendants, from a tree.
root
: root Node of tree to prune.node_names
: vector of strings, used to specify nodes to remove.
Tree Search
MCPhyloTree.find_binary
— Methodfind_binary(root::T, bin::String)::T where T<:AbstractNode
Find a node by its binary representation. The function assumes that the node is present in the tree.
Do not use this function if you are unsure whether the node is in the tree at all.
Returns a reference to the desired Node.
root
: root Node of tree to search.bin
: binary representation of desired Node as a String.
MCPhyloTree.find_by_binary
— Methodfind_by_binary(tree::GeneralNode, bin::String)
Alias for find_binary
.
MCPhyloTree.find_by_name
— Methodfind_by_name(tree::GeneralNode, name::AbstractString)
Alias for find_name
.
MCPhyloTree.find_by_num
— Methodfind_by_num(tree::GeneralNode, num::Int64)
Alias for find_num
.
MCPhyloTree.find_name
— Methodfind_name(root::T, name::S, rn::Vector{T})::Bool where {T<:AbstractNode, S<:AbstractString}
Do a post order traversal to find the node corresponding to the name
.
Returns true if node is found, false otherwise. Desired Node is pushed to rn.
root
: root Node of tree to be searched.name
: name of desired Node.rn
: Vector of Nodes; desired Node is pushed to this vector when found.
MCPhyloTree.find_name
— Methodfind_name(root::T, name::S)::T where {T<:AbstractNode, S<:AbstractString}
Find a node by its name. Returns reference to Node.
root
: root Node of tree to be searched.name
: name of desired Node.
MCPhyloTree.find_num
— Methodfind_num(root::T, num::Int64, rn::Vector{T})::Bool where T<:AbstractNode
Do a post order traversal to find the node corresponding to the num
.
Returns true if node is found, false otherwise. Desired Node is pushed to rn.
root
: root Node of tree to be searched.num
: number of desired Node.rn
: Vector of Nodes; desired Node is pushed to this vector when found.
MCPhyloTree.find_num
— Methodfind_num(root::T, num::Int64) where T<:AbstractNode
Find a node by its number. The function assumes that the node is present in the tree.
Do not use this function if you are unsure whether the node is in the tree at all.
Returns reference to Node.
root
: root Node of tree to be searched.num
: number of desired Node.
MCPhyloTree.find_root
— Methodfind_root(node::T)::T where T <: GeneralNode
Finds the root of tree indicated by Node.
Returns reference to root Node of the tree.
node
: Node in Tree of interest.
Tree Traversal
MCPhyloTree.get_leaves
— Methodget_leaves(root::T) where T<:AbstractNode
This function returns the leaves of a tree. Only the root node needs to be supplied.
Returns an Iterator over leaf Nodes.
root
: root Node of tree.
MCPhyloTree.level_order
— Methodlevel_order(node::T) where T<:GeneralNode
This function does level order traversal. Only the root node needs to be supplied.
Returns a level order iterator.
node
: root Node of tree.
MCPhyloTree.post_order
— Methodpost_order(root::T) where T<:GeneralNode
This function does post order traversal. Only the root node needs to be supplied.
Returns a post order iterator.
root
: root Node of tree.
MCPhyloTree.pre_order
— Methodpre_order(root::T) where T<:GeneralNode
This function does pre order traversal. Only the root node needs to be supplied.
Returns a preoder Iterator.
root
: root Node of tree.
Tree Representations
MCPhyloTree.newick
— Methodnewick(root::T)::String where T<:AbstractNode
Creates a newick representation of the tree.
Returns a properly formatted newick String.
node
: root node of tree used to create the newick string.