JAL-2349 JAL-3855 off by one and daft return
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
index 10f87b1..58e6ab1 100644 (file)
@@ -50,12 +50,12 @@ public class ContactMapRenderer implements AnnotationRowRendererI
       {
         column = hiddenColumns.visibleToAbsoluteColumn(column);
       }
-      // // TODO: highlight columns selected
-      // boolean colsel = false;
-      // if (columnSelection != null)
-      // {
-      // colsel = columnSelection.contains(column);
-      // }
+      // TODO: highlight columns selected
+      boolean colsel = false;
+      if (columnSelection != null)
+      {
+        colsel = columnSelection.contains(column);
+      }
 
       if (column > aaMax)
       {
@@ -67,61 +67,71 @@ public class ContactMapRenderer implements AnnotationRowRendererI
         x++;
         continue;
       }
-      if (_aa.sequenceRef != null)
-      {
-        // get the sequence position for the column
-        column = _aa.sequenceRef.findPosition(column) - 1;
-      }
       ContactListI contacts = viewport.getContactList(_aa, column);
       if (contacts == null)
       {
-        return;
-      }
-      int contact_height = contacts.getContactHeight();
-      // fractional number of contacts covering each pixel
-      double contacts_per_pixel = ((double) contact_height)
-              / ((double) _aa.graphHeight);
-
-      int pixels_step;
-
-      if (contacts_per_pixel >= 1)
-      {
-        // many contacts rendered per pixel
-        pixels_step = 1;
-      }
-      else
-      {
-        // pixel height for each contact
-        pixels_step = (int) Math
-                .ceil(((double) _aa.graphHeight) / (double) contact_height);
+        x++;
+        continue;
       }
+      // Bean holding mapping from contact list to pixels
+      final ContactGeometry cgeom = new ContactGeometry(contacts,
+              _aa.graphHeight);
 
-      int cstart = 0, cend;
-
-      for (int ht = y2,
-              eht = y2 - _aa.graphHeight; ht >= eht; ht -= pixels_step)
+      for (int ht = y2, eht = y2
+              - _aa.graphHeight; ht >= eht; ht -= cgeom.pixels_step)
       {
-        cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
-        cend = (int) Math.min(contact_height,
-                Math.ceil(cstart + contacts_per_pixel * pixels_step));
+        ContactGeometry.contactInterval ci = cgeom.mapFor(y2 - ht,
+                y2 - ht + cgeom.pixels_step);
+        // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
+        // cend = (int) Math.min(contact_height,
+        // Math.ceil(cstart + contacts_per_pixel * pixels_step));
 
         // TODO show maximum colour for range - sort of done
         // also need a 'getMaxPosForRange(start,end)' to accurately render
-        Color col = getColorForRange(min, max, contacts, cstart, cend);
-
+        Color col;
+        boolean rowsel = false;
+        if (columnSelection != null)
+        {
+          if (_aa.sequenceRef == null)
+          {
+            rowsel = columnSelection.intersects(ci.cStart, ci.cEnd);
+          }
+          else
+          {
+            // TODO check we have correctly mapped cstart to local sequence
+            // numbering
+            int s = _aa.sequenceRef.findIndex(ci.cStart);
+            int e = _aa.sequenceRef.findIndex(ci.cEnd);
+            if (s > 0 && s < _aa.sequenceRef.getLength())
+            {
+              rowsel = columnSelection.intersects(s, e);
+            }
+          }
+        }
         // TODO: show selected region
-        // if (colsel || columnSelection!=null &&
-        // columnSelection.intersects(cstart,cend))
-        // {
-        // g.setColor(col.brighter());
-        // }
-        // else
+        if (colsel || rowsel)
         {
+
+          col = getSelectedColorForRange(min, max, contacts, ci.cStart,
+                  ci.cEnd);
+          if (colsel && rowsel)
+          {
+            col = new Color(col.getBlue(), col.getGreen(), col.getRed());
+          }
+          else
+          {
+            col = new Color(col.getBlue(), col.getBlue(), col.getBlue());
+          }
           g.setColor(col);
         }
-        if (pixels_step > 1)
+        else
         {
-          g.fillRect(x * charWidth, ht, charWidth, 1 + pixels_step);
+          col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd);
+          g.setColor(col);
+        }
+        if (cgeom.pixels_step > 1)
+        {
+          g.fillRect(x * charWidth, ht, charWidth, 1 + cgeom.pixels_step);
         }
         else
         {
@@ -133,7 +143,12 @@ public class ContactMapRenderer implements AnnotationRowRendererI
 
   }
 
-  Color minColor = Color.white, maxColor = Color.magenta;
+  // Shading parameters
+  // currently hardwired for alphafold
+  Color maxColor = new Color(246, 252, 243),
+          minColor = new Color(0, 60, 26),
+          selMinColor = new Color(26, 0, 60),
+          selMaxColor = new Color(243, 246, 252);
 
   Color shadeFor(float min, float max, float value)
   {
@@ -149,4 +164,13 @@ public class ContactMapRenderer implements AnnotationRowRendererI
     return shadeFor(min, max, (float) cr.getMean());
   }
 
+  public Color getSelectedColorForRange(float min, float max,
+          ContactListI cl, int i, int j)
+  {
+    ContactRange cr = cl.getRangeFor(i, j);
+    // average for moment - probably more interested in maxIntProj though
+    return jalview.util.ColorUtils.getGraduatedColour((float) cr.getMean(),
+            0, selMinColor, max, selMaxColor);
+  }
+
 }