e6dc9e61585b47ed2680806790b8727e04b663d2
[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     assertFalse(rcs.aboveThreshold('W', 0));
85     assertTrue(rcs.aboveThreshold('R', 1));
86     assertFalse(rcs.aboveThreshold('W', 2));
87     assertTrue(rcs.aboveThreshold('t', 3));
88     assertFalse(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     // Blosum requires presence of Conservation annotation
211     assertFalse(new Blosum62ColourScheme().isApplicableTo(peptide));
212     assertFalse(new Blosum62ColourScheme().isApplicableTo(nucleotide));
213     assertTrue(new BuriedColourScheme().isApplicableTo(peptide));
214     assertFalse(new BuriedColourScheme().isApplicableTo(nucleotide));
215     assertTrue(new HelixColourScheme().isApplicableTo(peptide));
216     assertFalse(new HelixColourScheme().isApplicableTo(nucleotide));
217     assertTrue(new HydrophobicColourScheme().isApplicableTo(peptide));
218     assertFalse(new HydrophobicColourScheme().isApplicableTo(nucleotide));
219     assertTrue(new StrandColourScheme().isApplicableTo(peptide));
220     assertFalse(new StrandColourScheme().isApplicableTo(nucleotide));
221     assertTrue(new TaylorColourScheme().isApplicableTo(peptide));
222     assertFalse(new TaylorColourScheme().isApplicableTo(nucleotide));
223     assertTrue(new TurnColourScheme().isApplicableTo(peptide));
224     assertFalse(new TurnColourScheme().isApplicableTo(nucleotide));
225     assertTrue(new ZappoColourScheme().isApplicableTo(peptide));
226     assertFalse(new ZappoColourScheme().isApplicableTo(nucleotide));
227
228     peptide.addAnnotation(new AlignmentAnnotation("Conservation",
229             "Conservation", new Annotation[1], 0f, 11f,
230             AlignmentAnnotation.BAR_GRAPH));
231     assertTrue(new Blosum62ColourScheme().isApplicableTo(peptide));
232
233     /*
234      * nucleotide-specific colour schemes
235      */
236     assertFalse(new NucleotideColourScheme().isApplicableTo(peptide));
237     assertTrue(new NucleotideColourScheme().isApplicableTo(nucleotide));
238     assertFalse(new PurinePyrimidineColourScheme().isApplicableTo(peptide));
239     assertTrue(new PurinePyrimidineColourScheme()
240             .isApplicableTo(nucleotide));
241     assertFalse(new RNAInteractionColourScheme().isApplicableTo(peptide));
242     assertTrue(new RNAInteractionColourScheme().isApplicableTo(nucleotide));
243
244     /*
245      * indifferent
246      */
247     assertTrue(new UserColourScheme().isApplicableTo(peptide));
248     assertTrue(new UserColourScheme().isApplicableTo(nucleotide));
249     assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}, 0, 0d)
250             .isApplicableTo(peptide));
251     assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}, 0, 0d)
252             .isApplicableTo(nucleotide));
253     ResidueColourScheme rcs = new PIDColourScheme();
254     assertTrue(rcs.isApplicableTo(peptide));
255     assertTrue(rcs.isApplicableTo(nucleotide));
256     assertTrue(new PIDColourScheme().isApplicableTo(peptide));
257     assertTrue(new PIDColourScheme().isApplicableTo(nucleotide));
258     assertTrue(new FollowerColourScheme().isApplicableTo(peptide));
259     assertTrue(new FollowerColourScheme().isApplicableTo(nucleotide));
260
261     /*
262      * TCoffee colour requires the presence of TCoffee score annotation
263      */
264     assertFalse(new TCoffeeColourScheme(peptide).isApplicableTo(peptide));
265     assertFalse(new TCoffeeColourScheme(nucleotide)
266             .isApplicableTo(nucleotide));
267     AlignmentAnnotation aa = new AlignmentAnnotation("T-COFFEE", "", null);
268     aa.setCalcId(TCoffeeScoreFile.TCOFFEE_SCORE);
269     peptide.addAnnotation(aa);
270     aa = new AlignmentAnnotation("T-COFFEE", "", null);
271     aa.setCalcId(TCoffeeScoreFile.TCOFFEE_SCORE);
272     nucleotide.addAnnotation(aa);
273     assertTrue(new TCoffeeColourScheme(peptide).isApplicableTo(peptide));
274     assertTrue(new TCoffeeColourScheme(nucleotide)
275             .isApplicableTo(nucleotide));
276
277     /*
278      * RNAHelices requires the presence of rna secondary structure
279      */
280     assertFalse(new RNAHelicesColour(peptide).isApplicableTo(peptide));
281     assertFalse(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide));
282     // add secondary structure (small but perfectly formed)
283     Annotation[] ss = new Annotation[2];
284     ss[0] = new Annotation("", "", '{', 0f);
285     ss[1] = new Annotation("", "", '}', 0f);
286     nucleotide.addAnnotation(new AlignmentAnnotation("SS", "", ss));
287     assertTrue(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide));
288   }
289
290   @Test(groups = "Functional")
291   public void testIsApplicableTo_dynamicColourScheme()
292   {
293     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
294     SequenceI pep2 = new Sequence("pep2", "AILFQYG");
295     AlignmentI peptide = new Alignment(new SequenceI[] { pep1, pep2 });
296   
297     /*
298      * demonstrate that we can 'plug in' a colour scheme with specified
299      * criteria for applicability; here, that there are more than 2 sequences
300      */
301     ColourSchemeI cs = new UserColourScheme()
302     {
303       @Override
304       public boolean isApplicableTo(AnnotatedCollectionI ac)
305       {
306         AlignmentI al = ac.getContext() == null ? (AlignmentI) ac
307                 : (AlignmentI) ac.getContext();
308         return al.getSequences().size() > 2;
309       }
310     };
311     assertFalse(cs.isApplicableTo(peptide));
312     peptide.addSequence(pep1);
313     assertTrue(cs.isApplicableTo(peptide));
314   }
315
316   @Test(groups = "Functional")
317   public void testGetName()
318   {
319     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
320     AlignmentI peptide = new Alignment(new SequenceI[] { pep1 });
321
322     assertEquals("Blosum62", new Blosum62ColourScheme().getSchemeName());
323     assertEquals("Buried Index", new BuriedColourScheme().getSchemeName());
324     assertEquals("Helix Propensity", new HelixColourScheme().getSchemeName());
325     assertEquals("Hydrophobic", new HydrophobicColourScheme().getSchemeName());
326     assertEquals("Strand Propensity", new StrandColourScheme().getSchemeName());
327     assertEquals("Taylor", new TaylorColourScheme().getSchemeName());
328     assertEquals("Turn Propensity", new TurnColourScheme().getSchemeName());
329     assertEquals("Zappo", new ZappoColourScheme().getSchemeName());
330     assertEquals("Nucleotide", new NucleotideColourScheme().getSchemeName());
331     assertEquals("Purine/Pyrimidine",
332             new PurinePyrimidineColourScheme().getSchemeName());
333     assertEquals("RNA Interaction type",
334             new RNAInteractionColourScheme().getSchemeName());
335     assertEquals("User Defined", new UserColourScheme().getSchemeName());
336     assertEquals("Score", new ScoreColourScheme(new int[] {},
337             new double[] {}, 0, 0d).getSchemeName());
338     assertEquals("% Identity", new PIDColourScheme().getSchemeName());
339     assertEquals("Follower", new FollowerColourScheme().getSchemeName());
340     assertEquals("T-Coffee Scores",
341             new TCoffeeColourScheme(peptide).getSchemeName());
342     assertEquals("RNA Helices",
343             new RNAHelicesColour(peptide).getSchemeName());
344   }
345 }