CurrentColour must not be accessed by multiple sequence renderers
[jalview.git] / src / jalview / schemes / ResidueColourScheme.java
index c435f7d..b905c45 100755 (executable)
@@ -1,6 +1,6 @@
 /*\r
 * Jalview - A Sequence Alignment Editor and Viewer\r
-* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+* Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
 *\r
 * This program is free software; you can redistribute it and/or\r
 * modify it under the terms of the GNU General Public License\r
@@ -41,7 +41,7 @@ public class ResidueColourScheme implements ColourSchemeI
     int threshold = 0;\r
 \r
     /* Set when threshold colouring to either pid_gaps or pid_nogaps*/\r
-    protected String ignoreGaps = "pid_gaps";\r
+    protected String ignoreGaps = AAFrequency.PID_GAPS;\r
 \r
     /** Consenus as a hashtable array */\r
     Hashtable [] consensus;\r
@@ -52,12 +52,6 @@ public class ResidueColourScheme implements ColourSchemeI
    /** DOCUMENT ME!! */\r
    int inc = 30;\r
 \r
-   /**\r
-    * The colour to be calculated, manipulated and returned\r
-    */\r
-   Color currentColour = null;\r
-\r
-\r
 \r
     /**\r
      * Creates a new ResidueColourScheme object.\r
@@ -83,15 +77,15 @@ public class ResidueColourScheme implements ColourSchemeI
     */\r
    public Color findColour(String aa)\r
    {\r
-       return colors[((Integer) (ResidueProperties.aaHash.get(aa))).intValue()];\r
+       return colors[ResidueProperties.aaIndex[aa.charAt(0)]];\r
    }\r
 \r
 \r
 \r
    public Color findColour(String s, int j)\r
    {\r
-\r
-       int index = ((Integer) (ResidueProperties.aaHash.get(s))).intValue();\r
+       Color currentColour;\r
+       int index = ResidueProperties.aaIndex[s.charAt(0)];\r
 \r
        if ((threshold == 0) || aboveThreshold(ResidueProperties.aa[index], j))\r
        {\r
@@ -103,7 +97,7 @@ public class ResidueColourScheme implements ColourSchemeI
        }\r
 \r
        if(conservationColouring)\r
-         applyConservation(j);\r
+         currentColour = applyConservation(currentColour, j);\r
 \r
 \r
        return currentColour;\r
@@ -129,9 +123,9 @@ public class ResidueColourScheme implements ColourSchemeI
     {\r
         threshold = ct;\r
         if(ignoreGaps)\r
-          this.ignoreGaps = "pid_nogaps";\r
+          this.ignoreGaps = AAFrequency.PID_NOGAPS;\r
         else\r
-          this.ignoreGaps = "pid_gaps";\r
+          this.ignoreGaps = AAFrequency.PID_GAPS;\r
     }\r
 \r
     /**\r
@@ -144,13 +138,25 @@ public class ResidueColourScheme implements ColourSchemeI
      */\r
     public boolean aboveThreshold(String s, int j)\r
     {\r
-        if ((((Integer) consensus[j].get("maxCount")).intValue() != -1) &&\r
-                consensus[j].contains(s))\r
+      char c = s.charAt(0);\r
+      if ('a' <= c && c <= 'z')\r
         {\r
-            if (((Float)consensus[j].get(ignoreGaps)).floatValue() >= threshold)\r
-            {\r
-                return true;\r
-            }\r
+          // TO UPPERCASE !!!\r
+          //Faster than toUpperCase\r
+          c -= ('a' - 'A');\r
+          s = String.valueOf(c);\r
+        }\r
+\r
+        if (consensus == null || consensus[j] == null)\r
+          return false;\r
+\r
+        if ( ( ( (Integer) consensus[j].get(AAFrequency.MAXCOUNT)).intValue() != -1) &&\r
+            consensus[j].contains(s))\r
+        {\r
+          if ( ( (Float) consensus[j].get(ignoreGaps)).floatValue() >= threshold)\r
+          {\r
+            return true;\r
+          }\r
         }\r
 \r
         return false;\r
@@ -177,15 +183,16 @@ public class ResidueColourScheme implements ColourSchemeI
      *\r
      * @param consensus DOCUMENT ME!\r
      */\r
-    public void setConsensus(Vector vconsensus)\r
+    public void setConsensus(Hashtable [] consensus)\r
     {\r
-       int i, iSize=vconsensus.size();\r
-       consensus = new Hashtable[iSize];\r
-       for(i=0; i<iSize; i++)\r
-        consensus[i] = (Hashtable)vconsensus.elementAt(i);\r
+      if(consensus==null)\r
+        return;\r
+\r
+      this.consensus = consensus;\r
     }\r
 \r
 \r
+\r
     public void setConservation(Conservation cons)\r
     {\r
       if(cons==null)\r
@@ -213,69 +220,47 @@ public class ResidueColourScheme implements ColourSchemeI
     *\r
     * @return DOCUMENT ME!\r
     */\r
-   void applyConservation(int i)\r
-   {\r
-       if ((conservation[i] != '*') && (conservation[i] != '+'))\r
-       {\r
-           int tmp = 10;\r
-           int t = 0;\r
-\r
-           if (!jalview.util.Comparison.isGap(conservation[i]))\r
-           {\r
-               t = conservation[i]-'0';\r
-           }\r
-\r
-           while (tmp >= t)\r
-           {\r
-               lighter(inc);\r
-               tmp--;\r
-           }\r
-       }\r
 \r
-   }\r
-\r
-    /**\r
-    * DOCUMENT ME!\r
-    *\r
-    * @param c DOCUMENT ME!\r
-    * @param inc DOCUMENT ME!\r
-    *\r
-    * @return DOCUMENT ME!\r
-    */\r
-   void lighter(int inc)\r
+   Color applyConservation(Color currentColour, int i)\r
    {\r
-       int red = currentColour.getRed();\r
-       int blue = currentColour.getBlue();\r
-       int green = currentColour.getGreen();\r
 \r
-       if (red < (255 - inc))\r
+     if ((conservation[i] != '*') && (conservation[i] != '+'))\r
+     {\r
+       if(jalview.util.Comparison.isGap(conservation[i]))\r
        {\r
-           red = red + inc;\r
+         currentColour = Color.white;\r
        }\r
        else\r
        {\r
-           red = 255;\r
-       }\r
+         float t = 11 - (conservation[i] - '0');\r
+         if(t==0)\r
+         {\r
+           return Color.white;\r
+         }\r
 \r
-       if (blue < (255 - inc))\r
-       {\r
-           blue = blue + inc;\r
-       }\r
-       else\r
-       {\r
-           blue = 255;\r
-       }\r
+         int red = currentColour.getRed();\r
+         int green = currentColour.getGreen();\r
+         int blue = currentColour.getBlue();\r
 \r
-       if (green < (255 - inc))\r
-       {\r
-           green = green + inc;\r
+         int dr = 255 - red;\r
+         int dg = 255 - green;\r
+         int db = 255 - blue;\r
+\r
+         dr *= t / 10f;\r
+         dg *= t / 10f;\r
+         db *= t / 10f;\r
+\r
+         red += (inc / 20f) * dr;\r
+         green += (inc / 20f) * dg;\r
+         blue += (inc / 20f) * db;\r
+\r
+         if (red > 255 || green > 255 || blue > 255)\r
+           currentColour = Color.white;\r
+         else\r
+           currentColour = new Color(red, green, blue);\r
        }\r
-       else\r
-       {\r
-           green = 255;\r
        }\r
-\r
-       currentColour = new Color(red, green, blue);\r
+       return currentColour;\r
    }\r
 \r
 \r