JAL-4134 repaint the tree or alignment view(s) when column selections change
[jalview.git] / src / jalview / gui / TreeCanvas.java
index 3a055ae..2bdfc27 100755 (executable)
@@ -54,6 +54,7 @@ import jalview.analysis.Conservation;
 import jalview.analysis.TreeModel;
 import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.Annotation;
 import jalview.datamodel.BinaryNode;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.ContactMatrixI;
@@ -67,6 +68,7 @@ import jalview.schemes.ColourSchemeI;
 import jalview.structure.SelectionSource;
 import jalview.util.Format;
 import jalview.util.MessageManager;
+import jalview.ws.datamodel.MappableContactMatrixI;
 
 /**
  * DOCUMENT ME!
@@ -141,9 +143,43 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
     scrollPane = scroller;
     addMouseListener(this);
     addMouseMotionListener(this);
+    
     ToolTipManager.sharedInstance().registerComponent(this);
   }
 
+  public void clearSelectedLeaves()
+  {
+    Vector<BinaryNode> leaves = tp.getTree()
+            .findLeaves(tp.getTree().getTopNode());
+    if (tp.isColumnWise())
+    {
+      markColumnsFor(getAssociatedPanels(), leaves, Color.white, true);
+    }
+    else
+    {
+      for (AlignmentPanel ap : getAssociatedPanels())
+      {
+        SequenceGroup selected = ap.av.getSelectionGroup();
+        if (selected != null)
+        {
+          {
+            for (int i = 0; i < leaves.size(); i++)
+            {
+              SequenceI seq = (SequenceI) leaves.elementAt(i).element();
+              if (selected.contains(seq))
+              {
+                selected.addOrRemove(seq, false);
+              }
+            }
+            selected.recalcConservation();
+          }
+        }
+        ap.av.sendSelection();
+      }
+    }
+    PaintRefresher.Refresh(tp, av.getSequenceSetId());
+    repaint();
+  }
   /**
    * DOCUMENT ME!
    * 
@@ -828,7 +864,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
       Vector<BinaryNode> leaves = tree.findLeaves(highlightNode);
       if (tp.isColumnWise())
       {
-        markColumnsFor(getAssociatedPanels(), leaves, Color.red);
+        markColumnsFor(getAssociatedPanels(), leaves, Color.red,false);
       }
       else
       {
@@ -1035,6 +1071,41 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
             cm.setColorForGroup(gp, colors.get(gp));
           }
         }
+        // stash colors in linked annotation row.
+        // doesn't work yet. TESTS!
+        int sstart = aa.sequenceRef != null ? aa.sequenceRef.getStart() - 1
+                : 0;
+        Annotation ae;
+        Color gpcol = null;
+        int[] seqpos = null;
+        for (BitSet gp : colors.keySet())
+        {
+          gpcol = colors.get(gp);
+          for (int p = gp.nextSetBit(0); p >= 0
+                  && p < Integer.MAX_VALUE; p = gp.nextSetBit(p + 1))
+          {
+            if (cm instanceof MappableContactMatrixI)
+            {
+              MappableContactMatrixI mcm = (MappableContactMatrixI) cm;
+              seqpos = mcm.getMappedPositionsFor(aa.sequenceRef, p);
+              if (seqpos == null)
+              {
+                // no mapping for this column.
+                continue;
+              }
+              // TODO: handle ranges...
+              ae = aa.getAnnotationForPosition(seqpos[0]);
+            }
+            else
+            {
+              ae = aa.getAnnotationForPosition(p + sstart);
+            }
+            if (ae != null)
+            {
+              ae.colour = gpcol.brighter().darker();
+            }
+          }
+        }
       }
     }
 
@@ -1071,23 +1142,39 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
       return false;
     }
     ColumnSelection cs = av.getColumnSelection();
-
     HiddenColumns hc = av.getAlignment().getHiddenColumns();
-    int offp = (rseq != null) ? rseq.findIndex(rseq.getStart() + colm)
-            : colm;
+    AlignmentAnnotation aa = tp.getAssocAnnotation();
+    int offp=-1;
+    if (aa != null)
+    {
+      ContactMatrixI cm = av.getContactMatrix(aa);
+      if (cm instanceof MappableContactMatrixI)
+      {
+        MappableContactMatrixI mcm = (MappableContactMatrixI) cm;
+        int pos[]=mcm.getMappedPositionsFor(rseq, colm+1);
+        if (pos!=null)
+        {
+          offp=rseq.findIndex(pos[0]);
+        }
+      }
+    }
+    if (offp<=0)
+    {
+      return false;
+    }
 
+    offp-=2;
     if (!av.hasHiddenColumns())
     {
-      return cs.contains(offp - 1);
+      return cs.contains(offp);
     }
-    if (hc.isVisible(offp - 1))
+    if (hc.isVisible(offp))
     {
-      return cs.contains(offp - 1);
+      return cs.contains(offp);
       // return cs.contains(hc.absoluteToVisibleColumn(offp));
     }
     return false;
   }
-
   private BitSet createColumnGroupFor(Vector<BinaryNode> l, Color col)
   {
     BitSet gp = new BitSet();
@@ -1116,7 +1203,7 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
   }
 
   private void markColumnsFor(AlignmentPanel[] aps, Vector<BinaryNode> l,
-          Color col)
+          Color col, boolean clearSelected)
   {
     SequenceI rseq = tp.assocAnnotation.sequenceRef;
     if (av == null || av.getAlignment() == null)
@@ -1125,6 +1212,16 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
       return;
     }
 
+    // TODO - sort indices for faster lookup
+    ColumnSelection cs = av.getColumnSelection();
+    HiddenColumns hc = av.getAlignment().getHiddenColumns();
+    ContactMatrixI cm = av.getContactMatrix(tp.assocAnnotation);
+    MappableContactMatrixI mcm = null;
+    int offp;
+    if (cm instanceof MappableContactMatrixI)
+    {
+      mcm = (MappableContactMatrixI) cm;
+    }
     for (BinaryNode bn : l)
     {
       int colm = -1;
@@ -1136,25 +1233,36 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
       {
         continue;
       }
-      ColumnSelection cs = av.getColumnSelection();
-      HiddenColumns hc = av.getAlignment().getHiddenColumns();
+      if (mcm!=null)
+      {
+        int[] seqpos = mcm.getMappedPositionsFor(
+                tp.assocAnnotation.sequenceRef, colm);
+        if (seqpos == null)
+        {
+          // no mapping for this column.
+          continue;
+        }
+        // TODO: handle ranges...
+        offp = seqpos[0]-1;
+      }
+      else
       {
-        int offp = (rseq != null) ? rseq.findIndex(rseq.getStart() + colm)
+        offp = (rseq != null) ? rseq.findIndex(rseq.getStart() + colm)
                 : colm;
-
-        if (!av.hasHiddenColumns() || hc.isVisible(offp - 1))
+      }
+      if (!av.hasHiddenColumns() || hc.isVisible(offp))
+      {
+        if (clearSelected || cs.contains(offp))
         {
-          if (cs.contains(offp - 1))
-          {
-            cs.removeElement(offp - 1);
-          }
-          else
-          {
-            cs.addElement(offp - 1);
-          }
+          cs.removeElement(offp);
+        }
+        else
+        {
+          cs.addElement(offp);
         }
       }
     }
+    PaintRefresher.Refresh(tp, av.getSequenceSetId());
   }
 
   public void createSeqGroupFor(AlignmentPanel[] aps, Vector<BinaryNode> l,