55f21114d8b15582c39180b1c87c75f53338405f
[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(),
160             "Label Contains x");
161     assertEquals(fr.getFeatureFilter("type2").toStableString(),
162             "(Score LE 2.4) AND (Score GT 1.1)");
163     assertEquals(fr.getFeatureFilter("type3").toStableString(),
164             "(AF Contains X) OR (CSQ:PolyPhen NE 0)");
165   }
166
167   /**
168    * Adds two features of the given type to the given sequence, also setting the
169    * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
170    * 
171    * @param seq
172    * @param featureType
173    * @param score
174    */
175   private void addFeatures(SequenceI seq, String featureType, int score)
176   {
177     addFeature(seq, featureType, score++);
178     addFeature(seq, featureType, score);
179   }
180
181   private void addFeature(SequenceI seq, String featureType, int score)
182   {
183     SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
184             score, "grp");
185     sf.setValue("AF", score);
186     sf.setValue("CSQ", new HashMap<String, String>()
187     {
188       {
189         put("PolyPhen", Integer.toString(score));
190       }
191     });
192     seq.addSequenceFeature(sf);
193   }
194
195   /**
196    * @see FeatureColourTest#testGetDescription()
197    * @throws IOException
198    */
199   @Test(groups = "Functional")
200   public void testGetColorTooltip() throws IOException
201   {
202     assertNull(FeatureSettings.getColorTooltip(null, false));
203
204     /*
205      * simple colour
206      */
207     FeatureColourI fc = new FeatureColour(Color.black);
208     String simpleTooltip = "Click to edit, right-click for menu";
209     assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
210     assertNull(FeatureSettings.getColorTooltip(fc, false));
211
212     /*
213      * graduated colour tooltip includes description of colour
214      */
215     fc.setColourByLabel(true);
216     assertEquals(FeatureSettings.getColorTooltip(fc, false),
217             "<html>By Label</html>");
218     assertEquals(FeatureSettings.getColorTooltip(fc, true),
219             "<html>By Label<br>" + simpleTooltip + "</br></html>");
220
221     /*
222      * graduated colour with threshold is html-encoded
223      */
224     fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
225     fc.setBelowThreshold(true);
226     fc.setThreshold(4f);
227     assertEquals(FeatureSettings.getColorTooltip(fc, false),
228             "<html>By Score (&lt; 4.0)</html>");
229     assertEquals(FeatureSettings.getColorTooltip(fc, true),
230             "<html>By Score (&lt; 4.0)<br>" + simpleTooltip
231                     + "</br></html>");
232
233     fc.setAboveThreshold(true);
234     assertEquals(FeatureSettings.getColorTooltip(fc, false),
235             "<html>By Score (&gt; 4.0)</html>");
236     assertEquals(FeatureSettings.getColorTooltip(fc, true),
237             "<html>By Score (&gt; 4.0)<br>" + simpleTooltip
238                     + "</br></html>");
239   }
240 }