2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.datamodel;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertSame;
26 import static org.testng.AssertJUnit.assertTrue;
28 import java.util.Arrays;
29 import java.util.List;
31 import org.testng.annotations.Test;
33 public class ColumnSelectionTest
36 @Test(groups = { "Functional" })
37 public void testAddElement()
39 ColumnSelection cs = new ColumnSelection();
43 cs.addElement(5); // ignored
44 List<Integer> sel = cs.getSelected();
45 assertEquals("[2, 5, 3]", sel.toString());
49 * Test the remove method - in particular to verify that remove(int i) removes
50 * the element whose value is i, _NOT_ the i'th element.
52 @Test(groups = { "Functional" })
53 public void testRemoveElement()
55 ColumnSelection cs = new ColumnSelection();
59 // removing elements not in the list has no effect
62 List<Integer> sel = cs.getSelected();
63 assertEquals(2, sel.size());
64 assertEquals(new Integer(2), sel.get(0));
65 assertEquals(new Integer(5), sel.get(1));
67 // removing an element in the list removes it
69 assertEquals(1, sel.size());
70 assertEquals(new Integer(5), sel.get(0));
74 * Test the method that finds the visible column position of an alignment
75 * column, allowing for hidden columns.
77 @Test(groups = { "Functional" })
78 public void testFindColumnPosition()
80 ColumnSelection cs = new ColumnSelection();
81 assertEquals(5, cs.findColumnPosition(5));
83 // hiding column 6 makes no difference
85 assertEquals(5, cs.findColumnPosition(5));
87 // hiding column 4 moves column 5 to column 4
89 assertEquals(4, cs.findColumnPosition(5));
91 // hiding columns 1 and 2 moves column 5 to column 2
93 assertEquals(2, cs.findColumnPosition(5));
97 * Test the code used to locate the reference sequence ruler origin
99 @Test(groups = { "Functional" })
100 public void testLocateVisibleBoundsofSequence()
102 ColumnSelection cs = new ColumnSelection();
103 SequenceI seq = new Sequence("RefSeq", "-A-SD-ASD--E---");
104 assertEquals(2, seq.findIndex(seq.getStart()));
108 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
109 seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
110 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
111 seq.findIndex(seq.getEnd()) - 1 }),
112 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
114 // hidden column on gap after end of sequence - should not affect bounds
117 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 1,
118 seq.findIndex(seq.getEnd()) - 1, seq.getStart(),
119 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
120 seq.findIndex(seq.getEnd()) - 1 }),
121 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
123 cs.revealAllHiddenColumns();
124 // hidden column on gap before beginning of sequence - should vis bounds by
128 Arrays.toString(new int[] { seq.findIndex(seq.getStart()) - 2,
129 seq.findIndex(seq.getEnd()) - 2, seq.getStart(),
130 seq.getEnd(), seq.findIndex(seq.getStart()) - 1,
131 seq.findIndex(seq.getEnd()) - 1 }),
132 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
134 cs.revealAllHiddenColumns();
135 // hide columns around most of sequence - leave one residue remaining
136 cs.hideColumns(1, 3);
137 cs.hideColumns(6, 11);
139 cs.getVisibleSequenceStrings(0, 5, new SequenceI[] { seq })[0]);
141 Arrays.toString(new int[] { 1, 1, 3, 3,
142 seq.findIndex(seq.getStart()) - 1,
143 seq.findIndex(seq.getEnd()) - 1 }),
144 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
145 cs.revealAllHiddenColumns();
147 // hide whole sequence - should just get location of hidden region
148 // containing sequence
149 cs.hideColumns(1, 11);
151 Arrays.toString(new int[] { 0, 1, 0, 0,
152 seq.findIndex(seq.getStart()) - 1,
153 seq.findIndex(seq.getEnd()) - 1 }),
154 Arrays.toString(cs.locateVisibleBoundsOfSequence(seq)));
158 @Test(groups = { "Functional" })
159 public void testHideColumns()
161 ColumnSelection cs = new ColumnSelection();
163 List<int[]> hidden = cs.getHiddenColumns();
164 assertEquals(1, hidden.size());
165 assertEquals("[5, 5]", Arrays.toString(hidden.get(0)));
168 assertEquals(2, hidden.size());
169 // two hidden ranges, in order:
170 assertSame(hidden, cs.getHiddenColumns());
171 assertEquals("[3, 3]", Arrays.toString(hidden.get(0)));
172 assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
174 // hiding column 4 expands [3, 3] to [3, 4]
175 // not fancy enough to coalesce this into [3, 5] though
177 hidden = cs.getHiddenColumns();
178 assertEquals(2, hidden.size());
179 assertEquals("[3, 4]", Arrays.toString(hidden.get(0)));
180 assertEquals("[5, 5]", Arrays.toString(hidden.get(1)));
182 // clear hidden columns (note they are added to selected)
183 cs.revealAllHiddenColumns();
184 // it is now actually null but getter returns an empty list
185 assertTrue(cs.getHiddenColumns().isEmpty());
187 cs.hideColumns(3, 6);
188 hidden = cs.getHiddenColumns();
189 int[] firstHiddenRange = hidden.get(0);
190 assertEquals("[3, 6]", Arrays.toString(firstHiddenRange));
192 // adding a subrange of already hidden should do nothing
193 cs.hideColumns(4, 5);
194 assertEquals(1, hidden.size());
195 assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
196 cs.hideColumns(3, 5);
197 assertEquals(1, hidden.size());
198 assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
199 cs.hideColumns(4, 6);
200 assertEquals(1, hidden.size());
201 assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
202 cs.hideColumns(3, 6);
203 assertEquals(1, hidden.size());
204 assertSame(firstHiddenRange, cs.getHiddenColumns().get(0));
206 cs.revealAllHiddenColumns();
207 cs.hideColumns(2, 4);
208 hidden = cs.getHiddenColumns();
209 assertEquals(1, hidden.size());
210 assertEquals("[2, 4]", Arrays.toString(hidden.get(0)));
212 // extend contiguous with 2 positions overlap
213 cs.hideColumns(3, 5);
214 assertEquals(1, hidden.size());
215 assertEquals("[2, 5]", Arrays.toString(hidden.get(0)));
217 // extend contiguous with 1 position overlap
218 cs.hideColumns(5, 6);
219 assertEquals(1, hidden.size());
220 assertEquals("[2, 6]", Arrays.toString(hidden.get(0)));
222 // extend contiguous with overlap both ends:
223 cs.hideColumns(1, 7);
224 assertEquals(1, hidden.size());
225 assertEquals("[1, 7]", Arrays.toString(hidden.get(0)));
229 * Test the method that hides a specified column including any adjacent
230 * selected columns. This is a convenience method for the case where multiple
231 * column regions are selected and then hidden using menu option View | Hide |
234 @Test(groups = { "Functional" })
235 public void testHideColumns_withSelection()
237 ColumnSelection cs = new ColumnSelection();
238 // select columns 4-6
242 // hide column 5 (and adjacent):
245 List<int[]> hidden = cs.getHiddenColumns();
246 assertEquals(1, hidden.size());
247 assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
248 // none now selected:
249 assertTrue(cs.getSelected().isEmpty());
251 // repeat, hiding column 4 (5 and 6)
252 cs = new ColumnSelection();
257 hidden = cs.getHiddenColumns();
258 assertEquals(1, hidden.size());
259 assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
260 assertTrue(cs.getSelected().isEmpty());
262 // repeat, hiding column (4, 5 and) 6
263 cs = new ColumnSelection();
268 hidden = cs.getHiddenColumns();
269 assertEquals(1, hidden.size());
270 assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
271 assertTrue(cs.getSelected().isEmpty());
273 // repeat, with _only_ adjacent columns selected
274 cs = new ColumnSelection();
278 hidden = cs.getHiddenColumns();
279 assertEquals(1, hidden.size());
280 assertEquals("[4, 6]", Arrays.toString(hidden.get(0)));
281 assertTrue(cs.getSelected().isEmpty());
285 * Test the method that hides all (possibly disjoint) selected column ranges
287 @Test(groups = { "Functional" })
288 public void testHideSelectedColumns()
290 ColumnSelection cs = new ColumnSelection();
291 int[] sel = { 2, 3, 4, 7, 8, 9, 20, 21, 22 };
296 cs.hideColumns(15, 18);
298 cs.hideSelectedColumns();
299 assertTrue(cs.getSelected().isEmpty());
300 List<int[]> hidden = cs.getHiddenColumns();
301 assertEquals(4, hidden.size());
302 assertEquals("[2, 4]", Arrays.toString(hidden.get(0)));
303 assertEquals("[7, 9]", Arrays.toString(hidden.get(1)));
304 assertEquals("[15, 18]", Arrays.toString(hidden.get(2)));
305 assertEquals("[20, 22]", Arrays.toString(hidden.get(3)));
309 * Test the method that gets runs of selected columns ordered by column. If
310 * this fails, HideSelectedColumns may also fail
312 @Test(groups = { "Functional" })
313 public void testgetSelectedRanges()
315 ColumnSelection cs = new ColumnSelection();
316 int[] sel = { 2, 3, 4, 7, 8, 9, 20, 21, 22 };
322 range = cs.getSelectedRanges();
323 assertEquals(3, range.size());
324 assertEquals("[2, 4]", Arrays.toString(range.get(0)));
325 assertEquals("[7, 9]", Arrays.toString(range.get(1)));
326 assertEquals("[20, 22]", Arrays.toString(range.get(2)));
329 range = cs.getSelectedRanges();
330 assertEquals(3, range.size());
331 assertEquals("[0, 4]", Arrays.toString(range.get(0)));
335 * Test the method that reveals a range of hidden columns given the start
336 * column of the range
338 @Test(groups = { "Functional" })
339 public void testRevealHiddenColumns()
341 ColumnSelection cs = new ColumnSelection();
342 cs.hideColumns(5, 8);
344 cs.revealHiddenColumns(5);
345 // hidden columns list now null but getter returns empty list:
346 assertTrue(cs.getHiddenColumns().isEmpty());
347 // revealed columns are marked as selected (added to selection):
348 assertEquals("[10, 5, 6, 7, 8]", cs.getSelected().toString());
350 // calling with a column other than the range start does nothing:
351 cs = new ColumnSelection();
352 cs.hideColumns(5, 8);
353 List<int[]> hidden = cs.getHiddenColumns();
354 cs.revealHiddenColumns(6);
355 assertSame(hidden, cs.getHiddenColumns());
356 assertTrue(cs.getSelected().isEmpty());
359 @Test(groups = { "Functional" })
360 public void testRevealAllHiddenColumns()
362 ColumnSelection cs = new ColumnSelection();
363 cs.hideColumns(5, 8);
364 cs.hideColumns(2, 3);
367 cs.revealAllHiddenColumns();
370 * revealing hidden columns adds them (in order) to the (unordered)
373 assertTrue(cs.getHiddenColumns().isEmpty());
374 assertEquals("[11, 1, 2, 3, 5, 6, 7, 8]", cs.getSelected().toString());
377 @Test(groups = { "Functional" })
378 public void testIsVisible()
380 ColumnSelection cs = new ColumnSelection();
381 cs.hideColumns(2, 4);
382 cs.hideColumns(6, 7);
383 assertTrue(cs.isVisible(0));
384 assertTrue(cs.isVisible(-99));
385 assertTrue(cs.isVisible(1));
386 assertFalse(cs.isVisible(2));
387 assertFalse(cs.isVisible(3));
388 assertFalse(cs.isVisible(4));
389 assertTrue(cs.isVisible(5));
390 assertFalse(cs.isVisible(6));
391 assertFalse(cs.isVisible(7));
394 @Test(groups = { "Functional" })
395 public void testGetVisibleContigs()
397 ColumnSelection cs = new ColumnSelection();
398 cs.hideColumns(3, 6);
399 cs.hideColumns(8, 9);
400 cs.hideColumns(12, 12);
402 // start position is inclusive, end position exclusive:
403 int[] visible = cs.getVisibleContigs(1, 13);
404 assertEquals("[1, 2, 7, 7, 10, 11]", Arrays.toString(visible));
406 visible = cs.getVisibleContigs(4, 14);
407 assertEquals("[7, 7, 10, 11, 13, 13]", Arrays.toString(visible));
409 visible = cs.getVisibleContigs(3, 10);
410 assertEquals("[7, 7]", Arrays.toString(visible));
412 visible = cs.getVisibleContigs(4, 6);
413 assertEquals("[]", Arrays.toString(visible));
416 @Test(groups = { "Functional" })
417 public void testInvertColumnSelection()
419 ColumnSelection cs = new ColumnSelection();
423 cs.hideColumns(3, 3);
424 cs.hideColumns(6, 6);
426 // invert selection from start (inclusive) to end (exclusive)
427 // hidden columns are _not_ changed
428 cs.invertColumnSelection(2, 9);
429 assertEquals("[2, 5, 7]", cs.getSelected().toString());
431 cs.invertColumnSelection(1, 9);
432 assertEquals("[1, 4, 8]", cs.getSelected().toString());
435 @Test(groups = { "Functional" })
436 public void testMaxColumnSelection()
438 ColumnSelection cs = new ColumnSelection();
442 assertEquals(513, cs.getMax());
443 cs.removeElement(513);
444 assertEquals(1, cs.getMax());
446 assertEquals(0, cs.getMax());
449 assertEquals(513, cs.getMax());
453 @Test(groups = { "Functional" })
454 public void testMinColumnSelection()
456 ColumnSelection cs = new ColumnSelection();
460 assertEquals(0, cs.getMin());
462 assertEquals(1, cs.getMin());
464 assertEquals(0, cs.getMin());