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