Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.ext.jmol

File JmolCommandsTest.java

 

Code metrics

2
121
18
1
377
283
19
0.16
6.72
18
1.06

Classes

Class Line # Actions
JmolCommandsTest 50 121 19
1.0100%
 

Contributing tests

This file is covered by 21 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.ext.jmol;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertTrue;
25   
26    import java.awt.Color;
27    import java.util.HashMap;
28    import java.util.LinkedHashMap;
29    import java.util.List;
30    import java.util.Map;
31   
32    import org.testng.annotations.BeforeClass;
33    import org.testng.annotations.DataProvider;
34    import org.testng.annotations.Test;
35   
36    import jalview.datamodel.Alignment;
37    import jalview.datamodel.AlignmentI;
38    import jalview.datamodel.ColumnSelection;
39    import jalview.datamodel.Sequence;
40    import jalview.datamodel.SequenceI;
41    import jalview.gui.AlignFrame;
42    import jalview.gui.SequenceRenderer;
43    import jalview.schemes.JalviewColourScheme;
44    import jalview.structure.AtomSpecModel;
45    import jalview.structure.StructureCommandI;
46    import jalview.structure.StructureCommandsI.AtomSpecType;
47    import jalview.structure.StructureMapping;
48    import jalview.structure.StructureSelectionManager;
49   
 
50    public class JmolCommandsTest
51    {
52    private JmolCommands testee;
53   
 
54  1 toggle @BeforeClass(alwaysRun = true)
55    public void setUp()
56    {
57  1 testee = new JmolCommands();
58    }
59   
 
60  1 toggle @Test(groups = { "Functional" })
61    public void testGetColourBySequenceCommands_hiddenColumns()
62    {
63    /*
64    * load these sequences, coloured by Strand propensity,
65    * with columns 2-4 hidden
66    */
67  1 SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
68  1 SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
69  1 AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
70  1 AlignFrame af = new AlignFrame(al, 800, 500);
71  1 af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
72  1 ColumnSelection cs = new ColumnSelection();
73  1 cs.addElement(2);
74  1 cs.addElement(3);
75  1 cs.addElement(4);
76  1 af.getViewport().setColumnSelection(cs);
77  1 af.hideSelColumns_actionPerformed(null);
78  1 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
79  1 SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
80  1 String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
81  1 StructureSelectionManager ssm = StructureSelectionManager.getStructureSelectionManager(null);
82   
83    /*
84    * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
85    */
86  1 HashMap<Integer, int[]> map = new HashMap<>();
87  11 for (int pos = 1; pos <= seq1.getLength(); pos++)
88    {
89  10 map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
90    }
91  1 StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
92    "A", map, null);
93  1 ssm.addStructureMapping(sm1);
94  1 StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
95    "B", map, null);
96  1 ssm.addStructureMapping(sm2);
97   
98    // TODO - comments in testee suggest this tests an obsolete method!
99  1 String[] commands = testee.colourBySequence(ssm, files, seqs, sr,
100    af.alignPanel);
101  1 assertEquals(commands.length, 2);
102   
103  1 String chainACommand = commands[0];
104    // M colour is #82827d == (130, 130, 125) (see strand.html help page)
105  1 assertTrue(
106    chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first
107    // one
108    // H colour is #60609f == (96, 96, 159)
109  1 assertTrue(chainACommand.contains(";select 22:A/1.1;color[96,96,159]"));
110    // hidden columns are Gray (128, 128, 128)
111  1 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)
114  1 assertTrue(
115    chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
116   
117  1 String chainBCommand = commands[1];
118    // M colour is #82827d == (130, 130, 125)
119  1 assertTrue(
120    chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
121    // V colour is #ffff00 == (255, 255, 0)
122  1 assertTrue(chainBCommand.contains(";select 22:B/2.1;color[255,255,0]"));
123    // hidden columns are Gray (128, 128, 128)
124  1 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)
127  1 assertTrue(
128    chainBCommand.contains(";select 26-30:B/2.1;color[73,73,182]"));
129    }
130   
 
131  1 toggle @Test(groups = "Functional")
132    public void testGetAtomSpec()
133    {
134  1 AtomSpecModel model = new AtomSpecModel();
135  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "");
136  1 model.addRange("1", 2, 4, "A");
137  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
138    "2-4:A/1.1");
139  1 model.addRange("1", 8, 8, "A");
140  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
141    "2-4:A/1.1|8:A/1.1");
142  1 model.addRange("1", 5, 7, "B");
143  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
144    "2-4:A/1.1|8:A/1.1|5-7:B/1.1");
145  1 model.addRange("1", 3, 5, "A");
146  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
147    "2-5:A/1.1|8:A/1.1|5-7:B/1.1");
148  1 model.addRange("2", 1, 4, "B");
149  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
150    "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1");
151  1 model.addRange("2", 5, 9, "C");
152  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
153    "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1|5-9:C/2.1");
154  1 model.addRange("1", 8, 10, "B");
155  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
156    "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
157  1 model.addRange("1", 8, 9, "B");
158  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
159    "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
160  1 model.addRange("2", 3, 10, "C"); // subsumes 5-9
161  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
162    "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1");
163  1 model.addRange("5", 25, 35, " ");
164  1 assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
165    "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");
166   
167    }
168   
 
