JAL-2738 copy to spikes/mungo
[jalview.git] / test / jalview / gui / AlignFrameTest.java
index 244fa0b..af9c045 100644 (file)
@@ -1,23 +1,70 @@
+/*
+ * 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.gui;
 
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertFalse;
-import static org.testng.AssertJUnit.assertTrue;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotSame;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
 
+import jalview.bin.Cache;
+import jalview.bin.Jalview;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
+import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.io.DataSourceType;
+import jalview.io.FileLoader;
+import jalview.io.Jalview2xmlTests;
+import jalview.renderer.ResidueShaderI;
+import jalview.schemes.BuriedColourScheme;
+import jalview.schemes.HelixColourScheme;
+import jalview.schemes.JalviewColourScheme;
+import jalview.schemes.StrandColourScheme;
+import jalview.schemes.TurnColourScheme;
+import jalview.util.MessageManager;
 
+import java.awt.Color;
 import java.util.List;
 
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 public class AlignFrameTest
 {
+  AlignFrame af;
 
-  @Test
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
+  @Test(groups = "Functional")
   public void testHideFeatureColumns()
   {
     SequenceI seq1 = new Sequence("Seq1", "ABCDEFGHIJ");
@@ -31,27 +78,30 @@ public class AlignFrameTest
     seq2.addSequenceFeature(new SequenceFeature("Turn", "", 7, 9,
             Float.NaN, null));
     AlignmentI al = new Alignment(new SequenceI[] { seq1, seq2 });
-    AlignFrame af = new AlignFrame(al, al.getWidth(), al.getHeight());
+    AlignFrame alignFrame = new AlignFrame(al, al.getWidth(), al.getHeight());
 
     /*
      * hiding a feature not present does nothing
      */
