1 |
|
package jalview.datamodel.features; |
2 |
|
|
3 |
|
import jalview.datamodel.SequenceFeature; |
4 |
|
import jalview.util.MessageManager; |
5 |
|
import jalview.util.matcher.Condition; |
6 |
|
import jalview.util.matcher.Matcher; |
7 |
|
import jalview.util.matcher.MatcherI; |
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
@author |
22 |
|
|
23 |
|
|
|
|
| 98.7% |
Uncovered Elements: 2 (158) |
Complexity: 41 |
Complexity Density: 0.43 |
|
24 |
|
public class FeatureMatcher implements FeatureMatcherI |
25 |
|
{ |
26 |
|
private static final String SCORE = "Score"; |
27 |
|
|
28 |
|
private static final String LABEL = "Label"; |
29 |
|
|
30 |
|
private static final String SPACE = " "; |
31 |
|
|
32 |
|
private static final String QUOTE = "'"; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
public static final FeatureMatcherI NULL_MATCHER = FeatureMatcher |
38 |
|
.byLabel(Condition.values()[0], ""); |
39 |
|
|
40 |
|
private static final String COLON = ":"; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
final private boolean byLabel; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
final private boolean byScore; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
final private String[] key; |
56 |
|
|
57 |
|
final private MatcherI matcher; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
@param |
64 |
|
@return |
65 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
66 |
32 |
public static String[] fromAttributeDisplayName(String attribute)... |
67 |
|
{ |
68 |
32 |
return attribute == null ? null : attribute.split(COLON); |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@param |
76 |
|
@return |
77 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
78 |
42 |
public static String toAttributeDisplayName(String[] attName)... |
79 |
|
{ |
80 |
42 |
return attName == null ? "" : String.join(COLON, attName); |
81 |
|
} |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@param |
96 |
|
@return |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (71) |
Complexity: 14 |
Complexity Density: 0.29 |
|
98 |
46 |
public static FeatureMatcher fromString(final String descriptor)... |
99 |
|
{ |
100 |
46 |
String invalidFormat = "Invalid matcher format: " + descriptor; |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
46 |
String attName = null; |
110 |
46 |
boolean byScore = false; |
111 |
46 |
boolean byLabel = false; |
112 |
46 |
Condition cond = null; |
113 |
46 |
String pattern = null; |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
46 |
String leftToParse = descriptor; |
120 |
46 |
String firstField = null; |
121 |
|
|
122 |
46 |
if (descriptor.startsWith(QUOTE)) |
123 |
|
{ |
124 |
|
|
125 |
13 |
int nextQuotePos = descriptor.indexOf(QUOTE, 1); |
126 |
13 |
if (nextQuotePos == -1) |
127 |
|
{ |
128 |
1 |
System.err.println(invalidFormat); |
129 |
1 |
return null; |
130 |
|
} |
131 |
12 |
firstField = descriptor.substring(1, nextQuotePos); |
132 |
12 |
leftToParse = descriptor.substring(nextQuotePos + 1).trim(); |
133 |
|
} |
134 |
|
else |
135 |
|
{ |
136 |
|
|
137 |
33 |
int nextSpacePos = descriptor.indexOf(SPACE); |
138 |
33 |
if (nextSpacePos == -1) |
139 |
|
{ |
140 |
2 |
System.err.println(invalidFormat); |
141 |
2 |
return null; |
142 |
|
} |
143 |
31 |
firstField = descriptor.substring(0, nextSpacePos); |
144 |
31 |
leftToParse = descriptor.substring(nextSpacePos + 1).trim(); |
145 |
|
} |
146 |
43 |
String lower = firstField.toLowerCase(); |
147 |
43 |
if (lower.startsWith(LABEL.toLowerCase())) |
148 |
|
{ |
149 |
5 |
byLabel = true; |
150 |
|
} |
151 |
38 |
else if (lower.startsWith(SCORE.toLowerCase())) |
152 |
|
{ |
153 |
8 |
byScore = true; |
154 |
|
} |
155 |
|
else |
156 |
|
{ |
157 |
30 |
attName = firstField; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
43 |
int nextSpacePos = leftToParse.indexOf(SPACE); |
166 |
43 |
if (nextSpacePos == -1) |
167 |
|
{ |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
3 |
cond = Condition.fromString(leftToParse); |
172 |
3 |
if (cond == null || cond.needsAPattern()) |
173 |
|
{ |
174 |
2 |
System.err.println(invalidFormat); |
175 |
2 |
return null; |
176 |
|
} |
177 |
|
} |
178 |
|
else |
179 |
|
{ |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
40 |
cond = Condition.fromString(leftToParse.substring(0, nextSpacePos)); |
184 |
40 |
leftToParse = leftToParse.substring(nextSpacePos + 1).trim(); |
185 |
40 |
if (leftToParse.startsWith(QUOTE)) |
186 |
|
{ |
187 |
|
|
188 |
11 |
if (leftToParse.endsWith(QUOTE)) |
189 |
|
{ |
190 |
9 |
pattern = leftToParse.substring(1, leftToParse.length() - 1); |
191 |
|
} |
192 |
|
else |
193 |
|
{ |
194 |
|
|
195 |
2 |
System.err.println(invalidFormat); |
196 |
2 |
return null; |
197 |
|
} |
198 |
|
} |
199 |
|
else |
200 |
|
{ |
201 |
|
|
202 |
29 |
pattern = leftToParse; |
203 |
|
} |
204 |
|
} |
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
210 |
39 |
try |
211 |
|
{ |
212 |
39 |
if (byLabel) |
213 |
|
{ |
214 |
5 |
return FeatureMatcher.byLabel(cond, pattern); |
215 |
|
} |
216 |
34 |
else if (byScore) |
217 |
|
{ |
218 |
6 |
return FeatureMatcher.byScore(cond, pattern); |
219 |
|
} |
220 |
|
else |
221 |
|
{ |
222 |
28 |
String[] attNames = FeatureMatcher |
223 |
|
.fromAttributeDisplayName(attName); |
224 |
28 |
return FeatureMatcher.byAttribute(cond, pattern, attNames); |
225 |
|
} |
226 |
|
} catch (NumberFormatException e) |
227 |
|
{ |
228 |
|
|
229 |
2 |
return null; |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
@param |
238 |
|
@param |
239 |
|
@return |
240 |
|
@throws |
241 |
|
|
242 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
243 |
31 |
public static FeatureMatcher byLabel(Condition cond, String pattern)... |
244 |
|
{ |
245 |
31 |
return new FeatureMatcher(new Matcher(cond, pattern), true, false, |
246 |
|
null); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
@param |
255 |
|
@return |
256 |
|
@throws |
257 |
|
|
258 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
259 |
36 |
public static FeatureMatcher byScore(Condition cond, String pattern)... |
260 |
|
{ |
261 |
36 |
return new FeatureMatcher(new Matcher(cond, pattern), false, true, |
262 |
|
null); |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
@param |
270 |
|
@param |
271 |
|
@param |
272 |
|
@return |
273 |
|
@throws |
274 |
|
|
275 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
276 |
72 |
public static FeatureMatcher byAttribute(Condition cond, String pattern,... |
277 |
|
String... attName) |
278 |
|
{ |
279 |
72 |
return new FeatureMatcher(new Matcher(cond, pattern), false, false, |
280 |
|
attName); |
281 |
|
} |
282 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
283 |
137 |
private FeatureMatcher(Matcher m, boolean forLabel, boolean forScore,... |
284 |
|
String[] theKey) |
285 |
|
{ |
286 |
137 |
key = theKey; |
287 |
137 |
matcher = m; |
288 |
137 |
byLabel = forLabel; |
289 |
137 |
byScore = forScore; |
290 |
|
} |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 1.5 |
|
291 |
87 |
@Override... |
292 |
|
public boolean matches(SequenceFeature feature) |
293 |
|
{ |
294 |
87 |
String value = byLabel ? feature.getDescription() |
295 |
74 |
: (byScore ? String.valueOf(feature.getScore()) |
296 |
|
: feature.getValueAsString(key)); |
297 |
87 |
return matcher.matches(value); |
298 |
|
} |
299 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
300 |
31 |
@Override... |
301 |
|
public String[] getAttribute() |
302 |
|
{ |
303 |
31 |
return key; |
304 |
|
} |
305 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
306 |
51 |
@Override... |
307 |
|
public MatcherI getMatcher() |
308 |
|
{ |
309 |
51 |
return matcher; |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 5 |
Complexity Density: 0.38 |
|
316 |
13 |
@Override... |
317 |
|
public String toString() |
318 |
|
{ |
319 |
13 |
StringBuilder sb = new StringBuilder(); |
320 |
13 |
if (byLabel) |
321 |
|
{ |
322 |
1 |
sb.append(MessageManager.getString("label.label")); |
323 |
|
} |
324 |
12 |
else if (byScore) |
325 |
|
{ |
326 |
1 |
sb.append(MessageManager.getString("label.score")); |
327 |
|
} |
328 |
|
else |
329 |
|
{ |
330 |
11 |
sb.append(String.join(COLON, key)); |
331 |
|
} |
332 |
|
|
333 |
13 |
Condition condition = matcher.getCondition(); |
334 |
13 |
sb.append(SPACE).append(condition.toString().toLowerCase()); |
335 |
13 |
if (condition.isNumeric()) |
336 |
|
{ |
337 |
7 |
sb.append(SPACE).append(matcher.getPattern()); |
338 |
|
} |
339 |
6 |
else if (condition.needsAPattern()) |
340 |
|
{ |
341 |
4 |
sb.append(" '").append(matcher.getPattern()).append(QUOTE); |
342 |
|
} |
343 |
|
|
344 |
13 |
return sb.toString(); |
345 |
|
} |
346 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
347 |
19 |
@Override... |
348 |
|
public boolean isByLabel() |
349 |
|
{ |
350 |
19 |
return byLabel; |
351 |
|
} |
352 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
353 |
17 |
@Override... |
354 |
|
public boolean isByScore() |
355 |
|
{ |
356 |
17 |
return byScore; |
357 |
|
} |
358 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
359 |
13 |
@Override... |
360 |
|
public boolean isByAttribute() |
361 |
|
{ |
362 |
13 |
return getAttribute() != null; |
363 |
|
} |
364 |
|
|
365 |
|
|
366 |
|
@inheritDoc |
367 |
|
|
368 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 6 |
Complexity Density: 0.35 |
|
369 |
46 |
@Override... |
370 |
|
public String toStableString() |
371 |
|
{ |
372 |
46 |
StringBuilder sb = new StringBuilder(); |
373 |
46 |
if (byLabel) |
374 |
|
{ |
375 |
5 |
sb.append(LABEL); |
376 |
|
} |
377 |
41 |
else if (byScore) |
378 |
|
{ |
379 |
6 |
sb.append(SCORE); |
380 |
|
} |
381 |
|
else |
382 |
|
{ |
383 |
|
|
384 |
|
|
385 |
|
|
386 |
35 |
String displayName = toAttributeDisplayName(key); |
387 |
35 |
if (displayName.contains(SPACE)) |
388 |
|
{ |
389 |
3 |
sb.append(QUOTE).append(displayName).append(QUOTE); |
390 |
|
} |
391 |
|
else |
392 |
|
{ |
393 |
32 |
sb.append(displayName); |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
46 |
Condition condition = matcher.getCondition(); |
398 |
46 |
sb.append(SPACE).append(condition.getStableName()); |
399 |
46 |
String pattern = matcher.getPattern(); |
400 |
46 |
if (condition.needsAPattern()) |
401 |
|
{ |
402 |
|
|
403 |
|
|
404 |
|
|
405 |
42 |
if (pattern.contains(SPACE)) |
406 |
|
{ |
407 |
4 |
sb.append(SPACE).append(QUOTE).append(pattern).append(QUOTE); |
408 |
|
} |
409 |
|
else |
410 |
|
{ |
411 |
38 |
sb.append(SPACE).append(pattern); |
412 |
|
} |
413 |
|
} |
414 |
|
|
415 |
46 |
return sb.toString(); |
416 |
|
} |
417 |
|
} |