JAL-2349 JAL-3855 resolve sequence position for contact lookup and catch out bound...
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
index 313e3db..aaa8a61 100644 (file)
@@ -8,10 +8,11 @@ import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
 import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.ContactListI;
+import jalview.datamodel.ContactRange;
+import jalview.datamodel.HiddenColumns;
 import jalview.renderer.api.AnnotationRowRendererI;
 
 import java.awt.Color;
-import java.awt.Font;
 import java.awt.Graphics;
 
 /**
@@ -20,20 +21,10 @@ import java.awt.Graphics;
  */
 public class ContactMapRenderer implements AnnotationRowRendererI
 {
-  /*
-   * 
-   *     // TODO Auto-generated method stub
-      void drawProfileDensity(Graphics g, AlignmentAnnotation _aa,
-              Annotation[] aa_annotations, int sRes, int eRes, float min,
-              float max, int y)
-      {
 
-   * 
-   * 
-   */
   @Override
   public void renderRow(Graphics g, int charWidth, int charHeight,
-          boolean hasHiddenColumns, AlignViewportI viewport,
+          boolean hasHiddenColumns, AlignViewportI viewport, HiddenColumns hiddenColumns,
           ColumnSelection columnSelection, AlignmentAnnotation _aa,
           Annotation[] aa_annotations, int sRes, int eRes, float min,
           float max, int y)
@@ -42,7 +33,6 @@ public class ContactMapRenderer implements AnnotationRowRendererI
     {
       return;
     }
-    Font ofont = g.getFont();
     eRes = Math.min(eRes, aa_annotations.length);
 
     int x = 0, y2 = y;
@@ -58,7 +48,7 @@ public class ContactMapRenderer implements AnnotationRowRendererI
       column = sRes + x;
       if (hasHiddenColumns)
       {
-        column = columnSelection.adjustForHiddenColumns(column);
+        column = hiddenColumns.visibleToAbsoluteColumn(column);
       }
 
       if (column > aaMax)
@@ -71,36 +61,64 @@ 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;
       }
-
-
-      int scale = Math
-              .max(1, _aa.graphHeight / contacts.getContactHeight());
-      int step = _aa.graphHeight / scale;
-      int valuesProcessed = 0;
-      // profl[1] is the number of values in the profile
-      for (int stp = 0, ht = y2, eht = y2 + _aa.graphHeight; ht < eht; ht += scale, stp++)
+      int contact_height = contacts.getContactHeight();
+      // fractional pixel height to render each contact cell
+      double pixels_per_contact = ((double) _aa.graphHeight)
+                      / (double) contact_height;
+      // fractional number of contacts covering each pixel
+      double contacts_per_pixel = 1d/pixels_per_contact;
+      // number of contacts to render at a time
+      int step = (pixels_per_contact<1) ? (int) Math.floor(contacts_per_pixel) : (int) Math.ceil(pixels_per_contact);
+      
+      int pixels_step = (int) Math.ceil(step*pixels_per_contact);
+      int cstart, cend = -1;
+      for (int ht = y2, eht = y2 - _aa.graphHeight; ht >= eht; ht -= pixels_step)
       {
-        valuesProcessed = stp * step;
-        g.setColor(contacts.getColorForScore(stp * step));
+        cstart = cend + 1;
+        cend = (int) Math.min(contact_height, cstart + step);
+        // TODO show maximum colour for range - sort of done
+        // also need a 'getMaxPosForRange(start,end)'
+        g.setColor(getColorForRange(min, max, contacts, cstart, cend));
 
-        if (scale > 1)
+        if (pixels_step>1)
+        {
+          g.fillRect(x * charWidth, ht, charWidth, 1 + (int) pixels_step);
+        }
+        else
         {
-          g.fillRect(x * charWidth, ht, charWidth, scale);
-        } else {
           g.drawLine(x * charWidth, ht, (x + 1) * charWidth, ht);
         }
       }
+      x++;
     }
-    x++;
 
   }
+
+  Color minColor = Color.white, maxColor = Color.magenta;
+
+
+  Color shadeFor(float min, float max, float value)
+  {
+    return jalview.util.ColorUtils.getGraduatedColour(value, 0, minColor,
+            max, maxColor);
+  }
+
+  public Color getColorForRange(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 shadeFor(min, max, (float) cr.getMean());
+  }
+
 }