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.StructureCommandI;
38 public class ChimeraCommandsTest
40 private ChimeraCommands testee;
42 @BeforeClass(alwaysRun = true)
45 testee = new ChimeraCommands();
48 @Test(groups = { "Functional" })
49 public void testColourBySequence()
51 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
52 ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
53 ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
54 ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
55 ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
56 ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
57 ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
58 ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
59 ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
60 ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
62 // Colours should appear in the Chimera command in the order in which
63 // they were added; within colour, by model, by chain, ranges in start order
64 List<StructureCommandI> commands = testee.colourBySequence(map);
65 assertEquals(commands.size(), 1);
66 assertEquals(commands.get(0).getCommand(),
67 "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 @Test(groups = { "Functional" })
71 public void testSetAttributes()
74 * make a map of { featureType, {featureValue, {residue range specification } } }
76 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
77 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
80 * start with just one feature/value...
82 featuresMap.put("chain", featureValues);
83 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
85 List<StructureCommandI> commands = testee.setAttributes(featuresMap);
86 assertEquals(1, commands.size());
89 * feature name gets a jv_ namespace prefix
90 * feature value is quoted in case it contains spaces
92 assertEquals(commands.get(0).getCommand(),
93 "setattr res jv_chain 'X' #0:8-20.A");
95 // add same feature value, overlapping range
96 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
97 // same feature value, contiguous range
98 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
99 commands = testee.setAttributes(featuresMap);
100 assertEquals(1, commands.size());
101 assertEquals(commands.get(0).getCommand(),
102 "setattr res jv_chain 'X' #0:3-25.A");
104 // same feature value and model, different chain
105 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
106 // same feature value and chain, different model
107 ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
108 commands = testee.setAttributes(featuresMap);
109 assertEquals(1, commands.size());
110 String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
111 assertEquals(commands.get(0).getCommand(), expected1);
113 // same feature, different value
114 ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
115 commands = testee.setAttributes(featuresMap);
116 assertEquals(2, commands.size());
117 // commands are ordered by feature type but not by value
118 // so test for the expected command in either order
119 String cmd1 = commands.get(0).getCommand();
120 String cmd2 = commands.get(1).getCommand();
121 assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
122 String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
123 assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
126 featureValues.clear();
127 featuresMap.put("side-chain binding!", featureValues);
128 ChimeraCommands.addAtomSpecRange(featureValues,
129 "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
130 // feature names are sanitised to change non-alphanumeric to underscore
131 // feature values are sanitised to encode single quote characters
132 commands = testee.setAttributes(featuresMap);
133 assertEquals(commands.size(), 1);
134 String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> 'ion!' #0:7-15.A";
135 assertTrue(commands.get(0).getCommand().equals(expected3));
139 * Tests for the method that prefixes and sanitises a feature name so it can
140 * be used as a valid, namespaced attribute name in Chimera or PyMol
142 @Test(groups = { "Functional" })
143 public void testMakeAttributeName()
145 assertEquals(testee.makeAttributeName(null), "jv_");
146 assertEquals(testee.makeAttributeName(""), "jv_");
147 assertEquals(testee.makeAttributeName("helix"), "jv_helix");
148 assertEquals(testee.makeAttributeName(
150 "jv_Hello_World_24");
151 assertEquals(testee.makeAttributeName(
152 "!this is-a_very*{odd(name"),
153 "jv__this_is_a_very__odd_name");
154 // name ending in color gets underscore appended
155 assertEquals(testee.makeAttributeName("helixColor"), "jv_helixColor_");
158 @Test(groups = "Functional")
159 public void testGetAtomSpec()
161 AtomSpecModel model = new AtomSpecModel();
162 assertEquals(testee.getAtomSpec(model, false), "");
163 model.addRange("1", 2, 4, "A");
164 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
165 model.addRange("1", 8, 8, "A");
166 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
167 model.addRange("1", 5, 7, "B");
168 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
169 model.addRange("1", 3, 5, "A");
170 assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
171 model.addRange("0", 1, 4, "B");
172 assertEquals(testee.getAtomSpec(model, false),
173 "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
174 model.addRange("0", 5, 9, "C");
175 assertEquals(testee.getAtomSpec(model, false),
176 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
177 model.addRange("1", 8, 10, "B");
178 assertEquals(testee.getAtomSpec(model, false),
179 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
180 model.addRange("1", 8, 9, "B");
181 assertEquals(testee.getAtomSpec(model, false),
182 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
183 model.addRange("0", 3, 10, "C"); // subsumes 5-9
184 assertEquals(testee.getAtomSpec(model, false),
185 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
186 model.addRange("5", 25, 35, " ");
187 assertEquals(testee.getAtomSpec(model, false),
188 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
192 @Test(groups = { "Functional" })
193 public void testSuperposeStructures()
195 AtomSpecModel ref = new AtomSpecModel();
196 ref.addRange("1", 12, 14, "A");
197 ref.addRange("1", 18, 18, "B");
198 ref.addRange("1", 22, 23, "B");
199 AtomSpecModel toAlign = new AtomSpecModel();
200 toAlign.addRange("2", 15, 17, "B");
201 toAlign.addRange("2", 20, 21, "B");
202 toAlign.addRange("2", 22, 22, "C");
203 List<StructureCommandI> command = testee.superposeStructures(ref,
205 // qualifier to restrict match to CA and no altlocs
206 String carbonAlphas = "@CA&~@.B-Z&~@.2-9";
207 String refSpec = "#1:12-14.A,18.B,22-23.B";
208 String toAlignSpec = "#2:15-17.B,20-21.B,22.C";
209 String expected = String.format("match %s%s %s%s; ribbon %s|%s; focus",
210 toAlignSpec, carbonAlphas, refSpec, carbonAlphas, toAlignSpec,
212 assertEquals(command.get(0).getCommand(), expected);
215 @Test(groups = "Functional")
216 public void testGetAtomSpec_alphaOnly()
218 AtomSpecModel model = new AtomSpecModel();
219 assertEquals(testee.getAtomSpec(model, true), "");
220 model.addRange("1", 2, 4, "A");
221 assertEquals(testee.getAtomSpec(model, true),
222 "#1:2-4.A@CA&~@.B-Z&~@.2-9");
223 model.addRange("1", 8, 8, "A");
224 assertEquals(testee.getAtomSpec(model, true),
225 "#1:2-4.A,8.A@CA&~@.B-Z&~@.2-9");
226 model.addRange("1", 5, 7, "B");
227 assertEquals(testee.getAtomSpec(model, true),
228 "#1:2-4.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
229 model.addRange("1", 3, 5, "A");
230 assertEquals(testee.getAtomSpec(model, true),
231 "#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
232 model.addRange("0", 1, 4, "B");
233 assertEquals(testee.getAtomSpec(model, true),
234 "#0:1-4.B@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
235 model.addRange("0", 5, 9, "C");
236 assertEquals(testee.getAtomSpec(model, true),
237 "#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");
238 model.addRange("1", 8, 10, "B");
239 assertEquals(testee.getAtomSpec(model, true),
240 "#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");
241 model.addRange("1", 8, 9, "B");
242 assertEquals(testee.getAtomSpec(model, true),
243 "#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");
244 model.addRange("0", 3, 10, "C"); // subsumes 5-9
245 assertEquals(testee.getAtomSpec(model, true),
246 "#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");
247 model.addRange("5", 25, 35, " "); // empty chain code
248 assertEquals(testee.getAtomSpec(model, true),
249 "#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 @Test(groups = "Functional")
253 public void testGetResidueSpec()
255 assertEquals(testee.getResidueSpec("ALA"), "::ALA");
258 @Test(groups = "Functional")
259 public void testShowBackbone()
261 List<StructureCommandI> cmds = testee.showBackbone();
262 assertEquals(cmds.size(), 1);
263 assertEquals(cmds.get(0).getCommand(),
264 "~display all;~ribbon;chain @CA|P");
267 @Test(groups = "Functional")
268 public void testOpenCommandFile()
270 assertEquals(testee.openCommandFile("nowhere").getCommand(),
274 @Test(groups = "Functional")
275 public void testSaveSession()
277 assertEquals(testee.saveSession("somewhere").getCommand(),
281 @Test(groups = "Functional")
282 public void testColourByChain()
284 assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
287 @Test(groups = { "Functional" })
288 public void testSetBackgroundColour()
290 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
291 assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
294 @Test(groups = { "Functional" })
295 public void testLoadFile()
297 StructureCommandI cmd = testee.loadFile("/some/filepath");
298 assertEquals(cmd.getCommand(), "open /some/filepath");
301 @Test(groups = { "Functional" })
302 public void testOpenSession()
304 StructureCommandI cmd = testee.openSession("/some/filepath");
305 assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
308 @Test(groups = "Functional")
309 public void testColourByCharge()
311 List<StructureCommandI> cmds = testee.colourByCharge();
312 assertEquals(cmds.size(), 1);
313 assertEquals(cmds.get(0)
315 "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
318 @Test(groups = "Functional")
319 public void testGetColourCommand()
321 assertEquals(testee.colourResidues("something", Color.MAGENTA)
323 "color #ff00ff something");
326 @Test(groups = "Functional")
327 public void testFocusView()
329 assertEquals(testee.focusView().getCommand(), "focus");
332 @Test(groups = "Functional")
333 public void testSetAttribute()
335 AtomSpecModel model = new AtomSpecModel();
336 model.addRange("1", 89, 92, "A");
337 model.addRange("2", 12, 20, "B");
338 model.addRange("2", 8, 9, "B");
339 assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(),
340 "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
343 @Test(groups = "Functional")
344 public void testHideAll()
346 StructureCommandI cmd = testee.hideAll();
347 assertEquals(cmd.getCommand(), "~display all; ~ribbon");
350 @Test(groups = "Functional")
351 public void testHideChain()
353 StructureCommandI cmd = testee.hideChain("1.1", "B");
354 assertEquals(cmd.getCommand(), "~ribbon #1.1:.B");
357 @Test(groups = "Functional")
358 public void testShowStructures()
361 * with nothing excluded
363 StructureCommandI cmd = testee.showStructures(null);
364 assertEquals(cmd.getCommand(), "ribbon");
367 * restricted to specified positions
369 AtomSpecModel restrictTo = new AtomSpecModel();
370 restrictTo.addRange("1.1", 12, 20, "A");
371 restrictTo.addRange("1.1", 30, 35, "A");
372 restrictTo.addRange("2.1", 11, 30, "B");
373 cmd = testee.showStructures(restrictTo);
374 assertEquals(cmd.getCommand(),
375 "ribbon #1.1:12-20.A,30-35.A|#2.1:11-30.B");