Merge remote-tracking branch 'origin/bug/JAL-3049colourCellTooltip' into
[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 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;
18
19 import java.awt.Color;
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.HashMap;
23
24 import org.testng.annotations.Test;
25
26 public class FeatureSettingsTest
27 {
28   /**
29    * Test a roundtrip of save and reload of feature colours and filters as XML
30    * 
31    * @throws IOException
32    */
33   @Test(groups = "Functional")
34   public void testSaveLoad() throws IOException
35   {
36     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
37             ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
38     SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
39
40     /*
41      * add some features to the sequence
42      */
43     int score = 1;
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++);
49
50     /*
51      * set colour schemes for features
52      */
53     FeatureRenderer fr = af.getFeatureRenderer();
54
55     // type1: red
56     fr.setColour("type1", new FeatureColour(Color.red));
57
58     // type2: by label
59     FeatureColourI byLabel = new FeatureColour();
60     byLabel.setColourByLabel(true);
61     fr.setColour("type2", byLabel);
62
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);
69
70     // type4: by attribute AF
71     FeatureColourI byAF = new FeatureColour();
72     byAF.setColourByLabel(true);
73     byAF.setAttributeName("AF");
74     fr.setColour("type4", byAF);
75
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);
83
84     /*
85      * set filters for feature types
86      */
87
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);
92
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);
98
99     // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
100     FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
101     filterByXY
102             .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
103     filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
104             "PolyPhen"));
105     fr.setFeatureFilter("type3", filterByXY);
106
107     /*
108      * save colours and filters to an XML file
109      */
110     File coloursFile = File.createTempFile("testSaveLoad", ".fc");
111     coloursFile.deleteOnExit();
112     FeatureSettings fs = new FeatureSettings(af);
113     fs.save(coloursFile);
114
115     /*
116      * change feature colours and filters
117      */
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);
124
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);
132
133     /*
134      * reload colours and filters from file and verify they are restored
135      */
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);
157
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)");
163   }
164
165   /**
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"
168    * 
169    * @param seq
170    * @param featureType
171    * @param score
172    */
173   private void addFeatures(SequenceI seq, String featureType, int score)
174   {
175     addFeature(seq, featureType, score++);
176     addFeature(seq, featureType, score);
177   }
178
179   private void addFeature(SequenceI seq, String featureType, int score)
180   {
181     SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
182             score, "grp");
183     sf.setValue("AF", score);
184     sf.setValue("CSQ", new HashMap<String, String>()
185     {
186       {
187         put("PolyPhen", Integer.toString(score));
188       }
189     });
190     seq.addSequenceFeature(sf);
191   }
192
193   /**
194    * @see FeatureColourTest#testGetDescription()
195    * @throws IOException
196    */
197   @Test(groups = "Functional")
198   public void testGetColorTooltip() throws IOException
199   {
200     assertNull(FeatureSettings.getColorTooltip(null));
201
202     /*
203      * simple colour
204      */
205     FeatureColourI fc = new FeatureColour(Color.black);
206     String simpleTooltip = "Click to edit, right-click for menu";
207     assertEquals(FeatureSettings.getColorTooltip(fc), simpleTooltip);
208
209     /*
210      * graduated colour tooltip includes description of colour
211      */
212     fc.setColourByLabel(true);
213     assertEquals(FeatureSettings.getColorTooltip(fc),
214             "<html>By Label<br>" + simpleTooltip + "</br></html>");
215
216     /*
217      * graduated colour with threshold is html-encoded
218      */
219     fc = new FeatureColour(null, Color.red, Color.blue, null, 2f, 10f);
220     fc.setBelowThreshold(true);
221     fc.setThreshold(4f);
222     assertEquals(FeatureSettings.getColorTooltip(fc),
223             "<html>By Score (&lt; 4.0)<br>" + simpleTooltip
224                     + "</br></html>");
225     fc.setAboveThreshold(true);
226     assertEquals(FeatureSettings.getColorTooltip(fc),
227             "<html>By Score (&gt; 4.0)<br>" + simpleTooltip
228                     + "</br></html>");
229   }
230 }