Merge branch 'releases/Release_2_10_4_Branch' into develop
[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.util.matcher.Condition;
17
18 import java.awt.Color;
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.HashMap;
22
23 import org.testng.annotations.Test;
24
25 public class FeatureSettingsTest
26 {
27   /**
28    * Test a roundtrip of save and reload of feature colours and filters as XML
29    * 
30    * @throws IOException
31    */
32   @Test(groups = "Functional")
33   public void testSaveLoad() throws IOException
34   {
35     AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(
36             ">Seq1\nACDEFGHIKLM", DataSourceType.PASTE);
37     SequenceI seq1 = af.getViewport().getAlignment().getSequenceAt(0);
38
39     /*
40      * add some features to the sequence
41      */
42     int score = 1;
43     addFeatures(seq1, "type1", score++);
44     addFeatures(seq1, "type2", score++);
45     addFeatures(seq1, "type3", score++);
46     addFeatures(seq1, "type4", score++);
47     addFeatures(seq1, "type5", score++);
48
49     /*
50      * set colour schemes for features
51      */
52     FeatureRenderer fr = af.getFeatureRenderer();
53
54     // type1: red
55     fr.setColour("type1", new FeatureColour(Color.red));
56
57     // type2: by label
58     FeatureColourI byLabel = new FeatureColour();
59     byLabel.setColourByLabel(true);
60     fr.setColour("type2", byLabel);
61
62     // type3: by score above threshold
63     FeatureColourI byScore = new FeatureColour(Color.BLACK, Color.BLUE, 1,
64             10);
65     byScore.setAboveThreshold(true);
66     byScore.setThreshold(2f);
67     fr.setColour("type3", byScore);
68
69     // type4: by attribute AF
70     FeatureColourI byAF = new FeatureColour();
71     byAF.setColourByLabel(true);
72     byAF.setAttributeName("AF");
73     fr.setColour("type4", byAF);
74
75     // type5: by attribute CSQ:PolyPhen below threshold
76     FeatureColourI byPolyPhen = new FeatureColour(Color.BLACK, Color.BLUE,
77             1, 10);
78     byPolyPhen.setBelowThreshold(true);
79     byPolyPhen.setThreshold(3f);
80     byPolyPhen.setAttributeName("CSQ", "PolyPhen");
81     fr.setColour("type5", byPolyPhen);
82
83     /*
84      * set filters for feature types
85      */
86
87     // filter type1 features by (label contains "x")
88     FeatureMatcherSetI filterByX = new FeatureMatcherSet();
89     filterByX.and(FeatureMatcher.byLabel(Condition.Contains, "x"));
90     fr.setFeatureFilter("type1", filterByX);
91
92     // filter type2 features by (score <= 2.4 and score > 1.1)
93     FeatureMatcherSetI filterByScore = new FeatureMatcherSet();
94     filterByScore.and(FeatureMatcher.byScore(Condition.LE, "2.4"));
95     filterByScore.and(FeatureMatcher.byScore(Condition.GT, "1.1"));
96     fr.setFeatureFilter("type2", filterByScore);
97
98     // filter type3 features by (AF contains X OR CSQ:PolyPhen != 0)
99     FeatureMatcherSetI filterByXY = new FeatureMatcherSet();
100     filterByXY
101             .and(FeatureMatcher.byAttribute(Condition.Contains, "X", "AF"));
102     filterByXY.or(FeatureMatcher.byAttribute(Condition.NE, "0", "CSQ",
103             "PolyPhen"));
104     fr.setFeatureFilter("type3", filterByXY);
105
106     /*
107      * save colours and filters to an XML file
108      */
109     File coloursFile = File.createTempFile("testSaveLoad", ".fc");
110     coloursFile.deleteOnExit();
111     FeatureSettings fs = new FeatureSettings(af);
112     fs.save(coloursFile);
113
114     /*
115      * change feature colours and filters
116      */
117     FeatureColourI pink = new FeatureColour(Color.pink);
118     fr.setColour("type1", pink);
119     fr.setColour("type2", pink);
120     fr.setColour("type3", pink);
121     fr.setColour("type4", pink);
122     fr.setColour("type5", pink);
123
124     FeatureMatcherSetI filter2 = new FeatureMatcherSet();
125     filter2.and(FeatureMatcher.byLabel(Condition.NotContains, "y"));
126     fr.setFeatureFilter("type1", filter2);
127     fr.setFeatureFilter("type2", filter2);
128     fr.setFeatureFilter("type3", filter2);
129     fr.setFeatureFilter("type4", filter2);
130     fr.setFeatureFilter("type5", filter2);
131
132     /*
133      * reload colours and filters from file and verify they are restored
134      */
135     fs.load(coloursFile);
136     FeatureColourI fc = fr.getFeatureStyle("type1");
137     assertTrue(fc.isSimpleColour());
138     assertEquals(fc.getColour(), Color.red);
139     fc = fr.getFeatureStyle("type2");
140     assertTrue(fc.isColourByLabel());
141     fc = fr.getFeatureStyle("type3");
142     assertTrue(fc.isGraduatedColour());
143     assertNull(fc.getAttributeName());
144     assertTrue(fc.isAboveThreshold());
145     assertEquals(fc.getThreshold(), 2f);
146     fc = fr.getFeatureStyle("type4");
147     assertTrue(fc.isColourByLabel());
148     assertTrue(fc.isColourByAttribute());
149     assertEquals(fc.getAttributeName(), new String[] { "AF" });
150     fc = fr.getFeatureStyle("type5");
151     assertTrue(fc.isGraduatedColour());
152     assertTrue(fc.isColourByAttribute());
153     assertEquals(fc.getAttributeName(), new String[] { "CSQ", "PolyPhen" });
154     assertTrue(fc.isBelowThreshold());
155     assertEquals(fc.getThreshold(), 3f);
156
157     assertEquals(fr.getFeatureFilter("type1").toStableString(), "Label Contains x");
158     assertEquals(fr.getFeatureFilter("type2").toStableString(),
159             "(Score LE 2.4) AND (Score GT 1.1)");
160     assertEquals(fr.getFeatureFilter("type3").toStableString(),
161             "(AF Contains X) OR (CSQ:PolyPhen NE 0.0)");
162   }
163
164   /**
165    * Adds two features of the given type to the given sequence, also setting the
166    * score as the value of attribute "AF" and sub-attribute "CSQ:PolyPhen"
167    * 
168    * @param seq
169    * @param featureType
170    * @param score
171    */
172   private void addFeatures(SequenceI seq, String featureType, int score)
173   {
174     addFeature(seq, featureType, score++);
175     addFeature(seq, featureType, score);
176   }
177
178   private void addFeature(SequenceI seq, String featureType, int score)
179   {
180     SequenceFeature sf = new SequenceFeature(featureType, "desc", 1, 2,
181             score, "grp");
182     sf.setValue("AF", score);
183     sf.setValue("CSQ", new HashMap<String, String>()
184     {
185       {
186         put("PolyPhen", Integer.toString(score));
187       }
188     });
189     seq.addSequenceFeature(sf);
190   }
191 }