Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.schemes

File FeatureColourTest.java

 

Code metrics

0
353
12
1
763
469
13
0.04
29.42
12
1.08

Classes

Class Line # Actions
FeatureColourTest 43 353 13 1
0.997260399.7%
 

Contributing tests

This file is covered by 11 tests. .

Source view

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.schemes;
22   
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;
28    import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
29   
30    import jalview.api.FeatureColourI;
31    import jalview.datamodel.SequenceFeature;
32    import jalview.gui.JvOptionPane;
33    import jalview.util.ColorUtils;
34    import jalview.util.Format;
35   
36    import java.awt.Color;
37   
38    import org.testng.annotations.BeforeClass;
39    import org.testng.annotations.Test;
40   
41    import junit.extensions.PA;
42   
 
43    public class FeatureColourTest
44    {
45   
 
46  1 toggle @BeforeClass(alwaysRun = true)
47    public void setUpJvOptionPane()
48    {
49  1 JvOptionPane.setInteractiveMode(false);
50  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51    }
52   
 
53  1 toggle @Test(groups = { "Functional" })
54    public void testCopyConstructor()
55    {
56    /*
57    * plain colour
58    */
59  1 FeatureColour fc = new FeatureColour(Color.RED);
60  1 FeatureColour fc1 = new FeatureColour(fc);
61  1 assertTrue(fc1.getColour().equals(Color.RED));
62  1 assertFalse(fc1.isGraduatedColour());
63  1 assertFalse(fc1.isColourByLabel());
64  1 assertFalse(fc1.isColourByAttribute());
65  1 assertNull(fc1.getAttributeName());
66   
67    /*
68    * min-max colour
69    */
70  1 fc = new FeatureColour(Color.gray, Color.black, 10f, 20f);
71  1 fc.setAboveThreshold(true);
72  1 fc.setThreshold(12f);
73  1 fc1 = new FeatureColour(fc);
74  1 assertTrue(fc1.isGraduatedColour());
75  1 assertFalse(fc1.isColourByLabel());
76  1 assertTrue(fc1.isAboveThreshold());
77  1 assertFalse(fc1.isColourByAttribute());
78  1 assertNull(fc1.getAttributeName());
79  1 assertEquals(12f, fc1.getThreshold());
80  1 assertEquals(Color.gray, fc1.getMinColour());
81  1 assertEquals(Color.black, fc1.getMaxColour());
82  1 assertEquals(Color.gray, fc1.getNoColour());
83  1 assertEquals(10f, fc1.getMin());
84  1 assertEquals(20f, fc1.getMax());
85   
86    /*
87    * min-max-noValue colour
88    */
89  1 fc = new FeatureColour(Color.gray, Color.black, Color.green, 10f, 20f);
90  1 fc.setAboveThreshold(true);
91  1 fc.setThreshold(12f);
92  1 fc1 = new FeatureColour(fc);
93  1 assertTrue(fc1.isGraduatedColour());
94  1 assertFalse(fc1.isColourByLabel());
95  1 assertFalse(fc1.isColourByAttribute());
96  1 assertNull(fc1.getAttributeName());
97  1 assertTrue(fc1.isAboveThreshold());
98  1 assertEquals(12f, fc1.getThreshold());
99  1 assertEquals(Color.gray, fc1.getMinColour());
100  1 assertEquals(Color.black, fc1.getMaxColour());
101  1 assertEquals(Color.green, fc1.getNoColour());
102  1 assertEquals(10f, fc1.getMin());
103  1 assertEquals(20f, fc1.getMax());
104   
105    /*
106    * colour by label
107    */
108  1 fc = new FeatureColour();
109  1 fc.setColourByLabel(true);
110  1 fc1 = new FeatureColour(fc);
111  1 assertTrue(fc1.isColourByLabel());
112  1 assertFalse(fc1.isGraduatedColour());
113  1 assertFalse(fc1.isColourByAttribute());
114  1 assertNull(fc1.getAttributeName());
115   
116    /*
117    * colour by attribute (label)
118    */
119  1 fc = new FeatureColour();
120  1 fc.setColourByLabel(true);
121  1 fc.setAttributeName("AF");
122  1 fc1 = new FeatureColour(fc);
123  1 assertTrue(fc1.isColourByLabel());
124  1 assertFalse(fc1.isGraduatedColour());
125  1 assertTrue(fc1.isColourByAttribute());
126  1 assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName());
127   
128    /*
129    * colour by attribute (value)
130    */
131  1 fc = new FeatureColour(Color.gray, Color.black, Color.green, 10f, 20f);
132  1 fc.setAboveThreshold(true);
133  1 fc.setThreshold(12f);
134  1 fc.setAttributeName("AF");
135  1 fc1 = new FeatureColour(fc);
136  1 assertTrue(fc1.isGraduatedColour());
137  1 assertFalse(fc1.isColourByLabel());
138  1 assertTrue(fc1.isColourByAttribute());
139  1 assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName());
140  1 assertTrue(fc1.isAboveThreshold());
141  1 assertEquals(12f, fc1.getThreshold());
142  1 assertEquals(Color.gray, fc1.getMinColour());
143  1 assertEquals(Color.black, fc1.getMaxColour());
144  1 assertEquals(Color.green, fc1.getNoColour());
145  1 assertEquals(10f, fc1.getMin());
146  1 assertEquals(20f, fc1.getMax());
147    }
148   
 
