a9eaca3695437445ba75c908ebe2b6e928f541fc
[jalview.git] / test / jalview / ext / rbvi / chimera / ChimeraXCommandsTest.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 ChimeraXCommandsTest
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 ChimeraXCommands().buildColourCommands(map).get(0);
68     assertEquals(
69             command,
70             "color #0/A:2-5,9-23/B:7|#1/A:1/B:4-7 #0000ff; color #1/A:3-5,8 #ffff00; color #0/A:3-9 #ff0000");
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     ChimeraXCommands commandGenerator = new ChimeraXCommands();
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),
98             "setattr #0/A:8-20 res jv_chain 'X' create true");
99
100     // add same feature value, overlapping range
101     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 3, 9, "A");
102     // same feature value, contiguous range
103     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "A");
104     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
105     assertEquals(1, commands.size());
106     assertEquals(commands.get(0),
107             "setattr #0/A:3-25 res jv_chain 'X' create true");
108
109     // same feature value and model, different chain
110     ChimeraCommands.addAtomSpecRange(featureValues, "X", 0, 21, 25, "B");
111     // same feature value and chain, different model
112     ChimeraCommands.addAtomSpecRange(featureValues, "X", 1, 26, 30, "A");
113     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
114     assertEquals(1, commands.size());
115     assertEquals(commands.get(0),
116             "setattr #0/A:3-25/B:21-25|#1/A:26-30 res jv_chain 'X' create true");
117
118     // same feature, different value
119     ChimeraCommands.addAtomSpecRange(featureValues, "Y", 0, 40, 50, "A");
120     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
121     assertEquals(2, commands.size());
122     // commands are ordered by feature type but not by value
123     // so use contains to test for the expected command:
124     assertTrue(commands
125             .contains(
126                     "setattr #0/A:3-25/B:21-25|#1/A:26-30 res jv_chain 'X' create true"));
127     assertTrue(commands
128             .contains("setattr #0/A:40-50 res jv_chain 'Y' create true"));
129
130     featuresMap.clear();
131     featureValues.clear();
132     featuresMap.put("side-chain binding!", featureValues);
133     ChimeraCommands.addAtomSpecRange(featureValues,
134             "<html>metal <a href=\"http:a.b.c/x\"> 'ion!", 0, 7, 15,
135             "A");
136     // feature names are sanitised to change non-alphanumeric to underscore
137     // feature values are sanitised to encode single quote characters
138     commands = commandGenerator.buildSetAttributeCommands(featuresMap);
139     assertTrue(commands.contains(
140             "setattr #0/A:7-15 res jv_side_chain_binding_ '<html>metal <a href=\"http:a.b.c/x\"> &#39;ion!' create true"));
141   }
142
143   @Test(groups = { "Functional" })
144   public void testColourBySequence_hiddenColumns()
145   {
146     /*
147      * load these sequences, coloured by Strand propensity,
148      * with columns 2-4 hidden
149      */
150     SequenceI seq1 = new Sequence("seq1", "MHRSQSSSGG");
151     SequenceI seq2 = new Sequence("seq2", "MVRSNGGSSS");
152     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
153     AlignFrame af = new AlignFrame(al, 800, 500);
154     af.changeColour_actionPerformed(JalviewColourScheme.Strand.toString());
155     ColumnSelection cs = new ColumnSelection();
156     cs.addElement(2);
157     cs.addElement(3);
158     cs.addElement(4);
159     af.getViewport().setColumnSelection(cs);
160     af.hideSelColumns_actionPerformed(null);
161     SequenceRenderer sr = new SequenceRenderer(af.getViewport());
162     SequenceI[][] seqs = new SequenceI[][] { { seq1 }, { seq2 } };
163     String[] files = new String[] { "seq1.pdb", "seq2.pdb" };
164     StructureSelectionManager ssm = new StructureSelectionManager();
165
166     /*
167      * map residues 1-10 to residues 21-30 (atoms 105-150) in structures
168      */
169     HashMap<Integer, int[]> map = new HashMap<>();
170     for (int pos = 1; pos <= seq1.getLength(); pos++)
171     {
172       map.put(pos, new int[] { 20 + pos, 5 * (20 + pos) });
173     }
174     StructureMapping sm1 = new StructureMapping(seq1, "seq1.pdb", "pdb1",
175             "A", map, null);
176     ssm.addStructureMapping(sm1);
177     StructureMapping sm2 = new StructureMapping(seq2, "seq2.pdb", "pdb2",
178             "B", map, null);
179     ssm.addStructureMapping(sm2);
180
181     String[] commands = new ChimeraXCommands()
182             .colourBySequence(ssm, files, seqs, sr, af.alignPanel);
183     assertEquals(1, commands.length);
184     String theCommand = commands[0];
185     // M colour is #82827d (see strand.html help page)
186     assertTrue(theCommand.contains("color #0/A:21|#1/B:21 #82827d"));// #0:21.A|#1:21.B"));
187     // H colour is #60609f
188     assertTrue(theCommand.contains("color #0/A:22 #60609f"));
189     // V colour is #ffff00
190     assertTrue(theCommand.contains("color #1/B:22 #ffff00"));
191     // hidden columns are Gray (128, 128, 128)
192     assertTrue(theCommand.contains("color #0/A:23-25|#1/B:23-25"));
193     // S and G are both coloured #4949b6
194     assertTrue(theCommand.contains("color #0/A:26-30|#1/B:26-30"));
195   }
196
197   @Test(groups = { "Functional" })
198   public void testSuperposeStructures()
199   {
200     StructureCommandsI testee = new ChimeraXCommands();
201     AtomSpecModel ref = new AtomSpecModel();
202     ref.addRange(1, 12, 14, "A");
203     ref.addRange(1, 18, 18, "B");
204     ref.addRange(1, 22, 23, "B");
205     AtomSpecModel toAlign = new AtomSpecModel();
206     toAlign.addRange(2, 15, 17, "B");
207     toAlign.addRange(2, 20, 21, "B");
208     toAlign.addRange(2, 22, 22, "C");
209     String command = testee.superposeStructures(ref, toAlign);
210     String refSpec = "#1/A:12-14/B:18,22-23";
211     String toAlignSpec = "#2/B:15-17,20-21/C:22";
212     String expected = String.format(
213             "align %s %s;~display all; chain @CA|P; ribbon %s|%s; focus",
214             refSpec, toAlignSpec, refSpec, toAlignSpec);
215     assertEquals(command, expected);
216   }
217 }