OpenMS
MRMTransitionGroup.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: 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 
231  inline void addPrecursorChromatogram(const ChromatogramType& chromatogram, const String& key)
232  {
233  // store the index where to find the chromatogram, using the key for lookup
234  auto result = precursor_chromatogram_map_.emplace(key, int(precursor_chromatograms_.size()));
235  if (!result.second) // ouch: key was already used
236  {
237  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
238  }
239  precursor_chromatograms_.push_back(chromatogram);
240  }
241 
242  inline bool hasPrecursorChromatogram(const String& key) const
243  {
245  }
246 
248  {
249  OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
250  OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
252  }
253 
254  inline const ChromatogramType & getPrecursorChromatogram(const String& key) const
255  {
256  OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
257  OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
259  }
261 
262 
264 
265  inline const std::vector<MRMFeature> & getFeatures() const
266  {
267  return mrm_features_;
268  }
269 
270  inline std::vector<MRMFeature> & getFeaturesMuteable()
271  {
272  return mrm_features_;
273  }
274 
275  inline void addFeature(const MRMFeature & feature)
276  {
277  mrm_features_.push_back(feature);
278  }
279 
280  inline void addFeature(MRMFeature && feature)
281  {
282  mrm_features_.push_back(std::move(feature));
283  }
285 
286 
288 
289 
291  inline bool isInternallyConsistent() const
292  {
293  OPENMS_PRECONDITION(transitions_.size() == chromatograms_.size(), "Same number of transitions as chromatograms are required")
294  OPENMS_PRECONDITION(transition_map_.size() == chromatogram_map_.size(), "Same number of transitions as chromatograms mappings are required")
295  OPENMS_PRECONDITION(isMappingConsistent_(), "Mapping needs to be consistent")
296  return true;
297  }
298 
300  inline bool chromatogramIdsMatch() const
301  {
302  for (std::map<String, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
303  {
304  if (getChromatogram(it->first).getNativeID() != it->first)
305  {
306  return false;
307  }
308  }
309  for (std::map<String, int>::const_iterator it = precursor_chromatogram_map_.begin(); it != precursor_chromatogram_map_.end(); it++)
310  {
311  if (getPrecursorChromatogram(it->first).getNativeID() != it->first)
312  {
313  return false;
314  }
315  }
316  return true;
317  }
318 
319  void getLibraryIntensity(std::vector<double> & result) const
320  {
321  for (typename TransitionsType::const_iterator it = transitions_.begin(); it != transitions_.end(); ++it)
322  {
323  result.push_back(it->getLibraryIntensity());
324  }
325  for (Size i = 0; i < result.size(); i++)
326  {
327  // the library intensity should never be below zero
328  if (result[i] < 0.0)
329  {
330  result[i] = 0.0;
331  }
332  }
333  }
334 
335  MRMTransitionGroup subset(std::vector<std::string> tr_ids) const
336  {
337  MRMTransitionGroup transition_group_subset;
338  transition_group_subset.setTransitionGroupID(tr_gr_id_);
339 
340  for (const auto& tr : transitions_)
341  {
342  if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
343  {
344  if (this->hasTransition(tr.getNativeID()))
345  {
346  transition_group_subset.addTransition(tr, tr.getNativeID());
347  }
348  if (this->hasChromatogram(tr.getNativeID()))
349  {
350  transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr.getNativeID())], tr.getNativeID());
351  }
352  }
353  }
354 
355  for (const auto& pc : precursor_chromatograms_)
356  {
357  // add precursor chromatograms if present
358  transition_group_subset.addPrecursorChromatogram(pc, pc.getNativeID());
359  }
360 
361  for (const auto& tgf : mrm_features_)
362  {
363  MRMFeature mf;
364  mf.setIntensity(tgf.getIntensity());
365  mf.setRT(tgf.getRT());
366  mf.MetaInfoInterface::operator=(tgf);
367 
368  for (const auto& tr : transitions_)
369  {
370  if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
371  {
372  mf.addFeature(tgf.getFeature(tr.getNativeID()),tr.getNativeID());
373  }
374  }
375  std::vector<String> pf_ids;
376  tgf.getPrecursorFeatureIDs(pf_ids);
377  for (const auto& pf_id : pf_ids)
378  {
379  mf.addPrecursorFeature(tgf.getPrecursorFeature(pf_id), pf_id);
380  }
381  transition_group_subset.addFeature(mf);
382  }
383 
384  return transition_group_subset;
385  }
386 
387  MRMTransitionGroup subsetDependent(std::vector<std::string> tr_ids) const
388  {
389  MRMTransitionGroup transition_group_subset;
390  transition_group_subset.setTransitionGroupID(tr_gr_id_);
391 
392  for (typename TransitionsType::const_iterator tr_it = transitions_.begin(); tr_it != transitions_.end(); ++tr_it)
393  {
394  if (std::find(tr_ids.begin(), tr_ids.end(), tr_it->getNativeID()) != tr_ids.end())
395  {
396  transition_group_subset.addTransition(*tr_it, tr_it->getNativeID());
397  transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr_it->getNativeID())], tr_it->getNativeID());
398  }
399  }
400 
401  for (std::vector< MRMFeature >::const_iterator tgf_it = mrm_features_.begin(); tgf_it != mrm_features_.end(); ++tgf_it)
402  {
403  transition_group_subset.addFeature(*tgf_it);
404  }
405 
406  return transition_group_subset;
407  }
409 
417  const MRMFeature& getBestFeature() const
418  {
419  OPENMS_PRECONDITION(!getFeatures().empty(), "Cannot get best feature for empty transition group")
420 
421  // Find the feature with the highest score
422  Size bestf = 0;
423  double highest_score = getFeatures()[0].getOverallQuality();
424  for (Size it = 0; it < getFeatures().size(); it++)
425  {
426  if (getFeatures()[it].getOverallQuality() > highest_score)
427  {
428  bestf = it;
429  highest_score = getFeatures()[it].getOverallQuality();
430  }
431  }
432  return getFeatures()[bestf];
433  }
434 
435 protected:
436 
438  bool isMappingConsistent_() const
439  {
440  if (transition_map_.size() != chromatogram_map_.size())
441  {
442  return false;
443  }
444  for (std::map<String, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
445  {
446  if (!hasTransition(it->first))
447  {
448  return false;
449  }
450  }
451  return true;
452  }
453 
456 
459 
461  std::vector<ChromatogramType> chromatograms_;
462 
464  std::vector<ChromatogramType> precursor_chromatograms_;
465 
468 
469  std::map<String, int> chromatogram_map_;
470  std::map<String, int> precursor_chromatogram_map_;
471  std::map<String, int> transition_map_;
472 
473  };
474 }
475 
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:305
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:470
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:242
const std::vector< ChromatogramType > & getChromatograms() const
Definition: MRMTransitionGroup.h:165
std::vector< ChromatogramType > chromatograms_
chromatogram list
Definition: MRMTransitionGroup.h:461
std::vector< TransitionType > & getTransitionsMuteable()
Definition: MRMTransitionGroup.h:121
void addFeature(MRMFeature &&feature)
Definition: MRMTransitionGroup.h:280
std::vector< MRMFeature > & getFeaturesMuteable()
Definition: MRMTransitionGroup.h:270
MRMTransitionGroup subset(std::vector< std::string > tr_ids) const
Definition: MRMTransitionGroup.h:335
const String & getTransitionGroupID() const
Definition: MRMTransitionGroup.h:104
String tr_gr_id_
transition group id (peak group id)
Definition: MRMTransitionGroup.h:455
bool isInternallyConsistent() const
Check whether internal state is consistent, e.g. same number of chromatograms and transitions are pre...
Definition: MRMTransitionGroup.h:291
void addTransition(const TransitionType &transition, const String &key)
Definition: MRMTransitionGroup.h:133
std::vector< ChromatogramType > precursor_chromatograms_
precursor chromatogram list
Definition: MRMTransitionGroup.h:464
const ChromatogramType & getPrecursorChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:254
bool chromatogramIdsMatch() const
Ensure that chromatogram native ids match their keys in the map.
Definition: MRMTransitionGroup.h:300
void addPrecursorChromatogram(const ChromatogramType &chromatogram, const String &key)
Definition: MRMTransitionGroup.h:231
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:467
MRMTransitionGroup subsetDependent(std::vector< std::string > tr_ids) const
Definition: MRMTransitionGroup.h:387
TransitionsType transitions_
transition list
Definition: MRMTransitionGroup.h:458
std::map< String, int > chromatogram_map_
Definition: MRMTransitionGroup.h:469
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:438
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:275
const std::vector< ChromatogramType > & getPrecursorChromatograms() const
Definition: MRMTransitionGroup.h:216
std::map< String, int > transition_map_
Definition: MRMTransitionGroup.h:471
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:265
ChromatogramType & getPrecursorChromatogram(const String &key)
Definition: MRMTransitionGroup.h:247
std::vector< ChromatogramType > & getChromatograms()
Definition: MRMTransitionGroup.h:160
void getLibraryIntensity(std::vector< double > &result) const
Definition: MRMTransitionGroup.h:319
const MRMFeature & getBestFeature() const
Returns the best feature by overall quality.
Definition: MRMTransitionGroup.h:417
The representation of a chromatogram.
Definition: MSChromatogram.h:30
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:97
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19