Merge branch 'develop' into features/JAL-2360colourSchemeApplicability
[jalview.git] / src / jalview / schemes / AnnotationColourGradient.java
index ef77e4d..fb1443d 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -53,43 +53,18 @@ public class AnnotationColourGradient extends FollowerColourScheme
   private boolean predefinedColours = false;
 
   private boolean seqAssociated = false;
-  Color rnaHelices[] = null;
-  private void initRnaHelicesShading(int n)
-  {
-    int j = 0;
-    if (rnaHelices==null) 
-    {
-      rnaHelices = new Color[n + 1];
-    }
-    else
-    if (rnaHelices != null && rnaHelices.length <= n)
-    {
-      Color[] t = new Color[n + 1];
-      System.arraycopy(rnaHelices, 0, t, 0, rnaHelices.length);
-      j = rnaHelices.length;
-      rnaHelices = t;
-    }
-    else
-    {
-      return;
-    }
-    // Generate random colors and store
-    for (; j <= n; j++)
-    {
-      rnaHelices[j] = jalview.util.ColorUtils
-              .generateRandomColor(Color.white);
-    }
-  }
+
   /**
    * false if the scheme was constructed without a minColour and maxColour used
    * to decide if existing colours should be taken from annotation elements when
    * they exist
    */
   private boolean noGradient = false;
+
   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,
@@ -141,6 +116,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
     bb = 0;
 
     noGradient = true;
+    checkLimits();
   }
 
   /**
@@ -167,11 +143,18 @@ public class AnnotationColourGradient extends FollowerColourScheme
     bb = maxColour.getBlue() - b1;
 
     noGradient = false;
+    checkLimits();
+  }
+
+  private void checkLimits()
+  {
     aamax = annotation.graphMax;
     aamin = annotation.graphMin;
     if (annotation.isRNA())
     {
-      initRnaHelicesShading(1 + (int) aamax);
+      // reset colour palette
+      ColourSchemeProperty.resetRnaHelicesShading();
+      ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
     }
   }
 
@@ -194,7 +177,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
       // resolve the context containing all the annotation for the sequence
       AnnotatedCollectionI alcontext = alignment instanceof AlignmentI ? alignment
               : alignment.getContext();
-      boolean f = true,rna=false;
+      boolean f = true, rna = false;
       for (AlignmentAnnotation alan : alcontext.findAnnotation(annotation
               .getCalcId()))
       {
@@ -220,12 +203,13 @@ public class AnnotationColourGradient extends FollowerColourScheme
       }
       if (rna)
       {
-        initRnaHelicesShading(1 + (int) aamax);
+        ColourSchemeProperty.initRnaHelicesShading(1 + (int) aamax);
       }
     }
   }
 
   float aamin = 0f, aamax = 0f;
+
   public String getAnnotation()
   {
     return annotation.label;
@@ -266,6 +250,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
    * 
    * @return DOCUMENT ME!
    */
+  @Override
   public Color findColour(char c)
   {
     return Color.red;
@@ -285,8 +270,8 @@ public class AnnotationColourGradient extends FollowerColourScheme
   public Color findColour(char c, int j, SequenceI seq)
   {
     Color currentColour = Color.white;
-    AlignmentAnnotation annotation = (seqAssociated ? seqannot.get(seq)
-            : this.annotation);
+    AlignmentAnnotation annotation = (seqAssociated && seqannot != null ? seqannot
+            .get(seq) : this.annotation);
     if (annotation == null)
     {
       return currentColour;
@@ -313,7 +298,8 @@ public class AnnotationColourGradient extends FollowerColourScheme
                 || (annotationThreshold != null && (aboveAnnotationThreshold == ABOVE_THRESHOLD ? aj.value >= annotationThreshold.value
                         : aj.value <= annotationThreshold.value)))
         {
-          if (predefinedColours && aj.colour != null)
+          if (predefinedColours && aj.colour != null
+                  && !aj.colour.equals(Color.black))
           {
             currentColour = aj.colour;
           }
@@ -331,7 +317,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
               {
                 if (annotation.isRNA())
                 {
-                  currentColour = rnaHelices[(int) aj.value];
+                  currentColour = ColourSchemeProperty.rnaHelices[(int) aj.value];
                 }
                 else
                 {
@@ -414,6 +400,7 @@ public class AnnotationColourGradient extends FollowerColourScheme
     return new Color(dr, dg, db);
 
   }
+
   public boolean isPredefinedColours()
   {
     return predefinedColours;
@@ -433,4 +420,16 @@ public class AnnotationColourGradient extends FollowerColourScheme
   {
     seqAssociated = sassoc;
   }
+
+  @Override
+  public String getSchemeName()
+  {
+    return "Annotation";
+  }
+
+  @Override
+  public boolean isSimple()
+  {
+    return false;
+  }
 }