jebl.evolution.trees
Class SimpleRootedTree

java.lang.Object
  extended by jebl.evolution.trees.SimpleRootedTree
All Implemented Interfaces:
Graph, RootedTree, Tree, Attributable

public final class SimpleRootedTree
extends Object
implements RootedTree

A simple, and initially immutable rooted tree implementation. All returned collections are defensively copied. The implementation of Node is private. A number of methods are provided that can be used to construct a tree (createExternalNode & createInternalNode).

Author:
Andrew Rambaut, Alexei Drummond

Nested Class Summary
 
Nested classes/interfaces inherited from interface jebl.evolution.graphs.Graph
Graph.NoEdgeException, Graph.Utils
 
Field Summary
protected  Set<Node> internalNodes
           
protected  jebl.evolution.trees.SimpleRootedTree.SimpleRootedNode rootNode
           
 
Constructor Summary
SimpleRootedTree()
           
SimpleRootedTree(RootedTree tree)
          Make a copy of the given rooted tree
SimpleRootedTree(Tree tree, Node ingroupNode, Node outgroupNode, double ingroupBranchLength)
          Make a copy of the given unrooted tree
 
Method Summary
protected  void calculateBranchLengths()
          Calculate branch lengths from the current node heights.
 boolean conceptuallyUnrooted()
          Due to current implementation limitations, trees store "branch" information in nodes.
 Node createExternalNode(Taxon taxon)
          Creates a new external node with the given taxon.
 jebl.evolution.trees.SimpleRootedTree.SimpleRootedNode createInternalNode(List<? extends Node> children)
          Once a SimpleRootedTree has been created, the node stucture can be created by calling createExternalNode and createInternalNode.
 Node createNodes(RootedTree tree, Node node)
          Clones the entire tree structure from the given RootedTree.
 Node createNodes(Tree tree, Node parent, Node child)
          Clones the entire tree structure from the given (unrooted) Tree.
 List<Node> getAdjacencies(Node node)
          Returns a list of nodes connected to this node by an edge
 Object getAttribute(String name)
           
 Map<String,Object> getAttributeMap()
          Gets the entire attribute map.
 Set<String> getAttributeNames()
           
 List<Node> getChildren(Node node)
           
 Edge getEdge(Node node1, Node node2)
          Returns the Edge that connects these two nodes
 double getEdgeLength(Node node1, Node node2)
          Returns the length of the edge that connects these two nodes
 Set<Edge> getEdges()
           
 List<Edge> getEdges(Node node)
          Returns a list of edges connected to this node
 Set<Edge> getExternalEdges()
          The set of external edges.
 Set<Node> getExternalNodes()
           
 double getHeight(Node node)
           
 Set<Edge> getInternalEdges()
          The set of internal edges.
 Set<Node> getInternalNodes()
           
 double getLength(Node node)
           
 Node getNode(Taxon taxon)
           
 Set<Node> getNodes()
           
 Node[] getNodes(Edge edge)
          Returns an array of 2 nodes which are the nodes at either end of the edge.
 Set<Node> getNodes(int degree)
           
 Node getParent(Node node)
           
 Node getRootNode()
          The root of the tree has the largest node height of all nodes in the tree.
 Set<Taxon> getTaxa()
           
 Taxon getTaxon(Node node)
           
 boolean hasHeights()
           
 boolean hasLengths()
           
 boolean isExternal(Node node)
           
 boolean isRoot(Node node)
           
 void removeAttribute(String name)
           
 void setAttribute(String name, Object value)
          Sets an named attribute for this object.
 void setConceptuallyUnrooted(boolean intent)
           
 void setHeight(Node node, double height)
           
 void setLength(Node node, double length)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rootNode

protected jebl.evolution.trees.SimpleRootedTree.SimpleRootedNode rootNode

internalNodes

protected final Set<Node> internalNodes
Constructor Detail

SimpleRootedTree

public SimpleRootedTree()

SimpleRootedTree

public SimpleRootedTree(RootedTree tree)
Make a copy of the given rooted tree

Parameters:
tree - a rooted tree

SimpleRootedTree

public SimpleRootedTree(Tree tree,
                        Node ingroupNode,
                        Node outgroupNode,
                        double ingroupBranchLength)
                 throws Graph.NoEdgeException
