JAL-3551 copy Jalview features to Pymol 'p' (with pull refactoring)
[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 or PyMol
139    */
140   @Test(groups = { "Functional" })
141   public void testMakeAttributeName()
142   {
143     ChimeraCommands testee = new ChimeraCommands();
144     assertEquals(testee.makeAttributeName(null), "jv_");
145     assertEquals(testee.makeAttributeName(""), "jv_");
146     assertEquals(testee.makeAttributeName("helix"), "jv_helix");
147     assertEquals(testee.makeAttributeName(
148             "Hello World 24"),
149             "jv_Hello_World_24");
150     assertEquals(
151             testee.makeAttributeName(
152                     "!this is-a_very*{odd(name"),
153             "jv__this_is_a_very__odd_name");
154     // name ending in color gets underscore appended
155     assertEquals(testee.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     // qualifier to restrict match to CA and no altlocs
209     String carbonAlphas = "@CA&~@.B-Z&~@.2-9";
210     String refSpec = "#1:12-14.A,18.B,22-23.B";
211     String toAlignSpec = "#2:15-17.B,20-21.B,22.C";
212     String expected = String.format(
213             "match %s%s %s%s; ribbon %s|%s; focus", toAlignSpec,
214             carbonAlphas, refSpec, carbonAlphas, toAlignSpec, refSpec);
215     assertEquals(command.get(0).getCommand(), expected);
216   }
217
218   @Test(groups = "Functional")
219   public void testGetAtomSpec_alphaOnly()
220   {
221     StructureCommandsI testee = new ChimeraCommands();
222     AtomSpecModel model = new AtomSpecModel();
223     assertEquals(testee.getAtomSpec(model, true), "");
224     model.addRange("1", 2, 4, "A");
225     assertEquals(testee.getAtomSpec(model, true),
226             "#1:2-4.A@CA&~@.B-Z&~@.2-9");
227     model.addRange("1", 8, 8, "A");
228     assertEquals(testee.getAtomSpec(model, true),
229             "#1:2-4.A,8.A@CA&~@.B-Z&~@.2-9");
230     model.addRange("1", 5, 7, "B");
231     assertEquals(testee.getAtomSpec(model, true),
232             "#1:2-4.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
233     model.addRange("1", 3, 5, "A");
234     assertEquals(testee.getAtomSpec(model, true),
235             "#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
236     model.addRange("0", 1, 4, "B");
237     assertEquals(testee.getAtomSpec(model, true),
238             "#0:1-4.B@CA&~@.B-Z&~@.2-9|#1:2-5.A,8.A,5-7.B@CA&~@.B-Z&~@.2-9");
239     model.addRange("0", 5, 9, "C");
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-7.B@CA&~@.B-Z&~@.2-9");
242     model.addRange("1", 8, 10, "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("1", 8, 9, "B");
246     assertEquals(testee.getAtomSpec(model, true),
247             "#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");
248     model.addRange("0", 3, 10, "C"); // subsumes 5-9
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");
251     model.addRange("5", 25, 35, " "); // empty chain code
252     assertEquals(testee.getAtomSpec(model, true),
253             "#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");
254   
255   }
256
257   @Test(groups = "Functional")
258   public void testGetModelStartNo()
259   {
260     StructureCommandsI testee = new ChimeraCommands();
261     assertEquals(testee.getModelStartNo(), 0);
262   }
263
264   @Test(groups = "Functional")
265   public void testGetResidueSpec()
266   {
267     ChimeraCommands testee = new ChimeraCommands();
268     assertEquals(testee.getResidueSpec("ALA"), "::ALA");
269   }
270
271   @Test(groups = "Functional")
272   public void testShowBackbone()
273   {
274     ChimeraCommands testee = new ChimeraCommands();
275     List<StructureCommandI> cmds = testee.showBackbone();
276     assertEquals(cmds.size(), 1);
277     assertEquals(cmds.get(0).getCommand(),
278             "~display all;~ribbon;chain @CA|P");
279   }
280
281   @Test(groups = "Functional")
282   public void testOpenCommandFile()
283   {
284     ChimeraCommands testee = new ChimeraCommands();
285     assertEquals(testee.openCommandFile("nowhere").getCommand(),
286             "open cmd:nowhere");
287   }
288
289   @Test(groups = "Functional")
290   public void testSaveSession()
291   {
292     ChimeraCommands testee = new ChimeraCommands();
293     assertEquals(testee.saveSession("somewhere").getCommand(),
294             "save somewhere");
295   }
296
297   @Test(groups = "Functional")
298   public void testGetColourCommand()
299   {
300     ChimeraCommands testee = new ChimeraCommands();
301     assertEquals(testee.colourResidues("something", Color.MAGENTA)
302             .getCommand(),
303             "color #ff00ff something");
304   }
305
306   @Test(groups = "Functional")
307   public void testSetAttribute()
308   {
309     ChimeraCommands testee = new ChimeraCommands();
310     AtomSpecModel model = new AtomSpecModel();
311     model.addRange("1", 89, 92, "A");
312     model.addRange("2", 12, 20, "B");
313     model.addRange("2", 8, 9, "B");
314     assertEquals(testee.setAttribute("phi", "27.3", model).getCommand(),
315             "setattr res phi '27.3' #1:89-92.A|#2:8-9.B,12-20.B");
316   }
317 }