JAL-2490 findFeaturesAtRes with performant feature lookup
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureRenderer.java
index de3b201..f4648c4 100644 (file)
@@ -31,21 +31,13 @@ import java.awt.Color;
 import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
-import java.awt.image.BufferedImage;
+import java.util.List;
 
 public class FeatureRenderer extends FeatureRendererModel
 {
   private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
           .getInstance(AlphaComposite.SRC_OVER, 1.0f);
 
-  protected SequenceI lastSeq;
-
-  BufferedImage offscreenImage;
-
-  private volatile SequenceFeature[] lastSequenceFeatures;
-
-  int sfSize;
-
   /**
    * Constructor given a viewport
    * 
@@ -213,51 +205,24 @@ public class FeatureRenderer extends FeatureRendererModel
   }
 
   /**
-   * This is used by Structure Viewers and the Overview Window to get the
-   * feature colour of the rendered sequence
-   * 
-   * @param defaultColour
-   * @param seq
-   * @param column
-   * @return
+   * {@inheritDoc}
    */
   @Override
-  public Color findFeatureColour(Color defaultColour, SequenceI seq,
-          int column, Graphics g)
+  public Color findFeatureColour(SequenceI seq, int column, Graphics g)
   {
     if (!av.isShowSequenceFeatures())
     {
-      return defaultColour;
+      return null;
     }
 
     SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
-    if (seq != lastSeq)
-    {
-      lastSeq = seq;
-      lastSequenceFeatures = sequenceFeatures;
-      if (lastSequenceFeatures != null)
-      {
-        sfSize = lastSequenceFeatures.length;
-      }
-    }
-    else
-    {
-      if (lastSequenceFeatures != sequenceFeatures)
-      {
-        lastSequenceFeatures = sequenceFeatures;
-        if (lastSequenceFeatures != null)
-        {
-          sfSize = lastSequenceFeatures.length;
-        }
-      }
-    }
 
-    if (lastSequenceFeatures == null || sfSize == 0)
+    if (sequenceFeatures == null || sequenceFeatures.length == 0)
     {
-      return defaultColour;
+      return null;
     }
 
-    if (Comparison.isGap(lastSeq.getCharAt(column)))
+    if (Comparison.isGap(seq.getCharAt(column)))
     {
       return Color.white;
     }
@@ -265,26 +230,35 @@ public class FeatureRenderer extends FeatureRendererModel
     Color renderedColour = null;
     if (transparency == 1.0f)
     {
+      /*
+       * simple case - just find the topmost rendered visible feature colour
+       */
       renderedColour = findFeatureColour(seq, seq.findPosition(column));
     }
     else
     {
-      renderedColour = drawSequence(g, lastSeq, column, column, 0, true);
+      /*
+       * transparency case - draw all visible features in render order to
+       * build up a composite colour on the graphics context
+       */
+      renderedColour = drawSequence(g, seq, column, column, 0, true);
     }
-    return renderedColour == null ? defaultColour : renderedColour;
+    return renderedColour;
   }
 
   /**
    * Draws the sequence features on the graphics context, or just determines the
-   * colour that would be drawn (if flag offscreenrender is true).
+   * colour that would be drawn (if flag colourOnly is true). Returns the last
+   * colour drawn (which may not be the effective colour if transparency
+   * applies), or null if no feature is drawn in the range given.
    * 
    * @param g
    *          the graphics context to draw on (may be null if colourOnly==true)
    * @param seq
    * @param start
-   *          start column (or sequence position in offscreenrender mode)
+   *          start column
    * @param end
-   *          end column (not used in offscreenrender mode)
+   *          end column
    * @param y1
    *          vertical offset at which to draw on the graphics
    * @param colourOnly
@@ -304,13 +278,6 @@ public class FeatureRenderer extends FeatureRendererModel
 
     updateFeatures();
 
-    if (lastSeq == null || seq != lastSeq
-            || sequenceFeatures != lastSequenceFeatures)
-    {
-      lastSeq = seq;
-      lastSequenceFeatures = sequenceFeatures;
-    }
-
     if (transparency != 1f && g != null)
     {
       Graphics2D g2 = (Graphics2D) g;
@@ -318,17 +285,13 @@ public class FeatureRenderer extends FeatureRendererModel
               transparency));
     }
 
-    int startPos = lastSeq.findPosition(start);
-    int endPos = lastSeq.findPosition(end);
+    int startPos = seq.findPosition(start);
+    int endPos = seq.findPosition(end);
 
-    sfSize = lastSequenceFeatures.length;
     Color drawnColour = null;
 
     /*
-     * iterate over features in ordering of their rendering;
-     * if drawing a range of columns, use render order to ensure last is on top
-     * if drawing a single column (as in findFeatureColour), with no 
-     * transparency, work backwards to find the topmost rendered feature colour
+     * iterate over features in ordering of their rendering (last is on top)
      */
     for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
     {
@@ -338,28 +301,15 @@ public class FeatureRenderer extends FeatureRendererModel
         continue;
       }
 
-      // loop through all features in sequence to find
-      // current feature to render
-      for (int sfindex = 0; sfindex < sfSize; sfindex++)
+      List<SequenceFeature> overlaps = seq.findFeatures(startPos, endPos,
+              type);
+      for (SequenceFeature sequenceFeature : overlaps)
       {
-        final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
-        if (!sequenceFeature.type.equals(type))
-        {
-          continue;
-        }
-
-        if (featureGroupNotShown(sequenceFeature))
-        {
-          continue;
-        }
-
         /*
-         * check feature overlaps the visible part of the alignment, 
-         * unless doing offscreenRender (to the Overview window or a 
-         * structure viewer) which is not limited 
+         * a feature type may be flagged as shown but the group 
+         * an instance of it belongs to may be hidden
          */
-        if (sequenceFeature.getBegin() > endPos
-                || sequenceFeature.getEnd() < startPos)
+        if (featureGroupNotShown(sequenceFeature))
         {
           continue;
         }
@@ -384,6 +334,10 @@ public class FeatureRenderer extends FeatureRendererModel
         }
         else if (showFeature(sequenceFeature))
         {
+          /*
+           * showing feature score by height of colour
+           * is not implemented as a selectable option 
+           *
           if (av.isShowSequenceFeaturesHeight()
                   && !Float.isNaN(sequenceFeature.score))
           {
@@ -399,6 +353,7 @@ public class FeatureRenderer extends FeatureRendererModel
           }
           else
           {
+          */
             boolean drawn = renderFeature(g, seq,
                     seq.findIndex(sequenceFeature.begin) - 1,
                     seq.findIndex(sequenceFeature.end) - 1, featureColour,
@@ -407,7 +362,7 @@ public class FeatureRenderer extends FeatureRendererModel
             {
               drawnColour = featureColour;
             }
-          }
+          /*}*/
         }
       }
     }
