1 package jalview.ext.pymol;
3 import jalview.structure.AtomSpecModel;
4 import jalview.structure.StructureCommand;
5 import jalview.structure.StructureCommandI;
6 import jalview.structure.StructureCommandsBase;
9 import java.util.ArrayList;
10 import java.util.List;
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 // where is this documented by PyMol?
123 // todo : xml-rpc answers 'method "@" is not supported'
124 return new StructureCommand("@" + 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);