JAL-3383 JAL-3253-applet
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureRenderer.java
index 1f47da3..9988076 100644 (file)
@@ -22,10 +22,11 @@ package jalview.renderer.seqfeatures;
 
 import jalview.api.AlignViewportI;
 import jalview.api.FeatureColourI;
-import jalview.datamodel.Range;
+import jalview.datamodel.ContiguousI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.util.Comparison;
+import jalview.util.Platform;
 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
 
 import java.awt.AlphaComposite;
@@ -33,6 +34,7 @@ import java.awt.Color;
 import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
+import java.util.ArrayList;
 import java.util.List;
 
 public class FeatureRenderer extends FeatureRendererModel
@@ -90,11 +92,13 @@ public class FeatureRenderer extends FeatureRendererModel
     int pady = (y1 + charHeight) - charHeight / 5;
 
     FontMetrics fm = g.getFontMetrics();
+    char s = '\0';
     for (int i = featureStart; i <= featureEnd; i++)
     {
-      char s = seq.getCharAt(i);
 
-      if (Comparison.isGap(s))
+      // colourOnly is just for Overview -- no need to check this again
+
+      if (!colourOnly && Comparison.isGap(s = seq.getCharAt(i)))
       {
         continue;
       }
@@ -103,7 +107,12 @@ public class FeatureRenderer extends FeatureRendererModel
 
       g.fillRect((i - start) * charWidth, y1, charWidth, charHeight);
 
-      if (colourOnly || !validCharWidth)
+      if (colourOnly)
+      {
+        return true;
+      }
+
+      if (!validCharWidth)
       {
         continue;
       }
@@ -117,6 +126,9 @@ public class FeatureRenderer extends FeatureRendererModel
   }
 
   /**
+   * 
+   * BH - this method is never called?
+   * 
    * Renders the sequence using the given SCORE feature colour between the given
    * start and end columns. Returns true if at least one column is drawn, else
    * false (the feature range does not overlap the start and end positions).
@@ -211,10 +223,12 @@ public class FeatureRenderer extends FeatureRendererModel
   @Override
   public Color findFeatureColour(SequenceI seq, int column, Graphics g)
   {
-    if (!av.isShowSequenceFeatures())
-    {
-      return null;
-    }
+    // BH 2019.08.01
+    // this is already checked in FeatureColorFinder
+    // if (!av.isShowSequenceFeatures())
+    // {
+    // return null;
+    // }
 
     // column is 'base 1' but getCharAt is an array index (ie from 0)
     if (Comparison.isGap(seq.getCharAt(column - 1)))
@@ -252,7 +266,8 @@ public class FeatureRenderer extends FeatureRendererModel
    * 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)
+   *          the graphics context to draw on (may be null only if t == 1 from
+   *          colourOnly==true)
    * @param seq
    * @param start
    *          start column
@@ -269,21 +284,27 @@ public class FeatureRenderer extends FeatureRendererModel
           final SequenceI seq, int start, int end, int y1,
           boolean colourOnly)
   {
+    // from SeqCanvas and OverviewRender
     /*
      * if columns are all gapped, or sequence has no features, nothing to do
      */
-    Range visiblePositions = seq.findPositions(start+1, end+1);
-    if (visiblePositions == null || !seq.getFeatures().hasFeatures())
+    ContiguousI visiblePositions;
+    if (!seq.getFeatures().hasFeatures() || (visiblePositions = seq
+            .findPositions(start + 1, end + 1)) == null)
     {
       return null;
     }
 
+    int vp0 = visiblePositions.getBegin();
+    int vp1 = visiblePositions.getEnd();
+
     updateFeatures();
 
-    if (transparency != 1f && g != null)
+    if (transparency != 1f) // g cannot be null here if trans == 1f - BH // && g
+                            // != null)
     {
-      Graphics2D g2 = (Graphics2D) g;
-      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
+      ((Graphics2D) g).setComposite(
+              AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
               transparency));
     }
 
@@ -292,26 +313,34 @@ public class FeatureRenderer extends FeatureRendererModel
     /*
      * iterate over features in ordering of their rendering (last is on top)
      */
-    for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
+    for (int renderIndex = 0, n = renderOrder.length; renderIndex < n; renderIndex++)
     {
       String type = renderOrder[renderIndex];
-      if (!showFeatureOfType(type))
+      if (!seq.hasFeatures(type) || !showFeatureOfType(type))
       {
         continue;
       }
 
       FeatureColourI fc = getFeatureStyle(type);
-      List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
-              visiblePositions.getBegin(), visiblePositions.getEnd(), type);
+      List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(vp0,
+              vp1, type);
 
-      filterFeaturesForDisplay(overlaps, fc);
+      // colourOnly (i.e. Overview) can only be here if translucent, so
+      // there is no need to check for filtering
+      if (!colourOnly && fc.isSimpleColour())
+      {
+        filterFeaturesForDisplay(overlaps);
+      }
 
