18c4a1408be05357fca5ad2d0774db403d2857dd
[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.api.AlignmentViewPanel;
27 import jalview.api.FeatureRenderer;
28 import jalview.datamodel.Alignment;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.AlignFrame;
35 import jalview.gui.JvOptionPane;
36 import jalview.gui.SequenceRenderer;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.schemes.JalviewColourScheme;
39 import jalview.structure.AtomSpec;
40 import jalview.structure.StructureMapping;
41 import jalview.structure.StructureSelectionManager;
42 import jalview.structures.models.AAStructureBindingModel;
43
44 import java.awt.Color;
45 import java.util.HashMap;
46 import java.util.LinkedHashMap;
47 import java.util.List;
48 import java.util.Map;
49
50 import org.testng.annotations.BeforeClass;
51 import org.testng.annotations.Test;
52
53 import junit.extensions.PA;
54
55 public class ChimeraCommandsTest
56 {
57   private SequenceRenderer sr;
58
59   private String[] files;
60   
61   private AAStructureBindingModel mockBinding = new AAStructureBindingModel(
62           null, null)
63   {
64     @Override
65     public void releaseReferences(Object svl)
66     {
67     }
68
69     @Override
70     public void highlightAtoms(List<AtomSpec> atoms)
71     {
72     }
73
74     @Override
75     public void setJalviewColourScheme(ColourSchemeI cs)
76     {
77     }
78
79     @Override
80     public String superposeStructures(AlignmentI[] alignments,
81             int[] structureIndices, HiddenColumns[] hiddenCols)
82     {
83       return null;
84     }
85
86     @Override
87     public void setBackgroundColour(Color col)
88     {
89     }
90
91     @Override
92     protected String[] getColourBySequenceCommands(String[] files,
93             AlignmentViewPanel avp)
94     {
95       return null;
96     }
97
98     @Override
99     public jalview.api.SequenceRenderer getSequenceRenderer(
100             AlignmentViewPanel alignment)
101     {
102       return sr;
103     }
104
105     @Override
106     protected void colourBySequence(String[] colourBySequenceCommands)
107     {
108     }
109
110     @Override
111     public void colourByChain()
112     {
113     }
114
115     @Override
116     public void colourByCharge()
117     {
118     }
119
120     @Override
121     public FeatureRenderer getFeatureRenderer(AlignmentViewPanel alignment)
122     {
123       return null;
124     }
125
126     @Override
127     public String[] getStructureFiles()
128     {
129       return files;
130     }
131
132     @Override
133     public String getModelSpec(int model)
134     {
135       return "#" + String.valueOf(model);
136     }
137   };
138
139   @BeforeClass(alwaysRun = true)
140   public void setUpJvOptionPane()
141   {
142     JvOptionPane.setInteractiveMode(false);
143     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
144   }
145
146   @Test(groups = { "Functional" })
147   public void testBuildColourCommands()
148   {
149     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
150     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 2, 5, "A");
151     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 7, 7, "B");
152     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 9, 23, "A");
153     ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 1, 1, "A");
154     ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 4, 7, "B");
155     ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 8, 8, "A");
156     ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 3, 5, "A");
157     ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 3, 5, "A");
158     ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 6, 9, "A");
159
160     // Colours should appear in the Chimera command in the order in which
161     // they were added; within colour, by model, by chain, ranges in start order
162     // all prefixed with #808080 to colour hidden regions (if shown) gray
163     String command = ChimeraCommands.buildColourCommands(map, mockBinding)
164             .get(0);
165     assertEquals(
166             command,
167             "color #808080; 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");
168   }
169
170   @Test(groups = { "Functional" })
171   public void testBuildSetAttributeCommands()
172   {
173     /*
174      * make a map of { featureType, {featureValue, {residue range specification } } }
175      */
176     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
177     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
178     
179     /*
180      * start with just one feature/value...
181      */
182     featuresMap.put("chain", featureValues);
183     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 8, 20, "A");
184   
185     List<String> commands = ChimeraCommands
186             .buildSetAttributeCommands(featuresMap, mockBinding);
187     assertEquals(1, commands.size());
188
189     /*
190      * feature name gets a jv_ namespace prefix
191      * feature value is quoted in case it contains spaces
192      */
193     assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:8-20.A");
194
195     // add same feature value, overlapping range
196     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
197     // same feature value, contiguous range
198     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
199     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
200             mockBinding);
201     assertEquals(1, commands.size());
202     assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:3-25.A");
203
204     // same feature value and model, different chain
205     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
206     // same feature value and chain, different model
207     ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
208     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
209             mockBinding);
210     assertEquals(1, commands.size());
211     assertEquals(commands.get(0),
212             "setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A");
213
214     // same feature, different value
215     ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
216     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
217             mockBinding);
218     assertEquals(2, commands.size());
219     // commands are ordered by feature type but not by value
220     // so use contains to test for the expected command:
221     assertTrue(commands
222             .contains("setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"));
223     assertTrue(commands.contains("setattr r jv_chain 'Y' #0:40-50.A"));
224
225     featuresMap.clear();
226     featureValues.clear();
227     featuresMap.put("side-chain binding!", featureValues);
228     ChimeraCommands.addAtomSpecRange(featureValues,
229             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
230             "A");
231     // feature names are sanitised to change non-alphanumeric to underscore
232     // feature values are sanitised to encode single quote characters
233     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
234             mockBinding);
235     assertTrue(commands
236             .contains("setattr r jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A"));
237   }
238
239   /**
240    * Tests for the method that prefixes and sanitises a feature name so it can
241    * be used as a valid, namespaced attribute name in Chimera
242    */
243   @Test(groups = { "Functional" })
244   public void testMakeAttributeName()
245   {
246     assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
247     assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
248     assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
249     assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
250             "jv_Hello_World_24");
251     assertEquals(
252             ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
253             "jv__this_is_a_very__odd_name");
254     // name ending in color gets underscore appended
255     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
256             "jv_helixColor_");
257   }
258
259   @Test(groups = { "Functional" })
260   public void testGetColourBySequenceCommands_hiddenColumns()
261   {
262     /*
263      * load these sequences, coloured by Strand propensity,
264      * with columns 2-4 hidden
265      */
266     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
267     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
268     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
269     AlignFrame af = new AlignFrame(al, 800, 500);
270     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
271     ColumnSelection cs = new ColumnSelection();
272     cs.addElement(2);
273     cs.addElement(3);
274     cs.addElement(4);
275     af.getViewport().setColumnSelection(cs);
276     af.hideSelColumns_actionPerformed(null);
277     sr = new SequenceRenderer(af.getViewport());
278     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
279     files = new String[] { "seq1.pdb", "seq2.pdb" };
280     StructureSelectionManager ssm = new StructureSelectionManager();
281
282     /*
283      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
284      */
285     HashMap<Integer, int[]> map = new HashMap<>();
286     for (int pos = 1; pos <= seq1.getLength(); pos++)
287     {
288       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
289     }
290     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
291             "A", map, null);
292     ssm.addStructureMapping(sm1);
293     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
294             "B", map, null);
295     ssm.addStructureMapping(sm2);
296
297     /*
298      * put data into the mock binding object
299      */
300     PA.setValue(mockBinding, "ssm", ssm);
301     PA.setValue(mockBinding, "sequence", seqs);
302
303     Map<Object, AtomSpecModel> colourMap = mockBinding
304             .buildColoursMap(af.alignPanel);
305     String[] commands = ChimeraCommands
306             .getColourBySequenceCommand(colourMap, mockBinding);
307     assertEquals(1, commands.length);
308     String theCommand = commands[0];
309     // M colour is #82827d (see strand.html help page)
310     assertTrue(theCommand.contains("color #82827d #0:21.A|#1:21.B"));
311     // H colour is #60609f
312     assertTrue(theCommand.contains("color #60609f #0:22.A"));
313     // V colour is #ffff00
314     assertTrue(theCommand.contains("color #ffff00 #1:22.B"));
315     // hidden columns are Gray (128, 128, 128)
316     assertTrue(theCommand.contains("color #808080 #0:23-25.A|#1:23-25.B"));
317     // S and G are both coloured #4949b6
318     assertTrue(theCommand.contains("color #4949b6 #0:26-30.A|#1:26-30.B"));
319   }
320
321   @Test(groups = "Functional")
322   public void testGetAtomSpec()
323   {
324     AtomSpecModel model = new AtomSpecModel();
325     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding), "");
326     model.addRange(1, 2, 4, "A");
327     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
328             "#1:2-4.A");
329     model.addRange(1, 8, 8, "A");
330     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
331             "#1:2-4.A,8.A");
332     model.addRange(1, 5, 7, "B");
333     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
334             "#1:2-4.A,8.A,5-7.B");
335     model.addRange(1, 3, 5, "A");
336     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
337             "#1:2-5.A,8.A,5-7.B");
338     model.addRange(0, 1, 4, "B");
339     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
340             "#0:1-4.B|#1:2-5.A,8.A,5-7.B");
341     model.addRange(0, 5, 9, "C");
342     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
343             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-7.B");
344     model.addRange(1, 8, 10, "B");
345     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
346             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
347     model.addRange(1, 8, 9, "B");
348     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
349             "#0:1-4.B,5-9.C|#1:2-5.A,8.A,5-10.B");
350     model.addRange(0, 3, 10, "C"); // subsumes 5-9
351     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
352             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B");
353     model.addRange(5, 25, 35, " "); // empty chain code - e.g. from homology
354                                     // modelling
355     assertEquals(ChimeraCommands.getAtomSpec(model, mockBinding),
356             "#0:1-4.B,3-10.C|#1:2-5.A,8.A,5-10.B|#5:25-35.");
357   
358   }
359 }