1 package jalview.datamodel.features;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
9 import java.util.Map.Entry;
10 import java.util.TreeMap;
13 * A singleton class to hold the set of attributes known for each feature type
15 public class FeatureAttributes
19 Character, Number, Mixed
22 private static FeatureAttributes instance = new FeatureAttributes();
25 * map, by feature type, of a map, by attribute name, of
26 * attribute description and min-max range (if known)
28 private Map<String, Map<String[], AttributeData>> attributes;
31 * a case-insensitive comparator so that attributes are ordered e.g.
37 private Comparator<String[]> comparator = new Comparator<String[]>()
40 public int compare(String[] o1, String[] o2)
43 while (i < o1.length || i < o2.length)
47 return o1.length <= i ? 0 : 1;
53 int comp = String.CASE_INSENSITIVE_ORDER.compare(o1[i], o2[i]);
60 return 0; // same length and all matched
64 private class AttributeData
67 * description(s) for this attribute, if known
68 * (different feature source might have differing descriptions)
70 List<String> description;
73 * minimum value (of any numeric values recorded)
78 * maximum value (of any numeric values recorded)
83 * flag is set true if any numeric value is detected for this attribute
85 boolean hasValue = false;
90 * Note one instance of this attribute, recording unique, non-null
91 * descriptions, and the min/max of any numerical values
96 void addInstance(String desc, String value)
102 value = value.trim();
105 * Parse numeric value unless we have previously
106 * seen text data for this attribute type
108 if (type == null || type == Datatype.Number)
112 float f = Float.valueOf(value);
113 min = hasValue ? Math.min(min, f) : f;
114 max = hasValue ? Math.max(max, f) : f;
116 type = (type == null || type == Datatype.Number)
119 } catch (NumberFormatException e)
122 * non-numeric data: treat attribute as Character (or Mixed)
124 type = (type == null || type == Datatype.Character)
136 * Answers the description of the attribute, if recorded and unique, or null if either no, or more than description is recorded
139 public String getDescription()
141 if (description != null && description.size() == 1)
143 return description.get(0);
148 public Datatype getType()
154 * Adds the given description to the list of known descriptions (without
159 public void addDescription(String desc)
163 if (description == null)
165 description = new ArrayList<>();
167 if (!description.contains(desc))
169 description.add(desc);
176 * Answers the singleton instance of this class
180 public static FeatureAttributes getInstance()
185 private FeatureAttributes()
187 attributes = new HashMap<>();
191 * Answers the attribute names known for the given feature type, in
192 * alphabetical order (not case sensitive), or an empty set if no attributes
193 * are known. An attribute name is typically 'simple' e.g. "AC", but may be
194 * 'compound' e.g. {"CSQ", "Allele"} where a feature has map-valued attributes
199 public List<String[]> getAttributes(String featureType)
201 if (!attributes.containsKey(featureType))
203 return Collections.<String[]> emptyList();
206 return new ArrayList<>(attributes.get(featureType).keySet());
210 * Answers true if at least one attribute is known for the given feature type,
216 public boolean hasAttributes(String featureType)
218 if (attributes.containsKey(featureType))
220 if (!attributes.get(featureType).isEmpty())
229 * Records the given attribute name and description for the given feature
230 * type, and updates the min-max for any numeric value
237 public void addAttribute(String featureType, String description,
238 Object value, String... attName)
240 if (featureType == null || attName == null)
246 * if attribute value is a map, drill down one more level to
247 * record its sub-fields
249 if (value instanceof Map<?, ?>)
251 for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet())
253 String[] attNames = new String[attName.length + 1];
254 System.arraycopy(attName, 0, attNames, 0, attName.length);
255 attNames[attName.length] = entry.getKey().toString();
256 addAttribute(featureType, description, entry.getValue(), attNames);
261 String valueAsString = value.toString();
262 Map<String[], AttributeData> atts = attributes.get(featureType);
265 atts = new TreeMap<>(comparator);
266 attributes.put(featureType, atts);
268 AttributeData attData = atts.get(attName);
271 attData = new AttributeData();
272 atts.put(attName, attData);
274 attData.addInstance(description, valueAsString);
278 * Answers the description of the given attribute for the given feature type,
279 * if known and unique, else null
285 public String getDescription(String featureType, String... attName)
288 Map<String[], AttributeData> atts = attributes.get(featureType);
291 AttributeData attData = atts.get(attName);
294 desc = attData.getDescription();
301 * Answers the [min, max] value range of the given attribute for the given
302 * feature type, if known, else null. Attributes with a mixture of text and
303 * numeric values are considered text (do not return a min-max range).
309 public float[] getMinMax(String featureType, String... attName)
311 Map<String[], AttributeData> atts = attributes.get(featureType);
314 AttributeData attData = atts.get(attName);
315 if (attData != null && attData.hasValue)
317 return new float[] { attData.min, attData.max };
324 * Records the given attribute description for the given feature type
330 public void addDescription(String featureType, String description,
333 if (featureType == null || attName == null)
338 Map<String[], AttributeData> atts = attributes.get(featureType);
341 atts = new TreeMap<>(comparator);
342 attributes.put(featureType, atts);
344 AttributeData attData = atts.get(attName);
347 attData = new AttributeData();
348 atts.put(attName, attData);
350 attData.addDescription(description);
354 * Answers the datatype of the feature, which is one of Character, Number or
355 * Mixed (or null if not known), as discovered from values recorded.
361 public Datatype getDatatype(String featureType, String... attName)
363 Map<String[], AttributeData> atts = attributes.get(featureType);
366 AttributeData attData = atts.get(attName);
369 return attData.getType();