Merge branch 'feature/JAL-3180colourAnnotationMenu' into merge/JAL-3180
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureRenderer.java
index 7915fd0..13885b4 100644 (file)
@@ -21,6 +21,8 @@
 package jalview.renderer.seqfeatures;
 
 import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
+import jalview.datamodel.ContiguousI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.util.Comparison;
@@ -31,18 +33,13 @@ import java.awt.Color;
 import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
+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;
-
-  private volatile SequenceFeature[] lastSequenceFeatures;
-
-  int sfSize;
-
   /**
    * Constructor given a viewport
    * 
@@ -104,8 +101,7 @@ public class FeatureRenderer extends FeatureRendererModel
 
       g.setColor(featureColour);
 
-      g.fillRect((i - start) * charWidth, y1, charWidth,
-              charHeight);
+      g.fillRect((i - start) * charWidth, y1, charWidth, charHeight);
 
       if (colourOnly || !validCharWidth)
       {
@@ -114,8 +110,8 @@ public class FeatureRenderer extends FeatureRendererModel
 
       g.setColor(Color.white);
       int charOffset = (charWidth - fm.charWidth(s)) / 2;
-      g.drawString(String.valueOf(s), charOffset
-              + (charWidth * (i - start)), pady);
+      g.drawString(String.valueOf(s),
+              charOffset + (charWidth * (i - start)), pady);
     }
     return true;
   }
@@ -203,19 +199,14 @@ public class FeatureRenderer extends FeatureRendererModel
 
       g.setColor(Color.black);
       int charOffset = (charWidth - fm.charWidth(s)) / 2;
-      g.drawString(String.valueOf(s), charOffset
-              + (charWidth * (i - start)), pady);
+      g.drawString(String.valueOf(s),
+              charOffset + (charWidth * (i - start)), pady);
     }
     return true;
   }
 
   /**
-   * This is used by Structure Viewers and the Overview Window to get the
-   * feature colour of the rendered sequence
-   * 
-   * @param seq
-   * @param column
-   * @return
+   * {@inheritDoc}
    */
   @Override
   public Color findFeatureColour(SequenceI seq, int column, Graphics g)
@@ -225,61 +216,48 @@ public class FeatureRenderer extends FeatureRendererModel
       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)
+    // column is 'base 1' but getCharAt is an array index (ie from 0)
+    if (Comparison.isGap(seq.getCharAt(column - 1)))
     {
+      /*
+       * returning null allows the colour scheme to provide gap colour
+       * - normally white, but can be customised
+       */
       return null;
     }
 
-    if (Comparison.isGap(lastSeq.getCharAt(column)))
-    {
-      return Color.white;
-    }
-
     Color renderedColour = null;
     if (transparency == 1.0f)
     {
-      renderedColour = findFeatureColour(seq, seq.findPosition(column));
+      /*
+       * simple case - just find the topmost rendered visible feature colour
+       */
+      renderedColour = findFeatureColour(seq, 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;
   }
 
   /**
    * 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
@@ -291,21 +269,17 @@ public class FeatureRenderer extends FeatureRendererModel
           final SequenceI seq, int start, int end, int y1,
           boolean colourOnly)
   {
-    SequenceFeature[] sequenceFeatures = seq.getSequenceFeatures();
-    if (sequenceFeatures == null || sequenceFeatures.length == 0)
+    /*
+     * if columns are all gapped, or sequence has no features, nothing to do
+     */
+    ContiguousI visiblePositions = seq.findPositions(start + 1, end + 1);
+    if (visiblePositions == null || !seq.getFeatures().hasFeatures())
     {
       return null;
     }
 
     updateFeatures();
 
-    if (lastSeq == null || seq != lastSeq
-            || sequenceFeatures != lastSequenceFeatures)
-    {
-      lastSeq = seq;
-      lastSequenceFeatures = sequenceFeatures;
-    }
-
     if (transparency != 1f && g != null)
     {
       Graphics2D g2 = (Graphics2D) g;
@@ -313,17 +287,10 @@ public class FeatureRenderer extends FeatureRendererModel
               transparency));
     }
 
-    int startPos = lastSeq.findPosition(start);
-    int endPos = lastSeq.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++)
     {
@@ -333,52 +300,71 @@ 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++)
+      FeatureColourI fc = getFeatureStyle(type);
+      List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
+              visiblePositions.getBegin(), visiblePositions.getEnd(), type);
+
+      if (fc.isSimpleColour())
       {
-        final SequenceFeature sequenceFeature = lastSequenceFeatures[sfindex];
-        if (!sequenceFeature.type.equals(type))
-        {
-          continue;
-        }
+        filterFeaturesForDisplay(overlaps);
+      }
 
