OpenMS
Loading...
Searching...
No Matches
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/TypeAliases.h> // StringList
14
15#include <set>
16#include <map>
17#include <string_view>
18#include <unordered_map>
19
20namespace OpenMS
21{
30 class OPENMS_DLLAPI ControlledVocabulary
31 {
32 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const ControlledVocabulary& cv);
33
34public:
37 {
38 size_t operator()(const std::string& key) const noexcept
39 {
40 size_t hash = 14695981039346656037ull;
41 for (auto c : key)
42 {
43 hash ^= static_cast<unsigned char>(c);
44 hash *= 1099511628211ull;
45 }
46 return hash;
47 }
48 };
49
51 struct OPENMS_DLLAPI CVTerm
52 {
54 enum class XRefType
55 {
56 XSD_STRING = 0, // xsd:string A string
57 XSD_INTEGER, // xsd:integer Any integer
58 XSD_DECIMAL, // xsd:decimal Any real number
59 XSD_NEGATIVE_INTEGER, // xsd:negativeInteger Any negative integer
60 XSD_POSITIVE_INTEGER, // xsd:positiveInteger Any integer > 0
61 XSD_NON_NEGATIVE_INTEGER, // xsd:nonNegativeInteger Any integer >= 0
62 XSD_NON_POSITIVE_INTEGER, // xsd:nonPositiveInteger Any integer < 0
63 XSD_BOOLEAN, // xsd:boolean True or false
64 XSD_DATE, // xsd:date An XML-Schema date
65 XSD_ANYURI, // xsd:anyURI uniform resource identifier
66 NONE
67 };
68
69 static std::string getXRefTypeName(XRefType type);
70 //static bool isSearchEngineSpecificScore();
72
73 std::string name;
74 std::string id;
75 std::set<std::string> parents;
76 std::set<std::string> children;
77 bool obsolete;
78 std::string description;
83 std::set<std::string> units;
84
87
88 CVTerm(const CVTerm& rhs);
89
90 CVTerm& operator=(const CVTerm& rhs);
91
93 std::string toXMLString(const std::string& ref, const std::string& value =std::string("")) const;
94
96 std::string toXMLString(const std::string& ref, const DataValue& value) const;
97
98 };
99
102
105
108
111
114
117
119 const std::string& name() const;
120
122 const std::string& label() const;
123
125 const std::string& version() const;
126
128 const std::string& url() const;
129
139 void loadFromOBO(const std::string& name, const std::string& filename);
140
142 bool exists(const std::string& id) const;
143
145 bool hasTermWithName(const std::string& name) const;
146
152 const CVTerm& getTerm(const std::string& id) const;
153
159 const CVTerm& getTermByName(const std::string& name, const std::string& desc = "") const;
160
161
163 const std::map<std::string, CVTerm>& getTerms() const;
164
175 void getAllChildTerms(std::set<std::string>& terms, const std::string& parent_id) const;
176
185 void addAllChildTerms(std::set<std::string>& terms, const std::string& parent_id) const;
186
198 template <class LAMBDA>
199 bool iterateAllChildren(const std::string& parent_id, LAMBDA lbd) const
200 {
201 for (const auto& child_id : getTerm(parent_id).children)
202 {
203 if (lbd(child_id) || iterateAllChildren(child_id, lbd))
204 return true;
205 }
206 return false;
207 }
208
216 const ControlledVocabulary::CVTerm* checkAndGetTermByName(const std::string& name) const;
217
226 bool isChildOf(const std::string& child_id, const std::string& parent_id) const;
227
228
244
245protected:
251 bool checkName_(const std::string& id, const std::string& name, bool ignore_case = true) const;
252
254 const CVTerm* findTerm_(std::string_view id) const;
255
258
260 // note: an unordered_map would iterate in a platform-dependent order; index_ provides the fast lookups
261 std::map<std::string, CVTerm> terms_;
263 std::unordered_map<std::string_view, const CVTerm*> index_;
265 std::map<std::string, std::string> namesToIds_;
267 std::string name_;
269 std::string label_;
271 std::string version_;
273 std::string url_;
274 };
275
277 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const ControlledVocabulary& cv);
278
279
280} // namespace OpenMS
281
Definition ControlledVocabulary.h:31
ControlledVocabulary()
Constructor.
bool exists(const std::string &id) const
Returns true if the term is in the CV. Returns false otherwise.
ControlledVocabulary & operator=(ControlledVocabulary &&rhs) noexcept
Move assignment operator.
bool iterateAllChildren(const std::string &parent_id, LAMBDA lbd) const
Iterates over all children (incl. subchildren etc) of parent recursively, i.e. the whole subtree.
Definition ControlledVocabulary.h:199
const CVTerm & getTermByName(const std::string &name, const std::string &desc="") const
Returns a term specified by name.
std::map< std::string, CVTerm > terms_
Map from ID to CVTerm.
Definition ControlledVocabulary.h:261
std::string url_
CV URL.
Definition ControlledVocabulary.h:273
bool checkName_(const std::string &id, const std::string &name, bool ignore_case=true) const
checks if a name corresponds to an id
const std::string & name() const
Returns the CV name (set in the load method)
std::string label_
CV label.
Definition ControlledVocabulary.h:269
ControlledVocabulary(const ControlledVocabulary &rhs)
Copy constructor.
std::unordered_map< std::string_view, const CVTerm * > index_
Hash index into terms_ for lookups by id (map nodes are stable); rebuilt whenever terms_ changes.
Definition ControlledVocabulary.h:263
bool hasTermWithName(const std::string &name) const
Returns true if a term with the given name is in the CV. Returns false otherwise.
const std::string & version() const
Returns the CV version (set in the load method)
const CVTerm * findTerm_(std::string_view id) const
Term by id, or nullptr if unknown.
friend std::ostream & operator<<(std::ostream &os, const ControlledVocabulary &cv)
Print the contents to a stream.
std::string name_
Name set in the load method.
Definition ControlledVocabulary.h:267
const std::string & label() const
Returns the CV label (set in the load method)
void rebuildIndex_()
Rebuilds index_ from terms_.
void loadFromOBO(const std::string &name, const std::string &filename)
Loads the CV from an OBO file.
std::string version_
CV version.
Definition ControlledVocabulary.h:271
std::map< std::string, std::string > namesToIds_
Map from name to id.
Definition ControlledVocabulary.h:265
ControlledVocabulary(ControlledVocabulary &&rhs) noexcept
Move constructor.
ControlledVocabulary & operator=(const ControlledVocabulary &rhs)
Assignment operator.
virtual ~ControlledVocabulary()
Destructor.
const ControlledVocabulary::CVTerm * checkAndGetTermByName(const std::string &name) const
Searches the existing terms for the given name.
void getAllChildTerms(std::set< std::string > &terms, const std::string &parent_id) const
Writes all child terms recursively into terms.
const std::map< std::string, CVTerm > & getTerms() const
returns all the terms stored in the CV
static const ControlledVocabulary & getPSIMSCV()
Returns a CV for parsing/storing PSI-MS related data, e.g. mzML, or handle accessions/ids in datastru...
bool isChildOf(const std::string &child_id, const std::string &parent_id) const
Returns if child is a child of parent.
const CVTerm & getTerm(const std::string &id) const
Returns a term specified by ID.
const std::string & url() const
Returns the CV url (set in the load method)
void addAllChildTerms(std::set< std::string > &terms, const std::string &parent_id) const
Writes the parent term and all descendant term IDs into terms.
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:33
std::vector< std::string > StringList
Vector of String.
Definition TypeAliases.h:39
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:52
std::string description
Term description.
Definition ControlledVocabulary.h:78
static std::string getXRefTypeName(XRefType type)
std::string toXMLString(const std::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:80
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:82
bool obsolete
Flag that indicates of the term is obsolete.
Definition ControlledVocabulary.h:77
std::string name
if it is a score type, lookup has_order
Definition ControlledVocabulary.h:73
StringList synonyms
List of synonyms.
Definition ControlledVocabulary.h:79
XRefType xref_type
xref value-type for the CV-term
Definition ControlledVocabulary.h:81
std::set< std::string > parents
The parent IDs.
Definition ControlledVocabulary.h:75
XRefType
define xsd types allowed in cv term to specify their value-type
Definition ControlledVocabulary.h:55
std::set< std::string > units
unit accession ids, defined by relationship has units
Definition ControlledVocabulary.h:83
CVTerm & operator=(const CVTerm &rhs)
std::set< std::string > children
The child IDs.
Definition ControlledVocabulary.h:76
static bool isHigherBetterScore(ControlledVocabulary::CVTerm term)
std::string id
Identifier.
Definition ControlledVocabulary.h:74
std::string toXMLString(const std::string &ref, const std::string &value=std::string("")) const
get mzidentml formatted string. i.e. a cvparam xml element, ref should be the name of the ControlledV...
ensure same hash on all platforms (for reproducibility)-
Definition ControlledVocabulary.h:37
size_t operator()(const std::string &key) const noexcept
Definition ControlledVocabulary.h:38