Merge branch 'bug/JAL-3612_overviewFeatureOrdering' into develop
[jalview.git] / test / jalview / renderer / seqfeatures / FeatureColourFinderTest.java
index 4fc079e..637bbf4 100644 (file)
@@ -1,25 +1,48 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 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;
 
+import java.awt.Color;
+import java.util.List;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
 import jalview.api.FeatureColourI;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
-import jalview.gui.FeatureRenderer;
 import jalview.io.DataSourceType;
 import jalview.io.FileLoader;
 import jalview.schemes.FeatureColour;
-
-import java.awt.Color;
-
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.BeforeTest;
-import org.testng.annotations.Test;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel;
+import jalview.viewmodel.seqfeatures.FeatureRendererModel.FeatureSettingsBean;
 
 /**
  * Unit tests for feature colour determination, including but not limited to
@@ -48,15 +71,14 @@ public class FeatureColourFinderTest
 
   private AlignFrame af;
 
-  private FeatureRenderer fr;
+  private FeatureRendererModel fr;
 
   @BeforeTest(alwaysRun = true)
   public void setUp()
   {
     // aligned column 8 is sequence position 6
     String s = ">s1\nABCDE---FGHIJKLMNOPQRSTUVWXYZ\n";
-    af = new FileLoader().LoadFileWaitTillLoaded(s,
-            DataSourceType.PASTE);
+    af = new FileLoader().LoadFileWaitTillLoaded(s, DataSourceType.PASTE);
     av = af.getViewport();
     seq = av.getAlignment().getSequenceAt(0);
     fr = af.getFeatureRenderer();
@@ -70,13 +92,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);
 
@@ -134,8 +153,8 @@ public class FeatureColourFinderTest
   @Test(groups = "Functional")
   public void testFindFeatureColour_gapPosition()
   {
-    seq.addSequenceFeature(new SequenceFeature("Metal", "Metal", 2, 12, 0f,
-            null));
+    seq.addSequenceFeature(
+            new SequenceFeature("Metal", "Metal", 2, 12, 0f, null));
     fr.setColour("Metal", new FeatureColour(Color.red));
     fr.featuresAdded();
     av.setShowSequenceFeatures(true);
@@ -143,6 +162,27 @@ public class FeatureColourFinderTest
     assertNull(c);
   }
 
+  /**
+   * Nested features coloured by label - expect the colour of the enclosed
+   * feature
+   */
+  @Test(groups = "Functional")
+  public void testFindFeatureColour_nestedFeatures()
+  {
+    SequenceFeature sf1 = new SequenceFeature("domain", "peptide", 1, 120, 0f, null);
+    seq.addSequenceFeature(sf1);
+    SequenceFeature sf2 = new SequenceFeature("domain", "binding", 10, 20,
+            0f, null);
+    seq.addSequenceFeature(sf2);
+    FeatureColourI fc = new FeatureColour(Color.red);
+    fc.setColourByLabel(true);
+    fr.setColour("domain", fc);
+    fr.featuresAdded();
+    av.setShowSequenceFeatures(true);
+    Color c = finder.findFeatureColour(null, seq, 15);
+    assertEquals(c, fr.getColor(sf2, fc));
+  }
+
   @Test(groups = "Functional")
   public void testFindFeatureColour_multipleFeaturesAtPositionNoTransparency()
   {
@@ -173,9 +213,9 @@ public class FeatureColourFinderTest
      * - currently no way other than mimicking reordering of
      * table in Feature Settings
      */
-    Object[][] data = new Object[2][];
-    data[0] = new Object[] { "Metal", red, true };
-    data[1] = new Object[] { "Domain", green, true };
+    FeatureSettingsBean[] data = new FeatureSettingsBean[2];
+    data[0] = new FeatureSettingsBean("Metal", red, null, true);
+    data[1] = new FeatureSettingsBean("Domain", green, null, true);
     fr.setFeaturePriority(data);
     c = finder.findFeatureColour(Color.blue, seq, 10);
     assertEquals(c, Color.red);
@@ -183,7 +223,7 @@ public class FeatureColourFinderTest
     /*
      * ..and turn off display of Metal
      */
-    data[0][2] = false;
+    data[0] = new FeatureSettingsBean("Metal", red, null, false);
     fr.setFeaturePriority(data);
     c = finder.findFeatureColour(Color.blue, seq, 10);
     assertEquals(c, Color.green);
@@ -217,8 +257,8 @@ public class FeatureColourFinderTest
     /*
      * turn off display of Metal - is this the easiest way to do it??
      */
-    Object[][] data = new Object[1][];
-    data[0] = new Object[] { "Metal", red, false };
+    FeatureSettingsBean[] data = new FeatureSettingsBean[1];
+    data[0] = new FeatureSettingsBean("Metal", red, null, false);
     fr.setFeaturePriority(data);
     c = finder.findFeatureColour(Color.blue, seq, 10);
     assertEquals(c, Color.blue);
@@ -226,7 +266,7 @@ public class FeatureColourFinderTest
     /*
      * turn display of Metal back on
      */
-    data[0] = new Object[] { "Metal", red, true };
+    data[0] = new FeatureSettingsBean("Metal", red, null, true);
     fr.setFeaturePriority(data);
     c = finder.findFeatureColour(Color.blue, seq, 10);
     assertEquals(c, Color.red);
@@ -265,8 +305,8 @@ public class FeatureColourFinderTest
     /*
      * currently contact feature == type "Disulphide Bond" or "Disulfide Bond" !!
      */
-    seq.addSequenceFeature(new SequenceFeature("Disulphide Bond",
-            "Contact", 2, 12, Float.NaN, "Disulphide"));
+    seq.addSequenceFeature(new SequenceFeature("Disulphide Bond", "Contact",
+            2, 12, Float.NaN, "Disulphide"));
     fr.setColour("Disulphide Bond", new FeatureColour(Color.red));
     fr.featuresAdded();
     av.setShowSequenceFeatures(true);
@@ -287,21 +327,43 @@ 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,
-            2, 0f, "KdGroup"));
-    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 4,
-            4, 5f, "KdGroup"));
-    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 7,
-            7, 10f, "KdGroup"));
+    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 2, 2,
+            0f, "KdGroup"));
+    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 4, 4,
+            5f, "KdGroup"));
+    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 7, 7,
+            10f, "KdGroup"));
 
     /*
      * graduated colour from 0 to 10
      */
     Color min = new Color(100, 50, 150);
     Color max = new Color(200, 0, 100);
