2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.schemes;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertTrue;
27 import static org.testng.AssertJUnit.fail;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.gui.JvOptionPane;
31 import jalview.util.ColorUtils;
32 import jalview.util.Format;
34 import java.awt.Color;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
39 public class FeatureColourTest
42 @BeforeClass(alwaysRun = true)
43 public void setUpJvOptionPane()
45 JvOptionPane.setInteractiveMode(false);
46 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49 @Test(groups = { "Functional" })
50 public void testCopyConstructor()
55 FeatureColour fc = new FeatureColour(Color.RED);
56 FeatureColour fc1 = new FeatureColour(fc);
57 assertTrue(fc1.getColour().equals(Color.RED));
58 assertFalse(fc1.isGraduatedColour());
59 assertFalse(fc1.isColourByLabel());
64 fc = new FeatureColour(Color.gray, Color.black, 10f, 20f);
65 fc.setAboveThreshold(true);
67 fc1 = new FeatureColour(fc);
68 assertTrue(fc1.isGraduatedColour());
69 assertFalse(fc1.isColourByLabel());
70 assertTrue(fc1.isAboveThreshold());
71 assertEquals(12f, fc1.getThreshold());
72 assertEquals(Color.gray, fc1.getMinColour());
73 assertEquals(Color.black, fc1.getMaxColour());
74 assertEquals(10f, fc1.getMin());
75 assertEquals(20f, fc1.getMax());
80 fc = new FeatureColour();
81 fc.setColourByLabel(true);
82 fc1 = new FeatureColour(fc);
83 assertTrue(fc1.isColourByLabel());
84 assertFalse(fc1.isGraduatedColour());
87 @Test(groups = { "Functional" })
88 public void testGetColor_simpleColour()
90 FeatureColour fc = new FeatureColour(Color.RED);
91 assertEquals(Color.RED,
92 fc.getColor(new SequenceFeature("Cath", "", 1, 2, 0f, null)));
95 @Test(groups = { "Functional" })
96 public void testGetColor_colourByLabel()
98 FeatureColour fc = new FeatureColour();
99 fc.setColourByLabel(true);
100 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f,
102 Color expected = ColorUtils.createColourFromName("desc");
103 assertEquals(expected, fc.getColor(sf));
106 @Test(groups = { "Functional" })
107 public void testGetColor_Graduated()
109 // graduated colour from score 0 to 100, gray(128, 128, 128) to red(255, 0,
111 FeatureColour fc = new FeatureColour(Color.GRAY, Color.RED, 0f, 100f);
112 // feature score is 75 which is 3/4 of the way from GRAY to RED
113 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f,
115 // the colour gradient is computed in float values from 0-1 (where 1 == 255)
116 float red = 128 / 255f + 3 / 4f * (255 - 128) / 255f;
117 float green = 128 / 255f + 3 / 4f * (0 - 128) / 255f;
118 float blue = 128 / 255f + 3 / 4f * (0 - 128) / 255f;
119 Color expected = new Color(red, green, blue);
120 assertEquals(expected, fc.getColor(sf));
123 @Test(groups = { "Functional" })
124 public void testGetColor_aboveBelowThreshold()
126 // gradient from [50, 150] from WHITE(255, 255, 255) to BLACK(0, 0, 0)
127 FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 50f,
129 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 70f,
131 fc.setThreshold(100f); // ignore for now
132 assertEquals(new Color(204, 204, 204), fc.getColor(sf));
134 fc.setAboveThreshold(true); // feature lies below threshold
135 assertNull(fc.getColor(sf));
137 fc.setBelowThreshold(true);
138 fc.setThreshold(70f);
139 assertNull(fc.getColor(sf)); // feature score == threshold - hidden
140 fc.setThreshold(69f);
141 assertNull(fc.getColor(sf)); // feature score > threshold - hidden
145 * Test output of feature colours to Jalview features file format
147 @Test(groups = { "Functional" })
148 public void testToJalviewFormat()
151 * plain colour - to RGB hex code
153 FeatureColour fc = new FeatureColour(Color.RED);
154 String redHex = Format.getHexString(Color.RED);
155 String hexColour = redHex;
156 assertEquals("domain\t" + hexColour, fc.toJalviewFormat("domain"));
159 * colour by label (no threshold)
161 fc = new FeatureColour();
162 fc.setColourByLabel(true);
163 assertEquals("domain\tlabel", fc.toJalviewFormat("domain"));
166 * colour by label (autoscaled) (an odd state you can reach by selecting
167 * 'above threshold', then deselecting 'threshold is min/max' then 'colour
170 fc.setAutoScaled(true);
171 assertEquals("domain\tlabel", fc.toJalviewFormat("domain"));
174 * colour by label (above threshold) (min/max values are output though not
175 * used by this scheme)
177 fc.setAutoScaled(false);
178 fc.setThreshold(12.5f);
179 fc.setAboveThreshold(true);
180 assertEquals("domain\tlabel|||0.0|0.0|above|12.5",
181 fc.toJalviewFormat("domain"));
184 * colour by label (below threshold)
186 fc.setBelowThreshold(true);
187 assertEquals("domain\tlabel|||0.0|0.0|below|12.5",
188 fc.toJalviewFormat("domain"));
191 * graduated colour, no threshold
193 fc = new FeatureColour(Color.GREEN, Color.RED, 12f, 25f);
194 String greenHex = Format.getHexString(Color.GREEN);
195 String expected = String.format("domain\t%s|%s|abso|12.0|25.0|none",
197 assertEquals(expected, fc.toJalviewFormat("domain"));
200 * colour ranges over the actual score ranges (not min/max)
202 fc.setAutoScaled(true);
203 expected = String.format("domain\t%s|%s|12.0|25.0|none", greenHex,
205 assertEquals(expected, fc.toJalviewFormat("domain"));
208 * graduated colour below threshold
210 fc.setThreshold(12.5f);
211 fc.setBelowThreshold(true);
212 expected = String.format("domain\t%s|%s|12.0|25.0|below|12.5",
214 assertEquals(expected, fc.toJalviewFormat("domain"));
217 * graduated colour above threshold
219 fc.setThreshold(12.5f);
220 fc.setAboveThreshold(true);
221 fc.setAutoScaled(false);
222 expected = String.format("domain\t%s|%s|abso|12.0|25.0|above|12.5",
224 assertEquals(expected, fc.toJalviewFormat("domain"));
228 * Test parsing of feature colours from Jalview features file format
230 @Test(groups = { "Functional" })
231 public void testParseJalviewFeatureColour()
234 * simple colour by name
236 FeatureColour fc = FeatureColour.parseJalviewFeatureColour("red");
237 assertTrue(fc.isSimpleColour());
238 assertEquals(Color.RED, fc.getColour());
241 * simple colour by hex code
243 fc = FeatureColour.parseJalviewFeatureColour(Format
244 .getHexString(Color.RED));
245 assertTrue(fc.isSimpleColour());
246 assertEquals(Color.RED, fc.getColour());
249 * simple colour by rgb triplet
251 fc = FeatureColour.parseJalviewFeatureColour("255,0,0");
252 assertTrue(fc.isSimpleColour());
253 assertEquals(Color.RED, fc.getColour());
260 fc = FeatureColour.parseJalviewFeatureColour("oops");
261 fail("expected exception");
262 } catch (IllegalArgumentException e)
264 assertEquals("Invalid colour descriptor: oops", e.getMessage());
268 * colour by label (no threshold)
270 fc = FeatureColour.parseJalviewFeatureColour("label");
271 assertTrue(fc.isColourByLabel());
272 assertFalse(fc.hasThreshold());
275 * colour by label (with threshold)
278 .parseJalviewFeatureColour("label|||0.0|0.0|above|12.0");
279 assertTrue(fc.isColourByLabel());
280 assertTrue(fc.isAboveThreshold());
281 assertEquals(12.0f, fc.getThreshold());
284 * graduated colour (by name) (no threshold)
286 fc = FeatureColour.parseJalviewFeatureColour("red|green|10.0|20.0");
287 assertTrue(fc.isGraduatedColour());
288 assertFalse(fc.hasThreshold());
289 assertEquals(Color.RED, fc.getMinColour());
290 assertEquals(Color.GREEN, fc.getMaxColour());
291 assertEquals(10f, fc.getMin());
292 assertEquals(20f, fc.getMax());
293 assertTrue(fc.isAutoScaled());
296 * graduated colour (by hex code) (above threshold)
298 String descriptor = String.format("%s|%s|10.0|20.0|above|15",
299 Format.getHexString(Color.RED),
300 Format.getHexString(Color.GREEN));
301 fc = FeatureColour.parseJalviewFeatureColour(descriptor);
302 assertTrue(fc.isGraduatedColour());
303 assertTrue(fc.hasThreshold());
304 assertTrue(fc.isAboveThreshold());
305 assertEquals(15f, fc.getThreshold());
306 assertEquals(Color.RED, fc.getMinColour());
307 assertEquals(Color.GREEN, fc.getMaxColour());
308 assertEquals(10f, fc.getMin());
309 assertEquals(20f, fc.getMax());
310 assertTrue(fc.isAutoScaled());
313 * graduated colour (by RGB triplet) (below threshold), absolute scale
315 descriptor = String.format("255,0,0|0,255,0|abso|10.0|20.0|below|15");
316 fc = FeatureColour.parseJalviewFeatureColour(descriptor);
317 assertTrue(fc.isGraduatedColour());
318 assertFalse(fc.isAutoScaled());
319 assertTrue(fc.hasThreshold());
320 assertTrue(fc.isBelowThreshold());
321 assertEquals(15f, fc.getThreshold());
322 assertEquals(Color.RED, fc.getMinColour());
323 assertEquals(Color.GREEN, fc.getMaxColour());
324 assertEquals(10f, fc.getMin());
325 assertEquals(20f, fc.getMax());
328 .format("blue|255,0,255|absolute|20.0|95.0|below|66.0");
329 fc = FeatureColour.parseJalviewFeatureColour(descriptor);
330 assertTrue(fc.isGraduatedColour());