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 names,
91 * and the min/max of any numerical values
96 void addInstance(String desc, String value)
104 float f = Float.valueOf(value);
105 min = hasValue ? Float.min(min, f) : f;
106 max = hasValue ? Float.max(max, f) : f;
108 type = (type == null || type == Datatype.Number) ? Datatype.Number
110 } catch (NumberFormatException e)
112 // not a number, ignore for min-max purposes
113 type = (type == null || type == Datatype.Character)
121 * Answers the description of the attribute, if recorded and unique, or null if either no, or more than description is recorded
124 public String getDescription()
126 if (description != null && description.size() == 1)
128 return description.get(0);
133 public Datatype getType()
139 * Adds the given description to the list of known descriptions (without
144 public void addDescription(String desc)
148 if (description == null)
150 description = new ArrayList<>();
152 if (!description.contains(desc))
154 description.add(desc);
161 * Answers the singleton instance of this class
165 public static FeatureAttributes getInstance()
170 private FeatureAttributes()
172 attributes = new HashMap<>();
176 * Answers the attribute names known for the given feature type, in
177 * alphabetical order (not case sensitive), or an empty set if no attributes
178 * are known. An attribute name is typically 'simple' e.g. "AC", but may be
179 * 'compound' e.g. {"CSQ", "Allele"} where a feature has map-valued attributes
184 public List<String[]> getAttributes(String featureType)
186 if (!attributes.containsKey(featureType))
188 return Collections.<String[]> emptyList();
191 return new ArrayList<>(attributes.get(featureType).keySet());
195 * Answers true if at least one attribute is known for the given feature type,
201 public boolean hasAttributes(String featureType)
203 if (attributes.containsKey(featureType))
205 if (!attributes.get(featureType).isEmpty())
214 * Records the given attribute name and description for the given feature
215 * type, and updates the min-max for any numeric value
222 public void addAttribute(String featureType, String description,
223 Object value, String... attName)
225 if (featureType == null || attName == null)
231 * if attribute value is a map, drill down one more level to
232 * record its sub-fields
234 if (value instanceof Map<?, ?>)
236 for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet())
238 String[] attNames = new String[attName.length + 1];
239 System.arraycopy(attName, 0, attNames, 0, attName.length);
240 attNames[attName.length] = entry.getKey().toString();
241 addAttribute(featureType, description, entry.getValue(), attNames);
246 String valueAsString = value.toString();
247 Map<String[], AttributeData> atts = attributes.get(featureType);
250 atts = new TreeMap<>(comparator);
251 attributes.put(featureType, atts);
253 AttributeData attData = atts.get(attName);
256 attData = new AttributeData();
257 atts.put(attName, attData);
259 attData.addInstance(description, valueAsString);
263 * Answers the description of the given attribute for the given feature type,
264 * if known and unique, else null
270 public String getDescription(String featureType, String... attName)
273 Map<String[], AttributeData> atts = attributes.get(featureType);
276 AttributeData attData = atts.get(attName);
279 desc = attData.getDescription();
286 * Answers the [min, max] value range of the given attribute for the given
287 * feature type, if known, else null. Attributes which only have text values
288 * would normally return null, however text values which happen to be numeric
289 * could result in a 'min-max' range.
295 public float[] getMinMax(String featureType, String... attName)
297 Map<String[], AttributeData> atts = attributes.get(featureType);
300 AttributeData attData = atts.get(attName);
301 if (attData != null && attData.hasValue)
303 return new float[] { attData.min, attData.max };
310 * Records the given attribute description for the given feature type
316 public void addDescription(String featureType, String description,
319 if (featureType == null || attName == null)
324 Map<String[], AttributeData> atts = attributes.get(featureType);
327 atts = new TreeMap<>(comparator);
328 attributes.put(featureType, atts);
330 AttributeData attData = atts.get(attName);
333 attData = new AttributeData();
334 atts.put(attName, attData);
336 attData.addDescription(description);
340 * Answers the datatype of the feature, which is one of Character, Number or
341 * Mixed (or null if not known), as discovered from values recorded.
347 public Datatype getDatatype(String featureType, String... attName)
349 Map<String[], AttributeData> atts = attributes.get(featureType);
352 AttributeData attData = atts.get(attName);
355 return attData.getType();