JAL-2015 parse/write Jalview feature format pushed into FeatureColour,
[jalview.git] / test / jalview / schemes / FeatureColourTest.java
index 81357fa..483ea5d 100644 (file)
@@ -3,8 +3,10 @@ package jalview.schemes;
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.AssertJUnit.fail;
 
 import jalview.datamodel.SequenceFeature;
+import jalview.util.Format;
 
 import java.awt.Color;
 
@@ -112,4 +114,187 @@ public class FeatureColourTest
     assertFalse(fc.isColored(sf));
     assertEquals(new Color(204, 204, 204), fc.getColor(sf));
   }
+
+  /**
+   * Test output of feature colours to Jalview features file format
+   */
+  @Test(groups = { "Functional" })
+  public void testToJalviewFormat()
+  {
+    /*
+     * plain colour - to RGB hex code
+     */
+    FeatureColour fc = new FeatureColour(Color.RED);
+    String redHex = Format.getHexString(Color.RED);
+    String hexColour = redHex;
+    assertEquals("domain\t" + hexColour, fc.toJalviewFormat("domain"));
+    
+    /*
+     * colour by label (no threshold)
+     */
+    fc = new FeatureColour();
+    fc.setColourByLabel(true);
+    assertEquals("domain\tlabel", 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.setAutoScaled(true);
+    assertEquals("domain\tlabel", fc.toJalviewFormat("domain"));
+
+    /*
+     * colour by label (above threshold) (min/max values are output though not
+     * used by this scheme)
+     */
+    fc.setAutoScaled(false);
+    fc.setThreshold(12.5f);
+    fc.setAboveThreshold(true);
+    assertEquals("domain\tlabel|||0.0|0.0|above|12.5",
+            fc.toJalviewFormat("domain"));
+
+    /*
+     * colour by label (below threshold)
+     */
+    fc.setBelowThreshold(true);
+    assertEquals("domain\tlabel|||0.0|0.0|below|12.5",
+            fc.toJalviewFormat("domain"));
+
+    /*
+     * graduated colour, no threshold
+     */
+    fc = new FeatureColour(Color.GREEN, Color.RED, 12f, 25f);
+    String greenHex = Format.getHexString(Color.GREEN);
+    String expected = String.format("domain\t%s|%s|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|abso|12.0|25.0|none", greenHex,
+            redHex);
+    assertEquals(expected, fc.toJalviewFormat("domain"));
+
+    /*
+     * graduated colour below threshold
+     */
+    fc.setThreshold(12.5f);
+    fc.setBelowThreshold(true);
+    expected = String.format("domain\t%s|%s|abso|12.0|25.0|below|12.5",
+            greenHex, redHex);
+    assertEquals(expected, fc.toJalviewFormat("domain"));
+
+    /*
+     * graduated colour above threshold
+     */
+    fc.setThreshold(12.5f);
+    fc.setAboveThreshold(true);
+    expected = String.format("domain\t%s|%s|abso|12.0|25.0|above|12.5",
+            greenHex, redHex);
+    assertEquals(expected, fc.toJalviewFormat("domain"));
+  }
+
+  /**
+   * Test parsing of feature colours from Jalview features file format
+   */
+  @Test(groups = { "Functional" })
+  public void testParseJalviewFeatureColour()
+  {
+    /*
+     * simple colour by name
+     */
+    FeatureColour fc = FeatureColour.parseJalviewFeatureColour("red");
+    assertTrue(fc.isSimpleColour());
+    assertEquals(Color.RED, fc.getColour());
+
+    /*
+     * simple colour by hex code
+     */
+    fc = FeatureColour.parseJalviewFeatureColour(Format
+            .getHexString(Color.RED));
+    assertTrue(fc.isSimpleColour());
+    assertEquals(Color.RED, fc.getColour());
+
+    /*
+     * simple colour by rgb triplet
+     */
+    fc = FeatureColour.parseJalviewFeatureColour("255,0,0");
+    assertTrue(fc.isSimpleColour());
+    assertEquals(Color.RED, fc.getColour());
+
+    /*
+     * malformed colour
+     */
+    try
+    {
+      fc = FeatureColour.parseJalviewFeatureColour("oops");
+      fail("expected exception");
+    } catch (IllegalArgumentException e)
+    {
+      assertEquals("Invalid colour descriptor: oops", e.getMessage());
+    }
+
+    /*
+     * colour by label (no threshold)
+     */
+    fc = FeatureColour.parseJalviewFeatureColour("label");
+    assertTrue(fc.isColourByLabel());
+    assertFalse(fc.hasThreshold());
+
+    /*
+     * colour by label (with threshold)
+     */
+    fc = FeatureColour
+            .parseJalviewFeatureColour("label|||0.0|0.0|above|12.0");
+    assertTrue(fc.isColourByLabel());
+    assertTrue(fc.isAboveThreshold());
+    assertEquals(12.0f, fc.getThreshold());
+
+    /*
+     * graduated colour (by name) (no threshold)
+     */
+    fc = FeatureColour.parseJalviewFeatureColour("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 hex code) (above threshold)
+     */
+    String descriptor = String.format("%s|%s|10.0|20.0|above|15",
+            Format.getHexString(Color.RED),
+            Format.getHexString(Color.GREEN));
+    fc = FeatureColour.parseJalviewFeatureColour(descriptor);
+    assertTrue(fc.isGraduatedColour());
+    assertTrue(fc.hasThreshold());
+    assertTrue(fc.isAboveThreshold());
+    assertEquals(15f, 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");
+    fc = FeatureColour.parseJalviewFeatureColour(descriptor);
+    assertTrue(fc.isGraduatedColour());
+    assertFalse(fc.isAutoScaled());
+    assertTrue(fc.hasThreshold());
+    assertTrue(fc.isBelowThreshold());
+    assertEquals(15f, fc.getThreshold());
+    assertEquals(Color.RED, fc.getMinColour());
+    assertEquals(Color.GREEN, fc.getMaxColour());
+    assertEquals(10f, fc.getMin());
+    assertEquals(20f, fc.getMax());
+  }
 }