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