edu.upenn.crimson
Class Species

java.lang.Object
  extended by edu.upenn.crimson.Species
All Implemented Interfaces:
java.lang.Comparable

public class Species
extends java.lang.Object
implements java.lang.Comparable

A single species object. Species can only exist within a tree and thus the species is added to a tree when it is created. Because species must exist within trees, copy(Tree) is used instead of clone() when duplicating a species for inclusion in a subtree.

Version:
$Id: Species.java,v 1.33 2007/05/16 18:55:58 fisher Exp $

Field Summary
private  int childIndex
          This is used by nextChild() to allow for traversing the tree in a recursive like fashion but without using recursion.
private  Species[] children
          This contains the set of children.
private  java.lang.String id
          The ID of the species.
private  int level
          Level from root to this species.
private  Species parent
          The parent will be null if this Species is the root.
private  double stemLength
          Length from parent to this species (default = 1.0).
private  double tempDepth
          Length from root to this species.
 
Constructor Summary
Species(int id, Tree tree)
           
Species(java.lang.String id, double stemLength, int level, Tree tree)
           
Species(java.lang.String id, Tree tree)
           
 
Method Summary
 void addChild(Species child)
          This will add the child to the set of children and then set the parent of the child to be this Species object.
 boolean collapse()
          This will remove this internal species, linking all children to our parent and adjusting their stem lengths accordingly.
 int compareTo(java.lang.Object obj)
          When comparing Species objects, we actually just compare the index values.
 void computeLevel()
          This will compute the level of the species, assuming the parent's level has already been computed.
 void computeTempDepth()
          This will compute the length to the root, assuming the parent's root length has already been computed.
 Species copy(Tree tree)
          This will return a copy of the current species WITHOUT the parent, children, or level.
 boolean equals(java.lang.Object obj)
          When comparing Species to be equal, we compare their IDs.
 boolean firstChild()
          This will return the true if this is the first child to be returned.
 Species[] getChildren()
           
 java.lang.String getID()
           
 int getLevel()
           
 Species getParent()
           
 java.util.HashSet getSiblings()
          It is assumed that all trees have a single root.
 double getStemLength()
           
 double getTempDepth()
           
 int hashCode()
          We use the hash of the ID as the object's hash code.
 boolean hasNextChild()
          This will return true if there are more children.
 boolean isBinary()
          Returns true if this node is binary.
 boolean isLeaf()
          Returns true if this species is a leaf (ie has no children).
 boolean isRoot()
          Returns true if this species doesn't have a parent.
 Species nextChild()
          This will return the next child keeping track of the children returned.
 int numChildren()
          Returns the number of children.
 java.lang.String printNexus()
          This will print out this species and all children as a NEXUS formatted tree.
 java.lang.String printTree(java.lang.String indent)
          This will print out the tree, one species per line, indenting each level.
 void removeChild(Species child)
          This will remove the child from the set of children.
 void resetChildIndex()
          This resets the index used by nextChild().
 void setID(java.lang.String id)
          The species ID is forced to be upper case.
 void setLevel(int level)
           
 void setParent(Species parent)
           
 void setStemLength(double stemLength)
           
 void setTempDepth_Level(double parentLength, int level)
          This will set the temporal depth and level at the same time.
 void setTempDepth(double tempDepth)
           
 java.lang.String toString()
           
 java.lang.String toStringFull()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

id

private java.lang.String id
The ID of the species. This must be unique for each Species and is case insensitive.


level

private int level
Level from root to this species. Can't rely on level for determining if the species is a root because it's not guarenteed that level values will be computed for a tree.


stemLength

private double stemLength
Length from parent to this species (default = 1.0).


tempDepth

private double tempDepth
Length from root to this species.


parent

private Species parent
The parent will be null if this Species is the root.


children

private Species[] children
This contains the set of children.


childIndex

private int childIndex
This is used by nextChild() to allow for traversing the tree in a recursive like fashion but without using recursion. Recursion with very large trees is prone to memory issues.

Constructor Detail

Species