@@ -415,13 +370,8 @@ public class FeatureRenderer extends FeatureRendererModel
     if (transparency != 1.0f && g != null)
     {
       /*
-       * get colour as rendered including transparency
-       * and reset transparency
+       * reset transparency
        */
-      if (offscreenImage != null && drawnColour != null)
-      {
-        drawnColour = new Color(offscreenImage.getRGB(0, 0));
-      }
       Graphics2D g2 = (Graphics2D) g;
       g2.setComposite(NO_TRANSPARENCY);
     }
@@ -430,24 +380,6 @@ public class FeatureRenderer extends FeatureRendererModel
   }
 
   /**
-   * Answers true if the feature belongs to a feature group which is not
-   * currently displayed, else false
-   * 
-   * @param sequenceFeature
-   * @return
-   */
-  protected boolean featureGroupNotShown(
-          final SequenceFeature sequenceFeature)
-  {
-    return featureGroups != null
-            && sequenceFeature.featureGroup != null
-            && sequenceFeature.featureGroup.length() != 0
-            && featureGroups.containsKey(sequenceFeature.featureGroup)
-            && !featureGroups.get(sequenceFeature.featureGroup)
-                    .booleanValue();
-  }
-
-  /**
    * Called when alignment in associated view has new/modified features to
    * discover and display.
    * 
@@ -455,7 +387,6 @@ public class FeatureRenderer extends FeatureRendererModel
   @Override
   public void featuresAdded()
   {
-    lastSeq = null;
     findAllFeatures();
   }
 
@@ -471,13 +402,10 @@ public class FeatureRenderer extends FeatureRendererModel
    */
   Color findFeatureColour(SequenceI seq, int pos)
   {
-    SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
-    if (sequenceFeatures == null || sequenceFeatures.length == 0)
-    {
-      return null;
-    }
-  
-    // updateFeatures();
+    /*
+     * check for new feature added while processing
+     */
+    updateFeatures();
 
     /*
      * inspect features in reverse renderOrder (the last in the array is 
@@ -491,27 +419,10 @@ public class FeatureRenderer extends FeatureRendererModel
         continue;
       }
 
-      for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++)
+      List<SequenceFeature> overlaps = seq.findFeatures(pos, pos, type);
+      for (SequenceFeature sequenceFeature : overlaps)
       {
-        SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
-        if (!sequenceFeature.type.equals(type))
-        {
-          continue;
-        }
-
-        if (featureGroupNotShown(sequenceFeature))
-        {
-          continue;
-        }
-
-        boolean featureIsAtPosition = sequenceFeature.begin <= pos
-                && sequenceFeature.end >= pos;
-        if (sequenceFeature.isContactFeature())
-        {
-          featureIsAtPosition = sequenceFeature.begin == pos
-                  || sequenceFeature.end == pos;
-        }
-        if (featureIsAtPosition)
+        if (!featureGroupNotShown(sequenceFeature))
         {
           return getColour(sequenceFeature);
         }