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