JAL-3518 more pull up / test coverage of structure command generation
[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 jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.ColumnSelection;
29 import jalview.datamodel.Sequence;
30 import jalview.datamodel.SequenceI;
31 import jalview.gui.AlignFrame;
32 import jalview.gui.JvOptionPane;
33 import jalview.gui.SequenceRenderer;
34 import jalview.schemes.JalviewColourScheme;
35 import jalview.structure.AtomSpecModel;
36 import jalview.structure.StructureCommandsI;
37 import jalview.structure.StructureMapping;
38 import jalview.structure.StructureSelectionManager;
39
40 import java.awt.Color;
41 import java.util.HashMap;
42 import java.util.LinkedHashMap;
43 import java.util.Map;
44
45 import org.testng.annotations.BeforeClass;
46 import org.testng.annotations.Test;
47
48 public class JmolCommandsTest
49 {
50
51   @BeforeClass(alwaysRun = true)
52   public void setUpJvOptionPane()
53   {
54     JvOptionPane.setInteractiveMode(false);
55     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
56   }
57
58   @Test(groups = { "Functional" })
59   public void testGetColourBySequenceCommands_hiddenColumns()
60   {
61     /*
62      * load these sequences, coloured by Strand propensity,
63      * with columns 2-4 hidden
64      */
65     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
66     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
67     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
68     AlignFrame af = new AlignFrame(al, 800, 500);
69     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
70     ColumnSelection cs = new ColumnSelection();
71     cs.addElement(2);
72     cs.addElement(3);
73     cs.addElement(4);
74     af.getViewport().setColumnSelection(cs);
75     af.hideSelColumns_actionPerformed(null);
76     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
77     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
78     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
79     StructureSelectionManager ssm = new StructureSelectionManager();
80
81     /*
82      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
83      */
84     HashMap<Integer, int[]> map = new HashMap<>();
85     for (int pos = 1; pos <= seq1.getLength(); pos++)
86     {
87       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
88     }
89     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
90             "A", map, null);
91     ssm.addStructureMapping(sm1);
92     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
93             "B", map, null);
94     ssm.addStructureMapping(sm2);
95
96     String[] commands = new JmolCommands().colourBySequence(ssm, files,
97             seqs, sr, af.alignPanel);
98     assertEquals(commands.length, 2);
99
100     String chainACommand = commands[0];
101     // M colour is #82827d == (130, 130, 125) (see strand.html help page)
102     assertTrue(
103             chainACommand.contains("select 21:A/1.1;color[130,130,125]")); // first
104                                                                            // one
105     // H colour is #60609f == (96, 96, 159)
106     assertTrue(chainACommand.contains(";select 22:A/1.1;color[96,96,159]"));
107     // hidden columns are Gray (128, 128, 128)
108     assertTrue(chainACommand
109             .contains(";select 23-25:A/1.1;color[128,128,128]"));
110     // S and G are both coloured #4949b6 == (73, 73, 182)
111     assertTrue(
112             chainACommand.contains(";select 26-30:A/1.1;color[73,73,182]"));
113
114     String chainBCommand = commands[1];
115     // M colour is #82827d == (130, 130, 125)
116     assertTrue(
117             chainBCommand.contains("select 21:B/2.1;color[130,130,125]"));
118     // V colour is #ffff00 == (255, 255, 0)
119     assertTrue(chainBCommand.contains(";select 22:B/2.1;color[255,255,0]"));
120     // hidden columns are Gray (128, 128, 128)
121     assertTrue(chainBCommand
122             .contains(";select 23-25:B/2.1;color[128,128,128]"));
123     // S and G are both coloured #4949b6 == (73, 73, 182)
124     assertTrue(
125             chainBCommand.contains(";select 26-30:B/2.1;color[73,73,182]"));
126   }
127
128   @Test(groups = "Functional")
129   public void testGetAtomSpec()
130   {
131     StructureCommandsI testee = new JmolCommands();
132     AtomSpecModel model = new AtomSpecModel();
133     assertEquals(testee.getAtomSpec(model, false), "");
134     model.addRange(1, 2, 4, "A");
135     assertEquals(testee.getAtomSpec(model, false), "2-4:A/1.1");
136     model.addRange(1, 8, 8, "A");
137     assertEquals(testee.getAtomSpec(model, false), "2-4:A/1.1|8:A/1.1");
138     model.addRange(1, 5, 7, "B");
139     assertEquals(testee.getAtomSpec(model, false),
140             "2-4:A/1.1|8:A/1.1|5-7:B/1.1");
141     model.addRange(1, 3, 5, "A");
142     assertEquals(testee.getAtomSpec(model, false),
143             "2-5:A/1.1|8:A/1.1|5-7:B/1.1");
144     model.addRange(2, 1, 4, "B");
145     assertEquals(testee.getAtomSpec(model, false),
146             "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1");
147     model.addRange(2, 5, 9, "C");
148     assertEquals(testee.getAtomSpec(model, false),
149             "2-5:A/1.1|8:A/1.1|5-7:B/1.1|1-4:B/2.1|5-9:C/2.1");
150     model.addRange(1, 8, 10, "B");
151     assertEquals(testee.getAtomSpec(model, false),
152             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
153     model.addRange(1, 8, 9, "B");
154     assertEquals(testee.getAtomSpec(model, false),
155             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|5-9:C/2.1");
156     model.addRange(2, 3, 10, "C"); // subsumes 5-9
157     assertEquals(testee.getAtomSpec(model, false),
158             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1");
159     model.addRange(5, 25, 35, " ");
160     assertEquals(testee.getAtomSpec(model, false),
161             "2-5:A/1.1|8:A/1.1|5-10:B/1.1|1-4:B/2.1|3-10:C/2.1|25-35:/5.1");
162   
163   }
164
165   @Test(groups = { "Functional" })
166   public void testColourBySequence()
167   {
168     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
169     JmolCommands.addAtomSpecRange(map, Color.blue, 1, 2, 5, "A");
170     JmolCommands.addAtomSpecRange(map, Color.blue, 1, 7, 7, "B");
171     JmolCommands.addAtomSpecRange(map, Color.blue, 1, 9, 23, "A");
172     JmolCommands.addAtomSpecRange(map, Color.blue, 2, 1, 1, "A");
173     JmolCommands.addAtomSpecRange(map, Color.blue, 2, 4, 7, "B");
174     JmolCommands.addAtomSpecRange(map, Color.yellow, 2, 8, 8, "A");
175     JmolCommands.addAtomSpecRange(map, Color.yellow, 2, 3, 5, "A");
176     JmolCommands.addAtomSpecRange(map, Color.red, 1, 3, 5, "A");
177     JmolCommands.addAtomSpecRange(map, Color.red, 1, 6, 9, "A");
178
179     // Colours should appear in the Jmol command in the order in which
180     // they were added; within colour, by model, by chain, ranges in start order
181     String[] commands = new JmolCommands().colourBySequence(map);
182     assertEquals(commands.length, 1);
183     String expected = "select 2-5:A/1.1|9-23:A/1.1|7:B/1.1|1:A/2.1|4-7:B/2.1;color[0,0,255]; "
184             + "select 3-5:A/2.1|8:A/2.1;color[255,255,0]; "
185             + "select 3-9:A/1.1;color[255,0,0]";
186     assertEquals(commands[0], expected);
187   }
188
189   @Test(groups = { "Functional" })
190   public void testSuperposeStructures()
191   {
192     StructureCommandsI testee = new JmolCommands();
193     AtomSpecModel ref = new AtomSpecModel();
194     ref.addRange(1, 12, 14, "A");
195     ref.addRange(1, 18, 18, "B");
196     ref.addRange(1, 22, 23, "B");
197     AtomSpecModel toAlign = new AtomSpecModel();
198     toAlign.addRange(2, 15, 17, "B");
199     toAlign.addRange(2, 20, 21, "B");
200     toAlign.addRange(2, 22, 22, "C");
201     String command = testee.superposeStructures(ref, toAlign);
202     String refSpec = "12-14:A/1.1|18:B/1.1|22-23:B/1.1";
203     String toAlignSpec = "15-17:B/2.1|20-21:B/2.1|22:C/2.1";
204     String expected = String.format(
205             "compare {2.1} {1.1} SUBSET {(*.CA | *.P) and conformation=1} ATOMS {%s}{%s} ROTATE TRANSLATE ;select %s|%s;cartoons",
206             toAlignSpec, refSpec, toAlignSpec, refSpec);
207     assertEquals(command, expected);
208   }
209
210   @Test(groups = "Functional")
211   public void testGetModelStartNo()
212   {
213     StructureCommandsI testee = new JmolCommands();
214     assertEquals(testee.getModelStartNo(), 1);
215   }
216 }