Make a copy of the given unrooted tree

Parameters:
tree - an unrooted tree
ingroupNode - the node on one side of the root
outgroupNode - the node on the other side of the root
ingroupBranchLength - the branch length from the root to the ingroup node
Throws:
Graph.NoEdgeException
Method Detail

createNodes

public Node createNodes(RootedTree tree,
                        Node node)
Clones the entire tree structure from the given RootedTree.

Parameters:
tree -
node -

createNodes

public Node createNodes(Tree tree,
                        Node parent,
                        Node child)
                 throws Graph.NoEdgeException
Clones the entire tree structure from the given (unrooted) Tree.

Parameters:
tree - the unrooted tree
parent - the parent node
child - the child node
Throws:
Graph.NoEdgeException

createExternalNode

public Node createExternalNode(Taxon taxon)
Creates a new external node with the given taxon. See createInternalNode for a description of how to use these methods.

Parameters:
taxon - the taxon associated with this node
Returns:
the created node reference

createInternalNode

public jebl.evolution.trees.SimpleRootedTree.SimpleRootedNode createInternalNode(List<? extends Node> children)
Once a SimpleRootedTree has been created, the node stucture can be created by calling createExternalNode and createInternalNode. First of all createExternalNode is called giving Taxon objects for the external nodes. Then these are put into sets and passed to createInternalNode to create a parent of these nodes. The last node created using createInternalNode is automatically the root so when all the nodes are created, the tree is complete.

Parameters:
children - the child nodes of this nodes
Returns:
the created node reference

setHeight

public void setHeight(Node node,
                      double height)
Parameters:
node - the node whose height is being set
height - the height

setLength

public void setLength(Node node,
                      double length)
Parameters:
node - the node whose branch length (to its parent) is being set
length - the length

getChildren

public List<Node> getChildren(Node node)
Specified by:
getChildren in interface RootedTree
Parameters:
node - the node whose children are being requested.
Returns:
the list of nodes that are the children of the given node. The list may be empty for a terminal node (a tip).

hasHeights

public boolean hasHeights()
Specified by:
hasHeights in interface RootedTree
Returns:
Whether this tree has node heights available

getHeight

public double getHeight(Node node)
Specified by:
getHeight in interface RootedTree
Parameters:
node - the node whose height is being requested.
Returns:
the height of the given node. The height will be less than the parent's height and greater than it children's heights.

hasLengths

public boolean hasLengths()
Specified by:
hasLengths in interface RootedTree
Returns:
Whether this tree has branch lengths available

getLength

public double getLength(Node node)
Specified by:
getLength in interface RootedTree
Parameters:
node - the node whose branch length (to its parent) is being requested.
Returns:
the length of the branch to the parent node (0.0 if the node is the root).

getParent

public Node getParent(Node node)
Specified by:
getParent in interface RootedTree
Parameters:
node - the node whose parent is requested
Returns:
the parent node of the given node, or null if the node is the root node.

getRootNode

public Node getRootNode()
The root of the tree has the largest node height of all nodes in the tree.

Specified by:
getRootNode in interface RootedTree
Returns:
the root of the tree.

getExternalNodes

public Set<Node> getExternalNodes()
Specified by:
getExternalNodes in interface Tree
Returns:
a set of all nodes that have degree 1. These nodes are often refered to as 'tips'.

getInternalNodes

public Set<Node> getInternalNodes()
Specified by:
getInternalNodes in interface Tree
Returns:
a set of all nodes that have degree 2 or more. These nodes are often refered to as internal nodes.

getTaxa

public Set<Taxon> getTaxa()
Specified by:
getTaxa in interface Tree
Returns:
the set of taxa associated with the external nodes of this tree. The size of this set should be the same as the size of the external nodes set.

getTaxon

public Taxon getTaxon(Node node)
Specified by:
getTaxon in interface Tree
Parameters:
node - the node whose associated taxon is being requested.
Returns:
the taxon object associated with the given node, or null if the node is an internal node.

isExternal

public boolean isExternal(Node node)
Specified by:
isExternal in interface Tree
Parameters:
node - the node
Returns:
true if the node is of degree 1.

getNode

