public class Tree extends java.lang.Object implements java.lang.Iterable<Branch>
Branch
.
Nodes are defined by a String. This entire class deals with rooted and unrooted
trees the same. Traditionally a rooted tree is detected by a node of degree two
but if we allow multifurcations at the root then a rooted tree need not have any
degree two nodes. Users should be aware of the type of tree they're using and
any consequences.Constructor and Description |
---|
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 appropriate parameter
|
Modifier and Type | Method and Description |
---|---|
double |
branchScore(Tree t)
Calculates the branch score distance between this tree and another tree
|
boolean |
equals(java.lang.Object o) |
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.List<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
|
java.util.Set<Split> |
getSplits()
Gets the splits that represent the tree
|
int |
hashCode() |
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.
|
static Tree |
randomTree(java.util.List<java.lang.String> taxa,
boolean rooted)
Creates a random rooted or unrooted tree from a list of taxa
|
int |
RF(Tree t)
Calculates the RF distance between this tree and another tree
|
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.
|
void |
toFilePAML(java.io.File f)
Writes the tree to a file with taxa names limited to 25 characters
for use in PAML.
|
java.lang.String |
toString() |
java.lang.String |
toString(boolean nameInternal)
Returns a textual representation of the tree in Newick format
|
double |
weightedRF(Tree t)
Calculates the weighted (by branch length) RF distance between this tree and another tree
|
public Tree(java.util.List<Branch> branches) throws TreeException
branches
- A list of branchesTreeException
- Thrown if the list of branches passed do not represent
a treepublic Tree(Tree old, Parameters p) throws Parameters.ParameterException
old
- The old treep
- The parameters used for the new branch lengthsParameters.ParameterException
- Thrown if there is a problem
with the parameters, e.g. a required parameter not existing.public java.util.List<Branch> getBranches()
public java.util.Iterator<Branch> iterator()
iterator
in interface java.lang.Iterable<Branch>
public java.util.List<java.lang.String> getInternal()
public java.util.List<java.lang.String> getLeaves()
public java.lang.String getRoot()
public double getLength() throws TreeException
TreeException
- Thrown if the tree does not have branch lengths
associated with itpublic Parameters getParameters() throws TreeException
TreeException
- Thrown if the tree does not have branch lengthspublic Parameters getParametersForEstimation()
public int getSize()
public int getNumberBranches()
public Branch getBranchByChild(java.lang.String child) throws TreeException
child
- The child nodeTreeException
- Thrown if the node does not exist or the root node
is passedpublic java.util.Set<Branch> getBranchesByParent(java.lang.String parent) throws TreeException
parent
- The parent nodeTreeException
- If the node does not exist in the tree or is a leafpublic java.lang.String getParent(java.lang.String child) throws TreeException
child
- The child nodeTreeException
- If the node does not exist or is the rootpublic Tree scaledTo(double length) throws TreeException
length
- The new total lengthTreeException
- Thrown if the tree does not have branch lengthspublic Tree midPointRoot(java.lang.String newRootName) throws TreeException
newRootName
- The name of the new root nodeTreeException
- If there is a problem with the tree, e.g. no branch
lengthspublic java.lang.String MRCA(java.util.List<java.lang.String> leaves) throws TreeException
leaves
- Set of leaves to calculate the MRCA forTreeException
- Thrown if a leave does not existpublic boolean isExternal(Branch b)
b
- The branch to testpublic java.util.List<Branch> getBranchesReversed()
getBranches()
.public java.util.Set<Split> getSplits() throws TreeException
TreeException
- If the tree is invalidpublic int RF(Tree t) throws TreeException
t
- The other treeTreeException
- If one tree or the other is not validpublic double weightedRF(Tree t) throws TreeException
t
- The other treeTreeException
- If one tree or the other is not validpublic double branchScore(Tree t) throws TreeException
t
- The other treeTreeException
- If one tree or the other is not validpublic java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(boolean nameInternal)
nameInternal
- Whether internal branches should be namedpublic void toFile(java.io.File f) throws OutputException
f
- The file to write the tree toOutputException
- Thrown if there is a problem writing the filepublic void toFile(java.io.File f, boolean nameInternal) throws OutputException
f
- The file to write the tree tonameInternal
- Whether internal branches should be namedOutputException
- Thrown if there is a problem writing the filepublic void toFilePAML(java.io.File f) throws OutputException
f
- The file to write the tree toOutputException
- Thrown if there is a problem writing the filepublic boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public static Tree fromNewickString(java.lang.String newick) throws TreeException
newick
- The newick stringTreeException
- Thrown if the tree cannot be created (e.g. incorrectly
formatted string)public static Tree fromFile(java.io.File f) throws InputException
f
- The input fileInputException
- If there is a problem reading from the input filepublic static Tree randomTree(java.util.List<java.lang.String> taxa, boolean rooted) throws TreeException
taxa
- A list of taxa to include in the treerooted
- Whether the tree should be rootedTreeException
- Thrown if there are too through taxa passed and so
there are no trees to select a random tree from.