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