OpenMS
MRMTransitionGroup.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Hannes Roest $
6 // $Authors: Hannes Roest $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Macros.h>
13 #include <boost/numeric/conversion/cast.hpp>
14 
15 namespace OpenMS
16 {
17 
40  template <typename ChromatogramType, typename TransitionType>
42  {
43 
44 public:
45 
47 
48  typedef std::vector<MRMFeature> MRMFeatureListType;
51  typedef std::vector<TransitionType> TransitionsType;
55 
61  {
62  }
63 
66  tr_gr_id_(rhs.tr_gr_id_),
74  {
75  }
76 
79  {
80  }
82 
84  {
85  if (&rhs != this)
86  {
87  tr_gr_id_ = rhs.tr_gr_id_;
95  }
96  return *this;
97  }
98 
99  inline Size size() const
100  {
101  return chromatograms_.size();
102  }
103 
104  inline const String & getTransitionGroupID() const
105  {
106  return tr_gr_id_;
107  }
108 
109  inline void setTransitionGroupID(const String & tr_gr_id)
110  {
111  tr_gr_id_ = tr_gr_id;
112  }
113 
115 
116  inline const std::vector<TransitionType> & getTransitions() const
117  {
118  return transitions_;
119  }
120 
121  inline std::vector<TransitionType> & getTransitionsMuteable()
122  {
123  return transitions_;
124  }
125 
133  inline void addTransition(const TransitionType& transition, const String& key)
134  {
135  // store the index where to find the transition, using the key for lookup
136  auto result = transition_map_.emplace(key, int(transitions_.size()));
137  if (!result.second) // ouch: key was already used
138  {
139  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Transition with nativeID was already present!", key);
140  }
141  transitions_.push_back(transition);
142  }
143 
144  inline bool hasTransition(const String& key) const
145  {
146  return transition_map_.find(key) != transition_map_.end();
147  }
148 
149  inline const TransitionType& getTransition(const String& key)
150  {
151  OPENMS_PRECONDITION(hasTransition(key), "Cannot retrieve transitions that does not exist")
152  OPENMS_PRECONDITION(transitions_.size() > (size_t)transition_map_[key], "Mapping needs to be accurate")
153  return transitions_[transition_map_[key]];
154  }
156 
157 
159 
160  inline std::vector<ChromatogramType>& getChromatograms()
161  {
162  return chromatograms_;
163  }
164 
165  inline const std::vector<ChromatogramType>& getChromatograms() const
166  {
167  return chromatograms_;
168  }
169 
177  inline void addChromatogram(const ChromatogramType& chromatogram, const String& key)
178  {
179  // store the index where to find the chromatogram, using the key for lookup
180  auto result = chromatogram_map_.emplace(key, int(chromatograms_.size()));
181  if (!result.second) // ouch: key was already used
182  {
183  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
184  }
185  chromatograms_.push_back(chromatogram);
186  }
187 
188  inline bool hasChromatogram(const String& key) const
189  {
190  return chromatogram_map_.find(key) != chromatogram_map_.end();
191  }
192 
194  {
195  OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
196  OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_[key], "Mapping needs to be accurate")
197  return chromatograms_[chromatogram_map_.at(key)];
198  }
199 
200  inline const ChromatogramType& getChromatogram(const String& key) const
201  {
202  OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
203  OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_.at(key), "Mapping needs to be accurate")
204  return chromatograms_[chromatogram_map_.at(key)];
205  }
207 
208 
210 
211  inline std::vector<ChromatogramType>& getPrecursorChromatograms()
212  {
214  }
215 
216  inline const std::vector<ChromatogramType>& getPrecursorChromatograms() const
217  {
219  }
220 
230  inline void addPrecursorChromatogram(const ChromatogramType& chromatogram, const String& key)
231  {
232  // store the index where to find the chromatogram, using the key for lookup
233  auto result = precursor_chromatogram_map_.emplace(key, int(precursor_chromatograms_.size()));
234  if (!result.second) // ouch: key was already used
235  {
236  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
237  }
238  precursor_chromatograms_.push_back(chromatogram);
239  }
240 
241  inline bool hasPrecursorChromatogram(const String& key) const
242  {
244  }
245 
247  {
248  OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
249  OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
251  }
252 
253  inline const ChromatogramType & getPrecursorChromatogram(const String& key) const
254  {
255  OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
256  OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
258  }
260 
261 
263 
264  inline const std::vector<MRMFeature> & getFeatures() const
265  {
266  return mrm_features_;
267  }
268 
269  inline std::vector<MRMFeature> & getFeaturesMuteable()
270  {
271  return mrm_features_;
272  }
273 
274  inline void addFeature(const MRMFeature & feature)
275  {
276  mrm_features_.push_back(feature);
277  }
278 
279  inline void addFeature(MRMFeature && feature)
280  {
281  mrm_features_.push_back(std::move(feature));
282  }
284 
285 
287 
288 
290  inline bool isInternallyConsistent() const
291  {
292  OPENMS_PRECONDITION(transitions_.size() == chromatograms_.size(), "Same number of transitions as chromatograms are required")
293  OPENMS_PRECONDITION(transition_map_.size() == chromatogram_map_.size(), "Same number of transitions as chromatograms mappings are required")
294  OPENMS_PRECONDITION(isMappingConsistent_(), "Mapping needs to be consistent")
295  return true;
296  }
297 
299  inline bool chromatogramIdsMatch() const
300  {
301  for (std::map<String, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
302  {
303  if (getChromatogram(it->first).getNativeID() != it->first)
304  {
305  return false;
306  }
307  }
308  for (std::map<String, int>::const_iterator it = precursor_chromatogram_map_.begin(); it != precursor_chromatogram_map_.end(); it++)
309  {
310  if (getPrecursorChromatogram(it->first).getNativeID() != it->first)
311  {
312  return false;
313  }
314  }
315  return true;
316  }
317 
318  void getLibraryIntensity(std::vector<double> & result) const
319  {
320  for (typename TransitionsType::const_iterator it = transitions_.begin(); it != transitions_.end(); ++it)
321  {
322  result.push_back(it->getLibraryIntensity());
323  }
324  for (Size i = 0; i < result.size(); i++)
325  {
326  // the library intensity should never be below zero
327  if (result[i] < 0.0)
328  {
329  result[i] = 0.0;
330  }
331  }
332  }
333 
334  MRMTransitionGroup subset(std::vector<std::string> tr_ids) const
335  {
336  MRMTransitionGroup transition_group_subset;
337  transition_group_subset.setTransitionGroupID(tr_gr_id_);
338 
339  for (const auto& tr : transitions_)
340  {
341  if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
342  {
343  if (this->hasTransition(tr.getNativeID()))
344  {
345  transition_group_subset.addTransition(tr, tr.getNativeID());
346  }
347  if (this->hasChromatogram(tr.getNativeID()))
348  {
349  transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr.getNativeID())], tr.getNativeID());
350  }
351  }
352  }
353 
354  for (const auto& pc : precursor_chromatograms_)
355  {
356  // add precursor chromatograms if present
357  transition_group_subset.addPrecursorChromatogram(pc, pc.getNativeID());
358  }
359 
360  for (const auto& tgf : mrm_features_)
361  {
362  MRMFeature mf;
363  mf.setIntensity(tgf.getIntensity());
364  mf.setRT(tgf.getRT());
365  mf.MetaInfoInterface::operator=(tgf);
366 
367  for (const auto& tr : transitions_)
368  {
369  if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
370  {
371  mf.addFeature(tgf.getFeature(tr.getNativeID()),tr.getNativeID());
372  }
373  }
374  std::vector<String> pf_ids;
375  tgf.getPrecursorFeatureIDs(pf_ids);
376  for (const auto& pf_id : pf_ids)
377  {
378  mf.addPrecursorFeature(tgf.getPrecursorFeature(pf_id), pf_id);
379  }
380  transition_group_subset.addFeature(mf);
381  }
382 
383  return transition_group_subset;
384  }
385 
386  MRMTransitionGroup subsetDependent(std::vector<std::string> tr_ids) const
387  {
388  MRMTransitionGroup transition_group_subset;
389  transition_group_subset.setTransitionGroupID(tr_gr_id_);
390 
391  for (typename TransitionsType::const_iterator tr_it = transitions_.begin(); tr_it != transitions_.end(); ++tr_it)
392  {
393  if (std::find(tr_ids.begin(), tr_ids.end(), tr_it->getNativeID()) != tr_ids.end())
394  {
395  transition_group_subset.addTransition(*tr_it, tr_it->getNativeID());
396  transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr_it->getNativeID())], tr_it->getNativeID());
397  }
398  }
399 
400  for (std::vector< MRMFeature >::const_iterator tgf_it = mrm_features_.begin(); tgf_it != mrm_features_.end(); ++tgf_it)
401  {
402  transition_group_subset.addFeature(*tgf_it);
403  }
404 
405  return transition_group_subset;
406  }
408 
416  const MRMFeature& getBestFeature() const
417  {
418  OPENMS_PRECONDITION(!getFeatures().empty(), "Cannot get best feature for empty transition group")
419 
420  // Find the feature with the highest score
421  Size bestf = 0;
422  double highest_score = getFeatures()[0].getOverallQuality();
423  for (Size it = 0; it < getFeatures().size(); it++)
424  {
425  if (getFeatures()[it].getOverallQuality() > highest_score)
426  {
427  bestf = it;
428  highest_score = getFeatures()[it].getOverallQuality();
429  }
430  }
431  return getFeatures()[bestf];
432  }
433 
434 protected:
435 
437  bool isMappingConsistent_() const
438  {
439  if (transition_map_.size() != chromatogram_map_.size())
440  {
441  return false;
442  }
443  for (std::map<String, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
444  {
445  if (!hasTransition(it->first))
446  {
447  return false;
448  }
449  }
450  return true;
451  }
452 
455 
458 
460  std::vector<ChromatogramType> chromatograms_;
461 
463  std::vector<ChromatogramType> precursor_chromatograms_;
464 
467 
468  std::map<String, int> chromatogram_map_;
469  std::map<String, int> precursor_chromatogram_map_;
470  std::map<String, int> transition_map_;
471 
472  };
473 }
474 
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:28
const String & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software.
Invalid value exception.
Definition: Exception.h:303
A multi-chromatogram MRM feature.
Definition: MRMFeature.h:26
void addFeature(const Feature &feature, const String &key)
Adds an feature from a single chromatogram into the feature.
void addPrecursorFeature(const Feature &feature, const String &key)
Adds a precursor feature from a single chromatogram into the feature.
The representation of a group of transitions in a targeted proteomics experiment.
Definition: MRMTransitionGroup.h:42
const ChromatogramType & getChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:200
ChromatogramType::PeakType PeakType
Peak type.
Definition: MRMTransitionGroup.h:53
std::vector< ChromatogramType > & getPrecursorChromatograms()
Definition: MRMTransitionGroup.h:211
void addChromatogram(const ChromatogramType &chromatogram, const String &key)
Definition: MRMTransitionGroup.h:177
virtual ~MRMTransitionGroup()
Destructor.
Definition: MRMTransitionGroup.h:78
std::map< String, int > precursor_chromatogram_map_
Definition: MRMTransitionGroup.h:469
ChromatogramType & getChromatogram(const String &key)
Definition: MRMTransitionGroup.h:193
std::vector< MRMFeature > MRMFeatureListType
Type definitions.
Definition: MRMTransitionGroup.h:49
MRMTransitionGroup(const MRMTransitionGroup &rhs)
Copy Constructor.
Definition: MRMTransitionGroup.h:65
bool hasPrecursorChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:241
const std::vector< ChromatogramType > & getChromatograms() const
Definition: MRMTransitionGroup.h:165
std::vector< ChromatogramType > chromatograms_
chromatogram list
Definition: MRMTransitionGroup.h:460
std::vector< TransitionType > & getTransitionsMuteable()
Definition: MRMTransitionGroup.h:121
void addFeature(MRMFeature &&feature)
Definition: MRMTransitionGroup.h:279
std::vector< MRMFeature > & getFeaturesMuteable()
Definition: MRMTransitionGroup.h:269
MRMTransitionGroup subset(std::vector< std::string > tr_ids) const
Definition: MRMTransitionGroup.h:334
const String & getTransitionGroupID() const
Definition: MRMTransitionGroup.h:104
String tr_gr_id_
transition group id (peak group id)
Definition: MRMTransitionGroup.h:454
bool isInternallyConsistent() const
Check whether internal state is consistent, e.g. same number of chromatograms and transitions are pre...
Definition: MRMTransitionGroup.h:290
void addTransition(const TransitionType &transition, const String &key)
Definition: MRMTransitionGroup.h:133
std::vector< ChromatogramType > precursor_chromatograms_
precursor chromatogram list
Definition: MRMTransitionGroup.h:463
const ChromatogramType & getPrecursorChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:253
bool chromatogramIdsMatch() const
Ensure that chromatogram native ids match their keys in the map.
Definition: MRMTransitionGroup.h:299
void addPrecursorChromatogram(const ChromatogramType &chromatogram, const String &key)
Definition: MRMTransitionGroup.h:230
std::vector< TransitionType > TransitionsType
List of Reaction Monitoring transitions (meta data) type.
Definition: MRMTransitionGroup.h:51
MRMTransitionGroup()
Default constructor.
Definition: MRMTransitionGroup.h:60
bool hasTransition(const String &key) const
Definition: MRMTransitionGroup.h:144
MRMFeatureListType mrm_features_
feature list
Definition: MRMTransitionGroup.h:466
MRMTransitionGroup subsetDependent(std::vector< std::string > tr_ids) const
Definition: MRMTransitionGroup.h:386
TransitionsType transitions_
transition list
Definition: MRMTransitionGroup.h:457
std::map< String, int > chromatogram_map_
Definition: MRMTransitionGroup.h:468
MRMTransitionGroup & operator=(const MRMTransitionGroup &rhs)
Definition: MRMTransitionGroup.h:83
bool isMappingConsistent_() const
Checks that the mapping between chromatograms and transitions is consistent.
Definition: MRMTransitionGroup.h:437
const TransitionType & getTransition(const String &key)
Definition: MRMTransitionGroup.h:149
bool hasChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:188
void addFeature(const MRMFeature &feature)
Definition: MRMTransitionGroup.h:274
const std::vector< ChromatogramType > & getPrecursorChromatograms() const
Definition: MRMTransitionGroup.h:216
std::map< String, int > transition_map_
Definition: MRMTransitionGroup.h:470
Size size() const
Definition: MRMTransitionGroup.h:99
const std::vector< TransitionType > & getTransitions() const
Definition: MRMTransitionGroup.h:116
void setTransitionGroupID(const String &tr_gr_id)
Definition: MRMTransitionGroup.h:109
const std::vector< MRMFeature > & getFeatures() const
Definition: MRMTransitionGroup.h:264
ChromatogramType & getPrecursorChromatogram(const String &key)
Definition: MRMTransitionGroup.h:246
std::vector< ChromatogramType > & getChromatograms()
Definition: MRMTransitionGroup.h:160
void getLibraryIntensity(std::vector< double > &result) const
Definition: MRMTransitionGroup.h:318
const MRMFeature & getBestFeature() const
Returns the best feature by overall quality.
Definition: MRMTransitionGroup.h:416
The representation of a chromatogram.
Definition: MSChromatogram.h:31
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:190
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition: Peak2D.h:148
A more convenient string class.
Definition: String.h:34
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22