Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File FeatureSettingsTest.java

 

Code metrics

0
101
5
1
239
159
5
0.05
20.2
5
1

Classes

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