2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.ext.rbvi.chimera;
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertTrue;
26 import java.awt.Color;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import java.util.List;
32 import org.testng.annotations.BeforeClass;
33 import org.testng.annotations.Test;
35 import jalview.structure.AtomSpecModel;
36 import jalview.structure.StructureCommand;
37 import jalview.structure.StructureCommandI;
38 import jalview.structure.StructureCommandsI.AtomSpecType;
40 public class ChimeraCommandsTest
42 private ChimeraCommands testee;
44 @BeforeClass(alwaysRun = true)
47 testee = new ChimeraCommands();
50 @Test(groups = { "Functional" })
51 public void testColourBySequence()
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");
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");
73 @Test(groups = { "Functional" })
74 public void testSetAttributes()
77 * make a map of { featureType, {featureValue, {residue range specification } } }
79 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
80 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
83 * start with just one feature/value...
85 featuresMap.put("chain", featureValues);
86 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
88 List<StructureCommandI> commands = testee.setAttributes(featuresMap);
89 assertEquals(1, commands.size());
92 * feature name gets a jv_ namespace prefix
93 * feature value is quoted in case it contains spaces
95 assertEquals(commands.get(0).getCommand(),
96 "setattr res jv_chain 'X' #0:8-20.A");
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");
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);
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));
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\"> 'ion!' #0:7-15.A";
138 assertTrue(commands.get(0).getCommand().equals(expected3));
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
145 @Test(groups = { "Functional" })
146 public void testMakeAttributeName()
148 assertEquals(testee.makeAttributeName(null), "jv_");
149 assertEquals(testee.makeAttributeName(""), "jv_");
150 assertEquals(testee.makeAttributeName("helix"), "jv_helix");
151 assertEquals(testee.makeAttributeName("Hello World 24"),
152 "jv_Hello_World_24");
153 assertEquals(testee.makeAttributeName("!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_");
159 @Test(groups = "Functional")
160 public void testGetAtomSpec()
162 AtomSpecModel model = new AtomSpecModel();
163 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "");
164 model.addRange("1", 2, 4, "A");
165 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
167 model.addRange("1", 8, 8, "A");
168 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
170 model.addRange("1", 5, 7, "B");
171 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
172 "#1:2-4.A,8.A,5-7.B");
173 model.addRange("1", 3, 5, "A");
174 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
175 "#1:2-5.A,8.A,5-7.B");
176 model.addRange("0", 1, 4, "B");
177 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
178 "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
179 model.addRange("0", 5, 9, "C");
180 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
181 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
182 model.addRange("1", 8, 10, "B");
183 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
184 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
185 model.addRange("1", 8, 9, "B");
186 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
187 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
188 model.addRange("0", 3, 10, "C"); // subsumes 5-9
189 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
190 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
191 model.addRange("5", 25, 35, " ");
192 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
193 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
197 @Test(groups = { "Functional" })
198 public void testSuperposeStructures()
200 AtomSpecModel ref = new AtomSpecModel();
201 ref.addRange("1", 12, 14, "A");
202 ref.addRange("1", 18, 18, "B");
203 ref.addRange("1", 22, 23, "B");
204 AtomSpecModel toAlign = new AtomSpecModel();
205 toAlign.addRange("2", 15, 17, "B");
206 toAlign.addRange("2", 20, 21, "B");
207 toAlign.addRange("2", 22, 22, "C");
208 List<StructureCommandI> command = testee.superposeStructures(ref,
209 toAlign, AtomSpecType.ALPHA);
210 // qualifier to restrict match to CA and no altlocs
211 String carbonAlphas = "@CA&~@.B-Z&~@.2-9";
212 String refSpec = "#1:12-14.A,18.B,22-23.B";
213 String toAlignSpec = "#2:15-17.B,20-21.B,22.C";
214 String expected = String.format("match %s%s %s%s; ribbon %s|%s; focus",
215 toAlignSpec, carbonAlphas, refSpec, carbonAlphas, toAlignSpec,
217 assertEquals(command.get(0).getCommand(), expected);
220 @Test(groups = "Functional")
221 public void testGetAtomSpec_alphaOnly()
223 AtomSpecModel model = new AtomSpecModel();
224 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA), "");
225 model.addRange("1", 2, 4, "A");
226 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
227 "#1:2-4.A@CA&~@.B-Z&~@.2-9");
228 model.addRange("1", 8, 8, "A");
229 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
230 "#1:2-4.A,8.A@CA&~@.B-Z&~@.2-9");
231 model.addRange("1", 5, 7, "B");
232 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
233 "#1:2-4.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
234 model.addRange("1", 3, 5, "A");
235 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
236 "#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
237 model.addRange("0", 1, 4, "B");
238 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
239 "#0:1-4.B@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
240 model.addRange("0", 5, 9, "C");
241 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
242 "#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");
243 model.addRange("1", 8, 10, "B");
244 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
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("1", 8, 9, "B");
247 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
248 "#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");
249 model.addRange("0", 3, 10, "C"); // subsumes 5-9
250 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
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");
252 model.addRange("5", 25, 35, " "); // empty chain code
253 assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
254 "#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");
258 @Test(groups = "Functional")
259 public void testGetModelStartNo()
261 assertEquals(testee.getModelStartNo(), 0);
264 @Test(groups = "Functional")
265 public void testGetResidueSpec()
267 assertEquals(testee.getResidueSpec("ALA"), "::ALA");
270 @Test(groups = "Functional")
271 public void testShowBackbone()
273 List<StructureCommandI> cmds = testee.showBackbone();
274 assertEquals(cmds.size(), 1);
275 assertEquals(cmds.get(0).getCommand(),
276 "~display all;~ribbon;chain @CA|P");
279 @Test(groups = "Functional")
280 public void testOpenCommandFile()
282 assertEquals(testee.openCommandFile("nowhere").getCommand(),
286 @Test(groups = "Functional")
287 public void testSaveSession()
289 assertEquals(testee.saveSession("somewhere").getCommand(),
293 @Test(groups = "Functional")
294 public void testColourByChain()
296 assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
299 @Test(groups = { "Functional" })
300 public void testSetBackgroundColour()
302 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
303 assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
306 @Test(groups = { "Functional" })
307 public void testLoadFile()
309 StructureCommandI cmd = testee.loadFile("/some/filepath");
310 assertEquals(cmd.getCommand(), "open /some/filepath");
313 @Test(groups = { "Functional" })
314 public void testOpenSession()
316 StructureCommandI cmd = testee.openSession("/some/filepath");
317 assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
320 @Test(groups = "Functional")
321 public void testColourByCharge()
323 List<StructureCommandI> cmds = testee.colourByCharge();
324 assertEquals(cmds.size(), 1);
325 assertEquals(cmds.get(0).getCommand(),
326 "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
329 @Test(groups = "Functional")
330 public void testGetColourCommand()
333 testee.colourResidues("something", Color.MAGENTA).getCommand(),
334 "color #ff00ff something");
337 @Test(groups = "Functional")
338 public void testFocusView()
340 assertEquals(testee.focusView().getCommand(), "focus");
343 @Test(groups = "Functional")
344 public void testSetAttribute()
346 AtomSpecModel model = new AtomSpecModel();
347 model.addRange("1", 89, 92, "A");
348 model.addRange("2", 12, 20, "B");
349 model.addRange("2", 8, 9, "B");
350 assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(),
351 "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
354 @Test(groups = "Functional")
355 public void testCloseViewer()
357 assertEquals(testee.closeViewer(), new StructureCommand("stop really"));
360 @Test(groups = "Functional")
361 public void testGetSelectedResidues()
363 assertEquals(testee.getSelectedResidues(),
364 new StructureCommand("list selection level residue"));
367 @Test(groups = "Functional")
368 public void testListResidueAttributes()
370 assertEquals(testee.listResidueAttributes(),
371 new StructureCommand("list resattr"));
374 @Test(groups = "Functional")
375 public void testGetResidueAttributes()
377 assertEquals(testee.getResidueAttributes("binding site"),
378 new StructureCommand("list residues attr 'binding site'"));
381 @Test(groups = "Functional")
382 public void testStartNotifications()
384 List<StructureCommandI> cmds = testee.startNotifications("to here");
385 assertEquals(cmds.size(), 2);
386 assertEquals(cmds.get(0),
387 new StructureCommand("listen start models url to here"));
388 assertEquals(cmds.get(1), new StructureCommand(
389 "listen start select prefix SelectionChanged url to here"));
392 @Test(groups = "Functional")
393 public void testStopNotifications()
395 List<StructureCommandI> cmds = testee.stopNotifications();
396 assertEquals(cmds.size(), 2);
397 assertEquals(cmds.get(0), new StructureCommand("listen stop models"));
398 assertEquals(cmds.get(1),
399 new StructureCommand("listen stop selection"));