-    FeatureColourI fc = new FeatureColour(min, max, 0, 10);
+    FeatureColourI fc = new FeatureColour(null, min, max, null, 0, 10);
     fr.setColour("kd", fc);
     fr.featuresAdded();
     av.setShowSequenceFeatures(true);
@@ -334,7 +396,7 @@ public class FeatureColourFinderTest
     fr.setColour("Metal", red);
     fr.featuresAdded();
     av.setShowSequenceFeatures(true);
-  
+
     /*
      * the FeatureSettings transparency slider has range 0-70 which
      * corresponds to a transparency value of 1 - 0.3
@@ -360,7 +422,7 @@ public class FeatureColourFinderTest
     fr.setColour("Domain", green);
     fr.featuresAdded();
     av.setShowSequenceFeatures(true);
-  
+
     /*
      * Domain (green) rendered above Metal (red) above background (cyan)
      * 1) 0.6 * red(255, 0, 0) + 0.4 * cyan(0, 255, 255) = (153, 102, 102)
@@ -369,7 +431,7 @@ public class FeatureColourFinderTest
     fr.setTransparency(0.6f);
     Color c = finder.findFeatureColour(Color.cyan, seq, 10);
     assertEquals(c, new Color(61, 194, 41));
-  
+
     /*
      * now promote Metal above Domain
      * - currently no way other than mimicking reordering of
@@ -378,19 +440,19 @@ public class FeatureColourFinderTest
      * 1) 0.6 * green(0, 255, 0) + 0.4 * cyan(0, 255, 255) = (0, 255, 102)
      * 2) 0.6* red(255, 0, 0) + 0.4 * (0, 255, 102) = (153, 102, 41) rounded
      */
