JAL-3518 basic refactoring / pull up of superposeStructures; to tidy!
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraCommandsTest.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.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.SequenceRenderer;
33 import jalview.schemes.JalviewColourScheme;
34 import jalview.structure.AtomSpecModel;
35 import jalview.structure.StructureCommandsI;
36 import jalview.structure.StructureMapping;
37 import jalview.structure.StructureSelectionManager;
38
39 import java.awt.Color;
40 import java.util.HashMap;
41 import java.util.LinkedHashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import org.testng.annotations.Test;
46
47 public class ChimeraCommandsTest
48 {
49
50   @Test(groups = { "Functional" })
51   public void testBuildColourCommands()
52   {
53
54     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
55     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 2, 5, "A");
56     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 7, 7, "B");
57     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 9, 23, "A");
58     ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 1, 1, "A");
59     ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 4, 7, "B");
60     ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 8, 8, "A");
61     ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 3, 5, "A");
62     ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 3, 5, "A");
63     ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 6, 9, "A");
64
65     // Colours should appear in the Chimera command in the order in which
66     // they were added; within colour, by model, by chain, ranges in start order
67     String command = new ChimeraCommands().buildColourCommands(map).get(0);
68     assertEquals(
69             command,
70             "color #0000ff #0:2-5.A,9-23.A,7.B|#1:1.A,4-7.B; color #ffff00 #1:3-5.A,8.A; color #ff0000 #0:3-9.A");
71   }
72
73   @Test(groups = { "Functional" })
74   public void testBuildSetAttributeCommands()
75   {
76     /*
77      * make a map of { featureType, {featureValue, {residue range specification } } }
78      */
79     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
80     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
81     
82     /*
83      * start with just one feature/value...
84      */
85     featuresMap.put("chain", featureValues);
86     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 8, 20, "A");
87   
88     ChimeraCommands commandGenerator = new ChimeraCommands();
89     List<String> commands = commandGenerator
90             .buildSetAttributeCommands(featuresMap);
91     assertEquals(1, commands.size());
92
93     /*
94      * feature name gets a jv_ namespace prefix
95      * feature value is quoted in case it contains spaces
96      */
97     assertEquals(commands.get(0), "setattr res jv_chain 'X' #0:8-20.A");
98
99     // add same feature value, overlapping range
100     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
101     // same feature value, contiguous range
102     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
103     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
104     assertEquals(1, commands.size());
105     assertEquals(commands.get(0), "setattr res jv_chain 'X' #0:3-25.A");
106
107     // same feature value and model, different chain
108     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
109     // same feature value and chain, different model
110     ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
111     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
112     assertEquals(1, commands.size());
113     assertEquals(commands.get(0),
114             "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A");
115
116     // same feature, different value
117     ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
118     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
119     assertEquals(2, commands.size());
120     // commands are ordered by feature type but not by value
121     // so use contains to test for the expected command:
122     assertTrue(commands
123             .contains(
124                     "setattr res jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"));
125     assertTrue(commands.contains("setattr res jv_chain 'Y' #0:40-50.A"));
126
127     featuresMap.clear();
128     featureValues.clear();
129     featuresMap.put("side-chain binding!", featureValues);
130     ChimeraCommands.addAtomSpecRange(featureValues,
131             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
132             "A");
133     // feature names are sanitised to change non-alphanumeric to underscore
134     // feature values are sanitised to encode single quote characters
135     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
136     assertTrue(commands
137             .contains(
138                     "setattr res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A"));
139   }
140
141   /**
142    * Tests for the method that prefixes and sanitises a feature name so it can
143    * be used as a valid, namespaced attribute name in Chimera
144    */
145   @Test(groups = { "Functional" })
146   public void testMakeAttributeName()
147   {
148     assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
149     assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
150     assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
151     assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
152             "jv_Hello_World_24");
153     assertEquals(
154             ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
155             "jv__this_is_a_very__odd_name");
156     // name ending in color gets underscore appended
157     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
158             "jv_helixColor_");
159   }
160
161   @Test(groups = { "Functional" })
162   public void testColourBySequence_hiddenColumns()
163   {
164     /*
165      * load these sequences, coloured by Strand propensity,
166      * with columns 2-4 hidden
167      */
168     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
169     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
170     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
171     AlignFrame af = new AlignFrame(al, 800, 500);
172     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
173     ColumnSelection cs = new ColumnSelection();
174     cs.addElement(2);
175     cs.addElement(3);
176     cs.addElement(4);
177     af.getViewport().setColumnSelection(cs);
178     af.hideSelColumns_actionPerformed(null);
179     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
180     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
181     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
182     StructureSelectionManager ssm = new StructureSelectionManager();
183
184     /*
185      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
186      */
187     HashMap<Integer, int[]> map = new HashMap<>();
188     for (int pos = 1; pos <= seq1.getLength(); pos++)
189     {
190       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
191     }
192     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
193             "A", map, null);
194     ssm.addStructureMapping(sm1);
195     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
196             "B", map, null);
197     ssm.addStructureMapping(sm2);
198
199     String[] commands = new ChimeraCommands()
200             .colourBySequence(ssm, files, seqs, sr, af.alignPanel);
201     assertEquals(1, commands.length);
202     String theCommand = commands[0];
203     // M colour is #82827d (see strand.html help page)
204     assertTrue(theCommand.contains("color #82827d #0:21.A|#1:21.B"));
205     // H colour is #60609f
206     assertTrue(theCommand.contains("color #60609f #0:22.A"));
207     // V colour is #ffff00
208     assertTrue(theCommand.contains("color #ffff00 #1:22.B"));
209     // hidden columns are Gray (128, 128, 128)
210     assertTrue(theCommand.contains("color #808080 #0:23-25.A|#1:23-25.B"));
211     // S and G are both coloured #4949b6
212     assertTrue(theCommand.contains("color #4949b6 #0:26-30.A|#1:26-30.B"));
213   }
214
215   @Test(groups = "Functional")
216   public void testGetAtomSpec()
217   {
218     StructureCommandsI testee = new ChimeraCommands();
219     AtomSpecModel model = new AtomSpecModel();
220     assertEquals(testee.getAtomSpec(model, false), "");
221     model.addRange(1, 2, 4, "A");
222     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A");
223     model.addRange(1, 8, 8, "A");
224     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A");
225     model.addRange(1, 5, 7, "B");
226     assertEquals(testee.getAtomSpec(model, false), "#1:2-4.A,8.A,5-7.B");
227     model.addRange(1, 3, 5, "A");
228     assertEquals(testee.getAtomSpec(model, false), "#1:2-5.A,8.A,5-7.B");
229     model.addRange(0, 1, 4, "B");
230     assertEquals(testee.getAtomSpec(model, false),
231             "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
232     model.addRange(0, 5, 9, "C");
233     assertEquals(testee.getAtomSpec(model, false),
234             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
235     model.addRange(1, 8, 10, "B");
236     assertEquals(testee.getAtomSpec(model, false),
237             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
238     model.addRange(1, 8, 9, "B");
239     assertEquals(testee.getAtomSpec(model, false),
240             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
241     model.addRange(0, 3, 10, "C"); // subsumes 5-9
242     assertEquals(testee.getAtomSpec(model, false),
243             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
244     model.addRange(5, 25, 35, " "); // empty chain code - e.g. from homology
245                                     // modelling
246     assertEquals(testee.getAtomSpec(model, false),
247             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
248
249   }
250
251   @Test(groups = "Functional")
252   public void testGetAtomSpec_alphaOnly()
253   {
254     StructureCommandsI testee = new ChimeraCommands();
255     AtomSpecModel model = new AtomSpecModel();
256     assertEquals(testee.getAtomSpec(model, true), "");
257     model.addRange(1, 2, 4, "A");
258     assertEquals(testee.getAtomSpec(model, true), "#1:2-4.A@CA|P");
259     model.addRange(1, 8, 8, "A");
260     assertEquals(testee.getAtomSpec(model, true), "#1:2-4.A,8.A@CA|P");
261     model.addRange(1, 5, 7, "B");
262     assertEquals(testee.getAtomSpec(model, true),
263             "#1:2-4.A,8.A,5-7.B@CA|P");
264     model.addRange(1, 3, 5, "A");
265     assertEquals(testee.getAtomSpec(model, true),
266             "#1:2-5.A,8.A,5-7.B@CA|P");
267     model.addRange(0, 1, 4, "B");
268     assertEquals(testee.getAtomSpec(model, true),
269             "#0:1-4.B@CA|P|#1:2-5.A,8.A,5-7.B@CA|P");
270     model.addRange(0, 5, 9, "C");
271     assertEquals(testee.getAtomSpec(model, true),
272             "#0:1-4.B,5-9.C@CA|P|#1:2-5.A,8.A,5-7.B@CA|P");
273     model.addRange(1, 8, 10, "B");
274     assertEquals(testee.getAtomSpec(model, true),
275             "#0:1-4.B,5-9.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P");
276     model.addRange(1, 8, 9, "B");
277     assertEquals(testee.getAtomSpec(model, true),
278             "#0:1-4.B,5-9.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P");
279     model.addRange(0, 3, 10, "C"); // subsumes 5-9
280     assertEquals(testee.getAtomSpec(model, true),
281             "#0:1-4.B,3-10.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P");
282     model.addRange(5, 25, 35, " "); // empty chain code
283     assertEquals(testee.getAtomSpec(model, true),
284             "#0:1-4.B,3-10.C@CA|P|#1:2-5.A,8.A,5-10.B@CA|P|#5:25-35.@CA|P");
285   
286   }
287
288   @Test(groups = { "Functional" })
289   public void testSuperposeStructures()
290   {
291     StructureCommandsI testee = new ChimeraCommands();
292     AtomSpecModel ref = new AtomSpecModel();
293     ref.addRange(1, 12, 14, "A");
294     ref.addRange(1, 18, 18, "B");
295     ref.addRange(1, 22, 23, "B");
296     AtomSpecModel toAlign = new AtomSpecModel();
297     toAlign.addRange(2, 15, 17, "B");
298     toAlign.addRange(2, 20, 21, "B");
299     toAlign.addRange(2, 22, 22, "C");
300     String command = testee.superposeStructures(ref, toAlign);
301     String refSpec = "#1:12-14.A,18.B,22-23.B@CA|P&~@.B-Z&~@.2-9";
302     String toAlignSpec = "#2:15-17.B,20-21.B,22.C@CA|P&~@.B-Z&~@.2-9";
303     String expected = String.format(
304             "match %s %s;~display all; chain @CA|P; ribbon %s|%s; focus",
305             refSpec, toAlignSpec, refSpec, toAlignSpec);
306     assertEquals(command, expected);
307   }
308 }