JAL-3853 JAL-1870 patch tests for annotation colour gradient without transparency...
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
index c57b679..8526684 100755 (executable)
  */
 package jalview.schemes;
 
-import java.awt.Color;
-import java.util.IdentityHashMap;
-import java.util.Map;
-
+import jalview.api.AlignViewportI;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
@@ -31,9 +28,25 @@ 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
 {
+  /**
+   * map positional scores to transparency rather than colour
+   */
+  boolean positionToTransparency = false;
+
+  /**
+   * compute shade based on annotation row score
+   */
+  boolean perLineScore = false;
+
   public static final int NO_THRESHOLD = -1;
 
   public static final int BELOW_THRESHOLD = 0;
@@ -44,7 +57,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
 
   private final int aboveAnnotationThreshold;
 
-  private boolean thresholdIsMinMax = false;
+  public boolean thresholdIsMinMax = false;
 
   private GraphLine annotationThreshold;
 
@@ -74,11 +87,11 @@ public class AnnotationColourGradient extends FollowerColourScheme
   private IdentityHashMap<SequenceI, AlignmentAnnotation> seqannot = null;
 
   @Override
-  public ColourSchemeI applyTo(AnnotatedCollectionI sg,
-          Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+  public ColourSchemeI getInstance(AlignViewportI view,
+          AnnotatedCollectionI sg)
   {
     AnnotationColourGradient acg = new AnnotationColourGradient(annotation,
-            colourScheme, aboveAnnotationThreshold);
+            getColourScheme(), aboveAnnotationThreshold);
     acg.thresholdIsMinMax = thresholdIsMinMax;
     acg.annotationThreshold = (annotationThreshold == null) ? null
             : new GraphLine(annotationThreshold);
@@ -91,6 +104,8 @@ public class AnnotationColourGradient extends FollowerColourScheme
     acg.predefinedColours = predefinedColours;
     acg.seqAssociated = seqAssociated;
     acg.noGradient = noGradient;
+    acg.positionToTransparency = positionToTransparency;
+    acg.perLineScore = perLineScore;
     return acg;
   }
 
@@ -102,11 +117,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,19 +198,22 @@ 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;
+      boolean f = true, sf = true, rna = false;
+      long plcount = 0, ancount = 0;
       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)))
         {
+          ancount++;
           if (!rna && alan.isRNA())
           {
             rna = true;
@@ -209,8 +228,30 @@ public class AnnotationColourGradient extends FollowerColourScheme
             aamin = alan.graphMin;
           }
           f = false;
+          if (alan.score == alan.score)
+          {
+            if (sf || alan.score < plmin)
+            {
+              plmin = alan.score;
+            }
+            if (sf || alan.score > plmax)
+            {
+              plmax = alan.score;
+            }
+            sf = false;
+            plcount++;
+          }
         }
       }
+      if (plcount > 0 && plcount == ancount)
+      {
+        perLineScore = plmax!=plmin;
+        aamax=plmax;
+      }
+      else
+      {
+        perLineScore = false;
+      }
       if (rna)
       {
         ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
@@ -218,7 +259,15 @@ public class AnnotationColourGradient extends FollowerColourScheme
     }
   }
 
-  float aamin = 0f, aamax = 0f;
+  /**
+   * positional annotation max/min
+   */
+  double aamin = 0.0, aamax = 0.0;
+
+  /**
+   * per line score max/min
+   */
+  double plmin = Double.NaN, plmax = Double.NaN;
 
   public AlignmentAnnotation getAnnotation()
   {
@@ -249,8 +298,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 +317,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 Color.white;
+    }
+
+    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 ((aboveAnnotationThreshold == ABOVE_THRESHOLD
+              && aj.value <= annotationThreshold.value)
+              || (aboveAnnotationThreshold == BELOW_THRESHOLD
+                      && 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 currentColour;
+      return aj.colour;
     }
-    if ((threshold == 0) || aboveThreshold(c, j))
+
+    Color result = Color.white;
+    if (ann.hasIcons && ann.graph == AlignmentAnnotation.NO_GRAPH)
     {
-      if (ann.annotations != null && j < ann.annotations.length
-              && ann.annotations[j] != null
-              && !jalview.util.Comparison.isGap(c))
+      /*
+       * secondary structure symbol colouring
+       */
+      if (aj.secondaryStructure > ' ' && aj.secondaryStructure != '.'
+              && aj.secondaryStructure != '-')
       {
-        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)))
+        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 +459,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
     {
@@ -413,11 +483,25 @@ public class AnnotationColourGradient extends FollowerColourScheme
       }
     }
 
-    int dr = (int) (redRange * range + redMin);
-    int dg = (int) (greenRange * range + greenMin);
-    int db = (int) (blueRange * range + blueMin);
-
-    return new Color(dr, dg, db);
+    // midtr sets the ceiling for bleaching out the shading
+    int trans = 0, midtr = 239;
+    if (perLineScore)
+    {
+      trans = (int) ((1f - range) * midtr);
+      range = (float) ((ann.score - plmin) / (plmax - aamin));
+    }
+    int dr = (int) (redRange * range + redMin),
+            dg = (int) (greenRange * range + greenMin),
+            db = (int) (blueRange * range + blueMin);
+    if (ann.score == ann.score && positionToTransparency)
+    {
+      return new Color(Math.min(dr + trans, midtr), Math.min(dg
+              + trans, midtr), Math.min(db + trans, midtr));
+    }
+    else
+    {
+      return new Color(dr, dg, db);
+    }
   }
 
   public boolean isPredefinedColours()
@@ -445,8 +529,30 @@ 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()
+  {
+    return false;
+  }
+
+  public boolean isPositionToTransparency()
+  {
+    return positionToTransparency;
+  }
+
+  public void setPositionToTransparency(boolean positionToTransparency)
   {
-    this.thresholdIsMinMax = thresholdIsMinMax;
+    this.positionToTransparency = positionToTransparency;
   }
 }