Merge branch 'bug/JAL-3612_overviewFeatureOrdering' into develop
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureRenderer.java
index 8f4f139..e66b7d5 100644 (file)
  */
 package jalview.renderer.seqfeatures;
 
-import jalview.api.AlignViewportI;
-import jalview.api.FeatureColourI;
-import jalview.datamodel.SequenceFeature;
-import jalview.datamodel.SequenceI;
-import jalview.util.Comparison;
-import jalview.viewmodel.seqfeatures.FeatureRendererModel;
-
 import java.awt.AlphaComposite;
 import java.awt.Color;
 import java.awt.FontMetrics;
@@ -34,6 +27,18 @@ import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.util.List;
 
+import jalview.api.AlignViewportI;
+import jalview.api.FeatureColourI;
+import jalview.datamodel.ContiguousI;
+import jalview.datamodel.MappedFeatures;
+import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+import jalview.gui.Desktop;
+import jalview.util.Comparison;
+import jalview.util.ReverseListIterator;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
+
 public class FeatureRenderer extends FeatureRendererModel
 {
   private static final AlphaComposite NO_TRANSPARENCY = AlphaComposite
@@ -100,18 +105,21 @@ 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)
       {
         continue;
       }
 
+      /*
+       * JAL-3045 text is always drawn over features, even if
+       * 'Show Text' is unchecked in the format menu
+       */
       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;
   }
@@ -199,8 +207,8 @@ 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;
   }
@@ -216,9 +224,14 @@ public class FeatureRenderer extends FeatureRendererModel
       return null;
     }
 
-    if (Comparison.isGap(seq.getCharAt(column)))
+    // column is 'base 1' but getCharAt is an array index (ie from 0)
+    if (Comparison.isGap(seq.getCharAt(column - 1)))
     {
-      return Color.white;
+      /*
+       * returning null allows the colour scheme to provide gap colour
+       * - normally white, but can be customised
+       */
+      return null;
     }
 
     Color renderedColour = null;
@@ -264,7 +277,12 @@ public class FeatureRenderer extends FeatureRendererModel
           final SequenceI seq, int start, int end, int y1,
           boolean colourOnly)
   {
-    if (!seq.getFeatures().hasFeatures())
+    /*
+     * 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()
+            && !av.isShowComplementFeatures())
     {
       return null;
     }
@@ -281,6 +299,16 @@ public class FeatureRenderer extends FeatureRendererModel
     Color drawnColour = null;
 
     /*
+     * draw 'complement' features below ours if configured to do so
+     */
+    if (av.isShowComplementFeatures()
+            && !av.isShowComplementFeaturesOnTop())
+    {
+      drawnColour = drawComplementFeatures(g, seq, start, end, y1,
+              colourOnly, visiblePositions, drawnColour);
+    }
+
+    /*
      * iterate over features in ordering of their rendering (last is on top)
      */
     for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
@@ -292,28 +320,51 @@ public class FeatureRenderer extends FeatureRendererModel
       }
 
       FeatureColourI fc = getFeatureStyle(type);
-      List<SequenceFeature> overlaps = seq.findFeatures(start + 1, end + 1,
-              type);
+      List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
+              visiblePositions.getBegin(), visiblePositions.getEnd(), type);
 
-      filterFeaturesForDisplay(overlaps, fc);
+      if (overlaps.size() > 1 && fc.isSimpleColour())
+      {
+        filterFeaturesForDisplay(overlaps);
+      }
 
       for (SequenceFeature sf : overlaps)
       {
+        Color featureColour = getColor(sf, fc);
+        if (featureColour == null)
+        {
+          /*
+           * feature excluded by filters, or colour threshold
+           */
+          continue;
+        }
+
         /*
-         * a feature type may be flagged as shown but the group 
-         * an instance of it belongs to may be hidden
+         * if feature starts/ends outside the visible range,
+         * restrict to visible positions (or if a contact feature,
+         * to a single position)
          */
-        if (featureGroupNotShown(sf))
+        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 = fc.getColor(sf);
+        int featureStartCol = seq.findIndex(visibleStart);
+        int featureEndCol = sf.begin == sf.end ? featureStartCol
+                : seq.findIndex(visibleEnd);
+
+        // Color featureColour = getColour(sequenceFeature);
+
         boolean isContactFeature = sf.isContactFeature();
 
-        int featureStartCol = seq.findIndex(sf.begin);
-        int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
-                .findIndex(sf.end);
         if (isContactFeature)
         {
           boolean drawn = renderFeature(g, seq, featureStartCol - 1,
@@ -327,7 +378,7 @@ public class FeatureRenderer extends FeatureRendererModel
             drawnColour = featureColour;
           }
         }
-        else if (showFeature(sf))
+        else
         {
           /*
            * showing feature score by height of colour
@@ -349,19 +400,27 @@ 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;
+          }
           /*}*/
         }
       }
     }
 