149  1 toggle @Test(groups = { "Functional" })
150    public void testCopyConstructor_minMax()
151    {
152    /*
153    * graduated colour
154    */
155  1 FeatureColour fc = new FeatureColour(Color.BLUE, Color.RED, 1f, 5f);
156  1 assertTrue(fc.isGraduatedColour());
157  1 assertFalse(fc.isColourByLabel());
158  1 assertFalse(fc.isColourByAttribute());
159  1 assertNull(fc.getAttributeName());
160  1 assertEquals(1f, fc.getMin());
161  1 assertEquals(5f, fc.getMax());
162   
163    /*
164    * update min-max bounds
165    */
166  1 FeatureColour fc1 = new FeatureColour(fc, 2f, 6f);
167  1 assertTrue(fc1.isGraduatedColour());
168  1 assertFalse(fc1.isColourByLabel());
169  1 assertFalse(fc1.isColourByAttribute());
170  1 assertNull(fc1.getAttributeName());
171  1 assertEquals(2f, fc1.getMin());
172  1 assertEquals(6f, fc1.getMax());
173  1 assertFalse((boolean) PA.getValue(fc1, "isHighToLow"));
174   
175    /*
176    * update min-max bounds - high to low
177    */
178  1 fc1 = new FeatureColour(fc, 23f, 16f);
179  1 assertTrue(fc1.isGraduatedColour());
180  1 assertFalse(fc1.isColourByLabel());
181  1 assertFalse(fc1.isColourByAttribute());
182  1 assertNull(fc1.getAttributeName());
183  1 assertEquals(23f, fc1.getMin());
184  1 assertEquals(16f, fc1.getMax());
185  1 assertTrue((boolean) PA.getValue(fc1, "isHighToLow"));
186   
187    /*
188    * graduated colour by attribute
189    */
190  1 fc1.setAttributeName("AF");
191  1 fc1 = new FeatureColour(fc1, 13f, 36f);
192  1 assertTrue(fc1.isGraduatedColour());
193  1 assertFalse(fc1.isColourByLabel());
194  1 assertTrue(fc1.isColourByAttribute());
195  1 assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName());
196  1 assertEquals(13f, fc1.getMin());
197  1 assertEquals(36f, fc1.getMax());
198  1 assertFalse((boolean) PA.getValue(fc1, "isHighToLow"));
199   
200    /*
201    * colour by label
202    */
203  1 fc = new FeatureColour(Color.BLUE, Color.RED, 1f, 5f);
204  1 fc.setColourByLabel(true);
205  1 assertFalse(fc.isGraduatedColour());
206  1 assertTrue(fc.isColourByLabel());
207  1 assertFalse(fc.isColourByAttribute());
208  1 assertNull(fc.getAttributeName());
209  1 assertEquals(1f, fc.getMin());
210  1 assertEquals(5f, fc.getMax());
211   
212    /*
213    * update min-max bounds
214    */
215  1 fc1 = new FeatureColour(fc, 2f, 6f);
216  1 assertFalse(fc1.isGraduatedColour());
217  1 assertTrue(fc1.isColourByLabel());
218  1 assertFalse(fc1.isColourByAttribute());
219  1 assertNull(fc1.getAttributeName());
220  1 assertEquals(2f, fc1.getMin());
221  1 assertEquals(6f, fc1.getMax());
222   
223    /*
224    * colour by attribute text
225    */
226  1 fc1.setAttributeName("AC");
227  1 fc1 = new FeatureColour(fc1, 13f, 36f);
228  1 assertFalse(fc1.isGraduatedColour());
229  1 assertTrue(fc1.isColourByLabel());
230  1 assertTrue(fc1.isColourByAttribute());
231  1 assertArrayEquals(new String[] { "AC" }, fc1.getAttributeName());
232  1 assertEquals(13f, fc1.getMin());
233  1 assertEquals(36f, fc1.getMax());
234    }
235   
 
