JAL-1333 more efficient color statement
authorj.procter@dundee.ac.uk <jprocter@Jims-MacBook-Pro-2.local>
Tue, 8 Jul 2014 19:15:11 +0000 (15:15 -0400)
committerj.procter@dundee.ac.uk <jprocter@Jims-MacBook-Pro-2.local>
Tue, 8 Jul 2014 19:15:11 +0000 (15:15 -0400)
src/jalview/ext/rbvi/chimera/ChimeraCommands.java

index 41c7abb..d3c8c09 100644 (file)
@@ -32,6 +32,7 @@ import jalview.util.Format;
 
 import java.awt.Color;
 import java.util.ArrayList;
+import java.util.Hashtable;
 
 /**
  * Routines for generating Chimera commands for Jalview/Chimera binding
@@ -56,7 +57,7 @@ public class ChimeraCommands
   {
 
     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
-
+    Hashtable<String,StringBuffer> colranges=new Hashtable<String,StringBuffer>();
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
       float cols[] = new float[4];
@@ -101,22 +102,7 @@ public class ChimeraCommands
               {
                 if (lastCol != null)
                 {
-
-                  lastCol.getRGBComponents(cols);
-                  String newSelcom = "color " + cols[0] + "," + cols[1]
-                          + "," + cols[2] + " #" + startModel + ":"
-                          + startPos + "-" + lastPos + "." + lastChain;
-                  if (str.size() > 0
-                          && (str.get(str.size() - 1).length() + newSelcom
-                                  .length()) < 4096)
-                  {
-                    str.set(str.size() - 1, str.get(str.size() - 1) + ";"
-                            + newSelcom);
-                  }
-                  else
-                  {
-                    str.add(newSelcom);
-                  }
+                  addColourRange(colranges, lastCol,startModel,startPos,lastPos,lastChain); 
                 }
                 lastCol = null;
                 startPos = pos;
@@ -131,32 +117,42 @@ public class ChimeraCommands
             // final colour range
             if (lastCol != null)
             {
-
-              lastCol.getRGBComponents(cols);
-              String newSelcom = "color " + cols[0] + "," + cols[1] + ","
-                      + cols[2] + " #" + startModel + ":" + startPos + "-"
-                      + lastPos + "." + lastChain;
-              if (str.size() > 0
-                      && (str.get(str.size() - 1).length() + newSelcom
-                              .length()) < 4096)
-              {
-                str.set(str.size() - 1, str.get(str.size() - 1) + ";"
-                        + newSelcom);
-              }
-              else
-              {
-                str.add(newSelcom);
-              }
+              addColourRange(colranges, lastCol,startModel,startPos,lastPos,lastChain); 
             }
             break;
           }
         }
       }
       // Finally, add the command set ready to be returned.
+      StringBuffer coms=new StringBuffer();
+      for (String cr:colranges.keySet())
+      {
+        coms.append("color #"+cr+" "+colranges.get(cr)+";");
+      }
       cset.add(new StructureMappingcommandSet(ChimeraCommands.class,
-              files[pdbfnum], str.toArray(new String[str.size()])));
+              files[pdbfnum], new String[] { coms.toString() }));
     }
     return cset.toArray(new StructureMappingcommandSet[cset.size()]);
   }
 
+  private static void addColourRange(Hashtable<String, StringBuffer> colranges, Color lastCol, int startModel,
+          int startPos, int lastPos, String lastChain)
+  {
+    
+    String colstring = ((lastCol.getRed()< 16) ? "0":"")+Integer.toHexString(lastCol.getRed())
+            + ((lastCol.getGreen()< 16) ? "0":"")+Integer.toHexString(lastCol.getGreen())
+            + ((lastCol.getBlue()< 16) ? "0":"")+Integer.toHexString(lastCol.getBlue());
+    StringBuffer currange = colranges.get(colstring);
+    if (currange==null)
+    {
+      colranges.put(colstring,currange = new StringBuffer());
+    }
+    if (currange.length()>0)
+    {
+      currange.append("|");
+    }
+    currange.append("#" + startModel + ":" + ((startPos==lastPos) ? startPos : startPos + "-"
+            + lastPos) + "." + lastChain);
+  }
+
 }