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