JAL-2349 store/restore mappable contact matrix in project and fix up interactive...
[jalview.git] / src / jalview / gui / AnnotationPanel.java
index 3bf24b4..2177a26 100755 (executable)
@@ -656,7 +656,7 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
       to = Math.max(cXci.cStart, cXci.cEnd);
 
       // double click selects the whole group
-      if (evt.getClickCount()==2)
+      if (evt.getClickCount() == 2)
       {
         ContactMatrixI matrix = av.getContactMatrix(clicked);
 
@@ -667,6 +667,7 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
           {
             SequenceI rseq = clicked.sequenceRef;
             BitSet grp = matrix.getGroupsFor(currentX);
+            // TODO: cXci needs to be mapped to real groups
             for (int c = fr; c <= to; c++)
             {
               BitSet additionalGrp = matrix.getGroupsFor(c);
@@ -697,16 +698,18 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
       {
         // select corresponding range in segment under mouse
         {
-          for (int c = fr; c <= to; c++)
+          int[] rng = forCurrentX.getMappedPositionsFor(fr, to);
+          if (rng != null)
           {
-            av.getColumnSelection().addElement(c);
+            av.getColumnSelection().addRangeOfElements(rng, true);
           }
           av.getColumnSelection().addElement(currentX);
         }
         // PAE SPECIFIC
         // and also select everything lower than the max range adjacent
         // (kind of works)
-        if (evt.isControlDown() && PAEContactMatrix.PAEMATRIX.equals(clicked.getCalcId()))
+        if (evt.isControlDown()
+                && PAEContactMatrix.PAEMATRIX.equals(clicked.getCalcId()))
         {
           int c = fr - 1;
           ContactRange cr = forCurrentX.getRangeFor(fr, to);
@@ -724,28 +727,39 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
             if (// cr.getMin() <= cval &&
             cval <= thresh)
             {
-              av.getColumnSelection().addElement(c--);
-            }
-            else
-            {
-              break;
+              int[] cols = forCurrentX.getMappedPositionsFor(c, c);
+              if (cols != null)
+              {
+                av.getColumnSelection().addRangeOfElements(cols, true);
+              }
+              else
+              {
+                break;
+              }
             }
+            c--;
           }
-          c = to;
-          while (c < forCurrentX.getContactHeight())
-          {
-            cval = forCurrentX.getContactAt(c);
-            if (// cr.getMin() <= cval &&
-            cval <= thresh)
+            c = to;
+            while (c < forCurrentX.getContactHeight())
             {
-              av.getColumnSelection().addElement(c++);
-            }
-            else
-            {
-              break;
+              cval = forCurrentX.getContactAt(c);
+              if (// cr.getMin() <= cval &&
+              cval <= thresh)
+              {
+                int[] cols = forCurrentX.getMappedPositionsFor(c, c);
+                if (cols != null)
+                {
+                  av.getColumnSelection().addRangeOfElements(cols, true);
+                }
+              }
+              else
+              {
+                break;
+              }
+              c++;
+
             }
           }
-        }
       }
     }
     ap.paintAlignment(false, false);
@@ -1044,38 +1058,56 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
               rowIndex[1] - deltaY);
 
       // mark rectangular region formed by drag
-      jalview.bin.Console.trace("Matrix Selection from last(" + fromXc + ",["
-              + lastXci.cStart + "," + lastXci.cEnd + "]) to cur(" + toXc
-              + ",[" + cXci.cStart + "," + cXci.cEnd + "])");
+      jalview.bin.Console.trace("Matrix Selection from last(" + fromXc
+              + ",[" + lastXci.cStart + "," + lastXci.cEnd + "]) to cur("
+              + toXc + ",[" + cXci.cStart + "," + cXci.cEnd + "])");
       int fr, to;
       fr = Math.min(lastXci.cStart, lastXci.cEnd);
       to = Math.max(lastXci.cStart, lastXci.cEnd);
