*/
public class NJTree
{
- Vector cluster;
+ Vector<Cluster> cluster;
SequenceI[] sequence;
float rj;
- Vector groups = new Vector();
+ Vector<SequenceNode> groups = new Vector<SequenceNode>();
SequenceNode maxdist;
int ycount;
- Vector node;
+ Vector<SequenceNode> node;
String type;
Object found = null;
- Object leaves = null;
-
boolean hasDistances = true; // normal case for jalview trees
boolean hasBootstrap = false; // normal case for jalview trees
SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
- Vector leaves = new Vector();
- findLeaves(top, leaves);
+ Vector<SequenceNode> leaves = findLeaves(top);
int i = 0;
int namesleft = seqs.length;
SequenceNode j;
SequenceI nam;
String realnam;
- Vector one2many = new Vector();
+ Vector<SequenceI> one2many = new Vector<SequenceI>();
int countOne2Many = 0;
while (i < leaves.size())
{
- j = (SequenceNode) leaves.elementAt(i++);
+ j = leaves.elementAt(i++);
realnam = j.getName();
nam = null;
String pwtype, ScoreModelI sm, int start, int end)
{
this.sequence = sequence;
- this.node = new Vector();
+ this.node = new Vector<SequenceNode>();
this.type = type;
this.pwtype = pwtype;
if (seqData != null)
*
* @return Newick File with all tree data available
*/
+ @Override
public String toString()
{
jalview.io.NewickFile fout = new jalview.io.NewickFile(getTopNode());
*/
public void UpdatePlaceHolders(List<SequenceI> list)
{
- Vector leaves = new Vector();
- findLeaves(top, leaves);
+ Vector<SequenceNode> leaves = findLeaves(top);
int sz = leaves.size();
SequenceIdMatcher seqmatcher = null;
while (i < sz)
{
- SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);
+ SequenceNode leaf = leaves.elementAt(i++);
if (list.contains(leaf.element()))
{
{
@Override
- public void transform(BinaryNode node)
+ public void transform(BinaryNode nd)
{
- Object el = node.element();
+ Object el = nd.element();
if (el != null && el instanceof SequenceI)
{
- node.setName(((SequenceI) el).getName());
+ nd.setName(((SequenceI) el).getName());
}
}
});
}
joinClusters(one, two);
- top = (SequenceNode) (node.elementAt(one));
+ top = (node.elementAt(one));
reCount(top);
findHeight(top);
{
float dist = distance[i][j];
- int noi = ((Cluster) cluster.elementAt(i)).value.length;
- int noj = ((Cluster) cluster.elementAt(j)).value.length;
+ int noi = cluster.elementAt(i).value.length;
+ int noj = cluster.elementAt(j).value.length;
int[] value = new int[noi + noj];
for (int ii = 0; ii < noi; ii++)
{
- value[ii] = ((Cluster) cluster.elementAt(i)).value[ii];
+ value[ii] = cluster.elementAt(i).value[ii];
}
for (int ii = noi; ii < (noi + noj); ii++)
{
- value[ii] = ((Cluster) cluster.elementAt(j)).value[ii - noi];
+ value[ii] = cluster.elementAt(j).value[ii - noi];
}
Cluster c = new Cluster(value);
SequenceNode sn = new SequenceNode();
- sn.setLeft((SequenceNode) (node.elementAt(i)));
- sn.setRight((SequenceNode) (node.elementAt(j)));
+ sn.setLeft((node.elementAt(i)));
+ sn.setRight((node.elementAt(j)));
- SequenceNode tmpi = (SequenceNode) (node.elementAt(i));
- SequenceNode tmpj = (SequenceNode) (node.elementAt(j));
+ SequenceNode tmpi = (node.elementAt(i));
+ SequenceNode tmpj = (node.elementAt(j));
if (type.equals("NJ"))
{
*/
public void findClusterDistance(int i, int j)
{
- int noi = ((Cluster) cluster.elementAt(i)).value.length;
- int noj = ((Cluster) cluster.elementAt(j)).value.length;
+ int noi = cluster.elementAt(i).value.length;
+ int noj = cluster.elementAt(j).value.length;
// New distances from cluster to others
float[] newdist = new float[noseqs];
public float[][] findDistances(ScoreModelI _pwmatrix)
{
- float[][] distance = new float[noseqs][noseqs];
+ float[][] dist = new float[noseqs][noseqs];
if (_pwmatrix == null)
{
// Resolve substitution model
_pwmatrix = ResidueProperties.getScoreMatrix("BLOSUM62");
}
}
- distance = _pwmatrix.findDistances(seqData);
- return distance;
+ dist = _pwmatrix.findDistances(seqData);
+ return dist;
}
*/
public void makeLeaves()
{
- cluster = new Vector();
+ cluster = new Vector<Cluster>();
for (int i = 0; i < noseqs; i++)
{
}
/**
+ * Search for leaf nodes below (or at) the given node
+ *
+ * @param nd
+ * root node to search from
+ *
+ * @return
+ */
+ public Vector<SequenceNode> findLeaves(SequenceNode nd)
+ {
+ Vector<SequenceNode> leaves = new Vector<SequenceNode>();
+ findLeaves(nd, leaves);
+ return leaves;
+ }
+
+ /**
* Search for leaf nodes.
*
- * @param node
+ * @param nd
* root node to search from
* @param leaves
* Vector of leaves to add leaf node objects too.
*
* @return Vector of leaf nodes on binary tree
*/
- public Vector findLeaves(SequenceNode node, Vector leaves)
+ Vector<SequenceNode> findLeaves(SequenceNode nd,
+ Vector<SequenceNode> leaves)
{
- if (node == null)
+ if (nd == null)
{
return leaves;
}
- if ((node.left() == null) && (node.right() == null)) // Interior node
+ if ((nd.left() == null) && (nd.right() == null)) // Interior node
// detection
{
- leaves.addElement(node);
+ leaves.addElement(nd);
return leaves;
}
* TODO: Identify internal nodes... if (node.isSequenceLabel()) {
* leaves.addElement(node); }
*/
- findLeaves((SequenceNode) node.left(), leaves);
- findLeaves((SequenceNode) node.right(), leaves);
+ findLeaves((SequenceNode) nd.left(), leaves);
+ findLeaves((SequenceNode) nd.right(), leaves);
}
return leaves;
/**
* Find the leaf node with a particular ycount
*
- * @param node
+ * @param nd
* initial point on tree to search from
* @param count
* value to search for
*
* @return null or the node with ycound=count
*/
- public Object findLeaf(SequenceNode node, int count)
+ public Object findLeaf(SequenceNode nd, int count)
{
- found = _findLeaf(node, count);
+ found = _findLeaf(nd, count);
return found;
}
/*
* #see findLeaf(SequenceNode node, count)
*/
- public Object _findLeaf(SequenceNode node, int count)
+ public Object _findLeaf(SequenceNode nd, int count)
{
- if (node == null)
+ if (nd == null)
{
return null;
}
- if (node.ycount == count)
+ if (nd.ycount == count)
{
- found = node.element();
+ found = nd.element();
return found;
}
else
{
- _findLeaf((SequenceNode) node.left(), count);
- _findLeaf((SequenceNode) node.right(), count);
+ _findLeaf((SequenceNode) nd.left(), count);
+ _findLeaf((SequenceNode) nd.right(), count);
}
return found;
/**
* printNode is mainly for debugging purposes.
*
- * @param node
+ * @param nd
* SequenceNode
*/
- public void printNode(SequenceNode node)
+ public void printNode(SequenceNode nd)
{
- if (node == null)
+ if (nd == null)
{
return;
}
- if ((node.left() == null) && (node.right() == null))
+ if ((nd.left() == null) && (nd.right() == null))
{
System.out
- .println("Leaf = " + ((SequenceI) node.element()).getName());
- System.out.println("Dist " + node.dist);
- System.out.println("Boot " + node.getBootstrap());
+ .println("Leaf = " + ((SequenceI) nd.element()).getName());
+ System.out.println("Dist " + nd.dist);
+ System.out.println("Boot " + nd.getBootstrap());
}
else
{
- System.out.println("Dist " + node.dist);
- printNode((SequenceNode) node.left());
- printNode((SequenceNode) node.right());
+ System.out.println("Dist " + nd.dist);
+ printNode((SequenceNode) nd.left());
+ printNode((SequenceNode) nd.right());
}
}
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
*/
- public void findMaxDist(SequenceNode node)
+ public void findMaxDist(SequenceNode nd)
{
- if (node == null)
+ if (nd == null)
{
return;
}
- if ((node.left() == null) && (node.right() == null))
+ if ((nd.left() == null) && (nd.right() == null))
{
- float dist = node.dist;
+ float dist = nd.dist;
if (dist > maxDistValue)
{
- maxdist = node;
+ maxdist = nd;
maxDistValue = dist;
}
}
else
{
- findMaxDist((SequenceNode) node.left());
- findMaxDist((SequenceNode) node.right());
+ findMaxDist((SequenceNode) nd.left());
+ findMaxDist((SequenceNode) nd.right());
}
}
*
* @return DOCUMENT ME!
*/
- public Vector getGroups()
+ public Vector<SequenceNode> getGroups()
{
return groups;
}
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
* @param threshold
* DOCUMENT ME!
*/
- public void groupNodes(SequenceNode node, float threshold)
+ public void groupNodes(SequenceNode nd, float threshold)
{
- if (node == null)
+ if (nd == null)
{
return;
}
- if ((node.height / maxheight) > threshold)
+ if ((nd.height / maxheight) > threshold)
{
- groups.addElement(node);
+ groups.addElement(nd);
}
else
{
- groupNodes((SequenceNode) node.left(), threshold);
- groupNodes((SequenceNode) node.right(), threshold);
+ groupNodes((SequenceNode) nd.left(), threshold);
+ groupNodes((SequenceNode) nd.right(), threshold);
}
}
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
- public float findHeight(SequenceNode node)
+ public float findHeight(SequenceNode nd)
{
- if (node == null)
+ if (nd == null)
{
return maxheight;
}
- if ((node.left() == null) && (node.right() == null))
+ if ((nd.left() == null) && (nd.right() == null))
{
- node.height = ((SequenceNode) node.parent()).height + node.dist;
+ nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
- if (node.height > maxheight)
+ if (nd.height > maxheight)
{
- return node.height;
+ return nd.height;
}
else
{
}
else
{
- if (node.parent() != null)
+ if (nd.parent() != null)
{
- node.height = ((SequenceNode) node.parent()).height + node.dist;
+ nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
}
else
{
maxheight = 0;
- node.height = (float) 0.0;
+ nd.height = (float) 0.0;
}
- maxheight = findHeight((SequenceNode) (node.left()));
- maxheight = findHeight((SequenceNode) (node.right()));
+ maxheight = findHeight((SequenceNode) (nd.left()));
+ maxheight = findHeight((SequenceNode) (nd.right()));
}
return maxheight;
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
*/
- public void printN(SequenceNode node)
+ public void printN(SequenceNode nd)
{
- if (node == null)
+ if (nd == null)
{
return;
}
- if ((node.left() != null) && (node.right() != null))
+ if ((nd.left() != null) && (nd.right() != null))
{
- printN((SequenceNode) node.left());
- printN((SequenceNode) node.right());
+ printN((SequenceNode) nd.left());
+ printN((SequenceNode) nd.right());
}
else
{
System.out.println(" name = "
- + ((SequenceI) node.element()).getName());
+ + ((SequenceI) nd.element()).getName());
}
- System.out.println(" dist = " + node.dist + " " + node.count + " "
- + node.height);
+ System.out.println(" dist = " + nd.dist + " " + nd.count + " "
+ + nd.height);
}
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
*/
- public void reCount(SequenceNode node)
+ public void reCount(SequenceNode nd)
{
ycount = 0;
_lycount = 0;
// _lylimit = this.node.size();
- _reCount(node);
+ _reCount(nd);
}
private long _lycount = 0, _lylimit = 0;
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
*/
- public void _reCount(SequenceNode node)
+ public void _reCount(SequenceNode nd)
{
// if (_lycount<_lylimit)
// {
// System.err.println("Warning: depth of _recount greater than number of nodes.");
// }
- if (node == null)
+ if (nd == null)
{
return;
}
_lycount++;
- if ((node.left() != null) && (node.right() != null))
+ if ((nd.left() != null) && (nd.right() != null))
{
- _reCount((SequenceNode) node.left());
- _reCount((SequenceNode) node.right());
+ _reCount((SequenceNode) nd.left());
+ _reCount((SequenceNode) nd.right());
- SequenceNode l = (SequenceNode) node.left();
- SequenceNode r = (SequenceNode) node.right();
+ SequenceNode l = (SequenceNode) nd.left();
+ SequenceNode r = (SequenceNode) nd.right();
- node.count = l.count + r.count;
- node.ycount = (l.ycount + r.ycount) / 2;
+ nd.count = l.count + r.count;
+ nd.ycount = (l.ycount + r.ycount) / 2;
}
else
{
- node.count = 1;
- node.ycount = ycount++;
+ nd.count = 1;
+ nd.ycount = ycount++;
}
_lycount--;
}
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
*/
- public void swapNodes(SequenceNode node)
+ public void swapNodes(SequenceNode nd)
{
- if (node == null)
+ if (nd == null)
{
return;
}
- SequenceNode tmp = (SequenceNode) node.left();
+ SequenceNode tmp = (SequenceNode) nd.left();
- node.setLeft(node.right());
- node.setRight(tmp);
+ nd.setLeft(nd.right());
+ nd.setRight(tmp);
}
/**
* DOCUMENT ME!
*
- * @param node
+ * @param nd
* DOCUMENT ME!
* @param dir
* DOCUMENT ME!
*/
- public void changeDirection(SequenceNode node, SequenceNode dir)
+ public void changeDirection(SequenceNode nd, SequenceNode dir)
{
- if (node == null)
+ if (nd == null)
{
return;
}
- if (node.parent() != top)
+ if (nd.parent() != top)
{
- changeDirection((SequenceNode) node.parent(), node);
+ changeDirection((SequenceNode) nd.parent(), nd);
- SequenceNode tmp = (SequenceNode) node.parent();
+ SequenceNode tmp = (SequenceNode) nd.parent();
- if (dir == node.left())
+ if (dir == nd.left())
{
- node.setParent(dir);
- node.setLeft(tmp);
+ nd.setParent(dir);
+ nd.setLeft(tmp);
}
- else if (dir == node.right())
+ else if (dir == nd.right())
{
- node.setParent(dir);
- node.setRight(tmp);
+ nd.setParent(dir);
+ nd.setRight(tmp);
}
}
else
{
- if (dir == node.left())
+ if (dir == nd.left())
{
- node.setParent(node.left());
+ nd.setParent(nd.left());
- if (top.left() == node)
+ if (top.left() == nd)
{
- node.setRight(top.right());
+ nd.setRight(top.right());
}
else
{
- node.setRight(top.left());
+ nd.setRight(top.left());
}
}
else
{
- node.setParent(node.right());
+ nd.setParent(nd.right());
- if (top.left() == node)
+ if (top.left() == nd)
{
- node.setLeft(top.right());
+ nd.setLeft(top.right());
}
else
{
- node.setLeft(top.left());
+ nd.setLeft(top.left());
}
}
}
*/
public void applyToNodes(NodeTransformI nodeTransformI)
{
- for (Enumeration nodes = node.elements(); nodes.hasMoreElements(); nodeTransformI
- .transform((BinaryNode) nodes.nextElement()))
+ for (Enumeration<SequenceNode> nodes = node.elements(); nodes
+ .hasMoreElements(); nodeTransformI
+ .transform(nodes.nextElement()))
{
;
}
tree.findHeight(tree.getTopNode());
// Now have to calculate longest name based on the leaves
- Vector leaves = tree.findLeaves(tree.getTopNode(), new Vector());
+ Vector<SequenceNode> leaves = tree.findLeaves(tree.getTopNode());
boolean has_placeholders = false;
longestName = "";
for (int i = 0; i < leaves.size(); i++)
{
- SequenceNode lf = (SequenceNode) leaves.elementAt(i);
+ SequenceNode lf = leaves.elementAt(i);
if (lf.isPlaceholder())
{
}
/**
- * DOCUMENT ME!
+ * Empty method to satisfy the MouseListener interface
*
* @param e
- * DOCUMENT ME!
*/
@Override
public void mouseReleased(MouseEvent e)
}
/**
- * DOCUMENT ME!
+ * Empty method to satisfy the MouseListener interface
*
* @param e
- * DOCUMENT ME!
*/
@Override
public void mouseEntered(MouseEvent e)
}
/**
- * DOCUMENT ME!
+ * Empty method to satisfy the MouseListener interface
*
* @param e
- * DOCUMENT ME!
*/
@Override
public void mouseExited(MouseEvent e)
}
/**
- * DOCUMENT ME!
+ * Handles a mouse click on a tree node (clicks elsewhere are handled in
+ * mousePressed). Click selects the sub-tree, double-click swaps leaf nodes
+ * order, right-click opens a dialogue to choose colour for the sub-tree.
*
* @param e
- * DOCUMENT ME!
*/
@Override
public void mouseClicked(MouseEvent evt)
}
else
{
- Vector leaves = new Vector();
- tree.findLeaves(highlightNode, leaves);
+ Vector<SequenceNode> leaves = tree.findLeaves(highlightNode);
for (int i = 0; i < leaves.size(); i++)
{
- SequenceI seq = (SequenceI) ((SequenceNode) leaves.elementAt(i))
+ SequenceI seq = (SequenceI) leaves.elementAt(i)
.element();
treeSelectionChanged(seq);
}
}
/**
- * DOCUMENT ME!
+ * Handles a mouse press on a sequence name or the tree background canvas
+ * (click on a node is handled in mouseClicked). The action is to create
+ * groups by partitioning the tree at the mouse position. Colours for the
+ * groups (and sequence names) are generated randomly.
*
* @param e
- * DOCUMENT ME!
*/
@Override
public void mousePressed(MouseEvent e)
/*
* defer right-click handling on Windows to
- * mouseClicked; this also matches Cmd-click on Mac
- * which should do nothing here
+ * mouseClicked; note isRightMouseButton
+ * also matches Cmd-click on Mac which should do
+ * nothing here
*/
if (SwingUtilities.isRightMouseButton(e))
{
(int) (Math.random() * 255), (int) (Math.random() * 255));
setColor((SequenceNode) tree.getGroups().elementAt(i), col.brighter());
- Vector l = tree.findLeaves(
- (SequenceNode) tree.getGroups().elementAt(i), new Vector());
+ Vector<SequenceNode> l = tree.findLeaves((SequenceNode) tree
+ .getGroups().elementAt(i));
- Vector sequences = new Vector();
+ Vector<SequenceI> sequences = new Vector<SequenceI>();
for (int j = 0; j < l.size(); j++)
{
- SequenceI s1 = (SequenceI) ((SequenceNode) l.elementAt(j))
+ SequenceI s1 = (SequenceI) l.elementAt(j)
.element();
if (!sequences.contains(s1))