Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.gui

File FeatureSettingsTest.java

 

Code metrics

0
101
5
1
238
158
5
0.05
20.2
5
1

Classes

Class Line # Actions
FeatureSettingsTest 26 101 5 0
1.0100%
 

Contributing tests

This file is covered by 2 tests. .

Source view

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  1 toggle @Test(groups = "Functional")
34    public void testSaveLoad() throws IOException
35    {
36  1 AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
37    ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
38  1 SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
39   
40    /*
41    * add some features to the sequence
42    */
43  1 int score = 1;
44  1 addFeatures(seq1, "type1", score++);
45  1 addFeatures(seq1, "type2", score++);
46  1 addFeatures(seq1, "type3", score++);
47  1 addFeatures(seq1, "type4", score++);
48  1 addFeatures(seq1, "type5", score++);
49   
50    /*
51    * set colour schemes for features
52    */
53  1 FeatureRenderer fr = af.getFeatureRenderer();
54   
55    // type1: red
56  1 fr.setColour("type1", new FeatureColour(Color.red));
57   
58    // type2: by label
59  1 FeatureColourI byLabel = new FeatureColour();
60  1 byLabel.setColourByLabel(true);
61  1 fr.setColour("type2", byLabel);
62   
63    // type3: by score above threshold
64  1 FeatureColourI byScore = new FeatureColour(Color.BLACK, Color.BLUE, 1,
65    10);
66  1 byScore.setAboveThreshold(true);
67  1 byScore.setThreshold(2f);
68  1 fr.setColour("type3", byScore);
69   
70    // type4: by attribute AF
71  1 FeatureColourI byAF = new FeatureColour();
72  1 byAF.setColourByLabel(true);
73  1 byAF.setAttributeName("AF");
74  1 fr.setColour("type4", byAF);
75   
76    // type5: by attribute CSQ:PolyPhen below threshold
77  1 FeatureColourI byPolyPhen = new FeatureColour(Color.BLACK, Color.BLUE,
78    1, 10);
79  1 byPolyPhen.setBelowThreshold(true);
80  1 byPolyPhen.setThreshold(3f);
81  1 byPolyPhen.setAttributeName("CSQ", "PolyPhen");
82  1 fr.setColour("type5", byPolyPhen);
83   
84    /*
85    * set filters for feature types
86    */
87   
88    // filter type1 features by (label contains "x")
89  1 FeatureMatcherSetI filterByX = new FeatureMatcherSet();
90  1 filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
91  1 fr.setFeatureFilter("type1", filterByX);
92   
93    // filter type2 features by (score <= 2.4 and score > 1.1)
94  1 FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
95  1 filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
96  1 filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
97  1 fr.setFeatureFilter("type2", filterByScore);
98   
99    // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
100  1 FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
101  1 filterByXY
102    .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
103  1 filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
104    "PolyPhen"));
105  1 fr.setFeatureFilter("type3", filterByXY);
106   
107    /*
108    * save colours and filters to an XML file
109    */
110  1 File coloursFile = File.createTempFile("testSaveLoad", ".fc");
111  1 coloursFile.deleteOnExit();
112  1 FeatureSettings fs = new FeatureSettings(af);
113  1 fs.save(coloursFile);
114   
115    /*
116    * change feature colours and filters
117    */
118  1 FeatureColourI pink = new FeatureColour(Color.pink);
119  1 fr.setColour("type1", pink);
120  1 fr.setColour("type2", pink);
121  1 fr.setColour("type3", pink);
122  1 fr.setColour("type4", pink);
123  1 fr.setColour("type5", pink);
124   
125  1 FeatureMatcherSetI filter2 = new FeatureMatcherSet();
126  1 filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
127  1 fr.setFeatureFilter("type1", filter2);
128  1 fr.setFeatureFilter("type2", filter2);
129  1 fr.setFeatureFilter("type3", filter2);
130  1 fr.setFeatureFilter("type4", filter2);
131  1 fr.setFeatureFilter("type5", filter2);
132   
133    /*
134    * reload colours and filters from file and verify they are restored
135    */
136  1 fs.load(coloursFile);
137  1 FeatureColourI fc = fr.getFeatureStyle("type1");
138  1 assertTrue(fc.isSimpleColour());
139  1 assertEquals(fc.getColour(), Color.red);
140  1 fc = fr.getFeatureStyle("type2");
141  1 assertTrue(fc.isColourByLabel());
142  1 fc = fr.getFeatureStyle("type3");
143  1 assertTrue(fc.isGraduatedColour());
144  1 assertNull(fc.getAttributeName());
145  1 assertTrue(fc.isAboveThreshold());
146  1 assertEquals(fc.getThreshold(), 2f);
147  1 fc = fr.getFeatureStyle("type4");
148  1 assertTrue(fc.isColourByLabel());
149  1 assertTrue(fc.isColourByAttribute());
150  1 assertEquals(fc.getAttributeName(), new String[] { "AF" });
151  1 fc = fr.getFeatureStyle("type5");
152  1 assertTrue(fc.isGraduatedColour());
153  1 assertTrue(fc.isColourByAttribute());
154  1 assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
155  1 assertTrue(fc.isBelowThreshold());
156  1 assertEquals(fc.getThreshold(), 3f);
157   
158  1 assertEquals(fr.getFeatureFilter("type1").toStableString(), "Label Contains x");
159  1 assertEquals(fr.getFeatureFilter("type2").toStableString(),
160    "(Score LE 2.4) AND (Score GT 1.1)");
161  1 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  5 toggle private void addFeatures(SequenceI seq, String featureType, int score)
174    {
175  5 addFeature(seq, featureType, score++);
176  5 addFeature(seq, featureType, score);
177    }
178   
 
