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.Test;
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;
47 public class FeatureSettingsTest
50 * Test a roundtrip of save and reload of feature colours and filters as XML
54 @Test(groups = "Functional")
55 public void testSaveLoad() throws IOException
57 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
58 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
59 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
62 * add some features to the sequence
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++);
72 * set colour schemes for features
74 FeatureRendererModel fr = af.getFeatureRenderer();
77 fr.setColour("type1", new FeatureColour(Color.red));
80 FeatureColourI byLabel = new FeatureColour();
81 byLabel.setColourByLabel(true);
82 fr.setColour("type2", byLabel);
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);
91 // type4: by attribute AF
92 FeatureColourI byAF = new FeatureColour();
93 byAF.setColourByLabel(true);
94 byAF.setAttributeName("AF");
95 fr.setColour("type4", byAF);
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);
106 * set filters for feature types
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);
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);
120 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
121 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
123 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
124 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
126 fr.setFeatureFilter("type3", filterByXY);
129 * save colours and filters to an XML file
131 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
132 coloursFile.deleteOnExit();
133 FeatureSettings fs = new FeatureSettings(af);
134 fs.save(coloursFile);
137 * change feature colours and filters
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);
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);
155 * reload colours and filters from file and verify they are restored
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);
179 assertEquals(fr.getFeatureFilter("type1").toStableString(),
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)");
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"
195 private void addFeatures(SequenceI seq, String featureType, int score)
197 addFeature(seq, featureType, score++);
198 addFeature(seq, featureType, score);
201 private void addFeature(SequenceI seq, String featureType, int score)
203 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
205 sf.setValue("AF", score);
206 sf.setValue("CSQ", new HashMap<String, String>()
209 put("PolyPhen", Integer.toString(score));
212 seq.addSequenceFeature(sf);
216 * @see FeatureColourTest#testGetDescription()
217 * @throws IOException
219 @Test(groups = "Functional")
220 public void testGetColorTooltip() throws IOException
222 assertNull(FeatureSettings.getColorTooltip(null, false));
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));
233 * graduated colour tooltip includes description of colour
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>");
242 * graduated colour with threshold is html-encoded
244 fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
245 fc.setBelowThreshold(true);
247 assertEquals(FeatureSettings.getColorTooltip(fc, false),
248 "<html>By Score (< 4.0)</html>");
249 assertEquals(FeatureSettings.getColorTooltip(fc, true),
250 "<html>By Score (< 4.0)<br>" + simpleTooltip
253 fc.setAboveThreshold(true);
254 assertEquals(FeatureSettings.getColorTooltip(fc, false),
255 "<html>By Score (> 4.0)</html>");
256 assertEquals(FeatureSettings.getColorTooltip(fc, true),
257 "<html>By Score (> 4.0)<br>" + simpleTooltip