JAL-1596 first REST interface coded
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraCommands.java
index 41c7abb..4ee74aa 100644 (file)
@@ -22,16 +22,16 @@ package jalview.ext.rbvi.chimera;
 
 import jalview.api.FeatureRenderer;
 import jalview.api.SequenceRenderer;
-import jalview.api.structures.JalviewStructureDisplayI;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.SequenceI;
 import jalview.structure.StructureMapping;
 import jalview.structure.StructureMappingcommandSet;
 import jalview.structure.StructureSelectionManager;
-import jalview.util.Format;
 
 import java.awt.Color;
 import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 /**
  * Routines for generating Chimera commands for Jalview/Chimera binding
@@ -57,19 +57,21 @@ public class ChimeraCommands
 
     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
 
+    /*
+     * Map of { colour, positionSpecs}
+     */
+    Map<String, StringBuilder> colranges = new LinkedHashMap<String, StringBuilder>();
     for (int pdbfnum = 0; pdbfnum < files.length; pdbfnum++)
     {
-      float cols[] = new float[4];
       StructureMapping[] mapping = ssm.getMapping(files[pdbfnum]);
-      StringBuffer command = new StringBuffer();
-      StructureMappingcommandSet smc;
-      ArrayList<String> str = new ArrayList<String>();
 
       if (mapping == null || mapping.length < 1)
+      {
         continue;
+      }
 
       int startPos = -1, lastPos = -1, startModel = -1, lastModel = -1;
-      String startChain = "", lastChain = "";
+      String lastChain = "";
       Color lastCol = null;
       for (int s = 0; s < sequence[pdbfnum].length; s++)
       {
@@ -89,39 +91,27 @@ public class ChimeraCommands
               int pos = mapping[m].getPDBResNum(asp.findPosition(r));
 
               if (pos < 1 || pos == lastPos)
+              {
                 continue;
+              }
 
               Color col = sr.getResidueBoxColour(sequence[pdbfnum][s], r);
 
               if (fr != null)
+              {
                 col = fr.findFeatureColour(col, sequence[pdbfnum][s], r);
+              }
               if (lastCol != col || lastPos + 1 != pos
                       || pdbfnum != lastModel
                       || !mapping[m].getChain().equals(lastChain))
               {
                 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;
                 startModel = pdbfnum;
-                startChain = mapping[m].getChain();
               }
               lastCol = col;
               lastPos = pos;
@@ -131,32 +121,52 @@ 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.
+      StringBuilder coms = new StringBuilder(256);
+      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()]);
   }
 
+  /**
+   * Helper method to record a range of positions of the same colour.
+   * 
+   * @param colranges
+   * @param colour
+   * @param model
+   * @param startPos
+   * @param endPos
+   * @param chain
+   */
+  private static void addColourRange(Map<String, StringBuilder> colranges,
+          Color colour, int model,
+          int startPos, int endPos, String chain)
+  {
+    String colstring = ((colour.getRed()< 16) ? "0":"")+Integer.toHexString(colour.getRed())
+            + ((colour.getGreen()< 16) ? "0":"")+Integer.toHexString(colour.getGreen())
+            + ((colour.getBlue()< 16) ? "0":"")+Integer.toHexString(colour.getBlue());
+    StringBuilder currange = colranges.get(colstring);
+    if (currange == null)
+    {
+      colranges.put(colstring, currange = new StringBuilder(256));
+    }
+    if (currange.length() > 0)
+    {
+      currange.append("|");
+    }
+    currange.append("#" + model + ":" + ((startPos==endPos) ? startPos : startPos + "-"
+            + endPos) + "." + chain);
+  }
+
 }