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
15 * @see https://pymolwiki.org/index.php/Category:Commands
17 public class PymolCommands extends StructureCommandsBase
19 private static final StructureCommandI COLOUR_BY_CHAIN = new StructureCommand(
23 * because the xml-rpc interface can only accept one command at a time, we can't
24 * concatenate commands and must instead form and send them individually
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"));
36 COLOR_BY_CHARGE.add(new StructureCommand("color"));
37 SHOW_BACKBONE.add(new StructureCommand("hide", "everything"));
38 SHOW_BACKBONE.add(new StructureCommand("show", "ribbon"));
42 public StructureCommandI colourByChain()
44 // https://pymolwiki.org/index.php/CBC
45 // TODO this doesn't execute as an xml-rpc command
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 // where is this documented by PyMol?
124 // todo : xml-rpc answers 'method "@" is not supported'
125 return new StructureCommand("@" + path); // should be .pml
129 public StructureCommandI saveSession(String filepath)
131 // https://pymolwiki.org/index.php/Save#EXAMPLES
132 return new StructureCommand("save", filepath); // should be .pse
136 * Returns a selection string in PyMOL 'selection macro' format:
139 * modelId// chain/residues/
142 * If more than one chain, makes a selection expression for each, and they are
143 * separated by spaces.
145 * @see https://pymolwiki.org/index.php/Selection_Macros
148 public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
150 StringBuilder sb = new StringBuilder(64);
151 boolean first = true;
152 for (String modelId : model.getModels())
154 for (String chain : model.getChains(modelId))
161 List<int[]> rangeList = model.getRanges(modelId, chain);
162 chain = chain.trim();
163 sb.append(modelId).append("//").append(chain).append("/");
164 boolean firstRange = true;
165 for (int[] range : rangeList)
172 sb.append(String.valueOf(range[0]));
173 if (range[0] != range[1])
175 sb.append("-").append(String.valueOf(range[1]));
185 return sb.toString();
189 public List<StructureCommandI> showBackbone()
191 return SHOW_BACKBONE;
195 protected StructureCommandI getColourCommand(String atomSpec, Color colour)
197 // https://pymolwiki.org/index.php/Color
198 return new StructureCommand("color", getColourString(colour), atomSpec);
202 protected String getResidueSpec(String residue)
204 // https://pymolwiki.org/index.php/Selection_Algebra
205 return "resn " + residue;
209 public StructureCommandI loadFile(String file)
211 return new StructureCommand("load", file);