1 package jalview.renderer;
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
7 import jalview.analysis.Conservation;
8 import jalview.datamodel.Profile;
9 import jalview.datamodel.ProfileI;
10 import jalview.datamodel.Profiles;
11 import jalview.datamodel.ProfilesI;
12 import jalview.datamodel.ResidueCount;
13 import jalview.datamodel.Sequence;
14 import jalview.datamodel.SequenceI;
15 import jalview.schemes.ColourSchemeI;
16 import jalview.schemes.PIDColourScheme;
17 import jalview.schemes.ResidueProperties;
18 import jalview.schemes.UserColourScheme;
19 import jalview.schemes.ZappoColourScheme;
21 import java.awt.Color;
22 import java.util.Collections;
24 import org.testng.annotations.Test;
26 public class ResidueShaderTest
29 @Test(groups = "Functional")
30 public void testAboveThreshold()
33 * make up profiles for this alignment:
39 ProfileI[] profiles = new ProfileI[4];
40 profiles[0] = new Profile(4, 0, 2, "AS");
41 profiles[1] = new Profile(4, 0, 4, "R");
42 profiles[2] = new Profile(4, 4, 0, "");
43 profiles[3] = new Profile(4, 1, 2, "T");
44 ResidueShader ccs = new ResidueShader(new PIDColourScheme());
45 ccs.setConsensus(new Profiles(profiles));
50 ccs.setThreshold(0, true);
51 assertTrue(ccs.aboveThreshold('a', 0));
52 assertTrue(ccs.aboveThreshold('S', 0));
53 assertTrue(ccs.aboveThreshold('W', 0));
54 assertTrue(ccs.aboveThreshold('R', 1));
55 assertTrue(ccs.aboveThreshold('W', 2));
56 assertTrue(ccs.aboveThreshold('t', 3));
57 assertTrue(ccs.aboveThreshold('Q', 3));
60 * with threshold, include gaps
62 ccs.setThreshold(60, false);
63 assertFalse(ccs.aboveThreshold('a', 0));
64 assertFalse(ccs.aboveThreshold('S', 0));
65 assertTrue(ccs.aboveThreshold('R', 1));
66 assertFalse(ccs.aboveThreshold('W', 2));
67 assertFalse(ccs.aboveThreshold('t', 3)); // 50% < 60%
70 * with threshold, ignore gaps
72 ccs.setThreshold(60, true);
73 assertFalse(ccs.aboveThreshold('a', 0));
74 assertFalse(ccs.aboveThreshold('S', 0));
75 assertTrue(ccs.aboveThreshold('R', 1));
76 assertFalse(ccs.aboveThreshold('W', 2));
77 assertTrue(ccs.aboveThreshold('t', 3)); // 67% > 60%
81 * Test colour bleaching based on conservation score and conservation slider.
82 * Scores of 10 or 11 should leave colours unchanged. Gap is always white.
84 @Test(groups = "Functional")
85 public void testApplyConservation()
87 ResidueShader ccs = new ResidueShader(new PIDColourScheme());
89 // no conservation present - no fading
90 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 12));
93 * stub Conservation to return a given consensus string
95 final String consSequence = "0123456789+*-";
96 Conservation cons = new Conservation(null,
97 Collections.<SequenceI> emptyList(), 0, 0)
100 public SequenceI getConsSequence()
102 return new Sequence("seq", consSequence);
105 ccs.setConservation(cons);
107 // column out of range:
108 assertEquals(Color.RED,
109 ccs.applyConservation(Color.RED, consSequence.length()));
112 * with 100% threshold, 'fade factor' is
113 * (11-score)/10 * 100/20 = (11-score)/2
114 * which is >= 1 for all scores i.e. all fade to white except +, *
116 ccs.setConservationInc(100);
117 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 0));
118 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 1));
119 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 2));
120 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 3));
121 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 4));
122 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 5));
123 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 6));
124 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 7));
125 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 8));
126 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 9));
127 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 10));
128 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 11));
129 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 12));
132 * with 0% threshold, there should be no fading
134 ccs.setConservationInc(0);
135 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 0));
136 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 1));
137 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 2));
138 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 3));
139 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 4));
140 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 5));
141 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 6));
142 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 7));
143 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 8));
144 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 9));
145 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 10));
146 assertEquals(Color.RED, ccs.applyConservation(Color.RED, 11));
147 assertEquals(Color.WHITE, ccs.applyConservation(Color.RED, 12)); // gap
150 * with 40% threshold, 'fade factor' is
151 * (11-score)/10 * 40/20 = (11-score)/5
152 * which is {>1, >1, >1, >1, >1, >1, 1, 0.8, 0.6, 0.4} for score 0-9
153 * e.g. score 7 colour fades 80% of the way to white (255, 255, 255)
155 ccs.setConservationInc(40);
156 Color colour = new Color(155, 105, 55);
157 assertEquals(Color.WHITE, ccs.applyConservation(colour, 0));
158 assertEquals(Color.WHITE, ccs.applyConservation(colour, 1));
159 assertEquals(Color.WHITE, ccs.applyConservation(colour, 2));
160 assertEquals(Color.WHITE, ccs.applyConservation(colour, 3));
161 assertEquals(Color.WHITE, ccs.applyConservation(colour, 4));
162 assertEquals(Color.WHITE, ccs.applyConservation(colour, 5));
163 assertEquals(Color.WHITE, ccs.applyConservation(colour, 6));
164 assertEquals(new Color(235, 225, 215), ccs.applyConservation(colour, 7));
165 assertEquals(new Color(215, 195, 175), ccs.applyConservation(colour, 8));
166 assertEquals(new Color(195, 165, 135), ccs.applyConservation(colour, 9));
167 assertEquals(colour, ccs.applyConservation(colour, 10));
168 assertEquals(colour, ccs.applyConservation(colour, 11));
169 assertEquals(Color.WHITE, ccs.applyConservation(colour, 12));
172 @Test(groups = "Functional")
173 public void testFindColour_gapColour()
176 * normally, a gap is coloured white
178 ResidueShader rs = new ResidueShader(new ZappoColourScheme());
179 assertEquals(Color.white, rs.findColour(' ', 7, null));
182 * a User Colour Scheme may specify a bespoke gap colour
184 Color[] colours = new Color[ResidueProperties.maxProteinIndex + 1];
185 colours[5] = Color.blue; // Q colour
186 colours[23] = Color.red; // gap colour
187 ColourSchemeI cs = new UserColourScheme(colours);
188 rs = new ResidueShader(cs);
190 assertEquals(Color.red, rs.findColour(' ', 7, null));
191 assertEquals(Color.blue, rs.findColour('Q', 7, null));
194 * stub Conservation to return a given consensus string
196 final String consSequence = "0123456789+*-";
197 Conservation cons = new Conservation(null,
198 Collections.<SequenceI> emptyList(), 0, 0)
201 public SequenceI getConsSequence()
203 return new Sequence("seq", consSequence);
206 rs.setConservation(cons);
209 * with 0% threshold, there should be no fading
211 rs.setConservationInc(0);
212 assertEquals(Color.red, rs.findColour(' ', 7, null));
213 assertEquals(Color.blue, rs.findColour('Q', 7, null));
216 * with 40% threshold, 'fade factor' is
217 * (11-score)/10 * 40/20 = (11-score)/5
218 * so position 7, score 7 fades 80% of the way to white (255, 255, 255)
220 rs.setConservationInc(40);
223 * gap colour is unchanged for Conservation
225 assertEquals(Color.red, rs.findColour(' ', 7, null));
226 assertEquals(Color.red, rs.findColour('-', 7, null));
227 assertEquals(Color.red, rs.findColour('.', 7, null));
230 * residue colour is faded 80% of the way from
231 * blue(0, 0, 255) to white(255, 255, 255)
232 * making (204, 204, 255)
234 assertEquals(new Color(204, 204, 255), rs.findColour('Q', 7, null));
237 * turn off By Conservation, apply Above Identity Threshold
238 * providing a stub Consensus that has modal residue "Q" with pid 60%
240 rs.setConservationApplied(false);
241 ProfilesI consensus = getStubConsensus("Q", 60f);
242 rs.setConsensus(consensus);
244 // with consensus pid (60) above threshold (50), colours are unchanged
245 rs.setThreshold(50, false);
246 assertEquals(Color.blue, rs.findColour('Q', 7, null));
247 assertEquals(Color.red, rs.findColour('-', 7, null));
249 // with consensus pid (60) below threshold (70),
250 // residue colour becomes white, gap colour is unchanged
251 rs.setThreshold(70, false);
252 assertEquals(Color.white, rs.findColour('Q', 7, null));
253 assertEquals(Color.red, rs.findColour('-', 7, null));
257 * @param modalResidue
261 protected ProfilesI getStubConsensus(final String modalResidue,
264 ProfilesI consensus = new ProfilesI() {
267 public ProfileI get(int i)
269 return new ProfileI() {
271 public void setCounts(ResidueCount residueCounts)
276 public float getPercentageIdentity(boolean ignoreGaps)
282 public ResidueCount getCounts()
288 public int getHeight()
294 public int getGapped()
300 public int getMaxCount()
306 public String getModalResidue()
312 public int getNonGapped()
319 public int getStartColumn()
325 public int getEndColumn()