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.ProfileI;
30 import jalview.datamodel.ProfilesI;
31 import jalview.datamodel.Sequence;
32 import jalview.datamodel.SequenceI;
33 import jalview.gui.JvOptionPane;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileParse;
36 import jalview.io.HMMFile;
38 import java.io.IOException;
39 import java.net.MalformedURLException;
41 import org.testng.annotations.BeforeClass;
42 import org.testng.annotations.Test;
44 public class AAFrequencyTest
47 @BeforeClass(alwaysRun = true)
48 public void setUpJvOptionPane()
50 JvOptionPane.setInteractiveMode(false);
51 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54 @Test(groups = { "Functional" })
55 public void testCalculate_noProfile()
57 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
58 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
59 SequenceI seq3 = new Sequence("Seq3", "C---G");
60 SequenceI seq4 = new Sequence("Seq4", "CA--t");
61 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
62 int width = seq1.getLength();
63 ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
67 ProfileI col = result.get(0);
68 assertEquals(100f, col.getPercentageIdentity(false));
69 assertEquals(100f, col.getPercentageIdentity(true));
70 assertEquals(4, col.getMaxCount());
71 assertEquals("C", col.getModalResidue());
72 assertNull(col.getCounts());
76 assertEquals(75f, col.getPercentageIdentity(false));
77 assertEquals(100f, col.getPercentageIdentity(true));
78 assertEquals(3, col.getMaxCount());
79 assertEquals("A", col.getModalResidue());
81 // col 2 is 50% G 50% C or 25/25 counting gaps
83 assertEquals(25f, col.getPercentageIdentity(false));
84 assertEquals(50f, col.getPercentageIdentity(true));
85 assertEquals(1, col.getMaxCount());
86 assertEquals("CG", col.getModalResidue());
90 assertEquals(0f, col.getPercentageIdentity(false));
91 assertEquals(0f, col.getPercentageIdentity(true));
92 assertEquals(0, col.getMaxCount());
93 assertEquals("", col.getModalResidue());
95 // col 4 is 75% T 25% G
97 assertEquals(75f, col.getPercentageIdentity(false));
98 assertEquals(75f, col.getPercentageIdentity(true));
99 assertEquals(3, col.getMaxCount());
100 assertEquals("T", col.getModalResidue());
103 @Test(groups = { "Functional" })
104 public void testCalculate_withProfile()
106 SequenceI seq1 = new Sequence("Seq1", "CAGT");
107 SequenceI seq2 = new Sequence("Seq2", "CACT");
108 SequenceI seq3 = new Sequence("Seq3", "C--G");
109 SequenceI seq4 = new Sequence("Seq4", "CA-t");
110 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
111 int width = seq1.getLength();
112 ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
115 ProfileI profile = result.get(0);
116 assertEquals(4, profile.getCounts().getCount('C'));
117 assertEquals(4, profile.getHeight());
118 assertEquals(4, profile.getNonGapped());
120 profile = result.get(1);
121 assertEquals(3, profile.getCounts().getCount('A'));
122 assertEquals(4, profile.getHeight());
123 assertEquals(3, profile.getNonGapped());
125 profile = result.get(2);
126 assertEquals(1, profile.getCounts().getCount('C'));
127 assertEquals(1, profile.getCounts().getCount('G'));
128 assertEquals(4, profile.getHeight());
129 assertEquals(2, profile.getNonGapped());
131 profile = result.get(3);
132 assertEquals(3, profile.getCounts().getCount('T'));
133 assertEquals(1, profile.getCounts().getCount('G'));
134 assertEquals(4, profile.getHeight());
135 assertEquals(4, profile.getNonGapped());
138 @Test(groups = { "Functional" }, enabled = false)
139 public void testCalculate_withProfileTiming()
141 SequenceI seq1 = new Sequence("Seq1", "CAGT");
142 SequenceI seq2 = new Sequence("Seq2", "CACT");
143 SequenceI seq3 = new Sequence("Seq3", "C--G");
144 SequenceI seq4 = new Sequence("Seq4", "CA-t");
145 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
147 // ensure class loaded and initialised
148 int width = seq1.getLength();
149 AAFrequency.calculate(seqs, width, 0, width, true);
152 long start = System.currentTimeMillis();
153 for (int i = 0; i < reps; i++)
155 AAFrequency.calculate(seqs, width, 0, width, true);
157 System.out.println(System.currentTimeMillis() - start);
161 * Test generation of consensus annotation with options 'include gaps'
162 * (profile percentages are of all sequences, whether gapped or not), and
163 * 'show logo' (the full profile with all residue percentages is reported in
164 * the description for the tooltip)
166 @Test(groups = { "Functional" })
167 public void testCompleteConsensus_includeGaps_showLogo()
170 * first compute the profiles
172 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
173 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
174 SequenceI seq3 = new Sequence("Seq3", "C---G");
175 SequenceI seq4 = new Sequence("Seq4", "CA--t");
176 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
177 int width = seq1.getLength();
178 ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
180 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
181 "PID", new Annotation[width]);
183 .completeConsensus(consensus, profiles, 0, 5, false, true, 4);
185 Annotation ann = consensus.annotations[0];
186 assertEquals("C 100%", ann.description);
187 assertEquals("C", ann.displayCharacter);
188 ann = consensus.annotations[1];
189 assertEquals("A 75%", ann.description);
190 assertEquals("A", ann.displayCharacter);
191 ann = consensus.annotations[2];
192 assertEquals("C 25%; G 25%", ann.description);
193 assertEquals("+", ann.displayCharacter);
194 ann = consensus.annotations[3];
195 assertEquals("", ann.description);
196 assertEquals("-", ann.displayCharacter);
197 ann = consensus.annotations[4];
198 assertEquals("T 75%; G 25%", ann.description);
199 assertEquals("T", ann.displayCharacter);
203 * Test generation of consensus annotation with options 'ignore gaps' (profile
204 * percentages are of the non-gapped sequences) and 'no logo' (only the modal
205 * residue[s] percentage is reported in the description for the tooltip)
207 @Test(groups = { "Functional" })
208 public void testCompleteConsensus_ignoreGaps_noLogo()
211 * first compute the profiles
213 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
214 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
215 SequenceI seq3 = new Sequence("Seq3", "C---G");
216 SequenceI seq4 = new Sequence("Seq4", "CA--t");
217 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
218 int width = seq1.getLength();
219 ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
221 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
222 "PID", new Annotation[width]);
224 .completeConsensus(consensus, profiles, 0, 5, true, false, 4);
226 Annotation ann = consensus.annotations[0];
227 assertEquals("C 100%", ann.description);
228 assertEquals("C", ann.displayCharacter);
229 ann = consensus.annotations[1];
230 assertEquals("A 100%", ann.description);
231 assertEquals("A", ann.displayCharacter);
232 ann = consensus.annotations[2];
233 assertEquals("[CG] 50%", ann.description);
234 assertEquals("+", ann.displayCharacter);
235 ann = consensus.annotations[3];
236 assertEquals("", ann.description);
237 assertEquals("-", ann.displayCharacter);
238 ann = consensus.annotations[4];
239 assertEquals("T 75%", ann.description);
240 assertEquals("T", ann.displayCharacter);
243 @Test(groups = { "Functional" })
244 public void testGetHMMProfileFor()
245 throws MalformedURLException, IOException
248 HMMFile hmmFile = new HMMFile(new FileParse(
249 "test/jalview/io/test_MADE1_hmm.txt", DataSourceType.FILE));
251 HiddenMarkovModel hmm = hmmFile.getHMM();
252 int[] expected = { 0, 4, 100, 'T', 71, 'C', 12, 'G', 9, 'A', 9 };
253 int[] actual = AAFrequency.getHMMProfileFor(hmm, 17, false);
254 for (int i = 0; i < actual.length; i++)
258 assertEquals(actual[i], expected[i], 5);
262 assertEquals(actual[i], expected[i], 1);
266 int[] expected2 = { 0, 4, 85, 'A', 85, 'C', 0, 'G', 0, 'T', 0 };
267 int[] actual2 = AAFrequency.getHMMProfileFor(hmm, 2, true);
268 for (int i = 0; i < actual.length; i++)
272 assertEquals(actual[i], expected[i], 5);
276 assertEquals(actual[i], expected[i], 1);