JAL-2446 merged to spike branch
[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.BitSet;
33 import java.util.List;
34 import java.util.Random;
35
36 import org.testng.annotations.BeforeClass;
37 import org.testng.annotations.Test;
38
39 public class HiddenColumnsTest
40 {
41
42   @BeforeClass(alwaysRun = true)
43   public void setUpJvOptionPane()
44   {
45     JvOptionPane.setInteractiveMode(false);
46     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
47   }
48
49   /**
50    * Test the method which counts the number of hidden columns
51    */
52   @Test(groups = { "Functional" })
53   public void testGetSize()
54   {
55     HiddenColumns hidden = new HiddenColumns();
56     assertEquals(0, hidden.getSize());
57
58     hidden.hideColumns(3, 5);
59     assertEquals(3, hidden.getSize());
60
61     hidden.hideColumns(8, 8);
62     assertEquals(4, hidden.getSize());
63
64     hidden.hideColumns(9, 14);
65     assertEquals(10, hidden.getSize());
66
67     ColumnSelection cs = new ColumnSelection();
68     hidden.revealAllHiddenColumns(cs);
69     assertEquals(0, hidden.getSize());
70   }
71
72   /**
73    * Test the method that finds the visible column position of an alignment
74    * column, allowing for hidden columns.
75    */
76   @Test(groups = { "Functional" })
77   public void testFindColumnPosition()
78   {
79     HiddenColumns cs = new HiddenColumns();
80     assertEquals(5, cs.findColumnPosition(5));
81
82     // hiding column 6 makes no difference
83     cs.hideColumns(6, 6);
84     assertEquals(5, cs.findColumnPosition(5));
85
86     // hiding column 4 moves column 5 to column 4
87     cs.hideColumns(4, 4);
88     assertEquals(4, cs.findColumnPosition(5));
89
90     // hiding column 4 moves column 4 to position 3
91     assertEquals(3, cs.findColumnPosition(4));
92
93     // hiding columns 1 and 2 moves column 5 to column 2
94     cs.hideColumns(1, 2);
95     assertEquals(2, cs.findColumnPosition(5));
96
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);
103
104     // hiding columns 5-10 and 20-27 moves column 8 to column 4
105     assertEquals(4, cs2.findColumnPosition(8));
106
107     // and moves column 24 to 13
108     assertEquals(13, cs2.findColumnPosition(24));
109
110     // and moves column 28 to 14
111     assertEquals(14, cs2.findColumnPosition(28));
112
113     // and moves column 40 to 25
114     assertEquals(25, cs2.findColumnPosition(40));
115
116     // check when hidden columns start at 0 that the visible column
117     // is returned as 0
118     HiddenColumns cs3 = new HiddenColumns();
119     cs3.hideColumns(0, 4);
120     assertEquals(0, cs3.findColumnPosition(2));
121
122   }
123
124   /**
125    * Test the method that finds the visible column position a given distance
126    * before another column
127    */
128   @Test(groups = { "Functional" })
129   public void testFindColumnNToLeft()
130   {
131     HiddenColumns cs = new HiddenColumns();
132
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);
137
138     // 0 returns same position
139     pos = cs.subtractVisibleColumns(0, 10);
140     assertEquals(10, pos);
141
142     // overflow to left returns negative number
143     pos = cs.subtractVisibleColumns(3, 0);
144     assertEquals(-3, pos);
145
146     // test that with hidden columns to left of result column
147     // behaviour is the same as above
148     cs.hideColumns(1, 3);
149
150     // position n to left of provided position
151     pos = cs.subtractVisibleColumns(3, 10);
152     assertEquals(7, pos);
153
154     // 0 returns same position
155     pos = cs.subtractVisibleColumns(0, 10);
156     assertEquals(10, pos);
157
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);
162
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);
167
168     // repeat last 2 tests with no hidden columns to left of required position
169     ColumnSelection colsel = new ColumnSelection();
170     cs.revealAllHiddenColumns(colsel);
171
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);
176
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);
181
182   }
183
184   @Test(groups = { "Functional" })
185   public void testGetVisibleContigs()
186   {
187     HiddenColumns cs = new HiddenColumns();
188     cs.hideColumns(3, 6);
189     cs.hideColumns(8, 9);
190     cs.hideColumns(12, 12);
191
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));
195
196     visible = cs.getVisibleContigs(4, 14);
197     assertEquals("[7, 7, 10, 11, 13, 13]", Arrays.toString(visible));
198
199     visible = cs.getVisibleContigs(3, 10);
200     assertEquals("[7, 7]", Arrays.toString(visible));
201
202     visible = cs.getVisibleContigs(4, 6);
203     assertEquals("[]", Arrays.toString(visible));
204   }
205
206   @Test(groups = { "Functional" })
207   public void testEquals()
208   {
209     HiddenColumns cs = new HiddenColumns();
210     cs.hideColumns(5, 9);
211
212     // a different set of hidden columns
213     HiddenColumns cs2 = new HiddenColumns();
214
215     // with no hidden columns
216     assertFalse(cs.equals(cs2));
217     assertFalse(cs2.equals(cs));
218
219     // with hidden columns added in a different order
220     cs2.hideColumns(6, 9);
221     cs2.hideColumns(5, 8);
222
223     assertTrue(cs.equals(cs2));
224     assertTrue(cs.equals(cs));
225     assertTrue(cs2.equals(cs));
226     assertTrue(cs2.equals(cs2));
227   }
228
229   @Test(groups = "Functional")
230   public void testCopyConstructor()
231   {
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)));
236
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)));
243   }
244
245   /**
246    * Test the code used to locate the reference sequence ruler origin
247    */
248   @Test(groups = { "Functional" })
249   public void testLocateVisibleBoundsofSequence()
250   {
251     // create random alignment
252     AlignmentGenerator gen = new AlignmentGenerator(false);
253     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
254
255     HiddenColumns cs = al.getHiddenColumns();
256     ColumnSelection colsel = new ColumnSelection();
257
258     SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
259     assertEquals(2, seq.findIndex(seq.getStart()));
260
261     // no hidden columns
262     assertEquals(
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)));
268
269     // hidden column on gap after end of sequence - should not affect bounds
270     colsel.hideSelectedColumns(13, al.getHiddenColumns());
271     assertEquals(
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)));
277
278     cs.revealAllHiddenColumns(colsel);
279     // hidden column on gap before beginning of sequence - should vis bounds by
280     // one
281     colsel.hideSelectedColumns(0, al.getHiddenColumns());
282     assertEquals(
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)));
288
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);
293     assertEquals("-D",
294             cs.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]);
295     assertEquals(
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);
301
302     // hide whole sequence - should just get location of hidden region
303     // containing sequence
304     cs.hideColumns(1, 11);
305     assertEquals(
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)));
310
311   }
312
313   @Test(groups = { "Functional" })
314   public void testLocateVisibleBoundsPathologicals()
315   {
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));
321     assertEquals(
322             "G",
323             ""
324                     + al.getSequenceAt(0).getCharAt(
325                             cs.adjustForHiddenColumns(9)));
326
327   }
328
329   @Test(groups = { "Functional" })
330   public void testHideColumns()
331   {
332     // create random alignment
333     AlignmentGenerator gen = new AlignmentGenerator(false);
334     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
335
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)));
342
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)));
349
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)));
356
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());
361
362     cs.hideColumns(3, 6);
363     hidden = cs.getHiddenRegions();
364     int[] firstHiddenRange = hidden.get(0);
365     assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
366
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));
380
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)));
386
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)));
391
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)));
396
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)));
401   }
402
403   /**
404    * Test the method that reveals a range of hidden columns given the start
405    * column of the range
406    */
407   @Test(groups = { "Functional" })
408   public void testRevealHiddenColumns()
409   {
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());
419
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());
428   }
429
430   @Test(groups = { "Functional" })
431   public void testRevealAllHiddenColumns()
432   {
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);
440
441     /*
442      * revealing hidden columns adds them (in order) to the (unordered)
443      * selection list
444      */
445     assertTrue(cs.getHiddenRegions().isEmpty());
446     assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]", colsel.getSelected()
447             .toString());
448   }
449
450   @Test(groups = { "Functional" })
451   public void testIsVisible()
452   {
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));
465   }
466
467   /**
468    * Test for the case when a hidden range encloses more one already hidden
469    * range
470    */
471   @Test(groups = { "Functional" })
472   public void testHideColumns_subsumingHidden()
473   {
474     /*
475      * JAL-2370 bug scenario:
476      * two hidden ranges subsumed by a third
477      */
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)));
485
486     cs.hideColumns(48, 80);
487     hidden = cs.getHiddenRegions();
488     assertEquals(1, hidden.size());
489     assertEquals("[48, 80]", Arrays.toString(hidden.get(0)));
490
491     /*
492      * another...joining hidden ranges
493      */
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)));
503
504     /*
505      * another...left overlap, subsumption, right overlap,
506      * no overlap of existing hidden ranges
507      */
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);
514
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)));
520   }
521
522   @Test(groups = { "Functional" })
523   public void testHideBitset()
524   {
525     HiddenColumns cs;
526
527     BitSet one = new BitSet();
528
529     // one hidden range
530     one.set(1);
531     cs = new HiddenColumns();
532     cs.hideMarkedBits(one);
533     assertEquals(1, cs.getHiddenRegions().size());
534
535     one.set(2);
536     cs = new HiddenColumns();
537     cs.hideMarkedBits(one);
538     assertEquals(1, cs.getHiddenRegions().size());
539
540     one.set(3);
541     cs = new HiddenColumns();
542     cs.hideMarkedBits(one);
543     assertEquals(1, cs.getHiddenRegions().size());
544
545     // split
546     one.clear(2);
547     cs = new HiddenColumns();
548     cs.hideMarkedBits(one);
549     assertEquals(2, cs.getHiddenRegions().size());
550
551     assertEquals(0, cs.adjustForHiddenColumns(0));
552     assertEquals(2, cs.adjustForHiddenColumns(1));
553     assertEquals(4, cs.adjustForHiddenColumns(2));
554
555     // one again
556     one.clear(1);
557     cs = new HiddenColumns();
558     cs.hideMarkedBits(one);
559
560     assertEquals(1, cs.getHiddenRegions().size());
561
562     assertEquals(0, cs.adjustForHiddenColumns(0));
563     assertEquals(1, cs.adjustForHiddenColumns(1));
564     assertEquals(2, cs.adjustForHiddenColumns(2));
565     assertEquals(4, cs.adjustForHiddenColumns(3));
566   }
567
568   @Test(groups = { "Functional" })
569   public void testGetBitset()
570   {
571     BitSet toMark, fromMark;
572     long seed = -3241532;
573     Random number = new Random(seed);
574     for (int n = 0; n < 1000; n++)
575     {
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);
582
583       // see if we can recover bitfield
584       hc.markHiddenRegions(fromMark = new BitSet());
585       assertEquals(toMark, fromMark);
586     }
587   }
588 }