Clover icon

Coverage Report

  1. Project Clover database Fri Nov 15 2024 13:56:46 GMT
  2. Package jalview.schemes

File ColourSchemesTest.java

 

Code metrics

22
96
16
3
322
227
28
0.29
6
5.33
1.75

Classes

Class Line # Actions
ColourSchemesTest 49 65 9
0.986666798.7%
ColourSchemesTest.Stripy 55 9 6
1.0100%
ColourSchemesTest.MyClustal 113 22 13
0.6046511560.5%
 

Contributing tests

This file is covered by 68 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.schemes;
22   
23    import static org.testng.Assert.assertEquals;
24    import static org.testng.Assert.assertFalse;
25    import static org.testng.Assert.assertSame;
26    import static org.testng.Assert.assertTrue;
27   
28    import java.awt.Color;
29    import java.util.Iterator;
30    import java.util.Map;
31   
32    import org.testng.annotations.AfterClass;
33    import org.testng.annotations.BeforeClass;
34    import org.testng.annotations.Test;
35   
36    import jalview.api.AlignViewportI;
37    import jalview.bin.Cache;
38    import jalview.bin.Jalview;
39    import jalview.datamodel.AnnotatedCollectionI;
40    import jalview.datamodel.SequenceCollectionI;
41    import jalview.datamodel.SequenceI;
42    import jalview.gui.AlignFrame;
43    import jalview.gui.Desktop;
44    import jalview.gui.SequenceRenderer;
45    import jalview.io.DataSourceType;
46    import jalview.io.FileLoader;
47    import jalview.schemes.ClustalxColourScheme.ClustalColour;
48   
 
