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