JAL-4366 JAL-4371 allow cruft after inferred pdb ids, ex 1xyz_A.pdb yields 1xyzA
[jalview.git] / src / jalview / schemes / ClustalxColourScheme.java
index f13a90c..04097a8 100755 (executable)
  */
 package jalview.schemes;
 
+import java.awt.Color;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import jalview.api.AlignViewportI;
 import jalview.datamodel.AnnotatedCollectionI;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceI;
+import jalview.util.ColorUtils;
 import jalview.util.Comparison;
 
-import java.awt.Color;
-import java.util.List;
-import java.util.Map;
-
 public class ClustalxColourScheme extends ResidueColourScheme
 {
   private static final int EIGHTY_FIVE = 85;
@@ -43,8 +46,8 @@ public class ClustalxColourScheme extends ResidueColourScheme
   {
     RED(0.9f, 0.2f, 0.1f), BLUE(0.5f, 0.7f, 0.9f), GREEN(0.1f, 0.8f, 0.1f),
     ORANGE(0.9f, 0.6f, 0.3f), CYAN(0.1f, 0.7f, 0.7f),
-    PINK(0.9f, 0.5f, 0.5f), MAGENTA(0.8f, 0.3f, 0.8f), YELLOW(0.8f, 0.8f,
-            0.0f);
+    PINK(0.9f, 0.5f, 0.5f), MAGENTA(0.8f, 0.3f, 0.8f),
+    YELLOW(0.8f, 0.8f, 0.0f);
 
     final Color colour;
 
@@ -53,6 +56,7 @@ public class ClustalxColourScheme extends ResidueColourScheme
       colour = new Color(r, g, b);
     }
   }
+
   private class ConsensusColour
   {
     Consensus[] cons;
@@ -93,7 +97,7 @@ public class ClustalxColourScheme extends ResidueColourScheme
   }
 
   @Override
-  public void alignmentChanged(AnnotatedCollectionI alignment,
+  public synchronized void alignmentChanged(AnnotatedCollectionI alignment,
           Map<SequenceI, SequenceCollectionI> hiddenReps)
   {
     int maxWidth = alignment.getWidth();
@@ -106,19 +110,18 @@ public class ClustalxColourScheme extends ResidueColourScheme
 
     for (SequenceI sq : seqs)
     {
-      char[] seq = sq.getSequence();
-
-      int end_j = seq.length - 1;
+      int end_j = sq.getLength() - 1;
+      int length = sq.getLength();
 
       for (int i = 0; i <= end_j; i++)
       {
-        if ((seq.length - 1) < i)
+        if (length - 1 < i)
         {
           res = 23;
         }
         else
         {
-          res = ResidueProperties.aaIndex[seq[i]];
+          res = ResidueProperties.aaIndex[sq.getCharAt(i)];
         }
         cons2[i][res]++;
       }
@@ -277,7 +280,7 @@ public class ClustalxColourScheme extends ResidueColourScheme
   }
 
   @Override
-  protected Color findColour(char c, int j, SequenceI seq)
+  protected synchronized Color findColour(char c, int j, SequenceI seq)
   {
     // TODO why the test for includeGaps here?
     if (cons2.length <= j || Comparison.isGap(c)
@@ -297,8 +300,7 @@ public class ClustalxColourScheme extends ResidueColourScheme
 
     for (int k = 0; k < residueColour[i].cons.length; k++)
     {
-      if (residueColour[i].cons[k].isConserved(cons2, j, size,
-              includeGaps))
+      if (residueColour[i].cons[k].isConserved(cons2, j, size, includeGaps))
       {
         colour = residueColour[i].c;
       }
@@ -336,11 +338,11 @@ public class ClustalxColourScheme extends ResidueColourScheme
   }
 
   @Override
-  public ColourSchemeI getInstance(AnnotatedCollectionI sg,
-          Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+  public ColourSchemeI getInstance(AlignViewportI view,
+          AnnotatedCollectionI sg)
   {
     ClustalxColourScheme css = new ClustalxColourScheme(sg,
-            hiddenRepSequences);
+            view == null ? null : view.getHiddenRepSequences());
     css.includeGaps = includeGaps;
     return css;
   }
@@ -362,4 +364,63 @@ public class ClustalxColourScheme extends ResidueColourScheme
   {
     return false;
   }
+  public String toRuleRep()
+  {
+    makeColours();
+    HashMap<String, String> cols=new HashMap();
+    for (String res:ResidueProperties.aa) {
+      StringBuilder sb = new StringBuilder();
+      
+      int rnum=ResidueProperties.aaIndex[res.charAt(0)];
+      if (rnum<0 || rnum>=residueColour.length)
+      {
+        continue;
+      }
+      
+      ConsensusColour cc = residueColour[rnum];
+      if (cc==null)
+      {
+        continue;
+      }
+      //sb.append("Residue "+res+" ("+rnum+")");
+      //sb.append("\t");
+      sb.append(cc.c.toString());
+      double f=0;
+      sb.append("\t");
+      for (Consensus cons: cc.cons) {
+        if (cons.threshold==0 || f!=cons.threshold)
+        {
+          if (f!=0)
+          {
+
+              sb.append("}, {");
+          } else {
+          sb.append("{");
+          }
+        sb.append(cons.threshold);
+        sb.append(",");
+        f=cons.threshold;
+        } else {
+          sb.append(",");
+        }
+        sb.append(cons.maskstr);
+      }
+      sb.append("}");
+      String clxrep=sb.toString();
+      String xres = cols.get(clxrep);
+      if (xres==null) { xres = "";}
+      xres+=res;
+      cols.put(clxrep, xres);
+    }
+    StringBuilder sb = new StringBuilder();
+    for (String clxrep:cols.keySet())
+    {
+      sb.append(cols.get(clxrep));
+      sb.append("\t");
+      sb.append(clxrep);
+      sb.append("\n");
+      
+    }
+    return sb.toString();
+  }
 }