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.HiddenMarkovModel;
29 import jalview.datamodel.Profile;
30 import jalview.datamodel.ProfileI;
31 import jalview.datamodel.Profiles;
32 import jalview.datamodel.ProfilesI;
33 import jalview.datamodel.Sequence;
34 import jalview.datamodel.SequenceI;
35 import jalview.gui.JvOptionPane;
36 import jalview.io.DataSourceType;
37 import jalview.io.FileParse;
38 import jalview.io.HMMFile;
40 import java.io.IOException;
41 import java.net.MalformedURLException;
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.Test;
46 public class AAFrequencyTest
49 HiddenMarkovModel hmm;
51 @BeforeClass(alwaysRun = true)
52 public void setUpJvOptionPane()
54 JvOptionPane.setInteractiveMode(false);
55 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
58 @Test(groups = { "Functional" })
59 public void testCalculate_noProfile()
61 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
62 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
63 SequenceI seq3 = new Sequence("Seq3", "C---G");
64 SequenceI seq4 = new Sequence("Seq4", "CA--t");
65 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
66 int width = seq1.getLength();
67 ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
71 ProfileI col = result.get(0);
72 assertEquals(100f, col.getPercentageIdentity(false));
73 assertEquals(100f, col.getPercentageIdentity(true));
74 assertEquals(4, col.getMaxCount());
75 assertEquals("C", col.getModalResidue());
76 assertNull(col.getCounts());
80 assertEquals(75f, col.getPercentageIdentity(false));
81 assertEquals(100f, col.getPercentageIdentity(true));
82 assertEquals(3, col.getMaxCount());
83 assertEquals("A", col.getModalResidue());
85 // col 2 is 50% G 50% C or 25/25 counting gaps
87 assertEquals(25f, col.getPercentageIdentity(false));
88 assertEquals(50f, col.getPercentageIdentity(true));
89 assertEquals(1, col.getMaxCount());
90 assertEquals("CG", col.getModalResidue());
94 assertEquals(0f, col.getPercentageIdentity(false));
95 assertEquals(0f, col.getPercentageIdentity(true));
96 assertEquals(0, col.getMaxCount());
97 assertEquals("", col.getModalResidue());
99 // col 4 is 75% T 25% G
101 assertEquals(75f, col.getPercentageIdentity(false));
102 assertEquals(75f, col.getPercentageIdentity(true));
103 assertEquals(3, col.getMaxCount());
104 assertEquals("T", col.getModalResidue());
107 @Test(groups = { "Functional" })
108 public void testCalculate_withProfile()
110 SequenceI seq1 = new Sequence("Seq1", "CAGT");
111 SequenceI seq2 = new Sequence("Seq2", "CACT");
112 SequenceI seq3 = new Sequence("Seq3", "C--G");
113 SequenceI seq4 = new Sequence("Seq4", "CA-t");
114 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
115 int width = seq1.getLength();
116 ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
119 ProfileI profile = result.get(0);
120 assertEquals(4, profile.getCounts().getCount('C'));
121 assertEquals(4, profile.getHeight());
122 assertEquals(4, profile.getNonGapped());
124 profile = result.get(1);
125 assertEquals(3, profile.getCounts().getCount('A'));
126 assertEquals(4, profile.getHeight());
127 assertEquals(3, profile.getNonGapped());
129 profile = result.get(2);
130 assertEquals(1, profile.getCounts().getCount('C'));
131 assertEquals(1, profile.getCounts().getCount('G'));
132 assertEquals(4, profile.getHeight());
133 assertEquals(2, profile.getNonGapped());
135 profile = result.get(3);
136 assertEquals(3, profile.getCounts().getCount('T'));
137 assertEquals(1, profile.getCounts().getCount('G'));
138 assertEquals(4, profile.getHeight());
139 assertEquals(4, profile.getNonGapped());
142 @Test(groups = { "Functional" }, enabled = false)
143 public void testCalculate_withProfileTiming()
145 SequenceI seq1 = new Sequence("Seq1", "CAGT");
146 SequenceI seq2 = new Sequence("Seq2", "CACT");
147 SequenceI seq3 = new Sequence("Seq3", "C--G");
148 SequenceI seq4 = new Sequence("Seq4", "CA-t");
149 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
151 // ensure class loaded and initialised
152 int width = seq1.getLength();
153 AAFrequency.calculate(seqs, width, 0, width, true);
156 long start = System.currentTimeMillis();
157 for (int i = 0; i < reps; i++)
159 AAFrequency.calculate(seqs, width, 0, width, true);
161 System.out.println(System.currentTimeMillis() - start);
165 * Test generation of consensus annotation with options 'include gaps'
166 * (profile percentages are of all sequences, whether gapped or not), and
167 * 'show logo' (the full profile with all residue percentages is reported in
168 * the description for the tooltip)
170 @Test(groups = { "Functional" })
171 public void testCompleteConsensus_includeGaps_showLogo()
174 * first compute the profiles
176 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
177 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
178 SequenceI seq3 = new Sequence("Seq3", "C---G");
179 SequenceI seq4 = new Sequence("Seq4", "CA--t");
180 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
181 int width = seq1.getLength();
182 ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
184 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
185 "PID", new Annotation[width]);
187 .completeConsensus(consensus, profiles, 0, 5, false, true, 4);
189 Annotation ann = consensus.annotations[0];
190 assertEquals("C 100%", ann.description);
191 assertEquals("C", ann.displayCharacter);
192 ann = consensus.annotations[1];
193 assertEquals("A 75%", ann.description);
194 assertEquals("A", ann.displayCharacter);
195 ann = consensus.annotations[2];
196 assertEquals("C 25%; G 25%", ann.description);
197 assertEquals("+", ann.displayCharacter);
198 ann = consensus.annotations[3];
199 assertEquals("", ann.description);
200 assertEquals("-", ann.displayCharacter);
201 ann = consensus.annotations[4];
202 assertEquals("T 75%; G 25%", ann.description);
203 assertEquals("T", ann.displayCharacter);
207 * Test generation of consensus annotation with options 'ignore gaps' (profile
208 * percentages are of the non-gapped sequences) and 'no logo' (only the modal
209 * residue[s] percentage is reported in the description for the tooltip)
211 @Test(groups = { "Functional" })
212 public void testCompleteConsensus_ignoreGaps_noLogo()
215 * first compute the profiles
217 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
218 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
219 SequenceI seq3 = new Sequence("Seq3", "C---G");
220 SequenceI seq4 = new Sequence("Seq4", "CA--t");
221 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
222 int width = seq1.getLength();
223 ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
225 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
226 "PID", new Annotation[width]);
228 .completeConsensus(consensus, profiles, 0, 5, true, false, 4);
230 Annotation ann = consensus.annotations[0];
231 assertEquals("C 100%", ann.description);
232 assertEquals("C", ann.displayCharacter);
233 ann = consensus.annotations[1];
234 assertEquals("A 100%", ann.description);
235 assertEquals("A", ann.displayCharacter);
236 ann = consensus.annotations[2];
237 assertEquals("[CG] 50%", ann.description);
238 assertEquals("+", ann.displayCharacter);
239 ann = consensus.annotations[3];
240 assertEquals("", ann.description);
241 assertEquals("-", ann.displayCharacter);
242 ann = consensus.annotations[4];
243 assertEquals("T 75%", ann.description);
244 assertEquals("T", ann.displayCharacter);
248 @Test(groups = { "Functional" }, priority = 1)
249 public void testExtractHMMProfile()
250 throws MalformedURLException, IOException
253 HMMFile hmmFile = new HMMFile(new FileParse(
254 "test/jalview/io/test_MADE1_hmm.txt", DataSourceType.FILE));
255 hmm = hmmFile.getHMM();
256 int[] expected = { 0, 4, 100, 'T', 71, 'C', 12, 'G', 9, 'A', 9 };
257 int[] actual = AAFrequency.extractHMMProfile(hmm, 17, false, false);
258 for (int i = 0; i < actual.length; i++)
262 assertEquals(actual[i], expected[i]);
266 assertEquals(actual[i], expected[i]);
270 int[] expected2 = { 0, 4, 100, 'A', 85, 'C', 0, 'G', 0, 'T', 0 };
271 int[] actual2 = AAFrequency.extractHMMProfile(hmm, 2, true, false);
272 for (int i = 0; i < actual2.length; i++)
276 assertEquals(actual[i], expected[i]);
280 assertEquals(actual[i], expected[i]);
284 assertNull(AAFrequency.extractHMMProfile(null, 98978867, true, false));
287 @Test(groups = { "Functional" }, priority = 2)
288 public void testGetAnalogueCount()
291 count = AAFrequency.getAnalogueCount(hmm, 0, 'T', false, false);
292 assertEquals(7859, count);
293 count = AAFrequency.getAnalogueCount(hmm, 20, 'G', false, false);
294 assertEquals(7546, count);
295 count = AAFrequency.getAnalogueCount(hmm, 1077, 'G', true, false);
296 assertEquals(0, count);
299 @Test(groups = { "Functional" }, priority = 3)
300 public void testCompleteInformation()
302 ProfileI prof1 = new Profile(1, 0, 100, "A");
303 ProfileI prof2 = new Profile(1, 0, 100, "-");
305 ProfilesI profs = new Profiles(new ProfileI[] { prof1, prof2 });
306 Annotation ann1 = new Annotation(6.5f);
307 Annotation ann2 = new Annotation(0f);
308 Annotation[] annots = new Annotation[] { ann1, ann2 };
309 SequenceI seq = new Sequence("", "AA", 0, 0);
311 AlignmentAnnotation annot = new AlignmentAnnotation("", "", annots);
312 annot.setSequenceRef(seq);
313 AAFrequency.completeInformation(annot, profs, 0, 1, 1, 1f);
314 float ic = annot.annotations[0].value;
315 assertEquals(0.91532f, ic, 0.0001f);
316 ic = annot.annotations[1].value;
317 assertEquals(0f, ic, 0.0001f);