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.StructureCommandsI.AtomSpecType;
46 import jalview.structure.StructureMapping;
47 import jalview.structure.StructureSelectionManager;
49 public class JmolCommandsTest
51 private JmolCommands testee;
53 @BeforeClass(alwaysRun = true)
56 testee = new JmolCommands();
59 @Test(groups = { "Functional" })
60 public void testGetColourBySequenceCommands_hiddenColumns()
63 * load these sequences, coloured by Strand propensity,
64 * with columns 2-4 hidden
66 SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
67 SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
68 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
69 AlignFrame af = new AlignFrame(al, 800, 500);
70 af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
71 ColumnSelection cs = new ColumnSelection();
75 af.getViewport().setColumnSelection(cs);
76 af.hideSelColumns_actionPerformed(null);
77 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
78 SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
79 String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
80 StructureSelectionManager ssm = StructureSelectionManager.getStructureSelectionManager(null);
83 * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
85 HashMap<Integer, int[]> map = new HashMap<>();
86 for (int pos = 1; pos <= seq1.getLength(); pos++)
88 map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
90 StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
92 ssm.addStructureMapping(sm1);
93 StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
95 ssm.addStructureMapping(sm2);
97 // TODO - comments in testee suggest this tests an obsolete method!
98 String[] commands = testee.colourBySequence(ssm, files, seqs, sr,
100 assertEquals(commands.length, 2);
102 String chainACommand = commands[0];
103 // M colour is #82827d == (130, 130, 125) (see strand.html help page)
105 chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first
107 // H colour is #60609f == (96, 96, 159)
108 assertTrue(chainACommand.contains(";select 22:A/1.1;color[96,96,159]"));
109 // hidden columns are Gray (128, 128, 128)
110 assertTrue(chainACommand
111 .contains(";select 23-25:A/1.1;color[128,128,128]"));
112 // S and G are both coloured #4949b6 == (73, 73, 182)
114 chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
116 String chainBCommand = commands[1];
117 // M colour is #82827d == (130, 130, 125)
119 chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
120 // V colour is #ffff00 == (255, 255, 0)
121 assertTrue(chainBCommand.contains(";select 22:B/2.1;color[255,255,0]"));
122 // hidden columns are Gray (128, 128, 128)
123 assertTrue(chainBCommand
124 .contains(";select 23-25:B/2.1;color[128,128,128]"));
125 // S and G are both coloured #4949b6 == (73, 73, 182)
127 chainBCommand.contains(";select 26-30:B/2.1;color[73,73,182]"));
130 @Test(groups = "Functional")
131 public void testGetAtomSpec()
133 AtomSpecModel model = new AtomSpecModel();
134 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "");
135 model.addRange("1", 2, 4, "A");
136 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
138 model.addRange("1", 8, 8, "A");
139 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
140 "2-4:A/1.1|8:A/1.1");
141 model.addRange("1", 5, 7, "B");
142 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
143 "2-4:A/1.1|8:A/1.1|5-7:B/1.1");
144 model.addRange("1", 3, 5, "A");
145 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
146 "2-5:A/1.1|8:A/1.1|5-7:B/1.1");
147 model.addRange("2", 1, 4, "B");
148 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
149 "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1");
150 model.addRange("2", 5, 9, "C");
151 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
152 "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1|5-9:C/2.1");
153 model.addRange("1", 8, 10, "B");
154 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
155 "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
156 model.addRange("1", 8, 9, "B");
157 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
158 "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
159 model.addRange("2", 3, 10, "C"); // subsumes 5-9
160 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
161 "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1");
162 model.addRange("5", 25, 35, " ");
163 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
164 "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1|25-35:/5.1");
168 @Test(groups = { "Functional" })
169 public void testColourBySequence()
171 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
172 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
173 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
174 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
175 JmolCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
176 JmolCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
177 JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
178 JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
179 JmolCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
180 JmolCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
182 // Colours should appear in the Jmol command in the order in which
183 // they were added; within colour, by model, by chain, ranges in start order
184 List<StructureCommandI> commands = testee.colourBySequence(map);
185 assertEquals(commands.size(), 1);
186 String expected1 = "select 2-5:A/1.1|9-23:A/1.1|7:B/1.1|1:A/2.1|4-7:B/2.1;color[0,0,255]";
187 String expected2 = "select 3-5:A/2.1|8:A/2.1;color[255,255,0]";
188 String expected3 = "select 3-9:A/1.1;color[255,0,0]";
189 assertEquals(commands.get(0).getCommand(),
190 expected1 + ";" + expected2 + ";" + expected3);
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,
205 toAlign, AtomSpecType.ALPHA); // doesn't matter for Jmol whether nuc
207 assertEquals(command.size(), 1);
208 String refSpec = "12-14:A/1.1|18:B/1.1|22-23:B/1.1";
209 String toAlignSpec = "15-17:B/2.1|20-21:B/2.1|22:C/2.1";
210 String expected = String.format(
211 "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
212 toAlignSpec, refSpec, toAlignSpec, refSpec);
213 assertEquals(command.get(0).getCommand(), expected);
216 @Test(groups = "Functional")
217 public void testGetModelStartNo()
219 assertEquals(testee.getModelStartNo(), 1);
222 @Test(groups = "Functional")
223 public void testColourByChain()
225 StructureCommandI cmd = testee.colourByChain();
226 assertEquals(cmd.getCommand(), "select *;color chain");
229 @Test(groups = "Functional")
230 public void testColourByCharge()
232 List<StructureCommandI> cmds = testee.colourByCharge();
233 assertEquals(cmds.size(), 1);
234 assertEquals(cmds.get(0).getCommand(),
235 "select *;color white;select ASP,GLU;color red;"
236 + "select LYS,ARG;color blue;select CYS;color yellow");
239 @Test(groups = "Functional")
240 public void testSetBackgroundColour()
242 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
243 assertEquals(cmd.getCommand(), "background [255,175,175]");
246 @Test(groups = "Functional")
247 public void testFocusView()
249 StructureCommandI cmd = testee.focusView();
250 assertEquals(cmd.getCommand(), "zoom 0");
253 @Test(groups = "Functional")
254 public void testSaveSession()
256 StructureCommandI cmd = testee.saveSession("/some/filepath");
257 assertEquals(cmd.getCommand(), "write STATE \"/some/filepath\"");
260 @Test(groups = "Functional")
261 public void testShowBackbone()
263 List<StructureCommandI> cmds = testee.showBackbone();
264 assertEquals(cmds.size(), 1);
265 assertEquals(cmds.get(0).getCommand(),
266 "select *; cartoons off; backbone");
269 @Test(groups = "Functional")
270 public void testLoadFile()
272 StructureCommandI cmd = testee.loadFile("/some/filepath");
273 assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
275 // single backslash gets escaped to double
276 cmd = testee.loadFile("\\some\\filepath");
277 assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
280 @Test(groups = "Functional")
281 public void testOpenSession()
283 StructureCommandI cmd = testee.openSession("/some/filepath");
284 assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
286 // single backslash gets escaped to double
287 cmd = testee.openSession("\\some\\filepath");
288 assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");