JAL=2674 Removed getHiddenColumnsCopy
[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.Assert.assertNull;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertFalse;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import jalview.analysis.AlignmentGenerator;
29 import jalview.gui.JvOptionPane;
30 import jalview.util.Comparison;
31
32 import java.util.Arrays;
33 import java.util.BitSet;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Random;
37
38 import org.testng.annotations.BeforeClass;
39 import org.testng.annotations.Test;
40
41 public class HiddenColumnsTest
42 {
43
44   @BeforeClass(alwaysRun = true)
45   public void setUpJvOptionPane()
46   {
47     JvOptionPane.setInteractiveMode(false);
48     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49   }
50
51   /**
52    * Test the method which counts the number of hidden columns
53    */
54   @Test(groups = { "Functional" })
55   public void testGetSize()
56   {
57     HiddenColumns hidden = new HiddenColumns();
58     assertEquals(0, hidden.getSize());
59
60     hidden.hideColumns(3, 5);
61     assertEquals(3, hidden.getSize());
62
63     hidden.hideColumns(8, 8);
64     assertEquals(4, hidden.getSize());
65
66     hidden.hideColumns(9, 14);
67     assertEquals(10, hidden.getSize());
68
69     ColumnSelection cs = new ColumnSelection();
70     hidden.revealAllHiddenColumns(cs);
71     assertEquals(0, hidden.getSize());
72   }
73
74   /**
75    * Test the method that finds the visible column position of an alignment
76    * column, allowing for hidden columns.
77    */
78   @Test(groups = { "Functional" })
79   public void testFindColumnPosition()
80   {
81     HiddenColumns cs = new HiddenColumns();
82     assertEquals(5, cs.findColumnPosition(5));
83
84     // hiding column 6 makes no difference
85     cs.hideColumns(6, 6);
86     assertEquals(5, cs.findColumnPosition(5));
87
88     // hiding column 4 moves column 5 to column 4
89     cs.hideColumns(4, 4);
90     assertEquals(4, cs.findColumnPosition(5));
91
92     // hiding column 4 moves column 4 to position 3
93     assertEquals(3, cs.findColumnPosition(4));
94
95     // hiding columns 1 and 2 moves column 5 to column 2
96     cs.hideColumns(1, 2);
97     assertEquals(2, cs.findColumnPosition(5));
98
99     // check with > 1 hidden column regions
100     // where some columns are in the hidden regions
101     HiddenColumns cs2 = new HiddenColumns();
102     cs2.hideColumns(5, 10);
103     cs2.hideColumns(20, 27);
104     cs2.hideColumns(40, 44);
105
106     // hiding columns 5-10 and 20-27 moves column 8 to column 4
107     assertEquals(4, cs2.findColumnPosition(8));
108
109     // and moves column 24 to 13
110     assertEquals(13, cs2.findColumnPosition(24));
111
112     // and moves column 28 to 14
113     assertEquals(14, cs2.findColumnPosition(28));
114
115     // and moves column 40 to 25
116     assertEquals(25, cs2.findColumnPosition(40));
117
118     // check when hidden columns start at 0 that the visible column
119     // is returned as 0
120     HiddenColumns cs3 = new HiddenColumns();
121     cs3.hideColumns(0, 4);
122     assertEquals(0, cs3.findColumnPosition(2));
123
124   }
125
126   /**
127    * Test the method that finds the visible column position a given distance
128    * before another column
129    */
130   @Test(groups = { "Functional" })
131   public void testFindColumnNToLeft()
132   {
133     HiddenColumns cs = new HiddenColumns();
134
135     // test that without hidden columns, findColumnNToLeft returns
136     // position n to left of provided position
137     long pos = cs.subtractVisibleColumns(3, 10);
138     assertEquals(7, pos);
139
140     // 0 returns same position
141     pos = cs.subtractVisibleColumns(0, 10);
142     assertEquals(10, pos);
143
144     // overflow to left returns negative number
145     pos = cs.subtractVisibleColumns(3, 0);
146     assertEquals(-3, pos);
147
148     // test that with hidden columns to left of result column
149     // behaviour is the same as above
150     cs.hideColumns(1, 3);
151
152     // position n to left of provided position
153     pos = cs.subtractVisibleColumns(3, 10);
154     assertEquals(7, pos);
155
156     // 0 returns same position
157     pos = cs.subtractVisibleColumns(0, 10);
158     assertEquals(10, pos);
159
160     // test with one set of hidden columns between start and required position
161     cs.hideColumns(12, 15);
162     pos = cs.subtractVisibleColumns(8, 17);
163     assertEquals(5, pos);
164
165     // test with two sets of hidden columns between start and required position
166     cs.hideColumns(20, 21);
167     pos = cs.subtractVisibleColumns(8, 23);
168     assertEquals(9, pos);
169
170     // repeat last 2 tests with no hidden columns to left of required position
171     ColumnSelection colsel = new ColumnSelection();
172     cs.revealAllHiddenColumns(colsel);
173
174     // test with one set of hidden columns between start and required position
175     cs.hideColumns(12, 15);
176     pos = cs.subtractVisibleColumns(8, 17);
177     assertEquals(5, pos);
178
179     // test with two sets of hidden columns between start and required position
180     cs.hideColumns(20, 21);
181     pos = cs.subtractVisibleColumns(8, 23);
182     assertEquals(9, pos);
183
184   }
185
186   @Test(groups = { "Functional" })
187   public void testGetVisibleContigs()
188   {
189     HiddenColumns cs = new HiddenColumns();
190
191     List<int[]> visible = cs.getVisibleContigs(3, 10);
192     assertEquals("[3, 9]", Arrays.toString(visible.get(0)));
193
194     cs.hideColumns(3, 6);
195     cs.hideColumns(8, 9);
196     cs.hideColumns(12, 12);
197
198     // Test both ends visible region
199
200     // start position is inclusive, end position exclusive
201     visible = cs.getVisibleContigs(1, 13);
202     assertEquals("[1, 2]", Arrays.toString(visible.get(0)));
203     assertEquals("[7, 7]", Arrays.toString(visible.get(1)));
204     assertEquals("[10, 11]", Arrays.toString(visible.get(2)));
205
206     // Test start hidden, end visible
207     visible = cs.getVisibleContigs(4, 14);
208     assertEquals("[7, 7]", Arrays.toString(visible.get(0)));
209     assertEquals("[10, 11]", Arrays.toString(visible.get(1)));
210     assertEquals("[13, 13]", Arrays.toString(visible.get(2)));
211
212     // Test start hidden, end hidden
213     visible = cs.getVisibleContigs(3, 10);
214     assertEquals("[7, 7]", Arrays.toString(visible.get(0)));
215
216     // Test start visible, end hidden
217     visible = cs.getVisibleContigs(0, 13);
218     assertEquals("[0, 2]", Arrays.toString(visible.get(0)));
219     assertEquals("[7, 7]", Arrays.toString(visible.get(1)));
220     assertEquals("[10, 11]", Arrays.toString(visible.get(2)));
221
222     // Test empty result
223     visible = cs.getVisibleContigs(4, 6);
224     assertEquals(0, visible.size());
225   }
226
227   @Test(groups = { "Functional" })
228   public void testEquals()
229   {
230     HiddenColumns cs = new HiddenColumns();
231     cs.hideColumns(5, 9);
232
233     // a different set of hidden columns
234     HiddenColumns cs2 = new HiddenColumns();
235
236     // with no hidden columns
237     assertFalse(cs.equals(cs2));
238     assertFalse(cs2.equals(cs));
239
240     // with hidden columns added in a different order
241     cs2.hideColumns(6, 9);
242     cs2.hideColumns(5, 8);
243
244     assertTrue(cs.equals(cs2));
245     assertTrue(cs.equals(cs));
246     assertTrue(cs2.equals(cs));
247     assertTrue(cs2.equals(cs2));
248   }
249
250   @Test(groups = "Functional")
251   public void testCopyConstructor()
252   {
253     HiddenColumns cs = new HiddenColumns();
254     cs.hideColumns(10, 11);
255     cs.hideColumns(5, 7);
256     Iterator<int[]> regions = cs.iterator();
257     assertEquals("[5, 7]",
258             Arrays.toString(regions.next()));
259
260     HiddenColumns cs2 = new HiddenColumns(cs);
261     regions = cs2.iterator();
262     assertTrue(cs2.hasHiddenColumns());
263     assertEquals(2, cs2.getNumberOfRegions());
264     // hidden columns are held in column order
265     assertEquals("[5, 7]",
266             Arrays.toString(regions.next()));
267     assertEquals("[10, 11]",
268             Arrays.toString(regions.next()));
269   }
270
271   @Test(groups = "Functional")
272   public void testCopyConstructor2()
273   {
274     HiddenColumns cs = new HiddenColumns();
275     cs.hideColumns(10, 11);
276     cs.hideColumns(5, 7);
277
278     HiddenColumns cs2 = new HiddenColumns(cs, 3, 9, 1);
279     assertTrue(cs2.hasHiddenColumns());
280     Iterator<int[]> regions = cs2.iterator();
281
282     // only [5,7] returned, offset by 1
283     assertEquals("[4, 6]",
284             Arrays.toString(regions.next()));
285     assertEquals(3, cs2.getSize());
286
287     cs2 = new HiddenColumns(cs, 8, 15, 4);
288     regions = cs2.iterator();
289     assertTrue(cs2.hasHiddenColumns());
290
291     // only [10,11] returned, offset by 4
292     assertEquals("[6, 7]",
293             Arrays.toString(regions.next()));
294     assertEquals(2, cs2.getSize());
295
296     cs2 = new HiddenColumns(cs, 6, 10, 4);
297     assertFalse(cs2.hasHiddenColumns());
298   }
299
300   /**
301    * Test the code used to locate the reference sequence ruler origin
302    */
303   @Test(groups = { "Functional" })
304   public void testLocateVisibleBoundsofSequence()
305   {
306     // create random alignment
307     AlignmentGenerator gen = new AlignmentGenerator(false);
308     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
309
310     HiddenColumns cs = al.getHiddenColumns();
311     ColumnSelection colsel = new ColumnSelection();
312
313     SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
314     assertEquals(2, seq.findIndex(seq.getStart()));
315
316     // no hidden columns
317     assertEquals(
318             Arrays.toString(new int[]
319     { seq.findIndex(seq.getStart()) - 1, seq.findIndex(seq.getStart()) - 1,
320         seq.findIndex(seq.getEnd()) - 1 }),
321             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
322
323     // hidden column on gap after end of sequence - should not affect bounds
324     colsel.hideSelectedColumns(13, al.getHiddenColumns());
325     assertEquals(
326             Arrays.toString(new int[]
327     { seq.findIndex(seq.getStart()) - 1, seq.findIndex(seq.getStart()) - 1,
328         seq.findIndex(seq.getEnd()) - 1 }),
329             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
330
331     cs.revealAllHiddenColumns(colsel);
332     // hidden column on gap before beginning of sequence - should vis bounds by
333     // one
334     colsel.hideSelectedColumns(0, al.getHiddenColumns());
335     assertEquals(
336             Arrays.toString(new int[]
337     { seq.findIndex(seq.getStart()) - 2, seq.findIndex(seq.getStart()) - 1,
338         seq.findIndex(seq.getEnd()) - 1 }),
339             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
340
341     cs.revealAllHiddenColumns(colsel);
342     // hide columns around most of sequence - leave one residue remaining
343     cs.hideColumns(1, 3);
344     cs.hideColumns(6, 11);
345     assertEquals("-D",
346             cs.getVisibleSequenceStrings(0, 5, new SequenceI[]
347     { seq })[0]);
348
349     assertEquals(
350             Arrays.toString(
351                     new int[]
352             { 1, seq.findIndex(seq.getStart()) - 1,
353         seq.findIndex(seq.getEnd()) - 1 }),
354             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
355     cs.revealAllHiddenColumns(colsel);
356
357     // hide whole sequence - should just get location of hidden region
358     // containing sequence
359     cs.hideColumns(1, 11);
360     assertEquals(
361             Arrays.toString(
362                     new int[]
363             { 0, seq.findIndex(seq.getStart()) - 1,
364         seq.findIndex(seq.getEnd()) - 1 }),
365             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
366
367     cs.revealAllHiddenColumns(colsel);
368     cs.hideColumns(0, 15);
369     assertEquals(Arrays
370             .toString(new int[]
371     { 0, seq.findIndex(seq.getStart()) - 1,
372         seq.findIndex(seq.getEnd()) - 1 }),
373             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
374
375     SequenceI seq2 = new Sequence("RefSeq2", "-------A-SD-ASD--E---");
376
377     cs.revealAllHiddenColumns(colsel);
378     cs.hideColumns(7, 17);
379     assertEquals(
380             Arrays.toString(
381                     new int[]
382             { 0, seq2.findIndex(seq2.getStart()) - 1,
383         seq2.findIndex(seq2.getEnd()) - 1 }),
384             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq2)));
385
386     cs.revealAllHiddenColumns(colsel);
387     cs.hideColumns(3, 17);
388     assertEquals(
389             Arrays.toString(
390                     new int[]
391             { 0, seq2.findIndex(seq2.getStart()) - 1,
392         seq2.findIndex(seq2.getEnd()) - 1 }),
393             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq2)));
394
395     cs.revealAllHiddenColumns(colsel);
396     cs.hideColumns(3, 19);
397     assertEquals(
398             Arrays.toString(
399                     new int[]
400             { 0, seq2.findIndex(seq2.getStart()) - 1,
401         seq2.findIndex(seq2.getEnd()) - 1 }),
402             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq2)));
403
404     cs.revealAllHiddenColumns(colsel);
405     cs.hideColumns(0, 0);
406     int[] test = cs.locateVisibleBoundsOfSequence(seq);
407     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
408             Arrays.toString(test));
409
410     cs.revealAllHiddenColumns(colsel);
411     cs.hideColumns(0, 1);
412     test = cs.locateVisibleBoundsOfSequence(seq);
413     assertEquals(Arrays.toString(new int[] { 1, 1, 11 }),
414             Arrays.toString(test));
415
416     cs.revealAllHiddenColumns(colsel);
417     cs.hideColumns(0, 2);
418     test = cs.locateVisibleBoundsOfSequence(seq);
419     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
420             Arrays.toString(test));
421
422     cs.revealAllHiddenColumns(colsel);
423     cs.hideColumns(1, 1);
424     test = cs.locateVisibleBoundsOfSequence(seq);
425     assertEquals(Arrays.toString(new int[] { 2, 1, 11 }),
426             Arrays.toString(test));
427
428     cs.revealAllHiddenColumns(colsel);
429     cs.hideColumns(1, 2);
430     test = cs.locateVisibleBoundsOfSequence(seq);
431     assertEquals(Arrays.toString(new int[] { 1, 1, 11 }),
432             Arrays.toString(test));
433
434     cs.revealAllHiddenColumns(colsel);
435     cs.hideColumns(1, 3);
436     test = cs.locateVisibleBoundsOfSequence(seq);
437     assertEquals(Arrays.toString(new int[] { 1, 1, 11 }),
438             Arrays.toString(test));
439
440     cs.revealAllHiddenColumns(colsel);
441     cs.hideColumns(0, 2);
442     cs.hideColumns(5, 6);
443     test = cs.locateVisibleBoundsOfSequence(seq);
444     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
445             Arrays.toString(test));
446
447     cs.revealAllHiddenColumns(colsel);
448     cs.hideColumns(0, 2);
449     cs.hideColumns(5, 6);
450     cs.hideColumns(9, 10);
451     test = cs.locateVisibleBoundsOfSequence(seq);
452     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
453             Arrays.toString(test));
454
455     cs.revealAllHiddenColumns(colsel);
456     cs.hideColumns(0, 2);
457     cs.hideColumns(7, 11);
458     test = cs.locateVisibleBoundsOfSequence(seq);
459     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
460             Arrays.toString(test));
461
462     cs.revealAllHiddenColumns(colsel);
463     cs.hideColumns(2, 4);
464     cs.hideColumns(7, 11);
465     test = cs.locateVisibleBoundsOfSequence(seq);
466     assertEquals(Arrays.toString(new int[] { 1, 1, 11 }),
467             Arrays.toString(test));
468
469     cs.revealAllHiddenColumns(colsel);
470     cs.hideColumns(2, 4);
471     cs.hideColumns(7, 12);
472     test = cs.locateVisibleBoundsOfSequence(seq);
473     assertEquals(Arrays.toString(new int[] { 1, 1, 11 }),
474             Arrays.toString(test));
475
476     cs.revealAllHiddenColumns(colsel);
477     cs.hideColumns(1, 11);
478     test = cs.locateVisibleBoundsOfSequence(seq);
479     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
480             Arrays.toString(test));
481
482     cs.revealAllHiddenColumns(colsel);
483     cs.hideColumns(0, 12);
484     test = cs.locateVisibleBoundsOfSequence(seq);
485     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
486             Arrays.toString(test));
487
488     cs.revealAllHiddenColumns(colsel);
489     cs.hideColumns(0, 4);
490     cs.hideColumns(6, 12);
491     test = cs.locateVisibleBoundsOfSequence(seq);
492     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
493             Arrays.toString(test));
494
495     cs.revealAllHiddenColumns(colsel);
496     cs.hideColumns(0, 1);
497     cs.hideColumns(3, 12);
498     test = cs.locateVisibleBoundsOfSequence(seq);
499     assertEquals(Arrays.toString(new int[] { 0, 1, 11 }),
500             Arrays.toString(test));
501
502     // These tests cover different behaviour to original
503     // locateVisibleBoundsOfSequence
504     // Previously first values of each were 3,9 and 6 respectively.
505     cs.revealAllHiddenColumns(colsel);
506     cs.hideColumns(3, 14);
507     cs.hideColumns(17, 19);
508     assertEquals(
509             Arrays.toString(
510                     new int[]
511             { 3, seq2.findIndex(seq2.getStart()) - 1,
512         seq2.findIndex(seq2.getEnd()) - 1 }),
513             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq2)));
514
515     cs.revealAllHiddenColumns(colsel);
516     cs.hideColumns(3, 7);
517     cs.hideColumns(9, 14);
518     cs.hideColumns(17, 19);
519     assertEquals(
520             Arrays.toString(
521                     new int[]
522             { 9, seq2.findIndex(seq2.getStart()) - 1,
523         seq2.findIndex(seq2.getEnd()) - 1 }),
524             Arrays.toString(cs.locateVisibleBoundsOfSequence(seq2)));
525
526     cs.revealAllHiddenColumns(colsel);
527     cs.hideColumns(0, 1);
528     cs.hideColumns(3, 4);
529     cs.hideColumns(6, 8);
530     cs.hideColumns(10, 12);
531     test = cs.locateVisibleBoundsOfSequence(seq);
532     assertEquals(Arrays.toString(new int[] { 6, 1, 11 }),
533             Arrays.toString(test));
534
535   }
536
537   @Test(groups = { "Functional" })
538   public void testLocateVisibleBoundsPathologicals()
539   {
540     // test some pathological cases we missed
541     AlignmentI al = new Alignment(
542             new SequenceI[]
543     { new Sequence("refseqGaptest", "KTDVTI----------NFI-----G----L") });
544     HiddenColumns cs = new HiddenColumns();
545     cs.hideInsertionsFor(al.getSequenceAt(0));
546     assertEquals("G", ""
547             + al.getSequenceAt(0).getCharAt(cs.adjustForHiddenColumns(9)));
548
549   }
550
551   @Test(groups = { "Functional" })
552   public void testHideColumns()
553   {
554     // create random alignment
555     AlignmentGenerator gen = new AlignmentGenerator(false);
556     AlignmentI al = gen.generate(50, 20, 123, 5, 5);
557
558     ColumnSelection colsel = new ColumnSelection();
559     HiddenColumns cs = al.getHiddenColumns();
560     colsel.hideSelectedColumns(5, al.getHiddenColumns());
561     Iterator<int[]> regions = cs.iterator();
562     assertEquals(1, cs.getNumberOfRegions());
563     assertEquals("[5, 5]", Arrays.toString(regions.next()));
564
565     colsel.hideSelectedColumns(3, al.getHiddenColumns());
566     regions = cs.iterator();
567     assertEquals(2, cs.getNumberOfRegions());
568     // two hidden ranges, in order:
569     assertEquals("[3, 3]", Arrays.toString(regions.next()));
570     assertEquals("[5, 5]", Arrays.toString(regions.next()));
571
572     // hiding column 4 expands [3, 3] to [3, 4]
573     // and merges to [5, 5] to make [3, 5]
574     colsel.hideSelectedColumns(4, al.getHiddenColumns());
575     regions = cs.iterator();
576     assertEquals(1, cs.getNumberOfRegions());
577     assertEquals("[3, 5]", Arrays.toString(regions.next()));
578
579     // clear hidden columns (note they are added to selected)
580     cs.revealAllHiddenColumns(colsel);
581     // it is now actually null but getter returns an empty list
582     assertEquals(0, cs.getNumberOfRegions());
583
584     cs.hideColumns(3, 6);
585     regions = cs.iterator();
586     int[] firstHiddenRange = regions.next();
587     assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
588
589     // adding a subrange of already hidden should do nothing
590     cs.hideColumns(4, 5);
591     regions = cs.iterator();
592     assertEquals(1, cs.getNumberOfRegions());
593     assertEquals("[3, 6]",
594             Arrays.toString(regions.next()));
595     cs.hideColumns(3, 5);
596     regions = cs.iterator();
597     assertEquals(1, cs.getNumberOfRegions());
598     assertEquals("[3, 6]",
599             Arrays.toString(regions.next()));
600     cs.hideColumns(4, 6);
601     regions = cs.iterator();
602     assertEquals(1, cs.getNumberOfRegions());
603     assertEquals("[3, 6]",
604             Arrays.toString(regions.next()));
605     cs.hideColumns(3, 6);
606     regions = cs.iterator();
607     assertEquals(1, cs.getNumberOfRegions());
608     assertEquals("[3, 6]",
609             Arrays.toString(regions.next()));
610
611     cs.revealAllHiddenColumns(colsel);
612     cs.hideColumns(2, 4);
613     regions = cs.iterator();
614     assertEquals(1, cs.getNumberOfRegions());
615     assertEquals("[2, 4]", Arrays.toString(regions.next()));
616
617     // extend contiguous with 2 positions overlap
618     cs.hideColumns(3, 5);
619     regions = cs.iterator();
620     assertEquals(1, cs.getNumberOfRegions());
621     assertEquals("[2, 5]", Arrays.toString(regions.next()));
622
623     // extend contiguous with 1 position overlap
624     cs.hideColumns(5, 6);
625     regions = cs.iterator();
626     assertEquals(1, cs.getNumberOfRegions());
627     assertEquals("[2, 6]", Arrays.toString(regions.next()));
628
629     // extend contiguous with overlap both ends:
630     cs.hideColumns(1, 7);
631     regions = cs.iterator();
632     assertEquals(1, cs.getNumberOfRegions());
633     assertEquals("[1, 7]", Arrays.toString(regions.next()));
634   }
635
636   /**
637    * Test the method that reveals a range of hidden columns given the start
638    * column of the range
639    */
640   @Test(groups = { "Functional" })
641   public void testRevealHiddenColumns()
642   {
643     ColumnSelection colsel = new ColumnSelection();
644     HiddenColumns cs = new HiddenColumns();
645     cs.hideColumns(5, 8);
646     colsel.addElement(10);
647     cs.revealHiddenColumns(5, colsel);
648
649     // hiddenColumns now empty
650     assertEquals(0, cs.getSize());
651
652     // revealed columns are marked as selected (added to selection):
653     assertEquals("[10, 5, 6, 7, 8]", colsel.getSelected().toString());
654
655     // calling with a column other than the range start does nothing:
656     colsel = new ColumnSelection();
657     cs = new HiddenColumns();
658     cs.hideColumns(5, 8);
659
660     int prevSize = cs.getSize();
661     cs.revealHiddenColumns(6, colsel);
662     assertEquals(prevSize, cs.getSize());
663     assertTrue(colsel.getSelected().isEmpty());
664   }
665
666   @Test(groups = { "Functional" })
667   public void testRevealAllHiddenColumns()
668   {
669     HiddenColumns hidden = new HiddenColumns();
670     ColumnSelection colsel = new ColumnSelection();
671     hidden.hideColumns(5, 8);
672     hidden.hideColumns(2, 3);
673     colsel.addElement(11);
674     colsel.addElement(1);
675     hidden.revealAllHiddenColumns(colsel);
676
677     /*
678      * revealing hidden columns adds them (in order) to the (unordered)
679      * selection list
680      */
681
682     // hiddenColumns now empty
683     assertEquals(0, hidden.getSize());
684
685     assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]",
686             colsel.getSelected().toString());
687   }
688
689   @Test(groups = { "Functional" })
690   public void testIsVisible()
691   {
692     HiddenColumns cs = new HiddenColumns();
693     cs.hideColumns(2, 4);
694     cs.hideColumns(6, 7);
695     assertTrue(cs.isVisible(0));
696     assertTrue(cs.isVisible(-99));
697     assertTrue(cs.isVisible(1));
698     assertFalse(cs.isVisible(2));
699     assertFalse(cs.isVisible(3));
700     assertFalse(cs.isVisible(4));
701     assertTrue(cs.isVisible(5));
702     assertFalse(cs.isVisible(6));
703     assertFalse(cs.isVisible(7));
704   }
705
706   /**
707    * Test for the case when a hidden range encloses more one already hidden
708    * range
709    */
710   @Test(groups = { "Functional" })
711   public void testHideColumns_subsumingHidden()
712   {
713     /*
714      * JAL-2370 bug scenario:
715      * two hidden ranges subsumed by a third
716      */
717     HiddenColumns cs = new HiddenColumns();
718     cs.hideColumns(49, 59);
719     cs.hideColumns(69, 79);
720     Iterator<int[]> regions = cs.iterator();
721     assertEquals(2, cs.getNumberOfRegions());
722     assertEquals("[49, 59]", Arrays.toString(regions.next()));
723     assertEquals("[69, 79]", Arrays.toString(regions.next()));
724
725     cs.hideColumns(48, 80);
726     regions = cs.iterator();
727     assertEquals(1, cs.getNumberOfRegions());
728     assertEquals("[48, 80]", Arrays.toString(regions.next()));
729
730     /*
731      * another...joining hidden ranges
732      */
733     cs = new HiddenColumns();
734     cs.hideColumns(10, 20);
735     cs.hideColumns(30, 40);
736     cs.hideColumns(50, 60);
737     // hiding 21-49 should merge to one range
738     cs.hideColumns(21, 49);
739     regions = cs.iterator();
740     assertEquals(1, cs.getNumberOfRegions());
741     assertEquals("[10, 60]", Arrays.toString(regions.next()));
742
743     /*
744      * another...left overlap, subsumption, right overlap,
745      * no overlap of existing hidden ranges
746      */
747     cs = new HiddenColumns();
748     cs.hideColumns(10, 20);
749     cs.hideColumns(10, 20);
750     cs.hideColumns(30, 35);
751     cs.hideColumns(40, 50);
752     cs.hideColumns(60, 70);
753
754     cs.hideColumns(15, 45);
755     regions = cs.iterator();
756     assertEquals(2, cs.getNumberOfRegions());
757     assertEquals("[10, 50]", Arrays.toString(regions.next()));
758     assertEquals("[60, 70]", Arrays.toString(regions.next()));
759   }
760
761   @Test(groups = { "Functional" })
762   public void testHideBitset()
763   {
764     HiddenColumns cs;
765
766     BitSet one = new BitSet();
767
768     // one hidden range
769     one.set(1);
770     cs = new HiddenColumns();
771     cs.hideMarkedBits(one);
772     assertEquals(1, cs.getNumberOfRegions());
773
774     one.set(2);
775     cs = new HiddenColumns();
776     cs.hideMarkedBits(one);
777     assertEquals(1, cs.getNumberOfRegions());
778
779     one.set(3);
780     cs = new HiddenColumns();
781     cs.hideMarkedBits(one);
782     assertEquals(1, cs.getNumberOfRegions());
783
784     // split
785     one.clear(2);
786     cs = new HiddenColumns();
787     cs.hideMarkedBits(one);
788     assertEquals(2, cs.getNumberOfRegions());
789
790     assertEquals(0, cs.adjustForHiddenColumns(0));
791     assertEquals(2, cs.adjustForHiddenColumns(1));
792     assertEquals(4, cs.adjustForHiddenColumns(2));
793
794     // one again
795     one.clear(1);
796     cs = new HiddenColumns();
797     cs.hideMarkedBits(one);
798
799     assertEquals(1, cs.getNumberOfRegions());
800
801     assertEquals(0, cs.adjustForHiddenColumns(0));
802     assertEquals(1, cs.adjustForHiddenColumns(1));
803     assertEquals(2, cs.adjustForHiddenColumns(2));
804     assertEquals(4, cs.adjustForHiddenColumns(3));
805   }
806
807   @Test(groups = { "Functional" })
808   public void testMarkHiddenRegions()
809   {
810     BitSet toMark, fromMark;
811     long seed = -3241532;
812     Random number = new Random(seed);
813     for (int n = 0; n < 1000; n++)
814     {
815       // create a random bitfield
816       toMark = BitSet
817               .valueOf(new long[]
818       { number.nextLong(), number.nextLong(), number.nextLong() });
819       toMark.set(n * number.nextInt(10), n * (25 + number.nextInt(25)));
820       HiddenColumns hc = new HiddenColumns();
821       hc.hideMarkedBits(toMark);
822
823       // see if we can recover bitfield
824       hc.markHiddenRegions(fromMark = new BitSet());
825       assertEquals(toMark, fromMark);
826     }
827   }
828
829   @Test(groups = { "Functional" })
830   public void testFindHiddenRegionPositions()
831   {
832     HiddenColumns hc = new HiddenColumns();
833
834     List<Integer> positions = hc.findHiddenRegionPositions(0, 20);
835     assertTrue(positions.isEmpty());
836
837     hc.hideColumns(3, 7);
838     hc.hideColumns(10, 10);
839     hc.hideColumns(14, 15);
840
841     positions = hc.findHiddenRegionPositions(0, 20);
842     assertEquals(3, positions.size());
843     assertEquals(3, positions.get(0).intValue());
844     assertEquals(5, positions.get(1).intValue());
845     assertEquals(8, positions.get(2).intValue());
846
847     positions = hc.findHiddenRegionPositions(7, 20);
848     assertEquals(2, positions.size());
849     assertEquals(5, positions.get(0).intValue());
850     assertEquals(8, positions.get(1).intValue());
851
852     positions = hc.findHiddenRegionPositions(11, 13);
853     assertEquals(0, positions.size());
854
855     positions = hc.findHiddenRegionPositions(7, 20);
856     assertEquals(2, positions.size());
857     assertEquals(5, positions.get(0).intValue());
858     assertEquals(8, positions.get(1).intValue());
859
860     positions = hc.findHiddenRegionPositions(0, 1);
861     assertEquals(0, positions.size());
862
863     positions = hc.findHiddenRegionPositions(17, 20);
864     assertEquals(0, positions.size());
865
866     positions = hc.findHiddenRegionPositions(10, 15);
867     assertEquals(2, positions.size());
868     assertEquals(5, positions.get(0).intValue());
869     assertEquals(8, positions.get(1).intValue());
870   }
871
872   @Test(groups = { "Functional" })
873   public void testRegionsToString()
874   {
875     HiddenColumns hc = new HiddenColumns();
876
877     String result = hc.regionsToString(",", "--");
878     assertEquals("", result);
879
880     hc.hideColumns(3, 7);
881     hc.hideColumns(10, 10);
882     hc.hideColumns(14, 15);
883
884     result = hc.regionsToString(",", "--");
885     assertEquals("3--7,10--10,14--15", result);
886   }
887
888   @Test(groups = "Functional")
889   public void testGetVisibleStartAndEndIndexTest()
890   {
891     Sequence seq = new Sequence("testSeq", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
892     AlignmentI align = new Alignment(new SequenceI[] { seq });
893     HiddenColumns hc = new HiddenColumns();
894
895     int[] startEnd = hc.getVisibleStartAndEndIndex(align.getWidth());
896     assertEquals(0, startEnd[0]);
897     assertEquals(25, startEnd[1]);
898
899     hc.hideColumns(0, 0);
900     startEnd = hc.getVisibleStartAndEndIndex(align.getWidth());
901     assertEquals(1, startEnd[0]);
902     assertEquals(25, startEnd[1]);
903
904     hc.hideColumns(6, 9);
905     hc.hideColumns(11, 12);
906     startEnd = hc.getVisibleStartAndEndIndex(align.getWidth());
907     assertEquals(1, startEnd[0]);
908     assertEquals(25, startEnd[1]);
909
910     hc.hideColumns(24, 25);
911     startEnd = hc.getVisibleStartAndEndIndex(align.getWidth());
912     System.out.println(startEnd[0] + " : " + startEnd[1]);
913     assertEquals(1, startEnd[0]);
914     assertEquals(23, startEnd[1]);
915   }
916
917   @Test(groups = "Functional")
918   public void testGetRegionWithEdgeAtRes()
919   {
920     HiddenColumns hc = new HiddenColumns();
921
922     int[] result = hc.getRegionWithEdgeAtRes(5);
923     assertNull(result);
924
925     hc.hideColumns(3, 7);
926     hc.hideColumns(10, 10);
927     hc.hideColumns(14, 15);
928
929     result = hc.getRegionWithEdgeAtRes(2);
930     assertEquals(3, result[0]);
931     assertEquals(7, result[1]);
932
933     result = hc.getRegionWithEdgeAtRes(5);
934     assertEquals(10, result[0]);
935     assertEquals(10, result[1]);
936
937     result = hc.getRegionWithEdgeAtRes(6);
938     assertNull(result);
939
940     result = hc.getRegionWithEdgeAtRes(0);
941     assertNull(result);
942
943     result = hc.getRegionWithEdgeAtRes(7);
944     assertEquals(14, result[0]);
945     assertEquals(15, result[1]);
946
947     result = hc.getRegionWithEdgeAtRes(8);
948     assertEquals(14, result[0]);
949     assertEquals(15, result[1]);
950   }
951
952   @Test(groups = "Functional")
953   public void testPropagateInsertions()
954   {
955     // create an alignment with no gaps - this will be the profile seq and other
956     // JPRED seqs
957     AlignmentGenerator gen = new AlignmentGenerator(false);
958     AlignmentI al = gen.generate(25, 10, 1234, 0, 0);
959
960     // get the profileseq
961     SequenceI profileseq = al.getSequenceAt(0);
962     SequenceI gappedseq = new Sequence(profileseq);
963     gappedseq.insertCharAt(5, al.getGapCharacter());
964     gappedseq.insertCharAt(6, al.getGapCharacter());
965     gappedseq.insertCharAt(7, al.getGapCharacter());
966     gappedseq.insertCharAt(8, al.getGapCharacter());
967
968     // create an alignment view with the gapped sequence
969     SequenceI[] seqs = new SequenceI[1];
970     seqs[0] = gappedseq;
971     AlignmentI newal = new Alignment(seqs);
972     HiddenColumns hidden = new HiddenColumns();
973     hidden.hideColumns(15, 17);
974
975     AlignmentView view = new AlignmentView(newal, hidden, null, true, false,
976             false);
977
978     // confirm that original contigs are as expected
979     List<int[]> visible = hidden.getVisibleContigs(0, 25);
980     assertEquals("[0, 14]", Arrays.toString(visible.get(0)));
981     assertEquals("[18, 24]", Arrays.toString(visible.get(1)));
982
983     // propagate insertions
984     HiddenColumns result = HiddenColumns.propagateInsertions(profileseq, al,
985             view);
986
987     // confirm that the contigs have changed to account for the gaps
988     visible = result.getVisibleContigs(0, 25);
989     assertEquals("[0, 10]", Arrays.toString(visible.get(0)));
990     assertEquals("[14, 24]", Arrays.toString(visible.get(1)));
991
992     // confirm the alignment has been changed so that the other sequences have
993     // gaps inserted where the columns are hidden
994     assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[10]));
995     assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[11]));
996     assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[12]));
997     assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[13]));
998     assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[14]));
999
1000   }
1001
1002   @Test(groups = "Functional")
1003   public void testPropagateInsertionsOverlap()
1004   {
1005     // test propagateInsertions where gaps and hiddenColumns overlap
1006
1007     // create an alignment with no gaps - this will be the profile seq and other
1008     // JPRED seqs
1009     AlignmentGenerator gen = new AlignmentGenerator(false);
1010     AlignmentI al = gen.generate(20, 10, 1234, 0, 0);
1011
1012     // get the profileseq
1013     SequenceI profileseq = al.getSequenceAt(0);
1014     SequenceI gappedseq = new Sequence(profileseq);
1015     gappedseq.insertCharAt(5, al.getGapCharacter());
1016     gappedseq.insertCharAt(6, al.getGapCharacter());
1017     gappedseq.insertCharAt(7, al.getGapCharacter());
1018     gappedseq.insertCharAt(8, al.getGapCharacter());
1019
1020     // create an alignment view with the gapped sequence
1021     SequenceI[] seqs = new SequenceI[1];
1022     seqs[0] = gappedseq;
1023     AlignmentI newal = new Alignment(seqs);
1024
1025     // hide columns so that some overlap with the gaps
1026     HiddenColumns hidden = new HiddenColumns();
1027     hidden.hideColumns(7, 10);
1028
1029     AlignmentView view = new AlignmentView(newal, hidden, null, true, false,
1030             false);
1031
1032     // confirm that original contigs are as expected
1033     List<int[]> visible = hidden.getVisibleContigs(0, 20);
1034     assertEquals("[0, 6]", Arrays.toString(visible.get(0)));
1035     assertEquals("[11, 19]", Arrays.toString(visible.get(1)));
1036
1037     // propagate insertions
1038     HiddenColumns result = HiddenColumns.propagateInsertions(profileseq, al,
1039             view);
1040
1041     // confirm that the contigs have changed to account for the gaps
1042     visible = result.getVisibleContigs(0, 20);
1043     assertEquals("[0, 4]", Arrays.toString(visible.get(0)));
1044     assertEquals("[7, 19]", Arrays.toString(visible.get(1)));
1045
1046     // confirm the alignment has been changed so that the other sequences have
1047     // gaps inserted where the columns are hidden
1048     assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[4]));
1049     assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[5]));
1050     assertTrue(Comparison.isGap(al.getSequenceAt(1).getSequence()[6]));
1051     assertFalse(Comparison.isGap(al.getSequenceAt(1).getSequence()[7]));
1052   }
1053
1054   @Test(groups = "Functional")
1055   public void testHasHiddenColumns()
1056   {
1057     HiddenColumns h = new HiddenColumns();
1058
1059     // new HiddenColumns2 has no hidden cols
1060     assertFalse(h.hasHiddenColumns());
1061
1062     // some columns hidden, returns true
1063     h.hideColumns(5, 10);
1064     assertTrue(h.hasHiddenColumns());
1065
1066     // reveal columns, no hidden cols again
1067     ColumnSelection sel = new ColumnSelection();
1068     h.revealAllHiddenColumns(sel);
1069     assertFalse(h.hasHiddenColumns());
1070   }
1071
1072   @Test(groups = "Functional")
1073   public void testHasManyHiddenColumns()
1074   {
1075     HiddenColumns h = new HiddenColumns();
1076
1077     // new HiddenColumns2 has no hidden cols
1078     assertFalse(h.hasManyHiddenColumns());
1079
1080     // one set of columns hidden, returns false
1081     h.hideColumns(5, 10);
1082     assertFalse(h.hasManyHiddenColumns());
1083
1084     // two sets hidden, returns true
1085     h.hideColumns(15, 17);
1086     assertTrue(h.hasManyHiddenColumns());
1087
1088     // back to one block, asserts false
1089     h.hideColumns(11, 14);
1090     assertFalse(h.hasManyHiddenColumns());
1091   }
1092
1093   @Test(groups = "Functional")
1094   public void testAdjustForHiddenColumns()
1095   {
1096     HiddenColumns h = new HiddenColumns();
1097     // returns input value when there are no hidden columns
1098     assertEquals(10, h.adjustForHiddenColumns(10));
1099
1100     h.hideColumns(20, 30);
1101     assertEquals(10, h.adjustForHiddenColumns(10));
1102     assertEquals(20 + 11, h.adjustForHiddenColumns(20));
1103     assertEquals(35 + 11, h.adjustForHiddenColumns(35));
1104
1105     h.hideColumns(5, 7);
1106     assertEquals(10 + 3, h.adjustForHiddenColumns(10));
1107     assertEquals(20 + 14, h.adjustForHiddenColumns(20));
1108     assertEquals(35 + 14, h.adjustForHiddenColumns(35));
1109
1110     ColumnSelection sel = new ColumnSelection();
1111     h.revealAllHiddenColumns(sel);
1112     h.hideColumns(0, 1);
1113     assertEquals(4, h.adjustForHiddenColumns(2));
1114   }
1115
1116   @Test(groups = "Functional")
1117   public void testGetHiddenBoundaryLeft()
1118   {
1119     HiddenColumns h = new HiddenColumns();
1120
1121     // returns same value if no hidden cols
1122     assertEquals(3, h.getHiddenBoundaryLeft(3));
1123
1124     h.hideColumns(5, 10);
1125     assertEquals(10, h.getHiddenBoundaryLeft(15));
1126     assertEquals(3, h.getHiddenBoundaryLeft(3));
1127     assertEquals(7, h.getHiddenBoundaryLeft(7));
1128
1129     h.hideColumns(15, 20);
1130     assertEquals(10, h.getHiddenBoundaryLeft(15));
1131     assertEquals(20, h.getHiddenBoundaryLeft(21));
1132   }
1133
1134   @Test(groups = "Functional")
1135   public void testGetHiddenBoundaryRight()
1136   {
1137     HiddenColumns h = new HiddenColumns();
1138
1139     // returns same value if no hidden cols
1140     assertEquals(3, h.getHiddenBoundaryRight(3));
1141
1142     h.hideColumns(5, 10);
1143     assertEquals(5, h.getHiddenBoundaryRight(3));
1144     assertEquals(15, h.getHiddenBoundaryRight(15));
1145     assertEquals(7, h.getHiddenBoundaryRight(7));
1146
1147     h.hideColumns(15, 20);
1148     assertEquals(15, h.getHiddenBoundaryRight(7));
1149     assertEquals(15, h.getHiddenBoundaryRight(14));
1150   }
1151
1152   @Test(groups = "Functional")
1153   public void testIterator()
1154   {
1155     HiddenColumns h = new HiddenColumns();
1156     Iterator<int[]> result = h.iterator();
1157     assertFalse(result.hasNext());
1158
1159     h.hideColumns(5, 10);
1160     result = h.iterator();
1161     int[] next = result.next();
1162     assertEquals(5, next[0]);
1163     assertEquals(10, next[1]);
1164     assertFalse(result.hasNext());
1165
1166     h.hideColumns(22, 23);
1167     result = h.iterator();
1168     next = result.next();
1169     assertEquals(5, next[0]);
1170     assertEquals(10, next[1]);
1171     next = result.next();
1172     assertEquals(22, next[0]);
1173     assertEquals(23, next[1]);
1174     assertFalse(result.hasNext());
1175
1176     // test for only one hidden region at start of alignment
1177     ColumnSelection sel = new ColumnSelection();
1178     h.revealAllHiddenColumns(sel);
1179     h.hideColumns(0, 1);
1180     result = h.iterator();
1181     next = result.next();
1182     assertEquals(0, next[0]);
1183     assertEquals(1, next[1]);
1184     assertFalse(result.hasNext());
1185   }
1186
1187   @Test(groups = "Functional")
1188   public void testGetVisibleSequenceStrings()
1189   {
1190     HiddenColumns h = new HiddenColumns();
1191     SequenceI seq1 = new Sequence("TEST1", "GALMFWKQESPVICYHRNDT");
1192     SequenceI seq2 = new Sequence("TEST2", "VICYHRNDTGA");
1193     SequenceI[] seqs = new SequenceI[2];
1194     seqs[0] = seq1;
1195     seqs[1] = seq2;
1196     String[] result = h.getVisibleSequenceStrings(5, 10, seqs);
1197     assertEquals(2, result.length);
1198     assertEquals("WKQES", result[0]);
1199     assertEquals("RNDTG", result[1]);
1200
1201     h.hideColumns(6, 8);
1202     result = h.getVisibleSequenceStrings(5, 10, seqs);
1203     assertEquals(2, result.length);
1204     assertEquals("WS", result[0]);
1205     assertEquals("RG", result[1]);
1206
1207     SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
1208     ColumnSelection sel = new ColumnSelection();
1209     h.revealAllHiddenColumns(sel);
1210     h.hideColumns(1, 3);
1211     h.hideColumns(6, 11);
1212     assertEquals("-D",
1213             h.getVisibleSequenceStrings(0, 5, new SequenceI[]
1214     { seq })[0]);
1215   }
1216
1217   @Test(groups = "Functional")
1218   public void testHideInsertionsFor()
1219   {
1220     HiddenColumns h = new HiddenColumns();
1221     HiddenColumns h2 = new HiddenColumns();
1222     SequenceI seq1 = new Sequence("TEST1", "GAL---MFW-KQESPVICY--HRNDT");
1223     SequenceI seq2 = new Sequence("TEST1", "GALMFWKQESPVICYHRNDT");
1224
1225     h.hideInsertionsFor(seq2);
1226     assertTrue(h.equals(h2));
1227
1228     h.hideInsertionsFor(seq1);
1229     h2.hideColumns(3, 5);
1230     h2.hideColumns(9, 9);
1231     h2.hideColumns(19, 20);
1232     assertTrue(h.equals(h2));
1233   }
1234
1235   @Test(groups = "Functional")
1236   public void testHideMarkedBits()
1237   {
1238     HiddenColumns h = new HiddenColumns();
1239     HiddenColumns h2 = new HiddenColumns();
1240
1241     BitSet tohide = new BitSet(21);
1242     h.hideMarkedBits(tohide);
1243     assertTrue(h.equals(h2));
1244
1245     // NB in hideMarkedBits, the last bit is not set to hidden
1246     tohide.set(3, 6);
1247     tohide.set(9);
1248     tohide.set(19, 21);
1249     h.hideMarkedBits(tohide);
1250
1251     h2.hideColumns(3, 5);
1252     h2.hideColumns(9, 9);
1253     h2.hideColumns(19, 20);
1254     assertTrue(h.equals(h2));
1255   }
1256
1257   @Test(groups = "Functional")
1258   public void testMakeVisibleAnnotation()
1259   {
1260     HiddenColumns h = new HiddenColumns();
1261     Annotation[] anns = new Annotation[] { null, null, new Annotation(1),
1262         new Annotation(2), new Annotation(3), null, null, new Annotation(4),
1263         new Annotation(5), new Annotation(6), new Annotation(7),
1264         new Annotation(8) };
1265     AlignmentAnnotation ann = new AlignmentAnnotation("an", "some an",
1266             anns);
1267
1268     // without hidden cols, just truncates
1269     h.makeVisibleAnnotation(3, 5, ann);
1270     assertEquals(3, ann.annotations.length);
1271     assertEquals(2.0f, ann.annotations[0].value);
1272     assertEquals(3.0f, ann.annotations[1].value);
1273     assertNull(ann.annotations[2]);
1274
1275     anns = new Annotation[] { null, null, new Annotation(1),
1276         new Annotation(2), new Annotation(3), null, null, new Annotation(4),
1277         new Annotation(5), new Annotation(6), new Annotation(7),
1278         new Annotation(8) };
1279     ann = new AlignmentAnnotation("an", "some an", anns);
1280     h.hideColumns(4, 7);
1281     h.makeVisibleAnnotation(1, 9, ann);
1282     assertEquals(5, ann.annotations.length);
1283     assertNull(ann.annotations[0]);
1284     assertEquals(1.0f, ann.annotations[1].value);
1285     assertEquals(2.0f, ann.annotations[2].value);
1286     assertEquals(5.0f, ann.annotations[3].value);
1287     assertEquals(6.0f, ann.annotations[4].value);
1288
1289     anns = new Annotation[] { null, null, new Annotation(1),
1290         new Annotation(2), new Annotation(3), null, null, new Annotation(4),
1291         new Annotation(5), new Annotation(6), new Annotation(7),
1292         new Annotation(8) };
1293     ann = new AlignmentAnnotation("an", "some an", anns);
1294     h.hideColumns(1, 2);
1295     h.makeVisibleAnnotation(1, 9, ann);
1296     assertEquals(3, ann.annotations.length);
1297     assertEquals(2.0f, ann.annotations[0].value);
1298     assertEquals(5.0f, ann.annotations[1].value);
1299     assertEquals(6.0f, ann.annotations[2].value);
1300   }
1301
1302   @Test(groups = "Functional")
1303   public void testSubtractVisibleColumns()
1304   {
1305     HiddenColumns h = new HiddenColumns();
1306     int result = h.subtractVisibleColumns(1, 10);
1307     assertEquals(9, result);
1308
1309     h.hideColumns(7, 9);
1310     result = h.subtractVisibleColumns(4, 10);
1311     assertEquals(3, result);
1312
1313     h.hideColumns(14, 15);
1314     result = h.subtractVisibleColumns(4, 10);
1315     assertEquals(3, result);
1316
1317     result = h.subtractVisibleColumns(10, 17);
1318     assertEquals(2, result);
1319
1320     result = h.subtractVisibleColumns(1, 7);
1321     assertEquals(5, result);
1322
1323     result = h.subtractVisibleColumns(1, 8);
1324     assertEquals(5, result);
1325
1326     result = h.subtractVisibleColumns(3, 15);
1327     assertEquals(10, result);
1328
1329     ColumnSelection sel = new ColumnSelection();
1330     h.revealAllHiddenColumns(sel);
1331     h.hideColumns(0, 30);
1332     result = h.subtractVisibleColumns(31, 0);
1333     assertEquals(-31, result);
1334   }
1335
1336   @Test(groups = "Functional")
1337   public void testBoundedIterator()
1338   {
1339     HiddenColumns h = new HiddenColumns();
1340     Iterator<int[]> it = h.getBoundedIterator(0, 10);
1341
1342     // no hidden columns = nothing to iterate over
1343     assertFalse(it.hasNext());
1344
1345     // [start,end] contains all hidden columns
1346     // all regions are returned
1347     h.hideColumns(3, 10);
1348     h.hideColumns(14, 16);
1349     it = h.getBoundedIterator(0, 20);
1350     assertTrue(it.hasNext());
1351     int[] next = it.next();
1352     assertEquals(3, next[0]);
1353     assertEquals(10, next[1]);
1354     next = it.next();
1355     assertEquals(14, next[0]);
1356     assertEquals(16, next[1]);
1357     assertFalse(it.hasNext());
1358
1359     // [start,end] overlaps a region
1360     // 1 region returned
1361     it = h.getBoundedIterator(5, 7);
1362     assertTrue(it.hasNext());
1363     next = it.next();
1364     assertEquals(3, next[0]);
1365     assertEquals(10, next[1]);
1366     assertFalse(it.hasNext());
1367
1368     // [start,end] fully contains 1 region and start of last
1369     // - 2 regions returned
1370     it = h.getBoundedIterator(3, 15);
1371     assertTrue(it.hasNext());
1372     next = it.next();
1373     assertEquals(3, next[0]);
1374     assertEquals(10, next[1]);
1375     next = it.next();
1376     assertEquals(14, next[0]);
1377     assertEquals(16, next[1]);
1378     assertFalse(it.hasNext());
1379
1380     // [start,end] contains end of first region and whole of last region
1381     // - 2 regions returned
1382     it = h.getBoundedIterator(4, 20);
1383     assertTrue(it.hasNext());
1384     next = it.next();
1385     assertEquals(3, next[0]);
1386     assertEquals(10, next[1]);
1387     next = it.next();
1388     assertEquals(14, next[0]);
1389     assertEquals(16, next[1]);
1390     assertFalse(it.hasNext());
1391   }
1392
1393   @Test(groups = "Functional")
1394   public void testBoundedStartIterator()
1395   {
1396     HiddenColumns h = new HiddenColumns();
1397     Iterator<Integer> it = h.getBoundedStartIterator(0, 10);
1398
1399     // no hidden columns = nothing to iterate over
1400     assertFalse(it.hasNext());
1401
1402     // [start,end] contains all hidden columns
1403     // all regions are returned
1404     h.hideColumns(3, 10);
1405     h.hideColumns(14, 16);
1406     it = h.getBoundedStartIterator(0, 20);
1407     assertTrue(it.hasNext());
1408     int next = it.next();
1409     assertEquals(3, next);
1410     next = it.next();
1411     assertEquals(6, next);
1412     assertFalse(it.hasNext());
1413
1414     // [start,end] does not contain a start of a region
1415     // no regions to iterate over
1416     it = h.getBoundedStartIterator(4, 5);
1417     assertFalse(it.hasNext());
1418
1419     // [start,end] fully contains 1 region and start of last
1420     // - 2 regions returned
1421     it = h.getBoundedStartIterator(3, 7);
1422     assertTrue(it.hasNext());
1423     next = it.next();
1424     assertEquals(3, next);
1425     next = it.next();
1426     assertEquals(6, next);
1427     assertFalse(it.hasNext());
1428
1429     // [start,end] contains whole of last region
1430     // - 1 region returned
1431     it = h.getBoundedStartIterator(4, 20);
1432     assertTrue(it.hasNext());
1433     next = it.next();
1434     assertEquals(6, next);
1435     assertFalse(it.hasNext());
1436   }
1437 }