JAL-2847 various error popups for db querying now have formatted strings
[jalview.git] / src / jalview / ext / archaeopteryx / JalviewBinding.java
index deadfe4..80b2e50 100644 (file)
@@ -1,30 +1,51 @@
 package jalview.ext.archaeopteryx;
 
+import jalview.analysis.AlignmentSorter;
+import jalview.analysis.Conservation;
+import jalview.api.AlignViewportI;
+import jalview.commands.CommandI;
+import jalview.commands.OrderCommand;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.HiddenColumns;
 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;
+import jalview.schemes.ColourSchemeI;
+import jalview.schemes.ColourSchemeProperty;
+import jalview.schemes.UserColourScheme;
 import jalview.structure.SelectionSource;
 import jalview.structure.StructureSelectionManager;
+import jalview.util.MappingUtils;
+import jalview.util.MessageManager;
 import jalview.viewmodel.AlignmentViewport;
 
+import java.awt.Color;
 import java.awt.event.ActionEvent;
 import java.awt.event.InputEvent;
 import java.awt.event.MouseEvent;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.swing.JTabbedPane;
 import javax.swing.SwingUtilities;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
+import javax.swing.event.InternalFrameAdapter;
+import javax.swing.event.InternalFrameEvent;
 
 import org.forester.archaeopteryx.MainFrame;
+import org.forester.phylogeny.Phylogeny;
 import org.forester.phylogeny.PhylogenyMethods;
 import org.forester.phylogeny.PhylogenyNode;
+import org.forester.phylogeny.data.BranchColor;
 
 /**
  * Class for binding the Archaeopteryx tree viewer to the Jalview alignment that
@@ -41,14 +62,24 @@ public final class JalviewBinding
 
   private AlignmentViewport parentAvport;
 
-  private JTabbedPane treeTabs;
+  private final JTabbedPane treeTabs;
 
   private final StructureSelectionManager ssm;
 
+  private AlignmentPanel[] associatedPanels;
+
   private Map<SequenceI, PhylogenyNode> sequencesBoundToNodes;
 
   private Map<PhylogenyNode, SequenceI> nodesBoundToSequences;
 
+  private float rootX;
+
+  private float furthestNodeX;
+
+  private int nrTreeGroups = 0;
+
+  private boolean applyToAllViews = false;
+
   /**
    * 
    * @param archaeopteryx
@@ -69,6 +100,16 @@ public final class JalviewBinding
           final Map<SequenceI, PhylogenyNode> alignMappedToNodes,
           final Map<PhylogenyNode, SequenceI> nodesMappedToAlign)
   {
+
+    if (archaeopteryx.getMainPanel().getTabbedPane().getTabCount() > 1)
+    {
+      JvOptionPane.showMessageDialog(Desktop.desktop,
+              MessageManager.getString("label.tabs_detected_archaeopteryx"),
+              MessageManager.getString("label.problem_reading_tree_file"),
+              JvOptionPane.WARNING_MESSAGE);
+      ;
+    }
+
     // deal with/prohibit null values here as that will cause problems
     parentAvport = jalviewAlignmentViewport;
     sequencesBoundToNodes = alignMappedToNodes;
@@ -77,13 +118,23 @@ public final class JalviewBinding
     treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
     treeTabs = archaeopteryx.getMainPanel().getTabbedPane();
     ssm = parentAvport.getStructureSelectionManager();
-
-   // archaeopteryx.getMainPanel().getControlPanel().setColorBranches(true);
     
     ssm.addSelectionListener(this);
     treeView.addMouseListener(this);
     PaintRefresher.Register(treeView, parentAvport.getSequenceSetId());
+    associatedPanels = PaintRefresher
+            .getAssociatedPanels(parentAvport.getSequenceSetId());
+
+    archaeopteryx.addInternalFrameListener(new InternalFrameAdapter()
+    {
+
+      @Override
+      public void internalFrameClosed(InternalFrameEvent e)
+      {
+        AptxInit.getAllAptxFrames().remove(archaeopteryx);
+      }
 
+    });
 
     treeTabs.addChangeListener(new ChangeListener()
     {
@@ -100,17 +151,20 @@ public final class JalviewBinding
            * Resend the selection to the tree view when tabs get switched, this
            * has to be buried in invokeLater as Forester first resets the tree
            * view on switching tabs, without invokeLater this would get called
-           * before Forester resets which would nullify the selection
+           * before Forester resets which would nullify the selection.
            */
           public void run()
           {
+            treeView = archaeopteryx.getMainPanel().getCurrentTreePanel();
             parentAvport.sendSelection();
+            // PaintRefresher.Refresh(treeView,
+            // parentAvport.getSequenceSetId());
 
           }
         });
 
       }
-      
+
     });
 
   }
