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