-        if (featureGroupNotShown(sequenceFeature))
+      for (SequenceFeature sf : overlaps)
+      {
+        Color featureColour = getColor(sf, fc);
+        if (featureColour == null)
         {
+          /*
+           * feature excluded by visibility settings, filters, or colour threshold
+           */
           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 
+         * if feature starts/ends outside the visible range,
+         * restrict to visible positions (or if a contact feature,
+         * to a single position)
          */
-        if (sequenceFeature.getBegin() > endPos
-                || sequenceFeature.getEnd() < startPos)
+        int visibleStart = sf.getBegin();
+        if (visibleStart < visiblePositions.getBegin())
         {
-          continue;
+          visibleStart = sf.isContactFeature() ? sf.getEnd()
+                  : visiblePositions.getBegin();
+        }
+        int visibleEnd = sf.getEnd();
+        if (visibleEnd > visiblePositions.getEnd())
+        {
+          visibleEnd = sf.isContactFeature() ? sf.getBegin()
+                  : visiblePositions.getEnd();
         }
 
-        Color featureColour = getColour(sequenceFeature);
-        boolean isContactFeature = sequenceFeature.isContactFeature();
+        int featureStartCol = seq.findIndex(visibleStart);
+        int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
+                .findIndex(visibleEnd);
+
+        // Color featureColour = getColour(sequenceFeature);
+
+        boolean isContactFeature = sf.isContactFeature();
 
         if (isContactFeature)
         {
-          boolean drawn = renderFeature(g, seq,
-                  seq.findIndex(sequenceFeature.begin) - 1,
-                  seq.findIndex(sequenceFeature.begin) - 1, featureColour,
-                  start, end, y1, colourOnly);
-          drawn |= renderFeature(g, seq,
-                  seq.findIndex(sequenceFeature.end) - 1,
-                  seq.findIndex(sequenceFeature.end) - 1, featureColour,
-                  start, end, y1, colourOnly);
+          boolean drawn = renderFeature(g, seq, featureStartCol - 1,
+                  featureStartCol - 1, featureColour, start, end, y1,
+                  colourOnly);
+          drawn |= renderFeature(g, seq, featureEndCol - 1,
+                  featureEndCol - 1, featureColour, start, end, y1,
+                  colourOnly);
           if (drawn)
           {
             drawnColour = featureColour;
           }
         }
-        else if (showFeature(sequenceFeature))
+        else
         {
+          /*
+           * showing feature score by height of colour
+           * is not implemented as a selectable option 
+           *
           if (av.isShowSequenceFeaturesHeight()
                   && !Float.isNaN(sequenceFeature.score))
           {
@@ -394,15 +380,16 @@ public class FeatureRenderer extends FeatureRendererModel
           }
           else
           {
+          */
             boolean drawn = renderFeature(g, seq,
-                    seq.findIndex(sequenceFeature.begin) - 1,
-                    seq.findIndex(sequenceFeature.end) - 1, featureColour,
+                    featureStartCol - 1,
+                    featureEndCol - 1, featureColour,
                     start, end, y1, colourOnly);
             if (drawn)
             {
               drawnColour = featureColour;
             }
-          }
+          /*}*/
         }
       }
     }
@@ -420,24 +407,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.
    * 
@@ -445,35 +414,38 @@ public class FeatureRenderer extends FeatureRendererModel
   @Override
   public void featuresAdded()
   {
-    lastSeq = null;
     findAllFeatures();
   }
 
   /**
-   * Returns the sequence feature colour rendered at the given sequence
-   * position, or null if none found. The feature of highest render order (i.e.
-   * on top) is found, subject to both feature type and feature group being
-   * visible, and its colour returned.
+   * 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
+   * found, subject to both feature type and feature group being visible, and
+   * its colour returned. This method is suitable when no feature transparency
+   * applied (only the topmost visible feature colour is rendered).
+   * <p>
+   * Note this method does not check for a gap in the column so would return the
+   * colour for features enclosing a gapped column. Check for gap before calling
+   * if different behaviour is wanted.
    * 
    * @param seq
-   * @param pos
+   * @param column
+   *          (1..)
    * @return
    */
-  Color findFeatureColour(SequenceI seq, int pos)
+  Color findFeatureColour(SequenceI seq, int column)
   {
-    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 
      * 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
+            - 1; renderIndex >= 0; renderIndex--)
     {
       String type = renderOrder[renderIndex];
       if (!showFeatureOfType(type))
@@ -481,33 +453,21 @@ public class FeatureRenderer extends FeatureRendererModel
         continue;
       }
 
-      for (int sfindex = 0; sfindex < sequenceFeatures.length; sfindex++)
+      List<SequenceFeature> overlaps = seq.findFeatures(column, column,
+              type);
+      for (SequenceFeature sequenceFeature : overlaps)
       {
-        SequenceFeature sequenceFeature = sequenceFeatures[sfindex];
-        if (!sequenceFeature.type.equals(type))
-        {
-          continue;
-        }
-
-        if (featureGroupNotShown(sequenceFeature))
+        if (!featureGroupNotShown(sequenceFeature))
         {
-          continue;
-        }
-
-        boolean featureIsAtPosition = sequenceFeature.begin <= pos
-                && sequenceFeature.end >= pos;
-        if (sequenceFeature.isContactFeature())
-        {
-          featureIsAtPosition = sequenceFeature.begin == pos
-                  || sequenceFeature.end == pos;
-        }
-        if (featureIsAtPosition)
-        {
-          return getColour(sequenceFeature);
+          Color col = getColour(sequenceFeature);
+          if (col != null)
+          {
+            return col;
+          }
         }
       }
     }
-  
+
     /*
      * no displayed feature found at position
      */