Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 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.00%
ColourSchemesTest.Stripy 55 9 6
0.00%
ColourSchemesTest.MyClustal 113 22 13
0.00%
 

Contributing tests

No tests hitting this source file were found.

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  0 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  0 toggle private Stripy(ColourSchemeI cs1, ColourSchemeI cs2)
72    {
73  0 odd = (ResidueColourScheme) cs1;
74  0 even = (ResidueColourScheme) cs2;
75    }
76   
 
77  0 toggle @Override
78    public ColourSchemeI getInstance(AlignViewportI view,
79    AnnotatedCollectionI sg)
80    {
81  0 final ColourSchemeI cs1 = ColourSchemes.getInstance().getColourScheme(
82    JalviewColourScheme.Taylor.toString(),
83    (AnnotatedCollectionI) null);
84  0 final ColourSchemeI cs2 = ColourSchemes.getInstance().getColourScheme(
85    JalviewColourScheme.Zappo.toString(),
86    (AnnotatedCollectionI) null);
87  0 return new Stripy(cs1, cs2);
88    }
89   
 
90  0 toggle @Override
91    public Color findColour(char c, int j, SequenceI seq)
92    {
93  0 if (j % 2 == 1)
94    {
95  0 return odd.findColour(c, j, seq);
96    }
97    else
98    {
99  0 return even.findColour(c, j, seq);
100    }
101    }
102   
 
103  0 toggle @Override
104    public String getSchemeName()
105    {
106  0 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  0 toggle private MyClustal()
118    {
119    }
120   
 
121  0 toggle private MyClustal(AnnotatedCollectionI sg,
122    Map<SequenceI, SequenceCollectionI> hiddenRepSequences)
123    {
124  0 delegate = new ClustalxColourScheme(sg, hiddenRepSequences);
125    }
126   
 
127  0 toggle @Override
128    public Color findColour(char c, int j, SequenceI seq)
129    {
130  0 Color col = delegate.findColour(c, j, seq);
131  0 Color result = col;
132  0 if (col.equals(ClustalColour.BLUE.colour))
133    {
134  0 result = Color.blue;
135    }
136  0 else if (col.equals(ClustalColour.CYAN.colour))
137    {
138  0 result = Color.cyan;
139    }
140  0 else if (col.equals(ClustalColour.GREEN.colour))
141    {
142  0 result = Color.green;
143    }
144  0 else if (col.equals(ClustalColour.MAGENTA.colour))
145    {
146  0 result = Color.magenta;
147    }
148  0 else if (col.equals(ClustalColour.ORANGE.colour))
149    {
150  0 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  0 return result;
165    }
166   
 
167  0 toggle @Override
168    public ColourSchemeI getInstance(AlignViewportI view,
169    AnnotatedCollectionI sg)
170    {
171  0 return new MyClustal(sg, view.getHiddenRepSequences());
172    }
173   
 
174  0 toggle @Override
175    public String getSchemeName()
176    {
177  0 return "MyClustal";
178    }
179   
180    }
181   
 
182  0 toggle @BeforeClass(alwaysRun = true)
183    public static void setUpBeforeClass() throws Exception
184    {
185    /*
186    * use read-only test properties file
187    */
188  0 Cache.loadProperties("test/jalview/io/testProps.jvprops");
189  0 Jalview.main(new String[] { "--nonews" });
190    }
191   
 
192  0 toggle @AfterClass(alwaysRun = true)
193    public static void tearDownAfterClass() throws Exception
194    {
195  0 if (Desktop.instance != null)
196  0 Desktop.instance.closeAll_actionPerformed(null);
197    }
198   
 
199  0 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  0 Iterator<ColourSchemeI> schemes = ColourSchemes.getInstance()
208    .getColourSchemes().iterator();
209  0 JalviewColourScheme[] jalviewSchemes = JalviewColourScheme.values();
210  0 int i = 0;
211  0 while (schemes.hasNext() && i < jalviewSchemes.length)
212    {
213  0 assertTrue(schemes.next().getSchemeName()
214    .equals(jalviewSchemes[i].toString()));
215  0 i++;
216    }
217    }
218   
 
