3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertNull;
5 import static org.testng.Assert.assertTrue;
7 import jalview.api.FeatureColourI;
8 import jalview.datamodel.SequenceFeature;
9 import jalview.datamodel.SequenceI;
10 import jalview.datamodel.features.FeatureMatcher;
11 import jalview.datamodel.features.FeatureMatcherSet;
12 import jalview.datamodel.features.FeatureMatcherSetI;
13 import jalview.io.DataSourceType;
14 import jalview.io.FileLoader;
15 import jalview.schemes.FeatureColour;
16 import jalview.schemes.FeatureColourTest;
17 import jalview.util.matcher.Condition;
19 import java.awt.Color;
21 import java.io.IOException;
22 import java.util.HashMap;
24 import org.testng.annotations.Test;
26 public class FeatureSettingsTest
29 * Test a roundtrip of save and reload of feature colours and filters as XML
33 @Test(groups = "Functional")
34 public void testSaveLoad() throws IOException
36 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
37 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
38 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
41 * add some features to the sequence
44 addFeatures(seq1, "type1", score++);
45 addFeatures(seq1, "type2", score++);
46 addFeatures(seq1, "type3", score++);
47 addFeatures(seq1, "type4", score++);
48 addFeatures(seq1, "type5", score++);
51 * set colour schemes for features
53 FeatureRenderer fr = af.getFeatureRenderer();
56 fr.setColour("type1", new FeatureColour(Color.red));
59 FeatureColourI byLabel = new FeatureColour();
60 byLabel.setColourByLabel(true);
61 fr.setColour("type2", byLabel);
63 // type3: by score above threshold
64 FeatureColourI byScore = new FeatureColour(null, Color.BLACK,
65 Color.BLUE, null, 1, 10);
66 byScore.setAboveThreshold(true);
67 byScore.setThreshold(2f);
68 fr.setColour("type3", byScore);
70 // type4: by attribute AF
71 FeatureColourI byAF = new FeatureColour();
72 byAF.setColourByLabel(true);
73 byAF.setAttributeName("AF");
74 fr.setColour("type4", byAF);
76 // type5: by attribute CSQ:PolyPhen below threshold
77 FeatureColourI byPolyPhen = new FeatureColour(null, Color.BLACK,
78 Color.BLUE, null, 1, 10);
79 byPolyPhen.setBelowThreshold(true);
80 byPolyPhen.setThreshold(3f);
81 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
82 fr.setColour("type5", byPolyPhen);
85 * set filters for feature types
88 // filter type1 features by (label contains "x")
89 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
90 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
91 fr.setFeatureFilter("type1", filterByX);
93 // filter type2 features by (score <= 2.4 and score > 1.1)
94 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
95 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
96 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
97 fr.setFeatureFilter("type2", filterByScore);
99 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
100 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
102 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
103 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
105 fr.setFeatureFilter("type3", filterByXY);
108 * save colours and filters to an XML file
110 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
111 coloursFile.deleteOnExit();
112 FeatureSettings fs = new FeatureSettings(af);
113 fs.save(coloursFile);
116 * change feature colours and filters
118 FeatureColourI pink = new FeatureColour(Color.pink);
119 fr.setColour("type1", pink);
120 fr.setColour("type2", pink);
121 fr.setColour("type3", pink);
122 fr.setColour("type4", pink);
123 fr.setColour("type5", pink);
125 FeatureMatcherSetI filter2 = new FeatureMatcherSet();
126 filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
127 fr.setFeatureFilter("type1", filter2);
128 fr.setFeatureFilter("type2", filter2);
129 fr.setFeatureFilter("type3", filter2);
130 fr.setFeatureFilter("type4", filter2);
131 fr.setFeatureFilter("type5", filter2);
134 * reload colours and filters from file and verify they are restored
136 fs.load(coloursFile);
137 FeatureColourI fc = fr.getFeatureStyle("type1");
138 assertTrue(fc.isSimpleColour());
139 assertEquals(fc.getColour(), Color.red);
140 fc = fr.getFeatureStyle("type2");
141 assertTrue(fc.isColourByLabel());
142 fc = fr.getFeatureStyle("type3");
143 assertTrue(fc.isGraduatedColour());
144 assertNull(fc.getAttributeName());
145 assertTrue(fc.isAboveThreshold());
146 assertEquals(fc.getThreshold(), 2f);
147 fc = fr.getFeatureStyle("type4");
148 assertTrue(fc.isColourByLabel());
149 assertTrue(fc.isColourByAttribute());
150 assertEquals(fc.getAttributeName(), new String[] { "AF" });
151 fc = fr.getFeatureStyle("type5");
152 assertTrue(fc.isGraduatedColour());
153 assertTrue(fc.isColourByAttribute());
154 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
155 assertTrue(fc.isBelowThreshold());
156 assertEquals(fc.getThreshold(), 3f);
158 assertEquals(fr.getFeatureFilter("type1").toStableString(), "Label Contains x");
159 assertEquals(fr.getFeatureFilter("type2").toStableString(),
160 "(Score LE 2.4) AND (Score GT 1.1)");
161 assertEquals(fr.getFeatureFilter("type3").toStableString(),
162 "(AF Contains X) OR (CSQ:PolyPhen NE 0.0)");
166 * Adds two features of the given type to the given sequence, also setting the
167 * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
173 private void addFeatures(SequenceI seq, String featureType, int score)
175 addFeature(seq, featureType, score++);
176 addFeature(seq, featureType, score);
179 private void addFeature(SequenceI seq, String featureType, int score)
181 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
183 sf.setValue("AF", score);
184 sf.setValue("CSQ", new HashMap<String, String>()
187 put("PolyPhen", Integer.toString(score));
190 seq.addSequenceFeature(sf);
194 * @see FeatureColourTest#testGetDescription()
195 * @throws IOException
197 @Test(groups = "Functional")
198 public void testGetColorTooltip() throws IOException
200 assertNull(FeatureSettings.getColorTooltip(null, false));
205 FeatureColourI fc = new FeatureColour(Color.black);
206 String simpleTooltip = "Click to edit, right-click for menu";
207 assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
208 assertNull(FeatureSettings.getColorTooltip(fc, false));
211 * graduated colour tooltip includes description of colour
213 fc.setColourByLabel(true);
214 assertEquals(FeatureSettings.getColorTooltip(fc, false),
215 "<html>By Label</html>");
216 assertEquals(FeatureSettings.getColorTooltip(fc, true),
217 "<html>By Label<br>" + simpleTooltip + "</br></html>");
220 * graduated colour with threshold is html-encoded
222 fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
223 fc.setBelowThreshold(true);
225 assertEquals(FeatureSettings.getColorTooltip(fc, false),
226 "<html>By Score (< 4.0)</html>");
227 assertEquals(FeatureSettings.getColorTooltip(fc, true),
228 "<html>By Score (< 4.0)<br>" + simpleTooltip
231 fc.setAboveThreshold(true);
232 assertEquals(FeatureSettings.getColorTooltip(fc, false),
233 "<html>By Score (> 4.0)</html>");
234 assertEquals(FeatureSettings.getColorTooltip(fc, true),
235 "<html>By Score (> 4.0)<br>" + simpleTooltip