-      for (SequenceFeature sf : overlaps)
+      for (int i = overlaps.size(); --i >= 0;)
       {
-        Color featureColour = fc.getColor(sf);
+        SequenceFeature sf = overlaps.get(i);
+        Color featureColour = getColor(sf, fc);
         if (featureColour == null)
         {
-          // score feature outwith threshold for colouring
+          /*
+           * feature excluded by visibility settings, filters, or colour threshold
+           */
           continue;
         }
 
@@ -320,22 +349,22 @@ public class FeatureRenderer extends FeatureRendererModel
          * restrict to visible positions (or if a contact feature,
          * to a single position)
          */
-        int visibleStart = sf.getBegin();
-        if (visibleStart < visiblePositions.getBegin())
+        int sf0 = sf.getBegin();
+        int sf1 = sf.getEnd();
+        int visibleStart = sf0;
+        if (visibleStart < vp0)
         {
-          visibleStart = sf.isContactFeature() ? sf.getEnd()
-                  : visiblePositions.getBegin();
+          visibleStart = sf.isContactFeature() ? sf1 : vp0;
         }
-        int visibleEnd = sf.getEnd();
-        if (visibleEnd > visiblePositions.getEnd())
+        int visibleEnd = sf1;
+        if (visibleEnd > vp1)
         {
-          visibleEnd = sf.isContactFeature() ? sf.getBegin()
-                  : visiblePositions.getEnd();
+          visibleEnd = sf.isContactFeature() ? sf0 : vp1;
         }
 
         int featureStartCol = seq.findIndex(visibleStart);
-        int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
-                .findIndex(visibleEnd);
+        int featureEndCol = (sf.begin == sf.end ? featureStartCol
+                : seq.findIndex(visibleEnd));
 
         // Color featureColour = getColour(sequenceFeature);
 
@@ -376,26 +405,24 @@ public class FeatureRenderer extends FeatureRendererModel
           else
           {
           */
-            boolean drawn = renderFeature(g, seq,
-                    featureStartCol - 1,
-                    featureEndCol - 1, featureColour,
-                    start, end, y1, colourOnly);
-            if (drawn)
-            {
-              drawnColour = featureColour;
-            }
+          boolean drawn = renderFeature(g, seq, featureStartCol - 1,
+                  featureEndCol - 1, featureColour, start, end, y1,
+                  colourOnly);
+          if (drawn)
+          {
+            drawnColour = featureColour;
+          }
           /*}*/
         }
       }
     }
 
-    if (transparency != 1.0f && g != null)
+    if (transparency != 1.0f)
     {
       /*
        * reset transparency
        */
-      Graphics2D g2 = (Graphics2D) g;
-      g2.setComposite(NO_TRANSPARENCY);
+      ((Graphics2D) g).setComposite(NO_TRANSPARENCY);
     }
 
     return drawnColour;
@@ -412,6 +439,10 @@ public class FeatureRenderer extends FeatureRendererModel
     findAllFeatures();
   }
 
+  private List<SequenceFeature> overlaps = (Platform.isJS()
+          ? new ArrayList<>()
+          : null);
+
   /**
    * Returns the sequence feature colour rendered at the given column position,
    * or null if none found. The feature of highest render order (i.e. on top) is
@@ -423,12 +454,18 @@ public class FeatureRenderer extends FeatureRendererModel
    * colour for features enclosing a gapped column. Check for gap before calling
    * if different behaviour is wanted.
    * 
+   * BH 2019.07.30
+   * 
+   * Adds a result ArrayList to parameters in order to avoid an unnecessary
+   * construction of that for every pixel checked.
+   * 
+   * 
    * @param seq
    * @param column
    *          (1..)
    * @return
    */
-  Color findFeatureColour(SequenceI seq, int column)
+  private Color findFeatureColour(SequenceI seq, int column)
   {
     /*
      * check for new feature added while processing
@@ -439,22 +476,29 @@ public class FeatureRenderer extends FeatureRendererModel
      * inspect features in reverse renderOrder (the last in the array is 
      * displayed on top) until we find one that is rendered at the position
      */
-    for (int renderIndex = renderOrder.length
-            - 1; renderIndex >= 0; renderIndex--)
+    for (int renderIndex = renderOrder.length; --renderIndex >= 0;)
     {
       String type = renderOrder[renderIndex];
-      if (!showFeatureOfType(type))
+      if (!seq.hasFeatures(type) || !showFeatureOfType(type))
       {
         continue;
       }
 
-      List<SequenceFeature> overlaps = seq.findFeatures(column, column,
-              type);
-      for (SequenceFeature sequenceFeature : overlaps)
+      if (overlaps != null)
       {
-        if (!featureGroupNotShown(sequenceFeature))
+        overlaps.clear();
+      }
+      List<SequenceFeature> list = seq.findFeatures(column, type, overlaps);
+      if (list.size() > 0)
+      {
+        for (int i = 0, n = list.size(); i < n; i++)
         {
-          Color col = getColour(sequenceFeature);
+          SequenceFeature sf = list.get(i);
+          if (featureGroupNotShown(sf))
+          {
+            continue;
+          }
+          Color col = getColour(sf);
           if (col != null)
           {
             return col;