fb442e31e8f8bf02b4442335cffbd928bb53d425
[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.gui.JvOptionPane;
27
28 import java.awt.Color;
29 import java.util.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class ChimeraCommandsTest
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   @Test(groups = { "Functional" })
48   public void testBuildColourCommands()
49   {
50
51     Map<Object, AtomSpecModel> map = new LinkedHashMap<Object, AtomSpecModel>();
52     ChimeraCommands.addColourRange(map, Color.blue, 0, 2, 5, "A");
53     ChimeraCommands.addColourRange(map, Color.blue, 0, 7, 7, "B");
54     ChimeraCommands.addColourRange(map, Color.blue, 0, 9, 23, "A");
55     ChimeraCommands.addColourRange(map, Color.blue, 1, 1, 1, "A");
56     ChimeraCommands.addColourRange(map, Color.blue, 1, 4, 7, "B");
57     ChimeraCommands.addColourRange(map, Color.yellow, 1, 8, 8, "A");
58     ChimeraCommands.addColourRange(map, Color.yellow, 1, 3, 5, "A");
59     ChimeraCommands.addColourRange(map, Color.red, 0, 3, 5, "A");
60     ChimeraCommands.addColourRange(map, Color.red, 0, 6, 9, "A");
61
62     // Colours should appear in the Chimera command in the order in which
63     // they were added; within colour, by model, by chain, ranges in start order
64     String command = ChimeraCommands.buildColourCommands(map).get(0);
65     assertEquals(
66             command,
67             "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");
68   }
69
70   @Test(groups = { "Functional" })
71   public void testBuildSetAttributeCommands()
72   {
73     /*
74      * make a map of { featureType, {featureValue, {residue range specification } } }
75      */
76     Map<String, Map<Object, AtomSpecModel>> featuresMap = new LinkedHashMap<String, Map<Object, AtomSpecModel>>();
77     Map<Object, AtomSpecModel> featureValues = new HashMap<Object, AtomSpecModel>();
78     
79     /*
80      * start with just one feature/value...
81      */
82     featuresMap.put("chain", featureValues);
83     ChimeraCommands.addColourRange(featureValues, "X", 0, 8, 20, "A");
84   
85     List<String> commands = ChimeraCommands
86             .buildSetAttributeCommands(featuresMap);
87     assertEquals(1, commands.size());
88
89     /*
90      * feature name gets a jv_ namespace prefix
91      * feature value is quoted in case it contains spaces
92      */
93     assertEquals(commands.get(0), "setattr r jv_chain \"X\" #0:8-20.A");
94
95     // add same feature value, overlapping range
96     ChimeraCommands.addColourRange(featureValues, "X", 0, 3, 9, "A");
97     // same feature value, contiguous range
98     ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "A");
99     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
100     assertEquals(1, commands.size());
101     assertEquals(commands.get(0), "setattr r jv_chain \"X\" #0:3-25.A");
102
103     // same feature value and model, different chain
104     ChimeraCommands.addColourRange(featureValues, "X", 0, 21, 25, "B");
105     // same feature value and chain, different model
106     ChimeraCommands.addColourRange(featureValues, "X", 1, 26, 30, "A");
107     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
108     assertEquals(1, commands.size());
109     assertEquals(commands.get(0),
110             "setattr r jv_chain \"X\" #0:3-25.A,21-25.B|#1:26-30.A");
111
112     // same feature, different value
113     ChimeraCommands.addColourRange(featureValues, "Y", 0, 40, 50, "A");
114     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
115     assertEquals(2, commands.size());
116     // commands are ordered by feature type but not by value
117     // so use contains to test for the expected command:
118     assertTrue(commands
119             .contains("setattr r jv_chain \"X\" #0:3-25.A,21-25.B|#1:26-30.A"));
120     assertTrue(commands.contains("setattr r jv_chain \"Y\" #0:40-50.A"));
121
122     featuresMap.clear();
123     featureValues.clear();
124     featuresMap.put("side-chain binding!", featureValues);
125     ChimeraCommands.addColourRange(featureValues, "metal ion!", 0, 7, 15,
126             "A");
127     // feature names are sanitised to change space or hyphen to underscore
128     commands = ChimeraCommands.buildSetAttributeCommands(featuresMap);
129     assertTrue(commands
130             .contains("setattr r jv_side_chain_binding_ \"metal ion!\" #0:7-15.A"));
131   }
132
133   /**
134    * Tests for the method that prefixes and sanitises a feature name so it can
135    * be used as a valid, namespaced attribute name in Chimera
136    */
137   @Test(groups = { "Functional" })
138   public void testMakeAttributeName()
139   {
140     assertEquals(ChimeraCommands.makeAttributeName(null), "jv_");
141     assertEquals(ChimeraCommands.makeAttributeName(""), "jv_");
142     assertEquals(ChimeraCommands.makeAttributeName("helix"), "jv_helix");
143     assertEquals(ChimeraCommands.makeAttributeName("Hello World 24"),
144             "jv_Hello_World_24");
145     assertEquals(
146             ChimeraCommands.makeAttributeName("!this is-a_very*{odd(name"),
147             "jv__this_is_a_very__odd_name");
148     // name ending in color gets underscore appended
149     assertEquals(ChimeraCommands.makeAttributeName("helixColor"),
150             "jv_helixColor_");
151   }
152 }