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