49a93c2f97ef33b89bf4e791edcbf4806c600317
[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
43   public void setUp()
44   {
45     testee = new ChimeraCommands();
46   }
47
48   @Test(groups = { "Functional" })
49   public void testColourBySequence()
50   {
51
52     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
53     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
54     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
55     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
56     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
57     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
58     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
59     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
60     ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
61     ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
62
63     // Colours should appear in the Chimera command in the order in which
64     // they were added; within colour, by model, by chain, ranges in start order
65     List<StructureCommandI> commands = testee.colourBySequence(map);
66     assertEquals(commands.size(), 1);
67     assertEquals(commands.get(0).getCommand(),
68             "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");
69   }
70
71   @Test(groups = { "Functional" })
72   public void testSetAttributes()
73   {
74     /*
75      * make a map of { featureType, {featureValue, {residue range specification } } }
76      */
77     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
78     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
79
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
193   @Test(groups = { "Functional" })
194   public void testSuperposeStructures()
195   {
196     AtomSpecModel ref = new AtomSpecModel();
197     ref.addRange("1", 12, 14, "A");
198     ref.addRange("1", 18, 18, "B");
199     ref.addRange("1", 22, 23, "B");
200     AtomSpecModel toAlign = new AtomSpecModel();
201     toAlign.addRange("2", 15, 17, "B");
202     toAlign.addRange("2", 20, 21, "B");
203     toAlign.addRange("2", 22, 22, "C");
204     List<StructureCommandI> command = testee.superposeStructures(ref,
205             toAlign);
206     // qualifier to restrict match to CA and no altlocs
207     String carbonAlphas = "@CA&~@.B-Z&~@.2-9";
208     String refSpec = "#1:12-14.A,18.B,22-23.B";
209     String toAlignSpec = "#2:15-17.B,20-21.B,22.C";
210     String expected = String.format("match %s%s %s%s; ribbon %s|%s; focus",
211             toAlignSpec, carbonAlphas, refSpec, carbonAlphas, toAlignSpec,
212             refSpec);
213     assertEquals(command.get(0).getCommand(), expected);
214   }
215
216   @Test(groups = "Functional")
217   public void testGetAtomSpec_alphaOnly()
218   {
219     AtomSpecModel model = new AtomSpecModel();
220     assertEquals(testee.getAtomSpec(model, true), "");
221     model.addRange("1", 2, 4, "A");
222     assertEquals(testee.getAtomSpec(model, true),
223             "#1:2-4.A@CA&~@.B-Z&~@.2-9");
224     model.addRange("1", 8, 8, "A");
225     assertEquals(testee.getAtomSpec(model, true),
226             "#1:2-4.A,8.A@CA&~@.B-Z&~@.2-9");
227     model.addRange("1", 5, 7, "B");
228     assertEquals(testee.getAtomSpec(model, true),
229             "#1:2-4.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
230     model.addRange("1", 3, 5, "A");
231     assertEquals(testee.getAtomSpec(model, true),
232             "#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
233     model.addRange("0", 1, 4, "B");
234     assertEquals(testee.getAtomSpec(model, true),
235             "#0:1-4.B@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
236     model.addRange("0", 5, 9, "C");
237     assertEquals(testee.getAtomSpec(model, true),
238             "#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");
239     model.addRange("1", 8, 10, "B");
240     assertEquals(testee.getAtomSpec(model, true),
241             "#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");
242     model.addRange("1", 8, 9, "B");
243     assertEquals(testee.getAtomSpec(model, true),
244             "#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");
245     model.addRange("0", 3, 10, "C"); // subsumes 5-9
246     assertEquals(testee.getAtomSpec(model, true),
247             "#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");
248     model.addRange("5", 25, 35, " "); // empty chain code
249     assertEquals(testee.getAtomSpec(model, true),
250             "#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");
251
252   }
253
254   @Test(groups = "Functional")
255   public void testGetModelStartNo()
256   {
257     assertEquals(testee.getModelStartNo(), 0);
258   }
259
260   @Test(groups = "Functional")
261   public void testGetResidueSpec()
262   {
263     assertEquals(testee.getResidueSpec("ALA"), "::ALA");
264   }
265
266   @Test(groups = "Functional")
267   public void testShowBackbone()
268   {
269     List<StructureCommandI> cmds = testee.showBackbone();
270     assertEquals(cmds.size(), 1);
271     assertEquals(cmds.get(0).getCommand(),
272             "~display all;~ribbon;chain @CA|P");
273   }
274
275   @Test(groups = "Functional")
276   public void testOpenCommandFile()
277   {
278     assertEquals(testee.openCommandFile("nowhere").getCommand(),
279             "open cmd:nowhere");
280   }
281
282   @Test(groups = "Functional")
283   public void testSaveSession()
284   {
285     assertEquals(testee.saveSession("somewhere").getCommand(),
286             "save somewhere");
287   }
288
289   @Test(groups = "Functional")
290   public void testColourByChain()
291   {
292     assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
293   }
294
295   @Test(groups = { "Functional" })
296   public void testSetBackgroundColour()
297   {
298     StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
299     assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
300   }
301
302   @Test(groups = { "Functional" })
303   public void testLoadFile()
304   {
305     StructureCommandI cmd = testee.loadFile("/some/filepath");
306     assertEquals(cmd.getCommand(), "open /some/filepath");
307   }
308
309   @Test(groups = { "Functional" })
310   public void testOpenSession()
311   {
312     StructureCommandI cmd = testee.openSession("/some/filepath");
313     assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
314   }
315
316   @Test(groups = "Functional")
317   public void testColourByCharge()
318   {
319     List<StructureCommandI> cmds = testee.colourByCharge();
320     assertEquals(cmds.size(), 1);
321     assertEquals(cmds.get(0)
322             .getCommand(),
323             "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
324   }
325
326   @Test(groups = "Functional")
327   public void testGetColourCommand()
328   {
329     assertEquals(testee.colourResidues("something", Color.MAGENTA)
330             .getCommand(),
331             "color #ff00ff something");
332   }
333
334   @Test(groups = "Functional")
335   public void testFocusView()
336   {
337     assertEquals(testee.focusView().getCommand(), "focus");
338   }
339
340   @Test(groups = "Functional")
341   public void testSetAttribute()
342   {
343     AtomSpecModel model = new AtomSpecModel();
344     model.addRange("1", 89, 92, "A");
345     model.addRange("2", 12, 20, "B");
346     model.addRange("2", 8, 9, "B");
347     assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(),
348             "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
349   }
350 }