219  0 toggle @Test(groups = "Functional")
220    public void testGetColourScheme()
221    {
222  0 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
223    ">seq1\nAGLRTWQU", DataSourceType.PASTE);
224  0 ColourSchemes schemes = ColourSchemes.getInstance();
225   
226  0 AnnotatedCollectionI al = af.getViewport().getAlignment();
227   
228  0 for (JalviewColourScheme cs : JalviewColourScheme.values())
229    {
230  0 ColourSchemeI registered = schemes.getColourScheme(cs.toString(), al);
231  0 assertSame(registered.getClass(), cs.getSchemeClass());
232    }
233  0 af.closeMenuItem_actionPerformed(true);
234    }
235   
 
236  0 toggle @Test(groups = "Functional")
237    public void testRegisterColourScheme()
238    {
239  0 ColourSchemes.getInstance().registerColourScheme(new Stripy());
240  0 ColourSchemes.getInstance().registerColourScheme(new MyClustal());
241  0 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  0 SequenceRenderer sr = new SequenceRenderer(af.getViewport());
248  0 SequenceI seq = af.getViewport().getAlignment().findName("FER_CAPAA");
249   
250    /*
251    * set and check Taylor colours
252    */
253  0 af.changeColour_actionPerformed(JalviewColourScheme.Taylor.toString());
254  0 Color taylor1 = sr.getResidueColour(seq, 88, null); // E 255,0,102
255  0 Color taylor2 = sr.getResidueColour(seq, 89, null); // A 204,255,0
256  0 Color taylor3 = sr.getResidueColour(seq, 90, null); // G 255,153,0
257  0 assertEquals(taylor1, new Color(255, 0, 102));
258  0 assertEquals(taylor2, new Color(204, 255, 0));
259  0 assertEquals(taylor3, new Color(255, 153, 0));
260   
261    /*
262    * set and check Zappo colours
263    */
264  0 af.changeColour_actionPerformed(JalviewColourScheme.Zappo.toString());
265  0 Color zappo1 = sr.getResidueColour(seq, 88, null); // E red
266  0 Color zappo2 = sr.getResidueColour(seq, 89, null); // A pink
267  0 Color zappo3 = sr.getResidueColour(seq, 90, null); // G magenta
268  0 assertEquals(zappo1, Color.red);
269  0 assertEquals(zappo2, Color.pink);
270  0 assertEquals(zappo3, Color.magenta);
271   
272    /*
273    * set 'stripy' colours - odd columns are Taylor and even are Zappo
274    */
275  0 af.changeColour_actionPerformed("stripy");
276  0 Color stripy1 = sr.getResidueColour(seq, 88, null);
277  0 Color stripy2 = sr.getResidueColour(seq, 89, null);
278  0 Color stripy3 = sr.getResidueColour(seq, 90, null);
279  0 assertEquals(stripy1, zappo1);
280  0 assertEquals(stripy2, taylor2);
281  0 assertEquals(stripy3, zappo3);
282   
283    /*
284    * set and check Clustal colours
285    */
286  0 af.changeColour_actionPerformed(JalviewColourScheme.Clustal.toString());
287  0 Color clustal1 = sr.getResidueColour(seq, 88, null);
288  0 Color clustal2 = sr.getResidueColour(seq, 89, null);
289  0 Color clustal3 = sr.getResidueColour(seq, 90, null);
290  0 assertEquals(clustal1, ClustalColour.MAGENTA.colour);
291  0 assertEquals(clustal2, ClustalColour.BLUE.colour);
292  0 assertEquals(clustal3, ClustalColour.ORANGE.colour);
293   
294    /*
295    * set 'MyClustal' colours - uses AWT colour equivalents
296    */
297  0 af.changeColour_actionPerformed("MyClustal");
298  0 Color myclustal1 = sr.getResidueColour(seq, 88, null);
299  0 Color myclustal2 = sr.getResidueColour(seq, 89, null);
300  0 Color myclustal3 = sr.getResidueColour(seq, 90, null);
301  0 assertEquals(myclustal1, Color.MAGENTA);
302  0 assertEquals(myclustal2, Color.BLUE);
303  0 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  0 toggle @Test(groups = "Functional")
311    public void testNameExists()
312    {
313  0 ColourSchemes cs = ColourSchemes.getInstance();
314  0 assertFalse(cs.nameExists(null));
315  0 assertFalse(cs.nameExists(""));
316  0 assertTrue(cs.nameExists("Clustal"));
317  0 assertTrue(cs.nameExists("CLUSTAL"));
318  0 assertFalse(cs.nameExists("CLUSTAL "));
319  0 assertTrue(cs.nameExists("% Identity"));
320  0 assertFalse(cs.nameExists("PID"));
321    }
322    }