public Node getNode(Taxon taxon)
Specified by:
getNode in interface Tree
Parameters:
taxon - the taxon
Returns:
the external node associated with the given taxon, or null if the taxon is not a member of the taxa set associated with this tree.

getEdges

public List<Edge> getEdges(Node node)
Returns a list of edges connected to this node

Specified by:
getEdges in interface Graph
Parameters:
node -
Returns:
the set of nodes that are attached by edges to the given node.

getAdjacencies

public List<Node> getAdjacencies(Node node)
Description copied from interface: Graph
Returns a list of nodes connected to this node by an edge

Specified by:
getAdjacencies in interface Graph
Parameters:
node -
Returns:
the set of nodes that are attached by edges to the given node.

getEdge

public Edge getEdge(Node node1,
                    Node node2)
             throws Graph.NoEdgeException
Returns the Edge that connects these two nodes

Specified by:
getEdge in interface Graph
Parameters:
node1 -
node2 -
Returns:
the edge object.
Throws:
Graph.NoEdgeException - if the nodes are not directly connected by an edge.

getEdgeLength

public double getEdgeLength(Node node1,
                            Node node2)
                     throws Graph.NoEdgeException
Description copied from interface: Graph
Returns the length of the edge that connects these two nodes

Specified by:
getEdgeLength in interface Graph
Parameters:
node1 -
node2 -
Returns:
the length of the edge connecting node1 and node2.
Throws:
Graph.NoEdgeException - if the nodes are not directly connected by an edge.

getNodes

public Node[] getNodes(Edge edge)
Returns an array of 2 nodes which are the nodes at either end of the edge.

Specified by:
getNodes in interface Graph
Parameters:
edge -
Returns:
an array of 2 edges

getNodes

public Set<Node> getNodes()
Specified by:
getNodes in interface Graph
Returns:
the set of all nodes in this graph.

getEdges

public Set<Edge> getEdges()
Specified by:
getEdges in interface Graph
Returns:
the set of all edges in this graph.

getExternalEdges

public Set<Edge> getExternalEdges()
The set of external edges. This is a pretty inefficient implementation because a new set is constructed each time this is called.

Specified by:
getExternalEdges in interface Tree
Returns:
the set of external edges.

getInternalEdges

public Set<Edge> getInternalEdges()
The set of internal edges. This is a pretty inefficient implementation because a new set is constructed each time this is called.

Specified by:
getInternalEdges in interface Tree
Returns:
the set of internal edges.

getNodes

public Set<Node> getNodes(int degree)
Specified by:
getNodes in interface Graph
Parameters:
degree - the number of edges connected to a node
Returns:
a set containing all nodes in this graph of the given degree.

calculateBranchLengths

protected void calculateBranchLengths()
Calculate branch lengths from the current node heights.


setConceptuallyUnrooted

public void setConceptuallyUnrooted(boolean intent)

conceptuallyUnrooted

public boolean conceptuallyUnrooted()
Description copied from interface: RootedTree
Due to current implementation limitations, trees store "branch" information in nodes. So, internally rooted trees are genetrated when un-rooted would be more natural.

Specified by:
conceptuallyUnrooted in interface RootedTree
Returns:
true if tree(s) are to be viewed as unrooted

isRoot

public boolean isRoot(Node node)
Specified by:
isRoot in interface RootedTree
Parameters:
node - the node
Returns:
true if the node is the root of this tree.

setAttribute

public void setAttribute(String name,
                         Object value)
Description copied from interface: Attributable
Sets an named attribute for this object.

Specified by:
setAttribute in interface Attributable
Parameters:
name - the name of the attribute.
value - the new value of the attribute.

getAttribute

public Object getAttribute(String name)
Specified by:
getAttribute in interface Attributable
Parameters:
name - the name of the attribute of interest.
Returns:
an object representing the named attributed for this object.

removeAttribute

public void removeAttribute(String name)
Specified by:
removeAttribute in interface Attributable
Parameters:
name - name of attribute to remove

getAttributeNames

public Set<String> getAttributeNames()
Specified by:
getAttributeNames in interface Attributable
Returns:
an array of the attributeNames that this object has.

getAttributeMap

public Map<String,Object> getAttributeMap()
Description copied from interface: Attributable
Gets the entire attribute map.

Specified by:
getAttributeMap in interface Attributable
Returns:
an unmodifiable map