-    assertFalse(af.hideFeatureColumns("exon", true));
-    assertTrue(af.getViewport().getColumnSelection().isEmpty());
-    assertTrue(af.getViewport().getColumnSelection().getHiddenColumns()
+    assertFalse(alignFrame.hideFeatureColumns("exon", true));
+    assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
+    assertTrue(alignFrame.getViewport().getAlignment().getHiddenColumns()
+            .getHiddenColumnsCopy()
             .isEmpty());
-    assertFalse(af.hideFeatureColumns("exon", false));
-    assertTrue(af.getViewport().getColumnSelection().isEmpty());
-    assertTrue(af.getViewport().getColumnSelection().getHiddenColumns()
+    assertFalse(alignFrame.hideFeatureColumns("exon", false));
+    assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
+    assertTrue(alignFrame.getViewport().getAlignment().getHiddenColumns()
+            .getHiddenColumnsCopy()
             .isEmpty());
 
     /*
      * hiding a feature in all columns does nothing
      */
-    assertFalse(af.hideFeatureColumns("Metal", true));
-    assertTrue(af.getViewport().getColumnSelection().isEmpty());
-    List<int[]> hidden = af.getViewport().getColumnSelection()
-            .getHiddenColumns();
+    assertFalse(alignFrame.hideFeatureColumns("Metal", true));
+    assertTrue(alignFrame.getViewport().getColumnSelection().isEmpty());
+    List<int[]> hidden = alignFrame.getViewport().getAlignment()
+            .getHiddenColumns()
+            .getHiddenColumnsCopy();
     assertTrue(hidden.isEmpty());
 
     /*
@@ -59,12 +109,464 @@ public class AlignFrameTest
      * sequence positions [2-4], [7-9] are column positions
      * [1-3], [6-8] base zero
      */
-    assertTrue(af.hideFeatureColumns("Turn", true));
-    hidden = af.getViewport().getColumnSelection().getHiddenColumns();
-    assertEquals(2, hidden.size());
-    assertEquals(1, hidden.get(0)[0]);
-    assertEquals(3, hidden.get(0)[1]);
-    assertEquals(6, hidden.get(1)[0]);
-    assertEquals(8, hidden.get(1)[1]);
+    assertTrue(alignFrame.hideFeatureColumns("Turn", true));
+    hidden = alignFrame.getViewport().getAlignment().getHiddenColumns()
+            .getHiddenColumnsCopy();
+    assertEquals(hidden.size(), 2);
+    assertEquals(hidden.get(0)[0], 1);
+    assertEquals(hidden.get(0)[1], 3);
+    assertEquals(hidden.get(1)[0], 6);
+    assertEquals(hidden.get(1)[1], 8);
+  }
+
+  @BeforeClass(alwaysRun = true)
+  public static void setUpBeforeClass() throws Exception
+  {
+    /*
+     * use read-only test properties file
+     */
+    Cache.loadProperties("test/jalview/io/testProps.jvprops");
+    Jalview.main(new String[] { "-nonews" });
+  }
+
+  @AfterMethod(alwaysRun = true)
+  public void tearDown()
+  {
+    Desktop.instance.closeAll_actionPerformed(null);
+  }
+
+  /**
+   * configure (read-only) properties for test to ensure Consensus is computed
+   * for colour Above PID testing
+   */
+  @BeforeMethod(alwaysRun = true)
+  public void setUp()
+  {
+    Cache.loadProperties("test/jalview/io/testProps.jvprops");
+    Cache.applicationProperties.setProperty("SHOW_IDENTITY",
+            Boolean.TRUE.toString());
+    af = new FileLoader().LoadFileWaitTillLoaded("examples/uniref50.fa",
+            DataSourceType.FILE);
+
+    /*
+     * wait for Consensus thread to complete
+     */
+    synchronized (this)
+    {
+      while (af.getViewport().getConsensusSeq() == null)
+      {
+        try
+        {
+          wait(50);
+        } catch (InterruptedException e)
+        {
+        }
+      }
+    }
+  }
+
+  /**
+   * Test that changing background (alignment) colour scheme
+   * <ul>
+   * <li>with Apply Colour to All Groups not selected, does not change group
+   * colours</li>
+   * <li>with Apply Colour to All Groups selected, does change group colours</li>
+   * <li>in neither case, changes alignment or group colour thresholds (PID or
+   * Conservation)</li>
+   * </ul>
+   */
+  @Test(groups = "Functional")
+  public void testChangeColour_background_groupsAndThresholds()
+  {
+    AlignViewport av = af.getViewport();
+    AlignmentI al = av.getAlignment();
+
+    /*
+     * Colour alignment by Buried Index
+     */
+    af.applyToAllGroups_actionPerformed(false);
+    af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
+    assertFalse(av.getResidueShading().conservationApplied());
+    assertEquals(av.getResidueShading().getThreshold(), 0);
+
+    /*
+     * Apply Conservation 20%
+     */
+    af.conservationMenuItem_actionPerformed(true);
+    SliderPanel sp = SliderPanel.getSliderPanel();
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.conservation_colour_increment",
+            new String[] { "Background" }));
+    assertTrue(sp.isForConservation());
+    sp.valueChanged(20);
+    assertTrue(av.getResidueShading().conservationApplied());
+    assertEquals(av.getResidueShading().getConservationInc(), 20);
+
+    /*
+     * Apply PID threshold 10% (conservation still applies as well)
+     */
+    af.abovePIDThreshold_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.percentage_identity_threshold",
+            new String[] { "Background" }));
+    sp.valueChanged(10);
+    assertEquals(av.getResidueShading().getThreshold(), 10);
+    assertTrue(av.getResidueShading().conservationApplied());
+    assertEquals(av.getResidueShading().getConservationInc(), 20);
+
+    /*
+     * create a group with Strand colouring, 30% Conservation
+     * and 40% PID threshold
+     */
+    SequenceGroup sg = new SequenceGroup();
+    sg.addSequence(al.getSequenceAt(0), false);
+    sg.setStartRes(15);
+    sg.setEndRes(25);
+    av.setSelectionGroup(sg);
+
+    /*
+     * apply 30% Conservation to group
+     */
+    PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
+    popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
+            .toString());
+    assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
+    assertEquals(al.getGroups().size(), 1);
+    assertSame(al.getGroups().get(0), sg);
+    popupMenu.conservationMenuItem_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.conservation_colour_increment",
+            new String[] { sg.getName() }));
+    sp.valueChanged(30);
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+
+    /*
+     * apply 40% PID threshold to group
+     */
+    popupMenu.abovePIDColour_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.percentage_identity_threshold",
+            new String[] { sg.getName() }));
+    sp.valueChanged(40);
+    assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
+    // conservation threshold is unchanged:
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+
+    /*
+     * change alignment colour - group colour, and all thresholds,
+     * should be unaffected
+     */
+    af.changeColour_actionPerformed(JalviewColourScheme.Turn.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof TurnColourScheme);
+    assertTrue(av.getResidueShading().conservationApplied());
+    assertEquals(av.getResidueShading().getConservationInc(), 20);
+    assertEquals(av.getResidueShading().getThreshold(), 10);
+    assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+    assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
+
+    /*
+     * Now change alignment colour with Apply Colour To All Groups
+     * - group colour should change, but not colour thresholds
+     */
+    af.applyToAllGroups_actionPerformed(true);
+    af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
+    assertTrue(av.getResidueShading().conservationApplied());
+    assertEquals(av.getResidueShading().getConservationInc(), 20);
+    assertEquals(av.getResidueShading().getThreshold(), 10);
+    assertTrue(sg.getColourScheme() instanceof HelixColourScheme);
+    assertTrue(sg.getGroupColourScheme().conservationApplied());
+    assertEquals(sg.getGroupColourScheme().getConservationInc(), 30);
+    assertEquals(sg.getGroupColourScheme().getThreshold(), 40);
+  }
+
+  /**
+   * Test residue colouring with various options
+   * <ol>
+   * <li>no PID or Conservation threshold</li>
+   * <li>colour by Conservation applied</li>
+   * <li>colour by Conservation removed</li>
+   * <li>colour above PID - various values</li>
+   * <li>colour above PID removed</li>
+   * <li>Above PID plus By Conservation combined</li>
+   * <li>remove Above PID to leave just By Conservation</li>
+   * <li>re-add Above PID</li>
+   * <li>remove By Conservation to leave just Above PID</li>
+   * <li>remove Above PID to leave original colours</li>
+   * </ol>
+   */
+  @Test(groups = "Functional")
+  public void testColourThresholdActions()
+  {
+    AlignViewport av = af.getViewport();
+    AlignmentI al = av.getAlignment();
+
+    /*
+     * Colour alignment by Helix Propensity, no thresholds
+     */
+    af.applyToAllGroups_actionPerformed(false);
+    af.changeColour_actionPerformed(JalviewColourScheme.Helix.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof HelixColourScheme);
+    assertFalse(av.getResidueShading().conservationApplied());
+    assertEquals(av.getResidueShading().getThreshold(), 0);
+
+    /*
+     * inspect the colour of 
+     * FER_CAPAN.9(I), column 14 (14 base 0)
+     * FER_CAPAN.10(SER), column 16 (15 base 0)
+     */
+    SequenceI ferCapan = al.findName("FER_CAPAN");
+    ResidueShaderI rs = av.getResidueShading();
+    Color c = rs.findColour('I', 14, ferCapan);
+    Color i_original = new Color(138, 117, 138);
+    assertEquals(c, i_original);
+    c = rs.findColour('S', 15, ferCapan);
+    Color s_original = new Color(54, 201, 54);
+    assertEquals(c, s_original);
+
+    /*
+     * colour by conservation with increment 10
+     */
+    af.conservationMenuItem_actionPerformed(true);
+    SliderPanel sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    assertEquals(sp.getValue(), 30); // initial slider setting
+    sp.valueChanged(10);
+    assertSame(rs, av.getResidueShading());
+    c = rs.findColour('I', 14, ferCapan);
+    Color i_faded = new Color(196, 186, 196);
+    assertEquals(c, i_faded);
+    c = rs.findColour('S', 15, ferCapan);
+    Color s_faded = new Color(144, 225, 144);
+    assertEquals(c, s_faded);
+
+    /*
+     * deselect By Conservation - colour should revert
+     */
+    af.conservationMenuItem_actionPerformed(false);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+
+    /*
+     * now Above PID, threshold = 0%
+     * should be no change
+     */
+    af.abovePIDThreshold_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    assertEquals(sp.getValue(), 0); // initial slider setting
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, i_original);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+
+    /*
+     * Above PID, threshold = 1%
+     * 15.I becomes White because no match to consensus (V)
+     * 16.S remains coloured as matches 66.66% consensus
+     */
+    sp.valueChanged(1);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, Color.white);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+
+    /*
+     * threshold 66% - no further change yet...
+     */
+    sp.valueChanged(66);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, Color.white);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+
+    /*
+     * threshold 67% - now both residues are white
+     */
+    sp.valueChanged(67);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, Color.white);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, Color.white);
+
+    /*
+     * deselect Above PID - colours should revert
+     */
+    af.abovePIDThreshold_actionPerformed(false);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, i_original);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+
+    /*
+     * Now combine Above 50% PID and By Conservation 10%
+     * 15.I is White because no match to consensus (V)
+     * 16.S is coloured but faded
+     */
+    af.abovePIDThreshold_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    sp.valueChanged(50);
+    af.conservationMenuItem_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    sp.valueChanged(10);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, Color.white);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_faded);
+
+    /*
+     * turn off Above PID - should just leave Conservation fading as before 
+     */
+    af.abovePIDThreshold_actionPerformed(false);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, i_faded);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_faded);
+
+    /*
+     * Now add Above 50% PID to conservation colouring
+     * - should give the same as PID followed by conservation (above)
+     */
+    af.abovePIDThreshold_actionPerformed(true);
+    SliderPanel.getSliderPanel().valueChanged(50);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, Color.white);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_faded);
+
+    /*
+     * turn off By Conservation
+     * should leave I white, S original (unfaded) colour
+     */
+    af.conservationMenuItem_actionPerformed(false);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, Color.white);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+
+    /*
+     * finally turn off Above PID to leave original colours
+     */
+    af.abovePIDThreshold_actionPerformed(false);
+    c = rs.findColour('I', 14, ferCapan);
+    assertEquals(c, i_original);
+    c = rs.findColour('S', 15, ferCapan);
+    assertEquals(c, s_original);
+  }
+
+  /**
+   * Verify that making a New View transfers alignment and group colour schemes,
+   * including any thresholds, to the new view. Because New View is performed by
+   * saving and reloading a 'project' file, this is similar to verifying a
+   * project save and reload.
+   * 
+   * @see Jalview2xmlTests#testStoreAndRecoverColourThresholds()
+   */
+  @Test(groups = "Functional")
+  public void testNewView_colourThresholds()
+  {
+    AlignViewport av = af.getViewport();
+    AlignmentI al = av.getAlignment();
+
+    /*
+     * Colour alignment by Buried Index, Above 10% PID, By Conservation 20%
+     */
+    af.changeColour_actionPerformed(JalviewColourScheme.Buried.toString());
+    assertTrue(av.getGlobalColourScheme() instanceof BuriedColourScheme);
+    af.abovePIDThreshold_actionPerformed(true);
+    SliderPanel sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    sp.valueChanged(10);
+    af.conservationMenuItem_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    sp.valueChanged(20);
+    ResidueShaderI rs = av.getResidueShading();
+    assertEquals(rs.getThreshold(), 10);
+    assertTrue(rs.conservationApplied());
+    assertEquals(rs.getConservationInc(), 20);
+
+    /*
+     * create a group with Strand colouring, 30% Conservation
+     * and 40% PID threshold
+     */
+    SequenceGroup sg = new SequenceGroup();
+    sg.addSequence(al.getSequenceAt(0), false);
+    sg.setStartRes(15);
+    sg.setEndRes(25);
+    av.setSelectionGroup(sg);
+    PopupMenu popupMenu = new PopupMenu(af.alignPanel, null, null);
+    popupMenu.changeColour_actionPerformed(JalviewColourScheme.Strand
+            .toString());
+    assertTrue(sg.getColourScheme() instanceof StrandColourScheme);
+    assertEquals(al.getGroups().size(), 1);
+    assertSame(al.getGroups().get(0), sg);
+    popupMenu.conservationMenuItem_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    sp.valueChanged(30);
+    popupMenu.abovePIDColour_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertFalse(sp.isForConservation());
+    sp.valueChanged(40);
+    rs = sg.getGroupColourScheme();
+    assertTrue(rs.conservationApplied());
+    assertEquals(rs.getConservationInc(), 30);
+    assertEquals(rs.getThreshold(), 40);
+
+    /*
+     * set slider panel focus to the background alignment
+     */
+    af.conservationMenuItem_actionPerformed(true);
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.conservation_colour_increment",
+            new String[] { "Background" }));
+
+    /*
+     * make a new View, verify alignment and group colour schemes
+     */
+    af.newView_actionPerformed(null);
+    assertEquals(af.alignPanel.getViewName(), "View 1");
+    AlignViewport av2 = af.getViewport();
+    assertNotSame(av, av2);
+    assertSame(av2, af.alignPanel.av);
+    rs = av2.getResidueShading();
+    assertNotSame(av.getResidueShading(), rs);
+    assertEquals(rs.getThreshold(), 10);
+    assertTrue(rs.conservationApplied(), rs.toString());
+    assertEquals(rs.getConservationInc(), 20);
+    assertEquals(av2.getAlignment().getGroups().size(), 1);
+    sg = av2.getAlignment().getGroups().get(0);
+    rs = sg.getGroupColourScheme();
+    assertTrue(rs.conservationApplied());
+    assertEquals(rs.getConservationInc(), 30);
+    assertEquals(rs.getThreshold(), 40);
+
+    /*
+     * check the Conservation SliderPanel (still open) is linked to 
+     * and updates the new view (JAL-2385)
+     */
+    sp = SliderPanel.getSliderPanel();
+    assertTrue(sp.isForConservation());
+    assertEquals(sp.getTitle(), MessageManager.formatMessage(
+            "label.conservation_colour_increment",
+            new String[] { "View 1" }));
+    sp.valueChanged(22);
+    assertEquals(av2.getResidueShading().getConservationInc(), 22);
   }
 }