49    public class ColourSchemesTest
50    {
51    /*
52    * a colour scheme that alternates Taylor and Zappo
53    * colouring by column
54    */
 
55    class Stripy extends ResidueColourScheme
56    {
57    private ResidueColourScheme odd;
58   
59    private ResidueColourScheme even;
60   
 
61  1 toggle private Stripy()
62    {
63    }
64   
65    /**
66    * constructor given colours for odd and even columns
67    *
68    * @param odd
69    * @param even
70    */
 
71  1 toggle private Stripy(ColourSchemeI cs1, ColourSchemeI cs2)
72    {
73  1 odd = (ResidueColourScheme) cs1;
74  1 even = (ResidueColourScheme) cs2;
75    }
76   
 
77  1 toggle @Override
78    public ColourSchemeI getInstance(AlignViewportI view,
79    AnnotatedCollectionI sg)
80    {
81  1 final ColourSchemeI cs1 = ColourSchemes.getInstance().getColourScheme(
82    JalviewColourScheme.Taylor.toString(),
83    (AnnotatedCollectionI) null);
84  1 final ColourSchemeI cs2 = ColourSchemes.getInstance().getColourScheme(
85    JalviewColourScheme.Zappo.toString(),
86    (AnnotatedCollectionI) null);
87  1 return new Stripy(cs1, cs2);
88    }
89   
 
90  3 toggle @Override
91    public Color findColour(char c, int j, SequenceI seq)
92    {
93  3 if (j % 2 == 1)
94    {
95  1 return odd.findColour(c, j, seq);
96    }
97    else
98    {
99  2 return even.findColour(c, j, seq);
100    }
101    }
102   
 
103  164 toggle @Override
104    public String getSchemeName()
105    {
106  164 return "stripy";
107    }
108    };
109   
110    /*
111    * a colour scheme that is Clustal but using AWT colour equivalents
112    */
 
113    class MyClustal extends ResidueColourScheme
114    {
115    ClustalxColourScheme delegate;
116   
 
117  1 toggle private MyClustal()
118    {
119    }
120   
 
121  1 toggle private MyClustal(AnnotatedCollectionI sg,
122    Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
123    {
124  1 delegate = new ClustalxColourScheme(sg, hiddenRepSequences);
125    }
126   
 
127  3 toggle @Override
128    public Color findColour(char c, int j, SequenceI seq)
129    {
130  3 Color col = delegate.findColour(c, j, seq);
131  3 Color result = col;
132  3 if (col.equals(ClustalColour.BLUE.colour))
133    {
134  1 result = Color.blue;
135    }
136  2 else if (col.equals(ClustalColour.CYAN.colour))
137    {
138  0 result = Color.cyan;
139    }
140  2 else if (col.equals(ClustalColour.GREEN.colour))
141    {
142  0 result = Color.green;
143    }
144  2 else if (col.equals(ClustalColour.MAGENTA.colour))
145    {
146  1 result = Color.magenta;
147    }
148  1 else if (col.equals(ClustalColour.ORANGE.colour))
149    {
150  1 result = Color.orange;
151    }
152  0 else if (col.equals(ClustalColour.PINK.colour))
153    {
154  0 result = Color.pink;
155    }
156  0 else if (col.equals(ClustalColour.RED.colour))
157    {
158  0 result = Color.red;
159    }
160  0 else if (col.equals(ClustalColour.YELLOW.colour))
161    {
162  0 result = Color.yellow;
163    }
164  3 return result;
165    }
166   
 
167  1 toggle @Override
168    public ColourSchemeI getInstance(AlignViewportI view,
169    AnnotatedCollectionI sg)
170    {
171  1 return new MyClustal(sg, view.getHiddenRepSequences());
172    }
173   
 
174  164 toggle @Override
175    public String getSchemeName()
176    {
177  164 return "MyClustal";
178    }
179   
180    }
181   
 
182  1 toggle @BeforeClass(alwaysRun = true)
183    public static void setUpBeforeClass() throws Exception
184    {
185    /*
186    * use read-only test properties file
187    */
188  1 Cache.loadProperties("test/jalview/io/testProps.jvprops");
189  1 Jalview.main(new String[] { "--nonews" });
190    }
191   
 
192  1 toggle @AfterClass(alwaysRun = true)
193    public static void tearDownAfterClass() throws Exception
194    {
195  1 if (Desktop.instance != null)
196  1 Desktop.instance.closeAll_actionPerformed(null);
197    }
198   
 
199  1 toggle @Test(groups = "Functional")
200    public void testGetColourSchemes()
201    {
202    /*
203    * this just verifies that built-in colour schemes are loaded into ColourSchemes
204    * in the order in which they are declared in the JalviewColourScheme enum
205    * (this also determines their order in Colour menus)
206    */
207  1 Iterator<ColourSchemeI> schemes = ColourSchemes.getInstance()
208    .getColourSchemes().iterator();
209  1 JalviewColourScheme[] jalviewSchemes = JalviewColourScheme.values();
210  1 int i = 0;
211  21 while (schemes.hasNext() && i < jalviewSchemes.length)
212    {
213  20 assertTrue(schemes.next().getSchemeName()
214    .equals(jalviewSchemes[i].toString()));
215  20 i++;
216    }
217    }
218   
 
219  1 toggle @Test(groups = "Functional")
220    public void testGetColourScheme()
221    {
222  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
223    ">seq1\nAGLRTWQU", DataSourceType.PASTE);
224  1 ColourSchemes schemes = ColourSchemes.getInstance();
225   
226  1 AnnotatedCollectionI al = af.getViewport().getAlignment();
227   
228  1 for (JalviewColourScheme cs : JalviewColourScheme.values())
229    {
230  20 ColourSchemeI registered = schemes.getColourScheme(cs.toString(), al);
231  20 assertSame(registered.getClass(), cs.getSchemeClass());
232    }
233  1 af.closeMenuItem_actionPerformed(true);
234    }
235   
 
236  1 toggle @Test(groups = "Functional")
237    public void testRegisterColourScheme()
238    {
239  1 ColourSchemes.getInstance().registerColourScheme(new Stripy());
240  1 ColourSchemes.getInstance().registerColourScheme(new MyClustal());
241  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
242    "examples/uniref50.fa", DataSourceType.FILE);
243    /*
244    * set a breakpoint here to see and play with the newly registered
245    * colour schemes in the AlignFrame colour menu
246    */
247  1 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
248  1 SequenceI seq = af.getViewport().getAlignment().findName("FER_CAPAA");
249   
250    /*
251    * set and check Taylor colours
252    */
253  1 af.changeColour_actionPerformed(JalviewColourScheme.Taylor.toString());
254  1 Color taylor1 = sr.getResidueColour(seq, 88, null); // E 255,0,102
255  1 Color taylor2 = sr.getResidueColour(seq, 89, null); // A 204,255,0
256  1 Color taylor3 = sr.getResidueColour(seq, 90, null); // G 255,153,0
257  1 assertEquals(taylor1, new Color(255, 0, 102));
258  1 assertEquals(taylor2, new Color(204, 255, 0));
259  1 assertEquals(taylor3, new Color(255, 153, 0));
260   
261    /*
262    * set and check Zappo colours
263    */
264  1 af.changeColour_actionPerformed(JalviewColourScheme.Zappo.toString());
265  1 Color zappo1 = sr.getResidueColour(seq, 88, null); // E red
266  1 Color zappo2 = sr.getResidueColour(seq, 89, null); // A pink
267  1 Color zappo3 = sr.getResidueColour(seq, 90, null); // G magenta
268  1 assertEquals(zappo1, Color.red);
269  1 assertEquals(zappo2, Color.pink);
270  1 assertEquals(zappo3, Color.magenta);
271   
272    /*
273    * set 'stripy' colours - odd columns are Taylor and even are Zappo
274    */
275  1 af.changeColour_actionPerformed("stripy");
276  1 Color stripy1 = sr.getResidueColour(seq, 88, null);
277  1 Color stripy2 = sr.getResidueColour(seq, 89, null);
278  1 Color stripy3 = sr.getResidueColour(seq, 90, null);
279  1 assertEquals(stripy1, zappo1);
280  1 assertEquals(stripy2, taylor2);
281  1 assertEquals(stripy3, zappo3);
282   
283    /*
284    * set and check Clustal colours
285    */
286  1 af.changeColour_actionPerformed(JalviewColourScheme.Clustal.toString());
287  1 Color clustal1 = sr.getResidueColour(seq, 88, null);
288  1 Color clustal2 = sr.getResidueColour(seq, 89, null);
289  1 Color clustal3 = sr.getResidueColour(seq, 90, null);
290  1 assertEquals(clustal1, ClustalColour.MAGENTA.colour);
291  1 assertEquals(clustal2, ClustalColour.BLUE.colour);
292  1 assertEquals(clustal3, ClustalColour.ORANGE.colour);
293   
294    /*
295    * set 'MyClustal' colours - uses AWT colour equivalents
296    */
297  1 af.changeColour_actionPerformed("MyClustal");
298  1 Color myclustal1 = sr.getResidueColour(seq, 88, null);
299  1 Color myclustal2 = sr.getResidueColour(seq, 89, null);
300  1 Color myclustal3 = sr.getResidueColour(seq, 90, null);
301  1 assertEquals(myclustal1, Color.MAGENTA);
302  1 assertEquals(myclustal2, Color.BLUE);
303  1 assertEquals(myclustal3, Color.ORANGE);
304    }
305   
306    /**
307    * Tests for check if scheme name exists. Built-in scheme names are the
308    * toString() values of enum JalviewColourScheme.
309    */
 
310  1 toggle @Test(groups = "Functional")
311    public void testNameExists()
312    {
313  1 ColourSchemes cs = ColourSchemes.getInstance();
314  1 assertFalse(cs.nameExists(null));
315  1 assertFalse(cs.nameExists(""));
316  1 assertTrue(cs.nameExists("Clustal"));
317  1 assertTrue(cs.nameExists("CLUSTAL"));
318  1 assertFalse(cs.nameExists("CLUSTAL "));
319  1 assertTrue(cs.nameExists("% Identity"));
320  1 assertFalse(cs.nameExists("PID"));
321    }
322    }