X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=test%2Fjalview%2Fschemes%2FFeatureColourTest.java;h=1a6a5f342e8ec9bb8b1fda575657e243ccd12f33;hb=refs%2Fheads%2Fbug%2FJAL-3120restoreFeatureColour;hp=9d9d996e4fd75fe331353ed15de0c12cafcd9ef3;hpb=528c0f1815bc67b54618ad5b16c2162946974caf;p=jalview.git diff --git a/test/jalview/schemes/FeatureColourTest.java b/test/jalview/schemes/FeatureColourTest.java index 9d9d996..1a6a5f3 100644 --- a/test/jalview/schemes/FeatureColourTest.java +++ b/test/jalview/schemes/FeatureColourTest.java @@ -1,19 +1,77 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.schemes; import static org.testng.AssertJUnit.assertEquals; import static org.testng.AssertJUnit.assertFalse; +import static org.testng.AssertJUnit.assertNull; import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; +import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals; +import jalview.api.FeatureColourI; import jalview.datamodel.SequenceFeature; +import jalview.gui.JvOptionPane; +import jalview.util.ColorUtils; import jalview.util.Format; import java.awt.Color; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class FeatureColourTest { + + @BeforeClass(alwaysRun = true) + public void setUpJvOptionPane() + { + JvOptionPane.setInteractiveMode(false); + JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); + } + + @Test(groups = { "Functional" }) + public void testConstructors() + { + FeatureColourI fc = new FeatureColour(); + assertNull(fc.getColour()); + assertTrue(fc.isSimpleColour()); + assertFalse(fc.isColourByLabel()); + assertFalse(fc.isGraduatedColour()); + assertFalse(fc.isColourByAttribute()); + assertEquals(Color.white, fc.getMinColour()); + assertEquals(Color.black, fc.getMaxColour()); + + fc = new FeatureColour(Color.RED); + assertEquals(Color.red, fc.getColour()); + assertTrue(fc.isSimpleColour()); + assertFalse(fc.isColourByLabel()); + assertFalse(fc.isGraduatedColour()); + assertFalse(fc.isColourByAttribute()); + assertEquals(ColorUtils.bleachColour(Color.RED, 0.9f), + fc.getMinColour()); + assertEquals(Color.RED, fc.getMaxColour()); + + } + @Test(groups = { "Functional" }) public void testCopyConstructor() { @@ -25,20 +83,48 @@ public class FeatureColourTest assertTrue(fc1.getColour().equals(Color.RED)); assertFalse(fc1.isGraduatedColour()); assertFalse(fc1.isColourByLabel()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); /* * min-max colour */ - fc = new FeatureColour(Color.gray, Color.black, 10f, 20f); + fc = new FeatureColour(null, Color.gray, Color.black, Color.gray, 10f, + 20f); + fc.setAboveThreshold(true); + fc.setThreshold(12f); + fc1 = new FeatureColour(fc); + assertTrue(fc1.isGraduatedColour()); + assertFalse(fc1.isColourByLabel()); + assertTrue(fc1.isAboveThreshold()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); + assertEquals(12f, fc1.getThreshold()); + assertEquals(Color.gray, fc1.getMinColour()); + assertEquals(Color.black, fc1.getMaxColour()); + assertEquals(Color.gray, fc1.getNoColour()); + assertEquals(10f, fc1.getMin()); + assertEquals(20f, fc1.getMax()); + + /* + * min-max-noValue colour + */ + fc = new FeatureColour(Color.red, Color.gray, Color.black, Color.green, + 10f, 20f); fc.setAboveThreshold(true); fc.setThreshold(12f); fc1 = new FeatureColour(fc); assertTrue(fc1.isGraduatedColour()); assertFalse(fc1.isColourByLabel()); + assertFalse(fc1.isSimpleColour()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); assertTrue(fc1.isAboveThreshold()); assertEquals(12f, fc1.getThreshold()); assertEquals(Color.gray, fc1.getMinColour()); assertEquals(Color.black, fc1.getMaxColour()); + assertEquals(Color.green, fc1.getNoColour()); + assertEquals(Color.red, fc1.getColour()); assertEquals(10f, fc1.getMin()); assertEquals(20f, fc1.getMax()); @@ -50,63 +136,51 @@ public class FeatureColourTest fc1 = new FeatureColour(fc); assertTrue(fc1.isColourByLabel()); assertFalse(fc1.isGraduatedColour()); - } + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); - @Test(groups = { "Functional" }) - public void testIsColored_simpleColour() - { - FeatureColour fc = new FeatureColour(Color.RED); - assertTrue(fc.isColored(new SequenceFeature())); - } - - @Test(groups = { "Functional" }) - public void testIsColored_colourByLabel() - { - FeatureColour fc = new FeatureColour(); + /* + * colour by attribute (label) + */ + fc = new FeatureColour(); fc.setColourByLabel(true); - assertTrue(fc.isColored(new SequenceFeature())); - } - - @Test(groups = { "Functional" }) - public void testIsColored_aboveThreshold() - { - // graduated colour range from score 20 to 100 - FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 20f, - 100f); - - // score 0 is adjusted to bottom of range - SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 0f, - null); - assertTrue(fc.isColored(sf)); - assertEquals(Color.WHITE, fc.getColor(sf)); - - // score 120 is adjusted to top of range - sf.setScore(120f); - assertEquals(Color.BLACK, fc.getColor(sf)); - - // value below threshold is still rendered - // setting threshold has no effect yet... - fc.setThreshold(60f); - sf.setScore(36f); - assertTrue(fc.isColored(sf)); - assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + fc.setAttributeName("AF"); + fc1 = new FeatureColour(fc); + assertTrue(fc1.isColourByLabel()); + assertFalse(fc1.isGraduatedColour()); + assertTrue(fc1.isColourByAttribute()); + assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName()); - // now apply threshold: + /* + * colour by attribute (value) + */ + fc = new FeatureColour(Color.yellow, Color.gray, Color.black, + Color.green, 10f, 20f); fc.setAboveThreshold(true); - assertFalse(fc.isColored(sf)); - // colour is still returned though ?!? - assertEquals(new Color(204, 204, 204), fc.getColor(sf)); - - sf.setScore(84); // above threshold now - assertTrue(fc.isColored(sf)); - assertEquals(new Color(51, 51, 51), fc.getColor(sf)); + fc.setThreshold(12f); + fc.setAttributeName("AF"); + fc1 = new FeatureColour(fc); + assertTrue(fc1.isGraduatedColour()); + assertFalse(fc1.isColourByLabel()); + assertTrue(fc1.isColourByAttribute()); + assertFalse(fc1.isSimpleColour()); + assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName()); + assertTrue(fc1.isAboveThreshold()); + assertEquals(12f, fc1.getThreshold()); + assertEquals(Color.gray, fc1.getMinColour()); + assertEquals(Color.black, fc1.getMaxColour()); + assertEquals(Color.green, fc1.getNoColour()); + assertEquals(Color.yellow, fc1.getColour()); + assertEquals(10f, fc1.getMin()); + assertEquals(20f, fc1.getMax()); } @Test(groups = { "Functional" }) public void testGetColor_simpleColour() { FeatureColour fc = new FeatureColour(Color.RED); - assertEquals(Color.RED, fc.getColor(new SequenceFeature())); + assertEquals(Color.RED, + fc.getColor(new SequenceFeature("Cath", "", 1, 2, 0f, null))); } @Test(groups = { "Functional" }) @@ -116,16 +190,20 @@ public class FeatureColourTest fc.setColourByLabel(true); SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f, null); - Color expected = UserColourScheme.createColourFromName("desc"); + Color expected = ColorUtils.createColourFromName("desc"); assertEquals(expected, fc.getColor(sf)); } @Test(groups = { "Functional" }) public void testGetColor_Graduated() { - // graduated colour from score 0 to 100, gray(128, 128, 128) to red(255, 0, - // 0) - FeatureColour fc = new FeatureColour(Color.GRAY, Color.RED, 0f, 100f); + /* + * graduated colour from + * score 0 to 100 + * gray(128, 128, 128) to red(255, 0, 0) + */ + FeatureColour fc = new FeatureColour(null, Color.GRAY, Color.RED, null, + 0f, 100f); // feature score is 75 which is 3/4 of the way from GRAY to RED SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f, null); @@ -138,20 +216,35 @@ public class FeatureColourTest } @Test(groups = { "Functional" }) - public void testGetColor_belowThreshold() + public void testGetColor_aboveBelowThreshold() { // gradient from [50, 150] from WHITE(255, 255, 255) to BLACK(0, 0, 0) - FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 50f, - 150f); + FeatureColour fc = new FeatureColour(null, Color.WHITE, Color.BLACK, + Color.white, 50f, 150f); SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 70f, null); + + /* + * feature with score of Float.NaN is always assigned minimum colour + */ + SequenceFeature sf2 = new SequenceFeature("type", "desc", 0, 20, + Float.NaN, null); + fc.setThreshold(100f); // ignore for now - assertTrue(fc.isColored(sf)); assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + assertEquals(Color.white, fc.getColor(sf2)); fc.setAboveThreshold(true); // feature lies below threshold - assertFalse(fc.isColored(sf)); - assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + assertNull(fc.getColor(sf)); + assertEquals(Color.white, fc.getColor(sf2)); + + fc.setBelowThreshold(true); + fc.setThreshold(70f); + assertNull(fc.getColor(sf)); // feature score == threshold - hidden + assertEquals(Color.white, fc.getColor(sf2)); + fc.setThreshold(69f); + assertNull(fc.getColor(sf)); // feature score > threshold - hidden + assertEquals(Color.white, fc.getColor(sf2)); } /** @@ -176,20 +269,29 @@ public class FeatureColourTest assertEquals("domain\tlabel", fc.toJalviewFormat("domain")); /* + * colour by attribute text (no threshold) + */ + fc = new FeatureColour(); + fc.setColourByLabel(true); + fc.setAttributeName("CLIN_SIG"); + assertEquals("domain\tattribute|CLIN_SIG", fc.toJalviewFormat("domain")); + + /* * colour by label (autoscaled) (an odd state you can reach by selecting * 'above threshold', then deselecting 'threshold is min/max' then 'colour * by label') */ + fc.setAttributeName((String[]) null); fc.setAutoScaled(true); assertEquals("domain\tlabel", fc.toJalviewFormat("domain")); /* - * colour by label (above threshold) (min/max values are output though not - * used by this scheme) + * colour by label (above threshold) */ fc.setAutoScaled(false); fc.setThreshold(12.5f); fc.setAboveThreshold(true); + // min/max values are output though not used by this scheme assertEquals("domain\tlabel|||0.0|0.0|above|12.5", fc.toJalviewFormat("domain")); @@ -201,38 +303,83 @@ public class FeatureColourTest fc.toJalviewFormat("domain")); /* - * graduated colour, no threshold + * colour by attributes text (below threshold) */ - fc = new FeatureColour(Color.GREEN, Color.RED, 12f, 25f); + fc.setBelowThreshold(true); + fc.setAttributeName("CSQ", "Consequence"); + assertEquals("domain\tattribute|CSQ:Consequence|||0.0|0.0|below|12.5", + fc.toJalviewFormat("domain")); + + /* + * graduated colour by score, no threshold + * - default constructor sets noValueColor = minColor + */ + fc = new FeatureColour(null, Color.GREEN, Color.RED, Color.GREEN, 12f, + 25f); String greenHex = Format.getHexString(Color.GREEN); - String expected = String.format("domain\t%s|%s|abso|12.0|25.0|none", - greenHex, redHex); + String expected = String.format( + "domain\tscore|%s|%s|noValueMin|abso|12.0|25.0|none", greenHex, + redHex); + assertEquals(expected, fc.toJalviewFormat("domain")); + + /* + * graduated colour by score, no threshold, no value gets min colour + */ + fc = new FeatureColour(Color.RED, Color.GREEN, Color.RED, Color.GREEN, + 12f, 25f); + expected = String.format( + "domain\tscore|%s|%s|noValueMin|abso|12.0|25.0|none", greenHex, + redHex); assertEquals(expected, fc.toJalviewFormat("domain")); /* + * graduated colour by score, no threshold, no value gets max colour + */ + fc = new FeatureColour(Color.RED, Color.GREEN, Color.RED, Color.RED, + 12f, 25f); + expected = String.format( + "domain\tscore|%s|%s|noValueMax|abso|12.0|25.0|none", greenHex, + redHex); + assertEquals(expected, fc.toJalviewFormat("domain")); + + /* * colour ranges over the actual score ranges (not min/max) */ fc.setAutoScaled(true); - expected = String.format("domain\t%s|%s|12.0|25.0|none", greenHex, + expected = String.format( + "domain\tscore|%s|%s|noValueMax|12.0|25.0|none", greenHex, redHex); assertEquals(expected, fc.toJalviewFormat("domain")); /* - * graduated colour below threshold + * graduated colour by score, below threshold */ fc.setThreshold(12.5f); fc.setBelowThreshold(true); - expected = String.format("domain\t%s|%s|12.0|25.0|below|12.5", + expected = String.format( + "domain\tscore|%s|%s|noValueMax|12.0|25.0|below|12.5", greenHex, redHex); assertEquals(expected, fc.toJalviewFormat("domain")); /* - * graduated colour above threshold + * graduated colour by score, above threshold */ fc.setThreshold(12.5f); fc.setAboveThreshold(true); fc.setAutoScaled(false); - expected = String.format("domain\t%s|%s|abso|12.0|25.0|above|12.5", + expected = String.format( + "domain\tscore|%s|%s|noValueMax|abso|12.0|25.0|above|12.5", + greenHex, redHex); + assertEquals(expected, fc.toJalviewFormat("domain")); + + /* + * graduated colour by attribute, above threshold + */ + fc.setAttributeName("CSQ", "AF"); + fc.setAboveThreshold(true); + fc.setAutoScaled(false); + expected = String.format( + "domain\tattribute|CSQ:AF|%s|%s|noValueMax|abso|12.0|25.0|above|12.5", greenHex, redHex); assertEquals(expected, fc.toJalviewFormat("domain")); } @@ -246,7 +393,7 @@ public class FeatureColourTest /* * simple colour by name */ - FeatureColour fc = FeatureColour.parseJalviewFeatureColour("red"); + FeatureColourI fc = FeatureColour.parseJalviewFeatureColour("red"); assertTrue(fc.isSimpleColour()); assertEquals(Color.RED, fc.getColour()); @@ -294,7 +441,28 @@ public class FeatureColourTest assertEquals(12.0f, fc.getThreshold()); /* - * graduated colour (by name) (no threshold) + * colour by attribute text (no threshold) + */ + fc = FeatureColour.parseJalviewFeatureColour("attribute|CLIN_SIG"); + assertTrue(fc.isColourByAttribute()); + assertTrue(fc.isColourByLabel()); + assertFalse(fc.hasThreshold()); + assertArrayEquals(new String[] { "CLIN_SIG" }, fc.getAttributeName()); + + /* + * colour by attributes text (with score threshold) + */ + fc = FeatureColour.parseJalviewFeatureColour( + "attribute|CSQ:Consequence|||0.0|0.0|above|12.0"); + assertTrue(fc.isColourByLabel()); + assertTrue(fc.isColourByAttribute()); + assertArrayEquals(new String[] { "CSQ", "Consequence" }, + fc.getAttributeName()); + assertTrue(fc.isAboveThreshold()); + assertEquals(12.0f, fc.getThreshold()); + + /* + * graduated colour by score (with colour names) (no threshold) */ fc = FeatureColour.parseJalviewFeatureColour("red|green|10.0|20.0"); assertTrue(fc.isGraduatedColour()); @@ -306,7 +474,35 @@ public class FeatureColourTest assertTrue(fc.isAutoScaled()); /* - * graduated colour (by hex code) (above threshold) + * graduated colour (explicitly by 'score') (no threshold) + */ + fc = FeatureColour + .parseJalviewFeatureColour("Score|red|green|10.0|20.0"); + assertTrue(fc.isGraduatedColour()); + assertFalse(fc.hasThreshold()); + assertEquals(Color.RED, fc.getMinColour()); + assertEquals(Color.GREEN, fc.getMaxColour()); + assertEquals(10f, fc.getMin()); + assertEquals(20f, fc.getMax()); + assertTrue(fc.isAutoScaled()); + + /* + * graduated colour by attribute (no threshold) + */ + fc = FeatureColour + .parseJalviewFeatureColour("attribute|AF|red|green|10.0|20.0"); + assertTrue(fc.isGraduatedColour()); + assertTrue(fc.isColourByAttribute()); + assertArrayEquals(new String[] { "AF" }, fc.getAttributeName()); + assertFalse(fc.hasThreshold()); + assertEquals(Color.RED, fc.getMinColour()); + assertEquals(Color.GREEN, fc.getMaxColour()); + assertEquals(10f, fc.getMin()); + assertEquals(20f, fc.getMax()); + assertTrue(fc.isAutoScaled()); + + /* + * graduated colour by score (colours by hex code) (above threshold) */ String descriptor = String.format("%s|%s|10.0|20.0|above|15", Format.getHexString(Color.RED), @@ -323,9 +519,26 @@ public class FeatureColourTest assertTrue(fc.isAutoScaled()); /* + * graduated colour by attributes (below threshold) + */ + fc = FeatureColour.parseJalviewFeatureColour( + "attribute|CSQ:AF|red|green|10.0|20.0|below|13"); + assertTrue(fc.isGraduatedColour()); + assertTrue(fc.isColourByAttribute()); + assertArrayEquals(new String[] { "CSQ", "AF" }, fc.getAttributeName()); + assertTrue(fc.hasThreshold()); + assertTrue(fc.isBelowThreshold()); + assertEquals(13f, fc.getThreshold()); + assertEquals(Color.RED, fc.getMinColour()); + assertEquals(Color.GREEN, fc.getMaxColour()); + assertEquals(10f, fc.getMin()); + assertEquals(20f, fc.getMax()); + assertTrue(fc.isAutoScaled()); + + /* * graduated colour (by RGB triplet) (below threshold), absolute scale */ - descriptor = String.format("255,0,0|0,255,0|abso|10.0|20.0|below|15"); + descriptor = "255,0,0|0,255,0|abso|10.0|20.0|below|15"; fc = FeatureColour.parseJalviewFeatureColour(descriptor); assertTrue(fc.isGraduatedColour()); assertFalse(fc.isAutoScaled()); @@ -337,9 +550,64 @@ public class FeatureColourTest assertEquals(10f, fc.getMin()); assertEquals(20f, fc.getMax()); - descriptor = String - .format("blue|255,0,255|absolute|20.0|95.0|below|66.0"); + descriptor = "blue|255,0,255|absolute|20.0|95.0|below|66.0"; fc = FeatureColour.parseJalviewFeatureColour(descriptor); assertTrue(fc.isGraduatedColour()); } + + @Test(groups = { "Functional" }) + public void testGetColor_colourByAttributeText() + { + FeatureColour fc = new FeatureColour(); + fc.setColourByLabel(true); + fc.setAttributeName("consequence"); + SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 1f, + null); + + /* + * if feature has no such attribute, use 'no value' colour + */ + assertEquals(FeatureColour.DEFAULT_NO_COLOUR, fc.getColor(sf)); + + /* + * if feature has attribute, generate colour from value + */ + sf.setValue("consequence", "benign"); + Color expected = ColorUtils.createColourFromName("benign"); + assertEquals(expected, fc.getColor(sf)); + } + + @Test(groups = { "Functional" }) + public void testGetColor_GraduatedByAttributeValue() + { + /* + * graduated colour based on attribute value for AF + * given a min-max range of 0-100 + */ + FeatureColour fc = new FeatureColour(Color.white, + new Color(50, 100, 150), new Color(150, 200, 250), Color.yellow, + 0f, 100f); + String attName = "AF"; + fc.setAttributeName(attName); + + /* + * first case: feature lacks the attribute - use 'no value' colour + */ + SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f, + null); + assertEquals(Color.yellow, fc.getColor(sf)); + + /* + * second case: attribute present but not numeric - treat as if absent + */ + sf.setValue(attName, "twelve"); + assertEquals(Color.yellow, fc.getColor(sf)); + + /* + * third case: valid attribute value + */ + sf.setValue(attName, "20.0"); + Color expected = new Color(70, 120, 170); + assertEquals(expected, fc.getColor(sf)); + } }