Clover icon

Coverage Report

  1. Project Clover database Fri Nov 28 2025 17:48:58 GMT
  2. Package jalview.analysis

File AAFrequencyTest.java

 

Code metrics

2
218
11
1
457
292
12
0.06
19.82
11
1.09

Classes

Class Line # Actions
AAFrequencyTest 42 218 12
0.935064993.5%
 

Contributing tests

This file is covered by 9 tests. .

Source view

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