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;
32 import jalview.gui.JvOptionPane;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
37 public class AAFrequencyTest
40 @BeforeClass(alwaysRun = true)
41 public void setUpJvOptionPane()
43 JvOptionPane.setInteractiveMode(false);
44 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47 @Test(groups = { "Functional" })
48 public void testCalculate_noProfile()
50 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
51 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
52 SequenceI seq3 = new Sequence("Seq3", "C---G");
53 SequenceI seq4 = new Sequence("Seq4", "CA--t");
54 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
55 int width = seq1.getLength();
56 ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
60 ProfileI col = result.get(0);
61 assertEquals(100f, col.getPercentageIdentity(false));
62 assertEquals(100f, col.getPercentageIdentity(true));
63 assertEquals(4, col.getMaxCount());
64 assertEquals("C", col.getModalResidue());
65 assertNull(col.getCounts());
69 assertEquals(75f, col.getPercentageIdentity(false));
70 assertEquals(100f, col.getPercentageIdentity(true));
71 assertEquals(3, col.getMaxCount());
72 assertEquals("A", col.getModalResidue());
74 // col 2 is 50% G 50% C or 25/25 counting gaps
76 assertEquals(25f, col.getPercentageIdentity(false));
77 assertEquals(50f, col.getPercentageIdentity(true));
78 assertEquals(1, col.getMaxCount());
79 assertEquals("CG", col.getModalResidue());
83 assertEquals(0f, col.getPercentageIdentity(false));
84 assertEquals(0f, col.getPercentageIdentity(true));
85 assertEquals(0, col.getMaxCount());
86 assertEquals("", col.getModalResidue());
88 // col 4 is 75% T 25% G
90 assertEquals(75f, col.getPercentageIdentity(false));
91 assertEquals(75f, col.getPercentageIdentity(true));
92 assertEquals(3, col.getMaxCount());
93 assertEquals("T", col.getModalResidue());
96 @Test(groups = { "Functional" })
97 public void testCalculate_withProfile()
99 SequenceI seq1 = new Sequence("Seq1", "CAGT");
100 SequenceI seq2 = new Sequence("Seq2", "CACT");
101 SequenceI seq3 = new Sequence("Seq3", "C--G");
102 SequenceI seq4 = new Sequence("Seq4", "CA-t");
103 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
104 int width = seq1.getLength();
105 ProfilesI result = AAFrequency.calculate(seqs, width, 0, width,
108 ProfileI profile = result.get(0);
109 assertEquals(4, profile.getCounts().getCount('C'));
110 assertEquals(4, profile.getHeight());
111 assertEquals(4, profile.getNonGapped());
113 profile = result.get(1);
114 assertEquals(3, profile.getCounts().getCount('A'));
115 assertEquals(4, profile.getHeight());
116 assertEquals(3, profile.getNonGapped());
118 profile = result.get(2);
119 assertEquals(1, profile.getCounts().getCount('C'));
120 assertEquals(1, profile.getCounts().getCount('G'));
121 assertEquals(4, profile.getHeight());
122 assertEquals(2, profile.getNonGapped());
124 profile = result.get(3);
125 assertEquals(3, profile.getCounts().getCount('T'));
126 assertEquals(1, profile.getCounts().getCount('G'));
127 assertEquals(4, profile.getHeight());
128 assertEquals(4, profile.getNonGapped());
131 @Test(groups = { "Functional" }, enabled = false)
132 public void testCalculate_withProfileTiming()
134 SequenceI seq1 = new Sequence("Seq1", "CAGT");
135 SequenceI seq2 = new Sequence("Seq2", "CACT");
136 SequenceI seq3 = new Sequence("Seq3", "C--G");
137 SequenceI seq4 = new Sequence("Seq4", "CA-t");
138 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
140 // ensure class loaded and initialised
141 int width = seq1.getLength();
142 AAFrequency.calculate(seqs, width, 0, width, true);
145 long start = System.currentTimeMillis();
146 for (int i = 0; i < reps; i++)
148 AAFrequency.calculate(seqs, width, 0, width, true);
150 System.out.println(System.currentTimeMillis() - start);
154 * Test generation of consensus annotation with options 'include gaps'
155 * (profile percentages are of all sequences, whether gapped or not), and
156 * 'show logo' (the full profile with all residue percentages is reported in
157 * the description for the tooltip)
159 @Test(groups = { "Functional" })
160 public void testCompleteConsensus_includeGaps_showLogo()
163 * first compute the profiles
165 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
166 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
167 SequenceI seq3 = new Sequence("Seq3", "C---G");
168 SequenceI seq4 = new Sequence("Seq4", "CA--t");
169 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
170 int width = seq1.getLength();
171 ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
173 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
174 "PID", new Annotation[width]);
176 .completeConsensus(consensus, profiles, 0, 5, false, true, 4);
178 Annotation ann = consensus.annotations[0];
179 assertEquals("C 100%", ann.description);
180 assertEquals("C", ann.displayCharacter);
181 ann = consensus.annotations[1];
182 assertEquals("A 75%", ann.description);
183 assertEquals("A", ann.displayCharacter);
184 ann = consensus.annotations[2];
185 assertEquals("C 25%; G 25%", ann.description);
186 assertEquals("+", ann.displayCharacter);
187 ann = consensus.annotations[3];
188 assertEquals("", ann.description);
189 assertEquals("-", ann.displayCharacter);
190 ann = consensus.annotations[4];
191 assertEquals("T 75%; G 25%", ann.description);
192 assertEquals("T", ann.displayCharacter);
196 * Test generation of consensus annotation with options 'ignore gaps' (profile
197 * percentages are of the non-gapped sequences) and 'no logo' (only the modal
198 * residue[s] percentage is reported in the description for the tooltip)
200 @Test(groups = { "Functional" })
201 public void testCompleteConsensus_ignoreGaps_noLogo()
204 * first compute the profiles
206 SequenceI seq1 = new Sequence("Seq1", "CAG-T");
207 SequenceI seq2 = new Sequence("Seq2", "CAC-T");
208 SequenceI seq3 = new Sequence("Seq3", "C---G");
209 SequenceI seq4 = new Sequence("Seq4", "CA--t");
210 SequenceI[] seqs = new SequenceI[] { seq1, seq2, seq3, seq4 };
211 int width = seq1.getLength();
212 ProfilesI profiles = AAFrequency.calculate(seqs, width, 0, width, true);
214 AlignmentAnnotation consensus = new AlignmentAnnotation("Consensus",
215 "PID", new Annotation[width]);
217 .completeConsensus(consensus, profiles, 0, 5, true, false, 4);
219 Annotation ann = consensus.annotations[0];
220 assertEquals("C 100%", ann.description);
221 assertEquals("C", ann.displayCharacter);
222 ann = consensus.annotations[1];
223 assertEquals("A 100%", ann.description);
224 assertEquals("A", ann.displayCharacter);
225 ann = consensus.annotations[2];
226 assertEquals("[CG] 50%", ann.description);
227 assertEquals("+", ann.displayCharacter);
228 ann = consensus.annotations[3];
229 assertEquals("", ann.description);
230 assertEquals("-", ann.displayCharacter);
231 ann = consensus.annotations[4];
232 assertEquals("T 75%", ann.description);
233 assertEquals("T", ann.displayCharacter);