236  1 toggle @Test(groups = { "Functional" })
237    public void testGetColor_simpleColour()
238    {
239  1 FeatureColour fc = new FeatureColour(Color.RED);
240  1 assertEquals(Color.RED,
241    fc.getColor(new SequenceFeature("Cath", "", 1, 2, 0f, null)));
242    }
243   
 
244  1 toggle @Test(groups = { "Functional" })
245    public void testGetColor_colourByLabel()
246    {
247  1 FeatureColour fc = new FeatureColour();
248  1 fc.setColourByLabel(true);
249  1 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f,
250    null);
251  1 Color expected = ColorUtils.createColourFromName("desc");
252  1 assertEquals(expected, fc.getColor(sf));
253    }
254   
 
255  1 toggle @Test(groups = { "Functional" })
256    public void testGetColor_Graduated()
257    {
258    /*
259    * graduated colour from
260    * score 0 to 100
261    * gray(128, 128, 128) to red(255, 0, 0)
262    */
263  1 FeatureColour fc = new FeatureColour(Color.GRAY, Color.RED, 0f, 100f);
264    // feature score is 75 which is 3/4 of the way from GRAY to RED
265  1 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f,
266    null);
267    // the colour gradient is computed in float values from 0-1 (where 1 == 255)
268  1 float red = 128 / 255f + 3 / 4f * (255 - 128) / 255f;
269  1 float green = 128 / 255f + 3 / 4f * (0 - 128) / 255f;
270  1 float blue = 128 / 255f + 3 / 4f * (0 - 128) / 255f;
271  1 Color expected = new Color(red, green, blue);
272  1 assertEquals(expected, fc.getColor(sf));
273    }
274   
 
275  1 toggle @Test(groups = { "Functional" })
276    public void testGetColor_aboveBelowThreshold()
277    {
278    // gradient from [50, 150] from WHITE(255, 255, 255) to BLACK(0, 0, 0)
279  1 FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 50f,
280    150f);
281  1 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 70f,
282    null);
283   
284    /*
285    * feature with score of Float.NaN is always assigned minimum colour
286    */
287  1 SequenceFeature sf2 = new SequenceFeature("type", "desc", 0, 20,
288    Float.NaN, null);
289   
290  1 fc.setThreshold(100f); // ignore for now
291  1 assertEquals(new Color(204, 204, 204), fc.getColor(sf));
292  1 assertEquals(Color.white, fc.getColor(sf2));
293   
294  1 fc.setAboveThreshold(true); // feature lies below threshold
295  1 assertNull(fc.getColor(sf));
296  1 assertEquals(Color.white, fc.getColor(sf2));
297   
298  1 fc.setBelowThreshold(true);
299  1 fc.setThreshold(70f);
300  1 assertNull(fc.getColor(sf)); // feature score == threshold - hidden
301  1 assertEquals(Color.white, fc.getColor(sf2));
302  1 fc.setThreshold(69f);
303  1 assertNull(fc.getColor(sf)); // feature score > threshold - hidden
304  1 assertEquals(Color.white, fc.getColor(sf2));
305    }
306   
307    /**
308    * Test output of feature colours to Jalview features file format
309    */
 
