JAL-2349 JAL-4033 refactored contact matrix geom calc to allow reporting of mapped...
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
index bed54e8..7a435c3 100644 (file)
@@ -3,6 +3,9 @@
  */
 package jalview.renderer;
 
+import java.awt.Color;
+import java.awt.Graphics;
+
 import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
@@ -12,9 +15,6 @@ import jalview.datamodel.ContactRange;
 import jalview.datamodel.HiddenColumns;
 import jalview.renderer.api.AnnotationRowRendererI;
 
-import java.awt.Color;
-import java.awt.Graphics;
-
 /**
  * @author jprocter
  *
@@ -24,10 +24,10 @@ public class ContactMapRenderer implements AnnotationRowRendererI
 
   @Override
   public void renderRow(Graphics g, int charWidth, int charHeight,
-          boolean hasHiddenColumns, AlignViewportI viewport, HiddenColumns hiddenColumns,
-          ColumnSelection columnSelection, AlignmentAnnotation _aa,
-          Annotation[] aa_annotations, int sRes, int eRes, float min,
-          float max, int y)
+          boolean hasHiddenColumns, AlignViewportI viewport,
+          HiddenColumns hiddenColumns, ColumnSelection columnSelection,
+          AlignmentAnnotation _aa, Annotation[] aa_annotations, int sRes,
+          int eRes, float min, float max, int y)
   {
     if (sRes > aa_annotations.length)
     {
@@ -48,7 +48,13 @@ public class ContactMapRenderer implements AnnotationRowRendererI
       column = sRes + x;
       if (hasHiddenColumns)
       {
-        column = hiddenColumns.adjustForHiddenColumns(column);
+        column = hiddenColumns.visibleToAbsoluteColumn(column);
+      }
+      // TODO: highlight columns selected
+      boolean colsel = false;
+      if (columnSelection != null)
+      {
+        colsel = columnSelection.contains(column);
       }
 
       if (column > aaMax)
@@ -61,32 +67,67 @@ public class ContactMapRenderer implements AnnotationRowRendererI
         x++;
         continue;
       }
-      /*
-       * {profile type, #values, total count, char1, pct1, char2, pct2...}
-       */
+      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;
       }
+      // Bean holding mapping from contact list to pixels
+      final ContactGeometry cgeom = new ContactGeometry(contacts,
+              _aa.graphHeight);
 
-      // cell height to render
-      double scale = (_aa.graphHeight < contacts.getContactHeight()) ? 1
-              : ((double) _aa.graphHeight)
-                      / (double) contacts.getContactHeight();
-      int cstart, cend = -1;
-      for (int ht = y2, eht = y2 - _aa.graphHeight; ht >= eht; ht -= scale)
+      for (int ht = y2, eht = y2
+              - _aa.graphHeight; ht >= eht; ht -= cgeom.pixels_step)
       {
-        cstart = cend + 1;
-        cend = Math.max(cstart + 1, contacts.getContactHeight()
-                * ((ht - y2) / _aa.graphHeight));
+        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)'
-        g.setColor(getColorForRange(min, max, contacts, cstart, cend));
+        // also need a 'getMaxPosForRange(start,end)' to accurately render
+        Color col;
+        boolean rowsel = false;
+        if (!colsel && 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 || rowsel)
+        {
 
-        if (scale > 1)
+          col = getSelectedColorForRange(min, max, contacts, ci.cStart,
+                  ci.cEnd);
+          g.setColor(col);
+        }
+        else
+        {
+          col = getColorForRange(min, max, contacts, ci.cStart, ci.cEnd);
+          g.setColor(col);
+        }
+        if (cgeom.pixels_step > 1)
         {
-          g.fillRect(x * charWidth, ht, charWidth, 1 + (int) scale);
+          g.fillRect(x * charWidth, ht, charWidth, 1 + cgeom.pixels_step);
         }
         else
         {
@@ -98,8 +139,9 @@ public class ContactMapRenderer implements AnnotationRowRendererI
 
   }
 
-  Color minColor = Color.white, maxColor = Color.magenta;
-
+  // Shading parameters
+  Color minColor = Color.white, maxColor = Color.green.darker().darker(),
+          selMaxColor = Color.magenta.darker();
 
   Color shadeFor(float min, float max, float value)
   {
@@ -115,4 +157,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, minColor, max, selMaxColor);
+  }
+
 }