-    Object[][] data = new Object[2][];
-    data[0] = new Object[] { "Metal", red, true };
-    data[1] = new Object[] { "Domain", green, true };
+    FeatureSettingsBean[] data = new FeatureSettingsBean[2];
+    data[0] = new FeatureSettingsBean("Metal", red, null, true);
+    data[1] = new FeatureSettingsBean("Domain", green, null, true);
     fr.setFeaturePriority(data);
     c = finder.findFeatureColour(Color.cyan, seq, 10);
     assertEquals(c, new Color(153, 102, 41));
-  
+
     /*
      * ..and turn off display of Metal
      * Domain (green) above background (pink)
      * 0.6 * green(0, 255, 0) + 0.4 * pink(255, 175, 175) = (102, 223, 70)
      */
-    data[0][2] = false;
+    data[0] = new FeatureSettingsBean("Metal", red, null, false);
     fr.setFeaturePriority(data);
     c = finder.findFeatureColour(Color.pink, seq, 10);
     assertEquals(c, new Color(102, 223, 70));
@@ -426,8 +488,8 @@ public class FeatureColourFinderTest
     /*
      * turn off display of Metal
      */
-    Object[][] data = new Object[1][];
-    data[0] = new Object[] { "Metal", red, false };
+    FeatureSettingsBean[] data = new FeatureSettingsBean[1];
+    data[0] = new FeatureSettingsBean("Metal", red, null, false);
     fr.setFeaturePriority(data);
     assertTrue(finder.noFeaturesDisplayed());
 
@@ -454,26 +516,41 @@ public class FeatureColourFinderTest
   @Test(groups = "Functional")
   public void testFindFeatureColour_graduatedWithThreshold()
   {
-    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 2,
-            2, 0f, "KdGroup"));
-    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 4,
-            4, 5f, "KdGroup"));
-    seq.addSequenceFeature(new SequenceFeature("kd", "hydrophobicity", 7,
-            7, 10f, "KdGroup"));
-  
-    /*
-     * graduated colour from 0 to 10
+    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);
+    FeatureColourI fc = new FeatureColour(null, min, max, null, 0, 10);
     fc.setAboveThreshold(true);
     fc.setThreshold(5f);
-    fr.setColour("kd", fc);
+    fr.setColour(kdFeature, fc);
+    FeatureColour green = new FeatureColour(Color.green);
+    fr.setColour(metalFeature, green);
     fr.featuresAdded();
+
+    /*
+     * render order is kd above Metal
+     */
+    FeatureSettingsBean[] data = new FeatureSettingsBean[2];
+    data[0] = new FeatureSettingsBean(kdFeature, fc, null, true);
+    data[1] = new FeatureSettingsBean(metalFeature, green, null, true);
+    fr.setFeaturePriority(data);
+
     av.setShowSequenceFeatures(true);
-  
+
     /*
      * position 2, column 1, score 0 - below threshold - default colour
      */
@@ -481,11 +558,12 @@ public class FeatureColourFinderTest
     assertEquals(c, Color.blue);
 
     /*
-     * position 4, column 3, score 5 - at threshold - default colour
+     * 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.blue);
-  
+    assertEquals(c, Color.green);
+
     /*
      * position 7, column 9, score 10 - maximum colour in range
      */
@@ -504,10 +582,11 @@ public class FeatureColourFinderTest
     assertEquals(c, min);
 
     /*
-     * position 4, column 3, score 5 - at threshold - default colour
+     * 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.blue);
+    assertEquals(c, Color.green);
 
     /*
      * position 7, column 9, score 10 - above threshold - default colour