@@ -123,14 +177,47 @@ public final class JalviewBinding
   @Override
   public void mouseClicked(MouseEvent e)
   {
+    SwingUtilities.invokeLater(new Runnable() {
+
+      @Override
+      /**
+       * invokeLater so that this always runs after Forester's mouseClicked
+       */
+      public void run()
+      {
+        final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
+        if (node != null)
+        {
+          if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
+          // selection if shift
+          // IS NOT pressed
+          {
+            parentAvport.setSelectionGroup(null);
+
+          }
+          showNodeSelectionOnAlign(node);
+        }
+        else
+        {
+
+          partitionTree(e.getX());
+      }
+        PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
+        treeView.repaint();
+
+
+
+      }
+    });
+
+
   }
 
   @Override
   public void mousePressed(final MouseEvent e)
   {
-    showNodeSelectionOnAlign(e);
-  }
 
+  }
   @Override
   public void mouseReleased(MouseEvent e)
   {
@@ -164,29 +251,247 @@ 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));
+
+          }
+
         }
 
       }
-      PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
 
+      treeView.repaint();
     }
 
 
   }
 
+  /**
+   * Partially refactored from TreeCanvas
+   */
+  public void partitionTree(final int x)
+  {
+    Phylogeny tree = treeView.getPhylogeny();
 
-  @Override
-  public void showNodeSelectionOnAlign(final MouseEvent e)
+    if (!tree.isEmpty())
+    {
+      // 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 < rootX || x > furthestNodeX)) // don't
+                                                                       // bother
+                                                                       // if 0
+                                                       // distance tree or
+                                                       // clicked x lies outside
+                                                       // of tree
+      {
+
+        float threshold = (x - rootX) / (furthestNodeX - rootX);
+        List<PhylogenyNode> foundNodes = getNodesAboveThreshold(threshold,
+                tree.getRoot());
+
+      }
+    }
+
+
+  }
+
+  public List<PhylogenyNode> getNodesAboveThreshold(double threshold,
+          PhylogenyNode node)
   {
-    final PhylogenyNode node = treeView.findNode(e.getX(), e.getY());
-    if (node != null)
+
+    List<PhylogenyNode> nodesAboveThreshold = new ArrayList<>();
+
+    parentAvport.setSelectionGroup(null);
+    parentAvport.getAlignment().deleteAllGroups();
+    parentAvport.clearSequenceColours();
+    if (parentAvport.getCodingComplement() != null)
     {
-      if ((e.getModifiers() & InputEvent.SHIFT_MASK) == 0) // clear previous
-                                                           // selection if shift
-                                                           // IS NOT pressed
+      parentAvport.getCodingComplement().setSelectionGroup(null);
+      parentAvport.getCodingComplement().getAlignment().deleteAllGroups();
+      parentAvport.getCodingComplement().clearSequenceColours();
+    }
+
+
+    colourNodesAboveThreshold(nodesAboveThreshold, threshold,
+            node);
+    return nodesAboveThreshold;
+
+  }
+
+  /**
+   * Partially refactored from TreeCanvas colourGroups (can be made nicer).
+   * 
+   * @param nodeList
+   * @param threshold
+   * @param treeLength
+   * @param node
+   * @return
+   */
+  private List<PhylogenyNode> colourNodesAboveThreshold(
+          List<PhylogenyNode> nodeList, double threshold,
+          PhylogenyNode node)
+  {
+
+    for (PhylogenyNode childNode : node.getDescendants())
+    {
+      childNode.getBranchData()
+              .setBranchColor(new BranchColor(Color.black));
+      float nodeCutoff = (childNode.getXcoord() - rootX)
+              / (furthestNodeX - rootX);
+
+      if (nodeCutoff > threshold)
       {
-        parentAvport.setSelectionGroup(null);
+        nodeList.add(childNode);
+
+        Color randomColour = new Color((int) (Math.random() * 255),
+                (int) (Math.random() * 255), (int) (Math.random() * 255));
+        childNode.getBranchData()
+                .setBranchColor(new BranchColor(randomColour));
+
+        List<SequenceI> groupSeqs = new ArrayList<>();
+        SequenceI seq = nodesBoundToSequences.get(childNode);
+        if (seq != null)
+        {
+          groupSeqs.add(seq);
+          parentAvport.setSequenceColour(seq, randomColour);
+        }
+
+        List<PhylogenyNode> descendantNodes = PhylogenyMethods
+                .getAllDescendants(childNode);
+        // .forEach instead?
+        for (PhylogenyNode descNode : descendantNodes)
+        {
+          seq = nodesBoundToSequences.get(descNode);
+          if (seq != null)
+          {
+            groupSeqs.add(seq);
+            parentAvport.setSequenceColour(seq, randomColour);
+          }
+
+          descNode.getBranchData()
+                  .setBranchColor(new BranchColor(randomColour));
+        }
+
+        if (groupSeqs != null)
+        {
+          nrTreeGroups++;
+          groupThresholdSequences(groupSeqs, randomColour);
+        }}
+
+      else
+      {
+        colourNodesAboveThreshold(nodeList, threshold, childNode);
+      }
+    }
+
+
+        for (AlignmentPanel associatedPanel : associatedPanels) {
+
+        associatedPanel.updateAnnotation();
+
+        final AlignViewportI codingComplement = associatedPanel.getAlignViewport()
+                .getCodingComplement();
+        if (codingComplement != null)
+        {
+          // 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
+      {
+        cs = ColourSchemeProperty.getColourScheme(treeGroup,
+                ColourSchemeProperty.getColourName(
+                        parentAvport.getGlobalColourScheme()));
+      }
+
+    }
+    treeGroup.setColourScheme(cs);
+    treeGroup.getGroupColourScheme().setThreshold(
+            parentAvport.getResidueShading().getThreshold(),
+            parentAvport.isIgnoreGapsConsensus());
+
+    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());
+          }
+        }
+      }
+
+    }
+
+  }
+
+  /**
+   * may or may not need an extra repaint on the alignment view (check what kira
+   * does)
+   */
+  @Override
+  public void showNodeSelectionOnAlign(final PhylogenyNode node)
+  {
 
       if (node.isInternal())
       {
@@ -198,8 +503,9 @@ public final class JalviewBinding
         showMatchingSequence(node);
       }
 
+
     }
