f6ef68e258ec8a6bc42b9d43b3210c83ea4d747c
[jalview.git] / test / jalview / ext / pymol / PymolCommandsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.pymol;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertTrue;
25
26 import java.awt.Color;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
34
35 import jalview.ext.rbvi.chimera.ChimeraCommands;
36 import jalview.structure.AtomSpecModel;
37 import jalview.structure.StructureCommand;
38 import jalview.structure.StructureCommandI;
39
40 public class PymolCommandsTest
41 {
42   private PymolCommands testee;
43
44   @BeforeClass(alwaysRun = true)
45   public void setUp()
46   {
47     testee = new PymolCommands();
48   }
49
50   @Test(groups = { "Functional" })
51   public void testColourBySequence()
52   {
53
54     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
55     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
56     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
57     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
58     PymolCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
59     PymolCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
60     PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
61     PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
62     PymolCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
63     PymolCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
64
65     // Colours should appear in the Pymol command in the order in which
66     // they were added; within colour, by model, by chain, ranges in start order
67     List<StructureCommandI> commands = testee.colourBySequence(map);
68     assertEquals(commands.size(), 3);
69     assertEquals(commands.get(0), new StructureCommand("color", "0x0000ff",
70             "0//A/2-5+9-23/ 0//B/7/ 1//A/1/ 1//B/4-7/"));
71     assertEquals(commands.get(
72             1),
73             new StructureCommand("color", "0xffff00", "1//A/3-5+8/"));
74     assertEquals(commands.get(
75             2),
76             new StructureCommand("color", "0xff0000", "0//A/3-9/"));
77   }
78
79   @Test(groups = "Functional")
80   public void testGetAtomSpec()
81   {
82     AtomSpecModel model = new AtomSpecModel();
83     assertEquals(testee.getAtomSpec(model, false), "");
84     model.addRange("1", 2, 4, "A");
85     assertEquals(testee.getAtomSpec(model, false), "1//A/2-4/");
86     model.addRange("1", 8, 8, "A");
87     assertEquals(testee.getAtomSpec(model, false), "1//A/2-4+8/");
88     model.addRange("1", 5, 7, "B");
89     assertEquals(testee.getAtomSpec(model, false), "1//A/2-4+8/ 1//B/5-7/");
90     model.addRange("1", 3, 5, "A");
91     assertEquals(testee.getAtomSpec(model, false), "1//A/2-5+8/ 1//B/5-7/");
92     model.addRange("0", 1, 4, "B");
93     assertEquals(testee.getAtomSpec(model, false),
94             "0//B/1-4/ 1//A/2-5+8/ 1//B/5-7/");
95     model.addRange("0", 5, 9, "C");
96     assertEquals(testee.getAtomSpec(model, false),
97             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-7/");
98     model.addRange("1", 8, 10, "B");
99     assertEquals(testee.getAtomSpec(model, false),
100             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
101     model.addRange("1", 8, 9, "B");
102     assertEquals(testee.getAtomSpec(model, false),
103             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
104     model.addRange("0", 3, 10, "C"); // subsumes 5-9
105     assertEquals(testee.getAtomSpec(model, false),
106             "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/");
107     model.addRange("5", 25, 35, " ");
108     assertEquals(testee.getAtomSpec(model, false),
109             "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/ 5///25-35/");
110
111   }
112
113   @Test(groups = { "Functional" })
114   public void testSuperposeStructures()
115   {
116     AtomSpecModel ref = new AtomSpecModel();
117     ref.addRange("1", 12, 14, "A");
118     ref.addRange("1", 18, 18, "B");
119     ref.addRange("1", 22, 23, "B");
120     AtomSpecModel toAlign = new AtomSpecModel();
121     toAlign.addRange("2", 15, 17, "B");
122     toAlign.addRange("2", 20, 21, "B");
123     toAlign.addRange("2", 22, 22, "C");
124     List<StructureCommandI> commands = testee.superposeStructures(ref,
125             toAlign);
126     assertEquals(commands.size(), 2);
127     String refSpecCA = "1//A/12-14/CA 1//B/18+22-23/CA";
128     String toAlignSpecCA = "2//B/15-17+20-21/CA 2//C/22/CA";
129     String refSpec = "1//A/12-14/ 1//B/18+22-23/";
130     String toAlignSpec = "2//B/15-17+20-21/ 2//C/22/";
131
132     // super command: separate arguments for regions to align
133     assertEquals(commands.get(0),
134             new StructureCommand("super", refSpecCA, toAlignSpecCA));
135     // show aligned regions: one argument for combined atom specs
136     assertEquals(commands.get(1), new StructureCommand("show", "cartoon",
137             refSpec + " " + toAlignSpec));
138   }
139
140   @Test(groups = "Functional")
141   public void testGetAtomSpec_alphaOnly()
142   {
143     AtomSpecModel model = new AtomSpecModel();
144     assertEquals(testee.getAtomSpec(model, true), "");
145     model.addRange("1", 2, 4, "A");
146     assertEquals(testee.getAtomSpec(model, true), "1//A/2-4/CA");
147     model.addRange("1", 8, 8, "A");
148     assertEquals(testee.getAtomSpec(model, true), "1//A/2-4+8/CA");
149     model.addRange("1", 5, 7, "B");
150     assertEquals(testee.getAtomSpec(model, true),
151             "1//A/2-4+8/CA 1//B/5-7/CA");
152     model.addRange("1", 3, 5, "A");
153     assertEquals(testee.getAtomSpec(model, true),
154             "1//A/2-5+8/CA 1//B/5-7/CA");
155     model.addRange("0", 1, 4, "B");
156     assertEquals(testee.getAtomSpec(model, true),
157             "0//B/1-4/CA 1//A/2-5+8/CA 1//B/5-7/CA");
158     model.addRange("0", 5, 9, "C");
159     assertEquals(testee.getAtomSpec(model, true),
160             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-7/CA");
161     model.addRange("1", 8, 10, "B");
162     assertEquals(testee.getAtomSpec(model, true),
163             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
164     model.addRange("1", 8, 9, "B");
165     assertEquals(testee.getAtomSpec(model, true),
166             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
167     model.addRange("0", 3, 10, "C"); // subsumes 5-9
168     assertEquals(testee.getAtomSpec(model, true),
169             "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA");
170     model.addRange("5", 25, 35, " ");
171     assertEquals(testee.getAtomSpec(model, true),
172             "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA 5///25-35/CA");
173   }
174
175   @Test(groups = "Functional")
176   public void testGetResidueSpec()
177   {
178     assertEquals(testee.getResidueSpec("ALA"), "resn ALA");
179   }
180
181   @Test(groups = "Functional")
182   public void testShowBackbone()
183   {
184     List<StructureCommandI> cmds = testee.showBackbone();
185     assertEquals(cmds.size(), 2);
186     assertEquals(cmds.get(0), new StructureCommand("hide", "everything"));
187     assertEquals(cmds.get(1), new StructureCommand("show", "ribbon"));
188   }
189
190   @Test(groups = "Functional")
191   public void testColourByCharge()
192   {
193     List<StructureCommandI> cmds = testee.colourByCharge();
194     assertEquals(cmds.size(), 4);
195     assertEquals(cmds.get(0), new StructureCommand("color", "white", "*"));
196     assertEquals(cmds.get(1),
197             new StructureCommand("color", "red", "resn ASP resn GLU"));
198     assertEquals(cmds.get(2),
199             new StructureCommand("color", "blue", "resn LYS resn ARG"));
200     assertEquals(cmds.get(3),
201             new StructureCommand("color", "yellow", "resn CYS"));
202   }
203
204   @Test(groups = "Functional")
205   public void testOpenCommandFile()
206   {
207     assertEquals(testee.openCommandFile("commands.pml"),
208             new StructureCommand("run", "commands.pml"));
209   }
210
211   @Test(groups = "Functional")
212   public void testSaveSession()
213   {
214     assertEquals(testee.saveSession("somewhere.pse"),
215             new StructureCommand("save", "somewhere.pse"));
216   }
217
218   @Test(groups = "Functional")
219   public void testOpenSession()
220   {
221     assertEquals(testee.openSession("/some/path"),
222             new StructureCommand("load", "/some/path", "", "0", "pse"));
223   }
224
225   @Test(groups = "Functional")
226   public void testColourByChain()
227   {
228     assertEquals(testee.colourByChain(),
229             new StructureCommand("spectrum", "chain"));
230   }
231
232   @Test(groups = "Functional")
233   public void testColourResidues()
234   {
235     assertEquals(testee.colourResidues("something",
236             Color.MAGENTA),
237             new StructureCommand("color", "0xff00ff", "something"));
238   }
239
240   @Test(groups = "Functional")
241   public void testLoadFile()
242   {
243     assertEquals(testee.loadFile("/some/path"),
244             new StructureCommand("load", "/some/path"));
245   }
246
247   @Test(groups = "Functional")
248   public void testSetBackgroundColour()
249   {
250     assertEquals(testee.setBackgroundColour(
251             Color.PINK),
252             new StructureCommand("bg_color", "0xffafaf"));
253   }
254
255   @Test(groups = "Functional")
256   public void testSetAttribute()
257   {
258     AtomSpecModel model = new AtomSpecModel();
259     model.addRange("1", 89, 92, "A");
260     model.addRange("2", 12, 20, "B");
261     model.addRange("2", 8, 9, "B");
262     assertEquals(testee.setAttribute("jv_kd", "27.3", model),
263             new StructureCommand("iterate", "1//A/89-92/ 2//B/8-9+12-20/",
264                     "p.jv_kd='27.3'"));
265   }
266
267   @Test(groups = { "Functional" })
268   public void testSetAttributes()
269   {
270     /*
271      * make a map of { featureType, {featureValue, {residue range specification } } }
272      */
273     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
274     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
275
276     /*
277      * start with just one feature/value...
278      */
279     featuresMap.put("chain", featureValues);
280     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
281
282     List<StructureCommandI> commands = testee.setAttributes(featuresMap);
283     assertEquals(commands.size(), 1);
284
285     /*
286      * feature name gets a jv_ namespace prefix
287      */
288     assertEquals(commands.get(0), new StructureCommand("iterate",
289             "0//A/8-20/", "p.jv_chain='X'"));
290
291     // add same feature value, overlapping range
292     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
293     // same feature value, contiguous range
294     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
295     commands = testee.setAttributes(featuresMap);
296     assertEquals(commands.size(), 1);
297     assertEquals(commands.get(0), new StructureCommand("iterate",
298             "0//A/3-25/", "p.jv_chain='X'"));
299
300     // same feature value and model, different chain
301     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
302     // same feature value and chain, different model
303     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
304     commands = testee.setAttributes(featuresMap);
305     assertEquals(commands.size(), 1);
306     StructureCommand expected1 = new StructureCommand("iterate",
307             "0//A/3-25/ 0//B/21-25/ 1//A/26-30/", "p.jv_chain='X'");
308     assertEquals(commands.get(0), expected1);
309
310     // same feature, different value
311     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
312     commands = testee.setAttributes(featuresMap);
313     assertEquals(2, commands.size());
314     // commands are ordered by feature type but not by value
315     // so test for the expected command in either order
316     StructureCommandI cmd1 = commands.get(0);
317     StructureCommandI cmd2 = commands.get(1);
318     StructureCommand expected2 = new StructureCommand("iterate",
319             "0//A/40-50/", "p.jv_chain='Y'");
320     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
321     // String expected2 = "setattr #0/A:40-50 res jv_chain 'Y' create true";
322     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
323
324     featuresMap.clear();
325     featureValues.clear();
326     featuresMap.put("side-chain binding!", featureValues);
327     ChimeraCommands.addAtomSpecRange(featureValues,
328             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
329     // feature names are sanitised to change non-alphanumeric to underscore
330     // feature values are sanitised to encode single quote characters
331     commands = testee.setAttributes(featuresMap);
332     assertEquals(commands.size(), 1);
333     StructureCommandI expected3 = new StructureCommand("iterate",
334             "0//A/7-15/",
335             "p.jv_side_chain_binding_='<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!'");
336     assertEquals(commands.get(0), expected3);
337   }
338
339   @Test(groups = "Functional")
340   public void testHideAll()
341   {
342     StructureCommandI cmd = testee.hideAll();
343     assertEquals(cmd, new StructureCommand("hide", "everything"));
344   }
345
346   @Test(groups = "Functional")
347   public void testHideChain()
348   {
349     StructureCommandI cmd = testee.hideChain("1.1", "B");
350     assertEquals(cmd, new StructureCommand("hide", "1.1//B//"));
351   }
352
353   @Test(groups = "Functional")
354   public void testShowStructures()
355   {
356     /*
357      * with nothing excluded
358      */
359     StructureCommandI cmd = testee.showStructures(null);
360     assertEquals(cmd, new StructureCommand("show", "cartoon"));
361
362     /*
363      * restricted to specified positions
364      */
365     AtomSpecModel restrictTo = new AtomSpecModel();
366     restrictTo.addRange("1.1", 12, 20, "A");
367     restrictTo.addRange("1.1", 30, 35, "A");
368     restrictTo.addRange("2.1", 11, 30, "B");
369     cmd = testee.showStructures(restrictTo);
370     assertEquals(cmd, new StructureCommand("show", "cartoon",
371             "1.1//A/12-20+30-35/ 2.1//B/11-30/"));
372   }
373
374   public void testCloseViewer()
375   {
376     assertEquals(testee.closeViewer(), new StructureCommand("quit"));
377   }
378 }