36957f5548785aec9dc0043eff733c88fad798a8
[jalview.git] / src / jalview / ext / pymol / PymolCommands.java
1 package jalview.ext.pymol;
2
3 import java.awt.Color;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Map;
8
9 import jalview.structure.AtomSpecModel;
10 import jalview.structure.StructureCommand;
11 import jalview.structure.StructureCommandI;
12 import jalview.structure.StructureCommandsBase;
13
14 /**
15  * A class that generates commands to send to PyMol over its XML-RPC interface.
16  * <p>
17  * Note that because the xml-rpc interface can only accept one command at a
18  * time, we can't concatenate commands, and must instead form and send them
19  * individually.
20  * 
21  * @see https://pymolwiki.org/index.php/Category:Commands
22  * @see https://pymolwiki.org/index.php/RPC
23  */
24 public class PymolCommands extends StructureCommandsBase
25 {
26   // https://pymol.org/dokuwiki/doku.php?id=command:zoom
27   // not currently documented on
28   // https://pymolwiki.org/index.php/Category:Commands
29   private static final StructureCommand FOCUS_VIEW = new StructureCommand(
30           "zoom");
31
32   // https://pymolwiki.org/index.php/Quit
33   private static final StructureCommand CLOSE_PYMOL = new StructureCommand(
34           "quit");
35
36   // not currently documented on
37   // https://pymolwiki.org/index.php/Category:Commands
38   private static final StructureCommand COLOUR_BY_CHAIN = new StructureCommand(
39           "spectrum", "chain");
40
41   private static final List<StructureCommandI> COLOR_BY_CHARGE = Arrays
42           .asList(new StructureCommand("color", "white", "*"),
43                   new StructureCommand("color", "red", "resn ASP resn GLU"),
44                   new StructureCommand("color", "blue",
45                           "resn LYS resn ARG"),
46                   new StructureCommand("color", "yellow", "resn CYS"));
47
48   private static final List<StructureCommandI> SHOW_BACKBONE = Arrays
49           .asList(new StructureCommand("hide", "everything"),
50                   new StructureCommand("show", "ribbon"));
51
52   @Override
53   public StructureCommandI colourByChain()
54   {
55     return COLOUR_BY_CHAIN;
56   }
57
58   @Override
59   public List<StructureCommandI> colourByCharge()
60   {
61     return COLOR_BY_CHARGE;
62   }
63
64   @Override
65   public StructureCommandI setBackgroundColour(Color col)
66   {
67     // https://pymolwiki.org/index.php/Bg_Color
68     return new StructureCommand("bg_color", getColourString(col));
69   }
70
71   /**
72    * Returns a colour formatted suitable for use in viewer command syntax. For
73    * example, red is {@code "0xff0000"}.
74    * 
75    * @param c
76    * @return
77    */
78   protected String getColourString(Color c)
79   {
80     return String.format("0x%02x%02x%02x", c.getRed(), c.getGreen(),
81             c.getBlue());
82   }
83
84   @Override
85   public StructureCommandI focusView()
86   {
87     return FOCUS_VIEW;
88   }
89
90   @Override
91   public List<StructureCommandI> showChains(List<String> toShow)
92   {
93     // https://pymolwiki.org/index.php/Show
94     List<StructureCommandI> commands = new ArrayList<>();
95     commands.add(new StructureCommand("hide", "everything"));
96     commands.add(new StructureCommand("show", "lines"));
97     StringBuilder chains = new StringBuilder();
98     for (String chain : toShow)
99     {
100       chains.append(" chain ").append(chain);
101     }
102     commands.add(
103             new StructureCommand("show", "cartoon", chains.toString()));
104     return commands;
105   }
106
107   @Override
108   public List<StructureCommandI> superposeStructures(AtomSpecModel refAtoms,
109           AtomSpecModel atomSpec)
110   {
111     // https://pymolwiki.org/index.php/Super
112     List<StructureCommandI> commands = new ArrayList<>();
113     String refAtomsAlphaOnly = getAtomSpec(refAtoms, true);
114     String atomSpec2AlphaOnly = getAtomSpec(atomSpec, true);
115     commands.add(new StructureCommand("super", refAtomsAlphaOnly,
116             atomSpec2AlphaOnly));
117
118     /*
119      * and show superposed residues as cartoon
120      */
121     String refAtomsAll = getAtomSpec(refAtoms, false);
122     String atomSpec2All = getAtomSpec(atomSpec, false);
123     commands.add(new StructureCommand("show", "cartoon",
124             refAtomsAll + " " + atomSpec2All));
125
126     return commands;
127   }
128
129   @Override
130   public StructureCommandI openCommandFile(String path)
131   {
132     // https://pymolwiki.org/index.php/Run
133     return new StructureCommand("run", path); // should be .pml
134   }
135
136   @Override
137   public StructureCommandI saveSession(String filepath)
138   {
139     // https://pymolwiki.org/index.php/Save#EXAMPLES
140     return new StructureCommand("save", filepath); // should be .pse
141   }
142
143   /**
144    * Returns a selection string in PyMOL 'selection macro' format:
145    * 
146    * <pre>
147    * modelId// chain/residues/
148    * </pre>
149    * 
150    * If more than one chain, makes a selection expression for each, and they are
151    * separated by spaces.
152    * 
153    * @see https://pymolwiki.org/index.php/Selection_Macros
154    */
155   @Override
156   public String getAtomSpec(AtomSpecModel model, boolean alphaOnly)
157   {
158     StringBuilder sb = new StringBuilder(64);
159     boolean first = true;
160     for (String modelId : model.getModels())
161     {
162       for (String chain : model.getChains(modelId))
163       {
164         if (!first)
165         {
166           sb.append(" ");
167         }
168         first = false;
169         List<int[]> rangeList = model.getRanges(modelId, chain);
170         chain = chain.trim();
171         sb.append(modelId).append("//").append(chain).append("/");
172         boolean firstRange = true;
173         for (int[] range : rangeList)
174         {
175           if (!firstRange)
176           {
177             sb.append("+");
178           }
179           firstRange = false;
180           sb.append(String.valueOf(range[0]));
181           if (range[0] != range[1])
182           {
183             sb.append("-").append(String.valueOf(range[1]));
184           }
185         }
186         sb.append("/");
187         if (alphaOnly)
188         {
189           sb.append("CA");
190         }
191       }
192     }
193     return sb.toString();
194   }
195
196   @Override
197   public List<StructureCommandI> showBackbone()
198   {
199     return SHOW_BACKBONE;
200   }
201
202   @Override
203   protected StructureCommandI colourResidues(String atomSpec, Color colour)
204   {
205     // https://pymolwiki.org/index.php/Color
206     return new StructureCommand("color", getColourString(colour), atomSpec);
207   }
208
209   @Override
210   protected String getResidueSpec(String residue)
211   {
212     // https://pymolwiki.org/index.php/Selection_Algebra
213     return "resn " + residue;
214   }
215
216   @Override
217   public StructureCommandI loadFile(String file)
218   {
219     return new StructureCommand("load", file);
220   }
221
222   /**
223    * Overrides the default implementation (which generates concatenated
224    * commands) to generate one per colour (because the XML-RPC interface to
225    * PyMOL only accepts one command at a time)
226    * 
227    * @param colourMap
228    * @return
229    */
230   @Override
231   public List<StructureCommandI> colourBySequence(
232           Map<Object, AtomSpecModel> colourMap)
233   {
234     List<StructureCommandI> commands = new ArrayList<>();
235     for (Object key : colourMap.keySet())
236     {
237       Color colour = (Color) key;
238       final AtomSpecModel colourData = colourMap.get(colour);
239       commands.add(getColourCommand(colourData, colour));
240     }
241
242     return commands;
243   }
244
245   /**
246    * Returns a viewer command to set the given atom property value on atoms
247    * specified by the AtomSpecModel, for example
248    * 
249    * <pre>
250    * iterate 4zho//B/12-34,48-55/CA,jv_chain='primary'
251    * </pre>
252    * 
253    * @param attributeName
254    * @param attributeValue
255    * @param atomSpecModel
256    * @return
257    */
258   protected StructureCommandI setAttribute(String attributeName,
259           String attributeValue, AtomSpecModel atomSpecModel)
260   {
261     StringBuilder sb = new StringBuilder(128);
262     sb.append("p.").append(attributeName).append("='")
263             .append(attributeValue).append("'");
264     String atomSpec = getAtomSpec(atomSpecModel, false);
265     return new StructureCommand("iterate", atomSpec, sb.toString());
266   }
267
268   /**
269    * Traverse the map of features/values/models/chains/positions to construct a
270    * list of 'set property' commands (one per distinct feature type and value).
271    * The values are stored in the 'p' dictionary of user-defined properties of
272    * each atom.
273    * <p>
274    * The format of each command is
275    * 
276    * <pre>
277    * <blockquote> iterate atomspec, p.featureName='value' 
278    * e.g. iterate 4zho//A/23,28-29/CA, p.jv_Metal='Fe'
279    * </blockquote>
280    * </pre>
281    * 
282    * @param featureMap
283    * @return
284    */
285   @Override
286   public List<StructureCommandI> setAttributes(
287           Map<String, Map<Object, AtomSpecModel>> featureMap)
288   {
289     List<StructureCommandI> commands = new ArrayList<>();
290     for (String featureType : featureMap.keySet())
291     {
292       String attributeName = makeAttributeName(featureType);
293
294       /*
295        * todo: clear down existing attributes for this feature?
296        */
297       // commands.add(new StructureCommand("iterate", "all",
298       // "p."+attributeName+"='None'"); //?
299
300       Map<Object, AtomSpecModel> values = featureMap.get(featureType);
301       for (Object value : values.keySet())
302       {
303         /*
304          * for each distinct value recorded for this feature type,
305          * add a command to set the attribute on the mapped residues
306          * Put values in single quotes, encoding any embedded single quotes
307          */
308         AtomSpecModel atomSpecModel = values.get(value);
309         String featureValue = value.toString();
310         featureValue = featureValue.replaceAll("\\'", "&#39;");
311         StructureCommandI cmd = setAttribute(attributeName, featureValue,
312                 atomSpecModel);
313         commands.add(cmd);
314       }
315     }
316
317     return commands;
318   }
319
320   @Override
321   public StructureCommandI openSession(String filepath)
322   {
323     // https://pymolwiki.org/index.php/Load
324     // this version of the command has no dependency on file extension
325     return new StructureCommand("load", filepath, "", "0", "pse");
326   }
327
328   @Override
329   public StructureCommandI closeViewer()
330   {
331     // https://pymolwiki.org/index.php/Quit
332     return CLOSE_PYMOL;
333   }
334
335 }