BioJulia/Bio.jl — pull request #17 by Ward9250 (2014-07-25 01:10:54+00:00)
Title: WIP: Version 1 of Phylo

A lot to add but I'll add to the branch piecemeal from my other Phylogenetics repo to make sure things are acceptable bit by bit. To begin with, we have the basic types and methods for building phylogenies with Phylo. It was important to go back and think harder about this than in the previous version where phylogenies were heavily borrowed from ape and R. These types and methods after all are going to be building blocks for the input / output and creation / manipulation of trees as well as all manner of other possible methods.

The structure of a phylogeny is now as follows:

A Phylogeny type contains a name, whether it is rooted and reportable, and the root. The root is a reference to an instance of PhyNode. A PhyNode has a name, a field for branch length (length of the edge between it and it's parent). An array for extensions / annotations, an array of references to it's children (other PhyNodes), and a reference to it's parent - if it is a single node created on it's own or is the root of a tree or subtree, this parent field point to itself i.e. the node is self referential at that field.

We have several basic methods for getting and setting information, as well as several functions that mania the references to parents and children to edit the structure of the tree. However, currently only two are exported - prune! and graft! - these are the "safe" tree manipulators. You can graft! a subtree or single child to another node of a tree, or prune! and node off a tree, returning the node and any other children it has - yielding a subtree cut away from the rest of the tree. The graft! methods sure that when grafting the parent and child references are made properly, and the prune! method likewise makes sure these links are broken properly so there are no situations where a parent refers to a child, yet the child doesn't refer back to that parent and so on.

Let's say you wanted to build a basic tree out of some nodes in a Julia session (tree building algorithms may well have to do this) Well make a basic cladogram without branch lengths.

a = PhyNode("A.")
b = PhyNode("B.")
c = PhyNode("C.")
d = PhyNode("D.")
e = PhyNode("E.")

Let's say you want a, b and c to be tips and e the root and d an internal node connecting a and b.
We can build the structure like so.
graft!(e, d)
graft!(e, c)
graft!(d, a)
graft!(d, b)

Calling any of a, b, c, d, e. in the console allows you to see the structure.

So we have our PhyNodes linked together, now we can finish the tree.

myTree = Phylogeny("My Simple Tree", e, true, false)

We can also cal methods on the nodes:

isLeaf(a)
isLeaf(b)
isLeaf(c)
isLeaf(d)
isLeaf(e)

isRoot(a)
isRoot(b)
isRoot(c)
isRoot(d)
isRoot(e)

isNode(a)
isNode(b)
isNode(c)
isNode(d)
isNode(e)

Hope you like! Comments of course welcome!