JAL-2360 testRegisterColourScheme to demonstrate a 'plug-in' scheme
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 20 Dec 2016 16:01:41 +0000 (16:01 +0000)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 20 Dec 2016 16:01:41 +0000 (16:01 +0000)
src/jalview/gui/AlignFrame.java
test/jalview/schemes/ColourSchemesTest.java

index 4687913..5127342 100644 (file)
@@ -5762,8 +5762,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
        * the colour scheme (inspected in changeColour_actionPerformed)
        */
       final String name = scheme.getSchemeName();
-      String label = MessageManager.getString("label.colourScheme_"
-              + name.toLowerCase().replace(" ", "_"));
+      String label = MessageManager.getStringOrReturn("label.colourScheme_"
+              + name.toLowerCase().replace(" ", "_"), name);
       final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(label);
       radioItem.setName(name);
       if (scheme instanceof UserColourScheme)
@@ -5773,7 +5773,6 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
          * Jalview session; right-click on this offers the option to
          * remove it as a colour choice
          */
-        radioItem.setText(((UserColourScheme) scheme).getName());
         radioItem.addMouseListener(new MouseAdapter()
         {
           @Override
index 7763002..1bc6348 100644 (file)
 package jalview.schemes;
 
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 
+import jalview.bin.Cache;
+import jalview.bin.Jalview;
+import jalview.datamodel.AnnotatedCollectionI;
+import jalview.datamodel.SequenceCollectionI;
+import jalview.datamodel.SequenceI;
+import jalview.gui.AlignFrame;
+import jalview.gui.AlignViewport;
+import jalview.gui.Desktop;
+import jalview.gui.SequenceRenderer;
+import jalview.io.DataSourceType;
+import jalview.io.FileLoader;
+import jalview.schemes.ClustalxColourScheme.ClustalColour;
+
+import java.awt.Color;
 import java.util.Iterator;
+import java.util.Map;
 
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class ColourSchemesTest
 {
+  /*
+   * a colour scheme that alternates Taylor and Zappo
+   * colouring by column
+   */
+  class Stripy extends ResidueColourScheme
+  {
+    @Override
+    public ColourSchemeI getInstance(AnnotatedCollectionI sg,
+            Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+    {
+      final ColourSchemeI odd = ColourSchemes.getInstance()
+              .getColourScheme(JalviewColourScheme.Taylor.toString(),
+                      (AnnotatedCollectionI) null);
+      final ColourSchemeI even = ColourSchemes.getInstance()
+              .getColourScheme(JalviewColourScheme.Zappo.toString(),
+                      (AnnotatedCollectionI) null);
+      return new ResidueColourScheme() {
+        @Override
+        public Color findColour(char c, int j, SequenceI seq)
+        {
+          if (j % 2 == 1)
+          {
+            return odd.findColour(c, j, seq);
+          }
+          else
+          {
+            return even.findColour(c, j, seq);
+          }
+        }
+
+        @Override
+        public ColourSchemeI getInstance(AnnotatedCollectionI sg,
+                Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+        {
+          return null;
+        }
+
+        @Override
+        public String getSchemeName()
+        {
+          return Stripy.this.getSchemeName();
+        }
+        
+      };
+    }
+    
+    @Override
+    public String getSchemeName()
+    {
+      return "stripy";
+    }
+  };
+  
+  /*
+   * a colour scheme that is Clustal but using AWT colour equivalents
+   */
+  class MyClustal extends ResidueColourScheme
+  {
+    ColourSchemeI delegate;
+
+    public MyClustal()
+    {
+    }
+
+    public MyClustal(ColourSchemeI scheme)
+    {
+      delegate = scheme;
+    }
+
+    @Override
+    public Color findColour(char c, int j, SequenceI seq)
+    {
+      Color col = delegate.findColour(c, j, seq);
+      Color result = col;
+      if (col.equals(ClustalColour.BLUE.colour))
+      {
+        result = Color.blue;
+      }
+      else if (col.equals(ClustalColour.CYAN.colour))
+      {
+        result = Color.cyan;
+      }
+      else if (col.equals(ClustalColour.GREEN.colour))
+      {
+        result = Color.green;
+      }
+      else if (col.equals(ClustalColour.MAGENTA.colour))
+      {
+        result = Color.magenta;
+      }
+      else if (col.equals(ClustalColour.ORANGE.colour))
+      {
+        result = Color.orange;
+      }
+      else if (col.equals(ClustalColour.PINK.colour))
+      {
+        result = Color.pink;
+      }
+      else if (col.equals(ClustalColour.RED.colour))
+      {
+        result = Color.red;
+      }
+      else if (col.equals(ClustalColour.YELLOW.colour))
+      {
+        result = Color.yellow;
+      }
+      return result;
+    }
+
+    @Override
+    public ColourSchemeI getInstance(AnnotatedCollectionI sg,
+            Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
+    {
+      return new MyClustal(new ClustalxColourScheme().getInstance(sg,
+              hiddenRepSequences));
+    }
+
+    @Override
+    public String getSchemeName()
+    {
+      return "MyClustal";
+    }
+
+  }
+
+  @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" });
+  }
+
+  @AfterClass(alwaysRun = true)
+  public static void tearDownAfterClass() throws Exception
+  {
+    Desktop.instance.closeAll_actionPerformed(null);
+  }
+
   @Test(groups = "Functional")
   public void testGetColourSchemes()
   {
@@ -19,10 +178,155 @@ public class ColourSchemesTest
     Iterator<ColourSchemeI> schemes = ColourSchemes.getInstance().getColourSchemes().iterator();
     JalviewColourScheme[] jalviewSchemes = JalviewColourScheme.values();
     int i = 0;
-    while (schemes.hasNext()) {
+    while (schemes.hasNext() && i < jalviewSchemes.length)
+    {
       assertTrue(schemes.next().getSchemeName()
               .equals(jalviewSchemes[i].toString()));
       i++;
     }
   }
+
+  @Test(groups = "Functional")
+  public void testGetColourScheme_forViewport()
+  {
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
+            ">seq1\nAGLRTWQU", DataSourceType.PASTE);
+    ColourSchemes cs = ColourSchemes.getInstance();
+
+    AlignViewport viewport = af.getViewport();
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Blosum62.toString(),
+            viewport) instanceof Blosum62ColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Buried.toString(),
+            viewport) instanceof BuriedColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Clustal.toString(),
+            viewport) instanceof ClustalxColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Helix.toString(),
+            viewport) instanceof HelixColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.Hydrophobic.toString(), viewport) instanceof HydrophobicColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.Nucleotide.toString(), viewport) instanceof NucleotideColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.PID.toString(),
+            viewport) instanceof PIDColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.PurinePyrimidine.toString(), viewport) instanceof PurinePyrimidineColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.RNAHelices.toString(), viewport) instanceof RNAHelicesColour);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Strand.toString(),
+            viewport) instanceof StrandColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Taylor.toString(),
+            viewport) instanceof TaylorColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.TCoffee.toString(),
+            viewport) instanceof TCoffeeColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Turn.toString(),
+            viewport) instanceof TurnColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Zappo.toString(),
+            viewport) instanceof ZappoColourScheme);
+    af.closeMenuItem_actionPerformed(true);
+  }
+
+  @Test(groups = "Functional")
+  public void testGetColourScheme_forAnnotatedCollection()
+  {
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
+            ">seq1\nAGLRTWQU", DataSourceType.PASTE);
+    ColourSchemes cs = ColourSchemes.getInstance();
+
+    AnnotatedCollectionI al = af.getViewport().getAlignment();
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Blosum62.toString(),
+            al) instanceof Blosum62ColourScheme);
+    assertTrue(cs
+            .getColourScheme(JalviewColourScheme.Buried.toString(), al) instanceof BuriedColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Clustal.toString(),
+            al) instanceof ClustalxColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Helix.toString(), al) instanceof HelixColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.Hydrophobic.toString(), al) instanceof HydrophobicColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.Nucleotide.toString(), al) instanceof NucleotideColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.PID.toString(), al) instanceof PIDColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.PurinePyrimidine.toString(), al) instanceof PurinePyrimidineColourScheme);
+    assertTrue(cs.getColourScheme(
+            JalviewColourScheme.RNAHelices.toString(), al) instanceof RNAHelicesColour);
+    assertTrue(cs
+            .getColourScheme(JalviewColourScheme.Strand.toString(), al) instanceof StrandColourScheme);
+    assertTrue(cs
+            .getColourScheme(JalviewColourScheme.Taylor.toString(), al) instanceof TaylorColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.TCoffee.toString(),
+            al) instanceof TCoffeeColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Turn.toString(), al) instanceof TurnColourScheme);
+    assertTrue(cs.getColourScheme(JalviewColourScheme.Zappo.toString(), al) instanceof ZappoColourScheme);
+    af.closeMenuItem_actionPerformed(true);
+  }
+  
+  @Test(groups = "Functional")
+  public void testRegisterColourScheme()
+  {
+    ColourSchemes.getInstance().registerColourScheme(new Stripy());
+    ColourSchemes.getInstance().registerColourScheme(new MyClustal());
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
+            "examples/uniref50.fa", DataSourceType.FILE);
+    /*
+     * set a breakpoint here to see and play with the newly registered
+     *  colour schemes in the AlignFrame colour menu
+     */
+    SequenceRenderer sr = new SequenceRenderer(af.getViewport());
+    SequenceI seq = af.getViewport().getAlignment().findName("FER_CAPAA");
+
+    /*
+     * set and check Taylor colours
+     */
+    af.changeColour_actionPerformed(JalviewColourScheme.Taylor.toString());
+    Color taylor1 = sr.getResidueBoxColour(seq, 88); // E 255,0,102
+    Color taylor2 = sr.getResidueBoxColour(seq, 89); // A 204,255,0
+    Color taylor3 = sr.getResidueBoxColour(seq, 90); // G 255,153,0
+    assertEquals(taylor1, new Color(255, 0, 102));
+    assertEquals(taylor2, new Color(204, 255, 0));
+    assertEquals(taylor3, new Color(255, 153, 0));
+
+    /*
+     * set and check Zappo colours
+     */
+    af.changeColour_actionPerformed(JalviewColourScheme.Zappo.toString());
+    Color zappo1 = sr.getResidueBoxColour(seq, 88); // E red
+    Color zappo2 = sr.getResidueBoxColour(seq, 89); // A pink
+    Color zappo3 = sr.getResidueBoxColour(seq, 90); // G magenta
+    assertEquals(zappo1, Color.red);
+    assertEquals(zappo2, Color.pink);
+    assertEquals(zappo3, Color.magenta);
+
+    /*
+     * set 'stripy' colours - odd columns are Taylor and even are Zappo 
+     */
+    af.changeColour_actionPerformed("stripy");
+    Color stripy1 = sr.getResidueBoxColour(seq, 88);
+    Color stripy2 = sr.getResidueBoxColour(seq, 89);
+    Color stripy3 = sr.getResidueBoxColour(seq, 90);
+    assertEquals(stripy1, zappo1);
+    assertEquals(stripy2, taylor2);
+    assertEquals(stripy3, zappo3);
+
+    /*
+     * set and check Clustal colours
+     */
+    af.changeColour_actionPerformed(JalviewColourScheme.Clustal.toString());
+    Color clustal1 = sr.getResidueBoxColour(seq, 88);
+    Color clustal2 = sr.getResidueBoxColour(seq, 89);
+    Color clustal3 = sr.getResidueBoxColour(seq, 90);
+    assertEquals(clustal1, ClustalColour.MAGENTA.colour);
+    assertEquals(clustal2, ClustalColour.BLUE.colour);
+    assertEquals(clustal3, ClustalColour.ORANGE.colour);
+
+    /*
+     * set 'MyClustal' colours - uses AWT colour equivalents
+     */
+    af.changeColour_actionPerformed("MyClustal");
+    Color myclustal1 = sr.getResidueBoxColour(seq, 88);
+    Color myclustal2 = sr.getResidueBoxColour(seq, 89);
+    Color myclustal3 = sr.getResidueBoxColour(seq, 90);
+    assertEquals(myclustal1, Color.MAGENTA);
+    assertEquals(myclustal2, Color.BLUE);
+    assertEquals(myclustal3, Color.ORANGE);
+  }
 }