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 // all prefixed with #808080 to colour hidden regions (if shown) gray
65 List<StructureCommandI> commands = testee.colourBySequence(map);
66 assertEquals(commands.size(), 1);
67 assertEquals(commands.get(0).getCommand(),
68 "color #808080; 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");
71 @Test(groups = { "Functional" })
72 public void testSetAttributes()
75 * make a map of { featureType, {featureValue, {residue range specification } } }
77 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
78 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
81 * start with just one feature/value...
83 featuresMap.put("chain", featureValues);
84 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
86 List<StructureCommandI> commands = testee.setAttributes(featuresMap);
87 assertEquals(1, commands.size());
90 * feature name gets a jv_ namespace prefix
91 * feature value is quoted in case it contains spaces
93 assertEquals(commands.get(0).getCommand(),
94 "setattr res jv_chain 'X' #0:8-20.A");
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");
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);
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));
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\"> 'ion!' #0:7-15.A";
136 assertTrue(commands.get(0).getCommand().equals(expected3));
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
143 @Test(groups = { "Functional" })
144 public void testMakeAttributeName()
146 assertEquals(testee.makeAttributeName(null), "jv_");
147 assertEquals(testee.makeAttributeName(""), "jv_");
148 assertEquals(testee.makeAttributeName("helix"), "jv_helix");
149 assertEquals(testee.makeAttributeName(
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_");
159 @Test(groups = "Functional")
160 public void testGetAtomSpec()
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.");
193 @Test(groups = { "Functional" })
194 public void testSuperposeStructures()
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,
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,
213 assertEquals(command.get(0).getCommand(), expected);
216 @Test(groups = "Functional")
217 public void testGetAtomSpec_alphaOnly()
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");
253 @Test(groups = "Functional")
254 public void testGetResidueSpec()
256 assertEquals(testee.getResidueSpec("ALA"), "::ALA");
259 @Test(groups = "Functional")
260 public void testShowBackbone()
262 List<StructureCommandI> cmds = testee.showBackbone();
263 assertEquals(cmds.size(), 1);
264 assertEquals(cmds.get(0).getCommand(),
265 "~display all;~ribbon;chain @CA|P");
268 @Test(groups = "Functional")
269 public void testOpenCommandFile()
271 assertEquals(testee.openCommandFile("nowhere").getCommand(),
275 @Test(groups = "Functional")
276 public void testSaveSession()
278 assertEquals(testee.saveSession("somewhere").getCommand(),
282 @Test(groups = "Functional")
283 public void testColourByChain()
285 assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
288 @Test(groups = { "Functional" })
289 public void testSetBackgroundColour()
291 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
292 assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
295 @Test(groups = { "Functional" })
296 public void testLoadFile()
298 StructureCommandI cmd = testee.loadFile("/some/filepath");
299 assertEquals(cmd.getCommand(), "open /some/filepath");
302 @Test(groups = { "Functional" })
303 public void testOpenSession()
305 StructureCommandI cmd = testee.openSession("/some/filepath");
306 assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
309 @Test(groups = "Functional")
310 public void testColourByCharge()
312 List<StructureCommandI> cmds = testee.colourByCharge();
313 assertEquals(cmds.size(), 1);
314 assertEquals(cmds.get(0)
316 "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
319 @Test(groups = "Functional")
320 public void testGetColourCommand()
322 assertEquals(testee.colourResidues("something", Color.MAGENTA)
324 "color #ff00ff something");
327 @Test(groups = "Functional")
328 public void testFocusView()
330 assertEquals(testee.focusView().getCommand(), "focus");
333 @Test(groups = "Functional")
334 public void testSetAttribute()
336 AtomSpecModel model = new AtomSpecModel();
337 model.addRange("1", 89, 92, "A");
338 model.addRange("2", 12, 20, "B");
339 model.addRange("2", 8, 9, "B");
340 assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(),
341 "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B");