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