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 
38 #include <OpenMS/KERNEL/Feature.h>
42 #include <OpenMS/FORMAT/MzTab.h>
47 #include <OpenMS/SYSTEM/File.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 
104  double mass_;
105  int charge_;
107  };
108 
109  class OPENMS_DLLAPI AccurateMassSearchResult
110  {
111  public:
114 
117 
120 
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_;
208  double db_mass_;
211  double observed_rt_;
213  std::vector<double> individual_intensities_;
216 
219  std::vector<String> matching_hmdb_ids_;
220 
221  std::vector<double> mass_trace_intensities_;
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:
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 
363  {
364  double mass;
365  std::vector<String> massIDs;
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 
391 
393 
399 
402 
405 
406  std::vector<AdductInfo> pos_adducts_;
407  std::vector<AdductInfo> neg_adducts_;
408 
411 
413  };
414 
415 }
OpenMS::AdductInfo::charge_
int charge_
negative or positive charge; must not be 0
Definition: AccurateMassSearchEngine.h:105
OpenMS::AccurateMassSearchResult::isotopes_sim_score_
double isotopes_sim_score_
Definition: AccurateMassSearchEngine.h:222
DefaultParamHandler.h
OpenMS::AccurateMassSearchResult::found_adduct_
String found_adduct_
Definition: AccurateMassSearchEngine.h:217
ConsensusXMLFile.h
MzTabFile.h
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_::asMass
double asMass(double t) const
Definition: AccurateMassSearchEngine.h:377
OpenMS::TOPPBase
Base class for TOPP applications.
Definition: TOPPBase.h:144
OpenMS::Param::copy
Param copy(const String &prefix, bool remove_prefix=false) const
Returns a new Param object containing all entries that start with prefix.
FileHandler.h
FileTypes.h
OpenMS::MzTabFile
File adapter for MzTab files.
Definition: MzTabFile.h:58
OpenMS::AdductInfo::mass_
double mass_
computed from ef_.getMonoWeight(), but stored explicitly for efficiency
Definition: AccurateMassSearchEngine.h:104
OpenMS::AccurateMassSearchEngine::pos_adducts_fname_
String pos_adducts_fname_
Definition: AccurateMassSearchEngine.h:400
OpenMS::AccurateMassSearchResult::observed_mz_
double observed_mz_
Stored information/results of DB query.
Definition: AccurateMassSearchEngine.h:205
OpenMS::AccurateMassSearchResult::charge_
Int charge_
Definition: AccurateMassSearchEngine.h:209
OpenMS::ConsensusXMLFile::store
void store(const String &filename, const ConsensusMap &consensus_map)
Stores a consensus map to file.
OpenMS::AccurateMassSearchEngine::MappingEntry_
Definition: AccurateMassSearchEngine.h:362
OpenMS::AccurateMassSearchEngine
An algorithm to search for exact mass matches from a spectrum against a database (e....
Definition: AccurateMassSearchEngine.h:258
OpenMS::AccurateMassSearchEngine::QueryResultsTable
std::vector< std::vector< AccurateMassSearchResult > > QueryResultsTable
Definition: AccurateMassSearchEngine.h:354
OpenMS::AccurateMassSearchResult::individual_intensities_
std::vector< double > individual_intensities_
Definition: AccurateMassSearchEngine.h:213
OpenMS::File::basename
static String basename(const String &file)
Returns the basename of the file (without the path).
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_
Definition: AccurateMassSearchEngine.h:370
OpenMS::Param::setValue
void setValue(const String &key, const DataValue &value, const String &description="", const StringList &tags=StringList())
Sets a value.
OpenMS::AccurateMassSearchResult::matching_hmdb_ids_
std::vector< String > matching_hmdb_ids_
Definition: AccurateMassSearchEngine.h:219
OpenMS::String
A more convenient string class.
Definition: String.h:58
OpenMS::BaseFeature
A basic LC-MS feature.
Definition: BaseFeature.h:56
Feature.h
ConsensusMap.h
OpenMS::FileTypes::CONSENSUSXML
OpenMS consensus map format (.consensusXML)
Definition: FileTypes.h:67
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::AccurateMassSearchEngine::mass_error_value_
double mass_error_value_
parameter stuff
Definition: AccurateMassSearchEngine.h:395
FeatureXMLFile.h
OpenMS::AccurateMassSearchEngine::neg_adducts_fname_
String neg_adducts_fname_
Definition: AccurateMassSearchEngine.h:401
OpenMS::MzTab
Data model of MzTab files. Please see the official MzTab specification at https://code....
Definition: MzTab.h:855
OpenMS::AdductInfo::name_
String name_
members
Definition: AccurateMassSearchEngine.h:102
OpenMS::AccurateMassSearchEngine::init
void init()
parse database and adduct files
OpenMS::AccurateMassSearchResult::searched_mass_
double searched_mass_
Definition: AccurateMassSearchEngine.h:207
OpenMS::AccurateMassSearchEngine::database_version_
String database_version_
Definition: AccurateMassSearchEngine.h:410
OpenMS::AccurateMassSearchEngine::mass_mappings_
std::vector< MappingEntry_ > mass_mappings_
Definition: AccurateMassSearchEngine.h:368
OpenMS::AccurateMassSearchResult::matching_index_
Size matching_index_
Definition: AccurateMassSearchEngine.h:214
ListUtils.h
OpenMS::DefaultParamHandler
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
ConsensusFeature.h
OpenMS::AccurateMassSearchEngine::MappingEntry_::mass
double mass
Definition: AccurateMassSearchEngine.h:364
OpenMS::Exception::InvalidParameter
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:347
OpenMS::AccurateMassSearchEngine::pos_adducts_
std::vector< AdductInfo > pos_adducts_
Definition: AccurateMassSearchEngine.h:406
MassTrace.h
OpenMS::AccurateMassSearchEngine::db_struct_file_
StringList db_struct_file_
Definition: AccurateMassSearchEngine.h:404
OpenMS::AccurateMassSearchEngine::ion_mode_
String ion_mode_
Definition: AccurateMassSearchEngine.h:397
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::AccurateMassSearchEngine::is_initialized_
bool is_initialized_
true if init_() was called without any subsequent param changes
Definition: AccurateMassSearchEngine.h:392
OpenMS::FileTypes::FEATUREXML
OpenMS feature file (.featureXML)
Definition: FileTypes.h:65
OpenMS::ProgressLogger
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
OpenMS::AccurateMassSearchEngine::MappingEntry_::massIDs
std::vector< String > massIDs
Definition: AccurateMassSearchEngine.h:365
ProgressLogger.h
OpenMS::AccurateMassSearchEngine::keep_unidentified_masses_
bool keep_unidentified_masses_
Definition: AccurateMassSearchEngine.h:412
OpenMS::AccurateMassSearchResult::observed_intensity_
double observed_intensity_
Definition: AccurateMassSearchEngine.h:212
int
OpenMS::AccurateMassSearchEngine::MappingEntry_::formula
String formula
Definition: AccurateMassSearchEngine.h:366
OpenMS::FeatureXMLFile::load
void load(const String &filename, FeatureMap &feature_map)
loads the file with name filename into map and calls updateRanges().
FeatureMap.h
OpenMS::AdductInfo
Definition: AccurateMassSearchEngine.h:57
OpenMS::FileTypes::Type
Type
Actual file types enum.
Definition: FileTypes.h:58
OpenMS::AccurateMassSearchResult::source_feature_index_
Size source_feature_index_
Definition: AccurateMassSearchEngine.h:215
OpenMS::ConsensusFeature
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:69
OpenMS::AccurateMassSearchEngine::MassIDMapping
std::vector< std::vector< String > > MassIDMapping
private member variables
Definition: AccurateMassSearchEngine.h:359
OpenMS::AccurateMassSearchEngine::iso_similarity_
bool iso_similarity_
Definition: AccurateMassSearchEngine.h:398
OpenMS::MzTabFile::store
void store(const String &filename, const MzTab &mz_tab) const
OpenMS::FileHandler::getType
static FileTypes::Type getType(const String &filename)
Tries to determine the file type (by name or content)
OpenMS::DefaultParamHandler::setParameters
void setParameters(const Param &param)
Sets the parameters.
OpenMS::DefaultParamHandler::getDefaults
const Param & getDefaults() const
Non-mutable access to the default parameters.
MzTab.h
OpenMS::AdductInfo::mol_multiplier_
UInt mol_multiplier_
Mol multiplier, e.g. 2 in 2M+H;+1.
Definition: AccurateMassSearchEngine.h:106
OpenMS::AdductInfo::ef_
EmpiricalFormula ef_
EF for the actual adduct e.g. 'H' in 2M+H;+1.
Definition: AccurateMassSearchEngine.h:103
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::ConsensusMap
A container for consensus elements.
Definition: ConsensusMap.h:79
OpenMS::StringList
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
OpenMS::FeatureXMLFile::store
void store(const String &filename, const FeatureMap &feature_map)
stores the map feature_map in file with name filename.
OpenMS::ConsensusXMLFile::load
void load(const String &filename, ConsensusMap &map)
Loads a consensus map from file and calls updateRanges.
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::AccurateMassSearchResult::mass_trace_intensities_
std::vector< double > mass_trace_intensities_
Definition: AccurateMassSearchEngine.h:221
main
int main(int argc, const char **argv)
Definition: INIFileEditor.cpp:73
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_::operator()
bool operator()(T1 const &t1, T2 const &t2) const
Definition: AccurateMassSearchEngine.h:383
OpenMS::AccurateMassSearchEngine::mass_error_unit_
String mass_error_unit_
Definition: AccurateMassSearchEngine.h:396
EmpiricalFormula.h
OpenMS::FeatureMap
A container for features.
Definition: FeatureMap.h:95
OpenMS::Param::remove
void remove(const String &key)
Remove the entry key or a section key (when suffix is ':')
OpenMS::Feature
An LC-MS feature.
Definition: Feature.h:70
OpenMS::AccurateMassSearchEngine::neg_adducts_
std::vector< AdductInfo > neg_adducts_
Definition: AccurateMassSearchEngine.h:407
OpenMS::EmpiricalFormula
Representation of an empirical formula.
Definition: EmpiricalFormula.h:82
OpenMS::FeatureXMLFile
This class provides Input/Output functionality for feature maps.
Definition: FeatureXMLFile.h:68
OpenMS::AccurateMassSearchResult::db_mass_
double db_mass_
Definition: AccurateMassSearchEngine.h:208
String.h
OpenMS::Param
Management and storage of parameters / INI files.
Definition: Param.h:73
OpenMS::AccurateMassSearchEngine::hmdb_properties_mapping_
HMDBPropsMapping hmdb_properties_mapping_
Definition: AccurateMassSearchEngine.h:390
OpenMS::AccurateMassSearchEngine::HMDBPropsMapping
std::map< String, std::vector< String > > HMDBPropsMapping
Definition: AccurateMassSearchEngine.h:360
OpenMS::AccurateMassSearchResult::empirical_formula_
String empirical_formula_
Definition: AccurateMassSearchEngine.h:218
OpenMS::AccurateMassSearchEngine::run
void run(FeatureMap &, MzTab &) const
OpenMS::AccurateMassSearchResult::observed_rt_
double observed_rt_
Definition: AccurateMassSearchEngine.h:211
OPENMS_LOG_INFO
#define OPENMS_LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:465
AccurateMassSearchEngine.h
OpenMS::AccurateMassSearchResult
Definition: AccurateMassSearchEngine.h:109
OpenMS::AccurateMassSearchEngine::db_mapping_file_
StringList db_mapping_file_
Definition: AccurateMassSearchEngine.h:403
OpenMS::AccurateMassSearchResult::mz_error_ppm_
double mz_error_ppm_
Definition: AccurateMassSearchEngine.h:210
OpenMS::AccurateMassSearchResult::theoretical_mz_
double theoretical_mz_
Definition: AccurateMassSearchEngine.h:206
File.h
OpenMS::AccurateMassSearchEngine::database_name_
String database_name_
Definition: AccurateMassSearchEngine.h:409
TOPPBase.h
OpenMS::AccurateMassSearchEngine::CompareEntryAndMass_::asMass
double asMass(const MappingEntry_ &v) const
Definition: AccurateMassSearchEngine.h:372
OpenMS::ConsensusXMLFile
This class provides Input functionality for ConsensusMaps and Output functionality for alignments and...
Definition: ConsensusXMLFile.h:61
OpenMS::AccurateMassSearchEngine::resolveAutoMode_
String resolveAutoMode_(const MAPTYPE &map) const
private member functions
Definition: AccurateMassSearchEngine.h:298