JAL-1333 more abstraction and refactoring and working demo of Jalview talking to...
[jalview.git] / src / jalview / ext / rbvi / chimera / ChimeraCommands.java
diff --git a/src/jalview/ext/rbvi/chimera/ChimeraCommands.java b/src/jalview/ext/rbvi/chimera/ChimeraCommands.java
new file mode 100644 (file)
index 0000000..41c7abb
--- /dev/null
@@ -0,0 +1,162 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
+ * Copyright (C) 2014 The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+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;
+
+/**
+ * Routines for generating Chimera commands for Jalview/Chimera binding
+ * 
+ * @author JimP
+ * 
+ */
+public class ChimeraCommands
+{
+
+  /**
+   * utility to construct the commands to colour chains by the given alignment
+   * for passing to Chimera
+   * 
+   * @returns Object[] { Object[] { <model being coloured>,
+   * 
+   */
+  public static StructureMappingcommandSet[] getColourBySequenceCommand(
+          StructureSelectionManager ssm, String[] files,
+          SequenceI[][] sequence, SequenceRenderer sr, FeatureRenderer fr,
+          AlignmentI alignment)
+  {
+
+    ArrayList<StructureMappingcommandSet> cset = new ArrayList<StructureMappingcommandSet>();
+
+    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 = "";
+      Color lastCol = null;
+      for (int s = 0; s < sequence[pdbfnum].length; s++)
+      {
+        for (int sp, m = 0; m < mapping.length; m++)
+        {
+          if (mapping[m].getSequence() == sequence[pdbfnum][s]
+                  && (sp = alignment.findIndex(sequence[pdbfnum][s])) > -1)
+          {
+            SequenceI asp = alignment.getSequenceAt(sp);
+            for (int r = 0; r < asp.getLength(); r++)
+            {
+              // no mapping to gaps in sequence
+              if (jalview.util.Comparison.isGap(asp.getCharAt(r)))
+              {
+                continue;
+              }
+              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);
+                  }
+                }
+                lastCol = null;
+                startPos = pos;
+                startModel = pdbfnum;
+                startChain = mapping[m].getChain();
+              }
+              lastCol = col;
+              lastPos = pos;
+              lastModel = pdbfnum;
+              lastChain = mapping[m].getChain();
+            }
+            // 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);
+              }
+            }
+            break;
+          }
+        }
+      }
+      // Finally, add the command set ready to be returned.
+      cset.add(new StructureMappingcommandSet(ChimeraCommands.class,
+              files[pdbfnum], str.toArray(new String[str.size()])));
+    }
+    return cset.toArray(new StructureMappingcommandSet[cset.size()]);
+  }
+
+}