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