179  10 toggle private void addFeature(SequenceI seq, String featureType, int score)
180    {
181  10 SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
182    score, "grp");
183  10 sf.setValue("AF", score);
184  10 sf.setValue("CSQ", new HashMap<String, String>()
185    {
 
186  10 toggle {
187  10 put("PolyPhen", Integer.toString(score));
188    }
189    });
190  10 seq.addSequenceFeature(sf);
191    }
192   
193    /**
194    * @see FeatureColourTest#testGetDescription()
195    * @throws IOException
196    */
 
197  1 toggle @Test(groups = "Functional")
198    public void testGetColorTooltip() throws IOException
199    {
200  1 assertNull(FeatureSettings.getColorTooltip(null, false));
201   
202    /*
203    * simple colour
204    */
205  1 FeatureColourI fc = new FeatureColour(Color.black);
206  1 String simpleTooltip = "Click to edit, right-click for menu";
207  1 assertEquals(FeatureSettings.getColorTooltip(fc, true), simpleTooltip);
208  1 assertNull(FeatureSettings.getColorTooltip(fc, false));
209   
210    /*
211    * graduated colour tooltip includes description of colour
212    */
213  1 fc.setColourByLabel(true);
214  1 assertEquals(FeatureSettings.getColorTooltip(fc, false),
215    "<html>By Label</html>");
216  1 assertEquals(FeatureSettings.getColorTooltip(fc, true),
217    "<html>By Label<br>" + simpleTooltip + "</br></html>");
218   
219    /*
220    * graduated colour with threshold is html-encoded
221    */
222  1 fc = new FeatureColour(Color.red, Color.blue, 2f, 10f);
223  1 fc.setBelowThreshold(true);
224  1 fc.setThreshold(4f);
225  1 assertEquals(FeatureSettings.getColorTooltip(fc, false),
226    "<html>By Score (&lt; 4.0)</html>");
227  1 assertEquals(FeatureSettings.getColorTooltip(fc, true),
228    "<html>By Score (&lt; 4.0)<br>" + simpleTooltip
229    + "</br></html>");
230   
231  1 fc.setAboveThreshold(true);
232  1 assertEquals(FeatureSettings.getColorTooltip(fc, false),
233    "<html>By Score (&gt; 4.0)</html>");
234  1 assertEquals(FeatureSettings.getColorTooltip(fc, true),
235    "<html>By Score (&gt; 4.0)<br>" + simpleTooltip
236    + "</br></html>");
237    }
238    }