Merge branch 'Jalview-BH/JAL-3026-JAL-3063-JAXB' into
[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.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.JvOptionPane;
35 import jalview.io.TCoffeeScoreFile;
36
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39
40 public class ResidueColourSchemeTest
41 {
42   @BeforeClass(alwaysRun = true)
43   public void setUp()
44   {
45
46   }
47
48   @BeforeClass(alwaysRun = true)
49   public void setUpJvOptionPane()
50   {
51     JvOptionPane.setInteractiveMode(false);
52     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53   }
54
55   @Test(groups = "Functional")
56   public void testIsApplicableTo()
57   {
58     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
59     SequenceI pep2 = new Sequence("pep2", "AILFQYG");
60     SequenceI dna1 = new Sequence("dna1", "ACTGAC");
61     SequenceI dna2 = new Sequence("dna2", "TCCAAG");
62     AlignmentI peptide = new Alignment(new SequenceI[] { pep1, pep2 });
63     AlignmentI nucleotide = new Alignment(new SequenceI[] { dna1, dna2 });
64
65     /*
66      * peptide-specific colour schemes
67      */
68     assertTrue(new ClustalxColourScheme(peptide, null)
69             .isApplicableTo(peptide));
70     assertFalse(new ClustalxColourScheme(nucleotide, null)
71             .isApplicableTo(nucleotide));
72     assertTrue(new Blosum62ColourScheme().isApplicableTo(peptide));
73     assertFalse(new Blosum62ColourScheme().isApplicableTo(nucleotide));
74     assertTrue(new BuriedColourScheme().isApplicableTo(peptide));
75     assertFalse(new BuriedColourScheme().isApplicableTo(nucleotide));
76     assertTrue(new HelixColourScheme().isApplicableTo(peptide));
77     assertFalse(new HelixColourScheme().isApplicableTo(nucleotide));
78     assertTrue(new HydrophobicColourScheme().isApplicableTo(peptide));
79     assertFalse(new HydrophobicColourScheme().isApplicableTo(nucleotide));
80     assertTrue(new StrandColourScheme().isApplicableTo(peptide));
81     assertFalse(new StrandColourScheme().isApplicableTo(nucleotide));
82     assertTrue(new TaylorColourScheme().isApplicableTo(peptide));
83     assertFalse(new TaylorColourScheme().isApplicableTo(nucleotide));
84     assertTrue(new TurnColourScheme().isApplicableTo(peptide));
85     assertFalse(new TurnColourScheme().isApplicableTo(nucleotide));
86     assertTrue(new ZappoColourScheme().isApplicableTo(peptide));
87     assertFalse(new ZappoColourScheme().isApplicableTo(nucleotide));
88
89     /*
90      * nucleotide-specific colour schemes
91      */
92     assertFalse(new NucleotideColourScheme().isApplicableTo(peptide));
93     assertTrue(new NucleotideColourScheme().isApplicableTo(nucleotide));
94     assertFalse(new PurinePyrimidineColourScheme().isApplicableTo(peptide));
95     assertTrue(new PurinePyrimidineColourScheme()
96             .isApplicableTo(nucleotide));
97     assertFalse(new RNAInteractionColourScheme().isApplicableTo(peptide));
98     assertTrue(new RNAInteractionColourScheme().isApplicableTo(nucleotide));
99
100     /*
101      * indifferent
102      */
103     assertTrue(new UserColourScheme().isApplicableTo(peptide));
104     assertTrue(new UserColourScheme().isApplicableTo(nucleotide));
105     assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}, 0, 0d)
106             .isApplicableTo(peptide));
107     assertTrue(new ScoreColourScheme(new int[] {}, new double[] {}, 0, 0d)
108             .isApplicableTo(nucleotide));
109     ResidueColourScheme rcs = new PIDColourScheme();
110     assertTrue(rcs.isApplicableTo(peptide));
111     assertTrue(rcs.isApplicableTo(nucleotide));
112     assertTrue(new PIDColourScheme().isApplicableTo(peptide));
113     assertTrue(new PIDColourScheme().isApplicableTo(nucleotide));
114     assertTrue(new FollowerColourScheme().isApplicableTo(peptide));
115     assertTrue(new FollowerColourScheme().isApplicableTo(nucleotide));
116
117     /*
118      * TCoffee colour requires the presence of TCoffee score annotation
119      */
120     assertFalse(new TCoffeeColourScheme(peptide).isApplicableTo(peptide));
121     assertFalse(new TCoffeeColourScheme(nucleotide)
122             .isApplicableTo(nucleotide));
123     AlignmentAnnotation aa = new AlignmentAnnotation("T-COFFEE", "", null);
124     aa.setCalcId(TCoffeeScoreFile.TCOFFEE_SCORE);
125     peptide.addAnnotation(aa);
126     aa = new AlignmentAnnotation("T-COFFEE", "", null);
127     aa.setCalcId(TCoffeeScoreFile.TCOFFEE_SCORE);
128     nucleotide.addAnnotation(aa);
129     assertTrue(new TCoffeeColourScheme(peptide).isApplicableTo(peptide));
130     assertTrue(new TCoffeeColourScheme(nucleotide)
131             .isApplicableTo(nucleotide));
132
133     /*
134      * RNAHelices requires the presence of rna secondary structure
135      */
136     assertFalse(new RNAHelicesColour(peptide).isApplicableTo(peptide));
137     assertFalse(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide));
138     // add secondary structure (small but perfectly formed)
139     Annotation[] ss = new Annotation[2];
140     ss[0] = new Annotation("", "", '{', 0f);
141     ss[1] = new Annotation("", "", '}', 0f);
142     nucleotide.addAnnotation(new AlignmentAnnotation("SS", "", ss));
143     assertTrue(new RNAHelicesColour(nucleotide).isApplicableTo(nucleotide));
144   }
145
146   @Test(groups = "Functional")
147   public void testIsApplicableTo_dynamicColourScheme()
148   {
149     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
150     SequenceI pep2 = new Sequence("pep2", "AILFQYG");
151     AlignmentI peptide = new Alignment(new SequenceI[] { pep1, pep2 });
152   
153     /*
154      * demonstrate that we can 'plug in' a colour scheme with specified
155      * criteria for applicability; here, that there are more than 2 sequences
156      */
157     ColourSchemeI cs = new UserColourScheme()
158     {
159       @Override
160       public boolean isApplicableTo(AnnotatedCollectionI ac)
161       {
162         AlignmentI al = ac.getContext() == null ? (AlignmentI) ac
163                 : (AlignmentI) ac.getContext();
164         return al.getSequences().size() > 2;
165       }
166     };
167     assertFalse(cs.isApplicableTo(peptide));
168     peptide.addSequence(pep1);
169     assertTrue(cs.isApplicableTo(peptide));
170   }
171
172   @Test(groups = "Functional")
173   public void testGetName()
174   {
175     SequenceI pep1 = new Sequence("pep1", "APQTWLS");
176     AlignmentI peptide = new Alignment(new SequenceI[] { pep1 });
177
178     assertEquals("Blosum62", new Blosum62ColourScheme().getSchemeName());
179     assertEquals("Buried Index", new BuriedColourScheme().getSchemeName());
180     assertEquals("Helix Propensity", new HelixColourScheme().getSchemeName());
181     assertEquals("Hydrophobic", new HydrophobicColourScheme().getSchemeName());
182     assertEquals("Strand Propensity", new StrandColourScheme().getSchemeName());
183     assertEquals("Taylor", new TaylorColourScheme().getSchemeName());
184     assertEquals("Turn Propensity", new TurnColourScheme().getSchemeName());
185     assertEquals("Zappo", new ZappoColourScheme().getSchemeName());
186     assertEquals("Nucleotide", new NucleotideColourScheme().getSchemeName());
187     assertEquals("Purine/Pyrimidine",
188             new PurinePyrimidineColourScheme().getSchemeName());
189     assertEquals("RNA Interaction type",
190             new RNAInteractionColourScheme().getSchemeName());
191     assertEquals("User Defined", new UserColourScheme().getSchemeName());
192     assertEquals("Score", new ScoreColourScheme(new int[] {},
193             new double[] {}, 0, 0d).getSchemeName());
194     assertEquals("% Identity", new PIDColourScheme().getSchemeName());
195     assertEquals("Follower", new FollowerColourScheme().getSchemeName());
196     assertEquals("T-Coffee Scores",
197             new TCoffeeColourScheme(peptide).getSchemeName());
198     assertEquals("RNA Helices",
199             new RNAHelicesColour(peptide).getSchemeName());
200   }
201 }