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.jmol;
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.datamodel.Alignment;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.ColumnSelection;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceI;
40 import jalview.gui.AlignFrame;
41 import jalview.gui.SequenceRenderer;
42 import jalview.schemes.JalviewColourScheme;
43 import jalview.structure.AtomSpecModel;
44 import jalview.structure.StructureCommandI;
45 import jalview.structure.StructureMapping;
46 import jalview.structure.StructureSelectionManager;
48 public class JmolCommandsTest
50 private JmolCommands testee;
52 @BeforeClass(alwaysRun = true)
55 testee = new JmolCommands();
59 * Test for the now deprecated version of getColourBySequenceCommand
61 @Test(groups = { "Functional" }, enabled = false)
62 public void testGetColourBySequenceCommands_hiddenColumns()
65 * load these sequences, coloured by Strand propensity,
66 * with columns 2-4 hidden
68 SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
69 SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
70 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
71 AlignFrame af = new AlignFrame(al, 800, 500);
72 af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
73 ColumnSelection cs = new ColumnSelection();
77 af.getViewport().setColumnSelection(cs);
78 af.hideSelColumns_actionPerformed(null);
79 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
80 SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
81 String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
82 StructureSelectionManager ssm = new StructureSelectionManager();
85 * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
87 HashMap<Integer, int[]> map = new HashMap<>();
88 for (int pos = 1; pos <= seq1.getLength(); pos++)
90 map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
92 StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
94 ssm.addStructureMapping(sm1);
95 StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
97 ssm.addStructureMapping(sm2);
99 String[] commands = testee.colourBySequence(ssm, files, seqs,
102 assertEquals(commands.length, 2);
104 String chainACommand = commands[0];
105 // M colour is #82827d == (130, 130, 125) (see strand.html help page)
107 chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first one
108 // H colour is #60609f == (96, 96, 159)
109 assertTrue(chainACommand.contains(";select 22:A/1.1;color[96,96,159]"));
110 // hidden columns are Gray (128, 128, 128)
111 assertTrue(chainACommand
112 .contains(";select 23-25:A/1.1;color[128,128,128]"));
113 // S and G are both coloured #4949b6 == (73, 73, 182)
115 chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
117 String chainBCommand = commands[1];
118 // M colour is #82827d == (130, 130, 125)
120 chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
121 // V colour is #ffff00 == (255, 255, 0)
122 assertTrue(chainBCommand.contains(";select 22:B/2.1;color[255,255,0]"));
123 // hidden columns are Gray (128, 128, 128)
124 assertTrue(chainBCommand
125 .contains(";select 23-25:B/2.1;color[128,128,128]"));
126 // S and G are both coloured #4949b6 == (73, 73, 182)
128 chainBCommand.contains(";select 26-30:B/2.1;color[73,73,182]"));
131 @Test(groups = { "Functional" })
132 public void testGetAtomSpec()
134 AtomSpecModel model = new AtomSpecModel();
135 assertEquals(testee.getAtomSpec(model, false), "");
138 * Jalview numbers models from 0, Jmol from 1
140 model.addRange("2", 2, 4, "A");
141 assertEquals(testee.getAtomSpec(model, false), "2-4:A/2.1");
143 model.addRange("2", 8, 8, "A");
144 assertEquals(testee.getAtomSpec(model, false), "(2-4,8)&:A/2.1");
146 model.addRange("2", 5, 7, "B");
147 assertEquals(testee.getAtomSpec(model,
149 "(2-4,8)&:A/2.1,5-7:B/2.1");
151 model.addRange("2", 3, 5, "A");
152 // 3-5 merges with 2-4 to make 2-5:
153 assertEquals(testee.getAtomSpec(model,
155 "(2-5,8)&:A/2.1,5-7:B/2.1");
157 model.addRange("1", 1, 4, "B");
158 assertEquals(testee.getAtomSpec(model,
160 "1-4:B/1.1,(2-5,8)&:A/2.1,5-7:B/2.1");
162 model.addRange("1", 5, 9, "C");
163 assertEquals(testee.getAtomSpec(model,
165 "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-7:B/2.1");
167 model.addRange("2", 8, 10, "B");
168 // 8-10 extends 5-7 to make 5-10
169 assertEquals(testee.getAtomSpec(model,
171 "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
173 model.addRange("2", 8, 9, "B");
174 // subsumed by 5-10 so makes no difference
175 assertEquals(testee.getAtomSpec(model,
177 "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
179 model.addRange("1", 3, 10, "C");
180 // subsumes 5-9 so replaces it
181 assertEquals(testee.getAtomSpec(model,
183 "1-4:B/1.1,3-10:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
185 // empty chain code - e.g. from homology modelling
186 model.addRange("6", 25, 35, " ");
187 assertEquals(testee.getAtomSpec(model,
189 "1-4:B/1.1,3-10:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1,25-35:/6.1");
192 @Test(groups = { "Functional" })
193 public void testColourBySequence()
195 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
196 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
197 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
198 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
199 JmolCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
200 JmolCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
201 JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
202 JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
203 JmolCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
204 JmolCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
206 // Colours should appear in the Jmol command in the order in which
207 // they were added; within colour, by model, by chain, ranges in start order
208 List<StructureCommandI> commands = testee.colourBySequence(map);
209 assertEquals(commands.size(), 1);
210 String expected1 = "select (2-5,9-23)&:A/1.1,7:B/1.1,1:A/2.1,4-7:B/2.1;color[0,0,255]";
211 String expected2 = "select (3-5,8)&:A/2.1;color[255,255,0]";
212 String expected3 = "select 3-9:A/1.1;color[255,0,0]";
213 assertEquals(commands.get(0).getCommand(),
214 expected1 + ";" + expected2 + ";" + expected3);
217 @Test(groups = { "Functional" })
218 public void testSuperposeStructures()
220 AtomSpecModel ref = new AtomSpecModel();
221 ref.addRange("1", 12, 14, "A");
222 ref.addRange("1", 18, 18, "B");
223 ref.addRange("1", 22, 23, "B");
224 AtomSpecModel toAlign = new AtomSpecModel();
225 toAlign.addRange("2", 15, 17, "B");
226 toAlign.addRange("2", 20, 21, "B");
227 toAlign.addRange("2", 22, 22, "C");
228 List<StructureCommandI> command = testee.superposeStructures(ref,
230 assertEquals(command.size(), 1);
231 String refSpec = "12-14:A/1.1,(18,22-23)&:B/1.1";
232 String toAlignSpec = "(15-17,20-21)&:B/2.1,22:C/2.1";
233 String expected = String.format(
234 "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
235 toAlignSpec, refSpec, toAlignSpec, refSpec);
236 assertEquals(command.get(0).getCommand(), expected);
239 @Test(groups = "Functional")
240 public void testColourByChain()
242 StructureCommandI cmd = testee.colourByChain();
243 assertEquals(cmd.getCommand(), "select *;color chain");
246 @Test(groups = "Functional")
247 public void testColourByCharge()
249 List<StructureCommandI> cmds = testee.colourByCharge();
250 assertEquals(cmds.size(), 1);
251 assertEquals(cmds.get(0).getCommand(),
252 "select *;color white;select ASP,GLU;color red;"
253 + "select LYS,ARG;color blue;select CYS;color yellow");
256 @Test(groups = "Functional")
257 public void testSetBackgroundColour()
259 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
260 assertEquals(cmd.getCommand(), "background [255,175,175]");
263 @Test(groups = "Functional")
264 public void testFocusView()
266 StructureCommandI cmd = testee.focusView();
267 assertEquals(cmd.getCommand(), "zoom 0");
270 @Test(groups = "Functional")
271 public void testSaveSession()
273 StructureCommandI cmd = testee.saveSession("/some/filepath");
274 assertEquals(cmd.getCommand(), "write STATE \"/some/filepath\"");
277 @Test(groups = "Functional")
278 public void testShowBackbone()
280 List<StructureCommandI> cmds = testee.showBackbone();
281 assertEquals(cmds.size(), 1);
282 assertEquals(cmds.get(0).getCommand(),
283 "select *; cartoons off; backbone");
286 @Test(groups = "Functional")
287 public void testLoadFile()
289 StructureCommandI cmd = testee.loadFile("/some/filepath");
290 assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
292 // single backslash gets escaped to double
293 cmd = testee.loadFile("\\some\\filepath");
294 assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
297 @Test(groups = "Functional")
298 public void testOpenSession()
300 StructureCommandI cmd = testee.openSession("/some/filepath");
301 assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
303 // single backslash gets escaped to double
304 cmd = testee.openSession("\\some\\filepath");
305 assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
308 @Test(groups = "Functional")
309 public void testHideAll()
311 StructureCommandI cmd = testee.hideAll();
312 assertEquals(cmd.getCommand(), "hide *");
315 @Test(groups = "Functional")
316 public void testHideChain()
318 StructureCommandI cmd = testee.hideChain("1.1", "B");
319 assertEquals(cmd.getCommand(), "hide add :B/1.1");
322 @Test(groups = "Functional")
323 public void testShowStructures()
326 * with nothing excluded
328 StructureCommandI cmd = testee.showStructures(null);
329 assertEquals(cmd.getCommand(), "display *; cartoon only");
332 * restricted to specified positions
334 AtomSpecModel restrictTo = new AtomSpecModel();
335 restrictTo.addRange("1.1", 12, 20, "A");
336 restrictTo.addRange("1.1", 30, 35, "A");
337 restrictTo.addRange("2.1", 11, 30, "B");
338 cmd = testee.showStructures(restrictTo);
339 assertEquals(cmd.getCommand(),
340 "display (12-20,30-35)&:A/1.1.1,11-30:B/2.1.1; select displayed; cartoon only");