2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertNull;
25 import static org.testng.Assert.assertTrue;
27 import java.awt.Color;
29 import java.io.IOException;
30 import java.util.HashMap;
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
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;
51 public class FeatureSettingsTest
53 @BeforeClass(alwaysRun = true)
54 public static void setUpBeforeClass() throws Exception
57 * use read-only test properties file
59 Cache.loadProperties("test/jalview/io/testProps.jvprops");
60 Jalview.main(new String[] { "-nonews" });
63 @AfterMethod(alwaysRun = true)
64 public void tearDown()
66 if (Desktop.instance != null)
67 Desktop.instance.closeAll_actionPerformed(null);
71 * Test a roundtrip of save and reload of feature colours and filters as XML
75 @Test(groups = "Functional")
76 public void testSaveLoad() throws IOException
78 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
79 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
80 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
83 * add some features to the sequence
86 addFeatures(seq1, "type1", score++);
87 addFeatures(seq1, "type2", score++);
88 addFeatures(seq1, "type3", score++);
89 addFeatures(seq1, "type4", score++);
90 addFeatures(seq1, "type5", score++);
93 * set colour schemes for features
95 FeatureRendererModel fr = af.getFeatureRenderer();
98 fr.setColour("type1", new FeatureColour(Color.red));
101 FeatureColourI byLabel = new FeatureColour();
102 byLabel.setColourByLabel(true);
103 fr.setColour("type2", byLabel);
105 // type3: by score above threshold
106 FeatureColourI byScore = new FeatureColour(null, Color.BLACK,
107 Color.BLUE, null, 1, 10);
108 byScore.setAboveThreshold(true);
109 byScore.setThreshold(2f);
110 fr.setColour("type3", byScore);
112 // type4: by attribute AF
113 FeatureColourI byAF = new FeatureColour();
114 byAF.setColourByLabel(true);
115 byAF.setAttributeName("AF");
116 fr.setColour("type4", byAF);
118 // type5: by attribute CSQ:PolyPhen below threshold
119 FeatureColourI byPolyPhen = new FeatureColour(null, Color.BLACK,
120 Color.BLUE, null, 1, 10);
121 byPolyPhen.setBelowThreshold(true);
122 byPolyPhen.setThreshold(3f);
123 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
124 fr.setColour("type5", byPolyPhen);
127 * set filters for feature types
130 // filter type1 features by (label contains "x")
131 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
132 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
133 fr.setFeatureFilter("type1", filterByX);
135 // filter type2 features by (score <= 2.4 and score > 1.1)
136 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
137 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
138 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
139 fr.setFeatureFilter("type2", filterByScore);
141 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
142 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
144 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
145 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
147 fr.setFeatureFilter("type3", filterByXY);
150 * save colours and filters to an XML file
152 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
153 coloursFile.deleteOnExit();
154 FeatureSettings fs = new FeatureSettings(af);
155 fs.save(coloursFile);
158 * change feature colours and filters
160 FeatureColourI pink = new FeatureColour(Color.pink);
161 fr.setColour("type1", pink);
162 fr.setColour("type2", pink);
163 fr.setColour("type3", pink);
164 fr.setColour("type4", pink);
165 fr.setColour("type5", pink);
167 FeatureMatcherSetI filter2 = new FeatureMatcherSet();
168 filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
169 fr.setFeatureFilter("type1", filter2);
170 fr.setFeatureFilter("type2", filter2);
171 fr.setFeatureFilter("type3", filter2);
172 fr.setFeatureFilter("type4", filter2);
173 fr.setFeatureFilter("type5", filter2);
176 * reload colours and filters from file and verify they are restored
178 fs.load(coloursFile);
179 FeatureColourI fc = fr.getFeatureStyle("type1");
180 assertTrue(fc.isSimpleColour());
181 assertEquals(fc.getColour(), Color.red);
182 fc = fr.getFeatureStyle("type2");
183 assertTrue(fc.isColourByLabel());
184 fc = fr.getFeatureStyle("type3");
185 assertTrue(fc.isGraduatedColour());
186 assertNull(fc.getAttributeName());
187 assertTrue(fc.isAboveThreshold());
188 assertEquals(fc.getThreshold(), 2f);
189 fc = fr.getFeatureStyle("type4");
190 assertTrue(fc.isColourByLabel());
191 assertTrue(fc.isColourByAttribute());
192 assertEquals(fc.getAttributeName(), new String[] { "AF" });
193 fc = fr.getFeatureStyle("type5");
194 assertTrue(fc.isGraduatedColour());
195 assertTrue(fc.isColourByAttribute());
196 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
197 assertTrue(fc.isBelowThreshold());
198 assertEquals(fc.getThreshold(), 3f);
200 assertEquals(fr.getFeatureFilter("type1").toStableString(),
202 assertEquals(fr.getFeatureFilter("type2").toStableString(),
203 "(Score LE 2.4) AND (Score GT 1.1)");
204 assertEquals(fr.getFeatureFilter("type3").toStableString(),
205 "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
209 * Adds two features of the given type to the given sequence, also setting the
210 * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
216 private void addFeatures(SequenceI seq, String featureType, int score)
218 addFeature(seq, featureType, score++);
219 addFeature(seq, featureType, score);
222 private void addFeature(SequenceI seq, String featureType, int score)
224 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
226 sf.setValue("AF", score);
227 sf.setValue("CSQ", new HashMap<String, String>()
230 put("PolyPhen", Integer.toString(score));
233 seq.addSequenceFeature(sf);
237 * @see FeatureColourTest#testGetDescription()
238 * @throws IOException
240 @Test(groups = "Functional")
241 public void testGetColorTooltip() throws IOException
243 assertNull(FeatureSettings.getColorTooltip(null, false));
248 FeatureColourI fc = new FeatureColour(Color.black);
249 String simpleTooltip = "Click to edit, right-click for menu";
250 assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
251 assertNull(FeatureSettings.getColorTooltip(fc, false));
254 * graduated colour tooltip includes description of colour
256 fc.setColourByLabel(true);
257 assertEquals(FeatureSettings.getColorTooltip(fc, false),
258 "<html>By Label</html>");
259 assertEquals(FeatureSettings.getColorTooltip(fc, true),
260 "<html>By Label<br>" + simpleTooltip + "</br></html>");
263 * graduated colour with threshold is html-encoded
265 fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
266 fc.setBelowThreshold(true);
268 assertEquals(FeatureSettings.getColorTooltip(fc, false),
269 "<html>By Score (< 4.0)</html>");
270 assertEquals(FeatureSettings.getColorTooltip(fc, true),
271 "<html>By Score (< 4.0)<br>" + simpleTooltip
274 fc.setAboveThreshold(true);
275 assertEquals(FeatureSettings.getColorTooltip(fc, false),
276 "<html>By Score (> 4.0)</html>");
277 assertEquals(FeatureSettings.getColorTooltip(fc, true),
278 "<html>By Score (> 4.0)<br>" + simpleTooltip