JAL-3551 working proof of concept of Jalview driving PyMOL
[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.StructureCommandI;
28 import jalview.structure.StructureCommandsI;
29
30 import java.awt.Color;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.testng.annotations.Test;
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(), 3);
61     assertEquals(commands.get(0).getCommand(),
62             "color #0000ff #0:2-5.A,9-23.A,7.B|#1:1.A,4-7.B");
63     assertEquals(commands.get(1).getCommand(),
64             "color #ffff00 #1:3-5.A,8.A");
65     assertEquals(commands.get(2).getCommand(), "color #ff0000 #0:3-9.A");
66   }
67
68   @Test(groups = { "Functional" })
69   public void testSetAttributes()
70   {
71     /*
72      * make a map of { featureType, {featureValue, {residue range specification } } }
73      */
74     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
75     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
76     
77     /*
78      * start with just one feature/value...
79      */
80     featuresMap.put("chain", featureValues);
81     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
82   
83     ChimeraCommands commandGenerator = new ChimeraCommands();
84     List<StructureCommandI> commands = commandGenerator
85             .setAttributes(featuresMap);
86     assertEquals(1, commands.size());
87
88     /*
89      * feature name gets a jv_ namespace prefix
90      * feature value is quoted in case it contains spaces
91      */
92     assertEquals(commands.get(0).getCommand(),
93             "setattr res jv_chain 'X' #0:8-20.A");
94
95     // add same feature value, overlapping range
96     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
97     // same feature value, contiguous range
98     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
99     commands = commandGenerator.setAttributes(featuresMap);
100     assertEquals(1, commands.size());
101     assertEquals(commands.get(0).getCommand(),
102             "setattr res jv_chain 'X' #0:3-25.A");
103
104     // same feature value and model, different chain
105     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
106     // same feature value and chain, different model
107     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
108     commands = commandGenerator.setAttributes(featuresMap);
109     assertEquals(1, commands.size());
110     String expected1 = "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A";
111     assertEquals(commands.get(0).getCommand(), expected1);
112
113     // same feature, different value
114     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
115     commands = commandGenerator.setAttributes(featuresMap);
116     assertEquals(2, commands.size());
117     // commands are ordered by feature type but not by value
118     // so test for the expected command in either order
119     String cmd1 = commands.get(0).getCommand();
120     String cmd2 = commands.get(1).getCommand();
121     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
122     String expected2 = "setattr res jv_chain 'Y' #0:40-50.A";
123     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
124
125     featuresMap.clear();
126     featureValues.clear();
127     featuresMap.put("side-chain binding!", featureValues);
128     ChimeraCommands.addAtomSpecRange(featureValues,
129             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15,
130             "A");
131     // feature names are sanitised to change non-alphanumeric to underscore
132     // feature values are sanitised to encode single quote characters
133     commands = commandGenerator.setAttributes(featuresMap);
134     assertEquals(commands.size(), 1);
135     String expected3 = "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A";
136     assertTrue(commands.get(0).getCommand().equals(expected3));
137   }
138
139   /**
140    * Tests for the method that prefixes and sanitises a feature name so it can
141    * be used as a valid, namespaced attribute name in Chimera
142    */
143   @Test(groups = { "Functional" })
144   public void testMakeAttributeName()
145   {
146     assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
147     assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
148     assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
149     assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
150             "jv_Hello_World_24");
151     assertEquals(
152             ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
153             "jv__this_is_a_very__odd_name");
154     // name ending in color gets underscore appended
155     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
156             "jv_helixColor_");
157   }
158
159   @Test(groups = "Functional")
160   public void testGetAtomSpec()
161   {
162     StructureCommandsI testee = new ChimeraCommands();
163     AtomSpecModel model = new AtomSpecModel();
164     assertEquals(testee.getAtomSpec(model, false), "");
165     model.addRange("1", 2, 4, "A");
166     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
167     model.addRange("1", 8, 8, "A");
168     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
169     model.addRange("1", 5, 7, "B");
170     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
171     model.addRange("1", 3, 5, "A");
172     assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
173     model.addRange("0", 1, 4, "B");
174     assertEquals(testee.getAtomSpec(model, false),
175             "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
176     model.addRange("0", 5, 9, "C");
177     assertEquals(testee.getAtomSpec(model, false),
178             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
179     model.addRange("1", 8, 10, "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("1", 8, 9, "B");
183     assertEquals(testee.getAtomSpec(model, false),
184             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
185     model.addRange("0", 3, 10, "C"); // subsumes 5-9
186     assertEquals(testee.getAtomSpec(model, false),
187             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
188     model.addRange("5", 25, 35, " ");
189     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   @Test(groups = { "Functional" })
195   public void testSuperposeStructures()
196   {
197     StructureCommandsI testee = new ChimeraCommands();
198     AtomSpecModel ref = new AtomSpecModel();
199     ref.addRange("1", 12, 14, "A");
200     ref.addRange("1", 18, 18, "B");
201     ref.addRange("1", 22, 23, "B");
202     AtomSpecModel toAlign = new AtomSpecModel();
203     toAlign.addRange("2", 15, 17, "B");
204     toAlign.addRange("2", 20, 21, "B");
205     toAlign.addRange("2", 22, 22, "C");
206     List<StructureCommandI> command = testee.superposeStructures(ref,
207             toAlign);
208     String refSpec = "#1:12-14.A,18.B,22-23.B@CA|P&~@.B-Z&~@.2-9";
209     String toAlignSpec = "#2:15-17.B,20-21.B,22.C@CA|P&~@.B-Z&~@.2-9";
210     String expected = String.format(
211             "match %s %s; ribbon %s|%s; focus",
212             refSpec, toAlignSpec, refSpec, toAlignSpec);
213     assertEquals(command.get(0).getCommand(), expected);
214   }
215
216   @Test(groups = "Functional")
217   public void testGetAtomSpec_alphaOnly()
218   {
219     StructureCommandsI testee = new ChimeraCommands();
220     AtomSpecModel model = new AtomSpecModel();
221     assertEquals(testee.getAtomSpec(model, true), "");
222     model.addRange("1", 2, 4, "A");
223     assertEquals(testee.getAtomSpec(model, true),
224             "#1:2-4.A@CA|P&~@.B-Z&~@.2-9");
225     model.addRange("1", 8, 8, "A");
226     assertEquals(testee.getAtomSpec(model, true),
227             "#1:2-4.A,8.A@CA|P&~@.B-Z&~@.2-9");
228     model.addRange("1", 5, 7, "B");
229     assertEquals(testee.getAtomSpec(model, true),
230             "#1:2-4.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
231     model.addRange("1", 3, 5, "A");
232     assertEquals(testee.getAtomSpec(model, true),
233             "#1:2-5.A,8.A,5-7.B@CA|P&~@.B-Z&~@.2-9");
234     model.addRange("0", 1, 4, "B");
235     assertEquals(testee.getAtomSpec(model, true),
236             "#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");
237     model.addRange("0", 5, 9, "C");
238     assertEquals(testee.getAtomSpec(model, true),
239             "#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");
240     model.addRange("1", 8, 10, "B");
241     assertEquals(testee.getAtomSpec(model, true),
242             "#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");
243     model.addRange("1", 8, 9, "B");
244     assertEquals(testee.getAtomSpec(model, true),
245             "#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");
246     model.addRange("0", 3, 10, "C"); // subsumes 5-9
247     assertEquals(testee.getAtomSpec(model, true),
248             "#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");
249     model.addRange("5", 25, 35, " "); // empty chain code
250     assertEquals(testee.getAtomSpec(model, true),
251             "#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");
252   
253   }
254
255   @Test(groups = "Functional")
256   public void testGetModelStartNo()
257   {
258     StructureCommandsI testee = new ChimeraCommands();
259     assertEquals(testee.getModelStartNo(), 0);
260   }
261
262   @Test(groups = "Functional")
263   public void testGetResidueSpec()
264   {
265     ChimeraCommands testee = new ChimeraCommands();
266     assertEquals(testee.getResidueSpec("ALA"), "::ALA");
267   }
268
269   @Test(groups = "Functional")
270   public void testShowBackbone()
271   {
272     ChimeraCommands testee = new ChimeraCommands();
273     List<StructureCommandI> cmds = testee.showBackbone();
274     assertEquals(cmds.size(), 1);
275     assertEquals(cmds.get(0).getCommand(), "~display all;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 }