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().getColourScheme(
62 JalviewColourScheme.Taylor.toString(),
63 (AnnotatedCollectionI) null);
64 final ColourSchemeI cs2 = ColourSchemes.getInstance().getColourScheme(
65 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()
187 .getColourSchemes().iterator();
188 JalviewColourScheme[] jalviewSchemes = JalviewColourScheme.values();
190 while (schemes.hasNext() && i < jalviewSchemes.length)
192 assertTrue(schemes.next().getSchemeName()
193 .equals(jalviewSchemes[i].toString()));
198 @Test(groups = "Functional")
199 public void testGetColourScheme()
201 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
202 ">seq1\nAGLRTWQU", DataSourceType.PASTE);
203 ColourSchemes schemes = ColourSchemes.getInstance();
205 AnnotatedCollectionI al = af.getViewport().getAlignment();
207 for (JalviewColourScheme cs : JalviewColourScheme.values())
209 ColourSchemeI registered = schemes.getColourScheme(cs.toString(), al);
210 assertSame(registered.getClass(), cs.getSchemeClass());
212 af.closeMenuItem_actionPerformed(true);
215 @Test(groups = "Functional")
216 public void testRegisterColourScheme()
218 ColourSchemes.getInstance().registerColourScheme(new Stripy());
219 ColourSchemes.getInstance().registerColourScheme(new MyClustal());
220 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
221 "examples/uniref50.fa", DataSourceType.FILE);
223 * set a breakpoint here to see and play with the newly registered
224 * colour schemes in the AlignFrame colour menu
226 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
227 SequenceI seq = af.getViewport().getAlignment().findName("FER_CAPAA");
230 * set and check Taylor colours
232 af.changeColour_actionPerformed(JalviewColourScheme.Taylor.toString());
233 Color taylor1 = sr.getResidueColour(seq, 88, null); // E 255,0,102
234 Color taylor2 = sr.getResidueColour(seq, 89, null); // A 204,255,0
235 Color taylor3 = sr.getResidueColour(seq, 90, null); // G 255,153,0
236 assertEquals(taylor1, new Color(255, 0, 102));
237 assertEquals(taylor2, new Color(204, 255, 0));
238 assertEquals(taylor3, new Color(255, 153, 0));
241 * set and check Zappo colours
243 af.changeColour_actionPerformed(JalviewColourScheme.Zappo.toString());
244 Color zappo1 = sr.getResidueColour(seq, 88, null); // E red
245 Color zappo2 = sr.getResidueColour(seq, 89, null); // A pink
246 Color zappo3 = sr.getResidueColour(seq, 90, null); // G magenta
247 assertEquals(zappo1, Color.red);
248 assertEquals(zappo2, Color.pink);
249 assertEquals(zappo3, Color.magenta);
252 * set 'stripy' colours - odd columns are Taylor and even are Zappo
254 af.changeColour_actionPerformed("stripy");
255 Color stripy1 = sr.getResidueColour(seq, 88, null);
256 Color stripy2 = sr.getResidueColour(seq, 89, null);
257 Color stripy3 = sr.getResidueColour(seq, 90, null);
258 assertEquals(stripy1, zappo1);
259 assertEquals(stripy2, taylor2);
260 assertEquals(stripy3, zappo3);
263 * set and check Clustal colours
265 af.changeColour_actionPerformed(JalviewColourScheme.Clustal.toString());
266 Color clustal1 = sr.getResidueColour(seq, 88, null);
267 Color clustal2 = sr.getResidueColour(seq, 89, null);
268 Color clustal3 = sr.getResidueColour(seq, 90, null);
269 assertEquals(clustal1, ClustalColour.MAGENTA.colour);
270 assertEquals(clustal2, ClustalColour.BLUE.colour);
271 assertEquals(clustal3, ClustalColour.ORANGE.colour);
274 * set 'MyClustal' colours - uses AWT colour equivalents
276 af.changeColour_actionPerformed("MyClustal");
277 Color myclustal1 = sr.getResidueColour(seq, 88, null);
278 Color myclustal2 = sr.getResidueColour(seq, 89, null);
279 Color myclustal3 = sr.getResidueColour(seq, 90, null);
280 assertEquals(myclustal1, Color.MAGENTA);
281 assertEquals(myclustal2, Color.BLUE);
282 assertEquals(myclustal3, Color.ORANGE);
286 * Tests for check if scheme name exists. Built-in scheme names are the
287 * toString() values of enum JalviewColourScheme.
289 @Test(groups = "Functional")
290 public void testNameExists()
292 ColourSchemes cs = ColourSchemes.getInstance();
293 assertFalse(cs.nameExists(null));
294 assertFalse(cs.nameExists(""));
295 assertTrue(cs.nameExists("Clustal"));
296 assertTrue(cs.nameExists("CLUSTAL"));
297 assertFalse(cs.nameExists("CLUSTAL "));
298 assertTrue(cs.nameExists("% Identity"));
299 assertFalse(cs.nameExists("PID"));