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