7759724698004fdaf3587bb1cc36bd9d9a9d75a7
[jalview.git] / test / jalview / ext / pymol / PymolCommandsTest.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.pymol;
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.BeforeClass;
33 import org.testng.annotations.Test;
34
35 import jalview.ext.rbvi.chimera.ChimeraCommands;
36 import jalview.structure.AtomSpecModel;
37 import jalview.structure.StructureCommand;
38 import jalview.structure.StructureCommandI;
39 import jalview.structure.StructureCommandsI.AtomSpecType;
40
41 public class PymolCommandsTest
42 {
43   private PymolCommands testee;
44
45   @BeforeClass(alwaysRun = true)
46   public void setUp()
47   {
48     testee = new PymolCommands();
49   }
50
51   @Test(groups = { "Functional" })
52   public void testColourBySequence()
53   {
54
55     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
56     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
57     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
58     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
59     PymolCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
60     PymolCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
61     PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
62     PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
63     PymolCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
64     PymolCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
65
66     // Colours should appear in the Pymol command in the order in which
67     // they were added; within colour, by model, by chain, ranges in start order
68     List<StructureCommandI> commands = testee.colourBySequence(map);
69     assertEquals(commands.size(), 3);
70     assertEquals(commands.get(0), new StructureCommand("color", "0x0000ff",
71             "0//A/2-5+9-23/ 0//B/7/ 1//A/1/ 1//B/4-7/"));
72     assertEquals(commands.get(
73             1),
74             new StructureCommand("color", "0xffff00", "1//A/3-5+8/"));
75     assertEquals(commands.get(
76             2),
77             new StructureCommand("color", "0xff0000", "0//A/3-9/"));
78   }
79
80   @Test(groups = "Functional")
81   public void testGetAtomSpec()
82   {
83     AtomSpecModel model = new AtomSpecModel();
84     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "");
85     model.addRange("1", 2, 4, "A");
86     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "1//A/2-4/");
87     model.addRange("1", 8, 8, "A");
88     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "1//A/2-4+8/");
89     model.addRange("1", 5, 7, "B");
90     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "1//A/2-4+8/ 1//B/5-7/");
91     model.addRange("1", 3, 5, "A");
92     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY), "1//A/2-5+8/ 1//B/5-7/");
93     model.addRange("0", 1, 4, "B");
94     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
95             "0//B/1-4/ 1//A/2-5+8/ 1//B/5-7/");
96     model.addRange("0", 5, 9, "C");
97     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
98             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-7/");
99     model.addRange("1", 8, 10, "B");
100     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
101             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
102     model.addRange("1", 8, 9, "B");
103     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
104             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
105     model.addRange("0", 3, 10, "C"); // subsumes 5-9
106     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
107             "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/");
108     model.addRange("5", 25, 35, " ");
109     assertEquals(testee.getAtomSpec(model, AtomSpecType.RESIDUE_ONLY),
110             "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/ 5///25-35/");
111
112   }
113
114   @Test(groups = { "Functional" })
115   public void testSuperposeStructures()
116   {
117     AtomSpecModel ref = new AtomSpecModel();
118     ref.addRange("1", 12, 14, "A");
119     ref.addRange("1", 18, 18, "B");
120     ref.addRange("1", 22, 23, "B");
121     AtomSpecModel toAlign = new AtomSpecModel();
122     toAlign.addRange("2", 15, 17, "B");
123     toAlign.addRange("2", 20, 21, "B");
124     toAlign.addRange("2", 22, 22, "C");
125     List<StructureCommandI> commands = testee.superposeStructures(ref,
126             toAlign, AtomSpecType.ALPHA);
127     assertEquals(commands.size(), 2);
128     String refSpecCA = "(1//A/12-14/CA 1//B/18+22-23/CA";
129     String toAlignSpecCA = "(2//B/15-17+20-21/CA 2//C/22/CA";
130     String refSpec = "1//A/12-14/ 1//B/18+22-23/";
131     String toAlignSpec = "2//B/15-17+20-21/ 2//C/22/";
132     String altLoc =  " and (altloc '' or altloc 'a'))";
133     // super command: separate arguments for regions to align
134     assertEquals(commands.get(0),
135             new StructureCommand("pair_fit", toAlignSpecCA+altLoc, refSpecCA+altLoc));
136     // show aligned regions: one argument for combined atom specs
137     assertEquals(commands.get(1), new StructureCommand("show", "cartoon",
138             refSpec + " " + toAlignSpec));
139   }
140
141   @Test(groups = "Functional")
142   public void testGetAtomSpec_alphaOnly()
143   {
144     AtomSpecModel model = new AtomSpecModel();
145     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA), "");
146     model.addRange("1", 2, 4, "A");
147     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA), "1//A/2-4/CA");
148     model.addRange("1", 8, 8, "A");
149     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA), "1//A/2-4+8/CA");
150     model.addRange("1", 5, 7, "B");
151     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
152             "1//A/2-4+8/CA 1//B/5-7/CA");
153     model.addRange("1", 3, 5, "A");
154     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
155             "1//A/2-5+8/CA 1//B/5-7/CA");
156     model.addRange("0", 1, 4, "B");
157     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
158             "0//B/1-4/CA 1//A/2-5+8/CA 1//B/5-7/CA");
159     model.addRange("0", 5, 9, "C");
160     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
161             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-7/CA");
162     model.addRange("1", 8, 10, "B");
163     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
164             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
165     model.addRange("1", 8, 9, "B");
166     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
167             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
168     model.addRange("0", 3, 10, "C"); // subsumes 5-9
169     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
170             "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA");
171     model.addRange("5", 25, 35, " ");
172     assertEquals(testee.getAtomSpec(model, AtomSpecType.ALPHA),
173             "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA 5///25-35/CA");
174   }
175
176   @Test(groups = "Functional")
177   public void testGetModelStartNo()
178   {
179     assertEquals(testee.getModelStartNo(), 0);
180   }
181
182   @Test(groups = "Functional")
183   public void testGetResidueSpec()
184   {
185     assertEquals(testee.getResidueSpec("ALA"), "resn ALA");
186   }
187
188   @Test(groups = "Functional")
189   public void testShowBackbone()
190   {
191     List<StructureCommandI> cmds = testee.showBackbone();
192     assertEquals(cmds.size(), 2);
193     assertEquals(cmds.get(0), new StructureCommand("hide", "everything"));
194     assertEquals(cmds.get(1), new StructureCommand("show", "ribbon"));
195   }
196
197   @Test(groups = "Functional")
198   public void testColourByCharge()
199   {
200     List<StructureCommandI> cmds = testee.colourByCharge();
201     assertEquals(cmds.size(), 4);
202     assertEquals(cmds.get(0), new StructureCommand("color", "white", "*"));
203     assertEquals(cmds.get(1),
204             new StructureCommand("color", "red", "resn ASP resn GLU"));
205     assertEquals(cmds.get(2),
206             new StructureCommand("color", "blue", "resn LYS resn ARG"));
207     assertEquals(cmds.get(3),
208             new StructureCommand("color", "yellow", "resn CYS"));
209   }
210
211   @Test(groups = "Functional")
212   public void testOpenCommandFile()
213   {
214     assertEquals(testee.openCommandFile("commands.pml"),
215             new StructureCommand("run", "commands.pml"));
216   }
217
218   @Test(groups = "Functional")
219   public void testSaveSession()
220   {
221     assertEquals(testee.saveSession("somewhere.pse"),
222             new StructureCommand("save", "somewhere.pse"));
223   }
224
225   @Test(groups = "Functional")
226   public void testOpenSession()
227   {
228     assertEquals(testee.openSession("/some/path"),
229             new StructureCommand("load", "/some/path", "", "0", "pse"));
230   }
231
232   @Test(groups = "Functional")
233   public void testColourByChain()
234   {
235     assertEquals(testee.colourByChain(),
236             new StructureCommand("spectrum", "chain"));
237   }
238
239   @Test(groups = "Functional")
240   public void testColourResidues()
241   {
242     assertEquals(testee.colourResidues("something",
243             Color.MAGENTA),
244             new StructureCommand("color", "0xff00ff", "something"));
245   }
246
247   @Test(groups = "Functional")
248   public void testLoadFile()
249   {
250     assertEquals(testee.loadFile("/some/path"),
251             new StructureCommand("load", "/some/path"));
252   }
253
254   @Test(groups = "Functional")
255   public void testSetBackgroundColour()
256   {
257     assertEquals(testee.setBackgroundColour(
258             Color.PINK),
259             new StructureCommand("bg_color", "0xffafaf"));
260   }
261
262   @Test(groups = "Functional")
263   public void testSetAttribute()
264   {
265     AtomSpecModel model = new AtomSpecModel();
266     model.addRange("1", 89, 92, "A");
267     model.addRange("2", 12, 20, "B");
268     model.addRange("2", 8, 9, "B");
269     assertEquals(testee.setAttribute("jv_kd", "27.3", model),
270             new StructureCommand("iterate", "1//A/89-92/ 2//B/8-9+12-20/",
271                     "p.jv_kd='27.3'"));
272   }
273
274   @Test(groups = { "Functional" })
275   public void testSetAttributes()
276   {
277     /*
278      * make a map of { featureType, {featureValue, {residue range specification } } }
279      */
280     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
281     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
282
283     /*
284      * start with just one feature/value...
285      */
286     featuresMap.put("chain", featureValues);
287     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 8, 20, "A");
288
289     List<StructureCommandI> commands = testee.setAttributes(featuresMap);
290     assertEquals(commands.size(), 1);
291
292     /*
293      * feature name gets a jv_ namespace prefix
294      */
295     assertEquals(commands.get(0), new StructureCommand("iterate",
296             "0//A/8-20/", "p.jv_chain='X'"));
297
298     // add same feature value, overlapping range
299     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 3, 9, "A");
300     // same feature value, contiguous range
301     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "A");
302     commands = testee.setAttributes(featuresMap);
303     assertEquals(commands.size(), 1);
304     assertEquals(commands.get(0), new StructureCommand("iterate",
305             "0//A/3-25/", "p.jv_chain='X'"));
306
307     // same feature value and model, different chain
308     ChimeraCommands.addAtomSpecRange(featureValues, "X", "0", 21, 25, "B");
309     // same feature value and chain, different model
310     ChimeraCommands.addAtomSpecRange(featureValues, "X", "1", 26, 30, "A");
311     commands = testee.setAttributes(featuresMap);
312     assertEquals(commands.size(), 1);
313     StructureCommand expected1 = new StructureCommand("iterate",
314             "0//A/3-25/ 0//B/21-25/ 1//A/26-30/", "p.jv_chain='X'");
315     assertEquals(commands.get(0), expected1);
316
317     // same feature, different value
318     ChimeraCommands.addAtomSpecRange(featureValues, "Y", "0", 40, 50, "A");
319     commands = testee.setAttributes(featuresMap);
320     assertEquals(2, commands.size());
321     // commands are ordered by feature type but not by value
322     // so test for the expected command in either order
323     StructureCommandI cmd1 = commands.get(0);
324     StructureCommandI cmd2 = commands.get(1);
325     StructureCommand expected2 = new StructureCommand("iterate",
326             "0//A/40-50/", "p.jv_chain='Y'");
327     assertTrue(cmd1.equals(expected1) || cmd2.equals(expected1));
328     // String expected2 = "setattr #0/A:40-50 res jv_chain 'Y' create true";
329     assertTrue(cmd1.equals(expected2) || cmd2.equals(expected2));
330
331     featuresMap.clear();
332     featureValues.clear();
333     featuresMap.put("side-chain binding!", featureValues);
334     ChimeraCommands.addAtomSpecRange(featureValues,
335             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", "0", 7, 15, "A");
336     // feature names are sanitised to change non-alphanumeric to underscore
337     // feature values are sanitised to encode single quote characters
338     commands = testee.setAttributes(featuresMap);
339     assertEquals(commands.size(), 1);
340     StructureCommandI expected3 = new StructureCommand("iterate",
341             "0//A/7-15/",
342             "p.jv_side_chain_binding_='<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!'");
343     assertEquals(commands.get(0), expected3);
344   }
345
346   @Test(groups = "Functional")
347   public void testCloseViewer()
348   {
349     assertEquals(testee.closeViewer(), new StructureCommand("quit"));
350   }
351 }