JAL-3206 strict above/below threshold test restored
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
index c57b679..54eaeb0 100755 (executable)
  */
 package jalview.schemes;
 
-import java.awt.Color;
-import java.util.IdentityHashMap;
-import java.util.Map;
-
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
@@ -31,6 +27,12 @@ import jalview.datamodel.Annotation;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
+import jalview.renderer.AnnotationRenderer;
+import jalview.util.Comparison;
+
+import java.awt.Color;
+import java.util.IdentityHashMap;
+import java.util.Map;
 
 public class AnnotationColourGradient extends FollowerColourScheme
 {
@@ -44,7 +46,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
 
   private final int aboveAnnotationThreshold;
 
-  private boolean thresholdIsMinMax = false;
+  public boolean thresholdIsMinMax = false;
 
   private GraphLine annotationThreshold;
 
@@ -74,11 +76,11 @@ public class AnnotationColourGradient extends FollowerColourScheme
   private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
 
   @Override
-  public ColourSchemeI applyTo(AnnotatedCollectionI sg,
+  public ColourSchemeI getInstance(AnnotatedCollectionI sg,
           Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
   {
     AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
-            colourScheme, aboveAnnotationThreshold);
+            getColourScheme(), aboveAnnotationThreshold);
     acg.thresholdIsMinMax = thresholdIsMinMax;
     acg.annotationThreshold = (annotationThreshold == null) ? null
             : new GraphLine(annotationThreshold);
@@ -102,11 +104,12 @@ public class AnnotationColourGradient extends FollowerColourScheme
   {
     if (originalColour instanceof AnnotationColourGradient)
     {
-      colourScheme = ((AnnotationColourGradient) originalColour).colourScheme;
+      setColourScheme(((AnnotationColourGradient) originalColour)
+              .getColourScheme());
     }
     else
     {
-      colourScheme = originalColour;
+      setColourScheme(originalColour);
     }
 
     this.annotation = annotation;
@@ -182,18 +185,19 @@ public class AnnotationColourGradient extends FollowerColourScheme
       }
       else
       {
-        seqannot = new IdentityHashMap<SequenceI, AlignmentAnnotation>();
+        seqannot = new IdentityHashMap<>();
       }
       // resolve the context containing all the annotation for the sequence
-      AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
+      AnnotatedCollectionI alcontext = alignment instanceof AlignmentI
+              ? alignment
               : alignment.getContext();
       boolean f = true, rna = false;
-      for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
-              .getCalcId()))
+      for (AlignmentAnnotation alan : alcontext
+              .findAnnotation(annotation.getCalcId()))
       {
         if (alan.sequenceRef != null
-                && (alan.label != null && annotation != null && alan.label
-                        .equals(annotation.label)))
+                && (alan.label != null && annotation != null
+                        && alan.label.equals(annotation.label)))
         {
           if (!rna && alan.isRNA())
           {
@@ -249,8 +253,8 @@ public class AnnotationColourGradient extends FollowerColourScheme
 
   public Color getMaxColour()
   {
-    return new Color(redMin + redRange, greenMin + greenRange, blueMin
-            + blueRange);
+    return new Color(redMin + redRange, greenMin + greenRange,
+            blueMin + blueRange);
   }
 
   /**
@@ -268,107 +272,125 @@ public class AnnotationColourGradient extends FollowerColourScheme
   }
 
   /**
-   * DOCUMENT ME!
+   * Returns the colour for a given character and position in a sequence
    * 
-   * @param n
-   *          DOCUMENT ME!
+   * @param c
+   *          the residue character
    * @param j
-   *          DOCUMENT ME!
-   * 
-   * @return DOCUMENT ME!
+   *          the aligned position
+   * @param seq
+   *          the sequence
+   * @return
    */
   @Override
   public Color findColour(char c, int j, SequenceI seq)
   {
-    Color currentColour = Color.white;
-    AlignmentAnnotation ann = (seqAssociated && seqannot != null ? seqannot
-            .get(seq) : this.annotation);
-    if (ann == null)
+    /*
+     * locate the annotation we are configured to colour by
+     */
+    AlignmentAnnotation ann = (seqAssociated && seqannot != null
+            ? seqannot.get(seq)
+            : this.annotation);
+
+    /*
+     * if gap or no annotation at position, no colour (White)
+     */
+    if (ann == null || ann.annotations == null
+            || j >= ann.annotations.length || ann.annotations[j] == null
+            || Comparison.isGap(c))
     {
-      return currentColour;
+      return Color.white;
     }
-    if ((threshold == 0) || aboveThreshold(c, j))
+
+    Annotation aj = ann.annotations[j];
+    // 'use original colours' => colourScheme != null
+    // -> look up colour to be used
+    // predefined colours => preconfigured shading
+    // -> only use original colours reference if thresholding enabled &
+    // minmax exists
+    // annotation.hasIcons => null or black colours replaced with glyph
+    // colours
+    // -> reuse original colours if present
+    // -> if thresholding enabled then return colour on non-whitespace glyph
+
+    /*
+     * if threshold applies, and annotation fails the test - no colour (white)
+     */
+    if (annotationThreshold != null)
     {
-      if (ann.annotations != null && j < ann.annotations.length
-              && ann.annotations[j] != null
-              && !jalview.util.Comparison.isGap(c))
+      if ((aboveAnnotationThreshold == ABOVE_THRESHOLD
+              && aj.value <= annotationThreshold.value)
+              || (aboveAnnotationThreshold == BELOW_THRESHOLD
+                      && aj.value >= annotationThreshold.value))
       {
-        Annotation aj = ann.annotations[j];
-        // 'use original colours' => colourScheme != null
-        // -> look up colour to be used
-        // predefined colours => preconfigured shading
-        // -> only use original colours reference if thresholding enabled &
-        // minmax exists
-        // annotation.hasIcons => null or black colours replaced with glyph
-        // colours
-        // -> reuse original colours if present
-        // -> if thresholding enabled then return colour on non-whitespace glyph
-
-        if (aboveAnnotationThreshold == NO_THRESHOLD
-                || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
-                        : aj.value <= annotationThreshold.value)))
+        return Color.white;
+      }
+    }
+
+    /*
+     * If 'use original colours' then return the colour of the annotation
+     * at the aligned position - computed using the background colour scheme
+     */
+    if (predefinedColours && aj.colour != null
+            && !aj.colour.equals(Color.black))
+    {
+      return aj.colour;
+    }
+
+    Color result = Color.white;
+    if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
+    {
+      /*
+       * secondary structure symbol colouring
+       */
+      if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
+              && aj.secondaryStructure != '-')
+      {
+        if (getColourScheme() != null)
         {
-          if (predefinedColours && aj.colour != null
-                  && !aj.colour.equals(Color.black))
-          {
-            currentColour = aj.colour;
-          }
-          else if (ann.hasIcons
-                  && ann.graph == AlignmentAnnotation.NO_GRAPH)
-          {
-            if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
-                    && aj.secondaryStructure != '-')
-            {
-              if (colourScheme != null)
-              {
-                currentColour = colourScheme.findColour(c, j, seq);
-              }
-              else
-              {
-                if (ann.isRNA())
-                {
-                  currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
-                }
-                else
-                {
-                  currentColour = ann.annotations[j].secondaryStructure == 'H' ? jalview.renderer.AnnotationRenderer.HELIX_COLOUR
-                          : ann.annotations[j].secondaryStructure == 'E' ? jalview.renderer.AnnotationRenderer.SHEET_COLOUR
-                                  : jalview.renderer.AnnotationRenderer.STEM_COLOUR;
-                }
-              }
-            }
-            else
-            {
-              //
-              return Color.white;
-            }
-          }
-          else if (noGradient)
+          result = getColourScheme().findColour(c, j, seq, null, 0f);
+        }
+        else
+        {
+          if (ann.isRNA())
           {
-            if (colourScheme != null)
-            {
-              currentColour = colourScheme.findColour(c, j, seq);
-            }
-            else
-            {
-              if (aj.colour != null)
-              {
-                currentColour = aj.colour;
-              }
-            }
+            result = ColourSchemeProperty.rnaHelices[(int) aj.value];
           }
           else
           {
-            currentColour = shadeCalculation(ann, j);
+            result = ann.annotations[j].secondaryStructure == 'H'
+                    ? AnnotationRenderer.HELIX_COLOUR
+                    : ann.annotations[j].secondaryStructure == 'E'
+                            ? AnnotationRenderer.SHEET_COLOUR
+                            : AnnotationRenderer.STEM_COLOUR;
           }
         }
-        if (conservationColouring)
+      }
+      else
+      {
+        return Color.white;
+      }
+    }
+    else if (noGradient)
+    {
+      if (getColourScheme() != null)
+      {
+        result = getColourScheme().findColour(c, j, seq, null, 0f);
+      }
+      else
+      {
+        if (aj.colour != null)
         {
-          currentColour = applyConservation(currentColour, j);
+          result = aj.colour;
         }
       }
     }