public Species(int id,
               Tree tree)

Species

public Species(java.lang.String id,
               Tree tree)

Species

public Species(java.lang.String id,
               double stemLength,
               int level,
               Tree tree)
Method Detail

setID

public void setID(java.lang.String id)
The species ID is forced to be upper case.


getID

public java.lang.String getID()

setStemLength

public void setStemLength(double stemLength)

getStemLength

public double getStemLength()

setTempDepth

public void setTempDepth(double tempDepth)

getTempDepth

public double getTempDepth()

setLevel

public void setLevel(int level)

getLevel

public int getLevel()

setParent

public void setParent(Species parent)

getParent

public Species getParent()

getChildren

public Species[] getChildren()

computeTempDepth

public void computeTempDepth()
This will compute the length to the root, assuming the parent's root length has already been computed.


computeLevel

public void computeLevel()
This will compute the level of the species, assuming the parent's level has already been computed.


setTempDepth_Level

public void setTempDepth_Level(double parentLength,
                               int level)
This will set the temporal depth and level at the same time. If both values are needed, this is more efficient than setting each value separately as it will only require propogating through the tree once.


addChild

public void addChild(Species child)
This will add the child to the set of children and then set the parent of the child to be this Species object.


removeChild

public void removeChild(Species child)
This will remove the child from the set of children. Children can only be removed by the Tree, so that the Tree can track the species in the tree.


numChildren

public int numChildren()
Returns the number of children.


getSiblings

public java.util.HashSet getSiblings()
It is assumed that all trees have a single root. Thus the root will not have any siblings. If a species doesn't have a parent, then it is the root and will return an empty HashSet. Otherwise, the siblings are computed from the parent's children.


isLeaf

public boolean isLeaf()
Returns true if this species is a leaf (ie has no children).


isRoot

public boolean isRoot()
Returns true if this species doesn't have a parent. Can't rely on level for determining if the species is a root because it's not guarenteed that level values will be computed for a tree.


isBinary

public boolean isBinary()
Returns true if this node is binary.


hashCode

public int hashCode()
We use the hash of the ID as the object's hash code.

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
When comparing Species to be equal, we compare their IDs.

Overrides:
equals in class java.lang.Object

compareTo

public int compareTo(java.lang.Object obj)
When comparing Species objects, we actually just compare the index values.

Specified by:
compareTo in interface java.lang.Comparable

copy

public Species copy(Tree tree)
This will return a copy of the current species WITHOUT the parent, children, or level. This is meant to be used when coping a species from a tree to be included in a subtree. In this case, the level, parent, and children values are not meaningful. The current species is stored in origSpecies, so that tree sampling routines can connect this subtree to the original tree.


toStringFull

public java.lang.String toStringFull()

toString

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

collapse

public boolean collapse()
This will remove this internal species, linking all children to our parent and adjusting their stem lengths accordingly. If this Species is a leaf, then it won't do anything. It returns true if the species is collapsed and false otherwise. If a species is collapsed, it's up to the calling routine to remove the spacies from the Tree.


resetChildIndex

public void resetChildIndex()
This resets the index used by nextChild(). This must be called before nextChild() is used to traverse a tree.


firstChild

public boolean firstChild()
This will return the true if this is the first child to be returned. It will return false if no children.


hasNextChild

public boolean hasNextChild()
This will return true if there are more children.


nextChild

public Species nextChild()
This will return the next child keeping track of the children returned. This can be used to traverse a tree in a recursive like fashion but without using a recursive routine. The method resetChildIndex() must be called prior to using this routine, to reset the index.


printNexus

public java.lang.String printNexus()
This will print out this species and all children as a NEXUS formatted tree. This species will be the root of the tree.

Notes:
This will give a stack overflow for deep trees.

printTree

public java.lang.String printTree(java.lang.String indent)
This will print out the tree, one species per line, indenting each level.

Notes:
This will give a stack overflow for deep trees.



Copyright 2006 Stephen Fisher, Susan Davidson, and Junhyong Kim, University of Pennsylvania.