JAL-2325 apply license to new source files
[jalview.git] / test / jalview / schemes / ResidueColourSchemeTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.schemes;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.datamodel.Profile;
28 import jalview.datamodel.ProfileI;
29 import jalview.datamodel.Profiles;
30
31 import java.awt.Color;
32
33 import org.testng.annotations.Test;
34
35 public class ResidueColourSchemeTest
36 {
37   @Test(groups = "Functional")
38   public void testAboveThreshold()
39   {
40     /*
41      * make up profiles for this alignment:
42      * AR-Q
43      * AR--
44      * SR-T
45      * SR-T
46      */
47     ProfileI[] profiles = new ProfileI[4];
48     profiles[0] = new Profile(4, 0, 2, "AS");
49     profiles[1] = new Profile(4, 0, 4, "R");
50     profiles[2] = new Profile(4, 4, 0, "");
51     profiles[3] = new Profile(4, 1, 2, "T");
52     ResidueColourScheme rcs = new ResidueColourScheme();
53     rcs.setConsensus(new Profiles(profiles));
54     
55     /*
56      * no threshold
57      */
58     rcs.setThreshold(0, true);
59     assertTrue(rcs.aboveThreshold('a', 0));
60     assertTrue(rcs.aboveThreshold('S', 0));
61     assertFalse(rcs.aboveThreshold('W', 0));
62     assertTrue(rcs.aboveThreshold('R', 1));
63     assertFalse(rcs.aboveThreshold('W', 2));
64     assertTrue(rcs.aboveThreshold('t', 3));
65     assertFalse(rcs.aboveThreshold('Q', 3));
66
67     /*
68      * with threshold, include gaps
69      */
70     rcs.setThreshold(60, false);
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     assertFalse(rcs.aboveThreshold('t', 3)); // 50% < 60%
76
77     /*
78      * with threshold, ignore gaps
79      */
80     rcs.setThreshold(60, true);
81     assertFalse(rcs.aboveThreshold('a', 0));
82     assertFalse(rcs.aboveThreshold('S', 0));
83     assertTrue(rcs.aboveThreshold('R', 1));
84     assertFalse(rcs.aboveThreshold('W', 2));
85     assertTrue(rcs.aboveThreshold('t', 3)); // 67% > 60%
86   }
87
88   /**
89    * Test colour bleaching based on conservation score and conservation slider.
90    * Scores of 10 or 11 should leave colours unchanged. Gap is always white.
91    */
92   @Test(groups = "Functional")
93   public void testApplyConservation()
94   {
95     ResidueColourScheme rcs = new ResidueColourScheme();
96
97     // no conservation present - no fading
98     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 12));
99     
100     // cheat by setting conservation sequence directly
101     // rather than calculating it - good enough for this test
102     String consensus = "0123456789+*-";
103     rcs.conservation = consensus.toCharArray();
104
105     // column out of range:
106     assertEquals(Color.RED,
107             rcs.applyConservation(Color.RED, consensus.length()));
108
109     /*
110      * with 100% threshold, 'fade factor' is 
111      * (11-score)/10 * 100/20 = (11-score)/2
112      * which is >= 1 for all scores i.e. all fade to white except +, *
113      */
114     rcs.setConservationInc(100);
115     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 0));
116     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 1));
117     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 2));
118     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 3));
119     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 4));
120     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 5));
121     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 6));
122     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 7));
123     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 8));
124     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 9));
125     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 10));
126     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 11));
127     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 12));
128
129     /*
130      * with 0% threshold, there should be no fading
131      */
132     rcs.setConservationInc(0);
133     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 0));
134     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 1));
135     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 2));
136     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 3));
137     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 4));
138     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 5));
139     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 6));
140     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 7));
141     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 8));
142     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 9));
143     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 10));
144     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 11));
145     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 12)); // gap
146
147     /*
148      * with 40% threshold, 'fade factor' is 
149      * (11-score)/10 * 40/20 = (11-score)/5
150      * which is {>1, >1, >1, >1, >1, >1, 1, 0.8, 0.6, 0.4} for score 0-9
151      * e.g. score 7 colour fades 80% of the way to white (255, 255, 255)
152      */
153     rcs.setConservationInc(40);
154     Color colour = new Color(155, 105, 55);
155     assertEquals(Color.WHITE, rcs.applyConservation(colour, 0));
156     assertEquals(Color.WHITE, rcs.applyConservation(colour, 1));
157     assertEquals(Color.WHITE, rcs.applyConservation(colour, 2));
158     assertEquals(Color.WHITE, rcs.applyConservation(colour, 3));
159     assertEquals(Color.WHITE, rcs.applyConservation(colour, 4));
160     assertEquals(Color.WHITE, rcs.applyConservation(colour, 5));
161     assertEquals(Color.WHITE, rcs.applyConservation(colour, 6));
162     assertEquals(new Color(235, 225, 215), rcs.applyConservation(colour, 7));
163     assertEquals(new Color(215, 195, 175), rcs.applyConservation(colour, 8));
164     assertEquals(new Color(195, 165, 135), rcs.applyConservation(colour, 9));
165     assertEquals(colour, rcs.applyConservation(colour, 10));
166     assertEquals(colour, rcs.applyConservation(colour, 11));
167     assertEquals(Color.WHITE, rcs.applyConservation(colour, 12));
168   }
169 }