1 package jalview.schemes;
3 import static org.testng.Assert.assertEquals;
5 import jalview.datamodel.Alignment;
6 import jalview.datamodel.AlignmentAnnotation;
7 import jalview.datamodel.AlignmentI;
8 import jalview.datamodel.Annotation;
9 import jalview.datamodel.GraphLine;
10 import jalview.datamodel.Sequence;
11 import jalview.datamodel.SequenceI;
13 import java.awt.Color;
15 import org.testng.annotations.BeforeClass;
16 import org.testng.annotations.Test;
18 public class AnnotationColourGradientTest
20 final static int WIDTH = 11;
22 final static int THRESHOLD_FIVE = 5;
24 private AlignmentAnnotation ann;
26 private SequenceI seq;
28 private AlignmentI al;
30 Color minColour = new Color(50, 200, 150);
32 Color maxColour = new Color(150, 100, 250);
35 * Setup creates an annotation over 11 columns with values 0-10 and threshold
38 @BeforeClass(alwaysRun = true)
41 Annotation[] anns = new Annotation[WIDTH];
43 * set annotations with values 0-10, graded colours
45 for (int col = 0; col < WIDTH; col++)
48 Color colour = new Color(hue, hue, hue);
49 anns[col] = new Annotation("a", "a", 'a', col, colour);
52 seq = new Sequence("", "");
53 al = new Alignment(new SequenceI[]{ seq});
56 * AlignmentAnnotation constructor works out min-max range
58 ann = new AlignmentAnnotation("", "", anns);
59 ann.setThreshold(new GraphLine(THRESHOLD_FIVE, "", Color.RED));
60 seq.addAlignmentAnnotation(ann);
63 @Test(groups = "Functional")
64 public void testShadeCalculation_noThreshold()
66 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
67 minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
68 for (int col = 0; col < WIDTH; col++)
70 Color result = testee.shadeCalculation(ann, col);
72 * column <n> is n/10 of the way from minCol to maxCol
74 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
76 assertEquals(result, expected, "for column " + col);
81 * Test the 'colour above threshold' case
83 @Test(groups = "Functional")
84 public void testShadeCalculation_aboveThreshold()
86 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
87 minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD);
88 for (int col = 0; col < WIDTH; col++)
90 Color result = testee.shadeCalculation(ann, col);
92 * colour is derived regardless of the threshold value
93 * (the renderer will suppress colouring if above/below threshold)
95 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
97 assertEquals(result, expected, "for column " + col);
101 * now make 6-10 the span of the colour range
102 * (annotation value == column number in this test)
104 testee.setThresholdIsMinMax(true);
105 for (int col = 0; col < THRESHOLD_FIVE; col++)
108 * colours below the threshold are computed as before
110 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
112 Color result = testee.shadeCalculation(ann, col);
113 assertEquals(result, expected, "for column " + col);
115 for (int col = THRESHOLD_FIVE; col < WIDTH; col++)
118 * colours for values >= threshold are graduated
119 * range is 6-10 so steps of 100/5 = 20
121 int factor = col - THRESHOLD_FIVE;
122 Color expected = new Color(50 + 20 * factor, 200 - 20 * factor,
124 Color result = testee.shadeCalculation(ann, col);
125 assertEquals(result, expected, "for column " + col);
130 * Test the 'colour below threshold' case
132 @Test(groups = "Functional")
133 public void testShadeCalculation_belowThreshold()
135 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
136 minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD);
138 for (int col = 0; col < WIDTH; col++)
140 Color result = testee.shadeCalculation(ann, col);
142 * colour is derived regardless of the threshold value
143 * (the renderer will suppress colouring if above/below threshold)
145 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
147 assertEquals(result, expected, "for column " + col);
151 * now make 0-5 the span of the colour range
152 * (annotation value == column number in this test)
154 testee.setThresholdIsMinMax(true);
155 for (int col = THRESHOLD_FIVE + 1; col < WIDTH; col++)
158 * colours above the threshold are computed as before
160 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
162 Color result = testee.shadeCalculation(ann, col);
163 assertEquals(result, expected, "for column " + col);
166 for (int col = 0; col <= THRESHOLD_FIVE; col++)
169 * colours for values <= threshold are graduated
170 * range is 0-5 so steps of 100/5 = 20
172 Color expected = new Color(50 + 20 * col, 200 - 20 * col,
174 Color result = testee.shadeCalculation(ann, col);
175 assertEquals(result, expected, "for column " + col);
180 * Test the 'colour above threshold' case
182 @Test(groups = "Functional")
183 public void testFindColour_aboveThreshold()
185 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
186 minColour, maxColour, AnnotationColourGradient.ABOVE_THRESHOLD);
187 testee = (AnnotationColourGradient) testee.getInstance(al, null);
189 for (int col = 0; col < WIDTH; col++)
191 Color result = testee.findColour('a', col, seq);
193 * expect white below threshold of 5
195 Color expected = col < 5 ? Color.white : new Color(50 + 10 * col,
198 assertEquals(result, expected, "for column " + col);
202 * now make 6-10 the span of the colour range
203 * (annotation value == column number in this test)
205 testee.setThresholdIsMinMax(true);
206 for (int col = 0; col < WIDTH; col++)
209 * colours for values >= threshold are graduated
210 * range is 6-10 so steps of 100/5 = 20
212 int factor = col - THRESHOLD_FIVE;
213 Color expected = col < 5 ? Color.white : new Color(50 + 20 * factor,
216 Color result = testee.findColour('a', col, seq);
217 assertEquals(result, expected, "for column " + col);
222 * Test the 'colour below threshold' case
224 @Test(groups = "Functional")
225 public void testFindColour_belowThreshold()
227 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
228 minColour, maxColour, AnnotationColourGradient.BELOW_THRESHOLD);
229 testee = (AnnotationColourGradient) testee.getInstance(al, null);
231 for (int col = 0; col < WIDTH; col++)
233 Color result = testee.findColour('a', col, seq);
234 Color expected = col > 5 ? Color.white : new Color(50 + 10 * col,
235 200 - 10 * col, 150 + 10 * col);
236 assertEquals(result, expected, "for column " + col);
240 * now make 0-5 the span of the colour range
241 * (annotation value == column number in this test)
243 testee.setThresholdIsMinMax(true);
244 for (int col = 0; col < WIDTH; col++)
247 * colours for values <= threshold are graduated
248 * range is 0-5 so steps of 100/5 = 20
250 Color expected = col > 5 ? Color.white : new Color(50 + 20 * col,
251 200 - 20 * col, 150 + 20 * col);
252 Color result = testee.findColour('a', col, seq);
253 assertEquals(result, expected, "for column " + col);
257 @Test(groups = "Functional")
258 public void testFindColour_noThreshold()
260 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
261 minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
262 testee = (AnnotationColourGradient) testee.getInstance(al, null);
264 for (int col = 0; col < WIDTH; col++)
266 Color result = testee.findColour('a', col, seq);
268 * column <n> is n/10 of the way from minCol to maxCol
270 Color expected = new Color(50 + 10 * col, 200 - 10 * col,
272 assertEquals(result, expected, "for column " + col);
276 @Test(groups = "Functional")
277 public void testFindColour_originalColours()
279 AnnotationColourGradient testee = new AnnotationColourGradient(ann,
280 minColour, maxColour, AnnotationColourGradient.NO_THRESHOLD);
281 testee = (AnnotationColourGradient) testee.getInstance(al, null);
284 * flag corresponding to 'use original colours' checkbox
285 * - just use the individual annotation colours
287 testee.setPredefinedColours(true);
290 * the annotation colour is returned, except for column 0 where it is
291 * black - in this case the colour scheme colour overrides it
293 for (int col = 0; col < WIDTH; col++)
296 Color c = col == 0 ? minColour : new Color(hue, hue, hue);
297 assertEquals(testee.findColour('a', col, seq), c, "for column " + col);