JAL-3187 return a colour for linked features with transparency
[jalview.git] / src / jalview / renderer / seqfeatures / FeatureRenderer.java
index 3f44bcc..4e81e69 100644 (file)
@@ -25,7 +25,10 @@ import jalview.api.FeatureColourI;
 import jalview.datamodel.Range;
 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;
 
 import java.awt.AlphaComposite;
@@ -101,8 +104,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)
       {
@@ -111,8 +113,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;
   }
@@ -200,8 +202,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;
   }
@@ -217,7 +219,8 @@ 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)))
     {
       /*
        * returning null allows the colour scheme to provide gap colour
@@ -273,7 +276,8 @@ public class FeatureRenderer extends FeatureRendererModel
      * 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())
+    if (visiblePositions == null || (!seq.getFeatures().hasFeatures()
+            && !av.isShowComplementFeatures()))
     {
       return null;
     }
@@ -304,11 +308,21 @@ public class FeatureRenderer extends FeatureRendererModel
       List<SequenceFeature> overlaps = seq.getFeatures().findFeatures(
               visiblePositions.getBegin(), visiblePositions.getEnd(), type);
 
-      filterFeaturesForDisplay(overlaps, fc);
+      if (fc.isSimpleColour())
+      {
+        filterFeaturesForDisplay(overlaps);
+      }
 
       for (SequenceFeature sf : overlaps)
       {
-        Color featureColour = fc.getColor(sf);
+        Color featureColour = getColor(sf, fc);
+        if (featureColour == null)
+        {
+          /*
+           * feature excluded by filters, or colour threshold
+           */
+          continue;
+        }
 
         /*
          * if feature starts/ends outside the visible range,
@@ -332,7 +346,11 @@ public class FeatureRenderer extends FeatureRendererModel
         int featureEndCol = sf.begin == sf.end ? featureStartCol : seq
                 .findIndex(visibleEnd);
 
-        if (sf.isContactFeature())
+        // Color featureColour = getColour(sequenceFeature);
+
+        boolean isContactFeature = sf.isContactFeature();
+
+        if (isContactFeature)
         {
           boolean drawn = renderFeature(g, seq, featureStartCol - 1,
                   featureStartCol - 1, featureColour, start, end, y1,
@@ -345,7 +363,7 @@ public class FeatureRenderer extends FeatureRendererModel
             drawnColour = featureColour;
           }
         }
-        else if (showFeature(sf))
+        else
         {
           /*
            * showing feature score by height of colour
@@ -380,6 +398,30 @@ public class FeatureRenderer extends FeatureRendererModel
       }
     }
 
+    /*
+     * if configured to do so, find and show complement's features
+     */
+    if (av.isShowComplementFeatures())
+    {
+      AlignViewportI comp = av.getCodingComplement();
+      FeatureRenderer fr2 = Desktop.getAlignFrameFor(comp)
+              .getFeatureRenderer();
+      for (int pos = visiblePositions.start; pos <= visiblePositions.end; pos++)
+      {
+        int column = seq.findIndex(pos);
+        List<SequenceFeature> features = fr2
+                .findComplementFeaturesAtResidue(seq, pos);
+        for (SequenceFeature sf : 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;
+        }
+      }
+    }
+
     if (transparency != 1.0f && g != null)
     {
       /*
@@ -407,7 +449,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
@@ -426,10 +469,38 @@ public class FeatureRenderer extends FeatureRendererModel
     updateFeatures();
 
     /*
+     * show complement features on top (if configured to show them)
+     */
+    if (av.isShowComplementFeatures())
+    {
+      AlignViewportI complement = av.getCodingComplement();
+      AlignFrame af = Desktop.getAlignFrameFor(complement);
+      FeatureRendererModel fr2 = af.getFeatureRenderer();
+      List<SequenceFeature> features = fr2.findComplementFeaturesAtResidue(
+              seq, seq.findPosition(column - 1));
+
+      ReverseListIterator<SequenceFeature> it = new ReverseListIterator<>(
+              features);
+      while (it.hasNext())
+      {
+        SequenceFeature sf = it.next();
+        if (!fr2.featureGroupNotShown(sf))
+        {
+          Color col = fr2.getColour(sf);
+          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))
@@ -443,11 +514,15 @@ public class FeatureRenderer extends FeatureRendererModel
       {
         if (!featureGroupNotShown(sequenceFeature))
         {
-          return getColour(sequenceFeature);
+          Color col = getColour(sequenceFeature);
+          if (col != null)
+          {
+            return col;
+          }
         }
       }
     }
-  
+
     /*
      * no displayed feature found at position
      */