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