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;
39 public class ChimeraCommandsTest
41 private ChimeraCommands testee;
43 @BeforeClass(alwaysRun = true)
46 testee = new ChimeraCommands();
49 @Test(groups = { "Functional" })
50 public void testColourBySequence()
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");
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");
72 @Test(groups = { "Functional" })
73 public void testSetAttributes()
76 * make a map of { featureType, {featureValue, {residue range specification } } }
78 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
79 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
82 * start with just one feature/value...
84 featuresMap.put("chain", featureValues);
85 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
87 List<StructureCommandI> commands = testee.setAttributes(featuresMap);
88 assertEquals(1, commands.size());
91 * feature name gets a jv_ namespace prefix
92 * feature value is quoted in case it contains spaces
94 assertEquals(commands.get(0).getCommand(),
95 "setattr res jv_chain 'X' #0:8-20.A");
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");
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);
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));
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\"> 'ion!' #0:7-15.A";
137 assertTrue(commands.get(0).getCommand().equals(expected3));
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
144 @Test(groups = { "Functional" })
145 public void testMakeAttributeName()
147 assertEquals(testee.makeAttributeName(null), "jv_");
148 assertEquals(testee.makeAttributeName(""), "jv_");
149 assertEquals(testee.makeAttributeName("helix"), "jv_helix");
150 assertEquals(testee.makeAttributeName(
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_");
160 @Test(groups = "Functional")
161 public void testGetAtomSpec()
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.");
194 @Test(groups = { "Functional" })
195 public void testSuperposeStructures()
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,
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,
214 assertEquals(command.get(0).getCommand(), expected);
217 @Test(groups = "Functional")
218 public void testGetAtomSpec_alphaOnly()
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");
255 @Test(groups = "Functional")
256 public void testGetModelStartNo()
258 assertEquals(testee.getModelStartNo(), 0);
261 @Test(groups = "Functional")
262 public void testGetResidueSpec()
264 assertEquals(testee.getResidueSpec("ALA"), "::ALA");
267 @Test(groups = "Functional")
268 public void testShowBackbone()
270 List<StructureCommandI> cmds = testee.showBackbone();
271 assertEquals(cmds.size(), 1);
272 assertEquals(cmds.get(0).getCommand(),
273 "~display all;~ribbon;chain @CA|P");
276 @Test(groups = "Functional")
277 public void testOpenCommandFile()
279 assertEquals(testee.openCommandFile("nowhere").getCommand(),
283 @Test(groups = "Functional")
284 public void testSaveSession()
286 assertEquals(testee.saveSession("somewhere").getCommand(),
290 @Test(groups = "Functional")
291 public void testColourByChain()
293 assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
296 @Test(groups = { "Functional" })
297 public void testSetBackgroundColour()
299 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
300 assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
303 @Test(groups = { "Functional" })
304 public void testLoadFile()
306 StructureCommandI cmd = testee.loadFile("/some/filepath");
307 assertEquals(cmd.getCommand(), "open /some/filepath");
310 @Test(groups = { "Functional" })
311 public void testOpenSession()
313 StructureCommandI cmd = testee.openSession("/some/filepath");
314 assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
317 @Test(groups = "Functional")
318 public void testColourByCharge()
320 List<StructureCommandI> cmds = testee.colourByCharge();
321 assertEquals(cmds.size(), 1);
322 assertEquals(cmds.get(0)
324 "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
327 @Test(groups = "Functional")
328 public void testGetColourCommand()
330 assertEquals(testee.colourResidues("something", Color.MAGENTA)
332 "color #ff00ff something");
335 @Test(groups = "Functional")
336 public void testFocusView()
338 assertEquals(testee.focusView().getCommand(), "focus");
341 @Test(groups = "Functional")
342 public void testSetAttribute()
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");
352 @Test(groups = "Functional")
353 public void testCloseViewer()
355 assertEquals(testee.closeViewer(), new StructureCommand("stop really"));
358 @Test(groups = "Functional")
359 public void testGetSelectedResidues()
361 assertEquals(testee.getSelectedResidues(),
362 new StructureCommand("list selection level residue"));
365 @Test(groups = "Functional")
366 public void testListResidueAttributes()
368 assertEquals(testee.listResidueAttributes(),
369 new StructureCommand("list resattr"));
372 @Test(groups = "Functional")
373 public void testGetResidueAttributes()
375 assertEquals(testee.getResidueAttributes("binding site"),
376 new StructureCommand("list residues attr 'binding site'"));
379 @Test(groups = "Functional")
380 public void testStartNotifications()
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"));
388 @Test(groups = "Functional")
389 public void testStopNotifications()
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"));