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