JAL-629 fixed a test failing when run without a Jalview.main() having been started...
[jalview.git] / test / jalview / gui / FeatureSettingsTest.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.assertEquals;
24 import static org.testng.Assert.assertNull;
25 import static org.testng.Assert.assertTrue;
26
27 import java.awt.Color;
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.HashMap;
31
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 import jalview.api.FeatureColourI;
37 import jalview.bin.Cache;
38 import jalview.bin.Jalview;
39 import jalview.datamodel.SequenceFeature;
40 import jalview.datamodel.SequenceI;
41 import jalview.datamodel.features.FeatureMatcher;
42 import jalview.datamodel.features.FeatureMatcherSet;
43 import jalview.datamodel.features.FeatureMatcherSetI;
44 import jalview.io.DataSourceType;
45 import jalview.io.FileLoader;
46 import jalview.schemes.FeatureColour;
47 import jalview.schemes.FeatureColourTest;
48 import jalview.util.matcher.Condition;
49 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
50
51 public class FeatureSettingsTest
52 {
53   @BeforeClass(alwaysRun = true)
54   public static void setUpBeforeClass() throws Exception
55   {
56     /*
57      * use read-only test properties file
58      */
59     Cache.loadProperties("test/jalview/io/testProps.jvprops");
60     Jalview.main(new String[] { "-nonews" });
61   }
62
63   @AfterMethod(alwaysRun = true)
64   public void tearDown()
65   {
66     Desktop.instance.closeAll_actionPerformed(null);
67   }
68
69   /**
70    * Test a roundtrip of save and reload of feature colours and filters as XML
71    * 
72    * @throws IOException
73    */
74   @Test(groups = "Functional")
75   public void testSaveLoad() throws IOException
76   {
77     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
78             ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
79     SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
80
81     /*
82      * add some features to the sequence
83      */
84     int score = 1;
85     addFeatures(seq1, "type1", score++);
86     addFeatures(seq1, "type2", score++);
87     addFeatures(seq1, "type3", score++);
88     addFeatures(seq1, "type4", score++);
89     addFeatures(seq1, "type5", score++);
90
91     /*
92      * set colour schemes for features
93      */
94     FeatureRendererModel fr = af.getFeatureRenderer();
95
96     // type1: red
97     fr.setColour("type1", new FeatureColour(Color.red));
98
99     // type2: by label
100     FeatureColourI byLabel = new FeatureColour();
101     byLabel.setColourByLabel(true);
102     fr.setColour("type2", byLabel);
103
104     // type3: by score above threshold
105     FeatureColourI byScore = new FeatureColour(null, Color.BLACK,
106             Color.BLUE, null, 1, 10);
107     byScore.setAboveThreshold(true);
108     byScore.setThreshold(2f);
109     fr.setColour("type3", byScore);
110
111     // type4: by attribute AF
112     FeatureColourI byAF = new FeatureColour();
113     byAF.setColourByLabel(true);
114     byAF.setAttributeName("AF");
115     fr.setColour("type4", byAF);
116
117     // type5: by attribute CSQ:PolyPhen below threshold
118     FeatureColourI byPolyPhen = new FeatureColour(null, Color.BLACK,
119             Color.BLUE, null, 1, 10);
120     byPolyPhen.setBelowThreshold(true);
121     byPolyPhen.setThreshold(3f);
122     byPolyPhen.setAttributeName("CSQ", "PolyPhen");
123     fr.setColour("type5", byPolyPhen);
124
125     /*
126      * set filters for feature types
127      */
128
129     // filter type1 features by (label contains "x")
130     FeatureMatcherSetI filterByX = new FeatureMatcherSet();
131     filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
132     fr.setFeatureFilter("type1", filterByX);
133
134     // filter type2 features by (score <= 2.4 and score > 1.1)
135     FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
136     filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
137     filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
138     fr.setFeatureFilter("type2", filterByScore);
139
140     // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
141     FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
142     filterByXY
143             .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
144     filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
145             "PolyPhen"));
146     fr.setFeatureFilter("type3", filterByXY);
147
148     /*
149      * save colours and filters to an XML file
150      */
151     File coloursFile = File.createTempFile("testSaveLoad", ".fc");
152     coloursFile.deleteOnExit();
153     FeatureSettings fs = new FeatureSettings(af);
154     fs.save(coloursFile);
155
156     /*
157      * change feature colours and filters
158      */
159     FeatureColourI pink = new FeatureColour(Color.pink);
160     fr.setColour("type1", pink);
161     fr.setColour("type2", pink);
162     fr.setColour("type3", pink);
163     fr.setColour("type4", pink);
164     fr.setColour("type5", pink);
165
166     FeatureMatcherSetI filter2 = new FeatureMatcherSet();
167     filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
168     fr.setFeatureFilter("type1", filter2);
169     fr.setFeatureFilter("type2", filter2);
170     fr.setFeatureFilter("type3", filter2);
171     fr.setFeatureFilter("type4", filter2);
172     fr.setFeatureFilter("type5", filter2);
173
174     /*
175      * reload colours and filters from file and verify they are restored
176      */
177     fs.load(coloursFile);
178     FeatureColourI fc = fr.getFeatureStyle("type1");
179     assertTrue(fc.isSimpleColour());
180     assertEquals(fc.getColour(), Color.red);
181     fc = fr.getFeatureStyle("type2");
182     assertTrue(fc.isColourByLabel());
183     fc = fr.getFeatureStyle("type3");
184     assertTrue(fc.isGraduatedColour());
185     assertNull(fc.getAttributeName());
186     assertTrue(fc.isAboveThreshold());
187     assertEquals(fc.getThreshold(), 2f);
188     fc = fr.getFeatureStyle("type4");
189     assertTrue(fc.isColourByLabel());
190     assertTrue(fc.isColourByAttribute());
191     assertEquals(fc.getAttributeName(), new String[] { "AF" });
192     fc = fr.getFeatureStyle("type5");
193     assertTrue(fc.isGraduatedColour());
194     assertTrue(fc.isColourByAttribute());
195     assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
196     assertTrue(fc.isBelowThreshold());
197     assertEquals(fc.getThreshold(), 3f);
198
199     assertEquals(fr.getFeatureFilter("type1").toStableString(),
200             "Label Contains x");
201     assertEquals(fr.getFeatureFilter("type2").toStableString(),
202             "(Score LE 2.4) AND (Score GT 1.1)");
203     assertEquals(fr.getFeatureFilter("type3").toStableString(),
204             "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
205   }
206
207   /**
208    * Adds two features of the given type to the given sequence, also setting the
209    * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
210    * 
211    * @param seq
212    * @param featureType
213    * @param score
214    */
215   private void addFeatures(SequenceI seq, String featureType, int score)
216   {
217     addFeature(seq, featureType, score++);
218     addFeature(seq, featureType, score);
219   }
220
221   private void addFeature(SequenceI seq, String featureType, int score)
222   {
223     SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
224             score, "grp");
225     sf.setValue("AF", score);
226     sf.setValue("CSQ", new HashMap<String, String>()
227     {
228       {
229         put("PolyPhen", Integer.toString(score));
230       }
231     });
232     seq.addSequenceFeature(sf);
233   }
234
235   /**
236    * @see FeatureColourTest#testGetDescription()
237    * @throws IOException
238    */
239   @Test(groups = "Functional")
240   public void testGetColorTooltip() throws IOException
241   {
242     assertNull(FeatureSettings.getColorTooltip(null, false));
243
244     /*
245      * simple colour
246      */
247     FeatureColourI fc = new FeatureColour(Color.black);
248     String simpleTooltip = "Click to edit, right-click for menu";
249     assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
250     assertNull(FeatureSettings.getColorTooltip(fc, false));
251
252     /*
253      * graduated colour tooltip includes description of colour
254      */
255     fc.setColourByLabel(true);
256     assertEquals(FeatureSettings.getColorTooltip(fc, false),
257             "<html>By Label</html>");
258     assertEquals(FeatureSettings.getColorTooltip(fc, true),
259             "<html>By Label<br>" + simpleTooltip + "</br></html>");
260
261     /*
262      * graduated colour with threshold is html-encoded
263      */
264     fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
265     fc.setBelowThreshold(true);
266     fc.setThreshold(4f);
267     assertEquals(FeatureSettings.getColorTooltip(fc, false),
268             "<html>By Score (&lt; 4.0)</html>");
269     assertEquals(FeatureSettings.getColorTooltip(fc, true),
270             "<html>By Score (&lt; 4.0)<br>" + simpleTooltip
271                     + "</br></html>");
272
273     fc.setAboveThreshold(true);
274     assertEquals(FeatureSettings.getColorTooltip(fc, false),
275             "<html>By Score (&gt; 4.0)</html>");
276     assertEquals(FeatureSettings.getColorTooltip(fc, true),
277             "<html>By Score (&gt; 4.0)<br>" + simpleTooltip
278                     + "</br></html>");
279   }
280 }