| OpenMS 2.5.0

AccurateMassSearchEngine.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2020.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Erhan Kenar, Chris Bielow $
33 // --------------------------------------------------------------------------
34
35 #pragma once
36
37 #include <OpenMS/KERNEL/MassTrace.h>
38 #include <OpenMS/KERNEL/Feature.h>
39 #include <OpenMS/KERNEL/ConsensusFeature.h>
40 #include <OpenMS/KERNEL/FeatureMap.h>
41 #include <OpenMS/KERNEL/ConsensusMap.h>
42 #include <OpenMS/FORMAT/MzTab.h>
43 #include <OpenMS/DATASTRUCTURES/DefaultParamHandler.h>
44 #include <OpenMS/DATASTRUCTURES/String.h>
45 #include <OpenMS/DATASTRUCTURES/ListUtils.h>
46 #include <OpenMS/CONCEPT/ProgressLogger.h>
47 #include <OpenMS/SYSTEM/File.h>
48 #include <OpenMS/CHEMISTRY/EmpiricalFormula.h>
49
50 #include <iosfwd>
51 #include <vector>
52
53 namespace OpenMS
54 {
55 class EmpiricalFormula;
56
57 class OPENMS_DLLAPI AdductInfo
58 {
59
60 public:
71 AdductInfo(const String& name, const EmpiricalFormula& adduct, int charge, UInt mol_multiplier = 1);
72
74 double getNeutralMass(double observed_mz) const;
75
77 double getMZ(double neutral_mass) const;
78
81 bool isCompatible(EmpiricalFormula db_entry) const;
82
84 int getCharge() const;
85
87 const String& getName() const;
88
90 const EmpiricalFormula& getEmpiricalFormula() const;
91
95 static AdductInfo parseAdductString(const String& adduct);
96
97 private:
99 AdductInfo();
100
102 String name_;
103 EmpiricalFormula ef_;
104 double mass_;
105 int charge_;
106 UInt mol_multiplier_;
107 };
108
109 class OPENMS_DLLAPI AccurateMassSearchResult
110 {
111 public:
113 AccurateMassSearchResult();
114
116 ~AccurateMassSearchResult();
117
119 AccurateMassSearchResult(const AccurateMassSearchResult&);
120
122 AccurateMassSearchResult& operator=(const AccurateMassSearchResult&);
123
125 double getObservedMZ() const;
126
128 void setObservedMZ(const double&);
129
131 double getCalculatedMZ() const;
132
134 void setCalculatedMZ(const double&);
135
137 double getQueryMass() const;
138
140 void setQueryMass(const double&);
141
143 double getFoundMass() const;
144
146 void setFoundMass(const double&);
147
149 Int getCharge() const;
150
152 void setCharge(const Int&);
153
155 double getMZErrorPPM() const;
156
158 void setMZErrorPPM(const double);
159
161 double getObservedRT() const;
162
164 void setObservedRT(const double& rt);
165
167 double getObservedIntensity() const;
168
170 void setObservedIntensity(const double&);
171
173 std::vector<double> getIndividualIntensities() const;
174
176 void setIndividualIntensities(const std::vector<double>&);
177
178 Size getMatchingIndex() const;
179 void setMatchingIndex(const Size&);
180
181 Size getSourceFeatureIndex() const;
182 void setSourceFeatureIndex(const Size&);
183
184 const String& getFoundAdduct() const;
185 void setFoundAdduct(const String&);
186
187 const String& getFormulaString() const;
188 void setEmpiricalFormula(const String&);
189
190 const std::vector<String>& getMatchingHMDBids() const;
191 void setMatchingHMDBids(const std::vector<String>&);
192
194 const std::vector<double>& getMasstraceIntensities() const;
195 void setMasstraceIntensities(const std::vector<double>&);
196
197 double getIsotopesSimScore() const;
198 void setIsotopesSimScore(const double&);
199
200 // debug/output functions
201 friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AccurateMassSearchResult& amsr);
202
203 private:
205 double observed_mz_;
206 double theoretical_mz_;
207 double searched_mass_;
208 double db_mass_;
209 Int charge_;
210 double mz_error_ppm_;
211 double observed_rt_;
212 double observed_intensity_;
213 std::vector<double> individual_intensities_;
214 Size matching_index_;
215 Size source_feature_index_;
216
217 String found_adduct_;
218 String empirical_formula_;
219 std::vector<String> matching_hmdb_ids_;
220
221 std::vector<double> mass_trace_intensities_;
222 double isotopes_sim_score_;
223 };
224
225 OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AccurateMassSearchResult& amsr);
226
258 class OPENMS_DLLAPI AccurateMassSearchEngine :
259 public DefaultParamHandler,
260 public ProgressLogger
261 {
262 public:
264 AccurateMassSearchEngine();
265
267 ~AccurateMassSearchEngine() override;
268
274 void queryByMZ(const double& observed_mz, const Int& observed_charge, const String& ion_mode, std::vector<AccurateMassSearchResult>& results, const EmpiricalFormula& observed_adduct = EmpiricalFormula()) const;
275 void queryByFeature(const Feature& feature, const Size& feature_index, const String& ion_mode, std::vector<AccurateMassSearchResult>& results) const;
276 void queryByConsensusFeature(const ConsensusFeature& cfeat, const Size& cf_index, const Size& number_of_maps, const String& ion_mode, std::vector<AccurateMassSearchResult>& results) const;
277
280 void run(FeatureMap&, MzTab&) const;
281
285 void run(ConsensusMap&, MzTab&) const;
286
288 void init();
289
290 protected:
291 void updateMembers_() override;
292
293 private:
295
298 template <typename MAPTYPE> String resolveAutoMode_(const MAPTYPE& map) const
299 {
300 String ion_mode_internal;
301 String ion_mode_detect_msg = "";
302 if (map.size() > 0)
303 {
304 if (map[0].metaValueExists("scan_polarity"))
305 {
306 StringList pols = ListUtils::create<String>(String(map[0].getMetaValue("scan_polarity")), ';');
307 if (pols.size() == 1 && pols[0].size() > 0)
308 {
309 pols[0].toLower();
310 if (pols[0] == "positive" || pols[0] == "negative")
311 {
312 ion_mode_internal = pols[0];
313 OPENMS_LOG_INFO << "Setting auto ion-mode to '" << ion_mode_internal << "' for file " << File::basename(map.getLoadedFilePath()) << std::endl;
314 }
315 else ion_mode_detect_msg = String("Meta value 'scan_polarity' does not contain unknown ion mode") + String(map[0].getMetaValue("scan_polarity"));
316 }
317 else
318 {
319 ion_mode_detect_msg = String("ambiguous ion mode: ") + String(map[0].getMetaValue("scan_polarity"));
320 }
321 }
322 else
323 {
324 ion_mode_detect_msg = String("Meta value 'scan_polarity' not found in (Consensus-)Feature map");
325 }
326 }
327 else
328 { // do nothing, since map is
329 OPENMS_LOG_INFO << "Meta value 'scan_polarity' cannot be determined since (Consensus-)Feature map is empty!" << std::endl;
330 }
331
332 if (ion_mode_detect_msg.size() > 0)
333 {
334 throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Auto ionization mode could not resolve ion mode of data (") + ion_mode_detect_msg + "!");
335 }
336
337 return ion_mode_internal;
338 }
339
340 void parseMappingFile_(const StringList&);
341 void parseStructMappingFile_(const StringList&);
342 void parseAdductsFile_(const String& filename, std::vector<AdductInfo>& result);
343 void searchMass_(double neutral_query_mass, double diff_mass, std::pair<Size, Size>& hit_indices) const;
344
346 void annotate_(const std::vector<AccurateMassSearchResult>&, BaseFeature&) const;
347
350 double computeCosineSim_(const std::vector<double>& x, const std::vector<double>& y) const;
351
352 double computeIsotopePatternSimilarity_(const Feature& feat, const EmpiricalFormula& form) const;
353
354 typedef std::vector<std::vector<AccurateMassSearchResult> > QueryResultsTable;
355
356 void exportMzTab_(const QueryResultsTable& overall_results, const Size number_of_maps, MzTab& mztab_out) const;
357
359 typedef std::vector<std::vector<String> > MassIDMapping;
360 typedef std::map<String, std::vector<String> > HMDBPropsMapping;
361
362 struct MappingEntry_
363 {
364 double mass;
365 std::vector<String> massIDs;
366 String formula;
367 };
368 std::vector<MappingEntry_> mass_mappings_;
369
370 struct CompareEntryAndMass_ // defined here to allow for inlining by compiler
371 {
372 double asMass(const MappingEntry_& v) const
373 {
374 return v.mass;
375 }
376
377 double asMass(double t) const
378 {
379 return t;
380 }
381
382 template <typename T1, typename T2>
383 bool operator()(T1 const& t1, T2 const& t2) const
384 {
385 return asMass(t1) < asMass(t2);
386 }
387
388 };
389
390 HMDBPropsMapping hmdb_properties_mapping_;
391
392 bool is_initialized_;
393
395 double mass_error_value_;
396 String mass_error_unit_;
397 String ion_mode_;
398 bool iso_similarity_;
399
400 String pos_adducts_fname_;
401 String neg_adducts_fname_;
402
403 StringList db_mapping_file_;
404 StringList db_struct_file_;
405
406 std::vector<AdductInfo> pos_adducts_;
407 std::vector<AdductInfo> neg_adducts_;
408
409 String database_name_;
410 String database_version_;
411
412 bool keep_unidentified_masses_;
413 };
414
415 }
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
OpenMS::AccurateMassSearchEngine::mass_error_value_
double mass_error_value_
parameter stuff
Definition: AccurateMassSearchEngine.h:395
OpenMS::AccurateMassSearchResult::db_mass_
double db_mass_
Definition: AccurateMassSearchEngine.h:208
MassTrace.h
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::AccurateMassSearchResult::empirical_formula_
String empirical_formula_
Definition: AccurateMassSearchEngine.h:218
OpenMS::AdductInfo::mass_
double mass_
computed from ef_.getMonoWeight(), but stored explicitly for efficiency
Definition: AccurateMassSearchEngine.h:104
OpenMS::AccurateMassSearchEngine::QueryResultsTable
std::vector< std::vector< AccurateMassSearchResult > > QueryResultsTable
Definition: AccurateMassSearchEngine.h:354
OpenMS::AccurateMassSearchEngine::neg_adducts_
std::vector< AdductInfo > neg_adducts_
Definition: AccurateMassSearchEngine.h:407
OpenMS::AccurateMassSearchResult::observed_rt_
double observed_rt_
Definition: AccurateMassSearchEngine.h:211
OpenMS::AccurateMassSearchResult::matching_index_
Size matching_index_
Definition: AccurateMassSearchEngine.h:214
OpenMS::DefaultParamHandler
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
OpenMS::String::toLower
String & toLower()
Converts the string to lowercase.
Feature.h
OpenMS::AccurateMassSearchResult::source_feature_index_
Size source_feature_index_
Definition: AccurateMassSearchEngine.h:215
OpenMS::AccurateMassSearchEngine::HMDBPropsMapping
std::map< String, std::vector< String > > HMDBPropsMapping
Definition: AccurateMassSearchEngine.h:360
OpenMS::AccurateMassSearchResult::found_adduct_
String found_adduct_
Definition: AccurateMassSearchEngine.h:217
OpenMS::AccurateMassSearchResult::mass_trace_intensities_
std::vector< double > mass_trace_intensities_
Definition: AccurateMassSearchEngine.h:221
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:95
OpenMS::AccurateMassSearchResult::observed_mz_
double observed_mz_
Stored information/results of DB query.
Definition: AccurateMassSearchEngine.h:205
OpenMS::AdductInfo::charge_
int charge_
negative or positive charge; must not be 0
Definition: AccurateMassSearchEngine.h:105
OpenMS::AccurateMassSearchEngine::MappingEntry_::formula
String formula
Definition: AccurateMassSearchEngine.h:366
OpenMS::AccurateMassSearchEngine::database_version_
String database_version_
Definition: AccurateMassSearchEngine.h:410
OpenMS::AccurateMassSearchEngine::db_mapping_file_
StringList db_mapping_file_
Definition: AccurateMassSearchEngine.h:403
OpenMS::AccurateMassSearchEngine::resolveAutoMode_
String resolveAutoMode_(const MAPTYPE &map) const
private member functions
Definition: AccurateMassSearchEngine.h:298
OpenMS::AccurateMassSearchEngine::MassIDMapping
std::vector< std::vector< String > > MassIDMapping
private member variables
Definition: AccurateMassSearchEngine.h:359
OpenMS::AccurateMassSearchEngine::is_initialized_
bool is_initialized_
true if init_() was called without any subsequent param changes
Definition: AccurateMassSearchEngine.h:392
OpenMS::AccurateMassSearchResult::theoretical_mz_
double theoretical_mz_
Definition: AccurateMassSearchEngine.h:206
OpenMS::AccurateMassSearchEngine::pos_adducts_fname_
String pos_adducts_fname_
Definition: AccurateMassSearchEngine.h:400
OpenMS::ConsensusFeature
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:69
OpenMS::AccurateMassSearchEngine
An algorithm to search for exact mass matches from a spectrum against a database (e.g. HMDB).
Definition: AccurateMassSearchEngine.h:258
OpenMS::AccurateMassSearchResult::mz_error_ppm_
double mz_error_ppm_
Definition: AccurateMassSearchEngine.h:210
OpenMS::File::basename
static String basename(const String &file)
Returns the basename of the file (without the path).
ConsensusMap.h
OpenMS::AccurateMassSearchEngine::db_struct_file_
StringList db_struct_file_
Definition: AccurateMassSearchEngine.h:404
OpenMS::AdductInfo::mol_multiplier_
UInt mol_multiplier_
Mol multiplier, e.g. 2 in 2M+H;+1.
Definition: AccurateMassSearchEngine.h:106
FeatureMap.h
String.h
OpenMS::Exception::InvalidParameter
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:347
ProgressLogger.h
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::AccurateMassSearchEngine::MappingEntry_::mass
double mass
Definition: AccurateMassSearchEngine.h:364
OpenMS::AccurateMassSearchResult::searched_mass_
double searched_mass_
Definition: AccurateMassSearchEngine.h:207
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::AccurateMassSearchEngine::MappingEntry_::massIDs
std::vector< String > massIDs
Definition: AccurateMassSearchEngine.h:365
OpenMS::ProgressLogger
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
OpenMS::AccurateMassSearchResult::individual_intensities_
std::vector< double > individual_intensities_
Definition: AccurateMassSearchEngine.h:213
OPENMS_LOG_INFO
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_::asMass
double asMass(const MappingEntry_ &v) const
Definition: AccurateMassSearchEngine.h:372
OpenMS::AdductInfo::name_
String name_
members
Definition: AccurateMassSearchEngine.h:102
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_::operator()
bool operator()(T1 const &t1, T2 const &t2) const
Definition: AccurateMassSearchEngine.h:383
DefaultParamHandler.h
ConsensusFeature.h
OpenMS::EmpiricalFormula
Representation of an empirical formula.
Definition: EmpiricalFormula.h:82
OpenMS::AccurateMassSearchEngine::ion_mode_
String ion_mode_
Definition: AccurateMassSearchEngine.h:397
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::AccurateMassSearchResult::observed_intensity_
double observed_intensity_
Definition: AccurateMassSearchEngine.h:212
MzTab.h
OpenMS::AccurateMassSearchResult::charge_
Int charge_
Definition: AccurateMassSearchEngine.h:209
OpenMS::AccurateMassSearchEngine::pos_adducts_
std::vector< AdductInfo > pos_adducts_
Definition: AccurateMassSearchEngine.h:406
OpenMS::MzTab
Data model of MzTab files. Please see the official MzTab specification at https://code.google.com/p/mztab/.
Definition: MzTab.h:855
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_
Definition: AccurateMassSearchEngine.h:370
OpenMS::AccurateMassSearchResult
Definition: AccurateMassSearchEngine.h:109
OpenMS::AdductInfo::ef_
EmpiricalFormula ef_
EF for the actual adduct e.g. &#39;H&#39; in 2M+H;+1.
Definition: AccurateMassSearchEngine.h:103
OpenMS::AccurateMassSearchResult::matching_hmdb_ids_
std::vector< String > matching_hmdb_ids_
Definition: AccurateMassSearchEngine.h:219
EmpiricalFormula.h
OpenMS::AccurateMassSearchEngine::mass_mappings_
std::vector< MappingEntry_ > mass_mappings_
Definition: AccurateMassSearchEngine.h:368
OpenMS::AccurateMassSearchEngine::database_name_
String database_name_
Definition: AccurateMassSearchEngine.h:409
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
OpenMS::AccurateMassSearchEngine::keep_unidentified_masses_
bool keep_unidentified_masses_
Definition: AccurateMassSearchEngine.h:412
int
OpenMS::ConsensusMap
A container for consensus elements.
Definition: ConsensusMap.h:79
OpenMS::AccurateMassSearchEngine::MappingEntry_
Definition: AccurateMassSearchEngine.h:362
File.h
OpenMS::AccurateMassSearchResult::isotopes_sim_score_
double isotopes_sim_score_
Definition: AccurateMassSearchEngine.h:222
OpenMS::AccurateMassSearchEngine::hmdb_properties_mapping_
HMDBPropsMapping hmdb_properties_mapping_
Definition: AccurateMassSearchEngine.h:390
OpenMS::AccurateMassSearchEngine::iso_similarity_
bool iso_similarity_
Definition: AccurateMassSearchEngine.h:398
OpenMS::AdductInfo
Definition: AccurateMassSearchEngine.h:57
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_::asMass
double asMass(double t) const
Definition: AccurateMassSearchEngine.h:377
ListUtils.h
OpenMS::AccurateMassSearchEngine::mass_error_unit_
String mass_error_unit_
Definition: AccurateMassSearchEngine.h:396
OpenMS::AccurateMassSearchEngine::neg_adducts_fname_
String neg_adducts_fname_
Definition: AccurateMassSearchEngine.h:401
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::BaseFeature
A basic LC-MS feature.
Definition: BaseFeature.h:56