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