1 package jalview.ext.pymol;
4 import java.util.ArrayList;
7 import jalview.structure.AtomSpecModel;
8 import jalview.structure.StructureCommand;
9 import jalview.structure.StructureCommandI;
10 import jalview.structure.StructureCommandsBase;
13 * A class that generates commands to send to PyMol over its XML-RPC interface.
15 * Note that because the xml-rpc interface can only accept one command at a
16 * time, we can't concatenate commands, and must instead form and send them
19 * @see https://pymolwiki.org/index.php/Category:Commands
20 * @see https://pymolwiki.org/index.php/RPC
22 public class PymolCommands extends StructureCommandsBase
24 private static final StructureCommand COLOUR_BY_CHAIN = new StructureCommand("spectrum", "chain");
26 private static final List<StructureCommandI> COLOR_BY_CHARGE = new ArrayList<>();
28 private static final List<StructureCommandI> SHOW_BACKBONE = new ArrayList<>();
31 COLOR_BY_CHARGE.add(new StructureCommand("color", "white", "*"));
33 .add(new StructureCommand("color", "red", "resn ASP resn GLU"));
35 new StructureCommand("color", "blue", "resn LYS resn ARG"));
37 .add(new StructureCommand("color", "yellow", "resn CYS"));
38 SHOW_BACKBONE.add(new StructureCommand("hide", "everything"));
39 SHOW_BACKBONE.add(new StructureCommand("show", "ribbon"));
43 public StructureCommandI colourByChain()
45 return COLOUR_BY_CHAIN;
49 public List<StructureCommandI> colourByCharge()
51 return COLOR_BY_CHARGE;
55 public StructureCommandI setBackgroundColour(Color col)
57 // https://pymolwiki.org/index.php/Bg_Color
58 return new StructureCommand("bg_color", getColourString(col));
62 * Returns a colour formatted suitable for use in viewer command syntax. For
63 * example, red is {@code "0xff0000"}.
68 protected String getColourString(Color c)
70 return String.format("0x%02x%02x%02x", c.getRed(), c.getGreen(),
75 public StructureCommandI focusView()
82 public List<StructureCommandI> showChains(List<String> toShow)
84 // https://pymolwiki.org/index.php/Show
85 List<StructureCommandI> commands = new ArrayList<>();
86 commands.add(new StructureCommand("hide", "everything"));
87 commands.add(new StructureCommand("show", "lines"));
88 StringBuilder chains = new StringBuilder();
89 for (String chain : toShow)
91 chains.append(" chain ").append(chain);
93 commands.add(new StructureCommand("show", "cartoon", chains.toString()));
98 public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
99 AtomSpecModel atomSpec)
101 // https://pymolwiki.org/index.php/Super
102 List<StructureCommandI> commands = new ArrayList<>();
103 String refAtomsAlphaOnly = getAtomSpec(refAtoms, true);
104 String atomSpec2AlphaOnly = getAtomSpec(atomSpec, true);
105 commands.add(new StructureCommand("super", refAtomsAlphaOnly,
106 atomSpec2AlphaOnly));
109 * and show superposed residues as cartoon
111 String refAtomsAll = getAtomSpec(refAtoms, false);
112 String atomSpec2All = getAtomSpec(atomSpec, false);
113 commands.add(new StructureCommand("show", "cartoon",
114 refAtomsAll + " " + atomSpec2All));
120 public StructureCommandI openCommandFile(String path)
122 // https://pymolwiki.org/index.php/Run
123 return new StructureCommand("run", path); // should be .pml
127 public StructureCommandI saveSession(String filepath)
129 // https://pymolwiki.org/index.php/Save#EXAMPLES
130 return new StructureCommand("save", filepath); // should be .pse
134 * Returns a selection string in PyMOL 'selection macro' format:
137 * modelId// chain/residues/
140 * If more than one chain, makes a selection expression for each, and they are
141 * separated by spaces.
143 * @see https://pymolwiki.org/index.php/Selection_Macros
146 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
148 StringBuilder sb = new StringBuilder(64);
149 boolean first = true;
150 for (String modelId : model.getModels())
152 for (String chain : model.getChains(modelId))
159 List<int[]> rangeList = model.getRanges(modelId, chain);
160 chain = chain.trim();
161 sb.append(modelId).append("//").append(chain).append("/");
162 boolean firstRange = true;
163 for (int[] range : rangeList)
170 sb.append(String.valueOf(range[0]));
171 if (range[0] != range[1])
173 sb.append("-").append(String.valueOf(range[1]));
183 return sb.toString();
187 public List<StructureCommandI> showBackbone()
189 return SHOW_BACKBONE;
193 protected StructureCommandI getColourCommand(String atomSpec, Color colour)
195 // https://pymolwiki.org/index.php/Color
196 return new StructureCommand("color", getColourString(colour), atomSpec);
200 protected String getResidueSpec(String residue)
202 // https://pymolwiki.org/index.php/Selection_Algebra
203 return "resn " + residue;
207 public StructureCommandI loadFile(String file)
209 return new StructureCommand("load", file);