f09c7c799081e8cd61f57ce8ffdbaf6b02226a58
[jalview.git] / test / jalview / gui / FeatureSettingsTest.java
1 package jalview.gui;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertNull;
5 import static org.testng.Assert.assertTrue;
6
7 import java.awt.Color;
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.HashMap;
11
12 import org.testng.annotations.Test;
13
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;
26
27 public class FeatureSettingsTest
28 {
29   /**
30    * Test a roundtrip of save and reload of feature colours and filters as XML
31    * 
32    * @throws IOException
33    */
34   @Test(groups = "Functional")
35   public void testSaveLoad() throws IOException
36   {
37     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
38             ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
39     SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
40
41     /*
42      * add some features to the sequence
43      */
44     int score = 1;
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++);
50
51     /*
52      * set colour schemes for features
53      */
54     FeatureRendererModel fr = af.getFeatureRenderer();
55
56     // type1: red
57     fr.setColour("type1", new FeatureColour(Color.red));
58
59     // type2: by label
60     FeatureColourI byLabel = new FeatureColour();
61     byLabel.setColourByLabel(true);
62     fr.setColour("type2", byLabel);
63
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);
70
71     // type4: by attribute AF
72     FeatureColourI byAF = new FeatureColour();
73     byAF.setColourByLabel(true);
74     byAF.setAttributeName("AF");
75     fr.setColour("type4", byAF);
76
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);
84
85     /*
86      * set filters for feature types
87      */
88
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);
93
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);
99
100     // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
101     FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
102     filterByXY
103             .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
104     filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
105             "PolyPhen"));
106     fr.setFeatureFilter("type3", filterByXY);
107
108     /*
109      * save colours and filters to an XML file
110      */
111     File coloursFile = File.createTempFile("testSaveLoad", ".fc");
112     coloursFile.deleteOnExit();
113     FeatureSettings fs = new FeatureSettings(af);
114     fs.save(coloursFile);
115
116     /*
117      * change feature colours and filters
118      */
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);
125
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);
133
134     /*
135      * reload colours and filters from file and verify they are restored
136      */
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);
158
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)");
164   }
165
166   /**
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"
169    * 
170    * @param seq
171    * @param featureType
172    * @param score
173    */
174   private void addFeatures(SequenceI seq, String featureType, int score)
175   {
176     addFeature(seq, featureType, score++);
177     addFeature(seq, featureType, score);
178   }
179
180   private void addFeature(SequenceI seq, String featureType, int score)
181   {
182     SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
183             score, "grp");
184     sf.setValue("AF", score);
185     sf.setValue("CSQ", new HashMap<String, String>()
186     {
187       {
188         put("PolyPhen", Integer.toString(score));
189       }
190     });
191     seq.addSequenceFeature(sf);
192   }
193
194   /**
195    * @see FeatureColourTest#testGetDescription()
196    * @throws IOException
197    */
198   @Test(groups = "Functional")
199   public void testGetColorTooltip() throws IOException
200   {
201     assertNull(FeatureSettings.getColorTooltip(null, false));
202
203     /*
204      * simple colour
205      */
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));
210
211     /*
212      * graduated colour tooltip includes description of colour
213      */
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>");
219
220     /*
221      * graduated colour with threshold is html-encoded
222      */
223     fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
224     fc.setBelowThreshold(true);
225     fc.setThreshold(4f);
226     assertEquals(FeatureSettings.getColorTooltip(fc, false),
227             "<html>By Score (&lt; 4.0)</html>");
228     assertEquals(FeatureSettings.getColorTooltip(fc, true),
229             "<html>By Score (&lt; 4.0)<br>" + simpleTooltip
230                     + "</br></html>");
231
232     fc.setAboveThreshold(true);
233     assertEquals(FeatureSettings.getColorTooltip(fc, false),
234             "<html>By Score (&gt; 4.0)</html>");
235     assertEquals(FeatureSettings.getColorTooltip(fc, true),
236             "<html>By Score (&gt; 4.0)<br>" + simpleTooltip
237                     + "</br></html>");
238   }
239 }