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.List;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
37 public class HiddenColumnsTest
40 @BeforeClass(alwaysRun = true)
41 public void setUpJvOptionPane()
43 JvOptionPane.setInteractiveMode(false);
44 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48 * Test the method which counts the number of hidden columns
50 @Test(groups = { "Functional" })
51 public void testGetSize()
53 HiddenColumns hidden = new HiddenColumns();
54 assertEquals(0, hidden.getSize());
56 hidden.hideColumns(3, 5);
57 assertEquals(3, hidden.getSize());
59 hidden.hideColumns(8, 8);
60 assertEquals(4, hidden.getSize());
62 hidden.hideColumns(9, 14);
63 assertEquals(10, hidden.getSize());
65 ColumnSelection cs = new ColumnSelection();
66 hidden.revealAllHiddenColumns(cs);
67 assertEquals(0, hidden.getSize());
71 * Test the method that finds the visible column position of an alignment
72 * column, allowing for hidden columns.
74 @Test(groups = { "Functional" })
75 public void testFindColumnPosition()
77 HiddenColumns cs = new HiddenColumns();
78 assertEquals(5, cs.findColumnPosition(5));
80 // hiding column 6 makes no difference
82 assertEquals(5, cs.findColumnPosition(5));
84 // hiding column 4 moves column 5 to column 4
86 assertEquals(4, cs.findColumnPosition(5));
88 // hiding column 4 moves column 4 to position 3
89 assertEquals(3, cs.findColumnPosition(4));
91 // hiding columns 1 and 2 moves column 5 to column 2
93 assertEquals(2, cs.findColumnPosition(5));
95 // check with > 1 hidden column regions
96 // where some columns are in the hidden regions
97 HiddenColumns cs2 = new HiddenColumns();
98 cs2.hideColumns(5, 10);
99 cs2.hideColumns(20, 27);
100 cs2.hideColumns(40, 44);
102 // hiding columns 5-10 and 20-27 moves column 8 to column 4
103 assertEquals(4, cs2.findColumnPosition(8));
105 // and moves column 24 to 13
106 assertEquals(13, cs2.findColumnPosition(24));
108 // and moves column 28 to 14
109 assertEquals(14, cs2.findColumnPosition(28));
111 // and moves column 40 to 25
112 assertEquals(25, cs2.findColumnPosition(40));
114 // check when hidden columns start at 0 that the visible column
116 HiddenColumns cs3 = new HiddenColumns();
117 cs3.hideColumns(0, 4);
118 assertEquals(0, cs3.findColumnPosition(2));
123 * Test the method that finds the visible column position a given distance
124 * before another column
126 @Test(groups = { "Functional" })
127 public void testFindColumnNToLeft()
129 HiddenColumns cs = new HiddenColumns();
131 // test that without hidden columns, findColumnNToLeft returns
132 // position n to left of provided position
133 int pos = cs.subtractVisibleColumns(3, 10);
134 assertEquals(7, pos);
136 // 0 returns same position
137 pos = cs.subtractVisibleColumns(0, 10);
138 assertEquals(10, pos);
140 // overflow to left returns negative number
141 pos = cs.subtractVisibleColumns(3, 0);
142 assertEquals(-3, pos);
144 // test that with hidden columns to left of result column
145 // behaviour is the same as above
146 cs.hideColumns(1, 3);
148 // position n to left of provided position
149 pos = cs.subtractVisibleColumns(3, 10);
150 assertEquals(7, pos);
152 // 0 returns same position
153 pos = cs.subtractVisibleColumns(0, 10);
154 assertEquals(10, pos);
156 // test with one set of hidden columns between start and required position
157 cs.hideColumns(12, 15);
158 pos = cs.subtractVisibleColumns(8, 17);
159 assertEquals(5, pos);
161 // test with two sets of hidden columns between start and required position
162 cs.hideColumns(20, 21);
163 pos = cs.subtractVisibleColumns(8, 23);
164 assertEquals(9, pos);
166 // repeat last 2 tests with no hidden columns to left of required position
167 ColumnSelection colsel = new ColumnSelection();
168 cs.revealAllHiddenColumns(colsel);
170 // test with one set of hidden columns between start and required position
171 cs.hideColumns(12, 15);
172 pos = cs.subtractVisibleColumns(8, 17);
173 assertEquals(5, pos);
175 // test with two sets of hidden columns between start and required position
176 cs.hideColumns(20, 21);
177 pos = cs.subtractVisibleColumns(8, 23);
178 assertEquals(9, pos);
182 @Test(groups = { "Functional" })
183 public void testGetVisibleContigs()
185 HiddenColumns cs = new HiddenColumns();
186 cs.hideColumns(3, 6);
187 cs.hideColumns(8, 9);
188 cs.hideColumns(12, 12);
190 // start position is inclusive, end position exclusive:
191 int[] visible = cs.getVisibleContigs(1, 13);
192 assertEquals("[1, 2, 7, 7, 10, 11]", Arrays.toString(visible));
194 visible = cs.getVisibleContigs(4, 14);
195 assertEquals("[7, 7, 10, 11, 13, 13]", Arrays.toString(visible));
197 visible = cs.getVisibleContigs(3, 10);
198 assertEquals("[7, 7]", Arrays.toString(visible));
200 visible = cs.getVisibleContigs(4, 6);
201 assertEquals("[]", Arrays.toString(visible));
204 @Test(groups = { "Functional" })
205 public void testEquals()
207 HiddenColumns cs = new HiddenColumns();
208 cs.hideColumns(5, 9);
210 // a different set of hidden columns
211 HiddenColumns cs2 = new HiddenColumns();
213 // with no hidden columns
214 assertFalse(cs.equals(cs2));
215 assertFalse(cs2.equals(cs));
217 // with hidden columns added in a different order
218 cs2.hideColumns(6, 9);
219 cs2.hideColumns(5, 8);
221 assertTrue(cs.equals(cs2));
222 assertTrue(cs.equals(cs));
223 assertTrue(cs2.equals(cs));
224 assertTrue(cs2.equals(cs2));
227 @Test(groups = "Functional")
228 public void testCopyConstructor()
230 HiddenColumns cs = new HiddenColumns();
231 cs.hideColumns(10, 11);
232 cs.hideColumns(5, 7);
233 assertEquals("[5, 7]", Arrays.toString(cs.getHiddenRegions().get(0)));
235 HiddenColumns cs2 = new HiddenColumns(cs);
236 assertTrue(cs2.hasHiddenColumns());
237 assertEquals(2, cs2.getHiddenRegions().size());
238 // hidden columns are held in column order
239 assertEquals("[5, 7]", Arrays.toString(cs2.getHiddenRegions().get(0)));
240 assertEquals("[10, 11]", Arrays.toString(cs2.getHiddenRegions().get(1)));
244 * Test the code used to locate the reference sequence ruler origin
246 @Test(groups = { "Functional" })
247 public void testLocateVisibleBoundsofSequence()
249 // create random alignment
250 AlignmentGenerator gen = new AlignmentGenerator(false);
251 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
253 HiddenColumns cs = al.getHiddenColumns();
254 ColumnSelection colsel = new ColumnSelection();
256 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
257 assertEquals(2, seq.findIndex(seq.getStart()));
261 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
262 seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
263 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
264 seq.findIndex(seq.getEnd()) - 1 }),
265 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
267 // hidden column on gap after end of sequence - should not affect bounds
268 colsel.hideSelectedColumns(13, al.getHiddenColumns());
270 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
271 seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
272 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
273 seq.findIndex(seq.getEnd()) - 1 }),
274 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
276 cs.revealAllHiddenColumns(colsel);
277 // hidden column on gap before beginning of sequence - should vis bounds by
279 colsel.hideSelectedColumns(0, al.getHiddenColumns());
281 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 2,
282 seq.findIndex(seq.getEnd()) - 2, seq.getStart(),
283 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
284 seq.findIndex(seq.getEnd()) - 1 }),
285 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
287 cs.revealAllHiddenColumns(colsel);
288 // hide columns around most of sequence - leave one residue remaining
289 cs.hideColumns(1, 3);
290 cs.hideColumns(6, 11);
292 cs.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]);
294 Arrays.toString(new int[] { 1, 1, 3, 3,
295 seq.findIndex(seq.getStart()) - 1,
296 seq.findIndex(seq.getEnd()) - 1 }),
297 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
298 cs.revealAllHiddenColumns(colsel);
300 // hide whole sequence - should just get location of hidden region
301 // containing sequence
302 cs.hideColumns(1, 11);
304 Arrays.toString(new int[] { 0, 1, 0, 0,
305 seq.findIndex(seq.getStart()) - 1,
306 seq.findIndex(seq.getEnd()) - 1 }),
307 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
311 @Test(groups = { "Functional" })
312 public void testLocateVisibleBoundsPathologicals()
314 // test some pathological cases we missed
315 AlignmentI al = new Alignment(new SequenceI[] { new Sequence(
316 "refseqGaptest", "KTDVTI----------NFI-----G----L") });
317 HiddenColumns cs = new HiddenColumns();
318 cs.hideInsertionsFor(al.getSequenceAt(0));
322 + al.getSequenceAt(0).getCharAt(
323 cs.adjustForHiddenColumns(9)));
327 @Test(groups = { "Functional" })
328 public void testHideColumns()
330 // create random alignment
331 AlignmentGenerator gen = new AlignmentGenerator(false);
332 AlignmentI al = gen.generate(50, 20, 123, 5, 5);
334 ColumnSelection colsel = new ColumnSelection();
335 HiddenColumns cs = al.getHiddenColumns();
336 colsel.hideSelectedColumns(5, al.getHiddenColumns());
337 List<int[]> hidden = cs.getHiddenRegions();
338 assertEquals(1, hidden.size());
339 assertEquals("[5, 5]", Arrays.toString(hidden.get(0)));
341 colsel.hideSelectedColumns(3, al.getHiddenColumns());
342 assertEquals(2, hidden.size());
343 // two hidden ranges, in order:
344 assertSame(hidden, cs.getHiddenRegions());
345 assertEquals("[3, 3]", Arrays.toString(hidden.get(0)));
346 assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
348 // hiding column 4 expands [3, 3] to [3, 4]
349 // and merges to [5, 5] to make [3, 5]
350 colsel.hideSelectedColumns(4, al.getHiddenColumns());
351 hidden = cs.getHiddenRegions();
352 assertEquals(1, hidden.size());
353 assertEquals("[3, 5]", Arrays.toString(hidden.get(0)));
355 // clear hidden columns (note they are added to selected)
356 cs.revealAllHiddenColumns(colsel);
357 // it is now actually null but getter returns an empty list
358 assertTrue(cs.getHiddenRegions().isEmpty());
360 cs.hideColumns(3, 6);
361 hidden = cs.getHiddenRegions();
362 int[] firstHiddenRange = hidden.get(0);
363 assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
365 // adding a subrange of already hidden should do nothing
366 cs.hideColumns(4, 5);
367 assertEquals(1, hidden.size());
368 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
369 cs.hideColumns(3, 5);
370 assertEquals(1, hidden.size());
371 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
372 cs.hideColumns(4, 6);
373 assertEquals(1, hidden.size());
374 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
375 cs.hideColumns(3, 6);
376 assertEquals(1, hidden.size());
377 assertSame(firstHiddenRange, cs.getHiddenRegions().get(0));
379 cs.revealAllHiddenColumns(colsel);
380 cs.hideColumns(2, 4);
381 hidden = cs.getHiddenRegions();
382 assertEquals(1, hidden.size());
383 assertEquals("[2, 4]", Arrays.toString(hidden.get(0)));
385 // extend contiguous with 2 positions overlap
386 cs.hideColumns(3, 5);
387 assertEquals(1, hidden.size());
388 assertEquals("[2, 5]", Arrays.toString(hidden.get(0)));
390 // extend contiguous with 1 position overlap
391 cs.hideColumns(5, 6);
392 assertEquals(1, hidden.size());
393 assertEquals("[2, 6]", Arrays.toString(hidden.get(0)));
395 // extend contiguous with overlap both ends:
396 cs.hideColumns(1, 7);
397 assertEquals(1, hidden.size());
398 assertEquals("[1, 7]", Arrays.toString(hidden.get(0)));
402 * Test the method that reveals a range of hidden columns given the start
403 * column of the range
405 @Test(groups = { "Functional" })
406 public void testRevealHiddenColumns()
408 ColumnSelection colsel = new ColumnSelection();
409 HiddenColumns cs = new HiddenColumns();
410 cs.hideColumns(5, 8);
411 colsel.addElement(10);
412 cs.revealHiddenColumns(5, colsel);
413 // hidden columns list now null but getter returns empty list:
414 assertTrue(cs.getHiddenRegions().isEmpty());
415 // revealed columns are marked as selected (added to selection):
416 assertEquals("[10, 5, 6, 7, 8]", colsel.getSelected().toString());
418 // calling with a column other than the range start does nothing:
419 colsel = new ColumnSelection();
420 cs = new HiddenColumns();
421 cs.hideColumns(5, 8);
422 List<int[]> hidden = cs.getHiddenRegions();
423 cs.revealHiddenColumns(6, colsel);
424 assertSame(hidden, cs.getHiddenRegions());
425 assertTrue(colsel.getSelected().isEmpty());
428 @Test(groups = { "Functional" })
429 public void testRevealAllHiddenColumns()
431 HiddenColumns cs = new HiddenColumns();
432 ColumnSelection colsel = new ColumnSelection();
433 cs.hideColumns(5, 8);
434 cs.hideColumns(2, 3);
435 colsel.addElement(11);
436 colsel.addElement(1);
437 cs.revealAllHiddenColumns(colsel);
440 * revealing hidden columns adds them (in order) to the (unordered)
443 assertTrue(cs.getHiddenRegions().isEmpty());
444 assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]", colsel.getSelected()
448 @Test(groups = { "Functional" })
449 public void testIsVisible()
451 HiddenColumns cs = new HiddenColumns();
452 cs.hideColumns(2, 4);
453 cs.hideColumns(6, 7);
454 assertTrue(cs.isVisible(0));
455 assertTrue(cs.isVisible(-99));
456 assertTrue(cs.isVisible(1));
457 assertFalse(cs.isVisible(2));
458 assertFalse(cs.isVisible(3));
459 assertFalse(cs.isVisible(4));
460 assertTrue(cs.isVisible(5));
461 assertFalse(cs.isVisible(6));
462 assertFalse(cs.isVisible(7));
466 * Test for the case when a hidden range encloses more one already hidden
469 @Test(groups = { "Functional" })
470 public void testHideColumns_subsumingHidden()
473 * JAL-2370 bug scenario:
474 * two hidden ranges subsumed by a third
476 HiddenColumns cs = new HiddenColumns();
477 cs.hideColumns(49, 59);
478 cs.hideColumns(69, 79);
479 List<int[]> hidden = cs.getHiddenRegions();
480 assertEquals(2, hidden.size());
481 assertEquals("[49, 59]", Arrays.toString(hidden.get(0)));
482 assertEquals("[69, 79]", Arrays.toString(hidden.get(1)));
484 cs.hideColumns(48, 80);
485 hidden = cs.getHiddenRegions();
486 assertEquals(1, hidden.size());
487 assertEquals("[48, 80]", Arrays.toString(hidden.get(0)));
490 * another...joining hidden ranges
492 cs = new HiddenColumns();
493 cs.hideColumns(10, 20);
494 cs.hideColumns(30, 40);
495 cs.hideColumns(50, 60);
496 // hiding 21-49 should merge to one range
497 cs.hideColumns(21, 49);
498 hidden = cs.getHiddenRegions();
499 assertEquals(1, hidden.size());
500 assertEquals("[10, 60]", Arrays.toString(hidden.get(0)));
503 * another...left overlap, subsumption, right overlap,
504 * no overlap of existing hidden ranges
506 cs = new HiddenColumns();
507 cs.hideColumns(10, 20);
508 cs.hideColumns(10, 20);
509 cs.hideColumns(30, 35);
510 cs.hideColumns(40, 50);
511 cs.hideColumns(60, 70);
513 cs.hideColumns(15, 45);
514 hidden = cs.getHiddenRegions();
515 assertEquals(2, hidden.size());
516 assertEquals("[10, 50]", Arrays.toString(hidden.get(0)));
517 assertEquals("[60, 70]", Arrays.toString(hidden.get(1)));