JAL-4134 support recovery of mapped positions for raw matrix column index in Mappable...
[jalview.git] / src / jalview / gui / TreeCanvas.java
index 8de161f..9463634 100755 (executable)
@@ -68,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!
@@ -1032,12 +1033,28 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
         // 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())
         {
-          Color gpcol = colors.get(gp);
-          for (int p = gp.nextSetBit(0); p >= 0; p = gp.nextSetBit(p + 1))
+          gpcol = colors.get(gp);
+          for (int p = gp.nextSetBit(0); p >= 0 && p < Integer.MAX_VALUE; p = gp.nextSetBit(p + 1))
           {
-            Annotation ae = aa.getAnnotationForPosition(p+sstart);
+            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();
@@ -1124,37 +1141,60 @@ public class TreeCanvas extends JPanel implements MouseListener, Runnable,
           Color col)
   {
     SequenceI rseq = tp.assocAnnotation.sequenceRef;
-    if (av==null||av.getAlignment()==null)
+    
+    if (av == null || av.getAlignment() == null)
     {
       // alignment is closed
       return;
     }
 
-    for (BinaryNode bn:l)
+    for (BinaryNode bn : l)
     {
-      int colm=-1;
-      try {
-        colm = Integer.parseInt(bn.getName().substring(bn.getName().indexOf("c")+1));
+      int colm = -1;
+      try
+      {
+        colm = Integer.parseInt(
+                bn.getName().substring(bn.getName().indexOf("c") + 1));
       } catch (Exception e)
       {
         continue;
       }
+      // 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)
       {
-        int offp = (rseq!=null) ? rseq.findIndex(rseq.getStart()+colm) : colm;
-        
-        if (!av.hasHiddenColumns() || hc.isVisible(offp-1))
-        { 
-          if (cs.contains(offp-1))
-          {
-            cs.removeElement(offp-1);
-          } else {
-            cs.addElement(offp-1);
-          }
+        mcm = (MappableContactMatrixI) cm;
+        int[] seqpos = mcm.getMappedPositionsFor(tp.assocAnnotation.sequenceRef,colm);
+        if (seqpos==null)
+        {
+          // no mapping for this column.
+          continue;
+        }
+        // TODO: handle ranges...
+        offp=seqpos[0];
+      }
+      else
+      {
+        offp = (rseq != null) ? rseq.findIndex(rseq.getStart() + colm)
+                : colm;
+      }
+      if (!av.hasHiddenColumns() || hc.isVisible(offp - 1))
+      {
+        if (cs.contains(offp - 1))
+        {
+          cs.removeElement(offp - 1);
+        }
+        else
+        {
+          cs.addElement(offp - 1);
         }
       }
-    } 
+    }
   }
 
   public void createSeqGroupFor(AlignmentPanel[] aps, Vector<BinaryNode> l,