-    return currentColour;
+    else
+    {
+      result = shadeCalculation(ann, j);
+    }
+
+    return result;
   }
 
   /**
@@ -392,14 +414,17 @@ public class AnnotationColourGradient extends FollowerColourScheme
             && aboveAnnotationThreshold == ABOVE_THRESHOLD
             && value >= ann.threshold.value)
     {
-      range = (value - ann.threshold.value)
+      range = ann.graphMax == ann.threshold.value ? 1f
+              : (value - ann.threshold.value)
               / (ann.graphMax - ann.threshold.value);
     }
     else if (thresholdIsMinMax && ann.threshold != null
             && aboveAnnotationThreshold == BELOW_THRESHOLD
             && value <= ann.threshold.value)
     {
-      range = (value - ann.graphMin) / (ann.threshold.value - ann.graphMin);
+      range = ann.graphMin == ann.threshold.value ? 0f
+              : (value - ann.graphMin)
+                      / (ann.threshold.value - ann.graphMin);
     }
     else
     {
@@ -445,8 +470,20 @@ public class AnnotationColourGradient extends FollowerColourScheme
     return thresholdIsMinMax;
   }
 
-  public void setThresholdIsMinMax(boolean thresholdIsMinMax)
+  public void setThresholdIsMinMax(boolean minMax)
+  {
+    this.thresholdIsMinMax = minMax;
+  }
+
+  @Override
+  public String getSchemeName()
+  {
+    return ANNOTATION_COLOUR;
+  }
+
+  @Override
+  public boolean isSimple()
   {
-    this.thresholdIsMinMax = thresholdIsMinMax;
+    return false;
   }
 }