1 package jalview.ext.pymol;
4 import java.util.ArrayList;
8 import jalview.structure.AtomSpecModel;
9 import jalview.structure.StructureCommand;
10 import jalview.structure.StructureCommandI;
11 import jalview.structure.StructureCommandsBase;
14 * A class that generates commands to send to PyMol over its XML-RPC interface.
16 * Note that because the xml-rpc interface can only accept one command at a
17 * time, we can't concatenate commands, and must instead form and send them
20 * @see https://pymolwiki.org/index.php/Category:Commands
21 * @see https://pymolwiki.org/index.php/RPC
23 public class PymolCommands extends StructureCommandsBase
25 private static final StructureCommand SHOW_RIBBON = new StructureCommand("show", "ribbon");
27 private static final StructureCommand SHOW_CARTOON = new StructureCommand("show", "cartoon");
29 private static final StructureCommand HIDE_EVERYTHING = new StructureCommand("hide", "everything");
31 private static final StructureCommand COLOUR_BY_CHAIN = new StructureCommand("spectrum", "chain");
33 private static final List<StructureCommandI> COLOR_BY_CHARGE = new ArrayList<>();
35 private static final List<StructureCommandI> SHOW_BACKBONE = new ArrayList<>();
38 COLOR_BY_CHARGE.add(new StructureCommand("color", "white", "*"));
40 .add(new StructureCommand("color", "red", "resn ASP resn GLU"));
42 new StructureCommand("color", "blue", "resn LYS resn ARG"));
44 .add(new StructureCommand("color", "yellow", "resn CYS"));
45 SHOW_BACKBONE.add(HIDE_EVERYTHING);
46 SHOW_BACKBONE.add(SHOW_RIBBON);
50 public StructureCommandI colourByChain()
52 return COLOUR_BY_CHAIN;
56 public List<StructureCommandI> colourByCharge()
58 return COLOR_BY_CHARGE;
62 public StructureCommandI setBackgroundColour(Color col)
64 // https://pymolwiki.org/index.php/Bg_Color
65 return new StructureCommand("bg_color", getColourString(col));
69 * Returns a colour formatted suitable for use in viewer command syntax. For
70 * example, red is {@code "0xff0000"}.
75 protected String getColourString(Color c)
77 return String.format("0x%02x%02x%02x", c.getRed(), c.getGreen(),
82 public StructureCommandI focusView()
89 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
90 AtomSpecModel atomSpec)
92 // https://pymolwiki.org/index.php/Super
93 List<StructureCommandI> commands = new ArrayList<>();
94 String refAtomsAlphaOnly = getAtomSpec(refAtoms, true);
95 String atomSpec2AlphaOnly = getAtomSpec(atomSpec, true);
96 commands.add(new StructureCommand("super", refAtomsAlphaOnly,
100 * and show superposed residues as cartoon
102 String refAtomsAll = getAtomSpec(refAtoms, false);
103 String atomSpec2All = getAtomSpec(atomSpec, false);
104 commands.add(new StructureCommand("show", "cartoon",
105 refAtomsAll + " " + atomSpec2All));
111 public StructureCommandI openCommandFile(String path)
113 // https://pymolwiki.org/index.php/Run
114 return new StructureCommand("run", path); // should be .pml
118 public StructureCommandI saveSession(String filepath)
120 // https://pymolwiki.org/index.php/Save#EXAMPLES
121 return new StructureCommand("save", filepath); // should be .pse
125 * Returns a selection string in PyMOL 'selection macro' format:
128 * modelId// chain/residues/
131 * If more than one chain, makes a selection expression for each, and they are
132 * separated by spaces.
134 * @see https://pymolwiki.org/index.php/Selection_Macros
137 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
139 StringBuilder sb = new StringBuilder(64);
140 boolean first = true;
141 for (String modelId : model.getModels())
143 for (String chain : model.getChains(modelId))
150 List<int[]> rangeList = model.getRanges(modelId, chain);
151 chain = chain.trim();
152 sb.append(modelId).append("//").append(chain).append("/");
153 boolean firstRange = true;
154 for (int[] range : rangeList)
161 sb.append(String.valueOf(range[0]));
162 if (range[0] != range[1])
164 sb.append("-").append(String.valueOf(range[1]));
174 return sb.toString();
178 public List<StructureCommandI> showBackbone()
180 return SHOW_BACKBONE;
184 protected StructureCommandI colourResidues(String atomSpec, Color colour)
186 // https://pymolwiki.org/index.php/Color
187 return new StructureCommand("color", getColourString(colour), atomSpec);
191 protected String getResidueSpec(String residue)
193 // https://pymolwiki.org/index.php/Selection_Algebra
194 return "resn " + residue;
198 public StructureCommandI loadFile(String file)
200 return new StructureCommand("load", file);
204 * Overrides the default implementation (which generates concatenated
205 * commands) to generate one per colour (because the XML-RPC interface to
206 * PyMOL only accepts one command at a time)
212 public List<StructureCommandI> colourBySequence(
213 Map<Object, AtomSpecModel> colourMap)
215 List<StructureCommandI> commands = new ArrayList<>();
216 for (Object key : colourMap.keySet())
218 Color colour = (Color) key;
219 final AtomSpecModel colourData = colourMap.get(colour);
220 commands.add(getColourCommand(colourData, colour));
227 * Returns a viewer command to set the given atom property value on atoms
228 * specified by the AtomSpecModel, for example
231 * iterate 4zho//B/12-34,48-55/CA,jv_chain='primary'
234 * @param attributeName
235 * @param attributeValue
236 * @param atomSpecModel
239 protected StructureCommandI setAttribute(String attributeName,
240 String attributeValue,
241 AtomSpecModel atomSpecModel)
243 StringBuilder sb = new StringBuilder(128);
244 sb.append("p.").append(attributeName).append("='")
245 .append(attributeValue).append("'");
246 String atomSpec = getAtomSpec(atomSpecModel, false);
247 return new StructureCommand("iterate", atomSpec, sb.toString());
251 * Traverse the map of features/values/models/chains/positions to construct a
252 * list of 'set property' commands (one per distinct feature type and value).
253 * The values are stored in the 'p' dictionary of user-defined properties of
256 * The format of each command is
259 * <blockquote> iterate atomspec, p.featureName='value'
260 * e.g. iterate 4zho//A/23,28-29/CA, p.jv_Metal='Fe'
268 public List<StructureCommandI> setAttributes(
269 Map<String, Map<Object, AtomSpecModel>> featureMap)
271 List<StructureCommandI> commands = new ArrayList<>();
272 for (String featureType : featureMap.keySet())
274 String attributeName = makeAttributeName(featureType);
277 * todo: clear down existing attributes for this feature?
279 // commands.add(new StructureCommand("iterate", "all",
280 // "p."+attributeName+"='None'"); //?
282 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
283 for (Object value : values.keySet())
286 * for each distinct value recorded for this feature type,
287 * add a command to set the attribute on the mapped residues
288 * Put values in single quotes, encoding any embedded single quotes
290 AtomSpecModel atomSpecModel = values.get(value);
291 String featureValue = value.toString();
292 featureValue = featureValue.replaceAll("\\'", "'");
293 StructureCommandI cmd = setAttribute(attributeName, featureValue,
303 public StructureCommandI openSession(String filepath)
305 // https://pymolwiki.org/index.php/Load
306 // this version of the command has no dependency on file extension
307 return new StructureCommand("load", filepath, "", "0", "pse");
311 public StructureCommandI showStructures(AtomSpecModel restrictTo)
313 if (restrictTo == null)
319 return new StructureCommand("show", "cartoon",
320 getAtomSpec(restrictTo, false));
325 public StructureCommandI hideChain(String modelId, String chainId)
327 return new StructureCommand("hide", modelId + "//" + chainId + "//");
331 public StructureCommandI hideAll()
333 return HIDE_EVERYTHING;