Traversing and iterating over trees

The code also provides iterators, and filtered iterators over the branches, nodes, branchnames and nodenames of a tree (using the random tree from Creating and writing phylogenies)

using Phylo
nu = Nonultrametric(5);
tree = rand(nu)

collect(nodeiter(tree))
9-element Vector{RecursiveNode{OneRoot, String, Dict{String, Any}, Dict{String, Any}, PolytomousBranching, Float64}}:
 leaf node 'tip 1'
 leaf node 'tip 2'
 leaf node 'tip 3'
 leaf node 'tip 4'
 leaf node 'tip 5'
 internal node 'Node 6'
 internal node 'Node 7'
 internal node 'Node 8'
 root node 'Node 9'
collect(nodenamefilter(isroot, tree))
1-element Vector{String}:
 "Node 9"

TreeSets are iterators themselves

trees = rand(nu, ["Tree 1", "Tree 2"])
collect(trees)
2-element Vector{RootedTree}:
 RootedTree with 5 tips and 1 root. Leaf names are tip 1, tip 2, tip 3, tip 4 and tip 5

9 nodes: [RecursiveNode{OneRoot} 'tip 1', a leaf with an incoming connection (branch 11), RecursiveNode{OneRoot} 'tip 2', a leaf with an incoming connection (branch 14), RecursiveNode{OneRoot} 'tip 3', a leaf with an incoming connection (branch 12), RecursiveNode{OneRoot} 'tip 4', a leaf with an incoming connection (branch 9), RecursiveNode{OneRoot} 'tip 5', a leaf with an incoming connection (branch 10), RecursiveNode{OneRoot} 'Node 6', an internal node with 1 inbound and 2 outbound connections (branches 16 and [9, 10]), RecursiveNode{OneRoot} 'Node 7', an internal node with 1 inbound and 2 outbound connections (branches 13 and [11, 12]), RecursiveNode{OneRoot} 'Node 8', an internal node with 1 inbound and 2 outbound connections (branches 15 and [13, 14]) and RecursiveNode{OneRoot} 'Node 9', a root node with 2 outbound connections (branches [15, 16])]

8 branches: [RecursiveBranch{OneRoot} 9, from node 'Node 6' to node 'tip 4' (length 0.5230131043902557), RecursiveBranch{OneRoot} 10, from node 'Node 6' to node 'tip 5' (length 0.36392595135579636), RecursiveBranch{OneRoot} 11, from node 'Node 7' to node 'tip 1' (length 0.06329396912043844), RecursiveBranch{OneRoot} 12, from node 'Node 7' to node 'tip 3' (length 0.8568933226666661), RecursiveBranch{OneRoot} 13, from node 'Node 8' to node 'Node 7' (length 0.49064143507325764), RecursiveBranch{OneRoot} 14, from node 'Node 8' to node 'tip 2' (length 1.0983443798842893), RecursiveBranch{OneRoot} 15, from node 'Node 9' to node 'Node 8' (length 0.12761911369688422) and RecursiveBranch{OneRoot} 16, from node 'Node 9' to node 'Node 6' (length 0.36519453067269303)]

Node records: "tip 1" => Dict{String, Any}() ... "Node 9" => Dict{String, Any}()

 RootedTree with 5 tips and 1 root. Leaf names are tip 1, tip 2, tip 3, tip 4 and tip 5

9 nodes: [RecursiveNode{OneRoot} 'tip 1', a leaf with an incoming connection (branch 11), RecursiveNode{OneRoot} 'tip 2', a leaf with an incoming connection (branch 14), RecursiveNode{OneRoot} 'tip 3', a leaf with an incoming connection (branch 12), RecursiveNode{OneRoot} 'tip 4', a leaf with an incoming connection (branch 9), RecursiveNode{OneRoot} 'tip 5', a leaf with an incoming connection (branch 10), RecursiveNode{OneRoot} 'Node 6', an internal node with 1 inbound and 2 outbound connections (branches 16 and [9, 10]), RecursiveNode{OneRoot} 'Node 7', an internal node with 1 inbound and 2 outbound connections (branches 13 and [11, 12]), RecursiveNode{OneRoot} 'Node 8', an internal node with 1 inbound and 2 outbound connections (branches 15 and [13, 14]) and RecursiveNode{OneRoot} 'Node 9', a root node with 2 outbound connections (branches [15, 16])]

8 branches: [RecursiveBranch{OneRoot} 9, from node 'Node 6' to node 'tip 4' (length 0.5230131043902557), RecursiveBranch{OneRoot} 10, from node 'Node 6' to node 'tip 5' (length 0.36392595135579636), RecursiveBranch{OneRoot} 11, from node 'Node 7' to node 'tip 1' (length 0.06329396912043844), RecursiveBranch{OneRoot} 12, from node 'Node 7' to node 'tip 3' (length 0.8568933226666661), RecursiveBranch{OneRoot} 13, from node 'Node 8' to node 'Node 7' (length 0.49064143507325764), RecursiveBranch{OneRoot} 14, from node 'Node 8' to node 'tip 2' (length 1.0983443798842893), RecursiveBranch{OneRoot} 15, from node 'Node 9' to node 'Node 8' (length 0.12761911369688422) and RecursiveBranch{OneRoot} 16, from node 'Node 9' to node 'Node 6' (length 0.36519453067269303)]

Node records: "tip 1" => Dict{String, Any}() ... "Node 9" => Dict{String, Any}()
Phylo.nodeiterFunction
nodeiter(tree::AbstractTree)

Returns an iterator over the nodes of any tree.

source
Phylo.nodefilterFunction
nodefilter(filterfn::Function, tree::AbstractTree)

Returns an iterator over the nodes of any tree, where the AbstractNode is filtered by the function filterfn.

source
Phylo.nodenameiterFunction
nodenameiter(tree::AbstractTree)

Returns an iterator over the names of the nodes of any tree.

source
Phylo.nodenamefilterFunction
nodenamefilter(filterfn::Function, tree::AbstractTree)

Returns an iterator over the nodenames of any tree, where the AbstractNode itself is filtered by the function filterfn.

source
Phylo.branchiterFunction
branchiter(tree::AbstractTree)

Returns an iterator over the branches of any tree.

source
Phylo.branchfilterFunction
branchfilter(filterfn::Function, tree::AbstractTree)

Returns an iterator over the branches of any tree, where the AbstractBranch is filtered by the function filterfn.

source
Phylo.branchnameiterFunction
branchnameiter(tree::AbstractTree)

Returns an iterator over the names of branches of any tree.

source
Phylo.branchnamefilterFunction
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.

source
Phylo.traversalFunction
traversal(::AbstractTree, ::TraversalOrder)
traversal(::AbstractTree, ::TraversalOrder, init)

Return an iterable object for a tree containing nodes in given order - anyorder, preorder, inorder, postorder or breadthfirst - optionally starting from init.

source