1 package jalview.ext.pymol;
4 import java.util.ArrayList;
5 import java.util.Arrays;
9 import jalview.structure.AtomSpecModel;
10 import jalview.structure.StructureCommand;
11 import jalview.structure.StructureCommandI;
12 import jalview.structure.StructureCommandsBase;
15 * A class that generates commands to send to PyMol over its XML-RPC interface.
17 * Note that because the xml-rpc interface can only accept one command at a
18 * time, we can't concatenate commands, and must instead form and send them
21 * @see https://pymolwiki.org/index.php/Category:Commands
22 * @see https://pymolwiki.org/index.php/RPC
24 public class PymolCommands extends StructureCommandsBase
26 // https://pymol.org/dokuwiki/doku.php?id=command:zoom
27 // not currently documented on
28 // https://pymolwiki.org/index.php/Category:Commands
29 private static final StructureCommand FOCUS_VIEW = new StructureCommand(
32 // https://pymolwiki.org/index.php/Quit
33 private static final StructureCommand CLOSE_PYMOL = new StructureCommand(
36 // not currently documented on
37 // https://pymolwiki.org/index.php/Category:Commands
38 private static final StructureCommand COLOUR_BY_CHAIN = new StructureCommand(
41 private static final List<StructureCommandI> COLOR_BY_CHARGE = Arrays
42 .asList(new StructureCommand("color", "white", "*"),
43 new StructureCommand("color", "red", "resn ASP resn GLU"),
44 new StructureCommand("color", "blue",
46 new StructureCommand("color", "yellow", "resn CYS"));
48 private static final List<StructureCommandI> SHOW_BACKBONE = Arrays
49 .asList(new StructureCommand("hide", "everything"),
50 new StructureCommand("show", "ribbon"));
53 public StructureCommandI colourByChain()
55 return COLOUR_BY_CHAIN;
59 public List<StructureCommandI> colourByCharge()
61 return COLOR_BY_CHARGE;
65 public StructureCommandI setBackgroundColour(Color col)
67 // https://pymolwiki.org/index.php/Bg_Color
68 return new StructureCommand("bg_color", getColourString(col));
72 * Returns a colour formatted suitable for use in viewer command syntax. For
73 * example, red is {@code "0xff0000"}.
78 protected String getColourString(Color c)
80 return String.format("0x%02x%02x%02x", c.getRed(), c.getGreen(),
85 public StructureCommandI focusView()
91 public List<StructureCommandI> showChains(List<String> toShow)
93 // https://pymolwiki.org/index.php/Show
94 List<StructureCommandI> commands = new ArrayList<>();
95 commands.add(new StructureCommand("hide", "everything"));
96 commands.add(new StructureCommand("show", "lines"));
97 StringBuilder chains = new StringBuilder();
98 for (String chain : toShow)
100 chains.append(" chain ").append(chain);
103 new StructureCommand("show", "cartoon", chains.toString()));
108 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
109 AtomSpecModel atomSpec, boolean isNucleotide)
111 // https://pymolwiki.org/index.php/Super
112 List<StructureCommandI> commands = new ArrayList<>();
113 String refAtomsAlphaOnly = "("+getAtomSpec(refAtoms, true)+" and (altloc '' or altloc 'a'))";
114 String atomSpec2AlphaOnly = "("+getAtomSpec(atomSpec, true)+" and (altloc '' or altloc 'a'))";
115 // pair_fit mobile -> reference
116 commands.add(new StructureCommand("pair_fit",
117 atomSpec2AlphaOnly, refAtomsAlphaOnly));
120 * and show superposed residues as cartoon
122 String refAtomsAll = getAtomSpec(refAtoms, false);
123 String atomSpec2All = getAtomSpec(atomSpec, false);
124 commands.add(new StructureCommand("show", "cartoon",
125 refAtomsAll + " " + atomSpec2All));
131 public StructureCommandI openCommandFile(String path)
133 // https://pymolwiki.org/index.php/Run
134 return new StructureCommand("run", path); // should be .pml
138 public StructureCommandI saveSession(String filepath)
140 // https://pymolwiki.org/index.php/Save#EXAMPLES
141 return new StructureCommand("save", filepath); // should be .pse
145 * Returns a selection string in PyMOL 'selection macro' format:
148 * modelId// chain/residues/
151 * If more than one chain, makes a selection expression for each, and they are
152 * separated by spaces.
154 * @see https://pymolwiki.org/index.php/Selection_Macros
157 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
159 StringBuilder sb = new StringBuilder(64);
160 boolean first = true;
161 for (String modelId : model.getModels())
163 for (String chain : model.getChains(modelId))
170 List<int[]> rangeList = model.getRanges(modelId, chain);
171 chain = chain.trim();
172 sb.append(modelId).append("//").append(chain).append("/");
173 boolean firstRange = true;
174 for (int[] range : rangeList)
181 sb.append(String.valueOf(range[0]));
182 if (range[0] != range[1])
184 sb.append("-").append(String.valueOf(range[1]));
194 return sb.toString();
198 public List<StructureCommandI> showBackbone()
200 return SHOW_BACKBONE;
204 protected StructureCommandI colourResidues(String atomSpec, Color colour)
206 // https://pymolwiki.org/index.php/Color
207 return new StructureCommand("color", getColourString(colour), atomSpec);
211 protected String getResidueSpec(String residue)
213 // https://pymolwiki.org/index.php/Selection_Algebra
214 return "resn " + residue;
218 public StructureCommandI loadFile(String file)
220 return new StructureCommand("load", file);
224 * Overrides the default implementation (which generates concatenated
225 * commands) to generate one per colour (because the XML-RPC interface to
226 * PyMOL only accepts one command at a time)
232 public List<StructureCommandI> colourBySequence(
233 Map<Object, AtomSpecModel> colourMap)
235 List<StructureCommandI> commands = new ArrayList<>();
236 for (Object key : colourMap.keySet())
238 Color colour = (Color) key;
239 final AtomSpecModel colourData = colourMap.get(colour);
240 commands.add(getColourCommand(colourData, colour));
247 * Returns a viewer command to set the given atom property value on atoms
248 * specified by the AtomSpecModel, for example
251 * iterate 4zho//B/12-34,48-55/CA,jv_chain='primary'
254 * @param attributeName
255 * @param attributeValue
256 * @param atomSpecModel
259 protected StructureCommandI setAttribute(String attributeName,
260 String attributeValue, AtomSpecModel atomSpecModel)
262 StringBuilder sb = new StringBuilder(128);
263 sb.append("p.").append(attributeName).append("='")
264 .append(attributeValue).append("'");
265 String atomSpec = getAtomSpec(atomSpecModel, false);
266 return new StructureCommand("iterate", atomSpec, sb.toString());
270 * Traverse the map of features/values/models/chains/positions to construct a
271 * list of 'set property' commands (one per distinct feature type and value).
272 * The values are stored in the 'p' dictionary of user-defined properties of
275 * The format of each command is
278 * <blockquote> iterate atomspec, p.featureName='value'
279 * e.g. iterate 4zho//A/23,28-29/CA, p.jv_Metal='Fe'
287 public List<StructureCommandI> setAttributes(
288 Map<String, Map<Object, AtomSpecModel>> featureMap)
290 List<StructureCommandI> commands = new ArrayList<>();
291 for (String featureType : featureMap.keySet())
293 String attributeName = makeAttributeName(featureType);
296 * todo: clear down existing attributes for this feature?
298 // commands.add(new StructureCommand("iterate", "all",
299 // "p."+attributeName+"='None'"); //?
301 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
302 for (Object value : values.keySet())
305 * for each distinct value recorded for this feature type,
306 * add a command to set the attribute on the mapped residues
307 * Put values in single quotes, encoding any embedded single quotes
309 AtomSpecModel atomSpecModel = values.get(value);
310 String featureValue = value.toString();
311 featureValue = featureValue.replaceAll("\\'", "'");
312 StructureCommandI cmd = setAttribute(attributeName, featureValue,
322 public StructureCommandI openSession(String filepath)
324 // https://pymolwiki.org/index.php/Load
325 // this version of the command has no dependency on file extension
326 return new StructureCommand("load", filepath, "", "0", "pse");
330 public StructureCommandI closeViewer()
332 // https://pymolwiki.org/index.php/Quit