-      jalview.bin.Console.trace("Marking " + fr + " to " + to);
-      for (int c = fr; c <= to; c++)
+      int[] mappedPos = forFromX.getMappedPositionsFor(fr, to);
+      if (mappedPos != null)
       {
-        if (cma.sequenceRef != null)
+        jalview.bin.Console.trace("Marking " + fr + " to " + to
+                + " mapping to sequence positions " + mappedPos[0] + " to "
+                + mappedPos[1]);
+        for (int pair = 0; pair < mappedPos.length; pair += 2)
         {
-          int col = cma.sequenceRef.findIndex(c);
-          av.getColumnSelection().addElement(col);
-        }
-        else
-        {
-          av.getColumnSelection().addElement(c);
+          for (int c = mappedPos[pair]; c <= mappedPos[pair + 1]; c++)
+//          {
+//            if (cma.sequenceRef != null)
+//            {
+//              int col = cma.sequenceRef.findIndex(cma.sequenceRef.getStart()+c);
+//              av.getColumnSelection().addElement(col);
+//            }
+//            else
+            {
+              av.getColumnSelection().addElement(c);
+            }
         }
       }
+      // and again for most recent corner of drag
       fr = Math.min(cXci.cStart, cXci.cEnd);
       to = Math.max(cXci.cStart, cXci.cEnd);
-      jalview.bin.Console.trace("Marking " + fr + " to " + to);
-      for (int c = fr; c <= to; c++)
+      mappedPos = forFromX.getMappedPositionsFor(fr, to);
+      if (mappedPos != null)
       {
-        if (cma.sequenceRef != null)
+        for (int pair = 0; pair < mappedPos.length; pair += 2)
         {
-          int col = cma.sequenceRef.findIndex(c);
-          av.getColumnSelection().addElement(col);
-        }
-        else
-        {
-          av.getColumnSelection().addElement(c);
+          jalview.bin.Console.trace("Marking " + fr + " to " + to
+                  + " mapping to sequence positions " + mappedPos[pair] + " to "
+                  + mappedPos[pair+1]);
+          for (int c = mappedPos[pair]; c <= mappedPos[pair + 1]; c++)
+          {
+//            if (cma.sequenceRef != null)
+//            {
+//              int col = cma.sequenceRef.findIndex(cma.sequenceRef.getStart()+c);
+//              av.getColumnSelection().addElement(col);
+//            }
+//            else
+            {
+              av.getColumnSelection().addElement(c);
+            }
+          }
         }
       }
       fr = Math.min(lastX, currentX);
@@ -1248,12 +1280,28 @@ public class AnnotationPanel extends JPanel implements AwtRenderPanelI,
         ContactRange cr = clist.getRangeFor(ci.cStart, ci.cEnd);
         tooltip = "Contact from " + clist.getPosition() + ", [" + ci.cStart
                 + " - " + ci.cEnd + "]" + "<br/>Mean:" + cr.getMean();
+        
         int col = ann.sequenceRef.findPosition(column);
+        int[][] highlightPos;
+        int[] mappedPos = clist.getMappedPositionsFor(ci.cStart, ci.cEnd);
+        if (mappedPos != null)
+        {
+          highlightPos = new int[1 + mappedPos.length][2];
+          highlightPos[0] = new int[] { col, col };
+          for (int p = 0, h = 0; p < mappedPos.length; h++, p += 2)
+          {
+            highlightPos[h][0] = ann.sequenceRef
+                    .findPosition(mappedPos[p] - 1);
+            highlightPos[h][1] = ann.sequenceRef
+                    .findPosition(mappedPos[p + 1] - 1);
+          }
+        }
+        else
+        {
+          highlightPos = new int[][] { new int[] { col, col } };
+        }
         ap.getStructureSelectionManager()
-                .highlightPositionsOn(ann.sequenceRef, new int[][]
-                { new int[] { col, col },
-                    new int[]
-                    { ci.cStart, ci.cEnd } }, null);
+                .highlightPositionsOn(ann.sequenceRef, highlightPos, null);
       }
     }
     return tooltip;