Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package jalview.datamodel

File HiddenSequencesTest.java

 

Code metrics

8
234
15
1
540
324
19
0.08
15.6
15
1.27

Classes

Class Line # Actions
HiddenSequencesTest 42 234 19
1.0100%
 

Contributing tests

This file is covered by 13 tests. .

Source view

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.AssertJUnit.assertEquals;
24    import static org.testng.AssertJUnit.assertFalse;
25    import static org.testng.AssertJUnit.assertNotNull;
26    import static org.testng.AssertJUnit.assertNotSame;
27    import static org.testng.AssertJUnit.assertNull;
28    import static org.testng.AssertJUnit.assertSame;
29    import static org.testng.AssertJUnit.assertTrue;
30    import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
31   
32    import jalview.gui.AlignViewport;
33    import jalview.gui.JvOptionPane;
34   
35    import java.util.List;
36   
37    import org.testng.annotations.BeforeClass;
38    import org.testng.annotations.BeforeTest;
39    import org.testng.annotations.Test;
40   
41    @Test(singleThreaded = true)
 
42    public class HiddenSequencesTest
43    {
44   
 
45  1 toggle @BeforeClass(alwaysRun = true)
46    public void setUpJvOptionPane()
47    {
48  1 JvOptionPane.setInteractiveMode(false);
49  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
50    }
51   
52    static int SEQ_COUNT = 25;
53   
54    SequenceI[] seqs;
55   
56    /**
57    * Set up an alignment of 10 sequences
58    */
 
59  4 toggle @BeforeTest(alwaysRun = true)
60    public void setUp()
61    {
62  4 seqs = new SequenceI[SEQ_COUNT];
63  104 for (int i = 0; i < SEQ_COUNT; i++)
64    {
65    // sequence lengths are 1, 2, ... 25
66  100 seqs[i] = new Sequence("Seq" + i,
67    "abcdefghijklmnopqrstuvwxy".substring(0, i + 1));
68    }
69    }
70   
71    /**
72    * Test the method that converts sequence alignment index to what it would be
73    * if all sequences were unhidden
74    */
 
75  1 toggle @Test(groups = "Functional")
76    public void testAdjustForHiddenSeqs()
77    {
78  1 AlignmentI al = new Alignment(seqs);
79  1 HiddenSequences hs = al.getHiddenSequences();
80  26 for (int i = 0; i < SEQ_COUNT; i++)
81    {
82  25 assertEquals(i, hs.adjustForHiddenSeqs(i));
83    }
84   
85    // hide seq1 and seq5 and seq6
86  1 hs.hideSequence(seqs[1]);
87  1 hs.hideSequence(seqs[5]);
88  1 hs.hideSequence(seqs[6]);
89   
90    /*
91    * alignment is now seq0/2/3/4/7/8/9
92    */
93  1 assertEquals(SEQ_COUNT - 3, al.getHeight());
94  1 assertEquals(0, hs.adjustForHiddenSeqs(0));
95  1 assertEquals(2, hs.adjustForHiddenSeqs(1));
96  1 assertEquals(3, hs.adjustForHiddenSeqs(2));
97  1 assertEquals(4, hs.adjustForHiddenSeqs(3));
98  1 assertEquals(7, hs.adjustForHiddenSeqs(4));
99  1 assertEquals(8, hs.adjustForHiddenSeqs(5));
100  1 assertEquals(9, hs.adjustForHiddenSeqs(6));
101    }
102   
103    /**
104    * Test the method that increments the internal array size if a sequence is
105    * added to the alignment (ugh this should not be exposed to the light of day)
106    */
 
107  1 toggle @Test(groups = "Functional")
108    public void testAdjustHeightSequenceAdded()
109    {
110  1 AlignmentI al = new Alignment(seqs);
111  1 assertEquals(SEQ_COUNT, al.getHeight());
112   
113  1 HiddenSequences hs = al.getHiddenSequences();
114    // initially does nothing
115  1 hs.adjustHeightSequenceAdded();
116  1 assertNull(hs.hiddenSequences);
117   
118    // hide one sequence
119  1 hs.hideSequence(seqs[3]);
120  1 assertEquals(1, hs.getSize());
121  1 assertEquals(SEQ_COUNT - 1, al.getHeight());
122  1 assertEquals(SEQ_COUNT, hs.hiddenSequences.length);
123   
124    /*
125    * add a sequence to the alignment
126    * - the safe way to call hs.adjustHeightSequenceAdded!
127    * (implementation depends on alignment height having
128    * been already updated for the added sequence)
129    */
130  1 al.addSequence(new Sequence("a", "b"));
131  1 assertEquals(1, hs.getSize());
132  1 assertEquals(SEQ_COUNT, al.getHeight());
133  1 assertEquals(SEQ_COUNT + 1, hs.hiddenSequences.length);
134    }
135   
136    /**
137    * Test the method that decrements the internal array size if a sequence is
138    * deleted from the alignment (ugh this should not be exposed to the light of
139    * day)
140    */
 
141  1 toggle @Test(groups = "Functional")
142    public void testAdjustHeightSequenceDeleted()
143    {
144  1 AlignmentI al = new Alignment(seqs);
145  1 assertEquals(SEQ_COUNT, al.getHeight());
146   
147  1 HiddenSequences hs = al.getHiddenSequences();
148    // initially does nothing
149  1 hs.adjustHeightSequenceAdded();
150  1 assertNull(hs.hiddenSequences);
151   
152    // hide two sequences
153  1 hs.hideSequence(seqs[3]);
154  1 hs.hideSequence(seqs[5]);
155  1 assertEquals(2, hs.getSize());
156  1 assertTrue(hs.isHidden(seqs[3]));
157  1 assertTrue(hs.isHidden(seqs[5]));
158  1 assertEquals(SEQ_COUNT - 2, al.getHeight());
159  1 assertEquals(SEQ_COUNT, hs.hiddenSequences.length);
160   
161    /*
162    * delete a visible sequence from the alignment
163    * - the safe way to call hs.adjustHeightSequenceDeleted!
164    * (implementation depends on alignment height having
165    * been already updated for the removed sequence)
166    */
167  1 al.deleteSequence(seqs[2]);
168  1 assertEquals(2, hs.getSize());
169    // the visible alignment is unchanged:
170  1 assertEquals(SEQ_COUNT - 3, al.getHeight());
171    // sequences array size has decremented:
172  1 assertEquals(SEQ_COUNT - 1, hs.hiddenSequences.length);
173    }
174   
175    /**
176    * Test the method that converts a 'full alignment' sequence index into the
177    * equivalent in the alignment with sequences hidden
178    */
 
179  1 toggle @Test(groups = "Functional")
180    public void testFindIndexWithoutHiddenSeqs()
181    {
182  1 AlignmentI al = new Alignment(seqs);
183  1 HiddenSequences hs = al.getHiddenSequences();
184  1 int height = al.getHeight();
185  26 for (int i = 0; i < height; i++)
186    {
187  25 assertEquals(i, hs.findIndexWithoutHiddenSeqs(i));
188    }
189   
190    // hide seq1 and seq5 and seq6
191  1 hs.hideSequence(seqs[1]);
192  1 hs.hideSequence(seqs[5]);
193  1 hs.hideSequence(seqs[6]);
194   
195    /*
196    * alignment is now seq0/2/3/4/7/8/9
197    */
198  1 assertEquals(height - 3, al.getHeight());
199  1 assertEquals(0, hs.findIndexWithoutHiddenSeqs(0));
200  1 assertEquals(0, hs.findIndexWithoutHiddenSeqs(1));
201  1 assertEquals(1, hs.findIndexWithoutHiddenSeqs(2));
202  1 assertEquals(2, hs.findIndexWithoutHiddenSeqs(3));
203  1 assertEquals(3, hs.findIndexWithoutHiddenSeqs(4));
204  1 assertEquals(3, hs.findIndexWithoutHiddenSeqs(5));
205  1 assertEquals(3, hs.findIndexWithoutHiddenSeqs(6));
206  1 assertEquals(4, hs.findIndexWithoutHiddenSeqs(7));
207  1 assertEquals(5, hs.findIndexWithoutHiddenSeqs(8));
208  1 assertEquals(6, hs.findIndexWithoutHiddenSeqs(9));
209   
210    /*
211    * hide first two sequences
212    */
213  1 hs.showAll(null);
214  1 hs.hideSequence(seqs[0]);
215  1 hs.hideSequence(seqs[1]);
216  1 assertEquals(-1, hs.findIndexWithoutHiddenSeqs(0));
217  1 assertEquals(-1, hs.findIndexWithoutHiddenSeqs(1));
218  24 for (int i = 2; i < height; i++)
219    {
220  23 assertEquals(i - 2, hs.findIndexWithoutHiddenSeqs(i));
221    }
222    }
223   
224    /**
225    * Test the method that finds the visible row position a given distance before
226    * another row
227    */
 
228  1 toggle @Test(groups = { "Functional" })
229    public void testFindIndexNFromRow()
230    {
231  1 AlignmentI al = new Alignment(seqs);
232  1 HiddenSequences hs = new HiddenSequences(al);
233   
234    // test that without hidden rows, findIndexNFromRow returns
235    // position n above provided position
236  1 int pos = hs.subtractVisibleRows(3, 10);
237  1 assertEquals(7, pos);
238   
239    // 0 returns same position
240  1 pos = hs.subtractVisibleRows(0, 10);
241  1 assertEquals(10, pos);
242   
243    // overflow to top returns negative number
244  1 pos = hs.subtractVisibleRows(3, 0);
245  1 assertEquals(-3, pos);
246   
247    // test that with hidden rows above result row
248    // behaviour is the same as above
249  1 hs.hideSequence(seqs[1]);
250  1 hs.hideSequence(seqs[2]);
251  1 hs.hideSequence(seqs[3]);
252   
253    // position n above provided position
254  1 pos = hs.subtractVisibleRows(3, 10);
255  1 assertEquals(7, pos);
256   
257    // 0 returns same position
258  1 pos = hs.subtractVisibleRows(0, 10);
259  1 assertEquals(10, pos);
260   
261    // test with one set of hidden rows between start and required position
262  1 hs.hideSequence(seqs[12]);
263  1 hs.hideSequence(seqs[13]);
264  1 hs.hideSequence(seqs[14]);
265  1 hs.hideSequence(seqs[15]);
266  1 pos = hs.subtractVisibleRows(8, 17);
267  1 assertEquals(5, pos);
268   
269    // test with two sets of hidden rows between start and required position
270  1 hs.hideSequence(seqs[20]);
271  1 hs.hideSequence(seqs[21]);
272  1 pos = hs.subtractVisibleRows(8, 23);
273  1 assertEquals(9, pos);
274   
275    // repeat last 2 tests with no hidden columns to left of required position
276  1 hs.showAll(null);
277   
278    // test with one set of hidden rows between start and required position
279  1 hs.hideSequence(seqs[12]);
280  1 hs.hideSequence(seqs[13]);
281  1 hs.hideSequence(seqs[14]);
282  1 hs.hideSequence(seqs[15]);
283  1 pos = hs.subtractVisibleRows(8, 17);
284  1 assertEquals(5, pos);
285   
286    // test with two sets of hidden rows between start and required position
287  1 hs.hideSequence(seqs[20]);
288  1 hs.hideSequence(seqs[21]);
289  1 pos = hs.subtractVisibleRows(8, 23);
290  1 assertEquals(9, pos);
291   
292    }
293   
294    /**
295    * Test the method that reconstructs (sort of) the full alignment including
296    * hidden sequences
297    */
 
298  1 toggle @Test(groups = "Functional")
299    public void testGetFullAlignment()
300    {
301  1 AlignmentI al = new Alignment(seqs);
302  1 assertArrayEquals(seqs, al.getSequencesArray());
303  1 al.setProperty("a", "b");
304  1 al.addAnnotation(new AlignmentAnnotation("ann", "label", 12f));
305  1 al.setSeqrep(seqs[4]);
306  1 SequenceGroup sg = new SequenceGroup();
307  1 sg.addSequence(seqs[8], false);
308  1 al.addGroup(sg);
309  1 ((Alignment) al).hasRNAStructure = true;
310   
311  1 HiddenSequences hs = al.getHiddenSequences();
312  1 AlignmentI al2 = hs.getFullAlignment();
313    // new alignment but with original sequences
314  1 assertNotSame(al, al2);
315  1 assertArrayEquals(al.getSequencesArray(), al2.getSequencesArray());
316   
317  1 hs.hideSequence(seqs[4]);
318  1 hs.hideSequence(seqs[9]);
319  1 al2 = hs.getFullAlignment();
320  1 assertNotSame(al, al2);
321  1 assertArrayEquals(seqs, al2.getSequencesArray());
322  1 assertNotNull(al2.getProperties());
323  1 assertSame(al.getProperties(), al2.getProperties());
324  1 assertNotNull(al2.getAlignmentAnnotation());
325  1 assertSame(al.getAlignmentAnnotation(), al2.getAlignmentAnnotation());
326  1 assertSame(seqs[4], al2.getSeqrep());
327  1 assertNotNull(al2.getGroups());
328  1 assertSame(al.getGroups(), al2.getGroups());
329  1 assertTrue(al2.hasRNAStructure());
330    }
331   
332    /**
333    * Test the method that returns the hidden sequence at a given index in the
334    * full alignment
335    *
336    * @return either the sequence (if hidden) or null (if not hidden)
337    */
 
338  1 toggle @Test(groups = "Functional")
339    public void testGetHiddenSequence()
340    {
341  1 AlignmentI al = new Alignment(seqs);
342  1 HiddenSequences hs = al.getHiddenSequences();
343  1 assertNull(hs.getHiddenSequence(0));
344  1 hs.hideSequence(seqs[3]);
345  1 assertSame(seqs[3], hs.getHiddenSequence(3));
346  1 assertNull(hs.getHiddenSequence(2));
347  1 assertNull(hs.getHiddenSequence(4));
348    }
349   
 
350  1 toggle @Test(groups = "Functional")
351    public void testGetSize()
352    {
353    }
354   
 
355  1 toggle @Test(groups = "Functional")
356    public void testGetWidth()
357    {
358  1 AlignmentI al = new Alignment(seqs);
359  1 HiddenSequences hs = al.getHiddenSequences();
360  1 assertEquals(0, hs.getWidth());
361  1 hs.hideSequence(seqs[6]);
362  1 hs.hideSequence(seqs[8]);
363  1 assertEquals(9, hs.getWidth());
364    }
365   
366    /**
367    * Test the method that adds a sequence to the hidden sequences and deletes it
368    * from the alignment, and its converse
369    */
 
370  1 toggle @Test(groups = "Functional")
371    public void testHideShowSequence()
372    {
373  1 AlignmentI al = new Alignment(seqs);
374  1 assertTrue(al.getSequences().contains(seqs[1]));
375  1 HiddenSequences hs = al.getHiddenSequences();
376  1 assertEquals(0, hs.getSize());
377  1 assertEquals(SEQ_COUNT, al.getHeight());
378   
379    /*
380    * hide the second sequence in the alignment
381    */
382  1 hs.hideSequence(seqs[1]);
383  1 assertFalse(hs.isHidden(seqs[0]));
384  1 assertTrue(hs.isHidden(seqs[1]));
385  1 assertFalse(al.getSequences().contains(seqs[1]));
386  1 assertEquals(1, hs.getSize());
387  1 assertEquals(SEQ_COUNT - 1, al.getHeight());
388  1 assertSame(seqs[2], al.getSequenceAt(1));
389   
390    /*
391    * hide what is now the second sequence in the alignment
392    */
393  1 hs.hideSequence(seqs[2]);
394  1 assertFalse(hs.isHidden(seqs[0]));
395  1 assertTrue(hs.isHidden(seqs[1]));
396  1 assertTrue(hs.isHidden(seqs[2]));
397  1 assertFalse(al.getSequences().contains(seqs[1]));
398  1 assertFalse(al.getSequences().contains(seqs[2]));
399  1 assertEquals(2, hs.getSize());
400  1 assertEquals(SEQ_COUNT - 2, al.getHeight());
401   
402    /*
403    * perform 'reveal' on what is now the second sequence in the alignment
404    * this should unhide the two sequences that precede it
405    */
406  1 List<SequenceI> revealed = hs.showSequence(1, null);
407  1 assertEquals(2, revealed.size());
408  1 assertTrue(revealed.contains(seqs[1]));
409  1 assertTrue(revealed.contains(seqs[2]));
410  1 assertEquals(0, hs.getSize());
411  1 assertEquals(SEQ_COUNT, al.getHeight());
412    }
413   
414    /**
415    * Test the method that adds a sequence to the hidden sequences and deletes it
416    * from the alignment, and its converse, where the first hidden sequences are
417    * at the bottom of the alignment (JAL-2437)
418    */
 
419  1 toggle @Test(groups = "Functional")
420    public void testHideShowLastSequences()
421    {
422  1 AlignmentI al = new Alignment(seqs);
423  1 assertTrue(al.getSequences().contains(seqs[1]));
424  1 HiddenSequences hs = al.getHiddenSequences();
425  1 assertEquals(0, hs.getSize());
426  1 assertEquals(SEQ_COUNT, al.getHeight());
427   
428    /*
429    * hide the last sequence in the alignment
430    */
431  1 hs.hideSequence(seqs[SEQ_COUNT - 1]);
432  1 assertFalse(hs.isHidden(seqs[SEQ_COUNT - 2]));
433  1 assertTrue(hs.isHidden(seqs[SEQ_COUNT - 1]));
434  1 assertFalse(al.getSequences().contains(seqs[SEQ_COUNT - 1]));
435  1 assertEquals(1, hs.getSize());
436  1 assertEquals(SEQ_COUNT - 1, al.getHeight());
437   
438    /*
439    * hide the third last sequence in the alignment
440    */
441  1 hs.hideSequence(seqs[SEQ_COUNT - 3]);
442  1 assertFalse(hs.isHidden(seqs[SEQ_COUNT - 2]));
443  1 assertTrue(hs.isHidden(seqs[SEQ_COUNT - 3]));
444  1 assertFalse(al.getSequences().contains(seqs[SEQ_COUNT - 3]));
445  1 assertEquals(2, hs.getSize());
446  1 assertEquals(SEQ_COUNT - 2, al.getHeight());
447   
448    /*
449    * reveal all the sequences, which should be reinstated in the same order as they started in
450    */
451  1 hs.showAll(null);
452  1 assertFalse(hs.isHidden(seqs[SEQ_COUNT - 3]));
453  1 assertFalse(hs.isHidden(seqs[SEQ_COUNT - 1]));
454  1 assertEquals(seqs[SEQ_COUNT - 3], al.getSequences().get(SEQ_COUNT - 3));
455  1 assertEquals(seqs[SEQ_COUNT - 2], al.getSequences().get(SEQ_COUNT - 2));
456  1 assertEquals(seqs[SEQ_COUNT - 1], al.getSequences().get(SEQ_COUNT - 1));
457  1 assertEquals(0, hs.getSize());
458  1 assertEquals(SEQ_COUNT, al.getHeight());
459    }
460   
 
461  1 toggle @Test(groups = "Functional")
462    public void testIsHidden()
463    {
464  1 AlignmentI al = new Alignment(seqs);
465  1 HiddenSequences hs = al.getHiddenSequences();
466  1 hs.hideSequence(seqs[7]);
467  1 hs.hideSequence(seqs[4]);
468  1 assertTrue(hs.isHidden(seqs[4]));
469  1 assertFalse(hs.isHidden(seqs[5]));
470  1 assertFalse(hs.isHidden(seqs[6]));
471  1 assertTrue(hs.isHidden(seqs[7]));
472  1 assertFalse(hs.isHidden(null));
473  1 assertFalse(hs.isHidden(new Sequence("", "")));
474    }
475   
476    /**
477    * Test hiding and unhiding a group with a representative sequence. The
478    * representative should be left visible when the group is hidden, and
479    * included in the selected group when it is unhidden.
480    */
 
481  1 toggle @Test(groups = "Functional")
482    public void testHideShowSequence_withHiddenRepSequence()
483    {
484  1 AlignmentI al = new Alignment(seqs);
485   
486    /*
487    * represent seqs 2-4 with seq3
488    * this hides seq2 and seq4 but not seq3
489    */
490  1 AlignViewport av = new AlignViewport(al);
491  1 SequenceGroup sg = new SequenceGroup();
492  1 sg.addSequence(seqs[1], false);
493  1 sg.addSequence(seqs[2], false);
494  1 sg.addSequence(seqs[3], false);
495  1 av.setSelectionGroup(sg);
496   
497    /*
498    * hiding group with reference sequence is done via AlignViewport
499    */
500  1 av.hideSequences(seqs[2], true);
501  1 HiddenSequences hs = al.getHiddenSequences();
502  1 assertEquals(2, hs.getSize());
503  1 assertTrue(hs.isHidden(seqs[1]));
504  1 assertFalse(hs.isHidden(seqs[2]));
505  1 assertTrue(hs.isHidden(seqs[3]));
506   
507    /*
508    * should now be no sequences selected in the alignment
509    */
510  1 assertNull(av.getSelectionGroup());
511   
512    /*
513    * visible alignment is now seq0/2/4/5/6/7/8/9
514    * 'reveal sequences' at the representative sequence (index = 1)
515    * this should unhide the one above i.e. seq1
516    * and return a selection list including seq2
517    *
518    * note have to call via AlignViewport to get the expected
519    * resulting sequence selection
520    */
521  1 av.showSequence(1);
522   
523    /*
524    * only seq3 is now hidden
525    */
526  1 assertEquals(1, hs.getSize());
527  1 assertTrue(hs.isHidden(seqs[3]));
528  1 assertEquals(SEQ_COUNT - 1, al.getHeight());
529  1 sg = av.getSelectionGroup();
530   
531    /*
532    * unhidden and representative sequence selected
533    * (this behaviour may change! JAL-2133)
534    */
535  1 assertEquals(2, sg.getSize());
536  1 assertTrue(sg.getSequences().contains(seqs[1]));
537  1 assertTrue(sg.getSequences().contains(seqs[2]));
538  1 assertFalse(sg.getSequences().contains(seqs[3]));
539    }
540    }