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