169  1 toggle @Test(groups = { "Functional" })
170    public void testColourBySequence()
171    {
172  1 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
173  1 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
174  1 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
175  1 JmolCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
176  1 JmolCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
177  1 JmolCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
178  1 JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
179  1 JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
180  1 JmolCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
181  1 JmolCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
182   
183    // Colours should appear in the Jmol command in the order in which
184    // they were added; within colour, by model, by chain, ranges in start order
185  1 List<StructureCommandI> commands = testee.colourBySequence(map);
186  1 assertEquals(commands.size(), 1);
187  1 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]";
188  1 String expected2 = "select 3-5:A/2.1|8:A/2.1;color[255,255,0]";
189  1 String expected3 = "select 3-9:A/1.1;color[255,0,0]";
190  1 assertEquals(commands.get(0).getCommand(),
191    expected1 + ";" + expected2 + ";" + expected3);
192    }
193   
 
194  1 toggle @Test(groups = { "Functional" })
195    public void testSuperposeStructures()
196    {
197  1 AtomSpecModel ref = new AtomSpecModel();
198  1 ref.addRange("1", 12, 14, "A");
199  1 ref.addRange("1", 18, 18, "B");
200  1 ref.addRange("1", 22, 23, "B");
201  1 AtomSpecModel toAlign = new AtomSpecModel();
202  1 toAlign.addRange("2", 15, 17, "B");
203  1 toAlign.addRange("2", 20, 21, "B");
204  1 toAlign.addRange("2", 22, 22, "C");
205  1 List<StructureCommandI> command = testee.superposeStructures(ref,
206    toAlign, AtomSpecType.ALPHA); // doesn't matter for Jmol whether nuc
207    // or protein
208  1 assertEquals(command.size(), 1);
209  1 String refSpec = "12-14:A/1.1|18:B/1.1|22-23:B/1.1";
210  1 String toAlignSpec = "15-17:B/2.1|20-21:B/2.1|22:C/2.1";
211  1 String expected = String.format(
212    "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
213    toAlignSpec, refSpec, toAlignSpec, refSpec);
214  1 assertEquals(command.get(0).getCommand(), expected);
215    }
216   
 
217  1 toggle @Test(groups = "Functional")
218    public void testGetModelStartNo()
219    {
220  1 assertEquals(testee.getModelStartNo(), 1);
221    }
222   
 
223  1 toggle @Test(groups = "Functional")
224    public void testColourByChain()
225    {
226  1 StructureCommandI cmd = testee.colourByChain();
227  1 assertEquals(cmd.getCommand(), "select *;color chain");
228    }
229   
 
230  1 toggle @Test(groups = "Functional")
231    public void testColourByCharge()
232    {
233  1 List<StructureCommandI> cmds = testee.colourByCharge();
234  1 assertEquals(cmds.size(), 1);
235  1 assertEquals(cmds.get(0).getCommand(),
236    "select *;color white;select ASP,GLU;color red;"
237    + "select LYS,ARG;color blue;select CYS;color yellow");
238    }
239   
 
240  1 toggle @Test(groups = "Functional")
241    public void testSetBackgroundColour()
242    {
243  1 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
244  1 assertEquals(cmd.getCommand(), "background [255,175,175]");
245    }
246   
 
247  1 toggle @Test(groups = "Functional")
248    public void testFocusView()
249    {
250  1 StructureCommandI cmd = testee.focusView();
251  1 assertEquals(cmd.getCommand(), "zoom 0");
252    }
253   
 
254  1 toggle @Test(groups = "Functional")
255    public void testSaveSession()
256    {
257  1 StructureCommandI cmd = testee.saveSession("/some/filepath");
258  1 assertEquals(cmd.getCommand(), "write STATE \"/some/filepath\"");
259  1 cmd = testee.saveSession("\\some\\filepath");
260  1 assertEquals(cmd.getCommand(), "write STATE \"\\\\some\\\\filepath\"");
261    }
262   
 
263  1 toggle @Test(groups = "Functional")
264    public void testShowBackbone()
265    {
266  1 List<StructureCommandI> cmds = testee.showBackbone();
267  1 assertEquals(cmds.size(), 1);
268  1 assertEquals(cmds.get(0).getCommand(),
269    "select *; cartoons off; backbone");
270    }
271   
 
