1 package jalview.datamodel.features;
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertSame;
6 import static org.testng.Assert.assertTrue;
7 import static org.testng.Assert.fail;
9 import jalview.datamodel.SequenceFeature;
10 import jalview.util.matcher.Condition;
12 import java.util.HashMap;
13 import java.util.Iterator;
16 import org.testng.annotations.Test;
18 public class FeatureMatcherSetTest
20 @Test(groups = "Functional")
21 public void testMatches_byAttribute()
24 * a numeric matcher - MatcherTest covers more conditions
26 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
28 FeatureMatcherSetI fms = new FeatureMatcherSet();
30 SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
31 assertFalse(fms.matches(sf));
32 sf.setValue("AF", "foobar");
33 assertFalse(fms.matches(sf));
34 sf.setValue("AF", "-2");
35 assertTrue(fms.matches(sf));
36 sf.setValue("AF", "-1");
37 assertTrue(fms.matches(sf));
38 sf.setValue("AF", "-3");
39 assertFalse(fms.matches(sf));
40 sf.setValue("AF", "");
41 assertFalse(fms.matches(sf));
44 * a string pattern matcher
46 fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF");
47 fms = new FeatureMatcherSet();
49 assertFalse(fms.matches(sf));
50 sf.setValue("AF", "raining cats and dogs");
51 assertTrue(fms.matches(sf));
54 @Test(groups = "Functional")
57 // condition1: AF value contains "dog" (matches)
58 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.Contains,
60 // condition 2: CSQ value does not contain "how" (does not match)
61 FeatureMatcherI fm2 = FeatureMatcher.byAttribute(Condition.NotContains,
64 SequenceFeature sf = new SequenceFeature("Cath", "helix domain", 11, 12,
66 sf.setValue("AF", "raining cats and dogs");
67 sf.setValue("CSQ", "showers");
69 assertTrue(fm1.matches(sf));
70 assertFalse(fm2.matches(sf));
72 FeatureMatcherSetI fms = new FeatureMatcherSet();
73 assertTrue(fms.matches(sf)); // if no conditions, then 'all' pass
75 assertTrue(fms.matches(sf));
77 assertFalse(fms.matches(sf));
80 * OR a failed attribute condition with a matched label condition
82 fms = new FeatureMatcherSet();
84 assertFalse(fms.matches(sf));
85 FeatureMatcher byLabelPass = FeatureMatcher.byLabel(Condition.Contains,
88 assertTrue(fms.matches(sf));
91 * OR a failed attribute condition with a failed score condition
93 fms = new FeatureMatcherSet();
95 assertFalse(fms.matches(sf));
96 FeatureMatcher byScoreFail = FeatureMatcher.byScore(Condition.LT,
99 assertFalse(fms.matches(sf));
102 * OR failed attribute and score conditions with matched label condition
104 fms = new FeatureMatcherSet();
105 fms.or(fm2).or(byScoreFail);
106 assertFalse(fms.matches(sf));
108 assertTrue(fms.matches(sf));
111 @Test(groups = "Functional")
112 public void testToString()
114 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.LT, "1.2",
116 assertEquals(fm1.toString(), "AF < 1.2");
118 FeatureMatcher fm2 = FeatureMatcher.byAttribute(Condition.NotContains,
121 assertEquals(fm2.toString(), "CLIN_SIG Does not contain 'PATH'");
126 FeatureMatcherSetI fms = new FeatureMatcherSet();
127 assertEquals(fms.toString(), "");
129 assertEquals(fms.toString(), "(AF < 1.2)");
131 assertEquals(fms.toString(),
132 "(AF < 1.2) AND (CLIN_SIG Does not contain 'PATH')");
137 fms = new FeatureMatcherSet();
138 assertEquals(fms.toString(), "");
140 assertEquals(fms.toString(), "(AF < 1.2)");
142 assertEquals(fms.toString(),
143 "(AF < 1.2) OR (CLIN_SIG Does not contain 'PATH')");
148 fail("Expected exception");
149 } catch (IllegalStateException e)
155 @Test(groups = "Functional")
158 // condition1: AF value contains "dog" (matches)
159 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.Contains,
161 // condition 2: CSQ value does not contain "how" (does not match)
162 FeatureMatcherI fm2 = FeatureMatcher.byAttribute(Condition.NotContains,
165 SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
166 sf.setValue("AF", "raining cats and dogs");
167 sf.setValue("CSQ", "showers");
169 assertTrue(fm1.matches(sf));
170 assertFalse(fm2.matches(sf));
172 FeatureMatcherSetI fms = new FeatureMatcherSet();
173 assertTrue(fms.matches(sf)); // if no conditions, then 'all' pass
175 assertTrue(fms.matches(sf));
177 assertTrue(fms.matches(sf)); // true or false makes true
179 fms = new FeatureMatcherSet();
181 assertFalse(fms.matches(sf));
183 assertTrue(fms.matches(sf)); // false or true makes true
188 fail("Expected exception");
189 } catch (IllegalStateException e)
195 @Test(groups = "Functional")
196 public void testIsEmpty()
198 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2.0",
200 FeatureMatcherSetI fms = new FeatureMatcherSet();
201 assertTrue(fms.isEmpty());
203 assertFalse(fms.isEmpty());
206 @Test(groups = "Functional")
207 public void testGetMatchers()
209 FeatureMatcherSetI fms = new FeatureMatcherSet();
214 Iterator<FeatureMatcherI> iterator = fms.getMatchers().iterator();
215 assertFalse(iterator.hasNext());
220 FeatureMatcherI fm1 = FeatureMatcher.byAttribute(Condition.GE, "-2",
223 iterator = fms.getMatchers().iterator();
224 assertSame(fm1, iterator.next());
225 assertFalse(iterator.hasNext());
230 FeatureMatcherI fm2 = FeatureMatcher.byAttribute(Condition.LT, "8f",
233 iterator = fms.getMatchers().iterator();
234 assertSame(fm1, iterator.next());
235 assertSame(fm2, iterator.next());
236 assertFalse(iterator.hasNext());
240 * Tests for the 'compound attribute' key i.e. where first key's value is a map
241 * from which we take the value for the second key, e.g. CSQ : Consequence
243 @Test(groups = "Functional")
244 public void testMatches_compoundKey()
247 * a numeric matcher - MatcherTest covers more conditions
249 FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
250 "CSQ", "Consequence");
251 SequenceFeature sf = new SequenceFeature("Cath", "desc", 2, 10, "grp");
252 FeatureMatcherSetI fms = new FeatureMatcherSet();
254 assertFalse(fms.matches(sf));
255 Map<String, String> csq = new HashMap<>();
256 sf.setValue("CSQ", csq);
257 assertFalse(fms.matches(sf));
258 csq.put("Consequence", "-2");
259 assertTrue(fms.matches(sf));
260 csq.put("Consequence", "-1");
261 assertTrue(fms.matches(sf));
262 csq.put("Consequence", "-3");
263 assertFalse(fms.matches(sf));
264 csq.put("Consequence", "");
265 assertFalse(fms.matches(sf));
266 csq.put("Consequence", "junk");
267 assertFalse(fms.matches(sf));
270 * a string pattern matcher
272 fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "CSQ",
274 fms = new FeatureMatcherSet();
276 assertFalse(fms.matches(sf));
277 csq.put("PolyPhen", "damaging");
278 assertFalse(fms.matches(sf));
279 csq.put("Consequence", "damaging");
280 assertFalse(fms.matches(sf));
281 csq.put("Consequence", "Catastrophic");
282 assertTrue(fms.matches(sf));