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 jalview.structure.AtomSpecModel;
27 import jalview.structure.StructureCommandsI;
29 import java.awt.Color;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
34 import org.testng.annotations.Test;
36 public class ChimeraCommandsTest
39 @Test(groups = { "Functional" })
40 public void testColourBySequence()
43 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
44 ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 2, 5, "A");
45 ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 7, 7, "B");
46 ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 9, 23, "A");
47 ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 1, 1, "A");
48 ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 4, 7, "B");
49 ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 8, 8, "A");
50 ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 3, 5, "A");
51 ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 3, 5, "A");
52 ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 6, 9, "A");
54 // Colours should appear in the Chimera command in the order in which
55 // they were added; within colour, by model, by chain, ranges in start order
56 String[] commands = new ChimeraCommands().colourBySequence(map);
57 assertEquals(commands.length, 1);
60 "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");
63 @Test(groups = { "Functional" })
64 public void testSetAttributes()
67 * make a map of { featureType, {featureValue, {residue range specification } } }
69 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
70 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
73 * start with just one feature/value...
75 featuresMap.put("chain", featureValues);
76 ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 8, 20, "A");
78 ChimeraCommands commandGenerator = new ChimeraCommands();
79 String[] commands = commandGenerator.setAttributes(featuresMap);
80 assertEquals(1, commands.length);
83 * feature name gets a jv_ namespace prefix
84 * feature value is quoted in case it contains spaces
86 assertEquals(commands[0], "setattr res jv_chain 'X' #0:8-20.A");
88 // add same feature value, overlapping range
89 ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
90 // same feature value, contiguous range
91 ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
92 commands = commandGenerator.setAttributes(featuresMap);
93 assertEquals(1, commands.length);
94 assertEquals(commands[0], "setattr res jv_chain 'X' #0:3-25.A");
96 // same feature value and model, different chain
97 ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
98 // same feature value and chain, different model
99 ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
100 commands = commandGenerator.setAttributes(featuresMap);
101 assertEquals(1, commands.length);
102 String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
103 assertEquals(commands[0],
106 // same feature, different value
107 ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
108 commands = commandGenerator.setAttributes(featuresMap);
109 assertEquals(2, commands.length);
110 // commands are ordered by feature type but not by value
111 // so test for the expected command in either order
113 commands[0].equals(expected1) || commands[1].equals(expected1));
114 String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
116 commands[0].equals(expected2) || commands[1].equals(expected2));
119 featureValues.clear();
120 featuresMap.put("side-chain binding!", featureValues);
121 ChimeraCommands.addAtomSpecRange(featureValues,
122 "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
124 // feature names are sanitised to change non-alphanumeric to underscore
125 // feature values are sanitised to encode single quote characters
126 commands = commandGenerator.setAttributes(featuresMap);
127 String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> 'ion!' #0:7-15.A";
129 commands[0].equals(expected3) || commands[1].equals(expected3));
133 * Tests for the method that prefixes and sanitises a feature name so it can
134 * be used as a valid, namespaced attribute name in Chimera
136 @Test(groups = { "Functional" })
137 public void testMakeAttributeName()
139 assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
140 assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
141 assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
142 assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
143 "jv_Hello_World_24");
145 ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
146 "jv__this_is_a_very__odd_name");
147 // name ending in color gets underscore appended
148 assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
152 @Test(groups = "Functional")
153 public void testGetAtomSpec()
155 StructureCommandsI testee = new ChimeraCommands();
156 AtomSpecModel model = new AtomSpecModel();
157 assertEquals(testee.getAtomSpec(model, false), "");
158 model.addRange(1, 2, 4, "A");
159 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
160 model.addRange(1, 8, 8, "A");
161 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
162 model.addRange(1, 5, 7, "B");
163 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
164 model.addRange(1, 3, 5, "A");
165 assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
166 model.addRange(0, 1, 4, "B");
167 assertEquals(testee.getAtomSpec(model, false),
168 "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
169 model.addRange(0, 5, 9, "C");
170 assertEquals(testee.getAtomSpec(model, false),
171 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
172 model.addRange(1, 8, 10, "B");
173 assertEquals(testee.getAtomSpec(model, false),
174 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
175 model.addRange(1, 8, 9, "B");
176 assertEquals(testee.getAtomSpec(model, false),
177 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
178 model.addRange(0, 3, 10, "C"); // subsumes 5-9
179 assertEquals(testee.getAtomSpec(model, false),
180 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
181 model.addRange(5, 25, 35, " "); // empty chain code - e.g. from homology
183 assertEquals(testee.getAtomSpec(model, false),
184 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
188 @Test(groups = { "Functional" })
189 public void testSuperposeStructures()
191 StructureCommandsI testee = new ChimeraCommands();
192 AtomSpecModel ref = new AtomSpecModel();
193 ref.addRange(1, 12, 14, "A");
194 ref.addRange(1, 18, 18, "B");
195 ref.addRange(1, 22, 23, "B");
196 AtomSpecModel toAlign = new AtomSpecModel();
197 toAlign.addRange(2, 15, 17, "B");
198 toAlign.addRange(2, 20, 21, "B");
199 toAlign.addRange(2, 22, 22, "C");
200 String command = testee.superposeStructures(ref, toAlign);
201 String refSpec = "#1:12-14.A,18.B,22-23.B@CA|P&~@.B-Z&~@.2-9";
202 String toAlignSpec = "#2:15-17.B,20-21.B,22.C@CA|P&~@.B-Z&~@.2-9";
203 String expected = String.format(
204 "match %s %s;~display all; chain @CA|P; ribbon %s|%s; focus",
205 refSpec, toAlignSpec, refSpec, toAlignSpec);
206 assertEquals(command, expected);
209 @Test(groups = "Functional")
210 public void testGetAtomSpec_alphaOnly()
212 StructureCommandsI testee = new ChimeraCommands();
213 AtomSpecModel model = new AtomSpecModel();
214 assertEquals(testee.getAtomSpec(model, true), "");
215 model.addRange(1, 2, 4, "A");
216 assertEquals(testee.getAtomSpec(model, true),
217 "#1:2-4.A@CA|P&~@.B-Z&~@.2-9");
218 model.addRange(1, 8, 8, "A");
219 assertEquals(testee.getAtomSpec(model, true),
220 "#1:2-4.A,8.A@CA|P&~@.B-Z&~@.2-9");
221 model.addRange(1, 5, 7, "B");
222 assertEquals(testee.getAtomSpec(model, true),
223 "#1:2-4.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
224 model.addRange(1, 3, 5, "A");
225 assertEquals(testee.getAtomSpec(model, true),
226 "#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
227 model.addRange(0, 1, 4, "B");
228 assertEquals(testee.getAtomSpec(model, true),
229 "#0:1-4.B@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
230 model.addRange(0, 5, 9, "C");
231 assertEquals(testee.getAtomSpec(model, true),
232 "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
233 model.addRange(1, 8, 10, "B");
234 assertEquals(testee.getAtomSpec(model, true),
235 "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
236 model.addRange(1, 8, 9, "B");
237 assertEquals(testee.getAtomSpec(model, true),
238 "#0:1-4.B,5-9.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
239 model.addRange(0, 3, 10, "C"); // subsumes 5-9
240 assertEquals(testee.getAtomSpec(model, true),
241 "#0:1-4.B,3-10.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9");
242 model.addRange(5, 25, 35, " "); // empty chain code
243 assertEquals(testee.getAtomSpec(model, true),
244 "#0:1-4.B,3-10.C@CA|P&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA|P&~@.B-Z&~@.2-9|#5:25-35.@CA|P&~@.B-Z&~@.2-9");
248 @Test(groups = "Functional")
249 public void testGetModelStartNo()
251 StructureCommandsI testee = new ChimeraCommands();
252 assertEquals(testee.getModelStartNo(), 0);
255 @Test(groups = "Functional")
256 public void testGetResidueSpec()
258 ChimeraCommands testee = new ChimeraCommands();
259 assertEquals(testee.getResidueSpec("ALA"), "::ALA");
262 @Test(groups = "Functional")
263 public void testShowBackbone()
265 ChimeraCommands testee = new ChimeraCommands();
266 assertEquals(testee.showBackbone(), "~display all;chain @CA|P");
269 @Test(groups = "Functional")
270 public void testOpenCommandFile()
272 ChimeraCommands testee = new ChimeraCommands();
273 assertEquals(testee.openCommandFile("nowhere"), "open cmd:nowhere");
276 @Test(groups = "Functional")
277 public void testSaveSession()
279 ChimeraCommands testee = new ChimeraCommands();
280 assertEquals(testee.saveSession("somewhere"), "save somewhere");
283 @Test(groups = "Functional")
284 public void testGetColourCommand()
286 ChimeraCommands testee = new ChimeraCommands();
287 assertEquals(testee.getColourCommand("something", Color.MAGENTA),
288 "color #ff00ff something");
291 @Test(groups = "Functional")
292 public void testSetAttribute()
294 ChimeraCommands testee = new ChimeraCommands();
295 AtomSpecModel model = new AtomSpecModel();
296 model.addRange(1, 89, 92, "A");
297 model.addRange(2, 12, 20, "B");
298 model.addRange(2, 8, 9, "B");
299 assertEquals(testee.setAttribute("phi", "27.3", model),
300 "setattr res phi '27.3' #1:89-92.A|#2:8-9.B,12-20.B");