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 |
|
|
|
|
| 98.7% |
Uncovered Elements: 2 (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 |
32 |
public static String[] fromAttributeDisplayName(String attribute)... |
89 |
|
{ |
90 |
32 |
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 |
43 |
public static String toAttributeDisplayName(String[] attName)... |
101 |
|
{ |
102 |
43 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (71) |
Complexity: 14 |
Complexity Density: 0.29 |
|
120 |
46 |
public static FeatureMatcher fromString(final String descriptor)... |
121 |
|
{ |
122 |
46 |
String invalidFormat = "Invalid matcher format: " + descriptor; |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
46 |
String attName = null; |
132 |
46 |
boolean byScore = false; |
133 |
46 |
boolean byLabel = false; |
134 |
46 |
Condition cond = null; |
135 |
46 |
String pattern = null; |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
46 |
String leftToParse = descriptor; |
142 |
46 |
String firstField = null; |
143 |
|
|
144 |
46 |
if (descriptor.startsWith(QUOTE)) |
145 |
|
{ |
146 |
|
|
147 |
13 |
int nextQuotePos = descriptor.indexOf(QUOTE, 1); |
148 |
13 |
if (nextQuotePos == -1) |
149 |
|
{ |
150 |
1 |
jalview.bin.Console.errPrintln(invalidFormat); |
151 |
1 |
return null; |
152 |
|
} |
153 |
12 |
firstField = descriptor.substring(1, nextQuotePos); |
154 |
12 |
leftToParse = descriptor.substring(nextQuotePos + 1).trim(); |
155 |
|
} |
156 |
|
else |
157 |
|
{ |
158 |
|
|
159 |
33 |
int nextSpacePos = descriptor.indexOf(SPACE); |
160 |
33 |
if (nextSpacePos == -1) |
161 |
|
{ |
162 |
2 |
jalview.bin.Console.errPrintln(invalidFormat); |
163 |
2 |
return null; |
164 |
|
} |
165 |
31 |
firstField = descriptor.substring(0, nextSpacePos); |
166 |
31 |
leftToParse = descriptor.substring(nextSpacePos + 1).trim(); |
167 |
|
} |
168 |
43 |
String lower = firstField.toLowerCase(Locale.ROOT); |
169 |
43 |
if (lower.startsWith(LABEL.toLowerCase(Locale.ROOT))) |
170 |
|
{ |
171 |
5 |
byLabel = true; |
172 |
|
} |
173 |
38 |
else if (lower.startsWith(SCORE.toLowerCase(Locale.ROOT))) |
174 |
|
{ |
175 |
8 |
byScore = true; |
176 |
|
} |
177 |
|
else |
178 |
|
{ |
179 |
30 |
attName = firstField; |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
43 |
int nextSpacePos = leftToParse.indexOf(SPACE); |
188 |
43 |
if (nextSpacePos == -1) |
189 |
|
{ |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
3 |
cond = Condition.fromString(leftToParse); |
194 |
3 |
if (cond == null || cond.needsAPattern()) |
195 |
|
{ |
196 |
2 |
jalview.bin.Console.errPrintln(invalidFormat); |
197 |
2 |
return null; |
198 |
|
} |
199 |
|
} |
200 |
|
else |
201 |
|
{ |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
40 |
cond = Condition.fromString(leftToParse.substring(0, nextSpacePos)); |
206 |
40 |
leftToParse = leftToParse.substring(nextSpacePos + 1).trim(); |
207 |
40 |
if (leftToParse.startsWith(QUOTE)) |
208 |
|
{ |
209 |
|
|
210 |
11 |
if (leftToParse.endsWith(QUOTE)) |
211 |
|
{ |
212 |
9 |
pattern = leftToParse.substring(1, leftToParse.length() - 1); |
213 |
|
} |
214 |
|
else |
215 |
|
{ |
216 |
|
|
217 |
2 |
jalview.bin.Console.errPrintln(invalidFormat); |
218 |
2 |
return null; |
219 |
|
} |
220 |
|
} |
221 |
|
else |
222 |
|
{ |
223 |
|
|
224 |
29 |
pattern = leftToParse; |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
39 |
try |
233 |
|
{ |
234 |
39 |
if (byLabel) |
235 |
|
{ |
236 |
5 |
return FeatureMatcher.byLabel(cond, pattern); |
237 |
|
} |
238 |
34 |
else if (byScore) |
239 |
|
{ |
240 |
6 |
return FeatureMatcher.byScore(cond, pattern); |
241 |
|
} |
242 |
|
else |
243 |
|
{ |
244 |
28 |
String[] attNames = FeatureMatcher |
245 |
|
.fromAttributeDisplayName(attName); |
246 |
28 |
return FeatureMatcher.byAttribute(cond, pattern, attNames); |
247 |
|
} |
248 |
|
} catch (NumberFormatException e) |
249 |
|
{ |
250 |
|
|
251 |
2 |
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 |
31 |
public static FeatureMatcher byLabel(Condition cond, String pattern)... |
266 |
|
{ |
267 |
31 |
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 |
37 |
public static FeatureMatcher byScore(Condition cond, String pattern)... |
282 |
|
{ |
283 |
37 |
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 |
75 |
public static FeatureMatcher byAttribute(Condition cond, String pattern,... |
299 |
|
String... attName) |
300 |
|
{ |
301 |
75 |
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 |
141 |
private FeatureMatcher(Matcher m, boolean forLabel, boolean forScore,... |
306 |
|
String[] theKey) |
307 |
|
{ |
308 |
141 |
key = theKey; |
309 |
141 |
matcher = m; |
310 |
141 |
byLabel = forLabel; |
311 |
141 |
byScore = forScore; |
312 |
|
} |
313 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 3 |
Complexity Density: 1.5 |
|
314 |
94 |
@Override... |
315 |
|
public boolean matches(SequenceFeature feature) |
316 |
|
{ |
317 |
94 |
String value = byLabel ? feature.getDescription() |
318 |
81 |
: (byScore ? String.valueOf(feature.getScore()) |
319 |
|
: feature.getValueAsString(key)); |
320 |
94 |
return matcher.matches(value); |
321 |
|
} |
322 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
323 |
31 |
@Override... |
324 |
|
public String[] getAttribute() |
325 |
|
{ |
326 |
31 |
return key; |
327 |
|
} |
328 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
329 |
39 |
@Override... |
330 |
|
public MatcherI getMatcher() |
331 |
|
{ |
332 |
39 |
return matcher; |
333 |
|
} |
334 |
|
|
335 |
|
|
336 |
|
|
337 |
|
|
338 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 5 |
Complexity Density: 0.38 |
|
339 |
13 |
@Override... |
340 |
|
public String toString() |
341 |
|
{ |
342 |
13 |
StringBuilder sb = new StringBuilder(); |
343 |
13 |
if (byLabel) |
344 |
|
{ |
345 |
1 |
sb.append(MessageManager.getString("label.label")); |
346 |
|
} |
347 |
12 |
else if (byScore) |
348 |
|
{ |
349 |
1 |
sb.append(MessageManager.getString("label.score")); |
350 |
|
} |
351 |
|
else |
352 |
|
{ |
353 |
11 |
sb.append(String.join(COLON, key)); |
354 |
|
} |
355 |
|
|
356 |
13 |
Condition condition = matcher.getCondition(); |
357 |
13 |
sb.append(SPACE).append(condition.toString().toLowerCase(Locale.ROOT)); |
358 |
13 |
if (condition.isNumeric()) |
359 |
|
{ |
360 |
7 |
sb.append(SPACE).append(matcher.getPattern()); |
361 |
|
} |
362 |
6 |
else if (condition.needsAPattern()) |
363 |
|
{ |
364 |
4 |
sb.append(" '").append(matcher.getPattern()).append(QUOTE); |
365 |
|
} |
366 |
|
|
367 |
13 |
return sb.toString(); |
368 |
|
} |
369 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
370 |
19 |
@Override... |
371 |
|
public boolean isByLabel() |
372 |
|
{ |
373 |
19 |
return byLabel; |
374 |
|
} |
375 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
376 |
17 |
@Override... |
377 |
|
public boolean isByScore() |
378 |
|
{ |
379 |
17 |
return byScore; |
380 |
|
} |
381 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
382 |
13 |
@Override... |
383 |
|
public boolean isByAttribute() |
384 |
|
{ |
385 |
13 |
return getAttribute() != null; |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
|
@inheritDoc |
390 |
|
|
391 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 6 |
Complexity Density: 0.35 |
|
392 |
47 |
@Override... |
393 |
|
public String toStableString() |
394 |
|
{ |
395 |
47 |
StringBuilder sb = new StringBuilder(); |
396 |
47 |
if (byLabel) |
397 |
|
{ |
398 |
5 |
sb.append(LABEL); |
399 |
|
} |
400 |
42 |
else if (byScore) |
401 |
|
{ |
402 |
6 |
sb.append(SCORE); |
403 |
|
} |
404 |
|
else |
405 |
|
{ |
406 |
|
|
407 |
|
|
408 |
|
|
409 |
36 |
String displayName = toAttributeDisplayName(key); |
410 |
36 |
if (displayName.contains(SPACE)) |
411 |
|
{ |
412 |
3 |
sb.append(QUOTE).append(displayName).append(QUOTE); |
413 |
|
} |
414 |
|
else |
415 |
|
{ |
416 |
33 |
sb.append(displayName); |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
47 |
Condition condition = matcher.getCondition(); |
421 |
47 |
sb.append(SPACE).append(condition.getStableName()); |
422 |
47 |
String pattern = matcher.getPattern(); |
423 |
47 |
if (condition.needsAPattern()) |
424 |
|
{ |
425 |
|
|
426 |
|
|
427 |
|
|
428 |
43 |
if (pattern.contains(SPACE)) |
429 |
|
{ |
430 |
4 |
sb.append(SPACE).append(QUOTE).append(pattern).append(QUOTE); |
431 |
|
} |
432 |
|
else |
433 |
|
{ |
434 |
39 |
sb.append(SPACE).append(pattern); |
435 |
|
} |
436 |
|
} |
437 |
|
|
438 |
47 |
return sb.toString(); |
439 |
|
} |
440 |
|
} |