912cd2779c8fb46f7bfe9e114de280ef1cd5aefa
[jalview.git] / test / jalview / gui / AnnotationColumnChooserTest.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.gui;
22
23 import static org.testng.Assert.assertFalse;
24 import static org.testng.AssertJUnit.assertEquals;
25
26 import jalview.analysis.AnnotationSorter.SequenceAnnotationOrder;
27 import jalview.bin.Cache;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.AlignmentI;
30 import jalview.datamodel.Annotation;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.SequenceI;
33 import jalview.io.DataSourceType;
34 import jalview.io.FileFormat;
35 import jalview.io.FormatAdapter;
36
37 import java.io.IOException;
38 import java.util.Iterator;
39
40 import org.testng.annotations.BeforeClass;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 /**
45  * Unit tests for AnnotationChooser
46  * 
47  * @author kmourao
48  *
49  */
50 public class AnnotationColumnChooserTest
51 {
52   @BeforeClass(alwaysRun = true)
53   public void setUpJvOptionPane()
54   {
55     JvOptionPane.setInteractiveMode(false);
56     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
57   }
58
59   // 4 sequences x 13 positions
60   final static String TEST_DATA = ">FER_CAPAA Ferredoxin\n"
61           + "TIETHKEAELVG-\n"
62           + ">FER_CAPAN Ferredoxin, chloroplast precursor\n"
63           + "TIETHKEAELVG-\n"
64           + ">FER1_SOLLC Ferredoxin-1, chloroplast precursor\n"
65           + "TIETHKEEELTA-\n" + ">Q93XJ9_SOLTU Ferredoxin I precursor\n"
66           + "TIETHKEEELTA-\n";
67
68   AnnotationChooser testee;
69
70   AlignmentPanel parentPanel;
71
72   AlignFrame af;
73
74   @BeforeMethod(alwaysRun = true)
75   public void setUp() throws IOException
76   {
77     Cache.loadProperties("test/jalview/io/testProps.jvprops");
78     // pin down annotation sort order for test
79     Cache.applicationProperties.setProperty(Preferences.SORT_ANNOTATIONS,
80             SequenceAnnotationOrder.NONE.name());
81     final String TRUE = Boolean.TRUE.toString();
82     Cache.applicationProperties.setProperty(Preferences.SHOW_AUTOCALC_ABOVE,
83             TRUE);
84     Cache.applicationProperties.setProperty("SHOW_QUALITY", TRUE);
85     Cache.applicationProperties.setProperty("SHOW_CONSERVATION", TRUE);
86     Cache.applicationProperties.setProperty("SHOW_IDENTITY", TRUE);
87
88     AlignmentI al = new FormatAdapter().readFile(TEST_DATA,
89             DataSourceType.PASTE, FileFormat.Fasta);
90     af = new AlignFrame(al, 700, 500);
91     parentPanel = new AlignmentPanel(af, af.getViewport());
92     addAnnotations();
93   }
94
95   /**
96    * Add 4 annotations, 3 of them sequence-specific.
97    * 
98    * <PRE>
99    * ann1 - for sequence 0 - label 'IUPRED' ann2 - not sequence related - label
100    * 'Beauty' ann3 - for sequence 3 - label 'JMol' ann4 - for sequence 2 - label
101    * 'IUPRED' ann5 - for sequence 1 - label 'JMol'
102    */
103   private void addAnnotations()
104   {
105     Annotation an = new Annotation(2f);
106     Annotation[] anns = new Annotation[] { an, an, an };
107     AlignmentAnnotation ann0 = new AlignmentAnnotation("IUPRED", "", anns);
108     AlignmentAnnotation ann1 = new AlignmentAnnotation("Beauty", "", anns);
109     AlignmentAnnotation ann2 = new AlignmentAnnotation("JMol", "", anns);
110     AlignmentAnnotation ann3 = new AlignmentAnnotation("IUPRED", "", anns);
111     AlignmentAnnotation ann4 = new AlignmentAnnotation("JMol", "", anns);
112     SequenceI[] seqs = parentPanel.getAlignment().getSequencesArray();
113     ann0.setSequenceRef(seqs[0]);
114     ann2.setSequenceRef(seqs[3]);
115     ann3.setSequenceRef(seqs[2]);
116     ann4.setSequenceRef(seqs[1]);
117     parentPanel.getAlignment().addAnnotation(ann0);
118     parentPanel.getAlignment().addAnnotation(ann1);
119     parentPanel.getAlignment().addAnnotation(ann2);
120     parentPanel.getAlignment().addAnnotation(ann3);
121     parentPanel.getAlignment().addAnnotation(ann4);
122   }
123
124   /**
125    * Test reset
126    */
127   @Test(groups = { "Functional" })
128   public void testReset()
129   {
130     AnnotationColumnChooser acc = new AnnotationColumnChooser(
131             af.getViewport(), af.alignPanel);
132
133     HiddenColumns oldhidden = new HiddenColumns();
134     oldhidden.hideColumns(10, 20);
135     acc.setOldHiddenColumns(oldhidden);
136
137     HiddenColumns newHidden = new HiddenColumns();
138     newHidden.hideColumns(0, 3);
139     newHidden.hideColumns(22, 25);
140     af.getViewport().setHiddenColumns(newHidden);
141
142     HiddenColumns currentHidden = af.getViewport().getAlignment()
143             .getHiddenColumns();
144     Iterator<int[]> regions = currentHidden.iterator();
145     int[] next = regions.next();
146     assertEquals(0, next[0]);
147     assertEquals(3, next[1]);
148     next = regions.next();
149     assertEquals(22, next[0]);
150     assertEquals(25, next[1]);
151
152     // now reset hidden columns
153     acc.reset();
154     currentHidden = af.getViewport().getAlignment().getHiddenColumns();
155     regions = currentHidden.iterator();
156     next = regions.next();
157     assertEquals(10, next[0]);
158     assertEquals(20, next[1]);
159
160     // check works with empty hidden columns as old columns
161     oldhidden = new HiddenColumns();
162     acc.setOldHiddenColumns(oldhidden);
163     acc.reset();
164     currentHidden = af.getViewport().getAlignment().getHiddenColumns();
165     assertFalse(currentHidden.hasHiddenColumns());
166
167     // check works with empty hidden columns as new columns
168     oldhidden.hideColumns(10, 20);
169     acc.setOldHiddenColumns(oldhidden);
170     currentHidden = af.getViewport().getAlignment().getHiddenColumns();
171     assertFalse(currentHidden.hasHiddenColumns());
172
173     acc.reset();
174     currentHidden = af.getViewport().getAlignment().getHiddenColumns();
175     regions = currentHidden.iterator();
176     next = regions.next();
177     assertEquals(10, next[0]);
178     assertEquals(20, next[1]);
179   }
180 }