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.util.matcher.Condition;
18 import java.awt.Color;
20 import java.io.IOException;
21 import java.util.HashMap;
23 import org.testng.annotations.Test;
25 public class FeatureSettingsTest
28 * Test a roundtrip of save and reload of feature colours and filters as XML
32 @Test(groups = "Functional")
33 public void testSaveLoad() throws IOException
35 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
36 ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
37 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
40 * add some features to the sequence
43 addFeatures(seq1, "type1", score++);
44 addFeatures(seq1, "type2", score++);
45 addFeatures(seq1, "type3", score++);
46 addFeatures(seq1, "type4", score++);
47 addFeatures(seq1, "type5", score++);
50 * set colour schemes for features
52 FeatureRenderer fr = af.getFeatureRenderer();
55 fr.setColour("type1", new FeatureColour(Color.red));
58 FeatureColourI byLabel = new FeatureColour();
59 byLabel.setColourByLabel(true);
60 fr.setColour("type2", byLabel);
62 // type3: by score above threshold
63 FeatureColourI byScore = new FeatureColour(Color.BLACK, Color.BLUE, 1,
65 byScore.setAboveThreshold(true);
66 byScore.setThreshold(2f);
67 fr.setColour("type3", byScore);
69 // type4: by attribute AF
70 FeatureColourI byAF = new FeatureColour();
71 byAF.setColourByLabel(true);
72 byAF.setAttributeName("AF");
73 fr.setColour("type4", byAF);
75 // type5: by attribute CSQ:PolyPhen below threshold
76 FeatureColourI byPolyPhen = new FeatureColour(Color.BLACK, Color.BLUE,
78 byPolyPhen.setBelowThreshold(true);
79 byPolyPhen.setThreshold(3f);
80 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
81 fr.setColour("type5", byPolyPhen);
84 * set filters for feature types
87 // filter type1 features by (label contains "x")
88 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
89 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
90 fr.setFeatureFilter("type1", filterByX);
92 // filter type2 features by (score <= 2.4 and score > 1.1)
93 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
94 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
95 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
96 fr.setFeatureFilter("type2", filterByScore);
98 // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
99 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
101 .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
102 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
104 fr.setFeatureFilter("type3", filterByXY);
107 * save colours and filters to an XML file
109 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
110 coloursFile.deleteOnExit();
111 FeatureSettings fs = new FeatureSettings(af);
112 fs.save(coloursFile);
115 * change feature colours and filters
117 FeatureColourI pink = new FeatureColour(Color.pink);
118 fr.setColour("type1", pink);
119 fr.setColour("type2", pink);
120 fr.setColour("type3", pink);
121 fr.setColour("type4", pink);
122 fr.setColour("type5", pink);
124 FeatureMatcherSetI filter2 = new FeatureMatcherSet();
125 filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
126 fr.setFeatureFilter("type1", filter2);
127 fr.setFeatureFilter("type2", filter2);
128 fr.setFeatureFilter("type3", filter2);
129 fr.setFeatureFilter("type4", filter2);
130 fr.setFeatureFilter("type5", filter2);
133 * reload colours and filters from file and verify they are restored
135 fs.load(coloursFile);
136 FeatureColourI fc = fr.getFeatureStyle("type1");
137 assertTrue(fc.isSimpleColour());
138 assertEquals(fc.getColour(), Color.red);
139 fc = fr.getFeatureStyle("type2");
140 assertTrue(fc.isColourByLabel());
141 fc = fr.getFeatureStyle("type3");
142 assertTrue(fc.isGraduatedColour());
143 assertNull(fc.getAttributeName());
144 assertTrue(fc.isAboveThreshold());
145 assertEquals(fc.getThreshold(), 2f);
146 fc = fr.getFeatureStyle("type4");
147 assertTrue(fc.isColourByLabel());
148 assertTrue(fc.isColourByAttribute());
149 assertEquals(fc.getAttributeName(), new String[] { "AF" });
150 fc = fr.getFeatureStyle("type5");
151 assertTrue(fc.isGraduatedColour());
152 assertTrue(fc.isColourByAttribute());
153 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
154 assertTrue(fc.isBelowThreshold());
155 assertEquals(fc.getThreshold(), 3f);
157 assertEquals(fr.getFeatureFilter("type1").toStableString(), "Label Contains x");
158 assertEquals(fr.getFeatureFilter("type2").toStableString(),
159 "(Score LE 2.4) AND (Score GT 1.1)");
160 assertEquals(fr.getFeatureFilter("type3").toStableString(),
161 "(AF Contains X) OR (CSQ:PolyPhen NE 0.0)");
165 * Adds two features of the given type to the given sequence, also setting the
166 * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
172 private void addFeatures(SequenceI seq, String featureType, int score)
174 addFeature(seq, featureType, score++);
175 addFeature(seq, featureType, score);
178 private void addFeature(SequenceI seq, String featureType, int score)
180 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
182 sf.setValue("AF", score);
183 sf.setValue("CSQ", new HashMap<String, String>()
186 put("PolyPhen", Integer.toString(score));
189 seq.addSequenceFeature(sf);