JAL-4134 - brutal hack to build a tree clustering columns of PAE Matrix
[jalview.git] / src / jalview / analysis / TreeModel.java
index 62b0594..1725adc 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.analysis;
 
+import jalview.bin.Cache;
 import jalview.datamodel.AlignmentView;
 import jalview.datamodel.BinaryNode;
 import jalview.datamodel.NodeTransformI;
@@ -50,7 +51,7 @@ public class TreeModel
 
   int noseqs;
 
-  SequenceNode top;
+  BinaryNode top;
 
   double maxDistValue;
 
@@ -58,7 +59,7 @@ public class TreeModel
 
   int ycount;
 
-  Vector<SequenceNode> node;
+  Vector<BinaryNode> node;
 
   boolean hasDistances = true; // normal case for jalview trees
 
@@ -77,10 +78,11 @@ public class TreeModel
    * @param treefile
    *          NewickFile
    */
-  public TreeModel(SequenceI[] seqs, AlignmentView odata, NewickFile treefile)
+  public TreeModel(SequenceI[] seqs, AlignmentView odata,
+          NewickFile treefile)
   {
-    this(seqs, treefile.getTree(), treefile.HasDistances(), treefile
-            .HasBootstrap(), treefile.HasRootDistance());
+    this(seqs, (SequenceNode) treefile.getTree(), treefile.HasDistances(),
+            treefile.HasBootstrap(), treefile.HasRootDistance());
     seqData = odata;
 
     associateLeavesToSequences(seqs);
@@ -93,8 +95,9 @@ public class TreeModel
    */
   public TreeModel(TreeBuilder tree)
   {
-    this(tree.getSequences(), tree.getTopNode(), tree.hasDistances(), tree
-            .hasBootstrap(), tree.hasRootDistance());
+    this(tree.getSequences(), (SequenceNode) tree.getTopNode(), tree.hasDistances(),
+            tree.hasBootstrap(), tree.hasRootDistance());
+    seqData = tree.getOriginalData();
   }
 
   /**
@@ -126,7 +129,7 @@ public class TreeModel
   {
     SequenceIdMatcher algnIds = new SequenceIdMatcher(seqs);
 
-    Vector<SequenceNode> leaves = findLeaves(top);
+    Vector<BinaryNode> leaves = findLeaves(top);
 
     int i = 0;
     int namesleft = seqs.length;
@@ -138,7 +141,8 @@ public class TreeModel
     // int countOne2Many = 0;
     while (i < leaves.size())
     {
-      j = leaves.elementAt(i++);
+      // TODO - decide if we get rid of the polymorphism here ?
+      j = (SequenceNode)leaves.elementAt(i++);
       realnam = j.getName();
       nam = null;
 
@@ -153,8 +157,8 @@ public class TreeModel
         if (one2many.contains(nam))
         {
           // countOne2Many++;
-          // if (jalview.bin.Cache.log.isDebugEnabled())
-          // jalview.bin.Cache.log.debug("One 2 many relationship for
+          // if (Cache.isDebugEnabled())
+          // Cache.debug("One 2 many relationship for
           // "+nam.getName());
         }
         else
@@ -169,8 +173,8 @@ public class TreeModel
         j.setPlaceholder(true);
       }
     }
-    // if (jalview.bin.Cache.log.isDebugEnabled() && countOne2Many>0) {
-    // jalview.bin.Cache.log.debug("There were "+countOne2Many+" alignment
+    // if (Cache.isDebugEnabled() && countOne2Many>0) {
+    // Cache.debug("There were "+countOne2Many+" alignment
     // sequence ids (out of "+one2many.size()+" unique ids) linked to two or
     // more leaves.");
     // }
@@ -186,8 +190,12 @@ public class TreeModel
   {
     NewickFile fout = new NewickFile(getTopNode());
 
-    return fout.print(hasBootstrap(), hasDistances(),
-            hasRootDistance()); // output all data available for tree
+    return fout.print(hasBootstrap(), hasDistances(), hasRootDistance()); // output
+                                                                          // all
+                                                                          // data
+                                                                          // available
+                                                                          // for
+                                                                          // tree
   }
 
   /**
@@ -199,7 +207,7 @@ public class TreeModel
    */
   public void updatePlaceHolders(List<SequenceI> list)
   {
-    Vector<SequenceNode> leaves = findLeaves(top);
+    Vector<BinaryNode> leaves = findLeaves(top);
 
     int sz = leaves.size();
     SequenceIdMatcher seqmatcher = null;
@@ -207,7 +215,7 @@ public class TreeModel
 
     while (i < sz)
     {
-      SequenceNode leaf = leaves.elementAt(i++);
+      SequenceNode leaf = (SequenceNode) leaves.elementAt(i++);
 
       if (list.contains(leaf.element()))
       {
@@ -247,8 +255,8 @@ public class TreeModel
           if (!leaf.isPlaceholder())
           {
             // Construct a new placeholder sequence object for this leaf
-            leaf.setElement(new Sequence(leaf.getName(),
-                    "THISISAPLACEHLDER"));
+            leaf.setElement(
+                    new Sequence(leaf.getName(), "THISISAPLACEHLDER"));
           }
           leaf.setPlaceholder(true);
 
@@ -282,15 +290,15 @@ public class TreeModel
   /**
    * Search for leaf nodes below (or at) the given node
    * 
-   * @param nd
+   * @param top2
    *          root node to search from
    * 
    * @return
    */
-  public Vector<SequenceNode> findLeaves(SequenceNode nd)
+  public Vector<BinaryNode> findLeaves(BinaryNode top2)
   {
-    Vector<SequenceNode> leaves = new Vector<SequenceNode>();
-    findLeaves(nd, leaves);
+    Vector<BinaryNode> leaves = new Vector<BinaryNode>();
+    findLeaves(top2, leaves);
     return leaves;
   }
 
@@ -304,8 +312,8 @@ public class TreeModel
    * 
    * @return Vector of leaf nodes on binary tree
    */
-  Vector<SequenceNode> findLeaves(SequenceNode nd,
-          Vector<SequenceNode> leaves)
+  Vector<BinaryNode> findLeaves(BinaryNode nd,
+          Vector<BinaryNode> leaves)
   {
     if (nd == null)
     {
@@ -338,7 +346,7 @@ public class TreeModel
    * @param nd
    *          SequenceNode
    */
-  void printNode(SequenceNode nd)
+  void printNode(BinaryNode nd)
   {
     if (nd == null)
     {
@@ -354,8 +362,8 @@ public class TreeModel
     else
     {
       System.out.println("Dist " + nd.dist);
-      printNode((SequenceNode) nd.left());
-      printNode((SequenceNode) nd.right());
+      printNode((BinaryNode) nd.left());
+      printNode((BinaryNode) nd.right());
     }
   }
 
@@ -381,14 +389,14 @@ public class TreeModel
    * @param threshold
    * @see #getGroups()
    */
-  public List<SequenceNode> groupNodes(float threshold)
+  public List<BinaryNode> groupNodes(float threshold)
   {
-    List<SequenceNode> groups = new ArrayList<SequenceNode>();
+    List<BinaryNode> groups = new ArrayList<BinaryNode>();
     _groupNodes(groups, getTopNode(), threshold);
     return groups;
   }
 
-  protected void _groupNodes(List<SequenceNode> groups, SequenceNode nd,
+  protected void _groupNodes(List<BinaryNode> groups, BinaryNode nd,
           float threshold)
   {
     if (nd == null)
@@ -415,7 +423,7 @@ public class TreeModel
    * 
    * @return DOCUMENT ME!
    */
-  public double findHeight(SequenceNode nd)
+  public double findHeight(BinaryNode nd)
   {
     if (nd == null)
     {
@@ -424,7 +432,7 @@ public class TreeModel
 
     if ((nd.left() == null) && (nd.right() == null))
     {
-      nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
+      nd.height = ((BinaryNode) nd.parent()).height + nd.dist;
 
       if (nd.height > maxheight)
       {
@@ -439,7 +447,7 @@ public class TreeModel
     {
       if (nd.parent() != null)
       {
-        nd.height = ((SequenceNode) nd.parent()).height + nd.dist;
+        nd.height = ((BinaryNode) nd.parent()).height + nd.dist;
       }
       else
       {
@@ -447,44 +455,20 @@ public class TreeModel
         nd.height = (float) 0.0;
       }
 
-      maxheight = findHeight((SequenceNode) (nd.left()));
-      maxheight = findHeight((SequenceNode) (nd.right()));
+      maxheight = findHeight((BinaryNode) (nd.left()));
+      maxheight = findHeight((BinaryNode) (nd.right()));
     }
 
     return maxheight;
   }
 
   /**
-   * Returns original alignment data used for calculation - or null where not
-   * available.
-   * 
-   * @return null or cut'n'pasteable alignment
-   */
-  public String printOriginalSequenceData(char gapChar)
-  {
-    if (seqData == null)
-    {
-      return null;
-    }
-
-    StringBuffer sb = new StringBuffer();
-    String[] seqdatas = seqData.getSequenceStrings(gapChar);
-    for (int i = 0; i < seqdatas.length; i++)
-    {
-      sb.append(new jalview.util.Format("%-" + 15 + "s").form(sequences[i]
-              .getName()));
-      sb.append(" " + seqdatas[i] + "\n");
-    }
-    return sb.toString();
-  }
-
-  /**
    * DOCUMENT ME!
    * 
    * @param nd
    *          DOCUMENT ME!
    */
-  void printN(SequenceNode nd)
+  void printN(BinaryNode nd)
   {
     if (nd == null)
     {
@@ -493,16 +477,16 @@ public class TreeModel
 
     if ((nd.left() != null) && (nd.right() != null))
     {
-      printN((SequenceNode) nd.left());
-      printN((SequenceNode) nd.right());
+      printN((BinaryNode) nd.left());
+      printN((BinaryNode) nd.right());
     }
     else
     {
       System.out.println(" name = " + ((SequenceI) nd.element()).getName());
     }
 
-    System.out.println(" dist = " + nd.dist + " " + nd.count + " "
-            + nd.height);
+    System.out.println(
+            " dist = " + nd.dist + " " + nd.count + " " + nd.height);
   }
 
   /**
@@ -511,7 +495,7 @@ public class TreeModel
    * @param nd
    *          DOCUMENT ME!
    */
-  public void reCount(SequenceNode nd)
+  public void reCount(BinaryNode nd)
   {
     ycount = 0;
     // _lycount = 0;
@@ -527,11 +511,12 @@ public class TreeModel
    * @param nd
    *          DOCUMENT ME!
    */
-  void _reCount(SequenceNode nd)
+  void _reCount(BinaryNode nd)
   {
     // if (_lycount<_lylimit)
     // {
-    // System.err.println("Warning: depth of _recount greater than number of nodes.");
+    // System.err.println("Warning: depth of _recount greater than number of
+    // nodes.");
     // }
     if (nd == null)
     {
@@ -542,11 +527,11 @@ public class TreeModel
     if ((nd.left() != null) && (nd.right() != null))
     {
 
-      _reCount((SequenceNode) nd.left());
-      _reCount((SequenceNode) nd.right());
+      _reCount((BinaryNode) nd.left());
+      _reCount((BinaryNode) nd.right());
 
-      SequenceNode l = (SequenceNode) nd.left();
-      SequenceNode r = (SequenceNode) nd.right();
+      BinaryNode l = (BinaryNode) nd.left();
+      BinaryNode r = (BinaryNode) nd.right();
 
       nd.count = l.count + r.count;
       nd.ycount = (l.ycount + r.ycount) / 2;
@@ -565,14 +550,14 @@ public class TreeModel
    * @param nd
    *          DOCUMENT ME!
    */
-  public void swapNodes(SequenceNode nd)
+  public void swapNodes(BinaryNode nd)
   {
     if (nd == null)
     {
       return;
     }
 
-    SequenceNode tmp = (SequenceNode) nd.left();
+    BinaryNode tmp = (BinaryNode) nd.left();
 
     nd.setLeft(nd.right());
     nd.setRight(tmp);
@@ -586,7 +571,7 @@ public class TreeModel
    * @param dir
    *          DOCUMENT ME!
    */
-  void changeDirection(SequenceNode nd, SequenceNode dir)
+  void changeDirection(BinaryNode nd, BinaryNode dir)
   {
     if (nd == null)
     {
@@ -595,9 +580,9 @@ public class TreeModel
 
     if (nd.parent() != top)
     {
-      changeDirection((SequenceNode) nd.parent(), nd);
+      changeDirection((BinaryNode) nd.parent(), nd);
 
-      SequenceNode tmp = (SequenceNode) nd.parent();
+      BinaryNode tmp = (BinaryNode) nd.parent();
 
       if (dir == nd.left())
       {
@@ -646,7 +631,7 @@ public class TreeModel
    * 
    * @return DOCUMENT ME!
    */
-  public SequenceNode getTopNode()
+  public BinaryNode getTopNode()
   {
     return top;
   }
@@ -681,9 +666,9 @@ public class TreeModel
    */
   public void applyToNodes(NodeTransformI nodeTransformI)
   {
-    for (Enumeration<SequenceNode> nodes = node.elements(); nodes
-            .hasMoreElements(); nodeTransformI.transform(nodes
-            .nextElement()))
+    for (Enumeration<BinaryNode> nodes = node.elements(); nodes
+            .hasMoreElements(); nodeTransformI
+                    .transform(nodes.nextElement()))
     {
       ;
     }