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.api.AlignmentViewPanel;
27 import jalview.api.FeatureRenderer;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.AlignFrame;
35 import jalview.gui.JvOptionPane;
36 import jalview.gui.SequenceRenderer;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.schemes.JalviewColourScheme;
39 import jalview.structure.AtomSpec;
40 import jalview.structure.StructureMapping;
41 import jalview.structure.StructureMappingcommandSet;
42 import jalview.structure.StructureSelectionManager;
43 import jalview.structures.models.AAStructureBindingModel;
45 import java.awt.Color;
46 import java.util.HashMap;
47 import java.util.LinkedHashMap;
48 import java.util.List;
51 import org.testng.annotations.BeforeClass;
52 import org.testng.annotations.Test;
54 import junit.extensions.PA;
56 public class ChimeraCommandsTest
59 private SequenceRenderer sr;
61 private AAStructureBindingModel mockBinding = new AAStructureBindingModel(
65 public void releaseReferences(Object svl)
70 public void highlightAtoms(List<AtomSpec> atoms)
75 public List<String> getChainNames()
81 public void setJalviewColourScheme(ColourSchemeI cs)
86 public String superposeStructures(AlignmentI[] alignments,
87 int[] structureIndices, HiddenColumns[] hiddenCols)
93 public void setBackgroundColour(Color col)
98 protected StructureMappingcommandSet[] getColourBySequenceCommands(
99 String[] files, AlignmentViewPanel avp)
105 public jalview.api.SequenceRenderer getSequenceRenderer(
106 AlignmentViewPanel alignment)
112 protected void colourBySequence(
113 StructureMappingcommandSet[] colourBySequenceCommands)
118 public void colourByChain()
123 public void colourByCharge()
128 public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
134 public String[] getStructureFiles()
140 public String getModelSpec(int model)
142 return "#" + String.valueOf(model);
146 @BeforeClass(alwaysRun = true)
147 public void setUpJvOptionPane()
149 JvOptionPane.setInteractiveMode(false);
150 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
153 @Test(groups = { "Functional" })
154 public void testBuildColourCommands()
157 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
158 ChimeraCommands.addColourRange(map, Color.blue, 0, 2, 5, "A");
159 ChimeraCommands.addColourRange(map, Color.blue, 0, 7, 7, "B");
160 ChimeraCommands.addColourRange(map, Color.blue, 0, 9, 23, "A");
161 ChimeraCommands.addColourRange(map, Color.blue, 1, 1, 1, "A");
162 ChimeraCommands.addColourRange(map, Color.blue, 1, 4, 7, "B");
163 ChimeraCommands.addColourRange(map, Color.yellow, 1, 8, 8, "A");
164 ChimeraCommands.addColourRange(map, Color.yellow, 1, 3, 5, "A");
165 ChimeraCommands.addColourRange(map, Color.red, 0, 3, 5, "A");
166 ChimeraCommands.addColourRange(map, Color.red, 0, 6, 9, "A");
168 // Colours should appear in the Chimera command in the order in which
169 // they were added; within colour, by model, by chain, ranges in start order
170 // all prefixed with #808080 to colour hidden regions (if shown) gray
171 String command = ChimeraCommands.buildColourCommands(map, mockBinding)
175 "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");
178 @Test(groups = { "Functional" })
179 public void testBuildSetAttributeCommands()
182 * make a map of { featureType, {featureValue, {residue range specification } } }
184 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
185 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
188 * start with just one feature/value...
190 featuresMap.put("chain", featureValues);
191 ChimeraCommands.addColourRange(featureValues, "X", 0, 8, 20, "A");
193 List<String> commands = ChimeraCommands
194 .buildSetAttributeCommands(featuresMap, mockBinding);
195 assertEquals(1, commands.size());
198 * feature name gets a jv_ namespace prefix
199 * feature value is quoted in case it contains spaces
201 assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:8-20.A");
203 // add same feature value, overlapping range
204 ChimeraCommands.addColourRange(featureValues, "X", 0, 3, 9, "A");
205 // same feature value, contiguous range
206 ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "A");
207 commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
209 assertEquals(1, commands.size());
210 assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:3-25.A");
212 // same feature value and model, different chain
213 ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "B");
214 // same feature value and chain, different model
215 ChimeraCommands.addColourRange(featureValues, "X", 1, 26, 30, "A");
216 commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
218 assertEquals(1, commands.size());
219 assertEquals(commands.get(0),
220 "setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A");
222 // same feature, different value
223 ChimeraCommands.addColourRange(featureValues, "Y", 0, 40, 50, "A");
224 commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
226 assertEquals(2, commands.size());
227 // commands are ordered by feature type but not by value
228 // so use contains to test for the expected command:
230 .contains("setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"));
231 assertTrue(commands.contains("setattr r jv_chain 'Y' #0:40-50.A"));
234 featureValues.clear();
235 featuresMap.put("side-chain binding!", featureValues);
236 ChimeraCommands.addColourRange(featureValues,
237 "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
239 // feature names are sanitised to change non-alphanumeric to underscore
240 // feature values are sanitised to encode single quote characters
241 commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
244 .contains("setattr r jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> 'ion!' #0:7-15.A"));
248 * Tests for the method that prefixes and sanitises a feature name so it can
249 * be used as a valid, namespaced attribute name in Chimera
251 @Test(groups = { "Functional" })
252 public void testMakeAttributeName()
254 assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
255 assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
256 assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
257 assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
258 "jv_Hello_World_24");
260 ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
261 "jv__this_is_a_very__odd_name");
262 // name ending in color gets underscore appended
263 assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
267 @Test(groups = { "Functional" })
268 public void testGetColourBySequenceCommands_hiddenColumns()
271 * load these sequences, coloured by Strand propensity,
272 * with columns 2-4 hidden
274 SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
275 SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
276 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
277 AlignFrame af = new AlignFrame(al, 800, 500);
278 af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
279 ColumnSelection cs = new ColumnSelection();
283 af.getViewport().setColumnSelection(cs);
284 af.hideSelColumns_actionPerformed(null);
285 sr = new SequenceRenderer(af.getViewport());
286 SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
287 String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
288 StructureSelectionManager ssm = new StructureSelectionManager();
291 * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
293 HashMap<Integer, int[]> map = new HashMap<>();
294 for (int pos = 1; pos <= seq1.getLength(); pos++)
296 map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
298 StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
300 ssm.addStructureMapping(sm1);
301 StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
303 ssm.addStructureMapping(sm2);
306 * put data into the mock binding object
308 PA.setValue(mockBinding, "ssm", ssm);
309 PA.setValue(mockBinding, "sequence", seqs);
311 StructureMappingcommandSet[] commands = ChimeraCommands
312 .getColourBySequenceCommand(files, af.alignPanel, mockBinding);
313 assertEquals(1, commands.length);
314 assertEquals(1, commands[0].commands.length);
315 String theCommand = commands[0].commands[0];
316 // M colour is #82827d (see strand.html help page)
317 assertTrue(theCommand.contains("color #82827d #0:21.A|#1:21.B"));
318 // H colour is #60609f
319 assertTrue(theCommand.contains("color #60609f #0:22.A"));
320 // V colour is #ffff00
321 assertTrue(theCommand.contains("color #ffff00 #1:22.B"));
322 // hidden columns are Gray (128, 128, 128)
323 assertTrue(theCommand.contains("color #808080 #0:23-25.A|#1:23-25.B"));
324 // S and G are both coloured #4949b6
325 assertTrue(theCommand.contains("color #4949b6 #0:26-30.A|#1:26-30.B"));
328 @Test(groups = "Functional")
329 public void testGetAtomSpec()
331 AtomSpecModel model = new AtomSpecModel();
332 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding), "");
333 model.addRange(1, 2, 4, "A");
334 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
336 model.addRange(1, 8, 8, "A");
337 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
339 model.addRange(1, 5, 7, "B");
340 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
341 "#1:2-4.A,8.A,5-7.B");
342 model.addRange(1, 3, 5, "A");
343 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
344 "#1:2-5.A,8.A,5-7.B");
345 model.addRange(0, 1, 4, "B");
346 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
347 "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
348 model.addRange(0, 5, 9, "C");
349 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
350 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
351 model.addRange(1, 8, 10, "B");
352 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
353 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
354 model.addRange(1, 8, 9, "B");
355 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
356 "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
357 model.addRange(0, 3, 10, "C"); // subsumes 5-9
358 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
359 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
360 model.addRange(5, 25, 35, " "); // empty chain code - e.g. from homology
362 assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
363 "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");