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