Merge commit 'alpha/update_2_12_for_2_11_2_series_merge^2' into HEAD
[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
27 import java.awt.Color;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 import jalview.structure.AtomSpecModel;
37 import jalview.structure.StructureCommand;
38 import jalview.structure.StructureCommandI;
39 public class ChimeraCommandsTest
40 {
41   private ChimeraCommands testee;
42
43   @BeforeClass(alwaysRun = true)
44   public void setUp()
45   {
46     testee = new ChimeraCommands();
47   }
48
49   @Test(groups = { "Functional" })
50   public void testColourBySequence()
51   {
52
53     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
54     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
55     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
56     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
57     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
58     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
59     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
60     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
61     ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
62     ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
63
64     // Colours should appear in the Chimera command in the order in which
65     // they were added; within colour, by model, by chain, ranges in start order
66     List<StructureCommandI> commands = testee.colourBySequence(map);
67     assertEquals(commands.size(), 1);
68     assertEquals(commands.get(0).getCommand(),
69             "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");
70   }
71
72   @Test(groups = { "Functional" })
73   public void testSetAttributes()
74   {
75     /*
76      * make a map of { featureType, {featureValue, {residue range specification } } }
77      */
78     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
79     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
80     /*
81      * start with just one feature/value...
82      */
83     featuresMap.put("chain", featureValues);
84     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
85
86     List<StructureCommandI> commands = testee.setAttributes(featuresMap);
87     assertEquals(1, commands.size());
88
89     /*
90      * feature name gets a jv_ namespace prefix
91      * feature value is quoted in case it contains spaces
92      */
93     assertEquals(commands.get(0).getCommand(),
94             "setattr res jv_chain 'X' #0:8-20.A");
95
96     // add same feature value, overlapping range
97     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
98     // same feature value, contiguous range
99     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
100     commands = testee.setAttributes(featuresMap);
101     assertEquals(1, commands.size());
102     assertEquals(commands.get(0).getCommand(),
103             "setattr res jv_chain 'X' #0:3-25.A");
104
105     // same feature value and model, different chain
106     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
107     // same feature value and chain, different model
108     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
109     commands = testee.setAttributes(featuresMap);
110     assertEquals(1, commands.size());
111     String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
112     assertEquals(commands.get(0).getCommand(), expected1);
113
114     // same feature, different value
115     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
116     commands = testee.setAttributes(featuresMap);
117     assertEquals(2, commands.size());
118     // commands are ordered by feature type but not by value
119     // so test for the expected command in either order
120     String cmd1 = commands.get(0).getCommand();
121     String cmd2 = commands.get(1).getCommand();
122     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
123     String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
124     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
125
126     featuresMap.clear();
127     featureValues.clear();
128     featuresMap.put("side-chain binding!", featureValues);
129     ChimeraCommands.addAtomSpecRange(featureValues,
130             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
131     // feature names are sanitised to change non-alphanumeric to underscore
132     // feature values are sanitised to encode single quote characters
133     commands = testee.setAttributes(featuresMap);
134     assertEquals(commands.size(), 1);
135     String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A";
136     assertTrue(commands.get(0).getCommand().equals(expected3));
137   }
138
139   /**
140    * Tests for the method that prefixes and sanitises a feature name so it can
141    * be used as a valid, namespaced attribute name in Chimera or PyMol
142    */
143   @Test(groups = { "Functional" })
144   public void testMakeAttributeName()
145   {
146     assertEquals(testee.makeAttributeName(null), "jv_");
147     assertEquals(testee.makeAttributeName(""), "jv_");
148     assertEquals(testee.makeAttributeName("helix"), "jv_helix");
149     assertEquals(testee.makeAttributeName(
150             "Hello World 24"),
151             "jv_Hello_World_24");
152     assertEquals(testee.makeAttributeName(
153             "!this is-a_very*{odd(name"),
154             "jv__this_is_a_very__odd_name");
155     // name ending in color gets underscore appended
156     assertEquals(testee.makeAttributeName("helixColor"), "jv_helixColor_");
157   }
158
159   @Test(groups = "Functional")
160   public void testGetAtomSpec()
161   {
162     AtomSpecModel model = new AtomSpecModel();
163     assertEquals(testee.getAtomSpec(model, false), "");
164     model.addRange("1", 2, 4, "A");
165     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
166     model.addRange("1", 8, 8, "A");
167     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
168     model.addRange("1", 5, 7, "B");
169     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
170     model.addRange("1", 3, 5, "A");
171     assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
172     model.addRange("0", 1, 4, "B");
173     assertEquals(testee.getAtomSpec(model, false),
174             "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
175     model.addRange("0", 5, 9, "C");
176     assertEquals(testee.getAtomSpec(model, false),
177             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
178     model.addRange("1", 8, 10, "B");
179     assertEquals(testee.getAtomSpec(model, false),
180             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
181     model.addRange("1", 8, 9, "B");
182     assertEquals(testee.getAtomSpec(model, false),
183             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
184     model.addRange("0", 3, 10, "C"); // subsumes 5-9
185     assertEquals(testee.getAtomSpec(model, false),
186             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
187     model.addRange("5", 25, 35, " ");
188     assertEquals(testee.getAtomSpec(model, false),
189             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
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
253   @Test(groups = "Functional")
254   public void testGetModelStartNo()
255   {
256     assertEquals(testee.getModelStartNo(), 0);
257   }
258
259   @Test(groups = "Functional")
260   public void testGetResidueSpec()
261   {
262     assertEquals(testee.getResidueSpec("ALA"), "::ALA");
263   }
264
265   @Test(groups = "Functional")
266   public void testShowBackbone()
267   {
268     List<StructureCommandI> cmds = testee.showBackbone();
269     assertEquals(cmds.size(), 1);
270     assertEquals(cmds.get(0).getCommand(),
271             "~display all;~ribbon;chain @CA|P");
272   }
273
274   @Test(groups = "Functional")
275   public void testOpenCommandFile()
276   {
277     assertEquals(testee.openCommandFile("nowhere").getCommand(),
278             "open cmd:nowhere");
279   }
280
281   @Test(groups = "Functional")
282   public void testSaveSession()
283   {
284     assertEquals(testee.saveSession("somewhere").getCommand(),
285             "save somewhere");
286   }
287
288   @Test(groups = "Functional")
289   public void testColourByChain()
290   {
291     assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
292   }
293
294   @Test(groups = { "Functional" })
295   public void testSetBackgroundColour()
296   {
297     StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
298     assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
299   }
300
301   @Test(groups = { "Functional" })
302   public void testLoadFile()
303   {
304     StructureCommandI cmd = testee.loadFile("/some/filepath");
305     assertEquals(cmd.getCommand(), "open /some/filepath");
306   }
307
308   @Test(groups = { "Functional" })
309   public void testOpenSession()
310   {
311     StructureCommandI cmd = testee.openSession("/some/filepath");
312     assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
313   }
314
315   @Test(groups = "Functional")
316   public void testColourByCharge()
317   {
318     List<StructureCommandI> cmds = testee.colourByCharge();
319     assertEquals(cmds.size(), 1);
320     assertEquals(cmds.get(0)
321             .getCommand(),
322             "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
323   }
324
325   @Test(groups = "Functional")
326   public void testGetColourCommand()
327   {
328     assertEquals(testee.colourResidues("something", Color.MAGENTA)
329             .getCommand(),
330             "color #ff00ff something");
331   }
332
333   @Test(groups = "Functional")
334   public void testFocusView()
335   {
336     assertEquals(testee.focusView().getCommand(), "focus");
337   }
338
339   @Test(groups = "Functional")
340   public void testSetAttribute()
341   {
342     AtomSpecModel model = new AtomSpecModel();
343     model.addRange("1", 89, 92, "A");
344     model.addRange("2", 12, 20, "B");
345     model.addRange("2", 8, 9, "B");
346     assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(),
347             "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
348   }
349
350   @Test(groups = "Functional")
351   public void testCloseViewer()
352   {
353     assertEquals(testee.closeViewer(), new StructureCommand("stop really"));
354   }
355
356   @Test(groups = "Functional")
357   public void testGetSelectedResidues()
358   {
359     assertEquals(testee.getSelectedResidues(),
360             new StructureCommand("list selection level residue"));
361   }
362
363   @Test(groups = "Functional")
364   public void testListResidueAttributes()
365   {
366     assertEquals(testee.listResidueAttributes(),
367             new StructureCommand("list resattr"));
368   }
369
370   @Test(groups = "Functional")
371   public void testGetResidueAttributes()
372   {
373     assertEquals(testee.getResidueAttributes("binding site"),
374             new StructureCommand("list residues attr 'binding site'"));
375   }
376
377   @Test(groups = "Functional")
378   public void testStartNotifications()
379   {
380     List<StructureCommandI> cmds = testee.startNotifications("to here");
381     assertEquals(cmds.size(), 2);
382     assertEquals(cmds.get(0), new StructureCommand("listen start models url to here"));
383     assertEquals(cmds.get(1), new StructureCommand("listen start select prefix SelectionChanged url to here"));
384   }
385
386   @Test(groups = "Functional")
387   public void testStopNotifications()
388   {
389     List<StructureCommandI> cmds = testee.stopNotifications();
390     assertEquals(cmds.size(), 2);
391     assertEquals(cmds.get(0), new StructureCommand("listen stop models"));
392     assertEquals(cmds.get(1), new StructureCommand("listen stop selection"));
393   }
394 }