1 package jalview.schemes;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertSame;
6 import static org.testng.Assert.assertTrue;
8 import jalview.api.AlignViewportI;
9 import jalview.bin.Cache;
10 import jalview.bin.Jalview;
11 import jalview.datamodel.AnnotatedCollectionI;
12 import jalview.datamodel.SequenceCollectionI;
13 import jalview.datamodel.SequenceI;
14 import jalview.gui.AlignFrame;
15 import jalview.gui.Desktop;
16 import jalview.gui.SequenceRenderer;
17 import jalview.io.DataSourceType;
18 import jalview.io.FileLoader;
19 import jalview.schemes.ClustalxColourScheme.ClustalColour;
21 import java.awt.Color;
22 import java.util.Iterator;
25 import org.testng.annotations.AfterClass;
26 import org.testng.annotations.BeforeClass;
27 import org.testng.annotations.Test;
29 public class ColourSchemesTest
32 * a colour scheme that alternates Taylor and Zappo
35 class Stripy extends ResidueColourScheme
37 private ResidueColourScheme odd;
39 private ResidueColourScheme even;
46 * constructor given colours for odd and even columns
51 private Stripy(ColourSchemeI cs1, ColourSchemeI cs2)
53 odd = (ResidueColourScheme) cs1;
54 even = (ResidueColourScheme) cs2;
58 public ColourSchemeI getInstance(AlignViewportI view,
59 AnnotatedCollectionI sg)
61 final ColourSchemeI cs1 = ColourSchemes.getInstance()
62 .getColourScheme(JalviewColourScheme.Taylor.toString(),
63 (AnnotatedCollectionI) null);
64 final ColourSchemeI cs2 = ColourSchemes.getInstance()
65 .getColourScheme(JalviewColourScheme.Zappo.toString(),
66 (AnnotatedCollectionI) null);
67 return new Stripy(cs1, cs2);
71 public Color findColour(char c, int j, SequenceI seq)
75 return odd.findColour(c, j, seq);
79 return even.findColour(c, j, seq);
84 public String getSchemeName()
91 * a colour scheme that is Clustal but using AWT colour equivalents
93 class MyClustal extends ResidueColourScheme
95 ClustalxColourScheme delegate;
101 private MyClustal(AnnotatedCollectionI sg,
102 Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
104 delegate = new ClustalxColourScheme(sg, hiddenRepSequences);
108 public Color findColour(char c, int j, SequenceI seq)
110 Color col = delegate.findColour(c, j, seq);
112 if (col.equals(ClustalColour.BLUE.colour))
116 else if (col.equals(ClustalColour.CYAN.colour))
120 else if (col.equals(ClustalColour.GREEN.colour))
122 result = Color.green;
124 else if (col.equals(ClustalColour.MAGENTA.colour))
126 result = Color.magenta;
128 else if (col.equals(ClustalColour.ORANGE.colour))
130 result = Color.orange;
132 else if (col.equals(ClustalColour.PINK.colour))
136 else if (col.equals(ClustalColour.RED.colour))
140 else if (col.equals(ClustalColour.YELLOW.colour))
142 result = Color.yellow;
148 public ColourSchemeI getInstance(AlignViewportI view,
149 AnnotatedCollectionI sg)
151 return new MyClustal(sg, view.getHiddenRepSequences());
155 public String getSchemeName()
162 @BeforeClass(alwaysRun = true)
163 public static void setUpBeforeClass() throws Exception
166 * use read-only test properties file
168 Cache.loadProperties("test/jalview/io/testProps.jvprops");
169 Jalview.main(new String[] { "-nonews" });
172 @AfterClass(alwaysRun = true)
173 public static void tearDownAfterClass() throws Exception
175 Desktop.instance.closeAll_actionPerformed(null);
178 @Test(groups = "Functional")
179 public void testGetColourSchemes()
182 * this just verifies that built-in colour schemes are loaded into ColourSchemes
183 * in the order in which they are declared in the JalviewColourScheme enum
184 * (this also determines their order in Colour menus)
186 Iterator<ColourSchemeI> schemes = ColourSchemes.getInstance().getColourSchemes().iterator();
187 JalviewColourScheme[] jalviewSchemes = JalviewColourScheme.values();
189 while (schemes.hasNext() && i < jalviewSchemes.length)
191 assertTrue(schemes.next().getSchemeName()
192 .equals(jalviewSchemes[i].toString()));
197 @Test(groups = "Functional")
198 public void testGetColourScheme()
200 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
201 ">seq1\nAGLRTWQU", DataSourceType.PASTE);
202 ColourSchemes schemes = ColourSchemes.getInstance();
204 AnnotatedCollectionI al = af.getViewport().getAlignment();
206 for (JalviewColourScheme cs : JalviewColourScheme.values())
208 ColourSchemeI registered = schemes.getColourScheme(cs.toString(), al);
209 assertSame(registered.getClass(), cs.getSchemeClass());
211 af.closeMenuItem_actionPerformed(true);
214 @Test(groups = "Functional")
215 public void testRegisterColourScheme()
217 ColourSchemes.getInstance().registerColourScheme(new Stripy());
218 ColourSchemes.getInstance().registerColourScheme(new MyClustal());
219 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
220 "examples/uniref50.fa", DataSourceType.FILE);
222 * set a breakpoint here to see and play with the newly registered
223 * colour schemes in the AlignFrame colour menu
225 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
226 SequenceI seq = af.getViewport().getAlignment().findName("FER_CAPAA");
229 * set and check Taylor colours
231 af.changeColour_actionPerformed(JalviewColourScheme.Taylor.toString());
232 Color taylor1 = sr.getResidueColour(seq, 88, null); // E 255,0,102
233 Color taylor2 = sr.getResidueColour(seq, 89, null); // A 204,255,0
234 Color taylor3 = sr.getResidueColour(seq, 90, null); // G 255,153,0
235 assertEquals(taylor1, new Color(255, 0, 102));
236 assertEquals(taylor2, new Color(204, 255, 0));
237 assertEquals(taylor3, new Color(255, 153, 0));
240 * set and check Zappo colours
242 af.changeColour_actionPerformed(JalviewColourScheme.Zappo.toString());
243 Color zappo1 = sr.getResidueColour(seq, 88, null); // E red
244 Color zappo2 = sr.getResidueColour(seq, 89, null); // A pink
245 Color zappo3 = sr.getResidueColour(seq, 90, null); // G magenta
246 assertEquals(zappo1, Color.red);
247 assertEquals(zappo2, Color.pink);
248 assertEquals(zappo3, Color.magenta);
251 * set 'stripy' colours - odd columns are Taylor and even are Zappo
253 af.changeColour_actionPerformed("stripy");
254 Color stripy1 = sr.getResidueColour(seq, 88, null);
255 Color stripy2 = sr.getResidueColour(seq, 89, null);
256 Color stripy3 = sr.getResidueColour(seq, 90, null);
257 assertEquals(stripy1, zappo1);
258 assertEquals(stripy2, taylor2);
259 assertEquals(stripy3, zappo3);
262 * set and check Clustal colours
264 af.changeColour_actionPerformed(JalviewColourScheme.Clustal.toString());
265 Color clustal1 = sr.getResidueColour(seq, 88, null);
266 Color clustal2 = sr.getResidueColour(seq, 89, null);
267 Color clustal3 = sr.getResidueColour(seq, 90, null);
268 assertEquals(clustal1, ClustalColour.MAGENTA.colour);
269 assertEquals(clustal2, ClustalColour.BLUE.colour);
270 assertEquals(clustal3, ClustalColour.ORANGE.colour);
273 * set 'MyClustal' colours - uses AWT colour equivalents
275 af.changeColour_actionPerformed("MyClustal");
276 Color myclustal1 = sr.getResidueColour(seq, 88, null);
277 Color myclustal2 = sr.getResidueColour(seq, 89, null);
278 Color myclustal3 = sr.getResidueColour(seq, 90, null);
279 assertEquals(myclustal1, Color.MAGENTA);
280 assertEquals(myclustal2, Color.BLUE);
281 assertEquals(myclustal3, Color.ORANGE);
285 * Tests for check if scheme name exists. Built-in scheme names are the
286 * toString() values of enum JalviewColourScheme.
288 @Test(groups = "Functional")
289 public void testNameExists()
291 ColourSchemes cs = ColourSchemes.getInstance();
292 assertFalse(cs.nameExists(null));
293 assertFalse(cs.nameExists(""));
294 assertTrue(cs.nameExists("Clustal"));
295 assertTrue(cs.nameExists("CLUSTAL"));
296 assertFalse(cs.nameExists("CLUSTAL "));
297 assertTrue(cs.nameExists("% Identity"));
298 assertFalse(cs.nameExists("PID"));