JAL-2422 updated unit test for ChimeraX command change
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraXCommandsTest.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.rbvi.chimera;
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.structure.AtomSpecModel;
36 import jalview.structure.StructureCommand;
37 import jalview.structure.StructureCommandI;
38
39 public class ChimeraXCommandsTest
40 {
41   private ChimeraXCommands testee;
42
43   @BeforeClass(alwaysRun = true)
44   public void setUp()
45   {
46     testee = new ChimeraXCommands();
47   }
48
49   @Test(groups = { "Functional" })
50   public void testColourByCharge()
51   {
52     List<StructureCommandI> cmd = testee.colourByCharge();
53     assertEquals(cmd.size(), 1);
54     assertEquals(cmd.get(0).getCommand(),
55             "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
56   }
57
58   @Test(groups = { "Functional" })
59   public void testColourByChain()
60   {
61     StructureCommandI cmd = testee.colourByChain();
62     assertEquals(cmd.getCommand(), "rainbow chain");
63   }
64
65   @Test(groups = { "Functional" })
66   public void testFocusView()
67   {
68     StructureCommandI cmd = testee.focusView();
69     assertEquals(cmd.getCommand(), "view");
70   }
71
72   @Test(groups = { "Functional" })
73   public void testSetBackgroundColour()
74   {
75     StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
76     assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
77   }
78
79   @Test(groups = { "Functional" })
80   public void testOpenSession()
81   {
82     StructureCommandI cmd = testee.openSession("/some/filepath");
83     assertEquals(cmd.getCommand(), "open /some/filepath format session");
84   }
85
86   @Test(groups = { "Functional" })
87   public void testColourBySequence()
88   {
89     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
90     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
91     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
92     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
93     ChimeraCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
94     ChimeraCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
95     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
96     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
97     ChimeraCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
98     ChimeraCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
99
100     /*
101      * Colours should appear in the Chimera command in the order in which
102      * they were added; within colour, by model, by chain, ranges in start order
103      */
104     List<StructureCommandI> commands = testee.colourBySequence(map);
105     assertEquals(commands.size(), 1);
106     assertEquals(commands.get(0).getCommand(),
107             "color #1/A:2-5,9-23/B:7|#2/A:1/B:4-7 #0000ff;color #2/A:3-5,8 #ffff00;color #1/A:3-9 #ff0000");
108   }
109
110   @Test(groups = { "Functional" })
111   public void testSetAttributes()
112   {
113     /*
114      * make a map of { featureType, {featureValue, {residue range specification } } }
115      */
116     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
117     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
118
119     /*
120      * start with just one feature/value...
121      */
122     featuresMap.put("chain", featureValues);
123     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
124
125     List<StructureCommandI> commands = testee.setAttributes(featuresMap);
126     assertEquals(commands.size(), 1);
127
128     /*
129      * feature name gets a jv_ namespace prefix
130      * feature value is quoted in case it contains spaces
131      */
132     assertEquals(commands.get(0).getCommand(),
133             "setattr #0/A:8-20 res jv_chain 'X' create true");
134
135     // add same feature value, overlapping range
136     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
137     // same feature value, contiguous range
138     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
139     commands = testee.setAttributes(featuresMap);
140     assertEquals(commands.size(), 1);
141     assertEquals(commands.get(0).getCommand(),
142             "setattr #0/A:3-25 res jv_chain 'X' create true");
143
144     // same feature value and model, different chain
145     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
146     // same feature value and chain, different model
147     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
148     commands = testee.setAttributes(featuresMap);
149     assertEquals(commands.size(), 1);
150     String expected1 = "setattr #0/A:3-25/B:21-25|#1/A:26-30 res jv_chain 'X' create true";
151     assertEquals(commands.get(0).getCommand(), expected1);
152
153     // same feature, different value
154     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
155     commands = testee.setAttributes(featuresMap);
156     assertEquals(2, commands.size());
157     // commands are ordered by feature type but not by value
158     // so test for the expected command in either order
159     String cmd1 = commands.get(0).getCommand();
160     String cmd2 = commands.get(1).getCommand();
161     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
162     String expected2 = "setattr #0/A:40-50 res jv_chain 'Y' create true";
163     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
164
165     featuresMap.clear();
166     featureValues.clear();
167     featuresMap.put("side-chain binding!", featureValues);
168     ChimeraCommands.addAtomSpecRange(featureValues,
169             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
170     // feature names are sanitised to change non-alphanumeric to underscore
171     // feature values are sanitised to encode single quote characters
172     commands = testee.setAttributes(featuresMap);
173     assertEquals(commands.size(), 1);
174     String expected3 = "setattr #0/A:7-15 res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' create true";
175     assertTrue(commands.get(0).getCommand().equals(expected3));
176   }
177
178   @Test(groups = { "Functional" })
179   public void testSuperposeStructures()
180   {
181     AtomSpecModel ref = new AtomSpecModel();
182     ref.addRange("1", 12, 14, "A");
183     ref.addRange("1", 18, 18, "B");
184     ref.addRange("1", 22, 23, "B");
185     AtomSpecModel toAlign = new AtomSpecModel();
186     toAlign.addRange("2", 15, 17, "B");
187     toAlign.addRange("2", 20, 21, "B");
188     toAlign.addRange("2", 22, 22, "C");
189     List<StructureCommandI> command = testee.superposeStructures(ref,
190             toAlign);
191     assertEquals(command.size(), 1);
192     String cmd = command.get(0).getCommand();
193     String refSpec = "#1/A:12-14/B:18,22-23";
194     String toAlignSpec = "#2/B:15-17,20-21/C:22";
195
196     /*
197      * superposition arguments include AlphaCarbon restriction,
198      * ribbon command does not
199      */
200     String expected = String.format(
201             "align %s@CA toAtoms %s@CA; ribbon %s|%s; view", toAlignSpec,
202             refSpec, toAlignSpec, refSpec);
203     assertEquals(cmd, expected);
204   }
205
206   @Test(groups = "Functional")
207   public void testGetAtomSpec()
208   {
209     AtomSpecModel model = new AtomSpecModel();
210     assertEquals(testee.getAtomSpec(model, false), "");
211     model.addRange("1", 2, 4, "A");
212     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-4");
213     model.addRange("1", 8, 8, "A");
214     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-4,8");
215     model.addRange("1", 5, 7, "B");
216     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-4,8/B:5-7");
217     model.addRange("1", 3, 5, "A");
218     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-5,8/B:5-7");
219     model.addRange("0", 1, 4, "B");
220     assertEquals(testee.getAtomSpec(model, false),
221             "#0/B:1-4|#1/A:2-5,8/B:5-7");
222     model.addRange("0", 5, 9, "C");
223     assertEquals(testee.getAtomSpec(model, false),
224             "#0/B:1-4/C:5-9|#1/A:2-5,8/B:5-7");
225     model.addRange("1", 8, 10, "B");
226     assertEquals(testee.getAtomSpec(model, false),
227             "#0/B:1-4/C:5-9|#1/A:2-5,8/B:5-10");
228     model.addRange("1", 8, 9, "B");
229     assertEquals(testee.getAtomSpec(model, false),
230             "#0/B:1-4/C:5-9|#1/A:2-5,8/B:5-10");
231     model.addRange("0", 3, 10, "C"); // subsumes 5-9
232     assertEquals(testee.getAtomSpec(model, false),
233             "#0/B:1-4/C:3-10|#1/A:2-5,8/B:5-10");
234     model.addRange("5", 25, 35, " ");
235     assertEquals(testee.getAtomSpec(model, false),
236             "#0/B:1-4/C:3-10|#1/A:2-5,8/B:5-10|#5/:25-35");
237   }
238
239   @Test(groups = "Functional")
240   public void testGetAtomSpec_alphaOnly()
241   {
242     AtomSpecModel model = new AtomSpecModel();
243     assertEquals(testee.getAtomSpec(model, true), "");
244     model.addRange("1", 2, 4, "A");
245     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-4@CA");
246     model.addRange("1", 8, 8, "A");
247     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-4,8@CA");
248     model.addRange("1", 5, 7, "B");
249     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-4,8/B:5-7@CA");
250     model.addRange("1", 3, 5, "A");
251     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-5,8/B:5-7@CA");
252     model.addRange("0", 1, 4, "B");
253     assertEquals(testee.getAtomSpec(model, true),
254             "#0/B:1-4@CA|#1/A:2-5,8/B:5-7@CA");
255     model.addRange("0", 5, 9, "C");
256     assertEquals(testee.getAtomSpec(model, true),
257             "#0/B:1-4/C:5-9@CA|#1/A:2-5,8/B:5-7@CA");
258     model.addRange("1", 8, 10, "B");
259     assertEquals(testee.getAtomSpec(model, true),
260             "#0/B:1-4/C:5-9@CA|#1/A:2-5,8/B:5-10@CA");
261     model.addRange("1", 8, 9, "B");
262     assertEquals(testee.getAtomSpec(model, true),
263             "#0/B:1-4/C:5-9@CA|#1/A:2-5,8/B:5-10@CA");
264     model.addRange("0", 3, 10, "C"); // subsumes 5-9
265     assertEquals(testee.getAtomSpec(model, true),
266             "#0/B:1-4/C:3-10@CA|#1/A:2-5,8/B:5-10@CA");
267     model.addRange("5", 25, 35, " "); // empty chain code
268     assertEquals(testee.getAtomSpec(model, true),
269             "#0/B:1-4/C:3-10@CA|#1/A:2-5,8/B:5-10@CA|#5/:25-35@CA");
270   }
271
272   @Test(groups = "Functional")
273   public void testGetModelStartNo()
274   {
275     assertEquals(testee.getModelStartNo(), 1);
276   }
277
278   @Test(groups = "Functional")
279   public void testGetResidueSpec()
280   {
281     assertEquals(testee.getResidueSpec("ALA"), ":ALA");
282   }
283
284   @Test(groups = "Functional")
285   public void testShowBackbone()
286   {
287     List<StructureCommandI> showBackbone = testee.showBackbone();
288     assertEquals(showBackbone.size(), 1);
289     assertEquals(showBackbone.get(0).getCommand(),
290             "~display all;~ribbon;show @CA|P atoms");
291   }
292
293   @Test(groups = "Functional")
294   public void testOpenCommandFile()
295   {
296     assertEquals(testee.openCommandFile("nowhere").getCommand(),
297             "open nowhere");
298   }
299
300   @Test(groups = "Functional")
301   public void testSaveSession()
302   {
303     assertEquals(testee.saveSession("somewhere").getCommand(),
304             "save somewhere format session");
305   }
306
307   @Test(groups = "Functional")
308   public void testGetColourCommand()
309   {
310     assertEquals(testee.colourResidues("something", Color.MAGENTA)
311             .getCommand(),
312             "color something #ff00ff");
313   }
314
315   @Test(groups = "Functional")
316   public void testSetAttribute()
317   {
318     AtomSpecModel model = new AtomSpecModel();
319     model.addRange("1", 89, 92, "A");
320     model.addRange("2", 12, 20, "B");
321     model.addRange("2", 8, 9, "B");
322     assertEquals(testee.setAttribute("jv_kd", "27.3", model)
323             .getCommand(),
324             "setattr #1/A:89-92|#2/B:8-9,12-20 res jv_kd '27.3' create true");
325   }
326
327   @Test(groups = "Functional")
328   public void testCloseViewer()
329   {
330     assertEquals(testee.closeViewer(), new StructureCommand("exit"));
331   }
332
333   @Test(groups = "Functional")
334   public void testGetSelectedResidues()
335   {
336     assertEquals(testee.getSelectedResidues(),
337             new StructureCommand("info selection level residue"));
338   }
339
340   @Test(groups = "Functional")
341   public void testStartNotifications()
342   {
343     List<StructureCommandI> cmds = testee.startNotifications("to here");
344     assertEquals(cmds.size(), 2);
345     assertEquals(cmds.get(0), new StructureCommand("info notify start models jalview prefix ModelChanged url to here"));
346     assertEquals(cmds.get(1), new StructureCommand("info notify start selection jalview prefix SelectionChanged url to here"));
347   }
348
349   @Test(groups = "Functional")
350   public void testStopNotifications()
351   {
352     List<StructureCommandI> cmds = testee.stopNotifications();
353     assertEquals(cmds.size(), 2);
354     assertEquals(cmds.get(0), new StructureCommand("info notify stop models jalview"));
355     assertEquals(cmds.get(1), new StructureCommand("info notify stop selection jalview"));
356   }
357
358   @Test(groups = "Functional")
359   public void testListResidueAttributes()
360   {
361     assertEquals(testee.listResidueAttributes(),
362             new StructureCommand("info resattr"));
363   }
364 }