3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertNull;
5 import static org.testng.Assert.assertTrue;
9 import java.io.IOException;
10 import java.util.HashMap;
12 import org.testng.annotations.Test;
14 import jalview.api.FeatureColourI;
15 import jalview.datamodel.SequenceFeature;
16 import jalview.datamodel.SequenceI;
17 import jalview.datamodel.features.FeatureMatcher;
18 import jalview.datamodel.features.FeatureMatcherSet;
19 import jalview.datamodel.features.FeatureMatcherSetI;
20 import jalview.io.DataSourceType;
21 import jalview.io.FileLoader;
22 import jalview.schemes.FeatureColour;
23 import jalview.schemes.FeatureColourTest;
24 import jalview.util.matcher.Condition;
25 import jalview.viewmodel.seqfeatures.FeatureRendererModel;
27 public class FeatureSettingsTest
30 * Test a roundtrip of save and reload of feature colours and filters as XML
34 @Test(groups = "Functional")
35 public void testSaveLoad() throws IOException
37 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
38 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
39 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
42 * add some features to the sequence
45 addFeatures(seq1, "type1", score++);
46 addFeatures(seq1, "type2", score++);
47 addFeatures(seq1, "type3", score++);
48 addFeatures(seq1, "type4", score++);
49 addFeatures(seq1, "type5", score++);
52 * set colour schemes for features
54 FeatureRendererModel fr = af.getFeatureRenderer();
57 fr.setColour("type1", new FeatureColour(Color.red));
60 FeatureColourI byLabel = new FeatureColour();
61 byLabel.setColourByLabel(true);
62 fr.setColour("type2", byLabel);
64 // type3: by score above threshold
65 FeatureColourI byScore = new FeatureColour(null, Color.BLACK,
66 Color.BLUE, null, 1, 10);
67 byScore.setAboveThreshold(true);
68 byScore.setThreshold(2f);
69 fr.setColour("type3", byScore);
71 // type4: by attribute AF
72 FeatureColourI byAF = new FeatureColour();
73 byAF.setColourByLabel(true);
74 byAF.setAttributeName("AF");
75 fr.setColour("type4", byAF);
77 // type5: by attribute CSQ:PolyPhen below threshold
78 FeatureColourI byPolyPhen = new FeatureColour(null, Color.BLACK,
79 Color.BLUE, null, 1, 10);
80 byPolyPhen.setBelowThreshold(true);
81 byPolyPhen.setThreshold(3f);
82 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
83 fr.setColour("type5", byPolyPhen);
86 * set filters for feature types
89 // filter type1 features by (label contains "x")
90 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
91 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
92 fr.setFeatureFilter("type1", filterByX);
94 // filter type2 features by (score <= 2.4 and score > 1.1)
95 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
96 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
97 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
98 fr.setFeatureFilter("type2", filterByScore);
100 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
101 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
103 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
104 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
106 fr.setFeatureFilter("type3", filterByXY);
109 * save colours and filters to an XML file
111 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
112 coloursFile.deleteOnExit();
113 FeatureSettings fs = new FeatureSettings(af);
114 fs.save(coloursFile);
117 * change feature colours and filters
119 FeatureColourI pink = new FeatureColour(Color.pink);
120 fr.setColour("type1", pink);
121 fr.setColour("type2", pink);
122 fr.setColour("type3", pink);
123 fr.setColour("type4", pink);
124 fr.setColour("type5", pink);
126 FeatureMatcherSetI filter2 = new FeatureMatcherSet();
127 filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
128 fr.setFeatureFilter("type1", filter2);
129 fr.setFeatureFilter("type2", filter2);
130 fr.setFeatureFilter("type3", filter2);
131 fr.setFeatureFilter("type4", filter2);
132 fr.setFeatureFilter("type5", filter2);
135 * reload colours and filters from file and verify they are restored
137 fs.load(coloursFile);
138 FeatureColourI fc = fr.getFeatureStyle("type1");
139 assertTrue(fc.isSimpleColour());
140 assertEquals(fc.getColour(), Color.red);
141 fc = fr.getFeatureStyle("type2");
142 assertTrue(fc.isColourByLabel());
143 fc = fr.getFeatureStyle("type3");
144 assertTrue(fc.isGraduatedColour());
145 assertNull(fc.getAttributeName());
146 assertTrue(fc.isAboveThreshold());
147 assertEquals(fc.getThreshold(), 2f);
148 fc = fr.getFeatureStyle("type4");
149 assertTrue(fc.isColourByLabel());
150 assertTrue(fc.isColourByAttribute());
151 assertEquals(fc.getAttributeName(), new String[] { "AF" });
152 fc = fr.getFeatureStyle("type5");
153 assertTrue(fc.isGraduatedColour());
154 assertTrue(fc.isColourByAttribute());
155 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
156 assertTrue(fc.isBelowThreshold());
157 assertEquals(fc.getThreshold(), 3f);
159 assertEquals(fr.getFeatureFilter("type1").toStableString(), "Label Contains x");
160 assertEquals(fr.getFeatureFilter("type2").toStableString(),
161 "(Score LE 2.4) AND (Score GT 1.1)");
162 assertEquals(fr.getFeatureFilter("type3").toStableString(),
163 "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
167 * Adds two features of the given type to the given sequence, also setting the
168 * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
174 private void addFeatures(SequenceI seq, String featureType, int score)
176 addFeature(seq, featureType, score++);
177 addFeature(seq, featureType, score);
180 private void addFeature(SequenceI seq, String featureType, int score)
182 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
184 sf.setValue("AF", score);
185 sf.setValue("CSQ", new HashMap<String, String>()
188 put("PolyPhen", Integer.toString(score));
191 seq.addSequenceFeature(sf);
195 * @see FeatureColourTest#testGetDescription()
196 * @throws IOException
198 @Test(groups = "Functional")
199 public void testGetColorTooltip() throws IOException
201 assertNull(FeatureSettings.getColorTooltip(null, false));
206 FeatureColourI fc = new FeatureColour(Color.black);
207 String simpleTooltip = "Click to edit, right-click for menu";
208 assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
209 assertNull(FeatureSettings.getColorTooltip(fc, false));
212 * graduated colour tooltip includes description of colour
214 fc.setColourByLabel(true);
215 assertEquals(FeatureSettings.getColorTooltip(fc, false),
216 "<html>By Label</html>");
217 assertEquals(FeatureSettings.getColorTooltip(fc, true),
218 "<html>By Label<br>" + simpleTooltip + "</br></html>");
221 * graduated colour with threshold is html-encoded
223 fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
224 fc.setBelowThreshold(true);
226 assertEquals(FeatureSettings.getColorTooltip(fc, false),
227 "<html>By Score (< 4.0)</html>");
228 assertEquals(FeatureSettings.getColorTooltip(fc, true),
229 "<html>By Score (< 4.0)<br>" + simpleTooltip
232 fc.setAboveThreshold(true);
233 assertEquals(FeatureSettings.getColorTooltip(fc, false),
234 "<html>By Score (> 4.0)</html>");
235 assertEquals(FeatureSettings.getColorTooltip(fc, true),
236 "<html>By Score (> 4.0)<br>" + simpleTooltip