7441feeece8c505510f2f4c9f2cbb3f288820b96
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraXCommandsTest.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 ChimeraXCommandsTest
39 {
40   private ChimeraXCommands testee;
41
42   @BeforeClass
43   public void setUp()
44   {
45     testee = new ChimeraXCommands();
46   }
47
48   @Test(groups = { "Functional" })
49   public void testColourByCharge()
50   {
51     List<StructureCommandI> cmd = testee.colourByCharge();
52     assertEquals(cmd.size(), 1);
53     assertEquals(cmd.get(0).getCommand(),
54             "color white;color :ASP,GLU red;color :LYS,ARG blue;color :CYS yellow");
55   }
56
57   @Test(groups = { "Functional" })
58   public void testColourByChain()
59   {
60     StructureCommandI cmd = testee.colourByChain();
61     assertEquals(cmd.getCommand(), "rainbow chain");
62   }
63
64   @Test(groups = { "Functional" })
65   public void testFocusView()
66   {
67     StructureCommandI cmd = testee.focusView();
68     assertEquals(cmd.getCommand(), "view");
69   }
70
71   @Test(groups = { "Functional" })
72   public void testSetBackgroundColour()
73   {
74     StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
75     assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
76   }
77
78   @Test(groups = { "Functional" })
79   public void testOpenSession()
80   {
81     StructureCommandI cmd = testee.openSession("/some/filepath");
82     assertEquals(cmd.getCommand(), "open /some/filepath format session");
83   }
84
85   @Test(groups = { "Functional" })
86   public void testColourBySequence()
87   {
88     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
89     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
90     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
91     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
92     ChimeraCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
93     ChimeraCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
94     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
95     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
96     ChimeraCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
97     ChimeraCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
98
99     /*
100      * Colours should appear in the Chimera command in the order in which
101      * they were added; within colour, by model, by chain, ranges in start order
102      */
103     List<StructureCommandI> commands = testee.colourBySequence(map);
104     assertEquals(commands.size(), 1);
105     assertEquals(commands.get(0).getCommand(),
106             "color #1/A:2-5,9-23/B:7|#2/A:1/B:4-7 #0000ff;color #2/A:3-5,8 #ffff00;color #1/A:3-9 #ff0000");
107   }
108
109   @Test(groups = { "Functional" })
110   public void testSetAttributes()
111   {
112     /*
113      * make a map of { featureType, {featureValue, {residue range specification } } }
114      */
115     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
116     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
117
118     /*
119      * start with just one feature/value...
120      */
121     featuresMap.put("chain", featureValues);
122     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
123
124     List<StructureCommandI> commands = testee.setAttributes(featuresMap);
125     assertEquals(commands.size(), 1);
126
127     /*
128      * feature name gets a jv_ namespace prefix
129      * feature value is quoted in case it contains spaces
130      */
131     assertEquals(commands.get(0).getCommand(),
132             "setattr #0/A:8-20 res jv_chain 'X' create true");
133
134     // add same feature value, overlapping range
135     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
136     // same feature value, contiguous range
137     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
138     commands = testee.setAttributes(featuresMap);
139     assertEquals(commands.size(), 1);
140     assertEquals(commands.get(0).getCommand(),
141             "setattr #0/A:3-25 res jv_chain 'X' create true");
142
143     // same feature value and model, different chain
144     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
145     // same feature value and chain, different model
146     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
147     commands = testee.setAttributes(featuresMap);
148     assertEquals(commands.size(), 1);
149     String expected1 = "setattr #0/A:3-25/B:21-25|#1/A:26-30 res jv_chain 'X' create true";
150     assertEquals(commands.get(0).getCommand(), expected1);
151
152     // same feature, different value
153     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
154     commands = testee.setAttributes(featuresMap);
155     assertEquals(2, commands.size());
156     // commands are ordered by feature type but not by value
157     // so test for the expected command in either order
158     String cmd1 = commands.get(0).getCommand();
159     String cmd2 = commands.get(1).getCommand();
160     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
161     String expected2 = "setattr #0/A:40-50 res jv_chain 'Y' create true";
162     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
163
164     featuresMap.clear();
165     featureValues.clear();
166     featuresMap.put("side-chain binding!", featureValues);
167     ChimeraCommands.addAtomSpecRange(featureValues,
168             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
169     // feature names are sanitised to change non-alphanumeric to underscore
170     // feature values are sanitised to encode single quote characters
171     commands = testee.setAttributes(featuresMap);
172     assertEquals(commands.size(), 1);
173     String expected3 = "setattr #0/A:7-15 res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' create true";
174     assertTrue(commands.get(0).getCommand().equals(expected3));
175   }
176
177   @Test(groups = { "Functional" })
178   public void testSuperposeStructures()
179   {
180     AtomSpecModel ref = new AtomSpecModel();
181     ref.addRange("1", 12, 14, "A");
182     ref.addRange("1", 18, 18, "B");
183     ref.addRange("1", 22, 23, "B");
184     AtomSpecModel toAlign = new AtomSpecModel();
185     toAlign.addRange("2", 15, 17, "B");
186     toAlign.addRange("2", 20, 21, "B");
187     toAlign.addRange("2", 22, 22, "C");
188     List<StructureCommandI> command = testee.superposeStructures(ref,
189             toAlign);
190     assertEquals(command.size(), 1);
191     String cmd = command.get(0).getCommand();
192     String refSpec = "#1/A:12-14/B:18,22-23";
193     String toAlignSpec = "#2/B:15-17,20-21/C:22";
194
195     /*
196      * superposition arguments include AlphaCarbon restriction,
197      * ribbon command does not
198      */
199     String expected = String.format(
200             "align %s@CA toAtoms %s@CA; ribbon %s|%s; view", toAlignSpec,
201             refSpec, toAlignSpec, refSpec);
202     assertEquals(cmd, expected);
203   }
204
205   @Test(groups = "Functional")
206   public void testGetAtomSpec()
207   {
208     AtomSpecModel model = new AtomSpecModel();
209     assertEquals(testee.getAtomSpec(model, false), "");
210     model.addRange("1", 2, 4, "A");
211     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-4");
212     model.addRange("1", 8, 8, "A");
213     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-4,8");
214     model.addRange("1", 5, 7, "B");
215     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-4,8/B:5-7");
216     model.addRange("1", 3, 5, "A");
217     assertEquals(testee.getAtomSpec(model, false), "#1/A:2-5,8/B:5-7");
218     model.addRange("0", 1, 4, "B");
219     assertEquals(testee.getAtomSpec(model, false),
220             "#0/B:1-4|#1/A:2-5,8/B:5-7");
221     model.addRange("0", 5, 9, "C");
222     assertEquals(testee.getAtomSpec(model, false),
223             "#0/B:1-4/C:5-9|#1/A:2-5,8/B:5-7");
224     model.addRange("1", 8, 10, "B");
225     assertEquals(testee.getAtomSpec(model, false),
226             "#0/B:1-4/C:5-9|#1/A:2-5,8/B:5-10");
227     model.addRange("1", 8, 9, "B");
228     assertEquals(testee.getAtomSpec(model, false),
229             "#0/B:1-4/C:5-9|#1/A:2-5,8/B:5-10");
230     model.addRange("0", 3, 10, "C"); // subsumes 5-9
231     assertEquals(testee.getAtomSpec(model, false),
232             "#0/B:1-4/C:3-10|#1/A:2-5,8/B:5-10");
233     model.addRange("5", 25, 35, " ");
234     assertEquals(testee.getAtomSpec(model, false),
235             "#0/B:1-4/C:3-10|#1/A:2-5,8/B:5-10|#5/:25-35");
236   }
237
238   @Test(groups = "Functional")
239   public void testGetAtomSpec_alphaOnly()
240   {
241     AtomSpecModel model = new AtomSpecModel();
242     assertEquals(testee.getAtomSpec(model, true), "");
243     model.addRange("1", 2, 4, "A");
244     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-4@CA");
245     model.addRange("1", 8, 8, "A");
246     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-4,8@CA");
247     model.addRange("1", 5, 7, "B");
248     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-4,8/B:5-7@CA");
249     model.addRange("1", 3, 5, "A");
250     assertEquals(testee.getAtomSpec(model, true), "#1/A:2-5,8/B:5-7@CA");
251     model.addRange("0", 1, 4, "B");
252     assertEquals(testee.getAtomSpec(model, true),
253             "#0/B:1-4@CA|#1/A:2-5,8/B:5-7@CA");
254     model.addRange("0", 5, 9, "C");
255     assertEquals(testee.getAtomSpec(model, true),
256             "#0/B:1-4/C:5-9@CA|#1/A:2-5,8/B:5-7@CA");
257     model.addRange("1", 8, 10, "B");
258     assertEquals(testee.getAtomSpec(model, true),
259             "#0/B:1-4/C:5-9@CA|#1/A:2-5,8/B:5-10@CA");
260     model.addRange("1", 8, 9, "B");
261     assertEquals(testee.getAtomSpec(model, true),
262             "#0/B:1-4/C:5-9@CA|#1/A:2-5,8/B:5-10@CA");
263     model.addRange("0", 3, 10, "C"); // subsumes 5-9
264     assertEquals(testee.getAtomSpec(model, true),
265             "#0/B:1-4/C:3-10@CA|#1/A:2-5,8/B:5-10@CA");
266     model.addRange("5", 25, 35, " "); // empty chain code
267     assertEquals(testee.getAtomSpec(model, true),
268             "#0/B:1-4/C:3-10@CA|#1/A:2-5,8/B:5-10@CA|#5/:25-35@CA");
269   }
270
271   @Test(groups = "Functional")
272   public void testGetModelStartNo()
273   {
274     assertEquals(testee.getModelStartNo(), 1);
275   }
276
277   @Test(groups = "Functional")
278   public void testGetResidueSpec()
279   {
280     assertEquals(testee.getResidueSpec("ALA"), ":ALA");
281   }
282
283   @Test(groups = "Functional")
284   public void testShowBackbone()
285   {
286     List<StructureCommandI> showBackbone = testee.showBackbone();
287     assertEquals(showBackbone.size(), 1);
288     assertEquals(showBackbone.get(0).getCommand(),
289             "~display all;~ribbon;show @CA|P atoms");
290   }
291
292   @Test(groups = "Functional")
293   public void testOpenCommandFile()
294   {
295     assertEquals(testee.openCommandFile("nowhere").getCommand(),
296             "open nowhere");
297   }
298
299   @Test(groups = "Functional")
300   public void testSaveSession()
301   {
302     assertEquals(testee.saveSession("somewhere").getCommand(),
303             "save somewhere format session");
304   }
305
306   @Test(groups = "Functional")
307   public void testGetColourCommand()
308   {
309     assertEquals(testee.colourResidues("something", Color.MAGENTA)
310             .getCommand(),
311             "color something #ff00ff");
312   }
313
314   @Test(groups = "Functional")
315   public void testSetAttribute()
316   {
317     AtomSpecModel model = new AtomSpecModel();
318     model.addRange("1", 89, 92, "A");
319     model.addRange("2", 12, 20, "B");
320     model.addRange("2", 8, 9, "B");
321     assertEquals(testee.setAttribute("jv_kd", "27.3", model)
322             .getCommand(),
323             "setattr #1/A:89-92|#2/B:8-9,12-20 res jv_kd '27.3' create true");
324   }
325 }