JAL-2422 JAL-3551 unit tests updated for code changes
[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
25 import java.awt.Color;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.testng.annotations.Test;
31
32 import jalview.structure.AtomSpecModel;
33 import jalview.structure.StructureCommandI;
34 import jalview.structure.StructureCommandsI;
35
36 public class PymolCommandsTest
37 {
38
39   @Test(groups = { "Functional" })
40   public void testColourBySequence()
41   {
42
43     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
44     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 2, 5, "A");
45     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 7, 7, "B");
46     PymolCommands.addAtomSpecRange(map, Color.blue, "0", 9, 23, "A");
47     PymolCommands.addAtomSpecRange(map, Color.blue, "1", 1, 1, "A");
48     PymolCommands.addAtomSpecRange(map, Color.blue, "1", 4, 7, "B");
49     PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 8, 8, "A");
50     PymolCommands.addAtomSpecRange(map, Color.yellow, "1", 3, 5, "A");
51     PymolCommands.addAtomSpecRange(map, Color.red, "0", 3, 5, "A");
52     PymolCommands.addAtomSpecRange(map, Color.red, "0", 6, 9, "A");
53
54     // Colours should appear in the Pymol command in the order in which
55     // they were added; within colour, by model, by chain, ranges in start order
56     List<StructureCommandI> commands = new PymolCommands()
57             .colourBySequence(map);
58     assertEquals(commands.size(), 3);
59     assertEquals(commands.get(0).toString(),
60             "color(0x0000ff,0//A/2-5+9-23/ 0//B/7/ 1//A/1/ 1//B/4-7/)");
61     assertEquals(commands.get(1).toString(), "color(0xffff00,1//A/3-5+8/)");
62     assertEquals(
63             commands.get(2).toString(), "color(0xff0000,0//A/3-9/)");
64   }
65
66   @Test(groups = "Functional")
67   public void testGetAtomSpec()
68   {
69     StructureCommandsI testee = new PymolCommands();
70     AtomSpecModel model = new AtomSpecModel();
71     assertEquals(testee.getAtomSpec(model, false), "");
72     model.addRange("1", 2, 4, "A");
73     assertEquals(testee.getAtomSpec(model, false), "1//A/2-4/");
74     model.addRange("1", 8, 8, "A");
75     assertEquals(testee.getAtomSpec(model, false), "1//A/2-4+8/");
76     model.addRange("1", 5, 7, "B");
77     assertEquals(testee.getAtomSpec(model, false), "1//A/2-4+8/ 1//B/5-7/");
78     model.addRange("1", 3, 5, "A");
79     assertEquals(testee.getAtomSpec(model, false), "1//A/2-5+8/ 1//B/5-7/");
80     model.addRange("0", 1, 4, "B");
81     assertEquals(testee.getAtomSpec(model, false),
82             "0//B/1-4/ 1//A/2-5+8/ 1//B/5-7/");
83     model.addRange("0", 5, 9, "C");
84     assertEquals(testee.getAtomSpec(model, false),
85             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-7/");
86     model.addRange("1", 8, 10, "B");
87     assertEquals(testee.getAtomSpec(model, false),
88             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
89     model.addRange("1", 8, 9, "B");
90     assertEquals(testee.getAtomSpec(model, false),
91             "0//B/1-4/ 0//C/5-9/ 1//A/2-5+8/ 1//B/5-10/");
92     model.addRange("0", 3, 10, "C"); // subsumes 5-9
93     assertEquals(testee.getAtomSpec(model, false),
94             "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/");
95     model.addRange("5", 25, 35, " ");
96     assertEquals(testee.getAtomSpec(model, false),
97             "0//B/1-4/ 0//C/3-10/ 1//A/2-5+8/ 1//B/5-10/ 5///25-35/");
98
99   }
100
101   @Test(groups = { "Functional" })
102   public void testSuperposeStructures()
103   {
104     StructureCommandsI testee = new PymolCommands();
105     AtomSpecModel ref = new AtomSpecModel();
106     ref.addRange("1", 12, 14, "A");
107     ref.addRange("1", 18, 18, "B");
108     ref.addRange("1", 22, 23, "B");
109     AtomSpecModel toAlign = new AtomSpecModel();
110     toAlign.addRange("2", 15, 17, "B");
111     toAlign.addRange("2", 20, 21, "B");
112     toAlign.addRange("2", 22, 22, "C");
113     List<StructureCommandI> commands = testee.superposeStructures(ref,
114             toAlign);
115     assertEquals(commands.size(), 2);
116     String refSpecCA = "1//A/12-14/CA 1//B/18+22-23/CA";
117     String toAlignSpecCA = "2//B/15-17+20-21/CA 2//C/22/CA";
118     String refSpec = "1//A/12-14/ 1//B/18+22-23/";
119     String toAlignSpec = "2//B/15-17+20-21/ 2//C/22/";
120     String expected1 = String.format("super(%s,%s)", refSpecCA,
121             toAlignSpecCA);
122     String expected2 = String.format("show(cartoon,%s %s)", refSpec,
123             toAlignSpec);
124     assertEquals(commands.get(0).toString(), expected1);
125     assertEquals(commands.get(1).toString(), expected2);
126   }
127
128   @Test(groups = "Functional")
129   public void testGetAtomSpec_alphaOnly()
130   {
131     StructureCommandsI testee = new PymolCommands();
132     AtomSpecModel model = new AtomSpecModel();
133     assertEquals(testee.getAtomSpec(model, true), "");
134     model.addRange("1", 2, 4, "A");
135     assertEquals(testee.getAtomSpec(model, true), "1//A/2-4/CA");
136     model.addRange("1", 8, 8, "A");
137     assertEquals(testee.getAtomSpec(model, true), "1//A/2-4+8/CA");
138     model.addRange("1", 5, 7, "B");
139     assertEquals(testee.getAtomSpec(model, true),
140             "1//A/2-4+8/CA 1//B/5-7/CA");
141     model.addRange("1", 3, 5, "A");
142     assertEquals(testee.getAtomSpec(model, true),
143             "1//A/2-5+8/CA 1//B/5-7/CA");
144     model.addRange("0", 1, 4, "B");
145     assertEquals(testee.getAtomSpec(model, true),
146             "0//B/1-4/CA 1//A/2-5+8/CA 1//B/5-7/CA");
147     model.addRange("0", 5, 9, "C");
148     assertEquals(testee.getAtomSpec(model, true),
149             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-7/CA");
150     model.addRange("1", 8, 10, "B");
151     assertEquals(testee.getAtomSpec(model, true),
152             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
153     model.addRange("1", 8, 9, "B");
154     assertEquals(testee.getAtomSpec(model, true),
155             "0//B/1-4/CA 0//C/5-9/CA 1//A/2-5+8/CA 1//B/5-10/CA");
156     model.addRange("0", 3, 10, "C"); // subsumes 5-9
157     assertEquals(testee.getAtomSpec(model, true),
158             "0//B/1-4/CA 0//C/3-10/CA 1//A/2-5+8/CA 1//B/5-10/CA");
159     model.addRange("5", 25, 35, " ");
160     assertEquals(testee.getAtomSpec(model, true),
161             "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");
162   }
163
164   @Test(groups = "Functional")
165   public void testGetModelStartNo()
166   {
167     StructureCommandsI testee = new PymolCommands();
168     assertEquals(testee.getModelStartNo(), 0);
169   }
170
171   @Test(groups = "Functional")
172   public void testGetResidueSpec()
173   {
174     PymolCommands testee = new PymolCommands();
175     assertEquals(testee.getResidueSpec("ALA"), "resn ALA");
176   }
177
178   @Test(groups = "Functional")
179   public void testShowBackbone()
180   {
181     PymolCommands testee = new PymolCommands();
182     List<StructureCommandI> cmds = testee.showBackbone();
183     assertEquals(cmds.size(), 2);
184     assertEquals(cmds.get(0).toString(), "hide(everything)");
185     assertEquals(cmds.get(1).toString(), "show(ribbon)");
186   }
187
188   @Test(groups = "Functional")
189   public void testColourByCharge()
190   {
191     PymolCommands testee = new PymolCommands();
192     List<StructureCommandI> cmds = testee.colourByCharge();
193     assertEquals(cmds.size(), 4);
194     assertEquals(cmds.get(0).toString(), "color(white,*)");
195     assertEquals(cmds.get(1).toString(), "color(red,resn ASP resn GLU)");
196     assertEquals(cmds.get(2).toString(), "color(blue,resn LYS resn ARG)");
197     assertEquals(cmds.get(3).toString(), "color(yellow,resn CYS)");
198   }
199
200   @Test(groups = "Functional")
201   public void testOpenCommandFile()
202   {
203     PymolCommands testee = new PymolCommands();
204     assertEquals(testee.openCommandFile("commands.pml").toString(),
205             "run(commands.pml)");
206   }
207
208   @Test(groups = "Functional")
209   public void testSaveSession()
210   {
211     PymolCommands testee = new PymolCommands();
212     assertEquals(testee.saveSession("somewhere.pse").toString(),
213             "save(somewhere.pse)");
214   }
215
216   @Test(groups = "Functional")
217   public void testColourByChain()
218   {
219     PymolCommands testee = new PymolCommands();
220     assertEquals(testee.colourByChain().toString(), "spectrum(chain)");
221   }
222
223   @Test(groups = "Functional")
224   public void testGetColourCommand()
225   {
226     PymolCommands testee = new PymolCommands();
227     assertEquals(
228             testee.getColourCommand("something", Color.MAGENTA).toString(),
229             "color(0xff00ff,something)");
230   }
231 }