JAL-2422 basic proof of concept of ChimeraX opened/coloured by Jalview
[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.JvOptionPane;
33 import jalview.gui.SequenceRenderer;
34 import jalview.schemes.JalviewColourScheme;
35 import jalview.structure.StructureMapping;
36 import jalview.structure.StructureMappingcommandSet;
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.BeforeClass;
46 import org.testng.annotations.Test;
47
48 public class ChimeraCommandsTest
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 testBuildColourCommands()
60   {
61
62     Map<Object, AtomSpecModel> map = new LinkedHashMap<>();
63     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 2, 5, "A");
64     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 7, 7, "B");
65     ChimeraCommands.addAtomSpecRange(map, Color.blue, 0, 9, 23, "A");
66     ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 1, 1, "A");
67     ChimeraCommands.addAtomSpecRange(map, Color.blue, 1, 4, 7, "B");
68     ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 8, 8, "A");
69     ChimeraCommands.addAtomSpecRange(map, Color.yellow, 1, 3, 5, "A");
70     ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 3, 5, "A");
71     ChimeraCommands.addAtomSpecRange(map, Color.red, 0, 6, 9, "A");
72
73     // Colours should appear in the Chimera command in the order in which
74     // they were added; within colour, by model, by chain, ranges in start order
75     String command = ChimeraCommands.buildColourCommands(map, false).get(0);
76     assertEquals(
77             command,
78             "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");
79   }
80
81   @Test(groups = { "Functional" })
82   public void testBuildSetAttributeCommands()
83   {
84     /*
85      * make a map of { featureType, {featureValue, {residue range specification } } }
86      */
87     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<>();
88     Map<Object, AtomSpecModel> featureValues = new HashMap<>();
89     
90     /*
91      * start with just one feature/value...
92      */
93     featuresMap.put("chain", featureValues);
94     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 8, 20, "A");
95   
96     List<String> commands = ChimeraCommands
97             .buildSetAttributeCommands(featuresMap, false);
98     assertEquals(1, commands.size());
99
100     /*
101      * feature name gets a jv_ namespace prefix
102      * feature value is quoted in case it contains spaces
103      */
104     assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:8-20.A");
105
106     // add same feature value, overlapping range
107     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
108     // same feature value, contiguous range
109     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
110     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
111             false);
112     assertEquals(1, commands.size());
113     assertEquals(commands.get(0), "setattr r jv_chain 'X' #0:3-25.A");
114
115     // same feature value and model, different chain
116     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
117     // same feature value and chain, different model
118     ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
119     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
120             false);
121     assertEquals(1, commands.size());
122     assertEquals(commands.get(0),
123             "setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A");
124
125     // same feature, different value
126     ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
127     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
128             false);
129     assertEquals(2, commands.size());
130     // commands are ordered by feature type but not by value
131     // so use contains to test for the expected command:
132     assertTrue(commands
133             .contains("setattr r jv_chain 'X' #0:3-25.A,21-25.B|#1:26-30.A"));
134     assertTrue(commands.contains("setattr r jv_chain 'Y' #0:40-50.A"));
135
136     featuresMap.clear();
137     featureValues.clear();
138     featuresMap.put("side-chain binding!", featureValues);
139     ChimeraCommands.addAtomSpecRange(featureValues,
140             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
141             "A");
142     // feature names are sanitised to change non-alphanumeric to underscore
143     // feature values are sanitised to encode single quote characters
144     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap,
145             false);
146     assertTrue(commands
147             .contains("setattr r jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' #0:7-15.A"));
148   }
149
150   /**
151    * Tests for the method that prefixes and sanitises a feature name so it can
152    * be used as a valid, namespaced attribute name in Chimera
153    */
154   @Test(groups = { "Functional" })
155   public void testMakeAttributeName()
156   {
157     assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
158     assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
159     assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
160     assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
161             "jv_Hello_World_24");
162     assertEquals(
163             ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
164             "jv__this_is_a_very__odd_name");
165     // name ending in color gets underscore appended
166     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
167             "jv_helixColor_");
168   }
169
170   @Test(groups = { "Functional" })
171   public void testGetColourBySequenceCommands_hiddenColumns()
172   {
173     /*
174      * load these sequences, coloured by Strand propensity,
175      * with columns 2-4 hidden
176      */
177     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
178     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
179     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
180     AlignFrame af = new AlignFrame(al, 800, 500);
181     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
182     ColumnSelection cs = new ColumnSelection();
183     cs.addElement(2);
184     cs.addElement(3);
185     cs.addElement(4);
186     af.getViewport().setColumnSelection(cs);
187     af.hideSelColumns_actionPerformed(null);
188     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
189     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
190     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
191     StructureSelectionManager ssm = new StructureSelectionManager();
192
193     /*
194      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
195      */
196     HashMap<Integer, int[]> map = new HashMap<>();
197     for (int pos = 1; pos <= seq1.getLength(); pos++)
198     {
199       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
200     }
201     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
202             "A", map, null);
203     ssm.addStructureMapping(sm1);
204     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
205             "B", map, null);
206     ssm.addStructureMapping(sm2);
207
208     StructureMappingcommandSet[] commands = ChimeraCommands
209             .getColourBySequenceCommand(ssm, files, seqs, sr, af.alignPanel,
210                     false);
211     assertEquals(1, commands.length);
212     assertEquals(1, commands[0].commands.length);
213     String theCommand = commands[0].commands[0];
214     // M colour is #82827d (see strand.html help page)
215     assertTrue(theCommand.contains("color #82827d #0:21.A|#1:21.B"));
216     // H colour is #60609f
217     assertTrue(theCommand.contains("color #60609f #0:22.A"));
218     // V colour is #ffff00
219     assertTrue(theCommand.contains("color #ffff00 #1:22.B"));
220     // hidden columns are Gray (128, 128, 128)
221     assertTrue(theCommand.contains("color #808080 #0:23-25.A|#1:23-25.B"));
222     // S and G are both coloured #4949b6
223     assertTrue(theCommand.contains("color #4949b6 #0:26-30.A|#1:26-30.B"));
224   }
225 }