JAL-2388 Working hidden regions hide/show in desktop
[jalview.git] / test / jalview / datamodel / HiddenColumnsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.datamodel;
22
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;
27
28 import jalview.analysis.AlignmentGenerator;
29 import jalview.gui.JvOptionPane;
30
31 import java.util.Arrays;
32 import java.util.List;
33
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 public class HiddenColumnsTest
38 {
39
40   @BeforeClass(alwaysRun = true)
41   public void setUpJvOptionPane()
42   {
43     JvOptionPane.setInteractiveMode(false);
44     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
45   }
46
47   /**
48    * Test the method which counts the number of hidden columns
49    */
50   @Test(groups = { "Functional" })
51   public void testGetSize()
52   {
53     HiddenColumns hidden = new HiddenColumns();
54     assertEquals(0, hidden.getSize());
55
56     hidden.hideColumns(3, 5);
57     assertEquals(3, hidden.getSize());
58
59     hidden.hideColumns(8, 8);
60     assertEquals(4, hidden.getSize());
61
62     hidden.hideColumns(9, 14);
63     assertEquals(10, hidden.getSize());
64
65     ColumnSelection cs = new ColumnSelection();
66     hidden.revealAllHiddenColumns(cs);
67     assertEquals(0, hidden.getSize());
68   }
69
70   /**
71    * Test the method that finds the visible column position of an alignment
72    * column, allowing for hidden columns.
73    */
74   @Test(groups = { "Functional" })
75   public void testFindColumnPosition()
76   {
77     HiddenColumns cs = new HiddenColumns();
78     assertEquals(5, cs.findColumnPosition(5));
79
80     // hiding column 6 makes no difference
81     cs.hideColumns(6, 6);
82     assertEquals(5, cs.findColumnPosition(5));
83
84     // hiding column 4 moves column 5 to column 4
85     cs.hideColumns(4, 4);
86     assertEquals(4, cs.findColumnPosition(5));
87
88     // hiding column 4 moves column 4 to position 3
89     assertEquals(3, cs.findColumnPosition(4));
90
91     // hiding columns 1 and 2 moves column 5 to column 2
92     cs.hideColumns(1, 2);
93     assertEquals(2, cs.findColumnPosition(5));
94
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);
101
102     // hiding columns 5-10 and 20-27 moves column 8 to column 4
103     assertEquals(4, cs2.findColumnPosition(8));
104
105     // and moves column 24 to 13
106     assertEquals(13, cs2.findColumnPosition(24));
107
108     // and moves column 28 to 14
109     assertEquals(14, cs2.findColumnPosition(28));
110
111     // and moves column 40 to 25
112     assertEquals(25, cs2.findColumnPosition(40));
113
114     // check when hidden columns start at 0 that the visible column
115     // is returned as 0
116     HiddenColumns cs3 = new HiddenColumns();
117     cs3.hideColumns(0, 4);
118     assertEquals(0, cs3.findColumnPosition(2));
119
120   }
121
122   /**
123    * Test the method that finds the visible column position a given distance
124    * before another column
125    */
126   @Test(groups = { "Functional" })
127   public void testFindColumnNToLeft()
128   {
129     HiddenColumns cs = new HiddenColumns();
130
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);
135
136     // 0 returns same position
137     pos = cs.subtractVisibleColumns(0, 10);
138     assertEquals(10, pos);
139
140     // overflow to left returns negative number
141     pos = cs.subtractVisibleColumns(3, 0);
142     assertEquals(-3, pos);
143
144     // test that with hidden columns to left of result column
145     // behaviour is the same as above
146     cs.hideColumns(1, 3);
147
148     // position n to left of provided position
149     pos = cs.subtractVisibleColumns(3, 10);
150     assertEquals(7, pos);
151
152     // 0 returns same position
153     pos = cs.subtractVisibleColumns(0, 10);
154     assertEquals(10, pos);
155
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);
160
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);
165
166     // repeat last 2 tests with no hidden columns to left of required position
167     ColumnSelection colsel = new ColumnSelection();
168     cs.revealAllHiddenColumns(colsel);
169
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);
174
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);
179
180   }
181
182   @Test(groups = { "Functional" })
183   public void testGetVisibleContigs()
184   {
185     HiddenColumns cs = new HiddenColumns();
186     cs.hideColumns(3, 6);
187     cs.hideColumns(8, 9);
188     cs.hideColumns(12, 12);
189
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));
193
194     visible = cs.getVisibleContigs(4, 14);
195     assertEquals("[7, 7, 10, 11, 13, 13]", Arrays.toString(visible));
196
197     visible = cs.getVisibleContigs(3, 10);
198     assertEquals("[7, 7]", Arrays.toString(visible));
199
200     visible = cs.getVisibleContigs(4, 6);
201     assertEquals("[]", Arrays.toString(visible));
202   }
203
204   @Test(groups = { "Functional" })
205   public void testEquals()
206   {
207     HiddenColumns cs = new HiddenColumns();
208     cs.hideColumns(5, 9);
209
210     // a different set of hidden columns
211     HiddenColumns cs2 = new HiddenColumns();
212
213     // with no hidden columns
214     assertFalse(cs.equals(cs2));
215     assertFalse(cs2.equals(cs));
216
217     // with hidden columns added in a different order
218     cs2.hideColumns(6, 9);
219     cs2.hideColumns(5, 8);
220
221     assertTrue(cs.equals(cs2));
222     assertTrue(cs.equals(cs));
223     assertTrue(cs2.equals(cs));
224     assertTrue(cs2.equals(cs2));
225   }
226
227   @Test(groups = "Functional")
228   public void testCopyConstructor()
229   {
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)));
234
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)));
241   }
242
243   /**
244    * Test the code used to locate the reference sequence ruler origin
245    */
246   @Test(groups = { "Functional" })
247   public void testLocateVisibleBoundsofSequence()
248   {
249     // create random alignment
250     AlignmentGenerator gen = new AlignmentGenerator(false);
251     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
252
253     HiddenColumns cs = al.getHiddenColumns();
254     ColumnSelection colsel = new ColumnSelection();
255
256     SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
257     assertEquals(2, seq.findIndex(seq.getStart()));
258
259     // no hidden columns
260     assertEquals(
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)));
266
267     // hidden column on gap after end of sequence - should not affect bounds
268     colsel.hideSelectedColumns(13, al.getHiddenColumns());
269     assertEquals(
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)));
275
276     cs.revealAllHiddenColumns(colsel);
277     // hidden column on gap before beginning of sequence - should vis bounds by
278     // one
279     colsel.hideSelectedColumns(0, al.getHiddenColumns());
280     assertEquals(
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)));
286
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);
291     assertEquals("-D",
292             cs.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]);
293     assertEquals(
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);
299
300     // hide whole sequence - should just get location of hidden region
301     // containing sequence
302     cs.hideColumns(1, 11);
303     assertEquals(
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)));
308
309   }
310
311   @Test(groups = { "Functional" })
312   public void testLocateVisibleBoundsPathologicals()
313   {
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));
319     assertEquals(
320             "G",
321             ""
322                     + al.getSequenceAt(0).getCharAt(
323                             cs.adjustForHiddenColumns(9)));
324
325   }
326
327   @Test(groups = { "Functional" })
328   public void testHideColumns()
329   {
330     // create random alignment
331     AlignmentGenerator gen = new AlignmentGenerator(false);
332     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
333
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)));
340
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)));
347
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)));
354
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());
359
360     cs.hideColumns(3, 6);
361     hidden = cs.getHiddenRegions();
362     int[] firstHiddenRange = hidden.get(0);
363     assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
364
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));
378
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)));
384
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)));
389
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)));
394
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)));
399   }
400
401   /**
402    * Test the method that reveals a range of hidden columns given the start
403    * column of the range
404    */
405   @Test(groups = { "Functional" })
406   public void testRevealHiddenColumns()
407   {
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());
417
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());
426   }
427
428   @Test(groups = { "Functional" })
429   public void testRevealAllHiddenColumns()
430   {
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);
438
439     /*
440      * revealing hidden columns adds them (in order) to the (unordered)
441      * selection list
442      */
443     assertTrue(cs.getHiddenRegions().isEmpty());
444     assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]", colsel.getSelected()
445             .toString());
446   }
447
448   @Test(groups = { "Functional" })
449   public void testIsVisible()
450   {
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));
463   }
464
465   /**
466    * Test for the case when a hidden range encloses more one already hidden
467    * range
468    */
469   @Test(groups = { "Functional" })
470   public void testHideColumns_subsumingHidden()
471   {
472     /*
473      * JAL-2370 bug scenario:
474      * two hidden ranges subsumed by a third
475      */
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)));
483
484     cs.hideColumns(48, 80);
485     hidden = cs.getHiddenRegions();
486     assertEquals(1, hidden.size());
487     assertEquals("[48, 80]", Arrays.toString(hidden.get(0)));
488
489     /*
490      * another...joining hidden ranges
491      */
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)));
501
502     /*
503      * another...left overlap, subsumption, right overlap,
504      * no overlap of existing hidden ranges
505      */
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);
512
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)));
518   }
519
520 }