310  1 toggle @Test(groups = { "Functional" })
311    public void testToJalviewFormat()
312    {
313    /*
314    * plain colour - to RGB hex code
315    */
316  1 FeatureColour fc = new FeatureColour(Color.RED);
317  1 String redHex = Format.getHexString(Color.RED);
318  1 String hexColour = redHex;
319  1 assertEquals("domain\t" + hexColour, fc.toJalviewFormat("domain"));
320   
321    /*
322    * colour by label (no threshold)
323    */
324  1 fc = new FeatureColour();
325  1 fc.setColourByLabel(true);
326  1 assertEquals("domain\tlabel", fc.toJalviewFormat("domain"));
327   
328    /*
329    * colour by attribute text (no threshold)
330    */
331  1 fc = new FeatureColour();
332  1 fc.setColourByLabel(true);
333  1 fc.setAttributeName("CLIN_SIG");
334  1 assertEquals("domain\tattribute|CLIN_SIG", fc.toJalviewFormat("domain"));
335   
336    /*
337    * colour by label (autoscaled) (an odd state you can reach by selecting
338    * 'above threshold', then deselecting 'threshold is min/max' then 'colour
339    * by label')
340    */
341  1 fc.setAttributeName((String[]) null);
342  1 fc.setAutoScaled(true);
343  1 assertEquals("domain\tlabel", fc.toJalviewFormat("domain"));
344   
345    /*
346    * colour by label (above threshold)
347    */
348  1 fc.setAutoScaled(false);
349  1 fc.setThreshold(12.5f);
350  1 fc.setAboveThreshold(true);
351    // min/max values are output though not used by this scheme
352  1 assertEquals("domain\tlabel|||0.0|0.0|above|12.5",
353    fc.toJalviewFormat("domain"));
354   
355    /*
356    * colour by label (below threshold)
357    */
358  1 fc.setBelowThreshold(true);
359  1 assertEquals("domain\tlabel|||0.0|0.0|below|12.5",
360    fc.toJalviewFormat("domain"));
361   
362    /*
363    * colour by attributes text (below threshold)
364    */
365  1 fc.setBelowThreshold(true);
366  1 fc.setAttributeName("CSQ", "Consequence");
367  1 assertEquals("domain\tattribute|CSQ:Consequence|||0.0|0.0|below|12.5",
368    fc.toJalviewFormat("domain"));
369   
370    /*
371    * graduated colour by score, no threshold
372    * - default constructor sets noValueColor = minColor
373    */
374  1 fc = new FeatureColour(Color.GREEN, Color.RED, 12f, 25f);
375  1 String greenHex = Format.getHexString(Color.GREEN);
376  1 String expected = String.format(
377    "domain\tscore|%s|%s|noValueMin|abso|12.0|25.0|none", greenHex,
378    redHex);
379  1 assertEquals(expected, fc.toJalviewFormat("domain"));
380   
381    /*
382    * graduated colour by score, no threshold, no value gets min colour
383    */
384  1 fc = new FeatureColour(Color.GREEN, Color.RED, Color.GREEN, 12f, 25f);
385  1 expected = String.format(
386    "domain\tscore|%s|%s|noValueMin|abso|12.0|25.0|none", greenHex,
387    redHex);
388  1 assertEquals(expected, fc.toJalviewFormat("domain"));
389   
390    /*
391    * graduated colour by score, no threshold, no value gets max colour
392    */
393  1 fc = new FeatureColour(Color.GREEN, Color.RED, Color.RED, 12f, 25f);
394  1 expected = String.format(
395    "domain\tscore|%s|%s|noValueMax|abso|12.0|25.0|none", greenHex,
396    redHex);
397  1 assertEquals(expected, fc.toJalviewFormat("domain"));
398   
399    /*
400    * colour ranges over the actual score ranges (not min/max)
401    */
402  1 fc.setAutoScaled(true);
403  1 expected = String.format(
404    "domain\tscore|%s|%s|noValueMax|12.0|25.0|none", greenHex,
405    redHex);
406  1 assertEquals(expected, fc.toJalviewFormat("domain"));
407   
408    /*
409    * graduated colour by score, below threshold
410    */
411  1 fc.setThreshold(12.5f);
412  1 fc.setBelowThreshold(true);
413  1 expected = String.format(
414    "domain\tscore|%s|%s|noValueMax|12.0|25.0|below|12.5",
415    greenHex, redHex);
416  1 assertEquals(expected, fc.toJalviewFormat("domain"));
417   
418    /*
419    * graduated colour by score, above threshold
420    */
421  1 fc.setThreshold(12.5f);
422  1 fc.setAboveThreshold(true);
423  1 fc.setAutoScaled(false);
424  1 expected = String.format(
425    "domain\tscore|%s|%s|noValueMax|abso|12.0|25.0|above|12.5",
426    greenHex, redHex);
427  1 assertEquals(expected, fc.toJalviewFormat("domain"));
428   
429    /*
430    * graduated colour by attribute, above threshold
431    */
432  1 fc.setAttributeName("CSQ", "AF");
433  1 fc.setAboveThreshold(true);
434  1 fc.setAutoScaled(false);
435  1 expected = String.format(
436    "domain\tattribute|CSQ:AF|%s|%s|noValueMax|abso|12.0|25.0|above|12.5",
437    greenHex, redHex);
438  1 assertEquals(expected, fc.toJalviewFormat("domain"));
439    }
440   
441    /**
442    * Test parsing of feature colours from Jalview features file format
443    */
 
