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 <set>
17#include <map>
18#include <memory>
19
20namespace OpenMS
21{
31 template<typename DigestionEnzymeType, typename InstanceType> class DigestionEnzymeDB
32 {
33 public:
34
38 typedef typename std::set<const DigestionEnzymeType*>::const_iterator ConstEnzymeIterator;
39 typedef typename std::set<const DigestionEnzymeType*>::iterator EnzymeIterator;
41
44 static const InstanceType* getInstance()
45 {
46 static InstanceType* db_ = nullptr;
47 if (db_ == nullptr)
48 {
49 db_ = new InstanceType;
50 }
51 return db_;
52 }
53
59 {
60 for (ConstEnzymeIterator it = const_enzymes_.begin(); it != const_enzymes_.end(); ++it)
61 {
62 delete *it;
63 }
64 }
66
73 const DigestionEnzymeType* getEnzyme(const std::string& name) const
74 {
75 auto pos = enzyme_names_.find(name);
76 if (pos == enzyme_names_.end())
77 {
78 throw Exception::ElementNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, name);
79 }
80 return pos->second;
81 }
82
85 const DigestionEnzymeType* getEnzymeByRegEx(const std::string& cleavage_regex) const
86 {
87 if (!hasRegEx(cleavage_regex))
88 {
89 // @TODO: why does this use a different exception than "getEnzyme"?
90 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
91 std::string("Enzyme with regex " + cleavage_regex + " was not registered in Enzyme DB, register first!").c_str());
92 }
93 return enzyme_regex_.at(cleavage_regex);
94 }
95
97 void getAllNames(std::vector<std::string>& all_names) const
98 {
99 all_names.clear();
100 for (ConstEnzymeIterator it = const_enzymes_.begin(); it != const_enzymes_.end(); ++it)
101 {
102 all_names.push_back((*it)->getName());
103 }
104 }
106
111 bool hasEnzyme(const std::string& name) const
112 {
113 return (enzyme_names_.contains(name));
114 }
115
117 bool hasRegEx(const std::string& cleavage_regex) const
118 {
119 return (enzyme_regex_.contains(cleavage_regex));
120 }
121
123 bool hasEnzyme(const DigestionEnzymeType* enzyme) const
124 {
125 return (const_enzymes_.contains(enzyme) );
126 }
128
132 inline ConstEnzymeIterator beginEnzyme() const { return const_enzymes_.begin(); } // we only allow constant iterators -- this DB is not meant to be modifiable
133 inline ConstEnzymeIterator endEnzyme() const { return const_enzymes_.end(); }
134
136 protected:
137 DigestionEnzymeDB() = default;
138
140 DigestionEnzymeDB(const DigestionEnzymeDB& enzymes_db) = delete;
142
147 DigestionEnzymeDB& operator=(const DigestionEnzymeDB& enzymes_db) = delete;
149
152 void addEnzyme_(const DigestionEnzymeType* enzyme)
153 {
154 std::string name = enzyme->getName();
155
156 // if an enzyme with the same name exists, remove the old one first
157 auto existing = enzyme_names_.find(name);
158 if (existing != enzyme_names_.end())
159 {
160 const DigestionEnzymeType* old = existing->second;
161 const_enzymes_.erase(old);
162 // remove old name/synonym entries
163 std::string old_name = old->getName();
164 enzyme_names_.erase(old_name);
165 enzyme_names_.erase(StringUtils::toLower(old_name));
166 for (const auto& syn : old->getSynonyms())
167 {
168 enzyme_names_.erase(syn);
169 }
170 // remove old regex entry
171 if (!old->getRegEx().empty())
172 {
173 enzyme_regex_.erase(old->getRegEx());
174 }
175 delete old;
176 }
177
178 // add to internal storage
179 const_enzymes_.insert(enzyme);
180 // add to internal indices (by name and its synonyms)
181 enzyme_names_[name] = enzyme;
182 enzyme_names_[StringUtils::toLower(name)] = enzyme;
183 for (std::set<std::string>::const_iterator it = enzyme->getSynonyms().begin(); it != enzyme->getSynonyms().end(); ++it)
184 {
185 enzyme_names_[*it] = enzyme;
186 }
187 // ... and by regex
188 if (enzyme->getRegEx() != "")
189 {
190 enzyme_regex_[enzyme->getRegEx()] = enzyme;
191 }
192 return;
193 }
194
198 void loadFromProviders_(std::vector<std::unique_ptr<DigestionEnzymeDataProvider<DigestionEnzymeType>>>& providers)
199 {
200 for (auto& provider : providers)
201 {
202 auto enzymes = provider->loadEnzymes();
203 for (auto& enzyme : enzymes)
204 {
205 addEnzyme_(enzyme.release());
206 }
207 }
208 }
209
210 std::map<std::string, const DigestionEnzymeType*> enzyme_names_;
211
212 std::map<std::string, const DigestionEnzymeType*> enzyme_regex_;
213
214 std::set<const DigestionEnzymeType*> const_enzymes_;
215
216 };
217}
218
Digestion enzyme database (base class)
Definition DigestionEnzymeDB.h:32
void getAllNames(std::vector< std::string > &all_names) const
returns all the enzyme names (does NOT include synonym names)
Definition DigestionEnzymeDB.h:97
bool hasEnzyme(const std::string &name) const
returns true if the db contains a enzyme with the given name (supports synonym names)
Definition DigestionEnzymeDB.h:111
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:117
ConstEnzymeIterator endEnzyme() const
Definition DigestionEnzymeDB.h:133
virtual ~DigestionEnzymeDB()
destructor
Definition DigestionEnzymeDB.h:58
void loadFromProviders_(std::vector< std::unique_ptr< DigestionEnzymeDataProvider< DigestionEnzymeType > > > &providers)
Definition DigestionEnzymeDB.h:198
ConstEnzymeIterator beginEnzyme() const
Definition DigestionEnzymeDB.h:132
std::map< std::string, const DigestionEnzymeType * > enzyme_names_
index by names
Definition DigestionEnzymeDB.h:210
const DigestionEnzymeType * getEnzymeByRegEx(const std::string &cleavage_regex) const
Definition DigestionEnzymeDB.h:85
static const InstanceType * getInstance()
Definition DigestionEnzymeDB.h:44
std::map< std::string, const DigestionEnzymeType * > enzyme_regex_
index by regex
Definition DigestionEnzymeDB.h:212
DigestionEnzymeDB & operator=(const DigestionEnzymeDB &enzymes_db)=delete
assignment operator
std::set< constDigestionEnzymeType * >::const_iterator ConstEnzymeIterator
Definition DigestionEnzymeDB.h:38
std::set< constDigestionEnzymeType * >::iterator EnzymeIterator
Definition DigestionEnzymeDB.h:39
bool hasEnzyme(const DigestionEnzymeType *enzyme) const
returns true if the db contains the enzyme of the given pointer
Definition DigestionEnzymeDB.h:123
const DigestionEnzymeType * getEnzyme(const std::string &name) const
Definition DigestionEnzymeDB.h:73
std::set< const DigestionEnzymeType * > const_enzymes_
set of enzymes
Definition DigestionEnzymeDB.h:214
void addEnzyme_(const DigestionEnzymeType *enzyme)
Definition DigestionEnzymeDB.h:152
Abstract interface for providing digestion enzyme data.
Definition DigestionEnzymeDataProvider.h:30
Element could not be found exception.
Definition Exception.h:654
A method or algorithm argument contains illegal values.
Definition Exception.h:630
std::string & toLower(std::string &s)
Definition StringUtils.h:584
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19