Merge branch 'features/JAL-2320_closeChimeraAction' into releases/Release_2_10_Branch
[jalview.git] / test / jalview / analysis / AAFrequencyTest.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.analysis;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.ProfileI;
29 import jalview.datamodel.ProfilesI;
30 import jalview.datamodel.Sequence;
31 import jalview.datamodel.SequenceI;
32
33 import org.testng.annotations.Test;
34
35 public class AAFrequencyTest
36 {
37   @Test(groups = { "Functional" })
38   public void testCalculate_noProfile()
39   {
40     SequenceI seq1 = new Sequence("Seq1", "CAG-T");
41     SequenceI seq2 = new Sequence("Seq2", "CAC-T");
42     SequenceI seq3 = new Sequence("Seq3", "C---G");
43     SequenceI seq4 = new Sequence("Seq4", "CA--t");
44     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
45     int width = seq1.getLength();
46     ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
47             false);
48
49     // col 0 is 100% C
50     ProfileI col = result.get(0);
51     assertEquals(100f, col.getPercentageIdentity(false));
52     assertEquals(100f, col.getPercentageIdentity(true));
53     assertEquals(4, col.getMaxCount());
54     assertEquals("C", col.getModalResidue());
55     assertNull(col.getCounts());
56
57     // col 1 is 75% A
58     col = result.get(1);
59     assertEquals(75f, col.getPercentageIdentity(false));
60     assertEquals(100f, col.getPercentageIdentity(true));
61     assertEquals(3, col.getMaxCount());
62     assertEquals("A", col.getModalResidue());
63
64     // col 2 is 50% G 50% C or 25/25 counting gaps
65     col = result.get(2);
66     assertEquals(25f, col.getPercentageIdentity(false));
67     assertEquals(50f, col.getPercentageIdentity(true));
68     assertEquals(1, col.getMaxCount());
69     assertEquals("CG", col.getModalResidue());
70
71     // col 3 is all gaps
72     col = result.get(3);
73     assertEquals(0f, col.getPercentageIdentity(false));
74     assertEquals(0f, col.getPercentageIdentity(true));
75     assertEquals(0, col.getMaxCount());
76     assertEquals("", col.getModalResidue());
77
78     // col 4 is 75% T 25% G
79     col = result.get(4);
80     assertEquals(75f, col.getPercentageIdentity(false));
81     assertEquals(75f, col.getPercentageIdentity(true));
82     assertEquals(3, col.getMaxCount());
83     assertEquals("T", col.getModalResidue());
84   }
85
86   @Test(groups = { "Functional" })
87   public void testCalculate_withProfile()
88   {
89     SequenceI seq1 = new Sequence("Seq1", "CAGT");
90     SequenceI seq2 = new Sequence("Seq2", "CACT");
91     SequenceI seq3 = new Sequence("Seq3", "C--G");
92     SequenceI seq4 = new Sequence("Seq4", "CA-t");
93     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
94     int width = seq1.getLength();
95     ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
96             true);
97
98     ProfileI profile = result.get(0);
99     assertEquals(4, profile.getCounts().getCount('C'));
100     assertEquals(4, profile.getHeight());
101     assertEquals(4, profile.getNonGapped());
102
103     profile = result.get(1);
104     assertEquals(3, profile.getCounts().getCount('A'));
105     assertEquals(4, profile.getHeight());
106     assertEquals(3, profile.getNonGapped());
107
108     profile = result.get(2);
109     assertEquals(1, profile.getCounts().getCount('C'));
110     assertEquals(1, profile.getCounts().getCount('G'));
111     assertEquals(4, profile.getHeight());
112     assertEquals(2, profile.getNonGapped());
113
114     profile = result.get(3);
115     assertEquals(3, profile.getCounts().getCount('T'));
116     assertEquals(1, profile.getCounts().getCount('G'));
117     assertEquals(4, profile.getHeight());
118     assertEquals(4, profile.getNonGapped());
119   }
120
121   @Test(groups = { "Functional" }, enabled = false)
122   public void testCalculate_withProfileTiming()
123   {
124     SequenceI seq1 = new Sequence("Seq1", "CAGT");
125     SequenceI seq2 = new Sequence("Seq2", "CACT");
126     SequenceI seq3 = new Sequence("Seq3", "C--G");
127     SequenceI seq4 = new Sequence("Seq4", "CA-t");
128     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
129
130     // ensure class loaded and initialised
131     int width = seq1.getLength();
132     AAFrequency.calculate(seqs, width, 0, width, true);
133
134     int reps = 100000;
135     long start = System.currentTimeMillis();
136     for (int i = 0; i < reps; i++)
137     {
138       AAFrequency.calculate(seqs, width, 0, width, true);
139     }
140     System.out.println(System.currentTimeMillis() - start);
141   }
142
143   /**
144    * Test generation of consensus annotation with options 'include gaps'
145    * (profile percentages are of all sequences, whether gapped or not), and
146    * 'show logo' (the full profile with all residue percentages is reported in
147    * the description for the tooltip)
148    */
149   @Test(groups = { "Functional" })
150   public void testCompleteConsensus_includeGaps_showLogo()
151   {
152     /*
153      * first compute the profiles
154      */
155     SequenceI seq1 = new Sequence("Seq1", "CAG-T");
156     SequenceI seq2 = new Sequence("Seq2", "CAC-T");
157     SequenceI seq3 = new Sequence("Seq3", "C---G");
158     SequenceI seq4 = new Sequence("Seq4", "CA--t");
159     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
160     int width = seq1.getLength();
161     ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
162
163     AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
164             "PID", new Annotation[width]);
165     AAFrequency
166             .completeConsensus(consensus, profiles, 0, 5, false, true, 4);
167
168     Annotation ann = consensus.annotations[0];
169     assertEquals("C 100%", ann.description);
170     assertEquals("C", ann.displayCharacter);
171     ann = consensus.annotations[1];
172     assertEquals("A 75%", ann.description);
173     assertEquals("A", ann.displayCharacter);
174     ann = consensus.annotations[2];
175     assertEquals("C 25%; G 25%", ann.description);
176     assertEquals("+", ann.displayCharacter);
177     ann = consensus.annotations[3];
178     assertEquals("", ann.description);
179     assertEquals("-", ann.displayCharacter);
180     ann = consensus.annotations[4];
181     assertEquals("T 75%; G 25%", ann.description);
182     assertEquals("T", ann.displayCharacter);
183   }
184
185   /**
186    * Test generation of consensus annotation with options 'ignore gaps' (profile
187    * percentages are of the non-gapped sequences) and 'no logo' (only the modal
188    * residue[s] percentage is reported in the description for the tooltip)
189    */
190   @Test(groups = { "Functional" })
191   public void testCompleteConsensus_ignoreGaps_noLogo()
192   {
193     /*
194      * first compute the profiles
195      */
196     SequenceI seq1 = new Sequence("Seq1", "CAG-T");
197     SequenceI seq2 = new Sequence("Seq2", "CAC-T");
198     SequenceI seq3 = new Sequence("Seq3", "C---G");
199     SequenceI seq4 = new Sequence("Seq4", "CA--t");
200     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
201     int width = seq1.getLength();
202     ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
203   
204     AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
205             "PID", new Annotation[width]);
206     AAFrequency
207             .completeConsensus(consensus, profiles, 0, 5, true, false, 4);
208   
209     Annotation ann = consensus.annotations[0];
210     assertEquals("C 100%", ann.description);
211     assertEquals("C", ann.displayCharacter);
212     ann = consensus.annotations[1];
213     assertEquals("A 100%", ann.description);
214     assertEquals("A", ann.displayCharacter);
215     ann = consensus.annotations[2];
216     assertEquals("[CG] 50%", ann.description);
217     assertEquals("+", ann.displayCharacter);
218     ann = consensus.annotations[3];
219     assertEquals("", ann.description);
220     assertEquals("-", ann.displayCharacter);
221     ann = consensus.annotations[4];
222     assertEquals("T 75%", ann.description);
223     assertEquals("T", ann.displayCharacter);
224   }
225 }