JAL-2422 JAL-3551 unit tests updated for code changes
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraCommandsTest.java
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.Test;
33
34 import jalview.structure.AtomSpecModel;
35 import jalview.structure.StructureCommandI;
36 import jalview.structure.StructureCommandsI;
37
38 public class ChimeraCommandsTest
39 {
40
41   @Test(groups = { "Functional" })
42   public void testColourBySequence()
43   {
44
45     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
46     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
47     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
48     ChimeraCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
49     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
50     ChimeraCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
51     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
52     ChimeraCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
53     ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
54     ChimeraCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
55
56     // Colours should appear in the Chimera command in the order in which
57     // they were added; within colour, by model, by chain, ranges in start order
58     List<StructureCommandI> commands = new ChimeraCommands()
59             .colourBySequence(map);
60     assertEquals(commands.size(), 1);
61     assertEquals(commands.get(0).getCommand(),
62             "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");
63   }
64
65   @Test(groups = { "Functional" })
66   public void testSetAttributes()
67   {
68     /*
69      * make a map of { featureType, {featureValue, {residue range specification } } }
70      */
71     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
72     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
73     
74     /*
75      * start with just one feature/value...
76      */
77     featuresMap.put("chain", featureValues);
78     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
79   
80     ChimeraCommands commandGenerator = new ChimeraCommands();
81     List<StructureCommandI> commands = commandGenerator
82             .setAttributes(featuresMap);
83     assertEquals(1, commands.size());
84
85     /*
86      * feature name gets a jv_ namespace prefix
87      * feature value is quoted in case it contains spaces
88      */
89     assertEquals(commands.get(0).getCommand(),
90             "setattr res jv_chain 'X' #0:8-20.A");
91
92     // add same feature value, overlapping range
93     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
94     // same feature value, contiguous range
95     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
96     commands = commandGenerator.setAttributes(featuresMap);
97     assertEquals(1, commands.size());
98     assertEquals(commands.get(0).getCommand(),
99             "setattr res jv_chain 'X' #0:3-25.A");
100
101     // same feature value and model, different chain
102     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
103     // same feature value and chain, different model
104     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
105     commands = commandGenerator.setAttributes(featuresMap);
106     assertEquals(1, commands.size());
107     String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
108     assertEquals(commands.get(0).getCommand(), expected1);
109
110     // same feature, different value
111     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
112     commands = commandGenerator.setAttributes(featuresMap);
113     assertEquals(2, commands.size());
114     // commands are ordered by feature type but not by value
115     // so test for the expected command in either order
116     String cmd1 = commands.get(0).getCommand();
117     String cmd2 = commands.get(1).getCommand();
118     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
119     String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
120     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
121
122     featuresMap.clear();
123     featureValues.clear();
124     featuresMap.put("side-chain binding!", featureValues);
125     ChimeraCommands.addAtomSpecRange(featureValues,
126             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15,
127             "A");
128     // feature names are sanitised to change non-alphanumeric to underscore
129     // feature values are sanitised to encode single quote characters
130     commands = commandGenerator.setAttributes(featuresMap);
131     assertEquals(commands.size(), 1);
132     String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A";
133     assertTrue(commands.get(0).getCommand().equals(expected3));
134   }
135
136   /**
137    * Tests for the method that prefixes and sanitises a feature name so it can
138    * be used as a valid, namespaced attribute name in Chimera
139    */
140   @Test(groups = { "Functional" })
141   public void testMakeAttributeName()
142   {
143     assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
144     assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
145     assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
146     assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
147             "jv_Hello_World_24");
148     assertEquals(
149             ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
150             "jv__this_is_a_very__odd_name");
151     // name ending in color gets underscore appended
152     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
153             "jv_helixColor_");
154   }
155
156   @Test(groups = "Functional")
157   public void testGetAtomSpec()
158   {
159     StructureCommandsI testee = new ChimeraCommands();
160     AtomSpecModel model = new AtomSpecModel();
161     assertEquals(testee.getAtomSpec(model, false), "");
162     model.addRange("1", 2, 4, "A");
163     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
164     model.addRange("1", 8, 8, "A");
165     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
166     model.addRange("1", 5, 7, "B");
167     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
168     model.addRange("1", 3, 5, "A");
169     assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
170     model.addRange("0", 1, 4, "B");
171     assertEquals(testee.getAtomSpec(model, false),
172             "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
173     model.addRange("0", 5, 9, "C");
174     assertEquals(testee.getAtomSpec(model, false),
175             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
176     model.addRange("1", 8, 10, "B");
177     assertEquals(testee.getAtomSpec(model, false),
178             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
179     model.addRange("1", 8, 9, "B");
180     assertEquals(testee.getAtomSpec(model, false),
181             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
182     model.addRange("0", 3, 10, "C"); // subsumes 5-9
183     assertEquals(testee.getAtomSpec(model, false),
184             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
185     model.addRange("5", 25, 35, " ");
186     assertEquals(testee.getAtomSpec(model, false),
187             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
188
189   }
190
191   @Test(groups = { "Functional" })
192   public void testSuperposeStructures()
193   {
194     StructureCommandsI testee = new ChimeraCommands();
195     AtomSpecModel ref = new AtomSpecModel();
196     ref.addRange("1", 12, 14, "A");
197     ref.addRange("1", 18, 18, "B");
198     ref.addRange("1", 22, 23, "B");
199     AtomSpecModel toAlign = new AtomSpecModel();
200     toAlign.addRange("2", 15, 17, "B");
201     toAlign.addRange("2", 20, 21, "B");
202     toAlign.addRange("2", 22, 22, "C");
203     List<StructureCommandI> command = testee.superposeStructures(ref,
204             toAlign);
205     // qualifier to restrict match to CA and no altlocs
206     String carbonAlphas = "@CA&~@.B-Z&~@.2-9";
207     String refSpec = "#1:12-14.A,18.B,22-23.B";
208     String toAlignSpec = "#2:15-17.B,20-21.B,22.C";
209     String expected = String.format(
210             "match %s%s %s%s; ribbon %s|%s; focus", toAlignSpec,
211             carbonAlphas, refSpec, carbonAlphas, toAlignSpec, refSpec);
212     assertEquals(command.get(0).getCommand(), expected);
213   }
214
215   @Test(groups = "Functional")
216   public void testGetAtomSpec_alphaOnly()
217   {
218     StructureCommandsI testee = new ChimeraCommands();
219     AtomSpecModel model = new AtomSpecModel();
220     assertEquals(testee.getAtomSpec(model, true), "");
221     model.addRange("1", 2, 4, "A");
222     assertEquals(testee.getAtomSpec(model, true),
223             "#1:2-4.A@CA&~@.B-Z&~@.2-9");
224     model.addRange("1", 8, 8, "A");
225     assertEquals(testee.getAtomSpec(model, true),
226             "#1:2-4.A,8.A@CA&~@.B-Z&~@.2-9");
227     model.addRange("1", 5, 7, "B");
228     assertEquals(testee.getAtomSpec(model, true),
229             "#1:2-4.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
230     model.addRange("1", 3, 5, "A");
231     assertEquals(testee.getAtomSpec(model, true),
232             "#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
233     model.addRange("0", 1, 4, "B");
234     assertEquals(testee.getAtomSpec(model, true),
235             "#0:1-4.B@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
236     model.addRange("0", 5, 9, "C");
237     assertEquals(testee.getAtomSpec(model, true),
238             "#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");
239     model.addRange("1", 8, 10, "B");
240     assertEquals(testee.getAtomSpec(model, true),
241             "#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");
242     model.addRange("1", 8, 9, "B");
243     assertEquals(testee.getAtomSpec(model, true),
244             "#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");
245     model.addRange("0", 3, 10, "C"); // subsumes 5-9
246     assertEquals(testee.getAtomSpec(model, true),
247             "#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");
248     model.addRange("5", 25, 35, " "); // empty chain code
249     assertEquals(testee.getAtomSpec(model, true),
250             "#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");
251   
252   }
253
254   @Test(groups = "Functional")
255   public void testGetModelStartNo()
256   {
257     StructureCommandsI testee = new ChimeraCommands();
258     assertEquals(testee.getModelStartNo(), 0);
259   }
260
261   @Test(groups = "Functional")
262   public void testGetResidueSpec()
263   {
264     ChimeraCommands testee = new ChimeraCommands();
265     assertEquals(testee.getResidueSpec("ALA"), "::ALA");
266   }
267
268   @Test(groups = "Functional")
269   public void testShowBackbone()
270   {
271     ChimeraCommands testee = new ChimeraCommands();
272     List<StructureCommandI> cmds = testee.showBackbone();
273     assertEquals(cmds.size(), 1);
274     assertEquals(cmds.get(0).getCommand(),
275             "~display all;~ribbon;chain @CA|P");
276   }
277
278   @Test(groups = "Functional")
279   public void testOpenCommandFile()
280   {
281     ChimeraCommands testee = new ChimeraCommands();
282     assertEquals(testee.openCommandFile("nowhere").getCommand(),
283             "open cmd:nowhere");
284   }
285
286   @Test(groups = "Functional")
287   public void testSaveSession()
288   {
289     ChimeraCommands testee = new ChimeraCommands();
290     assertEquals(testee.saveSession("somewhere").getCommand(),
291             "save somewhere");
292   }
293
294   @Test(groups = "Functional")
295   public void testGetColourCommand()
296   {
297     ChimeraCommands testee = new ChimeraCommands();
298     assertEquals(testee.getColourCommand("something", Color.MAGENTA)
299             .getCommand(),
300             "color #ff00ff something");
301   }
302
303   @Test(groups = "Functional")
304   public void testSetAttribute()
305   {
306     ChimeraCommands testee = new ChimeraCommands();
307     AtomSpecModel model = new AtomSpecModel();
308     model.addRange("1", 89, 92, "A");
309     model.addRange("2", 12, 20, "B");
310     model.addRange("2", 8, 9, "B");
311     assertEquals(testee.setAttribute("phi", "27.3", model).getCommand(),
312             "setattr res phi '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
313   }
314 }