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 java.util.Arrays;
32 import org.junit.Assert;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
36 public class ResidueCountTest
39 @BeforeClass(alwaysRun = true)
40 public void setUpJvOptionPane()
42 JvOptionPane.setInteractiveMode(false);
43 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47 * Test a mix of add and put for nucleotide counting
49 @Test(groups = "Functional")
50 public void test_countNucleotide()
52 ResidueCount rc = new ResidueCount(true);
53 assertEquals(rc.getCount('A'), 0);
54 assertEquals(rc.getGapCount(), 0);
56 assertEquals(rc.add('A'), 1);
57 assertEquals(rc.add('a'), 2);
60 assertEquals(rc.add('G'), 4);
62 assertEquals(rc.add('c'), 1);
64 assertEquals(rc.add('N'), 1);
66 assertEquals(rc.getCount('a'), 2);
67 assertEquals(rc.getCount('A'), 2);
68 assertEquals(rc.getCount('G'), 4);
69 assertEquals(rc.getCount('c'), 4);
70 assertEquals(rc.getCount('T'), 0); // never seen
71 assertEquals(rc.getCount('N'), 1);
72 assertEquals(rc.getCount('?'), 0);
73 assertEquals(rc.getCount('-'), 0);
74 assertEquals(rc.getTotalResidueCount(), 11);
76 assertFalse(rc.isCountingInts());
77 assertFalse(rc.isUsingOtherData());
81 * Test adding to gap count (either using addGap or add)
83 @Test(groups = "Functional")
84 public void testAddGap()
86 ResidueCount rc = new ResidueCount(true);
92 assertEquals(rc.getGapCount(), 4);
93 assertEquals(rc.getCount(' '), 4);
94 assertEquals(rc.getCount('-'), 4);
95 assertEquals(rc.getCount('.'), 4);
96 assertEquals(rc.getTotalResidueCount(), 0);
97 assertFalse(rc.isUsingOtherData());
98 assertFalse(rc.isCountingInts());
101 @Test(groups = "Functional")
102 public void testOverflow()
107 ResidueCount rc = new ResidueCount(true);
109 rc.put('A', Short.MAX_VALUE - 1);
110 assertFalse(rc.isCountingInts());
112 assertFalse(rc.isCountingInts());
114 assertTrue(rc.isCountingInts());
115 assertEquals(rc.getCount('a'), Short.MAX_VALUE + 1);
117 assertTrue(rc.isCountingInts());
118 assertEquals(rc.getCount('a'), Short.MAX_VALUE + 2);
119 assertEquals(rc.getGapCount(), 1);
121 assertEquals(rc.getGapCount(), 2);
126 rc = new ResidueCount(true);
127 rc.put('G', Short.MAX_VALUE + 1);
128 assertTrue(rc.isCountingInts());
129 assertEquals(rc.getCount('g'), Short.MAX_VALUE + 1);
131 assertTrue(rc.isCountingInts());
132 assertEquals(rc.getCount('g'), 1);
137 rc = new ResidueCount(true);
138 rc.put('G', Short.MIN_VALUE - 1);
139 assertTrue(rc.isCountingInts());
140 assertEquals(rc.getCount('g'), Short.MIN_VALUE - 1);
144 * Test a mix of add and put for peptide counting
146 @Test(groups = "Functional")
147 public void test_countPeptide()
149 ResidueCount rc = new ResidueCount(false);
159 assertEquals(rc.getCount('q'), 5);
160 assertEquals(rc.getCount('X'), 2);
161 assertEquals(rc.getCount('W'), 7);
162 assertEquals(rc.getCount('m'), 13);
163 assertEquals(rc.getCount('G'), 0);
164 assertEquals(rc.getCount('-'), 0);
165 assertEquals(rc.getTotalResidueCount(), 27);
167 assertFalse(rc.isCountingInts());
168 assertFalse(rc.isUsingOtherData());
171 @Test(groups = "Functional")
172 public void test_unexpectedPeptide()
174 ResidueCount rc = new ResidueCount(false);
175 // expected characters (upper or lower case):
176 String aas = "ACDEFGHIKLMNPQRSTVWXY";
177 String lower = aas.toLowerCase();
178 for (int i = 0; i < aas.length(); i++)
180 rc.put(aas.charAt(i), i);
181 rc.add(lower.charAt(i));
183 for (int i = 0; i < aas.length(); i++)
185 assertEquals(rc.getCount(aas.charAt(i)), i + 1);
187 assertFalse(rc.isUsingOtherData());
190 assertTrue(rc.isUsingOtherData());
191 assertEquals(rc.getCount('J'), 4);
193 assertEquals(rc.getCount('J'), 5);
196 @Test(groups = "Functional")
197 public void test_unexpectedNucleotide()
199 ResidueCount rc = new ResidueCount(true);
200 // expected characters (upper or lower case):
201 String nucs = "ACGTUN";
202 String lower = nucs.toLowerCase();
203 for (int i = 0; i < nucs.length(); i++)
205 rc.put(nucs.charAt(i), i);
206 rc.add(lower.charAt(i));
208 for (int i = 0; i < nucs.length(); i++)
210 assertEquals(rc.getCount(nucs.charAt(i)), i + 1);
212 assertFalse(rc.isUsingOtherData());
215 assertTrue(rc.isUsingOtherData());
218 @Test(groups = "Functional")
219 public void testGetModalCount()
221 ResidueCount rc = new ResidueCount(true);
225 assertEquals(rc.getModalCount(), 2);
227 // modal count is in the 'short overflow' counts
228 rc = new ResidueCount();
230 rc.put('g', Short.MAX_VALUE);
232 assertEquals(rc.getModalCount(), Short.MAX_VALUE + 1);
234 // modal count is in the 'other data' counts
235 rc = new ResidueCount(false);
239 assertEquals(rc.getModalCount(), 2);
241 // verify modal count excludes gap
242 rc = new ResidueCount();
249 assertEquals(rc.getModalCount(), 2);
252 @Test(groups = "Functional")
253 public void testGetResiduesForCount()
255 ResidueCount rc = new ResidueCount(true);
259 assertEquals(rc.getResiduesForCount(2), "C");
260 assertEquals(rc.getResiduesForCount(1), "G");
261 assertEquals(rc.getResiduesForCount(3), "");
262 assertEquals(rc.getResiduesForCount(0), "");
263 assertEquals(rc.getResiduesForCount(-1), "");
265 // modal count is in the 'short overflow' counts
266 rc = new ResidueCount(true);
268 rc.put('g', Short.MAX_VALUE);
270 assertEquals(rc.getResiduesForCount(Short.MAX_VALUE + 1), "G");
271 assertEquals(rc.getResiduesForCount(1), "C");
273 // peptide modal count is in the 'short overflow' counts
274 rc = new ResidueCount(false);
276 rc.put('p', Short.MAX_VALUE);
278 assertEquals(rc.getResiduesForCount(Short.MAX_VALUE + 1), "P");
279 assertEquals(rc.getResiduesForCount(1), "C");
281 // modal count is in the 'other data' counts
282 rc = new ResidueCount();
286 assertEquals(rc.getResiduesForCount(1), "Q");
287 assertEquals(rc.getResiduesForCount(2), "{");
289 // residues share modal count
290 rc = new ResidueCount();
296 assertEquals(rc.getResiduesForCount(1), "U");
297 assertEquals(rc.getResiduesForCount(2), "CG");
299 // expected and unexpected symbols share modal count
300 rc = new ResidueCount();
310 assertEquals(rc.getResiduesForCount(1), "U");
311 assertEquals(rc.getResiduesForCount(2), "CGT[");
314 @Test(groups = "Functional")
315 public void testGetSymbolCounts_nucleotide()
317 ResidueCount rc = new ResidueCount(true);
321 rc.add('J'); // 'otherData'
324 rc.put('[', 0); // 'otherdata'
326 SymbolCounts sc = rc.getSymbolCounts();
327 Assert.assertArrayEquals(new char[] { 'C', 'G', 'N', 'J', '[' },
329 Assert.assertArrayEquals(new int[] { 1, 3, 1, 1, 0 }, sc.values);
331 // now with overflow to int counts
332 rc.put('U', Short.MAX_VALUE);
334 sc = rc.getSymbolCounts();
335 Assert.assertArrayEquals(new char[] { 'C', 'G', 'N', 'U', 'J', '[' },
337 Assert.assertArrayEquals(new int[] { 1, 3, 1, 32768, 1, 0 }, sc.values);
340 @Test(groups = "Functional")
341 public void testGetSymbolCounts_peptide()
343 ResidueCount rc = new ResidueCount(false);
347 rc.add('Z'); // 'otherData'
351 SymbolCounts sc = rc.getSymbolCounts();
352 Assert.assertArrayEquals(new char[] { 'L', 'Q', 'W', 'Z' }, sc.symbols);
353 Assert.assertArrayEquals(new int[] { 1, 1, 3, 1 }, sc.values);
355 // now with overflow to int counts
356 rc.put('W', Short.MAX_VALUE);
358 sc = rc.getSymbolCounts();
359 Assert.assertArrayEquals(new char[] { 'L', 'Q', 'W', 'Z' }, sc.symbols);
360 Assert.assertArrayEquals(new int[] { 1, 1, 32768, 1 }, sc.values);
363 @Test(groups = "Functional")
364 public void testToString()
366 ResidueCount rc = new ResidueCount();
370 assertEquals(rc.toString(), "[ C:1 Q:2 ]");
374 assertEquals(rc.toString(), "[ C:1 Q:2 {:1 ]");
376 // switch from short to int counting:
377 rc.put('G', Short.MAX_VALUE);
379 assertEquals(rc.toString(), "[ C:1 G:32768 Q:2 {:1 ]");
382 @Test(groups = "Functional")
383 public void testGetTooltip()
385 ResidueCount rc = new ResidueCount();
388 assertEquals(rc.getTooltip(20, 1), "");
391 * count 7 C, 6 K, 7 Q, 10 P, 9 W, 1 F (total 40)
393 for (int i = 0; i < 7; i++)
398 for (int i = 0; i < 10; i++)
402 for (int i = 0; i < 9; i++)
406 for (int i = 0; i < 6; i++)
413 * percentages are rounded (0.5 rounded up)
414 * 10/40 9/40 7/40 6/40 1/40
416 assertEquals(rc.getTooltip(40, 0),
417 "P 25%; W 23%; C 18%; Q 18%; K 15%; F 3%");
421 * 10/30 9/30 8/30 7/30 6/30 1/30
423 assertEquals(rc.getTooltip(30, 1),
424 "P 33.3%; W 30.0%; Q 26.7%; C 23.3%; K 20.0%; F 3.3%");
427 @Test(groups = "Functional")
428 public void testPut()
430 ResidueCount rc = new ResidueCount();
432 assertEquals(rc.getCount('Q'), 3);
434 assertEquals(rc.getGapCount(), 4);
436 assertEquals(rc.getGapCount(), 5);
438 assertEquals(rc.getGapCount(), 6);
441 assertEquals(rc.getCount('?'), 5);
444 assertEquals(rc.getCount('?'), 6);
445 assertEquals(rc.getCount('!'), 7);
448 @Test(groups = "Functional")
449 public void testConstructor_forSequences()
451 SequenceI seq1 = new Sequence("seq1", "abcde--. FCD");
452 SequenceI seq2 = new Sequence("seq2", "ab.kKqBd-.");
453 ResidueCount rc = new ResidueCount(Arrays.asList(seq1, seq2));
455 assertEquals(rc.getGapCount(), 7);
456 assertEquals(rc.getTotalResidueCount(), 15); // excludes gaps
457 assertEquals(rc.getCount('a'), 2);
458 assertEquals(rc.getCount('A'), 2);
459 assertEquals(rc.getCount('B'), 3);
460 assertEquals(rc.getCount('c'), 2);
461 assertEquals(rc.getCount('D'), 3);
462 assertEquals(rc.getCount('f'), 1);
463 assertEquals(rc.getCount('K'), 2);
464 assertEquals(rc.getCount('Q'), 1);