2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.analysis;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
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;
33 import org.testng.annotations.Test;
35 public class AAFrequencyTest
37 @Test(groups = { "Functional" })
38 public void testCalculate_noProfile()
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,
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());
59 assertEquals(75f, col.getPercentageIdentity(false));
60 assertEquals(100f, col.getPercentageIdentity(true));
61 assertEquals(3, col.getMaxCount());
62 assertEquals("A", col.getModalResidue());
64 // col 2 is 50% G 50% C or 25/25 counting gaps
66 assertEquals(25f, col.getPercentageIdentity(false));
67 assertEquals(50f, col.getPercentageIdentity(true));
68 assertEquals(1, col.getMaxCount());
69 assertEquals("CG", col.getModalResidue());
73 assertEquals(0f, col.getPercentageIdentity(false));
74 assertEquals(0f, col.getPercentageIdentity(true));
75 assertEquals(0, col.getMaxCount());
76 assertEquals("", col.getModalResidue());
78 // col 4 is 75% T 25% G
80 assertEquals(75f, col.getPercentageIdentity(false));
81 assertEquals(75f, col.getPercentageIdentity(true));
82 assertEquals(3, col.getMaxCount());
83 assertEquals("T", col.getModalResidue());
86 @Test(groups = { "Functional" })
87 public void testCalculate_withProfile()
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,
98 ProfileI profile = result.get(0);
99 assertEquals(4, profile.getCounts().getCount('C'));
100 assertEquals(4, profile.getHeight());
101 assertEquals(4, profile.getNonGapped());
103 profile = result.get(1);
104 assertEquals(3, profile.getCounts().getCount('A'));
105 assertEquals(4, profile.getHeight());
106 assertEquals(3, profile.getNonGapped());
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());
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());
121 @Test(groups = { "Functional" }, enabled = false)
122 public void testCalculate_withProfileTiming()
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 };
130 // ensure class loaded and initialised
131 int width = seq1.getLength();
132 AAFrequency.calculate(seqs, width, 0, width, true);
135 long start = System.currentTimeMillis();
136 for (int i = 0; i < reps; i++)
138 AAFrequency.calculate(seqs, width, 0, width, true);
140 System.out.println(System.currentTimeMillis() - start);
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)
149 @Test(groups = { "Functional" })
150 public void testCompleteConsensus_includeGaps_showLogo()
153 * first compute the profiles
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);
163 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
164 "PID", new Annotation[width]);
166 .completeConsensus(consensus, profiles, 0, 5, false, true, 4);
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);
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)
190 @Test(groups = { "Functional" })
191 public void testCompleteConsensus_ignoreGaps_noLogo()
194 * first compute the profiles
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);
204 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
205 "PID", new Annotation[width]);
207 .completeConsensus(consensus, profiles, 0, 5, true, false, 4);
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);