JAL-2349 JAL-3855 resolve sequence position for contact lookup and catch out bound...
[jalview.git] / src / jalview / renderer / ContactMapRenderer.java
index bed54e8..aaa8a61 100644 (file)
@@ -48,7 +48,7 @@ public class ContactMapRenderer implements AnnotationRowRendererI
       column = sRes + x;
       if (hasHiddenColumns)
       {
-        column = hiddenColumns.adjustForHiddenColumns(column);
+        column = hiddenColumns.visibleToAbsoluteColumn(column);
       }
 
       if (column > aaMax)
@@ -61,32 +61,38 @@ 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;
       }
-
-      // cell height to render
-      double scale = (_aa.graphHeight < contacts.getContactHeight()) ? 1
-              : ((double) _aa.graphHeight)
-                      / (double) contacts.getContactHeight();
+      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 -= scale)
+      for (int ht = y2, eht = y2 - _aa.graphHeight; ht >= eht; ht -= pixels_step)
       {
         cstart = cend + 1;
-        cend = Math.max(cstart + 1, contacts.getContactHeight()
-                * ((ht - y2) / _aa.graphHeight));
+        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) scale);
+          g.fillRect(x * charWidth, ht, charWidth, 1 + (int) pixels_step);
         }
         else
         {