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.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
28 import jalview.analysis.AlignmentGenerator;
29 import jalview.gui.JvOptionPane;
31 import java.util.Arrays;
32 import java.util.BitSet;
33 import java.util.List;
34 import java.util.Random;
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
39 public class HiddenColumnsTest
42 @BeforeClass(alwaysRun = true)
43 public void setUpJvOptionPane()
45 JvOptionPane.setInteractiveMode(false);
46 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50 * Test the method which counts the number of hidden columns
52 @Test(groups = { "Functional" })
53 public void testGetSize()
55 HiddenColumns hidden = new HiddenColumns();
56 assertEquals(0, hidden.getSize());
58 hidden.hideColumns(3, 5);
59 assertEquals(3, hidden.getSize());
61 hidden.hideColumns(8, 8);
62 assertEquals(4, hidden.getSize());
64 hidden.hideColumns(9, 14);
65 assertEquals(10, hidden.getSize());
67 ColumnSelection cs = new ColumnSelection();
68 hidden.revealAllHiddenColumns(cs);
69 assertEquals(0, hidden.getSize());
73 * Test the method that finds the visible column position of an alignment
74 * column, allowing for hidden columns.
76 @Test(groups = { "Functional" })
77 public void testFindColumnPosition()
79 HiddenColumns cs = new HiddenColumns();
80 assertEquals(5, cs.findColumnPosition(5));
82 // hiding column 6 makes no difference
84 assertEquals(5, cs.findColumnPosition(5));
86 // hiding column 4 moves column 5 to column 4
88 assertEquals(4, cs.findColumnPosition(5));
90 // hiding column 4 moves column 4 to position 3
91 assertEquals(3, cs.findColumnPosition(4));
93 // hiding columns 1 and 2 moves column 5 to column 2
95 assertEquals(2, cs.findColumnPosition(5));
97 // check with > 1 hidden column regions
98 // where some columns are in the hidden regions
99 HiddenColumns cs2 = new HiddenColumns();
100 cs2.hideColumns(5, 10);
101 cs2.hideColumns(20, 27);
102 cs2.hideColumns(40, 44);
104 // hiding columns 5-10 and 20-27 moves column 8 to column 4
105 assertEquals(4, cs2.findColumnPosition(8));
107 // and moves column 24 to 13
108 assertEquals(13, cs2.findColumnPosition(24));
110 // and moves column 28 to 14
111 assertEquals(14, cs2.findColumnPosition(28));
113 // and moves column 40 to 25
114 assertEquals(25, cs2.findColumnPosition(40));
116 // check when hidden columns start at 0 that the visible column
118 HiddenColumns cs3 = new HiddenColumns();
119 cs3.hideColumns(0, 4);
120 assertEquals(0, cs3.findColumnPosition(2));
125 * Test the method that finds the visible column position a given distance
126 * before another column
128 @Test(groups = { "Functional" })
129 public void testFindColumnNToLeft()
131 HiddenColumns cs = new HiddenColumns();
133 // test that without hidden columns, findColumnNToLeft returns
134 // position n to left of provided position
135 int pos = cs.subtractVisibleColumns(3, 10);
136 assertEquals(7, pos);
138 // 0 returns same position
139 pos = cs.subtractVisibleColumns(0, 10);
140 assertEquals(10, pos);
142 // overflow to left returns negative number
143 pos = cs.subtractVisibleColumns(3, 0);
144 assertEquals(-3, pos);
146 // test that with hidden columns to left of result column
147 // behaviour is the same as above
148 cs.hideColumns(1, 3);
150 // position n to left of provided position
151 pos = cs.subtractVisibleColumns(3, 10);
152 assertEquals(7, pos);
154 // 0 returns same position
155 pos = cs.subtractVisibleColumns(0, 10);
156 assertEquals(10, pos);
158 // test with one set of hidden columns between start and required position
159 cs.hideColumns(12, 15);
160 pos = cs.subtractVisibleColumns(8, 17);
161 assertEquals(5, pos);
163 // test with two sets of hidden columns between start and required position
164 cs.hideColumns(20, 21);
165 pos = cs.subtractVisibleColumns(8, 23);
166 assertEquals(9, pos);
168 // repeat last 2 tests with no hidden columns to left of required position
169 ColumnSelection colsel = new ColumnSelection();
170 cs.revealAllHiddenColumns(colsel);
172 // test with one set of hidden columns between start and required position
173 cs.hideColumns(12, 15);
174 pos = cs.subtractVisibleColumns(8, 17);
175 assertEquals(5, pos);
177 // test with two sets of hidden columns between start and required position
178 cs.hideColumns(20, 21);
179 pos = cs.subtractVisibleColumns(8, 23);
180 assertEquals(9, pos);
184 @Test(groups = { "Functional" })
185 public void testGetVisibleContigs()
187 HiddenColumns cs = new HiddenColumns();
188 cs.hideColumns(3, 6);
189 cs.hideColumns(8, 9);
190 cs.hideColumns(12, 12);
192 // start position is inclusive, end position exclusive:
193 int[] visible = cs.getVisibleContigs(1, 13);
194 assertEquals("[1, 2, 7, 7, 10, 11]", Arrays.toString(visible));
196 visible = cs.getVisibleContigs(4, 14);
197 assertEquals("[7, 7, 10, 11, 13, 13]", Arrays.toString(visible));
199 visible = cs.getVisibleContigs(3, 10);
200 assertEquals("[7, 7]", Arrays.toString(visible));
202 visible = cs.getVisibleContigs(4, 6);
203 assertEquals("[]", Arrays.toString(visible));
206 @Test(groups = { "Functional" })
207 public void testEquals()
209 HiddenColumns cs = new HiddenColumns();
210 cs.hideColumns(5, 9);
212 // a different set of hidden columns
213 HiddenColumns cs2 = new HiddenColumns();
215 // with no hidden columns
216 assertFalse(cs.equals(cs2));
217 assertFalse(cs2.equals(cs));
219 // with hidden columns added in a different order
220 cs2.hideColumns(6, 9);
221 cs2.hideColumns(5, 8);
223 assertTrue(cs.equals(cs2));
224 assertTrue(cs.equals(cs));
225 assertTrue(cs2.equals(cs));
226 assertTrue(cs2.equals(cs2));
229 @Test(groups = "Functional")
230 public void testCopyConstructor()
232 HiddenColumns cs = new HiddenColumns();
233 cs.hideColumns(10, 11);
234 cs.hideColumns(5, 7);
235 assertEquals("[5, 7]", Arrays.toString(cs.getHiddenRegions().get(0)));
237 HiddenColumns cs2 = new HiddenColumns(cs);
238 assertTrue(cs2.hasHiddenColumns());
239 assertEquals(2, cs2.getHiddenRegions().size());
240 // hidden columns are held in column order
241 assertEquals("[5, 7]", Arrays.toString(cs2.getHiddenRegions().get(0)));
242 assertEquals("[10, 11]", Arrays.toString(cs2.getHiddenRegions().get(1)));
246 * Test the code used to locate the reference sequence ruler origin
248 @Test(groups = { "Functional" })
249 public void testLocateVisibleBoundsofSequence()
251 // create random alignment
252 AlignmentGenerator gen = new AlignmentGenerator(false);
253 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
255 HiddenColumns cs = al.getHiddenColumns();
256 ColumnSelection colsel = new ColumnSelection();
258 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
259 assertEquals(2, seq.findIndex(seq.getStart()));
263 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
264 seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
265 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
266 seq.findIndex(seq.getEnd()) - 1 }),
267 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
269 // hidden column on gap after end of sequence - should not affect bounds
270 colsel.hideSelectedColumns(13, al.getHiddenColumns());
272 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
273 seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
274 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
275 seq.findIndex(seq.getEnd()) - 1 }),
276 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
278 cs.revealAllHiddenColumns(colsel);
279 // hidden column on gap before beginning of sequence - should vis bounds by
281 colsel.hideSelectedColumns(0, al.getHiddenColumns());
283 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 2,
284 seq.findIndex(seq.getEnd()) - 2, seq.getStart(),
285 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
286 seq.findIndex(seq.getEnd()) - 1 }),
287 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
289 cs.revealAllHiddenColumns(colsel);
290 // hide columns around most of sequence - leave one residue remaining
291 cs.hideColumns(1, 3);
292 cs.hideColumns(6, 11);
294 cs.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]);
296 Arrays.toString(new int[] { 1, 1, 3, 3,
297 seq.findIndex(seq.getStart()) - 1,
298 seq.findIndex(seq.getEnd()) - 1 }),
299 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
300 cs.revealAllHiddenColumns(colsel);
302 // hide whole sequence - should just get location of hidden region
303 // containing sequence
304 cs.hideColumns(1, 11);
306 Arrays.toString(new int[] { 0, 1, 0, 0,
307 seq.findIndex(seq.getStart()) - 1,
308 seq.findIndex(seq.getEnd()) - 1 }),
309 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
313 @Test(groups = { "Functional" })
314 public void testLocateVisibleBoundsPathologicals()
316 // test some pathological cases we missed
317 AlignmentI al = new Alignment(new SequenceI[] { new Sequence(
318 "refseqGaptest", "KTDVTI----------NFI-----G----L") });
319 HiddenColumns cs = new HiddenColumns();
320 cs.hideInsertionsFor(al.getSequenceAt(0));
324 + al.getSequenceAt(0).getCharAt(
325 cs.adjustForHiddenColumns(9)));
329 @Test(groups = { "Functional" })
330 public void testHideColumns()
332 // create random alignment
333 AlignmentGenerator gen = new AlignmentGenerator(false);
334 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
336 ColumnSelection colsel = new ColumnSelection();
337 HiddenColumns cs = al.getHiddenColumns();
338 colsel.hideSelectedColumns(5, al.getHiddenColumns());
339 List<int[]> hidden = cs.getHiddenRegions();
340 assertEquals(1, hidden.size());
341 assertEquals("[5, 5]", Arrays.toString(hidden.get(0)));
343 colsel.hideSelectedColumns(3, al.getHiddenColumns());
344 assertEquals(2, hidden.size());
345 // two hidden ranges, in order:
346 assertSame(hidden, cs.getHiddenRegions());
347 assertEquals("[3, 3]", Arrays.toString(hidden.get(0)));
348 assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
350 // hiding column 4 expands [3, 3] to [3, 4]
351 // and merges to [5, 5] to make [3, 5]
352 colsel.hideSelectedColumns(4, al.getHiddenColumns());
353 hidden = cs.getHiddenRegions();
354 assertEquals(1, hidden.size());
355 assertEquals("[3, 5]", Arrays.toString(hidden.get(0)));
357 // clear hidden columns (note they are added to selected)
358 cs.revealAllHiddenColumns(colsel);
359 // it is now actually null but getter returns an empty list
360 assertTrue(cs.getHiddenRegions().isEmpty());
362 cs.hideColumns(3, 6);
363 hidden = cs.getHiddenRegions();
364 int[] firstHiddenRange = hidden.get(0);
365 assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
367 // adding a subrange of already hidden should do nothing
368 cs.hideColumns(4, 5);
369 assertEquals(1, hidden.size());
370 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
371 cs.hideColumns(3, 5);
372 assertEquals(1, hidden.size());
373 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
374 cs.hideColumns(4, 6);
375 assertEquals(1, hidden.size());
376 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
377 cs.hideColumns(3, 6);
378 assertEquals(1, hidden.size());
379 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
381 cs.revealAllHiddenColumns(colsel);
382 cs.hideColumns(2, 4);
383 hidden = cs.getHiddenRegions();
384 assertEquals(1, hidden.size());
385 assertEquals("[2, 4]", Arrays.toString(hidden.get(0)));
387 // extend contiguous with 2 positions overlap
388 cs.hideColumns(3, 5);
389 assertEquals(1, hidden.size());
390 assertEquals("[2, 5]", Arrays.toString(hidden.get(0)));
392 // extend contiguous with 1 position overlap
393 cs.hideColumns(5, 6);
394 assertEquals(1, hidden.size());
395 assertEquals("[2, 6]", Arrays.toString(hidden.get(0)));
397 // extend contiguous with overlap both ends:
398 cs.hideColumns(1, 7);
399 assertEquals(1, hidden.size());
400 assertEquals("[1, 7]", Arrays.toString(hidden.get(0)));
404 * Test the method that reveals a range of hidden columns given the start
405 * column of the range
407 @Test(groups = { "Functional" })
408 public void testRevealHiddenColumns()
410 ColumnSelection colsel = new ColumnSelection();
411 HiddenColumns cs = new HiddenColumns();
412 cs.hideColumns(5, 8);
413 colsel.addElement(10);
414 cs.revealHiddenColumns(5, colsel);
415 // hidden columns list now null but getter returns empty list:
416 assertTrue(cs.getHiddenRegions().isEmpty());
417 // revealed columns are marked as selected (added to selection):
418 assertEquals("[10, 5, 6, 7, 8]", colsel.getSelected().toString());
420 // calling with a column other than the range start does nothing:
421 colsel = new ColumnSelection();
422 cs = new HiddenColumns();
423 cs.hideColumns(5, 8);
424 List<int[]> hidden = cs.getHiddenRegions();
425 cs.revealHiddenColumns(6, colsel);
426 assertSame(hidden, cs.getHiddenRegions());
427 assertTrue(colsel.getSelected().isEmpty());
430 @Test(groups = { "Functional" })
431 public void testRevealAllHiddenColumns()
433 HiddenColumns cs = new HiddenColumns();
434 ColumnSelection colsel = new ColumnSelection();
435 cs.hideColumns(5, 8);
436 cs.hideColumns(2, 3);
437 colsel.addElement(11);
438 colsel.addElement(1);
439 cs.revealAllHiddenColumns(colsel);
442 * revealing hidden columns adds them (in order) to the (unordered)
445 assertTrue(cs.getHiddenRegions().isEmpty());
446 assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]", colsel.getSelected()
450 @Test(groups = { "Functional" })
451 public void testIsVisible()
453 HiddenColumns cs = new HiddenColumns();
454 cs.hideColumns(2, 4);
455 cs.hideColumns(6, 7);
456 assertTrue(cs.isVisible(0));
457 assertTrue(cs.isVisible(-99));
458 assertTrue(cs.isVisible(1));
459 assertFalse(cs.isVisible(2));
460 assertFalse(cs.isVisible(3));
461 assertFalse(cs.isVisible(4));
462 assertTrue(cs.isVisible(5));
463 assertFalse(cs.isVisible(6));
464 assertFalse(cs.isVisible(7));
468 * Test for the case when a hidden range encloses more one already hidden
471 @Test(groups = { "Functional" })
472 public void testHideColumns_subsumingHidden()
475 * JAL-2370 bug scenario:
476 * two hidden ranges subsumed by a third
478 HiddenColumns cs = new HiddenColumns();
479 cs.hideColumns(49, 59);
480 cs.hideColumns(69, 79);
481 List<int[]> hidden = cs.getHiddenRegions();
482 assertEquals(2, hidden.size());
483 assertEquals("[49, 59]", Arrays.toString(hidden.get(0)));
484 assertEquals("[69, 79]", Arrays.toString(hidden.get(1)));
486 cs.hideColumns(48, 80);
487 hidden = cs.getHiddenRegions();
488 assertEquals(1, hidden.size());
489 assertEquals("[48, 80]", Arrays.toString(hidden.get(0)));
492 * another...joining hidden ranges
494 cs = new HiddenColumns();
495 cs.hideColumns(10, 20);
496 cs.hideColumns(30, 40);
497 cs.hideColumns(50, 60);
498 // hiding 21-49 should merge to one range
499 cs.hideColumns(21, 49);
500 hidden = cs.getHiddenRegions();
501 assertEquals(1, hidden.size());
502 assertEquals("[10, 60]", Arrays.toString(hidden.get(0)));
505 * another...left overlap, subsumption, right overlap,
506 * no overlap of existing hidden ranges
508 cs = new HiddenColumns();
509 cs.hideColumns(10, 20);
510 cs.hideColumns(10, 20);
511 cs.hideColumns(30, 35);
512 cs.hideColumns(40, 50);
513 cs.hideColumns(60, 70);
515 cs.hideColumns(15, 45);
516 hidden = cs.getHiddenRegions();
517 assertEquals(2, hidden.size());
518 assertEquals("[10, 50]", Arrays.toString(hidden.get(0)));
519 assertEquals("[60, 70]", Arrays.toString(hidden.get(1)));
522 @Test(groups = { "Functional" })
523 public void testHideBitset()
527 BitSet one = new BitSet();
531 cs = new HiddenColumns();
532 cs.hideMarkedBits(one);
533 assertEquals(1, cs.getHiddenRegions().size());
536 cs = new HiddenColumns();
537 cs.hideMarkedBits(one);
538 assertEquals(1, cs.getHiddenRegions().size());
541 cs = new HiddenColumns();
542 cs.hideMarkedBits(one);
543 assertEquals(1, cs.getHiddenRegions().size());
547 cs = new HiddenColumns();
548 cs.hideMarkedBits(one);
549 assertEquals(2, cs.getHiddenRegions().size());
551 assertEquals(0, cs.adjustForHiddenColumns(0));
552 assertEquals(2, cs.adjustForHiddenColumns(1));
553 assertEquals(4, cs.adjustForHiddenColumns(2));
557 cs = new HiddenColumns();
558 cs.hideMarkedBits(one);
560 assertEquals(1, cs.getHiddenRegions().size());
562 assertEquals(0, cs.adjustForHiddenColumns(0));
563 assertEquals(1, cs.adjustForHiddenColumns(1));
564 assertEquals(2, cs.adjustForHiddenColumns(2));
565 assertEquals(4, cs.adjustForHiddenColumns(3));
568 @Test(groups = { "Functional" })
569 public void testGetBitset()
571 BitSet toMark, fromMark;
572 long seed = -3241532;
573 Random number = new Random(seed);
574 for (int n = 0; n < 1000; n++)
576 // create a random bitfield
577 toMark = BitSet.valueOf(new long[] { number.nextLong(),
578 number.nextLong(), number.nextLong() });
579 toMark.set(n * number.nextInt(10), n * (25 + number.nextInt(25)));
580 HiddenColumns hc = new HiddenColumns();
581 hc.hideMarkedBits(toMark);
583 // see if we can recover bitfield
584 hc.markHiddenRegions(fromMark = new BitSet());
585 assertEquals(toMark, fromMark);