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 CLOSE_PYMOL = new StructureCommand("quit");
27 private static final StructureCommand COLOUR_BY_CHAIN = new StructureCommand("spectrum", "chain");
29 private static final List<StructureCommandI> COLOR_BY_CHARGE = new ArrayList<>();
31 private static final List<StructureCommandI> SHOW_BACKBONE = new ArrayList<>();
34 COLOR_BY_CHARGE.add(new StructureCommand("color", "white", "*"));
36 .add(new StructureCommand("color", "red", "resn ASP resn GLU"));
38 new StructureCommand("color", "blue", "resn LYS resn ARG"));
40 .add(new StructureCommand("color", "yellow", "resn CYS"));
41 SHOW_BACKBONE.add(new StructureCommand("hide", "everything"));
42 SHOW_BACKBONE.add(new StructureCommand("show", "ribbon"));
46 public StructureCommandI colourByChain()
48 return COLOUR_BY_CHAIN;
52 public List<StructureCommandI> colourByCharge()
54 return COLOR_BY_CHARGE;
58 public StructureCommandI setBackgroundColour(Color col)
60 // https://pymolwiki.org/index.php/Bg_Color
61 return new StructureCommand("bg_color", getColourString(col));
65 * Returns a colour formatted suitable for use in viewer command syntax. For
66 * example, red is {@code "0xff0000"}.
71 protected String getColourString(Color c)
73 return String.format("0x%02x%02x%02x", c.getRed(), c.getGreen(),
78 public StructureCommandI focusView()
85 public List<StructureCommandI> showChains(List<String> toShow)
87 // https://pymolwiki.org/index.php/Show
88 List<StructureCommandI> commands = new ArrayList<>();
89 commands.add(new StructureCommand("hide", "everything"));
90 commands.add(new StructureCommand("show", "lines"));
91 StringBuilder chains = new StringBuilder();
92 for (String chain : toShow)
94 chains.append(" chain ").append(chain);
96 commands.add(new StructureCommand("show", "cartoon", chains.toString()));
101 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
102 AtomSpecModel atomSpec)
104 // https://pymolwiki.org/index.php/Super
105 List<StructureCommandI> commands = new ArrayList<>();
106 String refAtomsAlphaOnly = getAtomSpec(refAtoms, true);
107 String atomSpec2AlphaOnly = getAtomSpec(atomSpec, true);
108 commands.add(new StructureCommand("super", refAtomsAlphaOnly,
109 atomSpec2AlphaOnly));
112 * and show superposed residues as cartoon
114 String refAtomsAll = getAtomSpec(refAtoms, false);
115 String atomSpec2All = getAtomSpec(atomSpec, false);
116 commands.add(new StructureCommand("show", "cartoon",
117 refAtomsAll + " " + atomSpec2All));
123 public StructureCommandI openCommandFile(String path)
125 // https://pymolwiki.org/index.php/Run
126 return new StructureCommand("run", path); // should be .pml
130 public StructureCommandI saveSession(String filepath)
132 // https://pymolwiki.org/index.php/Save#EXAMPLES
133 return new StructureCommand("save", filepath); // should be .pse
137 * Returns a selection string in PyMOL 'selection macro' format:
140 * modelId// chain/residues/
143 * If more than one chain, makes a selection expression for each, and they are
144 * separated by spaces.
146 * @see https://pymolwiki.org/index.php/Selection_Macros
149 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
151 StringBuilder sb = new StringBuilder(64);
152 boolean first = true;
153 for (String modelId : model.getModels())
155 for (String chain : model.getChains(modelId))
162 List<int[]> rangeList = model.getRanges(modelId, chain);
163 chain = chain.trim();
164 sb.append(modelId).append("//").append(chain).append("/");
165 boolean firstRange = true;
166 for (int[] range : rangeList)
173 sb.append(String.valueOf(range[0]));
174 if (range[0] != range[1])
176 sb.append("-").append(String.valueOf(range[1]));
186 return sb.toString();
190 public List<StructureCommandI> showBackbone()
192 return SHOW_BACKBONE;
196 protected StructureCommandI colourResidues(String atomSpec, Color colour)
198 // https://pymolwiki.org/index.php/Color
199 return new StructureCommand("color", getColourString(colour), atomSpec);
203 protected String getResidueSpec(String residue)
205 // https://pymolwiki.org/index.php/Selection_Algebra
206 return "resn " + residue;
210 public StructureCommandI loadFile(String file)
212 return new StructureCommand("load", file);
216 * Overrides the default implementation (which generates concatenated
217 * commands) to generate one per colour (because the XML-RPC interface to
218 * PyMOL only accepts one command at a time)
224 public List<StructureCommandI> colourBySequence(
225 Map<Object, AtomSpecModel> colourMap)
227 List<StructureCommandI> commands = new ArrayList<>();
228 for (Object key : colourMap.keySet())
230 Color colour = (Color) key;
231 final AtomSpecModel colourData = colourMap.get(colour);
232 commands.add(getColourCommand(colourData, colour));
239 * Returns a viewer command to set the given atom property value on atoms
240 * specified by the AtomSpecModel, for example
243 * iterate 4zho//B/12-34,48-55/CA,jv_chain='primary'
246 * @param attributeName
247 * @param attributeValue
248 * @param atomSpecModel
251 protected StructureCommandI setAttribute(String attributeName,
252 String attributeValue,
253 AtomSpecModel atomSpecModel)
255 StringBuilder sb = new StringBuilder(128);
256 sb.append("p.").append(attributeName).append("='")
257 .append(attributeValue).append("'");
258 String atomSpec = getAtomSpec(atomSpecModel, false);
259 return new StructureCommand("iterate", atomSpec, sb.toString());
263 * Traverse the map of features/values/models/chains/positions to construct a
264 * list of 'set property' commands (one per distinct feature type and value).
265 * The values are stored in the 'p' dictionary of user-defined properties of
268 * The format of each command is
271 * <blockquote> iterate atomspec, p.featureName='value'
272 * e.g. iterate 4zho//A/23,28-29/CA, p.jv_Metal='Fe'
280 public List<StructureCommandI> setAttributes(
281 Map<String, Map<Object, AtomSpecModel>> featureMap)
283 List<StructureCommandI> commands = new ArrayList<>();
284 for (String featureType : featureMap.keySet())
286 String attributeName = makeAttributeName(featureType);
289 * todo: clear down existing attributes for this feature?
291 // commands.add(new StructureCommand("iterate", "all",
292 // "p."+attributeName+"='None'"); //?
294 Map<Object, AtomSpecModel> values = featureMap.get(featureType);
295 for (Object value : values.keySet())
298 * for each distinct value recorded for this feature type,
299 * add a command to set the attribute on the mapped residues
300 * Put values in single quotes, encoding any embedded single quotes
302 AtomSpecModel atomSpecModel = values.get(value);
303 String featureValue = value.toString();
304 featureValue = featureValue.replaceAll("\\'", "'");
305 StructureCommandI cmd = setAttribute(attributeName, featureValue,
315 public StructureCommandI openSession(String filepath)
317 // https://pymolwiki.org/index.php/Load
318 // this version of the command has no dependency on file extension
319 return new StructureCommand("load", filepath, "", "0", "pse");
323 public StructureCommandI closeViewer()
325 // https://pymolwiki.org/index.php/Quit