-  }
+
 
 
 
@@ -210,9 +516,11 @@ public final class JalviewBinding
     SequenceI matchingSequence = nodesBoundToSequences.get(nodeToMatch);
     if (matchingSequence != null)
     {
+      long nodeId = nodeToMatch.getId();
+      addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
       treeSelectionChanged(matchingSequence);
       parentAvport.sendSelection();
-      PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
+
     }
   }
 
@@ -222,6 +530,7 @@ public final class JalviewBinding
     List<PhylogenyNode> childNodes = PhylogenyMethods
             .getAllDescendants(parentNode);
 
+
     for (PhylogenyNode childNode : childNodes)
     {
       // childNode.getBranchData().setBranchColor(new BranchColor(Color.BLUE));
@@ -229,12 +538,17 @@ public final class JalviewBinding
       SequenceI matchingSequence = nodesBoundToSequences.get(childNode);
       if (matchingSequence != null)
       {
+        long nodeId = childNode.getId();
+        addOrRemoveInSet(treeView.getFoundNodes0(), nodeId);
+
         treeSelectionChanged(matchingSequence);
 
       }
+
     }
     parentAvport.sendSelection();
-    PaintRefresher.Refresh(treeView, parentAvport.getSequenceSetId());
+
+
   }
 
   /**
@@ -246,16 +560,115 @@ public final class JalviewBinding
   @Override
   public void treeSelectionChanged(final SequenceI sequence)
   {
-    SequenceGroup selected = parentAvport.getSelectionGroup();
-
-    if (selected == null)
+    if (!parentAvport.isClosed()) // alignment view could be closed
     {
-      selected = new SequenceGroup();
-      parentAvport.setSelectionGroup(selected);
+      SequenceGroup selected = parentAvport.getSelectionGroup();
+
+      if (selected == null)
+      {
+        selected = new SequenceGroup();
+        parentAvport.setSelectionGroup(selected);
+      }
+
+      selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
+        selected.addOrRemove(sequence, true);
     }
 
-    selected.setEndRes(parentAvport.getAlignment().getWidth() - 1);
-    selected.addOrRemove(sequence, true);
+  }
+
+  @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.av.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(av.getSequenceSetId()))
+    // {
+    // // ensure all the alignFrames refresh their GI after adding an undo item
+    // ap.alignFrame.updateEditMenuBar();
+    // }
+    // }
+    // else
+    // {
+    // treeCanvas.ap.alignFrame
+    // .addHistoryItem(sortAlignmentIn(treeCanvas.ap));
+    // }
+
+  }
+
+  public CommandI sortAlignmentIn(AlignmentPanel ap)
+  {
+    // TODO: move to alignment view controller
+    AlignmentViewport viewport = ap.av;
+    SequenceI[] oldOrder = viewport.getAlignment().getSequencesArray();
+    AlignmentSorter.sortByTree(viewport.getAlignment(),
+            treeView.getPhylogeny());
+    CommandI undo;
+    undo = new OrderCommand("Tree Sort", oldOrder, viewport.getAlignment());
+
+    ap.paintAlignment(true, false);
+    return undo;
+  }
+  
+
+
+  /**
+   * TO BE MOVED
+   * 
+   * @param set
+   * @param objectToCheck
+   */
+  public static <E> void addOrRemoveInSet(Set<E> set, E objectToCheck)
+  {
+    if (set.contains(objectToCheck))
+    {
+      set.remove(objectToCheck);
+    }
+    else
+    {
+      set.add(objectToCheck);
+    }
 
   }
 
@@ -268,6 +681,17 @@ public final class JalviewBinding
   {
     this.parentAvport = parentAvport;
   }
+
+  public AlignmentPanel[] getAssociatedPanels()
+  {
+    return associatedPanels;
+  }
+
+  public void setAssociatedPanels(AlignmentPanel[] associatedPanels)
+  {
+    this.associatedPanels = associatedPanels;
+  }
+
 }