Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.ext.rbvi.chimera

File ChimeraCommandsTest.java

 

Code metrics

0
148
26
1
396
307
26
0.18
5.69
26
1

Classes

Class Line # Actions
ChimeraCommandsTest 39 148 26
1.0100%
 

Contributing tests

This file is covered by 25 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.rbvi.chimera;
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.Test;
34   
35    import jalview.structure.AtomSpecModel;
36    import jalview.structure.StructureCommand;
37    import jalview.structure.StructureCommandI;
38   
 
39    public class ChimeraCommandsTest
40    {
41    private ChimeraCommands testee;
42   
 
43  1 toggle @BeforeClass(alwaysRun = true)
44    public void setUp()
45    {
46  1 testee = new ChimeraCommands();
47    }
48   
 
49  1 toggle @Test(groups = { "Functional" })
50    public void testColourBySequence()
51    {
52   
53  1 Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
54  1 ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
55  1 ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
56  1 ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
57  1 ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
58  1 ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
59  1 ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
60  1 ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
61  1 ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
62  1 ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
63   
64    // Colours should appear in the Chimera command in the order in which
65    // they were added; within colour, by model, by chain, ranges in start order
66  1 List<StructureCommandI> commands = testee.colourBySequence(map);
67  1 assertEquals(commands.size(), 1);
68  1 assertEquals(commands.get(0).getCommand(),
69    "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");
70    }
71   
 
72  1 toggle @Test(groups = { "Functional" })
73    public void testSetAttributes()
74    {
75    /*
76    * make a map of { featureType, {featureValue, {residue range specification } } }
77    */
78  1 Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
79  1 Map<Object, AtomSpecModel> featureValues = new HashMap<>();
80   
81    /*
82    * start with just one feature/value...
83    */
84  1 featuresMap.put("chain", featureValues);
85  1 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
86   
87  1 List<StructureCommandI> commands = testee.setAttributes(featuresMap);
88  1 assertEquals(1, commands.size());
89   
90    /*
91    * feature name gets a jv_ namespace prefix
92    * feature value is quoted in case it contains spaces
93    */
94  1 assertEquals(commands.get(0).getCommand(),
95    "setattr res jv_chain 'X' #0:8-20.A");
96   
97    // add same feature value, overlapping range
98  1 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
99    // same feature value, contiguous range
100  1 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
101  1 commands = testee.setAttributes(featuresMap);
102  1 assertEquals(1, commands.size());
103  1 assertEquals(commands.get(0).getCommand(),
104    "setattr res jv_chain 'X' #0:3-25.A");
105   
106    // same feature value and model, different chain
107  1 ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
108    // same feature value and chain, different model
109  1 ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
110  1 commands = testee.setAttributes(featuresMap);
111  1 assertEquals(1, commands.size());
112  1 String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
113  1 assertEquals(commands.get(0).getCommand(), expected1);
114   
115    // same feature, different value
116  1 ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
117  1 commands = testee.setAttributes(featuresMap);
118  1 assertEquals(2, commands.size());
119    // commands are ordered by feature type but not by value
120    // so test for the expected command in either order
121  1 String cmd1 = commands.get(0).getCommand();
122  1 String cmd2 = commands.get(1).getCommand();
123  1 assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
124  1 String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
125  1 assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
126   
127  1 featuresMap.clear();
128  1 featureValues.clear();
129  1 featuresMap.put("side-chain binding!", featureValues);
130  1 ChimeraCommands.addAtomSpecRange(featureValues,
131    "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
132    // feature names are sanitised to change non-alphanumeric to underscore
133    // feature values are sanitised to encode single quote characters
134  1 commands = testee.setAttributes(featuresMap);
135  1 assertEquals(commands.size(), 1);
136  1 String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A";
137  1 assertTrue(commands.get(0).getCommand().equals(expected3));
138    }
139   
140    /**
141    * Tests for the method that prefixes and sanitises a feature name so it can
142    * be used as a valid, namespaced attribute name in Chimera or PyMol
143    */
 
144  1 toggle @Test(groups = { "Functional" })
145    public void testMakeAttributeName()
146    {
147  1 assertEquals(testee.makeAttributeName(null), "jv_");
148  1 assertEquals(testee.makeAttributeName(""), "jv_");
149  1 assertEquals(testee.makeAttributeName("helix"), "jv_helix");
150  1 assertEquals(testee.makeAttributeName(
151    "Hello World 24"),
152    "jv_Hello_World_24");
153  1 assertEquals(testee.makeAttributeName(
154    "!this is-a_very*{odd(name"),
155    "jv__this_is_a_very__odd_name");
156    // name ending in color gets underscore appended
157  1 assertEquals(testee.makeAttributeName("helixColor"), "jv_helixColor_");
158    }
159   
 
160  1 toggle @Test(groups = "Functional")
161    public void testGetAtomSpec()
162    {
163  1 AtomSpecModel model = new AtomSpecModel();
164  1 assertEquals(testee.getAtomSpec(model, false), "");
165  1 model.addRange("1", 2, 4, "A");
166  1 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
167  1 model.addRange("1", 8, 8, "A");
168  1 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
169  1 model.addRange("1", 5, 7, "B");
170  1 assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
171  1 model.addRange("1", 3, 5, "A");
172  1 assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
173  1 model.addRange("0", 1, 4, "B");
174  1 assertEquals(testee.getAtomSpec(model, false),
175    "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
176  1 model.addRange("0", 5, 9, "C");
177  1 assertEquals(testee.getAtomSpec(model, false),
178    "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
179  1 model.addRange("1", 8, 10, "B");
180  1 assertEquals(testee.getAtomSpec(model, false),
181    "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
182  1 model.addRange("1", 8, 9, "B");
183  1 assertEquals(testee.getAtomSpec(model, false),
184    "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
185  1 model.addRange("0", 3, 10, "C"); // subsumes 5-9
186  1 assertEquals(testee.getAtomSpec(model, false),
187    "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
188  1 model.addRange("5", 25, 35, " ");
189  1 assertEquals(testee.getAtomSpec(model, false),
190    "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
191   
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);
207    // qualifier to restrict match to CA and no altlocs
208  1 String carbonAlphas = "@CA&~@.B-Z&~@.2-9";
209  1 String refSpec = "#1:12-14.A,18.B,22-23.B";
210  1 String toAlignSpec = "#2:15-17.B,20-21.B,22.C";
211  1 String expected = String.format("match %s%s %s%s; ribbon %s|%s; focus",
212    toAlignSpec, carbonAlphas, refSpec, carbonAlphas, toAlignSpec,
213    refSpec);
214  1 assertEquals(command.get(0).getCommand(), expected);
215    }
216   
 
217  1 toggle @Test(groups = "Functional")
218    public void testGetAtomSpec_alphaOnly()
219    {
220  1 AtomSpecModel model = new AtomSpecModel();
221  1 assertEquals(testee.getAtomSpec(model, true), "");
222  1 model.addRange("1", 2, 4, "A");
223  1 assertEquals(testee.getAtomSpec(model, true),
224    "#1:2-4.A@CA&~@.B-Z&~@.2-9");
225  1 model.addRange("1", 8, 8, "A");
226  1 assertEquals(testee.getAtomSpec(model, true),
227    "#1:2-4.A,8.A@CA&~@.B-Z&~@.2-9");
228  1 model.addRange("1", 5, 7, "B");
229  1 assertEquals(testee.getAtomSpec(model, true),
230    "#1:2-4.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
231  1 model.addRange("1", 3, 5, "A");
232  1 assertEquals(testee.getAtomSpec(model, true),
233    "#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
234  1 model.addRange("0", 1, 4, "B");
235  1 assertEquals(testee.getAtomSpec(model, true),
236    "#0:1-4.B@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
237  1 model.addRange("0", 5, 9, "C");
238  1 assertEquals(testee.getAtomSpec(model, true),
239    "#0:1-4.B,5-9.C@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
240  1 model.addRange("1", 8, 10, "B");
241  1 assertEquals(testee.getAtomSpec(model, true),
242    "#0:1-4.B,5-9.C@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA&~@.B-Z&~@.2-9");
243  1 model.addRange("1", 8, 9, "B");
244  1 assertEquals(testee.getAtomSpec(model, true),
245    "#0:1-4.B,5-9.C@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA&~@.B-Z&~@.2-9");
246  1 model.addRange("0", 3, 10, "C"); // subsumes 5-9
247  1 assertEquals(testee.getAtomSpec(model, true),
248    "#0:1-4.B,3-10.C@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA&~@.B-Z&~@.2-9");
249  1 model.addRange("5", 25, 35, " "); // empty chain code
250  1 assertEquals(testee.getAtomSpec(model, true),
251    "#0:1-4.B,3-10.C@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-10.B@CA&~@.B-Z&~@.2-9|#5:25-35.@CA&~@.B-Z&~@.2-9");
252   
253    }
254   
 
255  1 toggle @Test(groups = "Functional")
256    public void testGetModelStartNo()
257    {
258  1 assertEquals(testee.getModelStartNo(), 0);
259    }
260   
 
261  1 toggle @Test(groups = "Functional")
262    public void testGetResidueSpec()
263    {
264  1 assertEquals(testee.getResidueSpec("ALA"), "::ALA");
265    }
266   
 
267  1 toggle @Test(groups = "Functional")
268    public void testShowBackbone()
269    {
270  1 List<StructureCommandI> cmds = testee.showBackbone();
271  1 assertEquals(cmds.size(), 1);
272  1 assertEquals(cmds.get(0).getCommand(),
273    "~display all;~ribbon;chain @CA|P");
274    }
275   
 
276  1 toggle @Test(groups = "Functional")
277    public void testOpenCommandFile()
278    {
279  1 assertEquals(testee.openCommandFile("nowhere").getCommand(),
280    "open cmd:nowhere");
281    }
282   
 
283  1 toggle @Test(groups = "Functional")
284    public void testSaveSession()
285    {
286  1 assertEquals(testee.saveSession("somewhere").getCommand(),
287    "save somewhere");
288    }
289   
 
290  1 toggle @Test(groups = "Functional")
291    public void testColourByChain()
292    {
293  1 assertEquals(testee.colourByChain().getCommand(), "rainbow chain");
294    }
295   
 
296  1 toggle @Test(groups = { "Functional" })
297    public void testSetBackgroundColour()
298    {
299  1 StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
300  1 assertEquals(cmd.getCommand(), "set bgColor #ffafaf");
301    }
302   
 
303  1 toggle @Test(groups = { "Functional" })
304    public void testLoadFile()
305    {
306  1 StructureCommandI cmd = testee.loadFile("/some/filepath");
307  1 assertEquals(cmd.getCommand(), "open /some/filepath");
308    }
309   
 
310  1 toggle @Test(groups = { "Functional" })
311    public void testOpenSession()
312    {
313  1 StructureCommandI cmd = testee.openSession("/some/filepath");
314  1 assertEquals(cmd.getCommand(), "open chimera:/some/filepath");
315    }
316   
 
317  1 toggle @Test(groups = "Functional")
318    public void testColourByCharge()
319    {
320  1 List<StructureCommandI> cmds = testee.colourByCharge();
321  1 assertEquals(cmds.size(), 1);
322  1 assertEquals(cmds.get(0)
323    .getCommand(),
324    "color white;color red ::ASP,GLU;color blue ::LYS,ARG;color yellow ::CYS");
325    }
326   
 
327  1 toggle @Test(groups = "Functional")
328    public void testGetColourCommand()
329    {
330  1 assertEquals(testee.colourResidues("something", Color.MAGENTA)
331    .getCommand(),
332    "color #ff00ff something");
333    }
334   
 
335  1 toggle @Test(groups = "Functional")
336    public void testFocusView()
337    {
338  1 assertEquals(testee.focusView().getCommand(), "focus");
339    }
340   
 
341  1 toggle @Test(groups = "Functional")
342    public void testSetAttribute()
343    {
344  1 AtomSpecModel model = new AtomSpecModel();
345  1 model.addRange("1", 89, 92, "A");
346  1 model.addRange("2", 12, 20, "B");
347  1 model.addRange("2", 8, 9, "B");
348  1 assertEquals(testee.setAttribute("jv_kd", "27.3", model).getCommand(),
349    "setattr res jv_kd '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
350    }
351   
 
352  1 toggle @Test(groups = "Functional")
353    public void testCloseViewer()
354    {
355  1 assertEquals(testee.closeViewer(), new StructureCommand("stop really"));
356    }
357   
 
358  1 toggle @Test(groups = "Functional")
359    public void testGetSelectedResidues()
360    {
361  1 assertEquals(testee.getSelectedResidues(),
362    new StructureCommand("list selection level residue"));
363    }
364   
 
365  1 toggle @Test(groups = "Functional")
366    public void testListResidueAttributes()
367    {
368  1 assertEquals(testee.listResidueAttributes(),
369    new StructureCommand("list resattr"));
370    }
371   
 
372  1 toggle @Test(groups = "Functional")
373    public void testGetResidueAttributes()
374    {
375  1 assertEquals(testee.getResidueAttributes("binding site"),
376    new StructureCommand("list residues attr 'binding site'"));
377    }
378   
 
379  1 toggle @Test(groups = "Functional")
380    public void testStartNotifications()
381    {
382  1 List<StructureCommandI> cmds = testee.startNotifications("to here");
383  1 assertEquals(cmds.size(), 2);
384  1 assertEquals(cmds.get(0), new StructureCommand("listen start models url to here"));
385  1 assertEquals(cmds.get(1), new StructureCommand("listen start select prefix SelectionChanged url to here"));
386    }
387   
 
388  1 toggle @Test(groups = "Functional")
389    public void testStopNotifications()
390    {
391  1 List<StructureCommandI> cmds = testee.stopNotifications();
392  1 assertEquals(cmds.size(), 2);
393  1 assertEquals(cmds.get(0), new StructureCommand("listen stop models"));
394  1 assertEquals(cmds.get(1), new StructureCommand("listen stop selection"));
395    }
396    }