All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ControlledVocabulary.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: Marc Sturm, Andreas Bertsch, Mathias Walzer $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/DATASTRUCTURES/ListUtils.h> // StringList
14 
15 #include <set>
16 #include <map>
17 
18 namespace OpenMS
19 {
28  class OPENMS_DLLAPI ControlledVocabulary
29  {
30  friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const ControlledVocabulary& cv);
31 
32 public:
34  struct FNV1aHasher
35  {
36  size_t operator()(const String& key) const noexcept
37  {
38  size_t hash = 14695981039346656037ull;
39  for (auto c : key)
40  {
41  hash ^= static_cast<unsigned char>(c);
42  hash *= 1099511628211ull;
43  }
44  return hash;
45  }
46  };
47 
49  struct OPENMS_DLLAPI CVTerm
50  {
52  enum XRefType
53  {
54  XSD_STRING = 0, // xsd:string A string
55  XSD_INTEGER, // xsd:integer Any integer
56  XSD_DECIMAL, // xsd:decimal Any real number
57  XSD_NEGATIVE_INTEGER, // xsd:negativeInteger Any negative integer
58  XSD_POSITIVE_INTEGER, // xsd:positiveInteger Any integer > 0
59  XSD_NON_NEGATIVE_INTEGER, // xsd:nonNegativeInteger Any integer >= 0
60  XSD_NON_POSITIVE_INTEGER, // xsd:nonPositiveInteger Any integer < 0
61  XSD_BOOLEAN, // xsd:boolean True or false
62  XSD_DATE, // xsd:date An XML-Schema date
63  XSD_ANYURI, // xsd:anyURI uniform resource identifier
64  NONE
65  };
66 
68  //static bool isSearchEngineSpecificScore();
70 
73  std::set<String> parents;
74  std::set<String> children;
75  bool obsolete;
81  std::set<String> units;
82 
84  CVTerm();
85 
86  CVTerm(const CVTerm& rhs);
87 
88  CVTerm& operator=(const CVTerm& rhs);
89 
91  String toXMLString(const String& ref, const String& value = String("")) const;
92 
94  String toXMLString(const String& ref, const DataValue& value) const;
95 
96  };
97 
100 
103 
105  const String& name() const;
106 
108  const String& label() const;
109 
111  const String& version() const;
112 
114  const String& url() const;
115 
122  void loadFromOBO(const String& name, const String& filename);
123 
125  bool exists(const String& id) const;
126 
128  bool hasTermWithName(const String& name) const;
129 
135  const CVTerm& getTerm(const String& id) const;
136 
142  const CVTerm& getTermByName(const String& name, const String& desc = "") const;
143 
144 
146  const std::map<String, CVTerm>& getTerms() const;
147 
155  void getAllChildTerms(std::set<String>& terms, const String& parent_id) const;
156 
168  template <class LAMBDA>
169  bool iterateAllChildren(const String& parent_id, LAMBDA lbd) const
170  {
171  for (const auto& child_id : getTerm(parent_id).children)
172  {
173  if (lbd(child_id) || iterateAllChildren(child_id, lbd))
174  return true;
175  }
176  return false;
177  }
178 
185 
191  bool isChildOf(const String& child_id, const String& parent_id) const;
192 
193 
209 
210 protected:
216  bool checkName_(const String& id, const String& name, bool ignore_case = true) const;
217 
219  // note: unordered_map would be faster (5% for loading mzML), but order differs across platforms
220  std::map<String, CVTerm> terms_;
222  std::map<String, String> namesToIds_;
231  };
232 
234  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const ControlledVocabulary& cv);
235 
236 
237 } // namespace OpenMS
238 
Definition: ControlledVocabulary.h:29
ControlledVocabulary()
Constructor.
bool checkName_(const String &id, const String &name, bool ignore_case=true) const
checks if a name corresponds to an id
static const ControlledVocabulary & getPSIMSCV()
Returns a CV for parsing/storing PSI-MS related data, e.g. mzML, or handle accessions/ids in datastru...
bool hasTermWithName(const String &name) const
Returns true if a term with the given name is in the CV. Returns false otherwise.
const CVTerm & getTerm(const String &id) const
Returns a term specified by ID.
bool iterateAllChildren(const String &parent_id, LAMBDA lbd) const
Iterates over all children (incl. subchildren etc) of parent recursively, i.e. the whole subtree.
Definition: ControlledVocabulary.h:169
const String & label() const
Returns the CV label (set in the load method)
void loadFromOBO(const String &name, const String &filename)
Loads the CV from an OBO file.
String name_
Name set in the load method.
Definition: ControlledVocabulary.h:224
std::map< String, String > namesToIds_
Map from name to id.
Definition: ControlledVocabulary.h:222
String version_
CV version.
Definition: ControlledVocabulary.h:228
const CVTerm & getTermByName(const String &name, const String &desc="") const
Returns a term specified by name.
bool isChildOf(const String &child_id, const String &parent_id) const
Returns if child is a child of parent.
const String & url() const
Returns the CV url (set in the load method)
virtual ~ControlledVocabulary()
Destructor.
std::map< String, CVTerm > terms_
Map from ID to CVTerm.
Definition: ControlledVocabulary.h:220
void getAllChildTerms(std::set< String > &terms, const String &parent_id) const
Writes all child terms recursively into terms.
String label_
CV label.
Definition: ControlledVocabulary.h:226
const ControlledVocabulary::CVTerm * checkAndGetTermByName(const OpenMS::String &name) const
Searches the existing terms for the given name.
const String & name() const
Returns the CV name (set in the load method)
const String & version() const
Returns the CV version (set in the load method)
const std::map< String, CVTerm > & getTerms() const
returns all the terms stored in the CV
bool exists(const String &id) const
Returns true if the term is in the CV. Returns false otherwise.
String url_
CV URL.
Definition: ControlledVocabulary.h:230
friend std::ostream & operator<<(std::ostream &os, const ControlledVocabulary &cv)
Print the contents to a stream.
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:33
A more convenient string class.
Definition: String.h:34
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:44
const double c
Definition: Constants.h:188
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
Representation of a CV term.
Definition: ControlledVocabulary.h:50
std::set< String > parents
The parent IDs.
Definition: ControlledVocabulary.h:73
String toXMLString(const String &ref, const DataValue &value) const
get mzidentml formatted string. i.e. a cvparam xml element, ref should be the name of the ControlledV...
StringList unparsed
Unparsed lines from the definition file.
Definition: ControlledVocabulary.h:78
std::set< String > children
The child IDs.
Definition: ControlledVocabulary.h:74
StringList xref_binary
xref binary-data-type for the CV-term (list of all allowed data value types for the current binary da...
Definition: ControlledVocabulary.h:80
String id
Identifier.
Definition: ControlledVocabulary.h:72
String description
Term description.
Definition: ControlledVocabulary.h:76
bool obsolete
Flag that indicates of the term is obsolete.
Definition: ControlledVocabulary.h:75
static String getXRefTypeName(XRefType type)
String name
if it is a score type, lookup has_order
Definition: ControlledVocabulary.h:71
StringList synonyms
List of synonyms.
Definition: ControlledVocabulary.h:77
XRefType xref_type
xref value-type for the CV-term
Definition: ControlledVocabulary.h:79
std::set< String > units
unit accession ids, defined by relationship has units
Definition: ControlledVocabulary.h:81
String toXMLString(const String &ref, const String &value=String("")) const
get mzidentml formatted string. i.e. a cvparam xml element, ref should be the name of the ControlledV...
XRefType
define xsd types allowed in cv term to specify their value-type
Definition: ControlledVocabulary.h:53
@ XSD_BOOLEAN
Definition: ControlledVocabulary.h:61
@ XSD_DATE
Definition: ControlledVocabulary.h:62
@ XSD_NEGATIVE_INTEGER
Definition: ControlledVocabulary.h:57
@ XSD_POSITIVE_INTEGER
Definition: ControlledVocabulary.h:58
@ XSD_NON_POSITIVE_INTEGER
Definition: ControlledVocabulary.h:60
@ XSD_DECIMAL
Definition: ControlledVocabulary.h:56
@ XSD_NON_NEGATIVE_INTEGER
Definition: ControlledVocabulary.h:59
@ XSD_ANYURI
Definition: ControlledVocabulary.h:63
@ XSD_INTEGER
Definition: ControlledVocabulary.h:55
CVTerm & operator=(const CVTerm &rhs)
static bool isHigherBetterScore(ControlledVocabulary::CVTerm term)
ensure same hash on all platforms (for reproducibility)-
Definition: ControlledVocabulary.h:35
size_t operator()(const String &key) const noexcept
Definition: ControlledVocabulary.h:36