93c95ce5bb7b1c32cc79d74e598ae2254ba86483
[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.Profile;
29 import jalview.datamodel.ProfileI;
30 import jalview.datamodel.ProfilesI;
31 import jalview.datamodel.ResidueCount;
32 import jalview.datamodel.Sequence;
33 import jalview.datamodel.SequenceI;
34 import jalview.gui.JvOptionPane;
35
36 import java.util.Hashtable;
37
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 public class AAFrequencyTest
42 {
43
44   @BeforeClass(alwaysRun = true)
45   public void setUpJvOptionPane()
46   {
47     JvOptionPane.setInteractiveMode(false);
48     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49   }
50
51   @Test(groups = { "Functional" })
52   public void testCalculate_noProfile()
53   {
54     SequenceI seq1 = new Sequence("Seq1", "CAG-T");
55     SequenceI seq2 = new Sequence("Seq2", "CAC-T");
56     SequenceI seq3 = new Sequence("Seq3", "C---G");
57     SequenceI seq4 = new Sequence("Seq4", "CA--t");
58     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
59     int width = seq1.getLength();
60     ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
61             false);
62
63     // col 0 is 100% C
64     ProfileI col = result.get(0);
65     assertEquals(100f, col.getPercentageIdentity(false));
66     assertEquals(100f, col.getPercentageIdentity(true));
67     assertEquals(4, col.getMaxCount());
68     assertEquals("C", col.getModalResidue());
69     assertNull(col.getCounts());
70
71     // col 1 is 75% A
72     col = result.get(1);
73     assertEquals(75f, col.getPercentageIdentity(false));
74     assertEquals(100f, col.getPercentageIdentity(true));
75     assertEquals(3, col.getMaxCount());
76     assertEquals("A", col.getModalResidue());
77
78     // col 2 is 50% G 50% C or 25/25 counting gaps
79     col = result.get(2);
80     assertEquals(25f, col.getPercentageIdentity(false));
81     assertEquals(50f, col.getPercentageIdentity(true));
82     assertEquals(1, col.getMaxCount());
83     assertEquals("CG", col.getModalResidue());
84
85     // col 3 is all gaps
86     col = result.get(3);
87     assertEquals(0f, col.getPercentageIdentity(false));
88     assertEquals(0f, col.getPercentageIdentity(true));
89     assertEquals(0, col.getMaxCount());
90     assertEquals("", col.getModalResidue());
91
92     // col 4 is 75% T 25% G
93     col = result.get(4);
94     assertEquals(75f, col.getPercentageIdentity(false));
95     assertEquals(75f, col.getPercentageIdentity(true));
96     assertEquals(3, col.getMaxCount());
97     assertEquals("T", col.getModalResidue());
98   }
99
100   @Test(groups = { "Functional" })
101   public void testCalculate_withProfile()
102   {
103     SequenceI seq1 = new Sequence("Seq1", "CAGT");
104     SequenceI seq2 = new Sequence("Seq2", "CACT");
105     SequenceI seq3 = new Sequence("Seq3", "C--G");
106     SequenceI seq4 = new Sequence("Seq4", "CA-t");
107     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
108     int width = seq1.getLength();
109     ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
110             true);
111
112     ProfileI profile = result.get(0);
113     assertEquals(4, profile.getCounts().getCount('C'));
114     assertEquals(4, profile.getHeight());
115     assertEquals(4, profile.getNonGapped());
116
117     profile = result.get(1);
118     assertEquals(3, profile.getCounts().getCount('A'));
119     assertEquals(4, profile.getHeight());
120     assertEquals(3, profile.getNonGapped());
121
122     profile = result.get(2);
123     assertEquals(1, profile.getCounts().getCount('C'));
124     assertEquals(1, profile.getCounts().getCount('G'));
125     assertEquals(4, profile.getHeight());
126     assertEquals(2, profile.getNonGapped());
127
128     profile = result.get(3);
129     assertEquals(3, profile.getCounts().getCount('T'));
130     assertEquals(1, profile.getCounts().getCount('G'));
131     assertEquals(4, profile.getHeight());
132     assertEquals(4, profile.getNonGapped());
133   }
134
135   @Test(groups = { "Functional" }, enabled = false)
136   public void testCalculate_withProfileTiming()
137   {
138     SequenceI seq1 = new Sequence("Seq1", "CAGT");
139     SequenceI seq2 = new Sequence("Seq2", "CACT");
140     SequenceI seq3 = new Sequence("Seq3", "C--G");
141     SequenceI seq4 = new Sequence("Seq4", "CA-t");
142     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
143
144     // ensure class loaded and initialised
145     int width = seq1.getLength();
146     AAFrequency.calculate(seqs, width, 0, width, true);
147
148     int reps = 100000;
149     long start = System.currentTimeMillis();
150     for (int i = 0; i < reps; i++)
151     {
152       AAFrequency.calculate(seqs, width, 0, width, true);
153     }
154     System.out.println(System.currentTimeMillis() - start);
155   }
156
157   /**
158    * Test generation of consensus annotation with options 'include gaps'
159    * (profile percentages are of all sequences, whether gapped or not), and
160    * 'show logo' (the full profile with all residue percentages is reported in
161    * the description for the tooltip)
162    */
163   @Test(groups = { "Functional" })
164   public void testCompleteConsensus_includeGaps_showLogo()
165   {
166     /*
167      * first compute the profiles
168      */
169     SequenceI seq1 = new Sequence("Seq1", "CAG-T");
170     SequenceI seq2 = new Sequence("Seq2", "CAC-T");
171     SequenceI seq3 = new Sequence("Seq3", "C---G");
172     SequenceI seq4 = new Sequence("Seq4", "CA--t");
173     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
174     int width = seq1.getLength();
175     ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
176
177     AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
178             "PID", new Annotation[width]);
179     AAFrequency
180             .completeConsensus(consensus, profiles, 0, 5, false, true, 4);
181
182     Annotation ann = consensus.annotations[0];
183     assertEquals("C 100%", ann.description);
184     assertEquals("C", ann.displayCharacter);
185     ann = consensus.annotations[1];
186     assertEquals("A 75%", ann.description);
187     assertEquals("A", ann.displayCharacter);
188     ann = consensus.annotations[2];
189     assertEquals("C 25%; G 25%", ann.description);
190     assertEquals("+", ann.displayCharacter);
191     ann = consensus.annotations[3];
192     assertEquals("", ann.description);
193     assertEquals("-", ann.displayCharacter);
194     ann = consensus.annotations[4];
195     assertEquals("T 75%; G 25%", ann.description);
196     assertEquals("T", ann.displayCharacter);
197   }
198
199   /**
200    * Test generation of consensus annotation with options 'ignore gaps' (profile
201    * percentages are of the non-gapped sequences) and 'no logo' (only the modal
202    * residue[s] percentage is reported in the description for the tooltip)
203    */
204   @Test(groups = { "Functional" })
205   public void testCompleteConsensus_ignoreGaps_noLogo()
206   {
207     /*
208      * first compute the profiles
209      */
210     SequenceI seq1 = new Sequence("Seq1", "CAG-T");
211     SequenceI seq2 = new Sequence("Seq2", "CAC-T");
212     SequenceI seq3 = new Sequence("Seq3", "C---G");
213     SequenceI seq4 = new Sequence("Seq4", "CA--t");
214     SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
215     int width = seq1.getLength();
216     ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
217   
218     AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
219             "PID", new Annotation[width]);
220     AAFrequency
221             .completeConsensus(consensus, profiles, 0, 5, true, false, 4);
222   
223     Annotation ann = consensus.annotations[0];
224     assertEquals("C 100%", ann.description);
225     assertEquals("C", ann.displayCharacter);
226     ann = consensus.annotations[1];
227     assertEquals("A 100%", ann.description);
228     assertEquals("A", ann.displayCharacter);
229     ann = consensus.annotations[2];
230     assertEquals("[CG] 50%", ann.description);
231     assertEquals("+", ann.displayCharacter);
232     ann = consensus.annotations[3];
233     assertEquals("", ann.description);
234     assertEquals("-", ann.displayCharacter);
235     ann = consensus.annotations[4];
236     assertEquals("T 75%", ann.description);
237     assertEquals("T", ann.displayCharacter);
238   }
239
240   /**
241    * Test to include rounding down of a non-zero count to 0% (JAL-3202)
242    */
243   @Test(groups = { "Functional" })
244   public void testExtractProfile()
245   {
246     /*
247      * 200 sequences of which 30 gapped (170 ungapped)
248      * max count 70 for modal residue 'G'
249      */
250     ProfileI profile = new Profile(200, 30, 70, "G");
251     ResidueCount counts = new ResidueCount();
252     counts.put('G', 70);
253     counts.put('R', 60);
254     counts.put('L', 38);
255     counts.put('H', 2);
256     profile.setCounts(counts);
257
258     /*
259      * [0, noOfValues, totalPercent, char1, count1, ...]
260      * G: 70/170 = 41.2 = 41
261      * R: 60/170 = 35.3 = 35
262      * L: 38/170 = 22.3 = 22
263      * H: 2/170 = 1
264      * total (rounded) percentages = 99 
265      */
266     int[] extracted = AAFrequency.extractProfile(profile, true);
267     int[] expected = new int[] { 0, 4, 99, 'G', 41, 'R', 35, 'L', 22, 'H',
268         1 };
269     org.testng.Assert.assertEquals(extracted, expected);
270
271     /*
272      * add some counts of 1; these round down to 0% and should be discarded
273      */
274     counts.put('G', 68); // 68/170 = 40% exactly (percentages now total 98)
275     counts.put('Q', 1);
276     counts.put('K', 1);
277     extracted = AAFrequency.extractProfile(profile, true);
278     expected = new int[] { 0, 4, 98, 'G', 40, 'R', 35, 'L', 22, 'H', 1 };
279     org.testng.Assert.assertEquals(extracted, expected);
280
281   }
282
283   /**
284    * Tests for the profile calculation where gaps are included i.e. the
285    * denominator is the total number of sequences in the column
286    */
287   @Test(groups = { "Functional" })
288   public void testExtractProfile_countGaps()
289   {
290     /*
291      * 200 sequences of which 30 gapped (170 ungapped)
292      * max count 70 for modal residue 'G'
293      */
294     ProfileI profile = new Profile(200, 30, 70, "G");
295     ResidueCount counts = new ResidueCount();
296     counts.put('G', 70);
297     counts.put('R', 60);
298     counts.put('L', 38);
299     counts.put('H', 2);
300     profile.setCounts(counts);
301   
302     /*
303      * [0, noOfValues, totalPercent, char1, count1, ...]
304      * G: 70/200 = 35%
305      * R: 60/200 = 30%
306      * L: 38/200 = 19%
307      * H: 2/200 = 1%
308      * total (rounded) percentages = 85 
309      */
310     int[] extracted = AAFrequency.extractProfile(profile, false);
311     int[] expected = new int[] { AlignmentAnnotation.SEQUENCE_PROFILE, 4,
312         85, 'G', 35, 'R', 30, 'L', 19, 'H',
313         1 };
314     org.testng.Assert.assertEquals(extracted, expected);
315   
316     /*
317      * add some counts of 1; these round down to 0% and should be discarded
318      */
319     counts.put('G', 68); // 68/200 = 34%
320     counts.put('Q', 1);
321     counts.put('K', 1);
322     extracted = AAFrequency.extractProfile(profile, false);
323     expected = new int[] { AlignmentAnnotation.SEQUENCE_PROFILE, 4, 84, 'G',
324         34, 'R', 30, 'L', 19, 'H', 1 };
325     org.testng.Assert.assertEquals(extracted, expected);
326   
327   }
328
329   @Test(groups = { "Functional" })
330   public void testExtractCdnaProfile()
331   {
332     /*
333      * 200 sequences of which 30 gapped (170 ungapped)
334      * max count 70 for modal residue 'G'
335      */
336     Hashtable profile = new Hashtable();
337
338     /*
339      *  cdna profile is {seqCount, ungappedCount, codonCount1, ...codonCount64}
340      * where 1..64 positions correspond to encoded codons
341      * see CodingUtils.encodeCodon()
342      */
343     int[] codonCounts = new int[66];
344     char[] codon1 = new char[] { 'G', 'C', 'A' };
345     char[] codon2 = new char[] { 'c', 'C', 'A' };
346     char[] codon3 = new char[] { 't', 'g', 'A' };
347     char[] codon4 = new char[] { 'G', 'C', 't' };
348     int encoded1 = CodingUtils.encodeCodon(codon1);
349     int encoded2 = CodingUtils.encodeCodon(codon2);
350     int encoded3 = CodingUtils.encodeCodon(codon3);
351     int encoded4 = CodingUtils.encodeCodon(codon4);
352     codonCounts[2 + encoded1] = 30;
353     codonCounts[2 + encoded2] = 70;
354     codonCounts[2 + encoded3] = 9;
355     codonCounts[2 + encoded4] = 1;
356     codonCounts[0] = 120;
357     codonCounts[1] = 110;
358     profile.put(AAFrequency.PROFILE, codonCounts);
359   
360     /*
361      * [0, noOfValues, totalPercent, char1, count1, ...]
362      * codon1: 30/110 = 27.2 = 27% 
363      * codon2: 70/110 = 63.6% = 63%
364      * codon3: 9/110 = 8.1% = 8%
365      * codon4: 1/110 = 0.9% = 0% should be discarded
366      * total (rounded) percentages = 98
367      */
368     int[] extracted = AAFrequency.extractCdnaProfile(profile, true);
369     int[] expected = new int[] { AlignmentAnnotation.CDNA_PROFILE, 3, 98,
370         encoded2, 63, encoded1, 27, encoded3, 8 };
371     org.testng.Assert.assertEquals(extracted, expected);
372   }
373
374   @Test(groups = { "Functional" })
375   public void testExtractCdnaProfile_countGaps()
376   {
377     /*
378      * 200 sequences of which 30 gapped (170 ungapped)
379      * max count 70 for modal residue 'G'
380      */
381     Hashtable profile = new Hashtable();
382   
383     /*
384      *  cdna profile is {seqCount, ungappedCount, codonCount1, ...codonCount64}
385      * where 1..64 positions correspond to encoded codons
386      * see CodingUtils.encodeCodon()
387      */
388     int[] codonCounts = new int[66];
389     char[] codon1 = new char[] { 'G', 'C', 'A' };
390     char[] codon2 = new char[] { 'c', 'C', 'A' };
391     char[] codon3 = new char[] { 't', 'g', 'A' };
392     char[] codon4 = new char[] { 'G', 'C', 't' };
393     int encoded1 = CodingUtils.encodeCodon(codon1);
394     int encoded2 = CodingUtils.encodeCodon(codon2);
395     int encoded3 = CodingUtils.encodeCodon(codon3);
396     int encoded4 = CodingUtils.encodeCodon(codon4);
397     codonCounts[2 + encoded1] = 30;
398     codonCounts[2 + encoded2] = 70;
399     codonCounts[2 + encoded3] = 9;
400     codonCounts[2 + encoded4] = 1;
401     codonCounts[0] = 120;
402     codonCounts[1] = 110;
403     profile.put(AAFrequency.PROFILE, codonCounts);
404   
405     /*
406      * [0, noOfValues, totalPercent, char1, count1, ...]
407      * codon1: 30/120 = 25% 
408      * codon2: 70/120 = 58.3 = 58%
409      * codon3: 9/120 = 7.5 = 7%
410      * codon4: 1/120 = 0.8 = 0% should be discarded
411      * total (rounded) percentages = 90
412      */
413     int[] extracted = AAFrequency.extractCdnaProfile(profile, false);
414     int[] expected = new int[] { AlignmentAnnotation.CDNA_PROFILE, 3, 90,
415         encoded2, 58, encoded1, 25, encoded3, 7 };
416     org.testng.Assert.assertEquals(extracted, expected);
417   }
418 }