444  1 toggle @Test(groups = { "Functional" })
445    public void testParseJalviewFeatureColour()
446    {
447    /*
448    * simple colour by name
449    */
450  1 FeatureColourI fc = FeatureColour.parseJalviewFeatureColour("red");
451  1 assertTrue(fc.isSimpleColour());
452  1 assertEquals(Color.RED, fc.getColour());
453   
454    /*
455    * simple colour by hex code
456    */
457  1 fc = FeatureColour.parseJalviewFeatureColour(Format
458    .getHexString(Color.RED));
459  1 assertTrue(fc.isSimpleColour());
460  1 assertEquals(Color.RED, fc.getColour());
461   
462    /*
463    * simple colour by rgb triplet
464    */
465  1 fc = FeatureColour.parseJalviewFeatureColour("255,0,0");
466  1 assertTrue(fc.isSimpleColour());
467  1 assertEquals(Color.RED, fc.getColour());
468   
469    /*
470    * malformed colour
471    */
472  1 try
473    {
474  1 fc = FeatureColour.parseJalviewFeatureColour("oops");
475  0 fail("expected exception");
476    } catch (IllegalArgumentException e)
477    {
478  1 assertEquals("Invalid colour descriptor: oops", e.getMessage());
479    }
480   
481    /*
482    * colour by label (no threshold)
483    */
484  1 fc = FeatureColour.parseJalviewFeatureColour("label");
485  1 assertTrue(fc.isColourByLabel());
486  1 assertFalse(fc.hasThreshold());
487   
488    /*
489    * colour by label (with threshold)
490    */
491  1 fc = FeatureColour
492    .parseJalviewFeatureColour("label|||0.0|0.0|above|12.0");
493  1 assertTrue(fc.isColourByLabel());
494  1 assertTrue(fc.isAboveThreshold());
495  1 assertEquals(12.0f, fc.getThreshold());
496   
497    /*
498    * colour by attribute text (no threshold)
499    */
500  1 fc = FeatureColour.parseJalviewFeatureColour("attribute|CLIN_SIG");
501  1 assertTrue(fc.isColourByAttribute());
502  1 assertTrue(fc.isColourByLabel());
503  1 assertFalse(fc.hasThreshold());
504  1 assertArrayEquals(new String[] { "CLIN_SIG" }, fc.getAttributeName());
505   
506    /*
507    * colour by attributes text (with score threshold)
508    */
509  1 fc = FeatureColour.parseJalviewFeatureColour(
510    "attribute|CSQ:Consequence|||0.0|0.0|above|12.0");
511  1 assertTrue(fc.isColourByLabel());
512  1 assertTrue(fc.isColourByAttribute());
513  1 assertArrayEquals(new String[] { "CSQ", "Consequence" },
514    fc.getAttributeName());
515  1 assertTrue(fc.isAboveThreshold());
516  1 assertEquals(12.0f, fc.getThreshold());
517   
518    /*
519    * graduated colour by score (with colour names) (no threshold)
520    */
521  1 fc = FeatureColour.parseJalviewFeatureColour("red|green|10.0|20.0");
522  1 assertTrue(fc.isGraduatedColour());
523  1 assertFalse(fc.hasThreshold());
524  1 assertEquals(Color.RED, fc.getMinColour());
525  1 assertEquals(Color.GREEN, fc.getMaxColour());
526  1 assertEquals(10f, fc.getMin());
527  1 assertEquals(20f, fc.getMax());
528  1 assertTrue(fc.isAutoScaled());
529   
530    /*
531    * graduated colour (explicitly by 'score') (no threshold)
532    */
533  1 fc = FeatureColour
534    .parseJalviewFeatureColour("Score|red|green|10.0|20.0");
535  1 assertTrue(fc.isGraduatedColour());
536  1 assertFalse(fc.hasThreshold());
537  1 assertEquals(Color.RED, fc.getMinColour());
538  1 assertEquals(Color.GREEN, fc.getMaxColour());
539  1 assertEquals(10f, fc.getMin());
540  1 assertEquals(20f, fc.getMax());
541  1 assertTrue(fc.isAutoScaled());
542   
543    /*
544    * graduated colour by attribute (no threshold)
545    */
546  1 fc = FeatureColour
547    .parseJalviewFeatureColour("attribute|AF|red|green|10.0|20.0");
548  1 assertTrue(fc.isGraduatedColour());
549  1 assertTrue(fc.isColourByAttribute());
550  1 assertArrayEquals(new String[] { "AF" }, fc.getAttributeName());
551  1 assertFalse(fc.hasThreshold());
552  1 assertEquals(Color.RED, fc.getMinColour());
553  1 assertEquals(Color.GREEN, fc.getMaxColour());
554  1 assertEquals(10f, fc.getMin());
555  1 assertEquals(20f, fc.getMax());
556  1 assertTrue(fc.isAutoScaled());
557   
558    /*
559    * graduated colour by score (colours by hex code) (above threshold)
560    */
561  1 String descriptor = String.format("%s|%s|10.0|20.0|above|15",
562    Format.getHexString(Color.RED),
563    Format.getHexString(Color.GREEN));
564  1 fc = FeatureColour.parseJalviewFeatureColour(descriptor);
565  1 assertTrue(fc.isGraduatedColour());
566  1 assertTrue(fc.hasThreshold());
567  1 assertTrue(fc.isAboveThreshold());
568  1 assertEquals(15f, fc.getThreshold());
569  1 assertEquals(Color.RED, fc.getMinColour());
570  1 assertEquals(Color.GREEN, fc.getMaxColour());
571  1 assertEquals(10f, fc.getMin());
572  1 assertEquals(20f, fc.getMax());
573  1 assertTrue(fc.isAutoScaled());
574   
575    /*
576    * graduated colour by attributes (below threshold)
577    */
578  1 fc = FeatureColour.parseJalviewFeatureColour(
579    "attribute|CSQ:AF|red|green|10.0|20.0|below|13");
580  1 assertTrue(fc.isGraduatedColour());
581  1 assertTrue(fc.isColourByAttribute());
582  1 assertArrayEquals(new String[] { "CSQ", "AF" }, fc.getAttributeName());
583  1 assertTrue(fc.hasThreshold());
584  1 assertTrue(fc.isBelowThreshold());
585  1 assertEquals(13f, fc.getThreshold());
586  1 assertEquals(Color.RED, fc.getMinColour());
587  1 assertEquals(Color.GREEN, fc.getMaxColour());
588  1 assertEquals(10f, fc.getMin());
589  1 assertEquals(20f, fc.getMax());
590  1 assertTrue(fc.isAutoScaled());
591   
592    /*
593    * graduated colour (by RGB triplet) (below threshold), absolute scale
594    */
595  1 descriptor = "255,0,0|0,255,0|abso|10.0|20.0|below|15";
596  1 fc = FeatureColour.parseJalviewFeatureColour(descriptor);
597  1 assertTrue(fc.isGraduatedColour());
598  1 assertFalse(fc.isAutoScaled());
599  1 assertTrue(fc.hasThreshold());
600  1 assertTrue(fc.isBelowThreshold());
601  1 assertEquals(15f, fc.getThreshold());
602  1 assertEquals(Color.RED, fc.getMinColour());
603  1 assertEquals(Color.GREEN, fc.getMaxColour());
604  1 assertEquals(10f, fc.getMin());
605  1 assertEquals(20f, fc.getMax());
606   
607  1 descriptor = "blue|255,0,255|absolute|20.0|95.0|below|66.0";
608  1 fc = FeatureColour.parseJalviewFeatureColour(descriptor);
609  1 assertTrue(fc.isGraduatedColour());
610    }
611   
 
