X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fjalview%2Fschemes%2FFeatureColourTest.java;h=3f60152f2e1b433d4c9ead8b02d3e579f05134b8;hb=14307f5cfcbc90c419c892434613a500ca550ecc;hp=822fcd4eb98cf4956616ca8edc5f2ea08c9ee854;hpb=e957648a02a9e25795a92a4f39a5e3c1e7adb230;p=jalview.git diff --git a/test/jalview/schemes/FeatureColourTest.java b/test/jalview/schemes/FeatureColourTest.java index 822fcd4..3f60152 100644 --- a/test/jalview/schemes/FeatureColourTest.java +++ b/test/jalview/schemes/FeatureColourTest.java @@ -22,6 +22,7 @@ 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; @@ -32,6 +33,8 @@ import jalview.util.Format; import java.awt.Color; +import junit.extensions.PA; + import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; @@ -56,6 +59,8 @@ public class FeatureColourTest assertTrue(fc1.getColour().equals(Color.RED)); assertFalse(fc1.isGraduatedColour()); assertFalse(fc1.isColourByLabel()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); /* * min-max colour @@ -67,9 +72,31 @@ public class FeatureColourTest 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.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.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(10f, fc1.getMin()); assertEquals(20f, fc1.getMax()); @@ -81,58 +108,102 @@ public class FeatureColourTest fc1 = new FeatureColour(fc); assertTrue(fc1.isColourByLabel()); assertFalse(fc1.isGraduatedColour()); - } - - @Test(groups = { "Functional" }) - public void testIsColored_simpleColour() - { - FeatureColour fc = new FeatureColour(Color.RED); - assertTrue(fc - .isColored(new SequenceFeature("Cath", "", 1, 2, 0f, null))); - } + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); - @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("Cath", "", 1, 2, 0f, null))); + fc.setAttributeName("AF"); + fc1 = new FeatureColour(fc); + assertTrue(fc1.isColourByLabel()); + assertFalse(fc1.isGraduatedColour()); + assertTrue(fc1.isColourByAttribute()); + assertEquals("AF", fc1.getAttributeName()); + + /* + * colour by attribute (value) + */ + fc = new FeatureColour(Color.gray, Color.black, Color.green, 10f, 20f); + fc.setAboveThreshold(true); + fc.setThreshold(12f); + fc.setAttributeName("AF"); + fc1 = new FeatureColour(fc); + assertTrue(fc1.isGraduatedColour()); + assertFalse(fc1.isColourByLabel()); + assertTrue(fc1.isColourByAttribute()); + assertEquals("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(10f, fc1.getMin()); + assertEquals(20f, fc1.getMax()); } @Test(groups = { "Functional" }) - public void testIsColored_aboveThreshold() + public void testCopyConstructor_minMax() { - // graduated colour range from score 20 to 100 - FeatureColour fc = new FeatureColour(Color.WHITE, Color.BLACK, 20f, - 100f); + /* + * graduated colour + */ + FeatureColour fc = new FeatureColour(Color.BLUE, Color.RED, 1f, 5f); + assertTrue(fc.isGraduatedColour()); + assertFalse(fc.isColourByLabel()); + assertFalse(fc.isColourByAttribute()); + assertNull(fc.getAttributeName()); + assertEquals(1f, fc.getMin()); + assertEquals(5f, fc.getMax()); - // 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)); + /* + * update min-max bounds + */ + FeatureColour fc1 = new FeatureColour(fc, 2f, 6f); + assertTrue(fc1.isGraduatedColour()); + assertFalse(fc1.isColourByLabel()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); + assertEquals(2f, fc1.getMin()); + assertEquals(6f, fc1.getMax()); + assertFalse((boolean) PA.getValue(fc1, "isHighToLow")); - // now apply threshold: - fc.setAboveThreshold(true); - assertFalse(fc.isColored(sf)); - // colour is still returned though ?!? - assertEquals(new Color(204, 204, 204), fc.getColor(sf)); + /* + * update min-max bounds - high to low + */ + fc1 = new FeatureColour(fc, 23f, 16f); + assertTrue(fc1.isGraduatedColour()); + assertFalse(fc1.isColourByLabel()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); + assertEquals(23f, fc1.getMin()); + assertEquals(16f, fc1.getMax()); + assertTrue((boolean) PA.getValue(fc1, "isHighToLow")); + + /* + * colour by label + */ + fc = new FeatureColour(Color.BLUE, Color.RED, 1f, 5f); + fc.setColourByLabel(true); + assertFalse(fc.isGraduatedColour()); + assertTrue(fc.isColourByLabel()); + assertFalse(fc.isColourByAttribute()); + assertNull(fc.getAttributeName()); + assertEquals(1f, fc.getMin()); + assertEquals(5f, fc.getMax()); - sf.setScore(84); // above threshold now - assertTrue(fc.isColored(sf)); - assertEquals(new Color(51, 51, 51), fc.getColor(sf)); + /* + * update min-max bounds - converts to graduated colour + */ + fc1 = new FeatureColour(fc, 2f, 6f); + assertTrue(fc1.isGraduatedColour()); + assertFalse(fc1.isColourByLabel()); + assertFalse(fc1.isColourByAttribute()); + assertNull(fc1.getAttributeName()); + assertEquals(2f, fc1.getMin()); + assertEquals(6f, fc1.getMax()); } @Test(groups = { "Functional" }) @@ -157,8 +228,11 @@ public class FeatureColourTest @Test(groups = { "Functional" }) public void testGetColor_Graduated() { - // graduated colour from score 0 to 100, gray(128, 128, 128) to red(255, 0, - // 0) + /* + * 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); // feature score is 75 which is 3/4 of the way from GRAY to RED SequenceFeature sf = new SequenceFeature("type", "desc", 0, 20, 75f, @@ -172,20 +246,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); 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)); } /** @@ -376,4 +465,59 @@ public class FeatureColourTest 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(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)); + } }