JAL-2630 first pass groovy colour scheme (with slight refactoring)
[jalview.git] / src / jalview / schemes / ResidueColourScheme.java
index a15ca20..7dbcced 100755 (executable)
 package jalview.schemes;
 
 import jalview.analysis.Conservation;
-import jalview.analysis.Profile;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
+import jalview.datamodel.ProfileI;
+import jalview.datamodel.ProfilesI;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
 import jalview.util.ColorUtils;
 import jalview.util.Comparison;
-import jalview.util.MessageManager;
 
 import java.awt.Color;
 import java.util.Map;
 
 /**
- * DOCUMENT ME!
- * 
- * @author $author$
- * @version $Revision$
+ * Base class for residue-based colour schemes
  */
-public class ResidueColourScheme implements ColourSchemeI
+public abstract class ResidueColourScheme implements ColourSchemeI
 {
+  public static final String NONE = "None";
+
+  public static final String USER_DEFINED = "User Defined";
+
+  /*
+   * lookup up by character value e.g. 'G' to the colors array index
+   * e.g. if symbolIndex['K'] = 11 then colors[11] is the colour for K
+   */
   final int[] symbolIndex;
 
   boolean conservationColouring = false;
 
+  /*
+   * colour for residue characters as indexed by symbolIndex
+   */
   Color[] colors = null;
 
   int threshold = 0;
@@ -54,7 +63,7 @@ public class ResidueColourScheme implements ColourSchemeI
   /*
    * Consensus data indexed by column
    */
-  Profile[] consensus;
+  ProfilesI consensus;
 
   /*
    * Conservation string as a char array 
@@ -103,6 +112,17 @@ public class ResidueColourScheme implements ColourSchemeI
   }
 
   /**
+   * Returns the colour for symbol 'A'. Intended for use in a 'fixed colour'
+   * colour scheme (for example a feature colour).
+   */
+  @Override
+  public Color findColour()
+  {
+    // TODO delete this method in favour of ColorUtils.parseColourString()?
+    return findColour('A');
+  }
+
+  /**
    * Find a colour without an index in a sequence
    */
   @Override
@@ -114,24 +134,46 @@ public class ResidueColourScheme implements ColourSchemeI
   @Override
   public Color findColour(char c, int j, SequenceI seq)
   {
-    Color currentColour;
+    Color colour = Color.white;
 
-    if (colors != null && symbolIndex != null && (threshold == 0)
-            || aboveThreshold(c, j))
+    if (colors != null && symbolIndex != null)
     {
-      currentColour = colors[symbolIndex[c]];
+      colour = colors[symbolIndex[c]];
     }
-    else
+    colour = adjustColour(c, j, colour);
+
+    return colour;
+  }
+
+  /**
+   * Adjusts colour by applying thresholding or conservation shading, if in
+   * force. That is
+   * <ul>
+   * <li>if there is a threshold set for colouring, and the residue doesn't
+   * match the consensus (or a joint consensus) residue, or the consensus score
+   * is not above the threshold, then the colour is set to white</li>
+   * <li>if conservation colouring is selected, the colour is faded by an amount
+   * depending on the conservation score for the column, and the conservation
+   * colour threshold</li>
+   * </ul>
+   * 
+   * @param symbol
+   * @param column
+   * @param colour
+   * @return
+   */
+  protected Color adjustColour(char symbol, int column, Color colour)
+  {
+    if (!aboveThreshold(symbol, column))
     {
-      currentColour = Color.white;
+      colour = Color.white;
     }
 
     if (conservationColouring)
     {
-      currentColour = applyConservation(currentColour, j);
+      colour = applyConservation(colour, column);
     }
-
-    return currentColour;
+    return colour;
   }
 
   /**
@@ -175,6 +217,10 @@ public class ResidueColourScheme implements ColourSchemeI
    */
   public boolean aboveThreshold(char residue, int column)
   {
+    if (threshold == 0)
+    {
+      return true;
+    }
     if ('a' <= residue && residue <= 'z')
     {
       // TO UPPERCASE !!!
@@ -182,19 +228,20 @@ public class ResidueColourScheme implements ColourSchemeI
       residue -= ('a' - 'A');
     }
 
-    if (consensus == null || consensus.length < column
-            || consensus[column] == null)
+    if (consensus == null)
     {
       return false;
     }
 
+    ProfileI profile = consensus.get(column);
+
     /*
      * test whether this is the consensus (or joint consensus) residue
      */
-    if (consensus[column].getModalResidue().contains(
-            String.valueOf(residue)))
+    if (profile != null
+            && profile.getModalResidue().contains(String.valueOf(residue)))
     {
-      if (consensus[column].getPercentageIdentity(ignoreGaps) >= threshold)
+      if (profile.getPercentageIdentity(ignoreGaps) >= threshold)
       {
         return true;
       }
@@ -234,7 +281,7 @@ public class ResidueColourScheme implements ColourSchemeI
    *          DOCUMENT ME!
    */
   @Override
-  public void setConsensus(Profile[] consensus)
+  public void setConsensus(ProfilesI consensus)
   {
     if (consensus == null)
     {
@@ -327,18 +374,75 @@ public class ResidueColourScheme implements ColourSchemeI
   {
   }
 
+  /**
+   * Answers false if the colour scheme is nucleotide or peptide specific, and
+   * the data does not match, else true. Override to modify or extend this test
+   * as required.
+   */
   @Override
-  public ColourSchemeI applyTo(AnnotatedCollectionI sg,
-          Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+  public boolean isApplicableTo(AnnotatedCollectionI ac)
   {
-    try
+    if (!isPeptideSpecific() && !isNucleotideSpecific())
     {
-      return getClass().newInstance();
-    } catch (Exception q)
+      return true;
+    }
+
+    /*
+     * inspect the data context (alignment) for residue type
+     */
+    boolean nucleotide = false;
+    if (ac instanceof AlignmentI)
     {
-      throw new Error(MessageManager.formatMessage(
-              "error.implementation_error_cannot_duplicate_colour_scheme",
-              new String[] { getClass().getName() }), q);
+      nucleotide = ((AlignmentI) ac).isNucleotide();
     }
+    else
+    {
+      AnnotatedCollectionI context = ac.getContext();
+      if (context instanceof AlignmentI)
+      {
+        nucleotide = ((AlignmentI) context).isNucleotide();
+      }
+      else
+      {
+        // not sure what's going on, play safe
+        return true;
+      }
+    }
+
+    /*
+     * does data type match colour scheme type?
+     */
+    return (nucleotide && isNucleotideSpecific())
+            || (!nucleotide && isPeptideSpecific());
+  }
+
+  /**
+   * Answers true if the colour scheme is normally only for peptide data
+   * 
+   * @return
+   */
+  public boolean isPeptideSpecific()
+  {
+    return false;
+  }
+
+  /**
+   * Answers true if the colour scheme is normally only for nucleotide data
+   * 
+   * @return
+   */
+  public boolean isNucleotideSpecific()
+  {
+    return false;
+  }
+
+  /**
+   * Default method returns true. Override this to return false in colour
+   * schemes that are not determined solely by the sequence symbol.
+   */
+  @Override
+  public boolean isSimple()
+  {
+    return true;
   }
 }