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