Merge branch 'develop' into features/JAL-2360colourSchemeApplicability
[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.Alignment;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.AnnotatedCollectionI;
31 import jalview.datamodel.Annotation;
32 import jalview.datamodel.Profile;
33 import jalview.datamodel.ProfileI;
34 import jalview.datamodel.Profiles;
35 import jalview.datamodel.Sequence;
36 import jalview.datamodel.SequenceI;
37 import jalview.gui.JvOptionPane;
38 import jalview.io.TCoffeeScoreFile;
39
40 import java.awt.Color;
41
42 import org.testng.annotations.BeforeClass;
43 import org.testng.annotations.Test;
44
45 public class ResidueColourSchemeTest
46 {
47   @BeforeClass(alwaysRun = true)
48   public void setUp()
49   {
50
51   }
52
53   @BeforeClass(alwaysRun = true)
54   public void setUpJvOptionPane()
55   {
56     JvOptionPane.setInteractiveMode(false);
57     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58   }
59
60   @Test(groups = "Functional")
61   public void testAboveThreshold()
62   {
63     /*
64      * make up profiles for this alignment:
65      * AR-Q
66      * AR--
67      * SR-T
68      * SR-T
69      */
70     ProfileI[] profiles = new ProfileI[4];
71     profiles[0] = new Profile(4, 0, 2, "AS");
72     profiles[1] = new Profile(4, 0, 4, "R");
73     profiles[2] = new Profile(4, 4, 0, "");
74     profiles[3] = new Profile(4, 1, 2, "T");
75     ResidueColourScheme rcs = new PIDColourScheme();
76     rcs.setConsensus(new Profiles(profiles));
77     
78     /*
79      * no threshold
80      */
81     rcs.setThreshold(0, true);
82     assertTrue(rcs.aboveThreshold('a', 0));
83     assertTrue(rcs.aboveThreshold('S', 0));
84     assertTrue(rcs.aboveThreshold('W', 0));
85     assertTrue(rcs.aboveThreshold('R', 1));
86     assertTrue(rcs.aboveThreshold('W', 2));
87     assertTrue(rcs.aboveThreshold('t', 3));
88     assertTrue(rcs.aboveThreshold('Q', 3));
89
90     /*
91      * with threshold, include gaps
92      */
93     rcs.setThreshold(60, false);
94     assertFalse(rcs.aboveThreshold('a', 0));
95     assertFalse(rcs.aboveThreshold('S', 0));
96     assertTrue(rcs.aboveThreshold('R', 1));
97     assertFalse(rcs.aboveThreshold('W', 2));
98     assertFalse(rcs.aboveThreshold('t', 3)); // 50% < 60%
99
100     /*
101      * with threshold, ignore gaps
102      */
103     rcs.setThreshold(60, true);
104     assertFalse(rcs.aboveThreshold('a', 0));
105     assertFalse(rcs.aboveThreshold('S', 0));
106     assertTrue(rcs.aboveThreshold('R', 1));
107     assertFalse(rcs.aboveThreshold('W', 2));
108     assertTrue(rcs.aboveThreshold('t', 3)); // 67% > 60%
109   }
110
111   /**
112    * Test colour bleaching based on conservation score and conservation slider.
113    * Scores of 10 or 11 should leave colours unchanged. Gap is always white.
114    */
115   @Test(groups = "Functional")
116   public void testApplyConservation()
117   {
118     ResidueColourScheme rcs = new PIDColourScheme();
119
120     // no conservation present - no fading
121     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 12));
122     
123     // cheat by setting conservation sequence directly
124     // rather than calculating it - good enough for this test
125     String consensus = "0123456789+*-";
126     rcs.conservation = consensus.toCharArray();
127
128     // column out of range:
129     assertEquals(Color.RED,
130             rcs.applyConservation(Color.RED, consensus.length()));
131
132     /*
133      * with 100% threshold, 'fade factor' is 
134      * (11-score)/10 * 100/20 = (11-score)/2
135      * which is >= 1 for all scores i.e. all fade to white except +, *
136      */
137     rcs.setConservationInc(100);
138     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 0));
139     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 1));
140     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 2));
141     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 3));
142     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 4));
143     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 5));
144     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 6));
145     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 7));
146     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 8));
147     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 9));
148     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 10));
149     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 11));
150     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 12));
151
152     /*
153      * with 0% threshold, there should be no fading
154      */
155     rcs.setConservationInc(0);
156     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 0));
157     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 1));
158     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 2));
159     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 3));
160     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 4));
161     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 5));
162     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 6));
163     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 7));
164     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 8));
165     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 9));
166     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 10));
167     assertEquals(Color.RED, rcs.applyConservation(Color.RED, 11));
168     assertEquals(Color.WHITE, rcs.applyConservation(Color.RED, 12)); // gap
169
170     /*
171      * with 40% threshold, 'fade factor' is 
172      * (11-score)/10 * 40/20 = (11-score)/5
173      * which is {>1, >1, >1, >1, >1, >1, 1, 0.8, 0.6, 0.4} for score 0-9
174      * e.g. score 7 colour fades 80% of the way to white (255, 255, 255)
175      */
176     rcs.setConservationInc(40);
177     Color colour = new Color(155, 105, 55);
178     assertEquals(Color.WHITE, rcs.applyConservation(colour, 0));
179     assertEquals(Color.WHITE, rcs.applyConservation(colour, 1));
180     assertEquals(Color.WHITE, rcs.applyConservation(colour, 2));
181     assertEquals(Color.WHITE, rcs.applyConservation(colour, 3));
182     assertEquals(Color.WHITE, rcs.applyConservation(colour, 4));
183     assertEquals(Color.WHITE, rcs.applyConservation(colour, 5));
184     assertEquals(Color.WHITE, rcs.applyConservation(colour, 6));
185     assertEquals(new Color(235, 225, 215), rcs.applyConservation(colour, 7));
186     assertEquals(new Color(215, 195, 175), rcs.applyConservation(colour, 8));
187     assertEquals(new Color(195, 165, 135), rcs.applyConservation(colour, 9));
188     assertEquals(colour, rcs.applyConservation(colour, 10));
189     assertEquals(colour, rcs.applyConservation(colour, 11));
190     assertEquals(Color.WHITE, rcs.applyConservation(colour, 12));
191   }
192
193   @Test(groups = "Functional")
194   public void testIsApplicableTo()
195   {
196     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
197     SequenceI pep2 = new Sequence("pep2", "AILFQYG");
198     SequenceI dna1 = new Sequence("dna1", "ACTGAC");
199     SequenceI dna2 = new Sequence("dna2", "TCCAAG");
200     AlignmentI peptide = new Alignment(new SequenceI[] { pep1, pep2 });
201     AlignmentI nucleotide = new Alignment(new SequenceI[] { dna1, dna2 });
202
203     /*
204      * peptide-specific colour schemes
205      */
206     assertTrue(new ClustalxColourScheme(peptide, null)
207             .isApplicableTo(peptide));
208     assertFalse(new ClustalxColourScheme(nucleotide, null)
209             .isApplicableTo(nucleotide));
210     assertTrue(new Blosum62ColourScheme().isApplicableTo(peptide));
211     assertFalse(new Blosum62ColourScheme().isApplicableTo(nucleotide));
212     assertTrue(new BuriedColourScheme().isApplicableTo(peptide));
213     assertFalse(new BuriedColourScheme().isApplicableTo(nucleotide));
214     assertTrue(new HelixColourScheme().isApplicableTo(peptide));
215     assertFalse(new HelixColourScheme().isApplicableTo(nucleotide));
216     assertTrue(new HydrophobicColourScheme().isApplicableTo(peptide));
217     assertFalse(new HydrophobicColourScheme().isApplicableTo(nucleotide));
218     assertTrue(new StrandColourScheme().isApplicableTo(peptide));
219     assertFalse(new StrandColourScheme().isApplicableTo(nucleotide));
220     assertTrue(new TaylorColourScheme().isApplicableTo(peptide));
221     assertFalse(new TaylorColourScheme().isApplicableTo(nucleotide));
222     assertTrue(new TurnColourScheme().isApplicableTo(peptide));
223     assertFalse(new TurnColourScheme().isApplicableTo(nucleotide));
224     assertTrue(new ZappoColourScheme().isApplicableTo(peptide));
225     assertFalse(new ZappoColourScheme().isApplicableTo(nucleotide));
226
227     /*
228      * nucleotide-specific colour schemes
229      */
230     assertFalse(new NucleotideColourScheme().isApplicableTo(peptide));
231     assertTrue(new NucleotideColourScheme().isApplicableTo(nucleotide));
232     assertFalse(new PurinePyrimidineColourScheme().isApplicableTo(peptide));
233     assertTrue(new PurinePyrimidineColourScheme()
234             .isApplicableTo(nucleotide));
235     assertFalse(new RNAInteractionColourScheme().isApplicableTo(peptide));
236     assertTrue(new RNAInteractionColourScheme().isApplicableTo(nucleotide));
237
238     /*
239      * indifferent
240      */
241     assertTrue(new UserColourScheme().isApplicableTo(peptide));
242     assertTrue(new UserColourScheme().isApplicableTo(nucleotide));
243     assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}, 0, 0d)
244             .isApplicableTo(peptide));
245     assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}, 0, 0d)
246             .isApplicableTo(nucleotide));
247     ResidueColourScheme rcs = new PIDColourScheme();
248     assertTrue(rcs.isApplicableTo(peptide));
249     assertTrue(rcs.isApplicableTo(nucleotide));
250     assertTrue(new PIDColourScheme().isApplicableTo(peptide));
251     assertTrue(new PIDColourScheme().isApplicableTo(nucleotide));
252     assertTrue(new FollowerColourScheme().isApplicableTo(peptide));
253     assertTrue(new FollowerColourScheme().isApplicableTo(nucleotide));
254
255     /*
256      * TCoffee colour requires the presence of TCoffee score annotation
257      */
258     assertFalse(new TCoffeeColourScheme(peptide).isApplicableTo(peptide));
259     assertFalse(new TCoffeeColourScheme(nucleotide)
260             .isApplicableTo(nucleotide));
261     AlignmentAnnotation aa = new AlignmentAnnotation("T-COFFEE", "", null);
262     aa.setCalcId(TCoffeeScoreFile.TCOFFEE_SCORE);
263     peptide.addAnnotation(aa);
264     aa = new AlignmentAnnotation("T-COFFEE", "", null);
265     aa.setCalcId(TCoffeeScoreFile.TCOFFEE_SCORE);
266     nucleotide.addAnnotation(aa);
267     assertTrue(new TCoffeeColourScheme(peptide).isApplicableTo(peptide));
268     assertTrue(new TCoffeeColourScheme(nucleotide)
269             .isApplicableTo(nucleotide));
270
271     /*
272      * RNAHelices requires the presence of rna secondary structure
273      */
274     assertFalse(new RNAHelicesColour(peptide).isApplicableTo(peptide));
275     assertFalse(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide));
276     // add secondary structure (small but perfectly formed)
277     Annotation[] ss = new Annotation[2];
278     ss[0] = new Annotation("", "", '{', 0f);
279     ss[1] = new Annotation("", "", '}', 0f);
280     nucleotide.addAnnotation(new AlignmentAnnotation("SS", "", ss));
281     assertTrue(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide));
282   }
283
284   @Test(groups = "Functional")
285   public void testIsApplicableTo_dynamicColourScheme()
286   {
287     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
288     SequenceI pep2 = new Sequence("pep2", "AILFQYG");
289     AlignmentI peptide = new Alignment(new SequenceI[] { pep1, pep2 });
290   
291     /*
292      * demonstrate that we can 'plug in' a colour scheme with specified
293      * criteria for applicability; here, that there are more than 2 sequences
294      */
295     ColourSchemeI cs = new UserColourScheme()
296     {
297       @Override
298       public boolean isApplicableTo(AnnotatedCollectionI ac)
299       {
300         AlignmentI al = ac.getContext() == null ? (AlignmentI) ac
301                 : (AlignmentI) ac.getContext();
302         return al.getSequences().size() > 2;
303       }
304     };
305     assertFalse(cs.isApplicableTo(peptide));
306     peptide.addSequence(pep1);
307     assertTrue(cs.isApplicableTo(peptide));
308   }
309
310   @Test(groups = "Functional")
311   public void testGetName()
312   {
313     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
314     AlignmentI peptide = new Alignment(new SequenceI[] { pep1 });
315
316     assertEquals("Blosum62", new Blosum62ColourScheme().getSchemeName());
317     assertEquals("Buried Index", new BuriedColourScheme().getSchemeName());
318     assertEquals("Helix Propensity", new HelixColourScheme().getSchemeName());
319     assertEquals("Hydrophobic", new HydrophobicColourScheme().getSchemeName());
320     assertEquals("Strand Propensity", new StrandColourScheme().getSchemeName());
321     assertEquals("Taylor", new TaylorColourScheme().getSchemeName());
322     assertEquals("Turn Propensity", new TurnColourScheme().getSchemeName());
323     assertEquals("Zappo", new ZappoColourScheme().getSchemeName());
324     assertEquals("Nucleotide", new NucleotideColourScheme().getSchemeName());
325     assertEquals("Purine/Pyrimidine",
326             new PurinePyrimidineColourScheme().getSchemeName());
327     assertEquals("RNA Interaction type",
328             new RNAInteractionColourScheme().getSchemeName());
329     assertEquals("User Defined", new UserColourScheme().getSchemeName());
330     assertEquals("Score", new ScoreColourScheme(new int[] {},
331             new double[] {}, 0, 0d).getSchemeName());
332     assertEquals("% Identity", new PIDColourScheme().getSchemeName());
333     assertEquals("Follower", new FollowerColourScheme().getSchemeName());
334     assertEquals("T-Coffee Scores",
335             new TCoffeeColourScheme(peptide).getSchemeName());
336     assertEquals("RNA Helices",
337             new RNAHelicesColour(peptide).getSchemeName());
338   }
339 }