272  1 toggle @Test(groups = "Functional")
273    public void testLoadFile()
274    {
275  1 StructureCommandI cmd = testee.loadFile("/some/filepath");
276  1 assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
277   
278    // single backslash gets escaped to double
279  1 cmd = testee.loadFile("\\some\\filepath");
280  1 assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
281   
282    // double backslash gets escaped to file:\\\\
283  1 cmd = testee.loadFile("\\\\some\\filepath");
284  1 assertEquals(cmd.getCommand(),
285    "load FILES \"file:\\\\\\\\some\\\\filepath\"");
286   
287    // double forward slash is escaped to file:////
288  1 cmd = testee.loadFile("//some/filepath");
289  1 assertEquals(cmd.getCommand(), "load FILES \"file://some/filepath\"");
290   
291    }
292   
 
293  4 toggle @Test(
294    groups = "Functional",
295    dataProvider = "testFilePathEquivalence",
296    singleThreaded = true)
297    public void testFilePathEquivalence(String jalviewpath, String jmolpath)
298    {
299  4 assertEquals(JmolCommands.escapeQuotedFilename(jalviewpath), jmolpath,
300    "for " + jalviewpath);
301    // simple unescape
302  4 assertTrue(JmolCommands.filePathMatch(jmolpath.replace("\\\\", "\\"),
303    jalviewpath));
304    }
305   
306    /**
307    * Windows path transformations for Jmol 14.31.53 NOLOG4J positive examples
308    * for escapeQuotedFilename - results of escaping should, after a simple
309    * unescape (remove escape backslashes) also work for filePathMatch
310    *
311    * @return
312    */
313   
 
314  1 toggle @DataProvider(name = "testFilePathEquivalence")
315    public Object[][] fileEquivalences()
316    {
317  1 return new String[][] {
318    new String[]
319    { "/some/filepath", "/some/filepath" },
320    new String[]
321    { "\\some\\filepath", "\\\\some\\\\filepath" },
322    new String[]
323    { "\\\\some\\filepath", "file:\\\\\\\\some\\\\filepath" },
324    new String[]
325    { "//some/filepath", "file://some/filepath" } };
326    }
327   
 
328  7 toggle @Test(
329    groups = "Functional",
330    dataProvider = "testForwardBackslashFilePathEquivalence")
331    public void filePathMatchTest(String jmolPath, String jalviewPath)
332    {
333  7 assertTrue(JmolCommands.filePathMatch(jmolPath, jalviewPath));
334    }
335   
336    /**
337    * Windows path transformations for Jmol 14.31.53 NOLOG4J positive examples
338    * for JmolCommands.filePathMatch - handles windows network drive
339    * transformations
340    *
341    * @return
342    */
 
343  1 toggle @DataProvider(name = "testForwardBackslashFilePathEquivalence")
344    public Object[][] forwardBackslashFilePathEquivalence()
345    {
346  1 return new String[][] {
347    new String[]
348    { "file:\\\\some\\filepath", "\\\\some\\filepath" },
349    new String[]
350    { "file://some/filepath", "\\\\some\\filepath" },
351    new String[]
352    { "file:////some//filepath", "\\\\some\\filepath" },
353    new String[]
354    { "file:\\\\\\\\estorage.dundee.ac.uk\\\\cluster-gjb_lab\\\\jprocter\\\\public_html\\\\1QIP.cif",
355    "//estorage.dundee.ac.uk/cluster-gjb_lab/jprocter/public_html/1QIP.cif" },
356    new String[]
357    { "file:\\\\\\\\estorage.dundee.ac.uk\\\\cluster-gjb_lab\\\\jprocter\\\\public_html\\\\1QIP.cif",
358    "\\\\estorage.dundee.ac.uk\\cluster-gjb_lab\\jprocter\\public_html\\1QIP.cif" },
359    new String[]
360    { "file://estorage.dundee.ac.uk/cluster-gjb_lab/jprocter/public_html/1QIP.cif",
361    "\\\\estorage.dundee.ac.uk\\cluster-gjb_lab\\jprocter\\public_html\\1QIP.cif" },
362    new String[]
363    { "file:////estorage.dundee.ac.uk//cluster-gjb_lab//jprocter//public_html//1QIP.cif",
364    "\\\\estorage.dundee.ac.uk\\cluster-gjb_lab\\jprocter\\public_html\\1QIP.cif" } };
365    }
366   
 
367  1 toggle @Test(groups = "Functional")
368    public void testOpenSession()
369    {
370  1 StructureCommandI cmd = testee.openSession("/some/filepath");
371  1 assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
372   
373    // single backslash gets escaped to double
374  1 cmd = testee.openSession("\\some\\filepath");
375  1 assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
376    }
377    }