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.datamodel;
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertTrue;
27 import jalview.datamodel.ResidueCount.SymbolCounts;
28 import jalview.gui.JvOptionPane;
30 import org.junit.Assert;
31 import org.testng.annotations.BeforeClass;
32 import org.testng.annotations.Test;
34 public class ResidueCountTest
37 @BeforeClass(alwaysRun = true)
38 public void setUpJvOptionPane()
40 JvOptionPane.setInteractiveMode(false);
41 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45 * Test a mix of add and put for nucleotide counting
47 @Test(groups = "Functional")
48 public void test_countNucleotide()
50 ResidueCount rc = new ResidueCount(true);
51 assertEquals(rc.getCount('A'), 0);
52 assertEquals(rc.getGapCount(), 0);
54 assertEquals(rc.add('A'), 1);
55 assertEquals(rc.add('a'), 2);
58 assertEquals(rc.add('G'), 4);
60 assertEquals(rc.add('c'), 1);
62 assertEquals(rc.add('N'), 1);
64 assertEquals(rc.getCount('a'), 2);
65 assertEquals(rc.getCount('A'), 2);
66 assertEquals(rc.getCount('G'), 4);
67 assertEquals(rc.getCount('c'), 4);
68 assertEquals(rc.getCount('T'), 0); // never seen
69 assertEquals(rc.getCount('N'), 1);
70 assertEquals(rc.getCount('?'), 0);
71 assertEquals(rc.getCount('-'), 0);
73 assertFalse(rc.isCountingInts());
74 assertFalse(rc.isUsingOtherData());
78 * Test adding to gap count (either using addGap or add)
80 @Test(groups = "Functional")
81 public void testAddGap()
83 ResidueCount rc = new ResidueCount(true);
89 assertEquals(rc.getGapCount(), 4);
90 assertEquals(rc.getCount(' '), 4);
91 assertEquals(rc.getCount('-'), 4);
92 assertEquals(rc.getCount('.'), 4);
93 assertFalse(rc.isUsingOtherData());
94 assertFalse(rc.isCountingInts());
97 @Test(groups = "Functional")
98 public void testOverflow()
103 ResidueCount rc = new ResidueCount(true);
105 rc.put('A', Short.MAX_VALUE - 1);
106 assertFalse(rc.isCountingInts());
108 assertFalse(rc.isCountingInts());
110 assertTrue(rc.isCountingInts());
111 assertEquals(rc.getCount('a'), Short.MAX_VALUE + 1);
113 assertTrue(rc.isCountingInts());
114 assertEquals(rc.getCount('a'), Short.MAX_VALUE + 2);
115 assertEquals(rc.getGapCount(), 1);
117 assertEquals(rc.getGapCount(), 2);
122 rc = new ResidueCount(true);
123 rc.put('G', Short.MAX_VALUE + 1);
124 assertTrue(rc.isCountingInts());
125 assertEquals(rc.getCount('g'), Short.MAX_VALUE + 1);
127 assertTrue(rc.isCountingInts());
128 assertEquals(rc.getCount('g'), 1);
133 rc = new ResidueCount(true);
134 rc.put('G', Short.MIN_VALUE - 1);
135 assertTrue(rc.isCountingInts());
136 assertEquals(rc.getCount('g'), Short.MIN_VALUE - 1);
140 * Test a mix of add and put for peptide counting
142 @Test(groups = "Functional")
143 public void test_countPeptide()
145 ResidueCount rc = new ResidueCount(false);
155 assertEquals(rc.getCount('q'), 5);
156 assertEquals(rc.getCount('X'), 2);
157 assertEquals(rc.getCount('W'), 7);
158 assertEquals(rc.getCount('m'), 13);
159 assertEquals(rc.getCount('G'), 0);
160 assertEquals(rc.getCount('-'), 0);
162 assertFalse(rc.isCountingInts());
163 assertFalse(rc.isUsingOtherData());
166 @Test(groups = "Functional")
167 public void test_unexpectedPeptide()
169 ResidueCount rc = new ResidueCount(false);
170 // expected characters (upper or lower case):
171 String aas = "ACDEFGHIKLMNPQRSTVWXY";
172 String lower = aas.toLowerCase();
173 for (int i = 0; i < aas.length(); i++)
175 rc.put(aas.charAt(i), i);
176 rc.add(lower.charAt(i));
178 for (int i = 0; i < aas.length(); i++)
180 assertEquals(rc.getCount(aas.charAt(i)), i + 1);
182 assertFalse(rc.isUsingOtherData());
185 assertTrue(rc.isUsingOtherData());
186 assertEquals(rc.getCount('J'), 4);
188 assertEquals(rc.getCount('J'), 5);
191 @Test(groups = "Functional")
192 public void test_unexpectedNucleotide()
194 ResidueCount rc = new ResidueCount(true);
195 // expected characters (upper or lower case):
196 String nucs = "ACGTUN";
197 String lower = nucs.toLowerCase();
198 for (int i = 0; i < nucs.length(); i++)
200 rc.put(nucs.charAt(i), i);
201 rc.add(lower.charAt(i));
203 for (int i = 0; i < nucs.length(); i++)
205 assertEquals(rc.getCount(nucs.charAt(i)), i + 1);
207 assertFalse(rc.isUsingOtherData());
210 assertTrue(rc.isUsingOtherData());
213 @Test(groups = "Functional")
214 public void testGetModalCount()
216 ResidueCount rc = new ResidueCount(true);
220 assertEquals(rc.getModalCount(), 2);
222 // modal count is in the 'short overflow' counts
223 rc = new ResidueCount();
225 rc.put('g', Short.MAX_VALUE);
227 assertEquals(rc.getModalCount(), Short.MAX_VALUE + 1);
229 // modal count is in the 'other data' counts
230 rc = new ResidueCount(false);
234 assertEquals(rc.getModalCount(), 2);
236 // verify modal count excludes gap
237 rc = new ResidueCount();
244 assertEquals(rc.getModalCount(), 2);
247 @Test(groups = "Functional")
248 public void testGetResiduesForCount()
250 ResidueCount rc = new ResidueCount(true);
254 assertEquals(rc.getResiduesForCount(2), "C");
255 assertEquals(rc.getResiduesForCount(1), "G");
256 assertEquals(rc.getResiduesForCount(3), "");
257 assertEquals(rc.getResiduesForCount(0), "");
258 assertEquals(rc.getResiduesForCount(-1), "");
260 // modal count is in the 'short overflow' counts
261 rc = new ResidueCount(true);
263 rc.put('g', Short.MAX_VALUE);
265 assertEquals(rc.getResiduesForCount(Short.MAX_VALUE + 1), "G");
266 assertEquals(rc.getResiduesForCount(1), "C");
268 // peptide modal count is in the 'short overflow' counts
269 rc = new ResidueCount(false);
271 rc.put('p', Short.MAX_VALUE);
273 assertEquals(rc.getResiduesForCount(Short.MAX_VALUE + 1), "P");
274 assertEquals(rc.getResiduesForCount(1), "C");
276 // modal count is in the 'other data' counts
277 rc = new ResidueCount();
281 assertEquals(rc.getResiduesForCount(1), "Q");
282 assertEquals(rc.getResiduesForCount(2), "{");
284 // residues share modal count
285 rc = new ResidueCount();
291 assertEquals(rc.getResiduesForCount(1), "U");
292 assertEquals(rc.getResiduesForCount(2), "CG");
294 // expected and unexpected symbols share modal count
295 rc = new ResidueCount();
305 assertEquals(rc.getResiduesForCount(1), "U");
306 assertEquals(rc.getResiduesForCount(2), "CGT[");
309 @Test(groups = "Functional")
310 public void testGetSymbolCounts_nucleotide()
312 ResidueCount rc = new ResidueCount(true);
316 rc.add('J'); // 'otherData'
319 rc.put('[', 0); // 'otherdata'
321 SymbolCounts sc = rc.getSymbolCounts();
322 Assert.assertArrayEquals(new char[] { 'C', 'G', 'N', 'J', '[' },
324 Assert.assertArrayEquals(new int[] { 1, 3, 1, 1, 0 }, sc.values);
326 // now with overflow to int counts
327 rc.put('U', Short.MAX_VALUE);
329 sc = rc.getSymbolCounts();
330 Assert.assertArrayEquals(new char[] { 'C', 'G', 'N', 'U', 'J', '[' },
332 Assert.assertArrayEquals(new int[] { 1, 3, 1, 32768, 1, 0 }, sc.values);
335 @Test(groups = "Functional")
336 public void testGetSymbolCounts_peptide()
338 ResidueCount rc = new ResidueCount(false);
342 rc.add('Z'); // 'otherData'
346 SymbolCounts sc = rc.getSymbolCounts();
347 Assert.assertArrayEquals(new char[] { 'L', 'Q', 'W', 'Z' }, sc.symbols);
348 Assert.assertArrayEquals(new int[] { 1, 1, 3, 1 }, sc.values);
350 // now with overflow to int counts
351 rc.put('W', Short.MAX_VALUE);
353 sc = rc.getSymbolCounts();
354 Assert.assertArrayEquals(new char[] { 'L', 'Q', 'W', 'Z' }, sc.symbols);
355 Assert.assertArrayEquals(new int[] { 1, 1, 32768, 1 }, sc.values);
358 @Test(groups = "Functional")
359 public void testToString()
361 ResidueCount rc = new ResidueCount();
365 assertEquals(rc.toString(), "[ C:1 Q:2 ]");
369 assertEquals(rc.toString(), "[ C:1 Q:2 {:1 ]");
371 // switch from short to int counting:
372 rc.put('G', Short.MAX_VALUE);
374 assertEquals(rc.toString(), "[ C:1 G:32768 Q:2 {:1 ]");
377 @Test(groups = "Functional")
378 public void testGetTooltip()
380 ResidueCount rc = new ResidueCount();
383 assertEquals(rc.getTooltip(20, 1), "");
386 * count 7 C, 6 K, 7 Q, 10 P, 9 W, 1 F (total 40)
388 for (int i = 0; i < 7; i++)
393 for (int i = 0; i < 10; i++)
397 for (int i = 0; i < 9; i++)
401 for (int i = 0; i < 6; i++)
408 * percentages are rounded (0.5 rounded up)
409 * 10/40 9/40 7/40 6/40 1/40
411 assertEquals(rc.getTooltip(40, 0),
412 "P 25%; W 23%; C 18%; Q 18%; K 15%; F 3%");
416 * 10/30 9/30 8/30 7/30 6/30 1/30
418 assertEquals(rc.getTooltip(30, 1),
419 "P 33.3%; W 30.0%; Q 26.7%; C 23.3%; K 20.0%; F 3.3%");
422 @Test(groups = "Functional")
423 public void testPut()
425 ResidueCount rc = new ResidueCount();
427 assertEquals(rc.getCount('Q'), 3);
429 assertEquals(rc.getGapCount(), 4);
431 assertEquals(rc.getGapCount(), 5);
433 assertEquals(rc.getGapCount(), 6);
436 assertEquals(rc.getCount('?'), 5);
439 assertEquals(rc.getCount('?'), 6);
440 assertEquals(rc.getCount('!'), 7);