612  1 toggle @Test(groups = { "Functional" })
613    public void testGetColor_colourByAttributeText()
614    {
615  1 FeatureColour fc = new FeatureColour();
616  1 fc.setColourByLabel(true);
617  1 fc.setAttributeName("consequence");
618  1 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f,
619    null);
620   
621    /*
622    * if feature has no such attribute, use 'no value' colour
623    */
624  1 assertEquals(FeatureColour.DEFAULT_NO_COLOUR, fc.getColor(sf));
625   
626    /*
627    * if feature has attribute, generate colour from value
628    */
629  1 sf.setValue("consequence", "benign");
630  1 Color expected = ColorUtils.createColourFromName("benign");
631  1 assertEquals(expected, fc.getColor(sf));
632    }
633   
 
634  1 toggle @Test(groups = { "Functional" })
635    public void testGetColor_GraduatedByAttributeValue()
636    {
637    /*
638    * graduated colour based on attribute value for AF
639    * given a min-max range of 0-100
640    */
641  1 FeatureColour fc = new FeatureColour(new Color(50, 100, 150),
642    new Color(150, 200, 250), Color.yellow, 0f, 100f);
643  1 String attName = "AF";
644  1 fc.setAttributeName(attName);
645   
646    /*
647    * first case: feature lacks the attribute - use 'no value' colour
648    */
649  1 SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f,
650    null);
651  1 assertEquals(Color.yellow, fc.getColor(sf));
652   
653    /*
654    * second case: attribute present but not numeric - treat as if absent
655    */
656  1 sf.setValue(attName, "twelve");
657  1 assertEquals(Color.yellow, fc.getColor(sf));
658   
659    /*
660    * third case: valid attribute value
661    */
662  1 sf.setValue(attName, "20.0");
663  1 Color expected = new Color(70, 120, 170);
664  1 assertEquals(expected, fc.getColor(sf));
665    }
666   
667    /**
668    * Test description of feature colour suitable for a tooltip
669    */
 
