OpenMS  2.4.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-2018.
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 
92  static AdductInfo parseAdductString(const String& adduct);
93 
94  private:
96  AdductInfo();
97 
101  double mass_;
102  int charge_;
104  };
105 
106  class OPENMS_DLLAPI AccurateMassSearchResult
107  {
108  public:
111 
114 
117 
120 
122  double getObservedMZ() const;
123 
125  void setObservedMZ(const double&);
126 
128  double getCalculatedMZ() const;
129 
131  void setCalculatedMZ(const double&);
132 
134  double getQueryMass() const;
135 
137  void setQueryMass(const double&);
138 
140  double getFoundMass() const;
141 
143  void setFoundMass(const double&);
144 
146  Int getCharge() const;
147 
149  void setCharge(const Int&);
150 
152  double getMZErrorPPM() const;
153 
155  void setMZErrorPPM(const double);
156 
158  double getObservedRT() const;
159 
161  void setObservedRT(const double& rt);
162 
164  double getObservedIntensity() const;
165 
167  void setObservedIntensity(const double&);
168 
170  std::vector<double> getIndividualIntensities() const;
171 
173  void setIndividualIntensities(const std::vector<double>&);
174 
175  Size getMatchingIndex() const;
176  void setMatchingIndex(const Size&);
177 
178  Size getSourceFeatureIndex() const;
179  void setSourceFeatureIndex(const Size&);
180 
181  const String& getFoundAdduct() const;
182  void setFoundAdduct(const String&);
183 
184  const String& getFormulaString() const;
185  void setEmpiricalFormula(const String&);
186 
187  const std::vector<String>& getMatchingHMDBids() const;
188  void setMatchingHMDBids(const std::vector<String>&);
189 
191  const std::vector<double>& getMasstraceIntensities() const;
192  void setMasstraceIntensities(const std::vector<double>&);
193 
194  double getIsotopesSimScore() const;
195  void setIsotopesSimScore(const double&);
196 
197  // debug/output functions
198  friend OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AccurateMassSearchResult& amsr);
199 
200 private:
202  double observed_mz_;
205  double db_mass_;
208  double observed_rt_;
210  std::vector<double> individual_intensities_;
213 
216  std::vector<String> matching_hmdb_ids_;
217 
218  std::vector<double> mass_trace_intensities_;
220  };
221 
222  OPENMS_DLLAPI std::ostream& operator<<(std::ostream& os, const AccurateMassSearchResult& amsr);
223 
255  class OPENMS_DLLAPI AccurateMassSearchEngine :
256  public DefaultParamHandler,
257  public ProgressLogger
258  {
259 public:
262 
264  ~AccurateMassSearchEngine() override;
265 
270  void queryByMZ(const double& observed_mz, const Int& observed_charge, const String& ion_mode, std::vector<AccurateMassSearchResult>& results) const;
271  void queryByFeature(const Feature& feature, const Size& feature_index, const String& ion_mode, std::vector<AccurateMassSearchResult>& results) const;
272  void queryByConsensusFeature(const ConsensusFeature& cfeat, const Size& cf_index, const Size& number_of_maps, const String& ion_mode, std::vector<AccurateMassSearchResult>& results) const;
273 
276  void run(FeatureMap&, MzTab&) const;
277 
281  void run(ConsensusMap&, MzTab&) const;
282 
284  void init();
285 
286 protected:
287  void updateMembers_() override;
288 
289 private:
291 
294  template <typename MAPTYPE> String resolveAutoMode_(const MAPTYPE& map) const
295  {
296  String ion_mode_internal;
297  String ion_mode_detect_msg = "";
298  if (map.size() > 0)
299  {
300  if (map[0].metaValueExists("scan_polarity"))
301  {
302  StringList pols = ListUtils::create<String>(String(map[0].getMetaValue("scan_polarity")), ';');
303  if (pols.size() == 1 && pols[0].size() > 0)
304  {
305  pols[0].toLower();
306  if (pols[0] == "positive" || pols[0] == "negative")
307  {
308  ion_mode_internal = pols[0];
309  LOG_INFO << "Setting auto ion-mode to '" << ion_mode_internal << "' for file " << File::basename(map.getLoadedFilePath()) << std::endl;
310  }
311  else ion_mode_detect_msg = String("Meta value 'scan_polarity' does not contain unknown ion mode") + String(map[0].getMetaValue("scan_polarity"));
312  }
313  else
314  {
315  ion_mode_detect_msg = String("ambiguous ion mode: ") + String(map[0].getMetaValue("scan_polarity"));
316  }
317  }
318  else
319  {
320  ion_mode_detect_msg = String("Meta value 'scan_polarity' not found in (Consensus-)Feature map");
321  }
322  }
323  else
324  { // do nothing, since map is
325  LOG_INFO << "Meta value 'scan_polarity' cannot be determined since (Consensus-)Feature map is empty!" << std::endl;
326  }
327 
328  if (ion_mode_detect_msg.size() > 0)
329  {
330  throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, String("Auto ionization mode could not resolve ion mode of data (") + ion_mode_detect_msg + "!");
331  }
332 
333  return ion_mode_internal;
334  }
335 
336  void parseMappingFile_(const StringList&);
337  void parseStructMappingFile_(const StringList&);
338  void parseAdductsFile_(const String& filename, std::vector<AdductInfo>& result);
339  void searchMass_(double neutral_query_mass, double diff_mass, std::pair<Size, Size>& hit_indices) const;
340 
342  void annotate_(const std::vector<AccurateMassSearchResult>&, BaseFeature&) const;
343 
346  double computeCosineSim_(const std::vector<double>& x, const std::vector<double>& y) const;
347 
348  double computeIsotopePatternSimilarity_(const Feature& feat, const EmpiricalFormula& form) const;
349 
350  typedef std::vector<std::vector<AccurateMassSearchResult> > QueryResultsTable;
351 
352  void exportMzTab_(const QueryResultsTable& overall_results, const Size number_of_maps, MzTab& mztab_out) const;
353 
355  typedef std::vector<std::vector<String> > MassIDMapping;
356  typedef std::map<String, std::vector<String> > HMDBPropsMapping;
357 
359  {
360  double mass;
361  std::vector<String> massIDs;
363  };
364  std::vector<MappingEntry_> mass_mappings_;
365 
366  struct CompareEntryAndMass_ // defined here to allow for inlining by compiler
367  {
368  double asMass(const MappingEntry_& v) const
369  {
370  return v.mass;
371  }
372 
373  double asMass(double t) const
374  {
375  return t;
376  }
377 
378  template <typename T1, typename T2>
379  bool operator()(T1 const& t1, T2 const& t2) const
380  {
381  return asMass(t1) < asMass(t2);
382  }
383 
384  };
385 
387 
389 
395 
398 
401 
402  std::vector<AdductInfo> pos_adducts_;
403  std::vector<AdductInfo> neg_adducts_;
404 
407 
409  };
410 
411 }
412 
String name_
members
Definition: AccurateMassSearchEngine.h:99
String formula
Definition: AccurateMassSearchEngine.h:362
std::vector< AdductInfo > pos_adducts_
Definition: AccurateMassSearchEngine.h:402
String ion_mode_
Definition: AccurateMassSearchEngine.h:393
String mass_error_unit_
Definition: AccurateMassSearchEngine.h:392
bool operator()(T1 const &t1, T2 const &t2) const
Definition: AccurateMassSearchEngine.h:379
Definition: AccurateMassSearchEngine.h:57
double searched_mass_
Definition: AccurateMassSearchEngine.h:204
A more convenient string class.
Definition: String.h:57
bool iso_similarity_
Definition: AccurateMassSearchEngine.h:394
bool keep_unidentified_masses_
Definition: AccurateMassSearchEngine.h:408
Definition: AccurateMassSearchEngine.h:366
#define LOG_INFO
Macro if a information, e.g. a status should be reported.
Definition: LogStream.h:454
int charge_
negative or positive charge; must not be 0
Definition: AccurateMassSearchEngine.h:102
String empirical_formula_
Definition: AccurateMassSearchEngine.h:215
double db_mass_
Definition: AccurateMassSearchEngine.h:205
std::vector< std::vector< String > > MassIDMapping
private member variables
Definition: AccurateMassSearchEngine.h:355
A container for features.
Definition: FeatureMap.h:93
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
A container for consensus elements.
Definition: ConsensusMap.h:75
String database_name_
Definition: AccurateMassSearchEngine.h:405
std::vector< double > individual_intensities_
Definition: AccurateMassSearchEngine.h:210
double theoretical_mz_
Definition: AccurateMassSearchEngine.h:203
StringList db_mapping_file_
Definition: AccurateMassSearchEngine.h:399
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
Definition: AccurateMassSearchEngine.h:358
std::vector< String > massIDs
Definition: AccurateMassSearchEngine.h:361
A basic LC-MS feature.
Definition: BaseFeature.h:55
std::vector< String > matching_hmdb_ids_
Definition: AccurateMassSearchEngine.h:216
String found_adduct_
Definition: AccurateMassSearchEngine.h:214
double observed_rt_
Definition: AccurateMassSearchEngine.h:208
UInt mol_multiplier_
Mol multiplier, e.g. 2 in 2M+H;+1.
Definition: AccurateMassSearchEngine.h:103
Definition: AccurateMassSearchEngine.h:106
HMDBPropsMapping hmdb_properties_mapping_
Definition: AccurateMassSearchEngine.h:386
std::vector< AdductInfo > neg_adducts_
Definition: AccurateMassSearchEngine.h:403
Representation of an empirical formula.
Definition: EmpiricalFormula.h:81
String resolveAutoMode_(const MAPTYPE &map) const
private member functions
Definition: AccurateMassSearchEngine.h:294
double mass
Definition: AccurateMassSearchEngine.h:360
String pos_adducts_fname_
Definition: AccurateMassSearchEngine.h:396
std::map< String, std::vector< String > > HMDBPropsMapping
Definition: AccurateMassSearchEngine.h:356
Int charge_
Definition: AccurateMassSearchEngine.h:206
String & toLower()
Converts the string to lowercase.
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
std::vector< std::vector< AccurateMassSearchResult > > QueryResultsTable
Definition: AccurateMassSearchEngine.h:350
double observed_mz_
Stored information/results of DB query.
Definition: AccurateMassSearchEngine.h:202
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:347
An LC-MS feature.
Definition: Feature.h:70
double mz_error_ppm_
Definition: AccurateMassSearchEngine.h:207
static String basename(const String &file)
Returns the basename of the file (without the path).
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:73
double asMass(double t) const
Definition: AccurateMassSearchEngine.h:373
StringList db_struct_file_
Definition: AccurateMassSearchEngine.h:400
double isotopes_sim_score_
Definition: AccurateMassSearchEngine.h:219
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
EmpiricalFormula ef_
EF for the actual adduct e.g. &#39;H&#39; in 2M+H;+1.
Definition: AccurateMassSearchEngine.h:100
Base class for all classes that want to report their progress.
Definition: ProgressLogger.h:54
double mass_error_value_
parameter stuff
Definition: AccurateMassSearchEngine.h:391
double mass_
computed from ef_.getMonoWeight(), but stored explicitly for efficiency
Definition: AccurateMassSearchEngine.h:101
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:91
Size source_feature_index_
Definition: AccurateMassSearchEngine.h:212
Size matching_index_
Definition: AccurateMassSearchEngine.h:211
String neg_adducts_fname_
Definition: AccurateMassSearchEngine.h:397
double observed_intensity_
Definition: AccurateMassSearchEngine.h:209
int Int
Signed integer type.
Definition: Types.h:102
std::vector< double > mass_trace_intensities_
Definition: AccurateMassSearchEngine.h:218
double asMass(const MappingEntry_ &v) const
Definition: AccurateMassSearchEngine.h:368
bool is_initialized_
true if init_() was called without any subsequent param changes
Definition: AccurateMassSearchEngine.h:388
std::vector< MappingEntry_ > mass_mappings_
Definition: AccurateMassSearchEngine.h:364
String database_version_
Definition: AccurateMassSearchEngine.h:406
A consensus feature spanning multiple LC-MS/MS experiments.
Definition: ConsensusFeature.h:69
Data model of MzTab files. Please see the official MzTab specification at https://code.google.com/p/mztab/.
Definition: MzTab.h:699
An algorithm to search for exact mass matches from a spectrum against a database (e.g. HMDB).
Definition: AccurateMassSearchEngine.h:255