+    /*
+     * draw 'complement' features above ours if configured to do so
+     */
+    if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop())
+    {
+      drawnColour = drawComplementFeatures(g, seq, start, end, y1,
+              colourOnly, visiblePositions, drawnColour);
+    }
+
     if (transparency != 1.0f && g != null)
     {
       /*
@@ -375,6 +434,51 @@ public class FeatureRenderer extends FeatureRendererModel
   }
 
   /**
+   * Find any features on the CDS/protein complement of the sequence region and
+   * draw them, with visibility and colouring as configured in the complementary
+   * viewport
+   * 
+   * @param g
+   * @param seq
+   * @param start
+   * @param end
+   * @param y1
+   * @param colourOnly
+   * @param visiblePositions
+   * @param drawnColour
+   * @return
+   */
+  Color drawComplementFeatures(final Graphics g, final SequenceI seq,
+          int start, int end, int y1, boolean colourOnly,
+          ContiguousI visiblePositions, Color drawnColour)
+  {
+    AlignViewportI comp = av.getCodingComplement();
+    FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp)
+            .getFeatureRenderer();
+
+    final int visibleStart = visiblePositions.getBegin();
+    final int visibleEnd = visiblePositions.getEnd();
+
+    for (int pos = visibleStart; pos <= visibleEnd; pos++)
+    {
+      int column = seq.findIndex(pos);
+      MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq, pos);
+      if (mf != null)
+      {
+        for (SequenceFeature sf : mf.features)
+        {
+          FeatureColourI fc = fr2.getFeatureStyle(sf.getType());
+          Color featureColour = fr2.getColor(sf, fc);
+          renderFeature(g, seq, column - 1, column - 1, featureColour,
+                  start, end, y1, colourOnly);
+          drawnColour = featureColour;
+        }
+      }
+    }
+    return drawnColour;
+  }
+
+  /**
    * Called when alignment in associated view has new/modified features to
    * discover and display.
    * 
@@ -389,7 +493,8 @@ public class FeatureRenderer extends FeatureRendererModel
    * 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.
+   * 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
@@ -408,10 +513,23 @@ public class FeatureRenderer extends FeatureRendererModel
     updateFeatures();
 
     /*
+     * show complement features on top (if configured to show them)
+     */
+    if (av.isShowComplementFeatures() && av.isShowComplementFeaturesOnTop())
+    {
+      Color col = findComplementFeatureColour(seq, column);
+      if (col != null)
+      {
+        return col;
+      }
+    }
+
+    /*
      * 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))
@@ -419,20 +537,64 @@ public class FeatureRenderer extends FeatureRendererModel
         continue;
       }
 
+      /*
+       * find features of this type, and the colour of the _last_ one
+       * (the one that would be drawn on top) that has a colour
+       */
       List<SequenceFeature> overlaps = seq.findFeatures(column, column,
               type);
-      for (SequenceFeature sequenceFeature : overlaps)
+      for (int i = overlaps.size() - 1 ; i >= 0 ; i--)
       {
+        SequenceFeature sequenceFeature = overlaps.get(i);
         if (!featureGroupNotShown(sequenceFeature))
         {
-          return getColour(sequenceFeature);
+          Color col = getColour(sequenceFeature);
+          if (col != null)
+          {
+            return col;
+          }
         }
       }
     }
-  
+
     /*
-     * no displayed feature found at position
+     * show complement features underneath (if configured to show them)
      */
+    Color col = null;
+    if (av.isShowComplementFeatures()
+            && !av.isShowComplementFeaturesOnTop())
+    {
+      col = findComplementFeatureColour(seq, column);
+    }
+
+    return col;
+  }
+
+  Color findComplementFeatureColour(SequenceI seq, int column)
+  {
+    AlignViewportI complement = av.getCodingComplement();
+    AlignFrame af = Desktop.getAlignFrameFor(complement);
+    FeatureRendererModel fr2 = af.getFeatureRenderer();
+    MappedFeatures mf = fr2.findComplementFeaturesAtResidue(seq,
+            seq.findPosition(column - 1));
+    if (mf == null)
+    {
+      return null;
+    }
+    ReverseListIterator<SequenceFeature> it = new ReverseListIterator<>(
+            mf.features);
+    while (it.hasNext())
+    {
+      SequenceFeature sf = it.next();
+      if (!fr2.featureGroupNotShown(sf))
+      {
+        Color col = fr2.getColour(sf);
+        if (col != null)
+        {
+          return col;
+        }
+      }
+    }
     return null;
   }
 }