670  1 toggle @Test(groups = { "Functional" })
671    public void testGetDescription()
672    {
673    /*
674    * plain colour
675    */
676  1 FeatureColour fc = new FeatureColour(Color.RED);
677  1 assertEquals(
678    String.format("r=%d,g=%d,b=%d", Color.RED.getRed(),
679    Color.red.getGreen(), Color.red.getBlue()),
680    fc.getDescription());
681   
682    /*
683    * colour by label (no threshold)
684    */
685  1 fc = new FeatureColour();
686  1 fc.setColourByLabel(true);
687  1 assertEquals("By Label", fc.getDescription());
688   
689    /*
690    * colour by attribute text (no threshold)
691    */
692  1 fc = new FeatureColour();
693  1 fc.setColourByLabel(true);
694  1 fc.setAttributeName("CLIN_SIG");
695  1 assertEquals("By CLIN_SIG", fc.getDescription());
696   
697    /*
698    * colour by label (above score threshold)
699    */
700  1 fc = new FeatureColour();
701  1 fc.setColourByLabel(true);
702  1 fc.setAutoScaled(false);
703  1 fc.setThreshold(12.5f);
704  1 fc.setAboveThreshold(true);
705  1 assertEquals("By Label (Score > 12.5)",
706    fc.getDescription());
707   
708    /*
709    * colour by label (below score threshold)
710    */
711  1 fc.setBelowThreshold(true);
712  1 assertEquals("By Label (Score < 12.5)",
713    fc.getDescription());
714   
715    /*
716    * colour by attributes text (below score threshold)
717    */
718  1 fc.setBelowThreshold(true);
719  1 fc.setAttributeName("CSQ", "Consequence");
720  1 assertEquals(
721    "By CSQ:Consequence (Score < 12.5)",
722    fc.getDescription());
723   
724    /*
725    * graduated colour by score, no threshold
726    */
727  1 fc = new FeatureColour(Color.GREEN, Color.RED, 12f, 25f);
728  1 assertEquals("By Score", fc.getDescription());
729   
730    /*
731    * graduated colour by score, below threshold
732    */
733  1 fc.setThreshold(12.5f);
734  1 fc.setBelowThreshold(true);
735  1 assertEquals("By Score (< 12.5)",
736    fc.getDescription());
737   
738    /*
739    * graduated colour by score, above threshold
740    */
741  1 fc.setThreshold(12.5f);
742  1 fc.setAboveThreshold(true);
743  1 fc.setAutoScaled(false);
744  1 assertEquals("By Score (> 12.5)",
745    fc.getDescription());
746   
747    /*
748    * graduated colour by attribute, no threshold
749    */
750  1 fc.setAttributeName("CSQ", "AF");
751  1 fc.setAboveThreshold(false);
752  1 fc.setAutoScaled(false);
753  1 assertEquals("By CSQ:AF", fc.getDescription());
754   
755    /*
756    * graduated colour by attribute, above threshold
757    */
758  1 fc.setAboveThreshold(true);
759  1 fc.setAutoScaled(false);
760  1 assertEquals("By CSQ:AF (> 12.5)",
761    fc.getDescription());
762    }
763    }