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