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 COLOUR_BY_CHAIN = new StructureCommand("spectrum", "chain");
27 private static final List<StructureCommandI> COLOR_BY_CHARGE = new ArrayList<>();
29 private static final List<StructureCommandI> SHOW_BACKBONE = new ArrayList<>();
32 COLOR_BY_CHARGE.add(new StructureCommand("color", "white", "*"));
34 .add(new StructureCommand("color", "red", "resn ASP resn GLU"));
36 new StructureCommand("color", "blue", "resn LYS resn ARG"));
38 .add(new StructureCommand("color", "yellow", "resn CYS"));
39 SHOW_BACKBONE.add(new StructureCommand("hide", "everything"));
40 SHOW_BACKBONE.add(new StructureCommand("show", "ribbon"));
44 public StructureCommandI colourByChain()
46 return COLOUR_BY_CHAIN;
50 public List<StructureCommandI> colourByCharge()
52 return COLOR_BY_CHARGE;
56 public StructureCommandI setBackgroundColour(Color col)
58 // https://pymolwiki.org/index.php/Bg_Color
59 return new StructureCommand("bg_color", getColourString(col));
63 * Returns a colour formatted suitable for use in viewer command syntax. For
64 * example, red is {@code "0xff0000"}.
69 protected String getColourString(Color c)
71 return String.format("0x%02x%02x%02x", c.getRed(), c.getGreen(),
76 public StructureCommandI focusView()
83 public List<StructureCommandI> showChains(List<String> toShow)
85 // https://pymolwiki.org/index.php/Show
86 List<StructureCommandI> commands = new ArrayList<>();
87 commands.add(new StructureCommand("hide", "everything"));
88 commands.add(new StructureCommand("show", "lines"));
89 StringBuilder chains = new StringBuilder();
90 for (String chain : toShow)
92 chains.append(" chain ").append(chain);
94 commands.add(new StructureCommand("show", "cartoon", chains.toString()));
99 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
100 AtomSpecModel atomSpec)
102 // https://pymolwiki.org/index.php/Super
103 List<StructureCommandI> commands = new ArrayList<>();
104 String refAtomsAlphaOnly = getAtomSpec(refAtoms, true);
105 String atomSpec2AlphaOnly = getAtomSpec(atomSpec, true);
106 commands.add(new StructureCommand("super", refAtomsAlphaOnly,
107 atomSpec2AlphaOnly));
110 * and show superposed residues as cartoon
112 String refAtomsAll = getAtomSpec(refAtoms, false);
113 String atomSpec2All = getAtomSpec(atomSpec, false);
114 commands.add(new StructureCommand("show", "cartoon",
115 refAtomsAll + " " + atomSpec2All));
121 public StructureCommandI openCommandFile(String path)
123 // https://pymolwiki.org/index.php/Run
124 return new StructureCommand("run", path); // should be .pml
128 public StructureCommandI saveSession(String filepath)
130 // https://pymolwiki.org/index.php/Save#EXAMPLES
131 return new StructureCommand("save", filepath); // should be .pse
135 * Returns a selection string in PyMOL 'selection macro' format:
138 * modelId// chain/residues/
141 * If more than one chain, makes a selection expression for each, and they are
142 * separated by spaces.
144 * @see https://pymolwiki.org/index.php/Selection_Macros
147 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
149 StringBuilder sb = new StringBuilder(64);
150 boolean first = true;
151 for (String modelId : model.getModels())
153 for (String chain : model.getChains(modelId))
160 List<int[]> rangeList = model.getRanges(modelId, chain);
161 chain = chain.trim();
162 sb.append(modelId).append("//").append(chain).append("/");
163 boolean firstRange = true;
164 for (int[] range : rangeList)
171 sb.append(String.valueOf(range[0]));
172 if (range[0] != range[1])
174 sb.append("-").append(String.valueOf(range[1]));
184 return sb.toString();
188 public List<StructureCommandI> showBackbone()
190 return SHOW_BACKBONE;
194 protected StructureCommandI getColourCommand(String atomSpec, Color colour)
196 // https://pymolwiki.org/index.php/Color
197 return new StructureCommand("color", getColourString(colour), atomSpec);
201 protected String getResidueSpec(String residue)
203 // https://pymolwiki.org/index.php/Selection_Algebra
204 return "resn " + residue;
208 public StructureCommandI loadFile(String file)
210 return new StructureCommand("load", file);
214 * Overrides the default implementation (which generates concatenated
215 * commands) to generate one per colour (because the XML-RPC interface to
216 * PyMOL only accepts one command at a time)
222 public List<StructureCommandI> colourBySequence(
223 Map<Object, AtomSpecModel> colourMap)
225 List<StructureCommandI> commands = new ArrayList<>();
226 for (Object key : colourMap.keySet())
228 Color colour = (Color) key;
229 final AtomSpecModel colourData = colourMap.get(colour);
230 commands.add(getColourCommand(colourData, colour));