JAL-2844 entirely X coord based partitioning
[jalview.git] / src / jalview / ext / archaeopteryx / JalviewBinding.java
index e339d15..1649b62 100644 (file)
@@ -66,6 +66,10 @@ public final class JalviewBinding
 
   private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
 
+  private float rootX;
+
+  private float furthestNodeX;
+
   /**
    * 
    * @param archaeopteryx
@@ -177,7 +181,9 @@ public final class JalviewBinding
           partitionTree(e.getX());
       }
         PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
-        treeView.repaint();
+
+
+
       }
     });
 
@@ -222,11 +228,22 @@ public final class JalviewBinding
         if (matchingNode != null)
         {
           treeView.getFoundNodes0().add(matchingNode.getId());
+
+
+          if (!matchingNode.getBranchData().isHasBranchColor())
+          {
+            Color foundNodesColour = treeView.getTreeColorSet()
+                    .getFoundColor0();
+          matchingNode.getBranchData()
+                    .setBranchColor(new BranchColor(foundNodesColour));
+
+          }
+
         }
 
       }
 
-
+      treeView.repaint();
     }
 
 
@@ -241,20 +258,25 @@ public final class JalviewBinding
 
     if (!tree.isEmpty())
     {
-      double longestBranch = tree.calculateHeight(true);
-      if (longestBranch != 0)
+      // should be calculated on each partition as the tree can theoretically
+      // change in the meantime
+      PhylogenyNode furthestNode = PhylogenyMethods
+              .calculateNodeWithMaxDistanceToRoot(tree);
+      furthestNodeX = furthestNode.getXcoord();
+      rootX = tree.getRoot().getXcoord();
+
+      if (furthestNodeX != rootX && x < furthestNodeX) // don't bother if 0
+                                                       // distance tree or
+                                                       // clicked x lies outside
+                                                       // of tree
       {
         Graphics g = treeView.getGraphics();
         int panelHeight = treeView.getHeight();
         g.drawLine(x, 0, x, panelHeight);
 
-        // double relativeTreeWidth = longestBranch / viewWidth;
-
-        float rootX = tree.getRoot().getXcoord();
-
-        double threshold = ((double) x - rootX) / longestBranch;
+        float threshold = (x - rootX) / (furthestNodeX - rootX);
         List<PhylogenyNode> foundNodes = getNodesAboveThreshold(threshold,
-                longestBranch, tree.getRoot());
+                tree.getRoot());
 
       }
     }
@@ -263,7 +285,7 @@ public final class JalviewBinding
   }
 
   public List<PhylogenyNode> getNodesAboveThreshold(double threshold,
-          double treeLength, PhylogenyNode node)
+          PhylogenyNode node)
   {
 
     List<PhylogenyNode> nodesAboveThreshold = new ArrayList<>();
@@ -279,7 +301,7 @@ public final class JalviewBinding
     }
 
 
-    colourNodesAboveThreshold(nodesAboveThreshold, threshold, treeLength,
+    colourNodesAboveThreshold(nodesAboveThreshold, threshold,
             node);
     return nodesAboveThreshold;
 
@@ -296,14 +318,15 @@ public final class JalviewBinding
    */
   private List<PhylogenyNode> colourNodesAboveThreshold(
           List<PhylogenyNode> nodeList, double threshold,
-          double treeLength, PhylogenyNode node)
+          PhylogenyNode node)
   {
     // could also use PhylogenyMethods.getAllDescendants
     for (PhylogenyNode childNode : node.getDescendants())
     {
       childNode.getBranchData()
               .setBranchColor(new BranchColor(Color.black));
-      double nodeCutoff = childNode.calculateDistanceToRoot() / treeLength;
+      float nodeCutoff = (childNode.getXcoord() - rootX)
+              / (furthestNodeX - rootX);
 
       if (nodeCutoff > threshold)
       {
@@ -313,16 +336,14 @@ public final class JalviewBinding
                 (int) (Math.random() * 255), (int) (Math.random() * 255));
         TreePanelUtil.colorizeSubtree(childNode,
                 new BranchColor(randomColor));
-
         List<PhylogenyNode> descendantNodes = childNode
                 .getAllExternalDescendants();
-        List<SequenceI> descendantSeqs = new ArrayList<>();
+        List<SequenceI> descendantSeqs = new ArrayList<>(); // .forEach instead?
         for (PhylogenyNode descNode : descendantNodes)
         {
           descendantSeqs.add(nodesBoundToSequences.get(descNode));
         }
 
-
         SequenceGroup sg = new SequenceGroup(descendantSeqs, null, null,
                 true, true, false, 0,
                 parentAvport.getAlignment().getWidth() - 1);
@@ -335,7 +356,6 @@ public final class JalviewBinding
             cs = new UserColourScheme(
                     ((UserColourScheme) parentAvport.getGlobalColourScheme())
                             .getColours());
-
           }
           else
           {
@@ -383,13 +403,14 @@ public final class JalviewBinding
 
 
       }
+
       else
       {
-      colourNodesAboveThreshold(nodeList, threshold, treeLength,
+        colourNodesAboveThreshold(nodeList, threshold,
                 childNode);
       }
-
     }
+
     // GROSS
     ((AlignViewport) parentAvport).getAlignPanel().updateAnnotation();
 
@@ -400,7 +421,6 @@ public final class JalviewBinding
       ((AlignViewport) codingComplement).getAlignPanel().updateAnnotation();
     }
 
-
     return nodeList;
   }