Trees
Class Tree

java.lang.Object
  extended by Trees.Tree
All Implemented Interfaces:
java.lang.Iterable<Branch>

public class Tree
extends java.lang.Object
implements java.lang.Iterable<Branch>

Represents a phylogenetic tree. Trees are defined as a list of Branch. Nodes are defined by a String

Version:
1.0

Constructor Summary
Tree(java.util.List<Branch> branches)
          Creates a tree from a list of branches
Tree(Tree old, Parameters p)
          Duplicates a tree topology while replacing branch lengths using the appropiate parameter
 
Method Summary
static Tree fromFile(java.io.File f)
          Creates a tree from a file containing a Newick string
static Tree fromNewickString(java.lang.String newick)
          Creates a tree from a newick string
 Branch getBranchByChild(java.lang.String child)
          Gets the branch which has the given node as the child node
 java.util.List<Branch> getBranches()
          Gets a list of branches.
 java.util.Set<Branch> getBranchesByParent(java.lang.String parent)
          Gets the set of branches which have the passed node as a parent
 java.util.List<Branch> getBranchesReversed()
          Gets the branches in the reverse order to that returned by getBranches().
 java.util.List<java.lang.String> getInternal()
          Gets a list of internal nodes.
 java.util.Set<java.lang.String> getLeaves()
          Gets a list of leaves
 double getLength()
          Gets the length of the tree
 int getNumberBranches()
          Gets the number of branches
 Parameters getParameters()
          Returns a Parameters object containing a parameter for each branch length.
 Parameters getParametersForEstimation()
          Returns a Parameters object containing a parameter for each branch length.
 java.lang.String getParent(java.lang.String child)
          Gets the parent node of the given node
 java.lang.String getRoot()
          Get the root node
 int getSize()
          Gets the size of the tree, that is the number of taxa
 boolean isExternal(Branch b)
          Tests whether the given branch is an external branch (i.e.
 java.util.Iterator<Branch> iterator()
           
 Tree midPointRoot(java.lang.String newRootName)
          Mid point roots the tree and returns a new tree
 java.lang.String MRCA(java.util.List<java.lang.String> leaves)
          Returns the most recent common ancestor of a set of leaves
 Tree scaledTo(double length)
          Returns a new tree where the lengths are scales so the total length is different
 void toFile(java.io.File f)
          Writes the tree to a file in Newick format.
 void toFile(java.io.File f, boolean nameInternal)
          Write the tree to a file in Newick format.
 java.lang.String toString()
           
 java.lang.String toString(boolean nameInternal)
          Returns a textual representation of the tree in Newick format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Tree

public Tree(java.util.List<Branch> branches)
     throws TreeException
Creates a tree from a list of branches

Parameters:
branches - A list of branches
Throws:
TreeException - Thrown if the list of branches passed do not represent a tree

Tree

public Tree(Tree old,
            Parameters p)
     throws Parameters.ParameterException
Duplicates a tree topology while replacing branch lengths using the appropiate parameter

Parameters:
old - The old tree
p - The parameters used for the new branch lengths
Throws:
Parameters.ParameterException - Thrown if there is a problem with the parameters, e.g. a required parameter not existing.
Method Detail

getBranches

public java.util.List<Branch> getBranches()
Gets a list of branches. Returned in a order that ensures external branches are visited first and then work towars the root.

Returns:
A list of branches

iterator

public java.util.Iterator<Branch> iterator()
Specified by:
iterator in interface java.lang.Iterable<Branch>

getInternal

public java.util.List<java.lang.String> getInternal()
Gets a list of internal nodes. Nodes are returned in such a way that all child nodes are returned before their parent is.

Returns:
A list of internal nodes

getLeaves

public java.util.Set<java.lang.String> getLeaves()
Gets a list of leaves

Returns:
A list of leaves

getRoot

public java.lang.String getRoot()
Get the root node

Returns:
The root node

getLength

public double getLength()
                 throws TreeException
Gets the length of the tree

Returns:
The length of the tree
Throws:
TreeException - Thrown if the tree does not have branch lengths associated with it

getParameters

public Parameters getParameters()
                         throws TreeException
Returns a Parameters object containing a parameter for each branch length. Parameters are fixed at the relevance branch length

Returns:
Parameters object containiing branch lengths
Throws:
TreeException - Thrown if the tree does not have branch lengths

getParametersForEstimation

public Parameters getParametersForEstimation()
Returns a Parameters object containing a parameter for each branch length. Parameters are estimation parameters.

Returns:
Parameters object containiing branch lengths

getSize

public int getSize()
Gets the size of the tree, that is the number of taxa

Returns:
The size of the tree

getNumberBranches

public int getNumberBranches()
Gets the number of branches

Returns:
The number of branches

getBranchByChild

public Branch getBranchByChild(java.lang.String child)
                        throws TreeException
Gets the branch which has the given node as the child node

Parameters:
child - The child node
Returns:
The branch with the given child
Throws:
TreeException - Thrown if the node does not exist or the root node is passed

getBranchesByParent

public java.util.Set<Branch> getBranchesByParent(java.lang.String parent)
                                          throws TreeException
Gets the set of branches which have the passed node as a parent

Parameters:
parent - The parent node
Returns:
The set of banches with the given parent
Throws:
TreeException - If the node does not exist in the tree or is a leaf

getParent

public java.lang.String getParent(java.lang.String child)
                           throws TreeException
Gets the parent node of the given node

Parameters:
child - The child node
Returns:
The parent of the child
Throws:
TreeException - If the node does not exist or is the root

scaledTo

public Tree scaledTo(double length)
              throws TreeException
Returns a new tree where the lengths are scales so the total length is different

Parameters:
length - The new total length
Returns:
The scaled tree
Throws:
TreeException - Thrown if the tree does not have branch lengths

midPointRoot

public Tree midPointRoot(java.lang.String newRootName)
                  throws TreeException
Mid point roots the tree and returns a new tree

Parameters:
newRootName - The name of the new root node
Returns:
A new tree which is midpoint rooted
Throws:
TreeException - If there is a problem with the tree, e.g. no branch lengths

MRCA

public java.lang.String MRCA(java.util.List<java.lang.String> leaves)
                      throws TreeException
Returns the most recent common ancestor of a set of leaves

Parameters:
leaves - Set of leaves to calculate the MRCA for
Returns:
The MRCA
Throws:
TreeException - Thrown if a leave does not exist

isExternal

public boolean isExternal(Branch b)
Tests whether the given branch is an external branch (i.e. the child is a leaf)

Parameters:
b - The branch to test
Returns:
Whether it is an external branch

getBranchesReversed

public java.util.List<Branch> getBranchesReversed()
Gets the branches in the reverse order to that returned by getBranches().

Returns:
The branches in reverse order

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(boolean nameInternal)
Returns a textual representation of the tree in Newick format

Parameters:
nameInternal - Whether internal branches should be named
Returns:
A Newick string representing the tree

toFile

public void toFile(java.io.File f)
            throws OutputException
Writes the tree to a file in Newick format. This will not work correctly if any internal node names have a $ sign as both first and large character. Does not name internal nodes.

Parameters:
f - The file to write the tree to
Throws:
OutputException - Thrown if there is a problem writing the file

toFile

public void toFile(java.io.File f,
                   boolean nameInternal)
            throws OutputException
Write the tree to a file in Newick format. This will not work correctly if any internal node names have a $ sign as both first and large character.

Parameters:
f - The file to write the tree to
nameInternal - Whether internal branches should be named
Throws:
OutputException - Thrown if there is a problem writing the file

fromNewickString

public static Tree fromNewickString(java.lang.String newick)
                             throws TreeException
Creates a tree from a newick string

Parameters:
newick - The newick string
Returns:
The tree
Throws:
TreeException - Thrown if the tree cannot be created (e.g. incorrectly formatted string)

fromFile

public static Tree fromFile(java.io.File f)
                     throws InputException
Creates a tree from a file containing a Newick string

Parameters:
f - The input file
Returns:
The tree
Throws:
InputException - If there is a problem reading from the input file