JAL-2481 test correct behaviour of findFeatureColour for final residue in sequence
[jalview.git] / test / jalview / renderer / seqfeatures / FeatureColourFinderTest.java
index 9b68b43..f6dfed6 100644 (file)
@@ -2,6 +2,7 @@ package jalview.renderer.seqfeatures;
 
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotEquals;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 
@@ -16,6 +17,7 @@ import jalview.io.FileLoader;
 import jalview.schemes.FeatureColour;
 
 import java.awt.Color;
+import java.util.List;
 
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.BeforeTest;
@@ -70,13 +72,10 @@ public class FeatureColourFinderTest
   @BeforeMethod(alwaysRun = true)
   public void setUpBeforeTest()
   {
-    SequenceFeature[] sfs = seq.getSequenceFeatures();
-    if (sfs != null)
+    List<SequenceFeature> sfs = seq.getSequenceFeatures();
+    for (SequenceFeature sf : sfs)
     {
-      for (SequenceFeature sf : sfs)
-      {
-        seq.deleteFeature(sf);
-      }
+      seq.deleteFeature(sf);
     }
     fr.findAllFeatures(true);
 
@@ -287,6 +286,28 @@ public class FeatureColourFinderTest
   }
 
   @Test(groups = "Functional")
+  public void testFindFeatureAtEnd()
+  {
+    /*
+     * terminal residue feature
+     */
+    seq.addSequenceFeature(new SequenceFeature("PDBRESNUM", "pdb res 1",
+            seq.getEnd(), seq.getEnd(), Float.NaN, "1seq.pdb"));
+    fr.setColour("PDBRESNUM", new FeatureColour(Color.red));
+    fr.featuresAdded();
+    av.setShowSequenceFeatures(true);
+
+    /*
+     * final column should have PDBRESNUM feature, the others not
+     */
+    Color c = finder.findFeatureColour(Color.blue, seq,
+            seq.getLength() - 2);
+    assertNotEquals(c, Color.red);
+    c = finder.findFeatureColour(Color.blue, seq, seq.getLength() - 1);
+    assertEquals(c, Color.red);
+  }
+
+  @Test(groups = "Functional")
   public void testFindFeatureColour_graduatedFeatureColour()
   {
     seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 2,
@@ -450,4 +471,86 @@ public class FeatureColourFinderTest
     FeatureColourFinder finder2 = new FeatureColourFinder(null);
     assertTrue(finder2.noFeaturesDisplayed());
   }
+
+  @Test(groups = "Functional")
+  public void testFindFeatureColour_graduatedWithThreshold()
+  {
+    String kdFeature = "kd";
+    String metalFeature = "Metal";
+    seq.addSequenceFeature(new SequenceFeature(kdFeature, "hydrophobicity", 2,
+            2, 0f, "KdGroup"));
+    seq.addSequenceFeature(new SequenceFeature(kdFeature, "hydrophobicity", 4,
+            4, 5f, "KdGroup"));
+    seq.addSequenceFeature(new SequenceFeature(metalFeature, "Fe", 4, 4,
+            5f, "MetalGroup"));
+    seq.addSequenceFeature(new SequenceFeature(kdFeature, "hydrophobicity", 7,
+            7, 10f, "KdGroup"));
+  
+    /*
+     * kd feature has graduated colour from 0 to 10
+     * above threshold value of 5
+     */
+    Color min = new Color(100, 50, 150);
+    Color max = new Color(200, 0, 100);
+    FeatureColourI fc = new FeatureColour(min, max, 0, 10);
+    fc.setAboveThreshold(true);
+    fc.setThreshold(5f);
+    fr.setColour(kdFeature, fc);
+    FeatureColour green = new FeatureColour(Color.green);
+    fr.setColour(metalFeature, green);
+    fr.featuresAdded();
+
+    /*
+     * render order is kd above Metal
+     */
+    Object[][] data = new Object[2][];
+    data[0] = new Object[] { kdFeature, fc, true };
+    data[1] = new Object[] { metalFeature, green, true };
+    fr.setFeaturePriority(data);
+
+    av.setShowSequenceFeatures(true);
+  
+    /*
+     * position 2, column 1, score 0 - below threshold - default colour
+     */
+    Color c = finder.findFeatureColour(Color.blue, seq, 1);
+    assertEquals(c, Color.blue);
+
+    /*
+     * position 4, column 3, score 5 - at threshold
+     * should return Green (colour of Metal feature)
+     */
+    c = finder.findFeatureColour(Color.blue, seq, 3);
+    assertEquals(c, Color.green);
+  
+    /*
+     * position 7, column 9, score 10 - maximum colour in range
+     */
+    c = finder.findFeatureColour(Color.blue, seq, 9);
+    assertEquals(c, max);
+
+    /*
+     * now colour below threshold of 5
+     */
+    fc.setBelowThreshold(true);
+
+    /*
+     * position 2, column 1, score 0 - min colour
+     */
+    c = finder.findFeatureColour(Color.blue, seq, 1);
+    assertEquals(c, min);
+
+    /*
+     * position 4, column 3, score 5 - at threshold
+     * should return Green (colour of Metal feature)
+     */
+    c = finder.findFeatureColour(Color.blue, seq, 3);
+    assertEquals(c, Color.green);
+
+    /*
+     * position 7, column 9, score 10 - above threshold - default colour
+     */
+    c = finder.findFeatureColour(Color.blue, seq, 9);
+    assertEquals(c, Color.blue);
+  }
 }