1 package jalview.datamodel.features;
3 import jalview.datamodel.SequenceFeature;
4 import jalview.util.matcher.Condition;
5 import jalview.util.matcher.Matcher;
6 import jalview.util.matcher.MatcherI;
9 * An immutable class that models one or more match conditions, each of which is
10 * applied to the value obtained by lookup given the match key.
12 * For example, the value provider could be a SequenceFeature's attributes map,
13 * and the conditions might be
15 * <li>CSQ contains "pathological"</li>
17 * <li>AF <= 1.0e-5</li>
23 public class FeatureMatcher implements FeatureMatcherI
25 private static final String COLON = ":";
28 * if true, match is against feature description
30 final private boolean byLabel;
33 * if true, match is against feature score
35 final private boolean byScore;
38 * if not null, match is against feature attribute [sub-attribute]
40 final private String[] key;
42 final private MatcherI matcher;
45 * A factory constructor method for a matcher that applies its match condition
46 * to the feature label (description)
52 public static FeatureMatcher byLabel(Condition cond, String pattern)
54 return new FeatureMatcher(new Matcher(cond, pattern), true, false,
59 * A factory constructor method for a matcher that applies its match condition
60 * to the feature score
66 public static FeatureMatcher byScore(Condition cond, String pattern)
68 return new FeatureMatcher(new Matcher(cond, pattern), false, true,
73 * A factory constructor method for a matcher that applies its match condition
74 * to the named feature attribute [and optional sub-attribute]
81 public static FeatureMatcher byAttribute(Condition cond, String pattern,
84 return new FeatureMatcher(new Matcher(cond, pattern), false, false,
88 private FeatureMatcher(Matcher m, boolean forLabel, boolean forScore,
97 public boolean matches(SequenceFeature feature)
99 String value = byLabel ? feature.getDescription()
100 : (byScore ? String.valueOf(feature.getScore())
101 : feature.getValueAsString(key));
102 return matcher.matches(value);
106 public String[] getKey()
112 public MatcherI getMatcher()
118 * Answers a string description of this matcher, suitable for display, debugging
119 * or logging. The format may change in future.
122 public String toString()
124 StringBuilder sb = new StringBuilder();
125 sb.append(String.join(COLON, key)).append(" ")
126 .append(matcher.getCondition().toString());
127 Condition condition = matcher.getCondition();
128 if (condition.isNumeric())
130 sb.append(" ").append(matcher.getPattern());
132 else if (condition.needsAPattern())
134 sb.append(" '").append(matcher.getPattern()).append("'");
137 return sb.toString();