dcd9f94c9e1b9e68763a41c7dc5267f1f26be735
[jalview.git] / test / jalview / schemes / ResidueColourSchemeTest.java
1 package jalview.schemes;
2
3 import static org.testng.AssertJUnit.assertEquals;
4 import static org.testng.AssertJUnit.assertFalse;
5 import static org.testng.AssertJUnit.assertTrue;
6
7 import jalview.datamodel.Profile;
8
9 import java.awt.Color;
10
11 import org.testng.annotations.Test;
12
13 public class ResidueColourSchemeTest
14 {
15   @Test(groups = "Functional")
16   public void testAboveThreshold()
17   {
18     /*
19      * make up profiles for this alignment:
20      * AR-Q
21      * AR--
22      * SR-T
23      * SR-T
24      */
25     Profile[] profiles = new Profile[4]; 
26     profiles[0] = new Profile(4, 0, 2, "AS");
27     profiles[1] = new Profile(4, 0, 4, "R");
28     profiles[2] = new Profile(4, 4, 0, "");
29     profiles[3] = new Profile(4, 1, 2, "T");
30     ResidueColourScheme rcs = new ResidueColourScheme();
31     rcs.setConsensus(profiles);
32     
33     /*
34      * no threshold
35      */
36     rcs.setThreshold(0, true);
37     assertTrue(rcs.aboveThreshold('a', 0));
38     assertTrue(rcs.aboveThreshold('S', 0));
39     assertFalse(rcs.aboveThreshold('W', 0));
40     assertTrue(rcs.aboveThreshold('R', 1));
41     assertFalse(rcs.aboveThreshold('W', 2));
42     assertTrue(rcs.aboveThreshold('t', 3));
43     assertFalse(rcs.aboveThreshold('Q', 3));
44
45     /*
46      * with threshold, include gaps
47      */
48     rcs.setThreshold(60, false);
49     assertFalse(rcs.aboveThreshold('a', 0));
50     assertFalse(rcs.aboveThreshold('S', 0));
51     assertTrue(rcs.aboveThreshold('R', 1));
52     assertFalse(rcs.aboveThreshold('W', 2));
53     assertFalse(rcs.aboveThreshold('t', 3)); // 50% < 60%
54
55     /*
56      * with threshold, ignore gaps
57      */
58     rcs.setThreshold(60, true);
59     assertFalse(rcs.aboveThreshold('a', 0));
60     assertFalse(rcs.aboveThreshold('S', 0));
61     assertTrue(rcs.aboveThreshold('R', 1));
62     assertFalse(rcs.aboveThreshold('W', 2));
63     assertTrue(rcs.aboveThreshold('t', 3)); // 67% > 60%
64   }
65
66   /**
67    * Test colour bleaching based on conservation score and conservation slider.
68    * Scores of 10 or 11 should leave colours unchanged. Gap is always white.
69    */
70   @Test(groups = "Functional")
71   public void testApplyConservation()
72   {
73     ResidueColourScheme rcs = new ResidueColourScheme();
74
75     // no conservation present - no fading
76     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 12));
77     
78     // cheat by setting conservation sequence directly
79     // rather than calculating it - good enough for this test
80     String consensus = "0123456789+*-";
81     rcs.conservation = consensus.toCharArray();
82
83     // column out of range:
84     assertEquals(Color.RED,
85             rcs.applyConservation(Color.RED, consensus.length()));
86
87     /*
88      * with 100% threshold, 'fade factor' is 
89      * (11-score)/10 * 100/20 = (11-score)/2
90      * which is >= 1 for all scores i.e. all fade to white except +, *
91      */
92     rcs.setConservationInc(100);
93     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 0));
94     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 1));
95     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 2));
96     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 3));
97     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 4));
98     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 5));
99     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 6));
100     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 7));
101     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 8));
102     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 9));
103     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 10));
104     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 11));
105     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 12));
106
107     /*
108      * with 0% threshold, there should be no fading
109      */
110     rcs.setConservationInc(0);
111     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 0));
112     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 1));
113     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 2));
114     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 3));
115     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 4));
116     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 5));
117     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 6));
118     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 7));
119     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 8));
120     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 9));
121     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 10));
122     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 11));
123     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 12)); // gap
124
125     /*
126      * with 40% threshold, 'fade factor' is 
127      * (11-score)/10 * 40/20 = (11-score)/5
128      * which is {>1, >1, >1, >1, >1, >1, 1, 0.8, 0.6, 0.4} for score 0-9
129      * e.g. score 7 colour fades 80% of the way to white (255, 255, 255)
130      */
131     rcs.setConservationInc(40);
132     Color colour = new Color(155, 105, 55);
133     assertEquals(Color.WHITE, rcs.applyConservation(colour, 0));
134     assertEquals(Color.WHITE, rcs.applyConservation(colour, 1));
135     assertEquals(Color.WHITE, rcs.applyConservation(colour, 2));
136     assertEquals(Color.WHITE, rcs.applyConservation(colour, 3));
137     assertEquals(Color.WHITE, rcs.applyConservation(colour, 4));
138     assertEquals(Color.WHITE, rcs.applyConservation(colour, 5));
139     assertEquals(Color.WHITE, rcs.applyConservation(colour, 6));
140     assertEquals(new Color(235, 225, 215), rcs.applyConservation(colour, 7));
141     assertEquals(new Color(215, 195, 175), rcs.applyConservation(colour, 8));
142     assertEquals(new Color(195, 165, 135), rcs.applyConservation(colour, 9));
143     assertEquals(colour, rcs.applyConservation(colour, 10));
144     assertEquals(colour, rcs.applyConservation(colour, 11));
145     assertEquals(Color.WHITE, rcs.applyConservation(colour, 12));
146   }
147 }