JAL-3049 JAL-2069 colour icon tooltip including by attribute
[jalview.git] / test / jalview / schemes / FeatureColourTest.java
index 3f60152..754e077 100644 (file)
@@ -25,7 +25,9 @@ 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;
@@ -33,11 +35,11 @@ import jalview.util.Format;
 
 import java.awt.Color;
 
-import junit.extensions.PA;
-
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
+import junit.extensions.PA;
+
 public class FeatureColourTest
 {
 
@@ -121,7 +123,7 @@ public class FeatureColourTest
     assertTrue(fc1.isColourByLabel());
     assertFalse(fc1.isGraduatedColour());
     assertTrue(fc1.isColourByAttribute());
-    assertEquals("AF", fc1.getAttributeName());
+    assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName());
 
     /*
      * colour by attribute (value)
@@ -134,7 +136,7 @@ public class FeatureColourTest
     assertTrue(fc1.isGraduatedColour());
     assertFalse(fc1.isColourByLabel());
     assertTrue(fc1.isColourByAttribute());
-    assertEquals("AF", fc1.getAttributeName());
+    assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName());
     assertTrue(fc1.isAboveThreshold());
     assertEquals(12f, fc1.getThreshold());
     assertEquals(Color.gray, fc1.getMinColour());
@@ -183,6 +185,19 @@ public class FeatureColourTest
     assertTrue((boolean) PA.getValue(fc1, "isHighToLow"));
 
     /*
+     * graduated colour by attribute
+     */
+    fc1.setAttributeName("AF");
+    fc1 = new FeatureColour(fc1, 13f, 36f);
+    assertTrue(fc1.isGraduatedColour());
+    assertFalse(fc1.isColourByLabel());
+    assertTrue(fc1.isColourByAttribute());
+    assertArrayEquals(new String[] { "AF" }, fc1.getAttributeName());
+    assertEquals(13f, fc1.getMin());
+    assertEquals(36f, fc1.getMax());
+    assertFalse((boolean) PA.getValue(fc1, "isHighToLow"));
+
+    /*
      * colour by label
      */
     fc = new FeatureColour(Color.BLUE, Color.RED, 1f, 5f);
@@ -195,15 +210,27 @@ public class FeatureColourTest
     assertEquals(5f, fc.getMax());
 
     /*
-     * update min-max bounds - converts to graduated colour
+     * update min-max bounds
      */
     fc1 = new FeatureColour(fc, 2f, 6f);
-    assertTrue(fc1.isGraduatedColour());
-    assertFalse(fc1.isColourByLabel());
+    assertFalse(fc1.isGraduatedColour());
+    assertTrue(fc1.isColourByLabel());
     assertFalse(fc1.isColourByAttribute());
     assertNull(fc1.getAttributeName());
     assertEquals(2f, fc1.getMin());
     assertEquals(6f, fc1.getMax());
+
+    /*
+     * colour by attribute text
+     */
+    fc1.setAttributeName("AC");
+    fc1 = new FeatureColour(fc1, 13f, 36f);
+    assertFalse(fc1.isGraduatedColour());
+    assertTrue(fc1.isColourByLabel());
+    assertTrue(fc1.isColourByAttribute());
+    assertArrayEquals(new String[] { "AC" }, fc1.getAttributeName());
+    assertEquals(13f, fc1.getMin());
+    assertEquals(36f, fc1.getMax());
   }
 
   @Test(groups = { "Functional" })
@@ -299,20 +326,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"));
 
@@ -324,38 +360,80 @@ public class FeatureColourTest
             fc.toJalviewFormat("domain"));
 
     /*
-     * graduated colour, no threshold
+     * colour by attributes text (below threshold)
+     */
+    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(Color.GREEN, Color.RED, 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.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.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"));
   }
@@ -369,7 +447,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());
 
@@ -417,7 +495,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());
@@ -429,7 +528,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),
@@ -446,9 +573,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());
@@ -460,8 +604,7 @@ 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());
   }
@@ -520,4 +663,101 @@ public class FeatureColourTest
     Color expected = new Color(70, 120, 170);
     assertEquals(expected, fc.getColor(sf));
   }
+
+  /**
+   * Test description of feature colour suitable for a tooltip
+   */
+  @Test(groups = { "Functional" })
+  public void testGetDescription()
+  {
+    /*
+     * plain colour
+     */
+    FeatureColour fc = new FeatureColour(Color.RED);
+    assertEquals(
+            String.format("r=%d,g=%d,b=%d", Color.RED.getRed(),
+                    Color.red.getGreen(), Color.red.getBlue()),
+            fc.getDescription());
+  
+    /*
+     * colour by label (no threshold)
+     */
+    fc = new FeatureColour();
+    fc.setColourByLabel(true);
+    assertEquals("By Label", fc.getDescription());
+  
+    /*
+     * colour by attribute text (no threshold)
+     */
+    fc = new FeatureColour();
+    fc.setColourByLabel(true);
+    fc.setAttributeName("CLIN_SIG");
+    assertEquals("By CLIN_SIG", fc.getDescription());
+  
+    /*
+     * colour by label (above score threshold) 
+     */
+    fc = new FeatureColour();
+    fc.setColourByLabel(true);
+    fc.setAutoScaled(false);
+    fc.setThreshold(12.5f);
+    fc.setAboveThreshold(true);
+    assertEquals("By Label (Score > 12.5)",
+            fc.getDescription());
+  
+    /*
+     * colour by label (below score threshold)
+     */
+    fc.setBelowThreshold(true);
+    assertEquals("By Label (Score < 12.5)",
+            fc.getDescription());
+  
+    /*
+     * colour by attributes text (below score threshold)
+     */
+    fc.setBelowThreshold(true);
+    fc.setAttributeName("CSQ", "Consequence");
+    assertEquals(
+            "By CSQ:Consequence (Score < 12.5)",
+            fc.getDescription());
+  
+    /*
+     * graduated colour by score, no threshold
+     */
+    fc = new FeatureColour(Color.GREEN, Color.RED, 12f, 25f);
+    assertEquals("By Score", fc.getDescription());
+  
+    /*
+     * graduated colour by score, below threshold
+     */
+    fc.setThreshold(12.5f);
+    fc.setBelowThreshold(true);
+    assertEquals("By Score (< 12.5)",
+            fc.getDescription());
+  
+    /*
+     * graduated colour by score, above threshold
+     */
+    fc.setThreshold(12.5f);
+    fc.setAboveThreshold(true);
+    fc.setAutoScaled(false);
+    assertEquals("By Score (> 12.5)",
+            fc.getDescription());
+
+    /*
+     * graduated colour by attribute, no threshold
+     */
+    fc.setAttributeName("CSQ", "AF");
+    fc.setAboveThreshold(false);
+    fc.setAutoScaled(false);
+    assertEquals("By CSQ:AF", fc.getDescription());
+  
+    /*
+     * graduated colour by attribute, above threshold
+     */
+    fc.setAboveThreshold(true);
+    fc.setAutoScaled(false);
+    assertEquals("By CSQ:AF (> 12.5)",
+            fc.getDescription());
+  }
 }