OpenMS
Loading...
Searching...
No Matches
DigestionEnzymeDB.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: Xiao Liang $
6// $Authors: Xiao Liang, Chris Bielow $
7// --------------------------------------------------------------------------
8
9#pragma once
10
15
16#include <algorithm>
17#include <cctype>
18#include <set>
19#include <map>
20#include <memory>
21
22namespace OpenMS
23{
33 template<typename DigestionEnzymeType, typename InstanceType> class DigestionEnzymeDB
34 {
35 public:
36
40 typedef typename std::set<const DigestionEnzymeType*>::const_iterator ConstEnzymeIterator;
41 typedef typename std::set<const DigestionEnzymeType*>::iterator EnzymeIterator;
43
46 static const InstanceType* getInstance()
47 {
48 static InstanceType* db_ = nullptr;
49 if (db_ == nullptr)
50 {
51 db_ = new InstanceType;
52 }
53 return db_;
54 }
55
61 {
62 for (ConstEnzymeIterator it = const_enzymes_.begin(); it != const_enzymes_.end(); ++it)
63 {
64 delete *it;
65 }
66 }
68
77 const DigestionEnzymeType* getEnzyme(const std::string& name) const
78 {
79 const DigestionEnzymeType* enzyme = findEnzyme_(name);
80 if (enzyme == nullptr)
81 {
82 throw Exception::ElementNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, name);
83 }
84 return enzyme;
85 }
86
89 const DigestionEnzymeType* getEnzymeByRegEx(const std::string& cleavage_regex) const
90 {
91 if (!hasRegEx(cleavage_regex))
92 {
93 // @TODO: why does this use a different exception than "getEnzyme"?
94 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
95 std::string("Enzyme with regex " + cleavage_regex + " was not registered in Enzyme DB, register first!").c_str());
96 }
97 return enzyme_regex_.at(cleavage_regex);
98 }
99
101 void getAllNames(std::vector<std::string>& all_names) const
102 {
103 all_names.clear();
104 for (ConstEnzymeIterator it = const_enzymes_.begin(); it != const_enzymes_.end(); ++it)
105 {
106 all_names.push_back((*it)->getName());
107 }
108 }
110
115 bool hasEnzyme(const std::string& name) const
116 {
117 return (findEnzyme_(name) != nullptr);
118 }
119
121 bool hasRegEx(const std::string& cleavage_regex) const
122 {
123 return (enzyme_regex_.contains(cleavage_regex));
124 }
125
127 bool hasEnzyme(const DigestionEnzymeType* enzyme) const
128 {
129 return (const_enzymes_.contains(enzyme) );
130 }
132
136 inline ConstEnzymeIterator beginEnzyme() const { return const_enzymes_.begin(); } // we only allow constant iterators -- this DB is not meant to be modifiable
137 inline ConstEnzymeIterator endEnzyme() const { return const_enzymes_.end(); }
138
140 protected:
141 DigestionEnzymeDB() = default;
142
144 DigestionEnzymeDB(const DigestionEnzymeDB& enzymes_db) = delete;
146
151 DigestionEnzymeDB& operator=(const DigestionEnzymeDB& enzymes_db) = delete;
153
156 void addEnzyme_(const DigestionEnzymeType* enzyme)
157 {
158 std::string name = enzyme->getName();
159
160 // if an enzyme with the same name exists, remove the old one first
161 // (compare names only: a synonym or lookup key of another enzyme must not remove that enzyme)
162 auto existing = std::find_if(const_enzymes_.begin(), const_enzymes_.end(),
163 [&name](const DigestionEnzymeType* e) { return e->getName() == name; });
164 if (existing != const_enzymes_.end())
165 {
166 const DigestionEnzymeType* old = *existing;
167 const_enzymes_.erase(existing);
168 // remove old name/synonym entries, but only those that point to the enzyme being replaced
169 auto points_to_old = [old](const auto& entry) { return entry.second == old; };
170 std::erase_if(enzyme_names_, points_to_old);
171 std::erase_if(enzyme_keys_, points_to_old);
172 // remove old regex entry
173 if (!old->getRegEx().empty())
174 {
175 enzyme_regex_.erase(old->getRegEx());
176 }
177 delete old;
178 }
179
180 // add to internal storage
181 const_enzymes_.insert(enzyme);
182 // add to internal indices (by name and its synonyms, as written ...
183 enzyme_names_[name] = enzyme;
184 for (const auto& syn : enzyme->getSynonyms())
185 {
186 enzyme_names_[syn] = enzyme;
187 }
188 // ... and as lookup keys, see getEnzyme(); a synonym never re-points an existing key)
189 enzyme_keys_[lookupKey_(name)] = enzyme;
190 for (const auto& syn : enzyme->getSynonyms())
191 {
192 enzyme_keys_.emplace(lookupKey_(syn), enzyme);
193 }
194 // ... and by regex
195 if (enzyme->getRegEx() != "")
196 {
197 enzyme_regex_[enzyme->getRegEx()] = enzyme;
198 }
199 return;
200 }
201
205 void loadFromProviders_(std::vector<std::unique_ptr<DigestionEnzymeDataProvider<DigestionEnzymeType>>>& providers)
206 {
207 for (auto& provider : providers)
208 {
209 auto enzymes = provider->loadEnzymes();
210 for (auto& enzyme : enzymes)
211 {
212 addEnzyme_(enzyme.release());
213 }
214 }
215 }
216
218 static std::string lookupKey_(const std::string& name)
219 {
220 std::string key;
221 key.reserve(name.size());
222 for (unsigned char c : name)
223 {
224 if (std::isspace(c) || c == '-' || c == '_') continue;
225 key += static_cast<char>(std::tolower(c));
226 }
227 return key;
228 }
229
231 const DigestionEnzymeType* findEnzyme_(const std::string& name) const
232 {
233 auto pos = enzyme_names_.find(name);
234 if (pos != enzyme_names_.end()) return pos->second;
235 pos = enzyme_keys_.find(lookupKey_(name));
236 return (pos != enzyme_keys_.end()) ? pos->second : nullptr;
237 }
238
239 std::map<std::string, const DigestionEnzymeType*> enzyme_names_;
240
241 std::map<std::string, const DigestionEnzymeType*> enzyme_keys_;
242
243 std::map<std::string, const DigestionEnzymeType*> enzyme_regex_;
244
245 std::set<const DigestionEnzymeType*> const_enzymes_;
246
247 };
248}
249
Digestion enzyme database (base class)
Definition DigestionEnzymeDB.h:34
void getAllNames(std::vector< std::string > &all_names) const
returns all the enzyme names (does NOT include synonym names)
Definition DigestionEnzymeDB.h:101
bool hasEnzyme(const std::string &name) const
returns true if the db contains a enzyme with the given name (supports synonym names; matched as in g...
Definition DigestionEnzymeDB.h:115
const DigestionEnzymeType * findEnzyme_(const std::string &name) const
returns the enzyme with the given name or synonym (see getEnzyme()), or nullptr if unknown
Definition DigestionEnzymeDB.h:231
DigestionEnzymeDB(const DigestionEnzymeDB &enzymes_db)=delete
copy constructor
bool hasRegEx(const std::string &cleavage_regex) const
returns true if the db contains a enzyme with the given regex
Definition DigestionEnzymeDB.h:121
ConstEnzymeIterator endEnzyme() const
Definition DigestionEnzymeDB.h:137
virtual ~DigestionEnzymeDB()
destructor
Definition DigestionEnzymeDB.h:60
static std::string lookupKey_(const std::string &name)
key for lenient name lookup (see getEnzyme()): lowercase, without whitespace, '-' and '_'
Definition DigestionEnzymeDB.h:218
void loadFromProviders_(std::vector< std::unique_ptr< DigestionEnzymeDataProvider< DigestionEnzymeType > > > &providers)
Definition DigestionEnzymeDB.h:205
ConstEnzymeIterator beginEnzyme() const
Definition DigestionEnzymeDB.h:136
std::map< std::string, const DigestionEnzymeType * > enzyme_names_
index by names and synonyms (as written)
Definition DigestionEnzymeDB.h:239
const DigestionEnzymeType * getEnzymeByRegEx(const std::string &cleavage_regex) const
Definition DigestionEnzymeDB.h:89
static const InstanceType * getInstance()
Definition DigestionEnzymeDB.h:46
std::map< std::string, const DigestionEnzymeType * > enzyme_regex_
index by regex
Definition DigestionEnzymeDB.h:243
DigestionEnzymeDB & operator=(const DigestionEnzymeDB &enzymes_db)=delete
assignment operator
std::set< constDigestionEnzymeType * >::const_iterator ConstEnzymeIterator
Definition DigestionEnzymeDB.h:40
std::map< std::string, const DigestionEnzymeType * > enzyme_keys_
index by lookupKey_() of names and synonyms
Definition DigestionEnzymeDB.h:241
std::set< constDigestionEnzymeType * >::iterator EnzymeIterator
Definition DigestionEnzymeDB.h:41
bool hasEnzyme(const DigestionEnzymeType *enzyme) const
returns true if the db contains the enzyme of the given pointer
Definition DigestionEnzymeDB.h:127
const DigestionEnzymeType * getEnzyme(const std::string &name) const
Definition DigestionEnzymeDB.h:77
std::set< const DigestionEnzymeType * > const_enzymes_
set of enzymes
Definition DigestionEnzymeDB.h:245
void addEnzyme_(const DigestionEnzymeType *enzyme)
Definition DigestionEnzymeDB.h:156
Abstract interface for providing digestion enzyme data.
Definition DigestionEnzymeDataProvider.h:30
Element could not be found exception.
Definition Exception.h:657
A method or algorithm argument contains illegal values.
Definition Exception.h:633
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19