c0bb62996d6d8e7d29cd713006e77eed0c6e4a33
[jalview.git] / test / jalview / ext / jmol / JmolCommandsTest.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.jmol;
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.datamodel.Alignment;
36 import jalview.datamodel.AlignmentI;
37 import jalview.datamodel.ColumnSelection;
38 import jalview.datamodel.Sequence;
39 import jalview.datamodel.SequenceI;
40 import jalview.gui.AlignFrame;
41 import jalview.gui.SequenceRenderer;
42 import jalview.schemes.JalviewColourScheme;
43 import jalview.structure.AtomSpecModel;
44 import jalview.structure.StructureCommandI;
45 import jalview.structure.StructureMapping;
46 import jalview.structure.StructureSelectionManager;
47
48 public class JmolCommandsTest
49 {
50   private JmolCommands testee;
51
52   @BeforeClass(alwaysRun = true)
53   public void setUp()
54   {
55     testee = new JmolCommands();
56   }
57
58   /**
59    * Test for the now deprecated version of getColourBySequenceCommand
60    */
61   @Test(groups = { "Functional" }, enabled = false)
62   public void testGetColourBySequenceCommands_hiddenColumns()
63   {
64     /*
65      * load these sequences, coloured by Strand propensity,
66      * with columns 2-4 hidden
67      */
68     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
69     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
70     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
71     AlignFrame af = new AlignFrame(al, 800, 500);
72     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
73     ColumnSelection cs = new ColumnSelection();
74     cs.addElement(2);
75     cs.addElement(3);
76     cs.addElement(4);
77     af.getViewport().setColumnSelection(cs);
78     af.hideSelColumns_actionPerformed(null);
79     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
80     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
81     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
82     StructureSelectionManager ssm = new StructureSelectionManager();
83
84     /*
85      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
86      */
87     HashMap<Integer, int[]> map = new HashMap<>();
88     for (int pos = 1; pos <= seq1.getLength(); pos++)
89     {
90       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
91     }
92     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
93             "A", map, null);
94     ssm.addStructureMapping(sm1);
95     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
96             "B", map, null);
97     ssm.addStructureMapping(sm2);
98
99     String[] commands = testee.colourBySequence(ssm, files, seqs,
100             sr,
101             af.alignPanel);
102     assertEquals(commands.length, 2);
103
104     String chainACommand = commands[0];
105     // M colour is #82827d == (130, 130, 125) (see strand.html help page)
106     assertTrue(
107             chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first one
108     // H colour is #60609f == (96, 96, 159)
109     assertTrue(chainACommand.contains(";select 22:A/1.1;color[96,96,159]"));
110     // hidden columns are Gray (128, 128, 128)
111     assertTrue(chainACommand
112             .contains(";select 23-25:A/1.1;color[128,128,128]"));
113     // S and G are both coloured #4949b6 == (73, 73, 182)
114     assertTrue(
115             chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
116
117     String chainBCommand = commands[1];
118     // M colour is #82827d == (130, 130, 125)
119     assertTrue(
120             chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
121     // V colour is #ffff00 == (255, 255, 0)
122     assertTrue(chainBCommand.contains(";select 22:B/2.1;color[255,255,0]"));
123     // hidden columns are Gray (128, 128, 128)
124     assertTrue(chainBCommand
125             .contains(";select 23-25:B/2.1;color[128,128,128]"));
126     // S and G are both coloured #4949b6 == (73, 73, 182)
127     assertTrue(
128             chainBCommand.contains(";select 26-30:B/2.1;color[73,73,182]"));
129   }
130
131   @Test(groups = { "Functional" })
132   public void testGetAtomSpec()
133   {
134     AtomSpecModel model = new AtomSpecModel();
135     assertEquals(testee.getAtomSpec(model, false), "");
136
137     /*
138      * Jalview numbers models from 0, Jmol from 1
139      */
140     model.addRange("2", 2, 4, "A");
141     assertEquals(testee.getAtomSpec(model, false), "2-4:A/2.1");
142
143     model.addRange("2", 8, 8, "A");
144     assertEquals(testee.getAtomSpec(model, false), "(2-4,8)&:A/2.1");
145
146     model.addRange("2", 5, 7, "B");
147     assertEquals(testee.getAtomSpec(model,
148             false),
149             "(2-4,8)&:A/2.1,5-7:B/2.1");
150
151     model.addRange("2", 3, 5, "A");
152     // 3-5 merges with 2-4 to make 2-5:
153     assertEquals(testee.getAtomSpec(model,
154             false),
155             "(2-5,8)&:A/2.1,5-7:B/2.1");
156
157     model.addRange("1", 1, 4, "B");
158     assertEquals(testee.getAtomSpec(model,
159             false),
160             "1-4:B/1.1,(2-5,8)&:A/2.1,5-7:B/2.1");
161
162     model.addRange("1", 5, 9, "C");
163     assertEquals(testee.getAtomSpec(model,
164             false),
165             "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-7:B/2.1");
166
167     model.addRange("2", 8, 10, "B");
168     // 8-10 extends 5-7 to make 5-10
169     assertEquals(testee.getAtomSpec(model,
170             false),
171             "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
172
173     model.addRange("2", 8, 9, "B");
174     // subsumed by 5-10 so makes no difference
175     assertEquals(testee.getAtomSpec(model,
176             false),
177             "1-4:B/1.1,5-9:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
178
179     model.addRange("1", 3, 10, "C");
180     // subsumes 5-9 so replaces it
181     assertEquals(testee.getAtomSpec(model,
182             false),
183             "1-4:B/1.1,3-10:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1");
184
185     // empty chain code - e.g. from homology modelling
186     model.addRange("6", 25, 35, " ");
187     assertEquals(testee.getAtomSpec(model,
188             false),
189             "1-4:B/1.1,3-10:C/1.1,(2-5,8)&:A/2.1,5-10:B/2.1,25-35:/6.1");
190   }
191
192   @Test(groups = { "Functional" })
193   public void testColourBySequence()
194   {
195     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
196     JmolCommands.addAtomSpecRange(map, Color.blue, "1", 2, 5, "A");
197     JmolCommands.addAtomSpecRange(map, Color.blue, "1", 7, 7, "B");
198     JmolCommands.addAtomSpecRange(map, Color.blue, "1", 9, 23, "A");
199     JmolCommands.addAtomSpecRange(map, Color.blue, "2", 1, 1, "A");
200     JmolCommands.addAtomSpecRange(map, Color.blue, "2", 4, 7, "B");
201     JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 8, 8, "A");
202     JmolCommands.addAtomSpecRange(map, Color.yellow, "2", 3, 5, "A");
203     JmolCommands.addAtomSpecRange(map, Color.red, "1", 3, 5, "A");
204     JmolCommands.addAtomSpecRange(map, Color.red, "1", 6, 9, "A");
205
206     // Colours should appear in the Jmol command in the order in which
207     // they were added; within colour, by model, by chain, ranges in start order
208     List<StructureCommandI> commands = testee.colourBySequence(map);
209     assertEquals(commands.size(), 1);
210     String expected1 = "select (2-5,9-23)&:A/1.1,7:B/1.1,1:A/2.1,4-7:B/2.1;color[0,0,255]";
211     String expected2 = "select (3-5,8)&:A/2.1;color[255,255,0]";
212     String expected3 = "select 3-9:A/1.1;color[255,0,0]";
213     assertEquals(commands.get(0).getCommand(),
214             expected1 + ";" + expected2 + ";" + expected3);
215   }
216
217   @Test(groups = { "Functional" })
218   public void testSuperposeStructures()
219   {
220     AtomSpecModel ref = new AtomSpecModel();
221     ref.addRange("1", 12, 14, "A");
222     ref.addRange("1", 18, 18, "B");
223     ref.addRange("1", 22, 23, "B");
224     AtomSpecModel toAlign = new AtomSpecModel();
225     toAlign.addRange("2", 15, 17, "B");
226     toAlign.addRange("2", 20, 21, "B");
227     toAlign.addRange("2", 22, 22, "C");
228     List<StructureCommandI> command = testee.superposeStructures(ref,
229             toAlign);
230     assertEquals(command.size(), 1);
231     String refSpec = "12-14:A/1.1,(18,22-23)&:B/1.1";
232     String toAlignSpec = "(15-17,20-21)&:B/2.1,22:C/2.1";
233     String expected = String.format(
234             "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
235             toAlignSpec, refSpec, toAlignSpec, refSpec);
236     assertEquals(command.get(0).getCommand(), expected);
237   }
238
239   @Test(groups = "Functional")
240   public void testGetModelStartNo()
241   {
242     assertEquals(testee.getModelStartNo(), 1);
243   }
244
245   @Test(groups = "Functional")
246   public void testColourByChain()
247   {
248     StructureCommandI cmd = testee.colourByChain();
249     assertEquals(cmd.getCommand(), "select *;color chain");
250   }
251
252   @Test(groups = "Functional")
253   public void testColourByCharge()
254   {
255     List<StructureCommandI> cmds = testee.colourByCharge();
256     assertEquals(cmds.size(), 1);
257     assertEquals(cmds.get(0).getCommand(),
258             "select *;color white;select ASP,GLU;color red;"
259                     + "select LYS,ARG;color blue;select CYS;color yellow");
260   }
261
262   @Test(groups = "Functional")
263   public void testSetBackgroundColour()
264   {
265     StructureCommandI cmd = testee.setBackgroundColour(Color.PINK);
266     assertEquals(cmd.getCommand(), "background [255,175,175]");
267   }
268
269   @Test(groups = "Functional")
270   public void testFocusView()
271   {
272     StructureCommandI cmd = testee.focusView();
273     assertEquals(cmd.getCommand(), "zoom 0");
274   }
275
276   @Test(groups = "Functional")
277   public void testSaveSession()
278   {
279     StructureCommandI cmd = testee.saveSession("/some/filepath");
280     assertEquals(cmd.getCommand(), "write STATE \"/some/filepath\"");
281   }
282
283   @Test(groups = "Functional")
284   public void testShowBackbone()
285   {
286     List<StructureCommandI> cmds = testee.showBackbone();
287     assertEquals(cmds.size(), 1);
288     assertEquals(cmds.get(0).getCommand(),
289             "select *; cartoons off; backbone");
290   }
291
292   @Test(groups = "Functional")
293   public void testLoadFile()
294   {
295     StructureCommandI cmd = testee.loadFile("/some/filepath");
296     assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
297
298     // single backslash gets escaped to double
299     cmd = testee.loadFile("\\some\\filepath");
300     assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
301   }
302
303   @Test(groups = "Functional")
304   public void testOpenSession()
305   {
306     StructureCommandI cmd = testee.openSession("/some/filepath");
307     assertEquals(cmd.getCommand(), "load FILES \"/some/filepath\"");
308
309     // single backslash gets escaped to double
310     cmd = testee.openSession("\\some\\filepath");
311     assertEquals(cmd.getCommand(), "load FILES \"\\\\some\\\\filepath\"");
312   }
313 }