1 package jalview.datamodel.features;
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;
10 * An immutable class that models one or more match conditions, each of which is
11 * applied to the value obtained by lookup given the match key.
13 * For example, the value provider could be a SequenceFeature's attributes map,
14 * and the conditions might be
16 * <li>CSQ contains "pathological"</li>
18 * <li>AF <= 1.0e-5</li>
24 public class FeatureMatcher implements FeatureMatcherI
27 * a dummy matcher that comes in useful for the 'add a filter' gui row
29 public static final FeatureMatcherI NULL_MATCHER = FeatureMatcher
30 .byLabel(Condition.values()[0], "");
32 private static final String COLON = ":";
35 * if true, match is against feature description
37 final private boolean byLabel;
40 * if true, match is against feature score
42 final private boolean byScore;
45 * if not null, match is against feature attribute [sub-attribute]
47 final private String[] key;
49 final private MatcherI matcher;
52 * A factory constructor method for a matcher that applies its match condition
53 * to the feature label (description)
59 public static FeatureMatcher byLabel(Condition cond, String pattern)
61 return new FeatureMatcher(new Matcher(cond, pattern), true, false,
66 * A factory constructor method for a matcher that applies its match condition
67 * to the feature score
73 public static FeatureMatcher byScore(Condition cond, String pattern)
75 return new FeatureMatcher(new Matcher(cond, pattern), false, true,
80 * A factory constructor method for a matcher that applies its match condition
81 * to the named feature attribute [and optional sub-attribute]
88 public static FeatureMatcher byAttribute(Condition cond, String pattern,
91 return new FeatureMatcher(new Matcher(cond, pattern), false, false,
95 private FeatureMatcher(Matcher m, boolean forLabel, boolean forScore,
104 public boolean matches(SequenceFeature feature)
106 String value = byLabel ? feature.getDescription()
107 : (byScore ? String.valueOf(feature.getScore())
108 : feature.getValueAsString(key));
109 return matcher.matches(value);
113 public String[] getAttribute()
119 public MatcherI getMatcher()
125 * Answers a string description of this matcher, suitable for display, debugging
126 * or logging. The format may change in future.
129 public String toString()
131 StringBuilder sb = new StringBuilder();
134 sb.append(MessageManager.getString("label.label"));
138 sb.append(MessageManager.getString("label.score"));
142 sb.append(String.join(COLON, key));
145 Condition condition = matcher.getCondition();
146 sb.append(" ").append(condition.toString().toLowerCase());
147 if (condition.isNumeric())
149 sb.append(" ").append(matcher.getPattern());
151 else if (condition.needsAPattern())
153 sb.append(" '").append(matcher.getPattern()).append("'");
156 return sb.toString();
160 public boolean isByLabel()
166 public boolean isByScore()