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
43 static InstanceType* getInstance()
44 {
45 static InstanceType* db_ = nullptr;
46 if (db_ == nullptr)
47 {
48 db_ = new InstanceType;
49 }
50 return db_;
51 }
52
58 {
59 for (ConstEnzymeIterator it = const_enzymes_.begin(); it != const_enzymes_.end(); ++it)
60 {
61 delete *it;
62 }
63 }
65
72 const DigestionEnzymeType* getEnzyme(const String& name) const
73 {
74 auto pos = enzyme_names_.find(name);
75 if (pos == enzyme_names_.end())
76 {
77 throw Exception::ElementNotFound(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, name);
78 }
79 return pos->second;
80 }
81
84 const DigestionEnzymeType* getEnzymeByRegEx(const String& cleavage_regex) const
85 {
86 if (!hasRegEx(cleavage_regex))
87 {
88 // @TODO: why does this use a different exception than "getEnzyme"?
89 throw Exception::IllegalArgument(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
90 String("Enzyme with regex " + cleavage_regex + " was not registered in Enzyme DB, register first!").c_str());
91 }
92 return enzyme_regex_.at(cleavage_regex);
93 }
94
96 void getAllNames(std::vector<String>& all_names) const
97 {
98 all_names.clear();
99 for (ConstEnzymeIterator it = const_enzymes_.begin(); it != const_enzymes_.end(); ++it)
100 {
101 all_names.push_back((*it)->getName());
102 }
103 }
105
110 bool hasEnzyme(const String& name) const
111 {
112 return (enzyme_names_.find(name) != enzyme_names_.end());
113 }
114
116 bool hasRegEx(const String& cleavage_regex) const
117 {
118 return (enzyme_regex_.find(cleavage_regex) != enzyme_regex_.end());
119 }
120
122 bool hasEnzyme(const DigestionEnzymeType* enzyme) const
123 {
124 return (const_enzymes_.find(enzyme) != const_enzymes_.end() );
125 }
127
131 inline ConstEnzymeIterator beginEnzyme() const { return const_enzymes_.begin(); } // we only allow constant iterators -- this DB is not meant to be modifiable
132 inline ConstEnzymeIterator endEnzyme() const { return const_enzymes_.end(); }
133
135 protected:
136 DigestionEnzymeDB() = default;
137
139 DigestionEnzymeDB(const DigestionEnzymeDB& enzymes_db) = delete;
141
146 DigestionEnzymeDB& operator=(const DigestionEnzymeDB& enzymes_db) = delete;
148
151 void addEnzyme_(const DigestionEnzymeType* enzyme)
152 {
153 String name = enzyme->getName();
154
155 // if an enzyme with the same name exists, remove the old one first
156 auto existing = enzyme_names_.find(name);
157 if (existing != enzyme_names_.end())
158 {
159 const DigestionEnzymeType* old = existing->second;
160 const_enzymes_.erase(old);
161 // remove old name/synonym entries
162 String old_name = old->getName();
163 enzyme_names_.erase(old_name);
164 enzyme_names_.erase(old_name.toLower());
165 for (const auto& syn : old->getSynonyms())
166 {
167 enzyme_names_.erase(syn);
168 }
169 // remove old regex entry
170 if (!old->getRegEx().empty())
171 {
172 enzyme_regex_.erase(old->getRegEx());
173 }
174 delete old;
175 }
176
177 // add to internal storage
178 const_enzymes_.insert(enzyme);
179 // add to internal indices (by name and its synonyms)
180 enzyme_names_[name] = enzyme;
181 enzyme_names_[name.toLower()] = enzyme;
182 for (std::set<String>::const_iterator it = enzyme->getSynonyms().begin(); it != enzyme->getSynonyms().end(); ++it)
183 {
184 enzyme_names_[*it] = enzyme;
185 }
186 // ... and by regex
187 if (enzyme->getRegEx() != "")
188 {
189 enzyme_regex_[enzyme->getRegEx()] = enzyme;
190 }
191 return;
192 }
193
197 void loadFromProviders_(std::vector<std::unique_ptr<DigestionEnzymeDataProvider<DigestionEnzymeType>>>& providers)
198 {
199 for (auto& provider : providers)
200 {
201 auto enzymes = provider->loadEnzymes();
202 for (auto& enzyme : enzymes)
203 {
204 addEnzyme_(enzyme.release());
205 }
206 }
207 }
208
209 std::map<String, const DigestionEnzymeType*> enzyme_names_;
210
211 std::map<String, const DigestionEnzymeType*> enzyme_regex_;
212
213 std::set<const DigestionEnzymeType*> const_enzymes_;
214
215 };
216}
217
Digestion enzyme database (base class)
Definition DigestionEnzymeDB.h:32
static InstanceType * getInstance()
this member function serves as a replacement of the constructor
Definition DigestionEnzymeDB.h:43
DigestionEnzymeDB(const DigestionEnzymeDB &enzymes_db)=delete
copy constructor
std::map< String, const DigestionEnzymeType * > enzyme_regex_
index by regex
Definition DigestionEnzymeDB.h:211
ConstEnzymeIterator endEnzyme() const
Definition DigestionEnzymeDB.h:132
virtual ~DigestionEnzymeDB()
destructor
Definition DigestionEnzymeDB.h:57
void loadFromProviders_(std::vector< std::unique_ptr< DigestionEnzymeDataProvider< DigestionEnzymeType > > > &providers)
Definition DigestionEnzymeDB.h:197
ConstEnzymeIterator beginEnzyme() const
Definition DigestionEnzymeDB.h:131
const DigestionEnzymeType * getEnzyme(const String &name) const
Definition DigestionEnzymeDB.h:72
void getAllNames(std::vector< String > &all_names) const
returns all the enzyme names (does NOT include synonym names)
Definition DigestionEnzymeDB.h:96
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:122
bool hasEnzyme(const String &name) const
returns true if the db contains a enzyme with the given name (supports synonym names)
Definition DigestionEnzymeDB.h:110
std::set< const DigestionEnzymeType * > const_enzymes_
set of enzymes
Definition DigestionEnzymeDB.h:213
std::map< String, const DigestionEnzymeType * > enzyme_names_
index by names
Definition DigestionEnzymeDB.h:209
const DigestionEnzymeType * getEnzymeByRegEx(const String &cleavage_regex) const
Definition DigestionEnzymeDB.h:84
bool hasRegEx(const String &cleavage_regex) const
returns true if the db contains a enzyme with the given regex
Definition DigestionEnzymeDB.h:116
void addEnzyme_(const DigestionEnzymeType *enzyme)
Definition DigestionEnzymeDB.h:151
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
A more convenient string class.
Definition String.h:34
String & toLower()
Converts the string to lowercase.
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19