JAL-2844 rearranging code
authorkjvdheide <kjvanderheide@dundee.ac.uk>
Sun, 3 Dec 2017 14:56:38 +0000 (14:56 +0000)
committerkjvdheide <kjvanderheide@dundee.ac.uk>
Sun, 3 Dec 2017 14:56:38 +0000 (14:56 +0000)
src/jalview/ext/archaeopteryx/JalviewBinding.java

index 21e53a2..84f4347 100644 (file)
@@ -8,6 +8,7 @@ import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.ext.treeviewer.ExternalTreeViewerBindingI;
 import jalview.gui.AlignViewport;
+import jalview.gui.AlignmentPanel;
 import jalview.gui.Desktop;
 import jalview.gui.JvOptionPane;
 import jalview.gui.PaintRefresher;
@@ -21,7 +22,6 @@ import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.Color;
-import java.awt.Graphics;
 import java.awt.event.ActionEvent;
 import java.awt.event.InputEvent;
 import java.awt.event.MouseEvent;
@@ -37,7 +37,6 @@ import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
 import org.forester.archaeopteryx.MainFrame;
-import org.forester.archaeopteryx.TreePanelUtil;
 import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
@@ -62,6 +61,8 @@ public final class JalviewBinding
 
   private final StructureSelectionManager ssm;
 
+  private AlignmentPanel[] associatedPanels;
+
   private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
 
   private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
@@ -70,6 +71,8 @@ public final class JalviewBinding
 
   private float furthestNodeX;
 
