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