JAL-1601 Test getVisibleCongigMapFor
[jalview.git] / test / jalview / datamodel / AlignmentViewTest.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.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.contains;
25 import org.hamcrest.Matchers;
26 import static org.testng.Assert.assertEquals;
27
28 import java.util.Arrays;
29 import java.util.List;
30
31 import jalview.gui.AlignFrame;
32 import jalview.gui.JvOptionPane;
33 import jalview.io.DataSourceType;
34 import jalview.io.FileLoader;
35 import jalview.viewmodel.AlignmentViewport;
36
37 import org.testng.annotations.BeforeClass;
38 import org.testng.annotations.Test;
39
40
41 public class AlignmentViewTest
42 {
43
44   @BeforeClass(alwaysRun = true)
45   public void setUpJvOptionPane()
46   {
47     JvOptionPane.setInteractiveMode(false);
48     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
49   }
50
51   @Test(groups = { "Functional" })
52   public void testGetVisibleAlignmentGapChar()
53   {
54     SeqCigar ss = new SeqCigar(new Sequence("One", "A..CDE"));
55     CigarArray ca = new CigarArray(new CigarSimple[] { ss });
56     AlignmentView av = new AlignmentView(ca);
57     String dots = av.getSequenceStrings('.')[0];
58     assertEquals(dots, "A..CDE");
59     String dollars = av.getSequenceStrings('$')[0];
60     assertEquals(dollars, "A$$CDE");
61     assertEquals(av.getVisibleAlignment('$').getSequenceAt(0)
62             .getSequenceAsString(), "A$$CDE");
63   }
64
65   @Test
66   public void testGetVisibleContigMapFor()
67   {
68     AlignFrame af = new FileLoader()
69         .LoadFileWaitTillLoaded(">seq\nAAA-AAAA---AA--\n",
70             DataSourceType.PASTE);
71     var av = af.getViewport();
72     var seq = av.getAlignment().getSequenceAt(0);
73     var gapMap = seq.gapMap();
74     assertThat(Arrays.stream(gapMap).boxed().toArray(Integer[]::new),
75         Matchers.arrayContaining(0, 1, 2, 4, 5, 6, 7, 11, 12));
76
77     av.hideColumns(0, 1);
78     av.hideColumns(6, 7);
79     AlignmentView view = av.getAlignmentView(true);
80     var delMap = view.getVisibleContigMapFor(gapMap);
81     assertThat(Arrays.stream(delMap).boxed().toArray(Integer[]::new),
82         Matchers.arrayContaining(2, 3, 4, 7, 8));
83   }
84
85   @Test(groups = { "Functional" })
86   public void testGetVisibleContigs()
87   {
88     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
89             ">s1\n0123456789\n", DataSourceType.PASTE);
90     AlignmentViewport av = af.getViewport();
91     AlignmentView view = av.getAlignmentView(true);
92
93     /*
94      * verify getVisibleContigs returns inclusive [start, end] ranges
95      * 
96      * no columns hidden
97      */
98     int[] contigs = view.getVisibleContigs();
99     assertEquals(contigs, new int[] { 0, 9 });
100
101     /*
102      * hide 3 internal columns
103      */
104     av.hideColumns(5, 7);
105     // the old AlignmentView is now stale!
106     contigs = view.getVisibleContigs();
107     assertEquals(contigs, new int[] { 0, 9 });
108     // get a fresh AlignmentView
109     view = av.getAlignmentView(true);
110     contigs = view.getVisibleContigs();
111     assertEquals(contigs, new int[] { 0, 4, 8, 9 });
112
113     // hide first 2 columns
114     av.hideColumns(0, 1);
115     view = av.getAlignmentView(true);
116     contigs = view.getVisibleContigs();
117     assertEquals(contigs, new int[] { 2, 4, 8, 9 });
118
119     // hide last column
120     av.hideColumns(9, 9);
121     view = av.getAlignmentView(true);
122     contigs = view.getVisibleContigs();
123     assertEquals(contigs, new int[] { 2, 4, 8, 8 });
124
125     // unhide columns 5-7
126     av.showColumn(5);
127     view = av.getAlignmentView(true);
128     contigs = view.getVisibleContigs();
129     assertEquals(contigs, new int[] { 2, 8 });
130
131     // hide columns 2-7
132     av.hideColumns(2, 7);
133     view = av.getAlignmentView(true);
134     contigs = view.getVisibleContigs();
135     assertEquals(contigs, new int[] { 8, 8 });
136
137     // hide column 8
138     av.hideColumns(8, 8);
139     view = av.getAlignmentView(true);
140     contigs = view.getVisibleContigs();
141     assertEquals(contigs, new int[] {});
142
143     // unhide all
144     av.showAllHiddenColumns();
145     view = av.getAlignmentView(true);
146     contigs = view.getVisibleContigs();
147     assertEquals(contigs, new int[] { 0, 9 });
148   }
149 }