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.FileFormat;
46 import jalview.io.FileFormatI;
47 import jalview.io.FileLoader;
48 import jalview.io.IdentifyFile;
49 import jalview.schemes.FeatureColour;
50 import jalview.schemes.FeatureColourTest;
51 import jalview.util.matcher.Condition;
52 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
54 public class FeatureSettingsTest
56 @BeforeClass(alwaysRun = true)
57 public static void setUpBeforeClass() throws Exception
60 * use read-only test properties file
62 Cache.loadProperties("test/jalview/io/testProps.jvprops");
63 Jalview.main(new String[] { "--nonews" });
66 @AfterMethod(alwaysRun = true)
67 public void tearDown()
69 if (Desktop.instance != null)
70 Desktop.instance.closeAll_actionPerformed(null);
74 * Test a roundtrip of save and reload of feature colours and filters as XML
78 @Test(groups = "Functional")
79 public void testSaveLoad() throws IOException
81 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
82 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
83 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
86 * add some features to the sequence
89 addFeatures(seq1, "type1", score++);
90 addFeatures(seq1, "type2", score++);
91 addFeatures(seq1, "type3", score++);
92 addFeatures(seq1, "type4", score++);
93 addFeatures(seq1, "type5", score++);
96 * set colour schemes for features
98 FeatureRendererModel fr = af.getFeatureRenderer();
101 fr.setColour("type1", new FeatureColour(Color.red));
104 FeatureColourI byLabel = new FeatureColour();
105 byLabel.setColourByLabel(true);
106 fr.setColour("type2", byLabel);
108 // type3: by score above threshold
109 FeatureColourI byScore = new FeatureColour(null, Color.BLACK,
110 Color.BLUE, null, 1, 10);
111 byScore.setAboveThreshold(true);
112 byScore.setThreshold(2f);
113 fr.setColour("type3", byScore);
115 // type4: by attribute AF
116 FeatureColourI byAF = new FeatureColour();
117 byAF.setColourByLabel(true);
118 byAF.setAttributeName("AF");
119 fr.setColour("type4", byAF);
121 // type5: by attribute CSQ:PolyPhen below threshold
122 FeatureColourI byPolyPhen = new FeatureColour(null, Color.BLACK,
123 Color.BLUE, null, 1, 10);
124 byPolyPhen.setBelowThreshold(true);
125 byPolyPhen.setThreshold(3f);
126 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
127 fr.setColour("type5", byPolyPhen);
130 * set filters for feature types
133 // filter type1 features by (label contains "x")
134 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
135 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
136 fr.setFeatureFilter("type1", filterByX);
138 // filter type2 features by (score <= 2.4 and score > 1.1)
139 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
140 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
141 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
142 fr.setFeatureFilter("type2", filterByScore);
144 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
145 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
147 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
148 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
150 fr.setFeatureFilter("type3", filterByXY);
153 * save colours and filters to an XML file
155 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
156 coloursFile.deleteOnExit();
157 FeatureSettings fs = new FeatureSettings(af);
158 fs.save(coloursFile);
161 * change feature colours and filters
163 FeatureColourI pink = new FeatureColour(Color.pink);
164 fr.setColour("type1", pink);
165 fr.setColour("type2", pink);
166 fr.setColour("type3", pink);
167 fr.setColour("type4", pink);
168 fr.setColour("type5", pink);
170 FeatureMatcherSetI filter2 = new FeatureMatcherSet();
171 filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
172 fr.setFeatureFilter("type1", filter2);
173 fr.setFeatureFilter("type2", filter2);
174 fr.setFeatureFilter("type3", filter2);
175 fr.setFeatureFilter("type4", filter2);
176 fr.setFeatureFilter("type5", filter2);
179 * reload colours and filters from file and verify they are restored
181 fs.load(coloursFile);
182 FeatureColourI fc = fr.getFeatureStyle("type1");
183 assertTrue(fc.isSimpleColour());
184 assertEquals(fc.getColour(), Color.red);
185 fc = fr.getFeatureStyle("type2");
186 assertTrue(fc.isColourByLabel());
187 fc = fr.getFeatureStyle("type3");
188 assertTrue(fc.isGraduatedColour());
189 assertNull(fc.getAttributeName());
190 assertTrue(fc.isAboveThreshold());
191 assertEquals(fc.getThreshold(), 2f);
192 fc = fr.getFeatureStyle("type4");
193 assertTrue(fc.isColourByLabel());
194 assertTrue(fc.isColourByAttribute());
195 assertEquals(fc.getAttributeName(), new String[] { "AF" });
196 fc = fr.getFeatureStyle("type5");
197 assertTrue(fc.isGraduatedColour());
198 assertTrue(fc.isColourByAttribute());
199 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
200 assertTrue(fc.isBelowThreshold());
201 assertEquals(fc.getThreshold(), 3f);
203 assertEquals(fr.getFeatureFilter("type1").toStableString(),
205 assertEquals(fr.getFeatureFilter("type2").toStableString(),
206 "(Score LE 2.4) AND (Score GT 1.1)");
207 assertEquals(fr.getFeatureFilter("type3").toStableString(),
208 "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
212 * Adds two features of the given type to the given sequence, also setting the
213 * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
219 private void addFeatures(SequenceI seq, String featureType, int score)
221 addFeature(seq, featureType, score++);
222 addFeature(seq, featureType, score);
225 private void addFeature(SequenceI seq, String featureType, int score)
227 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
229 sf.setValue("AF", score);
230 sf.setValue("CSQ", new HashMap<String, String>()
233 put("PolyPhen", Integer.toString(score));
236 seq.addSequenceFeature(sf);
240 * @see FeatureColourTest#testGetDescription()
241 * @throws IOException
243 @Test(groups = "Functional")
244 public void testGetColorTooltip() throws IOException
246 assertNull(FeatureSettings.getColorTooltip(null, false));
251 FeatureColourI fc = new FeatureColour(Color.black);
252 String simpleTooltip = "Click to edit, right-click for menu";
253 assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
254 assertNull(FeatureSettings.getColorTooltip(fc, false));
257 * graduated colour tooltip includes description of colour
259 fc.setColourByLabel(true);
260 assertEquals(FeatureSettings.getColorTooltip(fc, false),
261 "<html>By Label</html>");
262 assertEquals(FeatureSettings.getColorTooltip(fc, true),
263 "<html>By Label<br>" + simpleTooltip + "</br></html>");
266 * graduated colour with threshold is html-encoded
268 fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
269 fc.setBelowThreshold(true);
271 assertEquals(FeatureSettings.getColorTooltip(fc, false),
272 "<html>By Score (< 4.0)</html>");
273 assertEquals(FeatureSettings.getColorTooltip(fc, true),
274 "<html>By Score (< 4.0)<br>" + simpleTooltip
277 fc.setAboveThreshold(true);
278 assertEquals(FeatureSettings.getColorTooltip(fc, false),
279 "<html>By Score (> 4.0)</html>");
280 assertEquals(FeatureSettings.getColorTooltip(fc, true),
281 "<html>By Score (> 4.0)<br>" + simpleTooltip
285 String fsfile = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><ns2:JalviewUserColours schemeName=\"Sequence Features\" xmlns:ns2=\"www.jalview.org/colours\">",
286 fsfile2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<ns2:JalviewUserColours schemeName=\"Sequence Features\" xmlns:ns2=\"www.jalview.org/colours\">";
288 @Test(groups = "Functional")
289 public void testIdentifyFeatureSettingsFile() throws Exception
291 FileFormatI type = new IdentifyFile().identify(fsfile,
292 DataSourceType.PASTE);
293 assertTrue(FileFormat.FeatureSettings == type,
294 "Feature settings file was identified as " + type);
295 type = new IdentifyFile().identify(fsfile2, DataSourceType.PASTE);
296 assertTrue(FileFormat.FeatureSettings == type,
297 "Feature settings file with newline was identified as " + type);