+  private int nrTreeGroups = 0;
+
   /**
    * 
    * @param archaeopteryx
@@ -113,6 +116,9 @@ public final class JalviewBinding
     treeView.addMouseListener(this);
     PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
 
+    associatedPanels = PaintRefresher
+            .getAssociatedPanels(parentAvport.getSequenceSetId());
+
 
     treeTabs.addChangeListener(new ChangeListener()
     {
@@ -181,6 +187,7 @@ public final class JalviewBinding
           partitionTree(e.getX());
       }
         PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
+        treeView.repaint();
 
 
 
@@ -272,9 +279,9 @@ public final class JalviewBinding
                                                        // clicked x lies outside
                                                        // of tree
       {
-        Graphics g = treeView.getGraphics();
-        int panelHeight = treeView.getHeight();
-        g.drawLine(x, 0, x, panelHeight);
+
+        // int panelHeight = treeView.getHeight();
+        // g.drawLine(x, 0, x, panelHeight);
 
         float threshold = (x - rootX) / (furthestNodeX - rootX);
         List<PhylogenyNode> foundNodes = getNodesAboveThreshold(threshold,
@@ -322,7 +329,7 @@ public final class JalviewBinding
           List<PhylogenyNode> nodeList, double threshold,
           PhylogenyNode node)
   {
-    // could also use PhylogenyMethods.getAllDescendants
+
     for (PhylogenyNode childNode : node.getDescendants())
     {
       childNode.getBranchData()
@@ -334,132 +341,136 @@ public final class JalviewBinding
       {
         nodeList.add(childNode);
 
-        Color randomColor = new Color((int) (Math.random() * 255),
+        Color randomColour = new Color((int) (Math.random() * 255),
                 (int) (Math.random() * 255), (int) (Math.random() * 255));
-        TreePanelUtil.colorizeSubtree(childNode,
-                new BranchColor(randomColor));
-        List<PhylogenyNode> descendantNodes = childNode
-                .getAllExternalDescendants();
-        List<SequenceI> descendantSeqs = new ArrayList<>(); // .forEach instead?
-        for (PhylogenyNode descNode : descendantNodes)
+        childNode.getBranchData()
+                .setBranchColor(new BranchColor(randomColour));
+
+        List<SequenceI> groupSeqs = new ArrayList<>();
+        SequenceI seq = nodesBoundToSequences.get(childNode);
+        if (seq != null)
         {
-          descendantSeqs.add(nodesBoundToSequences.get(descNode));
+          groupSeqs.add(seq);
+          parentAvport.setSequenceColour(seq, randomColour);
         }
 
-        SequenceGroup sg = new SequenceGroup(descendantSeqs, null, null,
-                true, true, false, 0,
-                parentAvport.getAlignment().getWidth() - 1);
-
-        ColourSchemeI cs = null;
-        if (parentAvport.getGlobalColourScheme() != null)
+        List<PhylogenyNode> descendantNodes = PhylogenyMethods
+                .getAllDescendants(childNode);
+        // .forEach instead?
+        for (PhylogenyNode descNode : descendantNodes)
         {
-          if (parentAvport.getGlobalColourScheme() instanceof UserColourScheme)
-          {
-            cs = new UserColourScheme(
-                    ((UserColourScheme) parentAvport.getGlobalColourScheme())
-                            .getColours());
-          }
-          else
+          seq = nodesBoundToSequences.get(descNode);
+          if (seq != null)
           {
-            cs = ColourSchemeProperty.getColourScheme(sg, ColourSchemeProperty
-                    .getColourName(parentAvport.getGlobalColourScheme()));
+            groupSeqs.add(seq);
+            parentAvport.setSequenceColour(seq, randomColour);
           }
+
+          descNode.getBranchData()
+                  .setBranchColor(new BranchColor(randomColour));
         }
-        sg.setColourScheme(cs);
-        sg.getGroupColourScheme().setThreshold(
-                parentAvport.getResidueShading().getThreshold(),
-                parentAvport.isIgnoreGapsConsensus());
-        // sg.recalcConservation();
-        sg.setName("Tree Group:" + sg.hashCode());
-        sg.setIdColour(randomColor);
-
-        if (parentAvport.getGlobalColourScheme() != null
-                && parentAvport.getResidueShading().conservationApplied())
+
+        if (groupSeqs != null)
         {
-          Conservation c = new Conservation("Group", sg.getSequences(null),
-                  sg.getStartRes(), sg.getEndRes());
-          c.calculate();
-          c.verdict(false, parentAvport.getConsPercGaps());
-          sg.cs.setConservation(c);
-        }
+          nrTreeGroups++;
+          groupThresholdSequences(groupSeqs, randomColour);
+        }}
 
-        parentAvport.getAlignment().addGroup(new SequenceGroup(sg));
-        // TODO can we push all of the below into AlignViewportI?
-        final AlignViewportI codingComplement = parentAvport
+      else
+      {
+        colourNodesAboveThreshold(nodeList, threshold, childNode);
+      }
+    }
+
+
+        for (AlignmentPanel associatedPanel : associatedPanels) {
+
+        associatedPanel.updateAnnotation();
+
+        final AlignViewportI codingComplement = associatedPanel.getAlignViewport()
                 .getCodingComplement();
         if (codingComplement != null)
         {
-          SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(sg,
-                  parentAvport,
-                  codingComplement);
-          if (mappedGroup.getSequences().size() > 0)
-          {
-            codingComplement.getAlignment().addGroup(mappedGroup);
-            for (SequenceI seq : mappedGroup.getSequences())
-            {
-              codingComplement.setSequenceColour(seq,
-                      randomColor.brighter());
-            }
-          }
+          // GROSS
+          ((AlignViewport) codingComplement).getAlignPanel()
+                  .updateAnnotation();
         }
+      }
 
 
-      }
+    return nodeList;
+  }
 
+  public void groupThresholdSequences(List<SequenceI> groupedSeqs,
+          Color groupColour)
+  {
+    SequenceGroup treeGroup = new SequenceGroup(groupedSeqs, null, null,
+            true, true, false, 0,
+            parentAvport.getAlignment().getWidth() - 1);
+
+    ColourSchemeI cs = null;
+    if (parentAvport.getGlobalColourScheme() != null)
+    {
+      if (parentAvport.getGlobalColourScheme() instanceof UserColourScheme)
+      {
+        cs = new UserColourScheme(
+                ((UserColourScheme) parentAvport.getGlobalColourScheme())
+                        .getColours());
+      }
       else
       {
-        colourNodesAboveThreshold(nodeList, threshold,
-                childNode);
+        cs = ColourSchemeProperty.getColourScheme(treeGroup,
+                ColourSchemeProperty.getColourName(
+                        parentAvport.getGlobalColourScheme()));
       }
-    }
-
-    // GROSS
-    ((AlignViewport) parentAvport).getAlignPanel().updateAnnotation();
 
-    final AlignViewportI codingComplement = parentAvport
-            .getCodingComplement();
-    if (codingComplement != null)
-    {
-      ((AlignViewport) codingComplement).getAlignPanel().updateAnnotation();
     }
+    treeGroup.setColourScheme(cs);
+    treeGroup.getGroupColourScheme().setThreshold(
+            parentAvport.getResidueShading().getThreshold(),
+            parentAvport.isIgnoreGapsConsensus());
 
-    return nodeList;
-  }
+    treeGroup.setName("Tree Group " + nrTreeGroups);
+    treeGroup.setIdColour(groupColour);
 
+    for (AlignmentPanel associatedPanel : associatedPanels)
+    {
+      AlignViewportI altViewport = associatedPanel
+              .getAlignViewport();
 
+      if (altViewport.getGlobalColourScheme() != null
+              && altViewport.getResidueShading()
+                      .conservationApplied())
+      {
+        Conservation conserv = new Conservation(treeGroup.getName(),
+                treeGroup.getSequences(null), treeGroup.getStartRes(),
+                treeGroup.getEndRes());
+        conserv.calculate();
+        conserv.verdict(false, altViewport.getConsPercGaps());
+        treeGroup.getGroupColourScheme().setConservation(conserv);
+      }
 
+      altViewport.getAlignment().addGroup(treeGroup);
+      // TODO can we push all of the below into AlignViewportI?
+      final AlignViewportI codingComplement = altViewport
+              .getCodingComplement();
+      if (codingComplement != null)
+      {
+        SequenceGroup mappedGroup = MappingUtils.mapSequenceGroup(treeGroup,
+                parentAvport, codingComplement);
+        if (mappedGroup.getSequences().size() > 0)
+        {
+          codingComplement.getAlignment().addGroup(mappedGroup);
+          for (SequenceI seq : mappedGroup.getSequences())
+          {
+            codingComplement.setSequenceColour(seq, groupColour.brighter());
+          }
+        }
+      }
 
-  // public List<PhylogenyNode> groupNodes(float threshold, PhylogenyNode root,
-  // double treeHeight)
-  // {
-  // List<PhylogenyNode> groups = new ArrayList<>();
-  // _groupNodes(groups, root, threshold, treeHeight);
-  // System.out.println(groups);
-  // return groups;
-  // }
-  //
-  // protected void _groupNodes(List<PhylogenyNode> groups, PhylogenyNode nd,
-  // float threshold, double treeHeight)
-  // {
-  // if (nd == null)
-  // {
-  // return;
-  // }
-  //
-  // if ((nd.calculateDistanceToRoot() / treeHeight) > threshold)
-  // {
-  // groups.add(nd);
-  // }
-  // else
-  // {
-  // for (PhylogenyNode childNode : nd.getDescendants())
-  // {
-  // _groupNodes(groups, childNode, threshold, treeHeight);
-  // }
-  // }
-  // }
-  //
+    }
 
+  }
 
   /**
    * may or may not need an extra repaint on the alignment view (check what kira
@@ -552,79 +563,10 @@ public final class JalviewBinding
 
   }
   public void sortByTree_actionPerformed() {
-    // parentAvport.mirrorCommand(command, undo, ssm, source);
 
-    // alignFrame
-    // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
-    
   }
   
 
-  /**
-   * sort the associated alignment view by the current tree.
-   * 
-   * @param e
-   */
-  // @Override
-  // public void sortByTree_actionPerformed()// modify for Aptx
-  // {
-  //
-  // // if (treeCanvas.applyToAllViews)
-  //
-  // final ArrayList<CommandI> commands = new ArrayList<>();
-  // for (AlignmentPanel ap : PaintRefresher
-  // .getAssociatedPanels(parentAvport.getSequenceSetId()))
-  // {
-  // commands.add(sortAlignmentIn(ap.parentAvport.getAlignPanel()));
-  // }
-  // parentAvport.getAlignPanel().alignFrame.addHistoryItem(new CommandI()
-  // {
-  //
-  // @Override
-  // public void undoCommand(AlignmentI[] views)
-  // {
-  // for (CommandI tsort : commands)
-  // {
-  // tsort.undoCommand(views);
-  // }
-  // }
-  //
-  // @Override
-  // public int getSize()
-  // {
-  // return commands.size();
-  // }
-  //
-  // @Override
-  // public String getDescription()
-  // {
-  // return "Tree Sort (many views)";
-  // }
-  //
-  // @Override
-  // public void doCommand(AlignmentI[] views)
-  // {
-  //
-  // for (CommandI tsort : commands)
-  // {
-  // tsort.doCommand(views);
-  // }
-  // }
-  // });
-  // for (AlignmentPanel ap : PaintRefresher
-  // .getAssociatedPanels(parentAvport.getSequenceSetId()))
-  // {
-  // // ensure all the alignFrames refresh their GI after adding an undo item
-  // ap.alignFrame.updateEditMenuBar();
-  // }
-  // }
-  // else
-  // {
-  // treeCanvas.ap.alignFrame
-  // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
-  // }
-
-
 
   /**
    * TO BE MOVED
@@ -654,6 +596,17 @@ public final class JalviewBinding
   {
     this.parentAvport = parentAvport;
   }
+
+  public AlignmentPanel[] getAssociatedPanels()
+  {
+    return associatedPanels;
+  }
+
+  public void setAssociatedPanels(AlignmentPanel[] associatedPanels)
+  {
+    this.associatedPanels = associatedPanels;
+  }
+
 }