Merge branch 'develop' into features/JAL-1793VCF
[jalview.git] / test / jalview / datamodel / features / FeatureMatcherTest.java
1 package jalview.datamodel.features;
2
3 import static org.testng.Assert.assertEquals;
4 import static org.testng.Assert.assertFalse;
5 import static org.testng.Assert.assertNull;
6 import static org.testng.Assert.assertTrue;
7
8 import jalview.datamodel.SequenceFeature;
9 import jalview.util.MessageManager;
10 import jalview.util.matcher.Condition;
11
12 import java.util.Locale;
13
14 import org.testng.annotations.Test;
15
16 public class FeatureMatcherTest
17 {
18   @Test
19   public void testMatches_byLabel()
20   {
21     SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
22             12, "grp");
23
24     /*
25      * contains - not case sensitive
26      */
27     assertTrue(
28             FeatureMatcher.byLabel(Condition.Contains, "IS").matches(sf));
29     assertTrue(FeatureMatcher.byLabel(Condition.Contains, "").matches(sf));
30     assertFalse(
31             FeatureMatcher.byLabel(Condition.Contains, "ISNT").matches(sf));
32
33     /*
34      * does not contain
35      */
36     assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "isnt")
37             .matches(sf));
38     assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "is")
39             .matches(sf));
40
41     /*
42      * matches
43      */
44     assertTrue(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY label")
45             .matches(sf));
46     assertFalse(FeatureMatcher.byLabel(Condition.Matches, "THIS is MY")
47             .matches(sf));
48
49     /*
50      * does not match
51      */
52     assertFalse(FeatureMatcher
53             .byLabel(Condition.NotMatches, "THIS is MY label").matches(sf));
54     assertTrue(FeatureMatcher.byLabel(Condition.NotMatches, "THIS is MY")
55             .matches(sf));
56
57     /*
58      * is present / not present
59      */
60     assertTrue(FeatureMatcher.byLabel(Condition.Present, "").matches(sf));
61     assertFalse(
62             FeatureMatcher.byLabel(Condition.NotPresent, "").matches(sf));
63   }
64
65   @Test
66   public void testMatches_byScore()
67   {
68     SequenceFeature sf = new SequenceFeature("Cath", "this is my label", 11,
69             12, 3.2f, "grp");
70
71     assertTrue(FeatureMatcher.byScore(Condition.LT, "3.3").matches(sf));
72     assertFalse(FeatureMatcher.byScore(Condition.LT, "3.2").matches(sf));
73     assertFalse(FeatureMatcher.byScore(Condition.LT, "2.2").matches(sf));
74
75     assertTrue(FeatureMatcher.byScore(Condition.LE, "3.3").matches(sf));
76     assertTrue(FeatureMatcher.byScore(Condition.LE, "3.2").matches(sf));
77     assertFalse(FeatureMatcher.byScore(Condition.LE, "2.2").matches(sf));
78
79     assertFalse(FeatureMatcher.byScore(Condition.EQ, "3.3").matches(sf));
80     assertTrue(FeatureMatcher.byScore(Condition.EQ, "3.2").matches(sf));
81
82     assertFalse(FeatureMatcher.byScore(Condition.GE, "3.3").matches(sf));
83     assertTrue(FeatureMatcher.byScore(Condition.GE, "3.2").matches(sf));
84     assertTrue(FeatureMatcher.byScore(Condition.GE, "2.2").matches(sf));
85
86     assertFalse(FeatureMatcher.byScore(Condition.GT, "3.3").matches(sf));
87     assertFalse(FeatureMatcher.byScore(Condition.GT, "3.2").matches(sf));
88     assertTrue(FeatureMatcher.byScore(Condition.GT, "2.2").matches(sf));
89   }
90   @Test
91   public void testMatches_byAttribute()
92   {
93     /*
94      * a numeric matcher - MatcherTest covers more conditions
95      */
96     FeatureMatcherI fm = FeatureMatcher
97             .byAttribute(Condition.GE, "-2", "AF");
98     SequenceFeature sf = new SequenceFeature("Cath", "desc", 11, 12, "grp");
99     assertFalse(fm.matches(sf));
100     sf.setValue("AF", "foobar");
101     assertFalse(fm.matches(sf));
102     sf.setValue("AF", "-2");
103     assertTrue(fm.matches(sf));
104     sf.setValue("AF", "-1");
105     assertTrue(fm.matches(sf));
106     sf.setValue("AF", "-3");
107     assertFalse(fm.matches(sf));
108     sf.setValue("AF", "");
109     assertFalse(fm.matches(sf));
110
111     /*
112      * a string pattern matcher
113      */
114     fm = FeatureMatcher.byAttribute(Condition.Contains, "Cat", "AF");
115     assertFalse(fm.matches(sf));
116     sf.setValue("AF", "raining cats and dogs");
117     assertTrue(fm.matches(sf));
118
119     fm = FeatureMatcher.byAttribute(Condition.Present, "", "AC");
120     assertFalse(fm.matches(sf));
121     sf.setValue("AC", "21");
122     assertTrue(fm.matches(sf));
123
124     fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AC_Females");
125     assertTrue(fm.matches(sf));
126     sf.setValue("AC_Females", "21");
127     assertFalse(fm.matches(sf));
128   }
129
130   @Test
131   public void testToString()
132   {
133     Locale.setDefault(Locale.ENGLISH);
134
135     /*
136      * toString uses the i18n translation of the enum conditions
137      */
138     FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.LT, "1.2",
139             "AF");
140     assertEquals(fm.toString(), "AF < 1.2");
141
142     /*
143      * Present / NotPresent omit the value pattern
144      */
145     fm = FeatureMatcher.byAttribute(Condition.Present, "", "AF");
146     assertEquals(fm.toString(), "AF is present");
147     fm = FeatureMatcher.byAttribute(Condition.NotPresent, "", "AF");
148     assertEquals(fm.toString(), "AF is not present");
149
150     /*
151      * by Label
152      */
153     fm = FeatureMatcher.byLabel(Condition.Matches, "foobar");
154     assertEquals(fm.toString(),
155             MessageManager.getString("label.label") + " matches 'foobar'");
156
157     /*
158      * by Score
159      */
160     fm = FeatureMatcher.byScore(Condition.GE, "12.2");
161     assertEquals(fm.toString(),
162             MessageManager.getString("label.score") + " >= 12.2");
163   }
164
165   @Test
166   public void testGetAttribute()
167   {
168     FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2",
169             "AF");
170     assertEquals(fm.getAttribute(), new String[] { "AF" });
171
172     /*
173      * compound key (attribute / subattribute)
174      */
175     fm = FeatureMatcher.byAttribute(Condition.GE, "-2F", "CSQ",
176             "Consequence");
177     assertEquals(fm.getAttribute(), new String[] { "CSQ", "Consequence" });
178
179     /*
180      * answers null if match is by Label or by Score
181      */
182     assertNull(FeatureMatcher.byLabel(Condition.NotContains, "foo")
183             .getAttribute());
184     assertNull(FeatureMatcher.byScore(Condition.LE, "-1").getAttribute());
185   }
186
187   @Test
188   public void testIsByLabel()
189   {
190     assertTrue(FeatureMatcher.byLabel(Condition.NotContains, "foo")
191             .isByLabel());
192     assertFalse(FeatureMatcher.byScore(Condition.LE, "-1").isByLabel());
193     assertFalse(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
194             .isByLabel());
195   }
196
197   @Test
198   public void testIsByScore()
199   {
200     assertFalse(FeatureMatcher.byLabel(Condition.NotContains, "foo")
201             .isByScore());
202     assertTrue(FeatureMatcher.byScore(Condition.LE, "-1").isByScore());
203     assertFalse(FeatureMatcher.byAttribute(Condition.LE, "-1", "AC")
204             .isByScore());
205   }
206
207   @Test
208   public void testGetMatcher()
209   {
210     FeatureMatcherI fm = FeatureMatcher.byAttribute(Condition.GE, "-2f",
211             "AF");
212     assertEquals(fm.getMatcher().getCondition(), Condition.GE);
213     assertEquals(fm.getMatcher().getFloatValue(), -2F);
214     assertEquals(fm.getMatcher().getPattern(), "-2.0");
215   }
216 }