JAL-2360 refactoring for JalviewColourScheme enum,
[jalview.git] / src / jalview / schemes / ResidueColourScheme.java
index c9abb0e..c1eeafa 100755 (executable)
 package jalview.schemes;
 
 import jalview.analysis.Conservation;
+import jalview.datamodel.AlignmentI;
 import jalview.datamodel.AnnotatedCollectionI;
-import jalview.datamodel.Profile;
+import jalview.datamodel.ProfileI;
+import jalview.datamodel.ProfilesI;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
 import jalview.util.ColorUtils;
@@ -33,13 +35,12 @@ 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 static final String NONE = "None";
+
   final int[] symbolIndex;
 
   boolean conservationColouring = false;
@@ -54,7 +55,7 @@ public class ResidueColourScheme implements ColourSchemeI
   /*
    * Consensus data indexed by column
    */
-  Profile[] consensus;
+  ProfilesI consensus;
 
   /*
    * Conservation string as a char array 
@@ -103,6 +104,16 @@ 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()
+  {
+    return findColour('A');
+  }
+
+  /**
    * Find a colour without an index in a sequence
    */
   @Override
@@ -182,19 +193,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 +246,7 @@ public class ResidueColourScheme implements ColourSchemeI
    *          DOCUMENT ME!
    */
   @Override
-  public void setConsensus(Profile[] consensus)
+  public void setConsensus(ProfilesI consensus)
   {
     if (consensus == null)
     {
@@ -341,4 +353,76 @@ public class ResidueColourScheme implements ColourSchemeI
               new String[] { getClass().getName() }), q);
     }
   }
+
+  /**
+   * Answers false if the colour scheme is nucleotide or peptide specific, and
+   * the data does not match, else false. Override to modify or extend this test
+   * as required.
+   */
+  @Override
+  public boolean isApplicableTo(AnnotatedCollectionI ac)
+  {
+    if (!isPeptideSpecific() && !isNucleotideSpecific())
+    {
+      return true;
+    }
+
+    /*
+     * inspect the data context (alignment dataset) for residue type
+     */
+    boolean nucleotide = false;
+    AnnotatedCollectionI context = ac.getContext();
+    if (context != null)
+    {
+      if (context instanceof AlignmentI)
+      {
+        nucleotide = ((AlignmentI) context).isNucleotide();
+      }
+      else
+      {
+        // not sure what's going on, play safe
+        return true;
+      }
+    }
+    else if (ac instanceof AlignmentI)
+    {
+      nucleotide = ((AlignmentI) ac).isNucleotide();
+    }
+    else
+    {
+      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;
+  }
+
+  @Override
+  public String getSchemeName()
+  {
+    return "Residue";
+  }
 }