JAL-1596 first REST interface coded
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraCommands.java
index d3c8c09..4ee74aa 100644 (file)
@@ -22,17 +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.Hashtable;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 /**
  * Routines for generating Chimera commands for Jalview/Chimera binding
@@ -57,20 +56,22 @@ public class ChimeraCommands
   {
 
     ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
-    Hashtable<String,StringBuffer> colranges=new Hashtable<String,StringBuffer>();
+
+    /*
+     * 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++)
       {
@@ -90,12 +91,16 @@ 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))
@@ -107,7 +112,6 @@ public class ChimeraCommands
                 lastCol = null;
                 startPos = pos;
                 startModel = pdbfnum;
-                startChain = mapping[m].getChain();
               }
               lastCol = col;
               lastPos = pos;
@@ -124,7 +128,7 @@ public class ChimeraCommands
         }
       }
       // Finally, add the command set ready to be returned.
-      StringBuffer coms=new StringBuffer();
+      StringBuilder coms = new StringBuilder(256);
       for (String cr:colranges.keySet())
       {
         coms.append("color #"+cr+" "+colranges.get(cr)+";");
@@ -135,24 +139,34 @@ public class ChimeraCommands
     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)
+  /**
+   * 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 = ((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)
+    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 StringBuffer());
+      colranges.put(colstring, currange = new StringBuilder(256));
     }
-    if (currange.length()>0)
+    if (currange.length() > 0)
     {
       currange.append("|");
     }
-    currange.append("#" + startModel + ":" + ((startPos==lastPos) ? startPos : startPos + "-"
-            + lastPos) + "." + lastChain);
+    currange.append("#" + model + ":" + ((startPos==endPos) ? startPos : startPos + "-"
+            + endPos) + "." + chain);
   }
 
 }