OpenMS
MRMTransitionGroup.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-2023.
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: Hannes Roest $
32 // $Authors: Hannes Roest $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Macros.h>
39 #include <boost/numeric/conversion/cast.hpp>
40 
41 namespace OpenMS
42 {
43 
66  template <typename ChromatogramType, typename TransitionType>
68  {
69 
70 public:
71 
73 
74  typedef std::vector<MRMFeature> MRMFeatureListType;
77  typedef std::vector<TransitionType> TransitionsType;
81 
87  {
88  }
89 
92  tr_gr_id_(rhs.tr_gr_id_),
100  {
101  }
102 
105  {
106  }
108 
110  {
111  if (&rhs != this)
112  {
113  tr_gr_id_ = rhs.tr_gr_id_;
121  }
122  return *this;
123  }
124 
125  inline Size size() const
126  {
127  return chromatograms_.size();
128  }
129 
130  inline const String & getTransitionGroupID() const
131  {
132  return tr_gr_id_;
133  }
134 
135  inline void setTransitionGroupID(const String & tr_gr_id)
136  {
137  tr_gr_id_ = tr_gr_id;
138  }
139 
141 
142  inline const std::vector<TransitionType> & getTransitions() const
143  {
144  return transitions_;
145  }
146 
147  inline std::vector<TransitionType> & getTransitionsMuteable()
148  {
149  return transitions_;
150  }
151 
159  inline void addTransition(const TransitionType& transition, const String& key)
160  {
161  // store the index where to find the transition, using the key for lookup
162  auto result = transition_map_.emplace(key, int(transitions_.size()));
163  if (!result.second) // ouch: key was already used
164  {
165  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Transition with nativeID was already present!", key);
166  }
167  transitions_.push_back(transition);
168  }
169 
170  inline bool hasTransition(const String& key) const
171  {
172  return transition_map_.find(key) != transition_map_.end();
173  }
174 
175  inline const TransitionType& getTransition(const String& key)
176  {
177  OPENMS_PRECONDITION(hasTransition(key), "Cannot retrieve transitions that does not exist")
178  OPENMS_PRECONDITION(transitions_.size() > (size_t)transition_map_[key], "Mapping needs to be accurate")
179  return transitions_[transition_map_[key]];
180  }
182 
183 
185 
186  inline std::vector<ChromatogramType>& getChromatograms()
187  {
188  return chromatograms_;
189  }
190 
191  inline const std::vector<ChromatogramType>& getChromatograms() const
192  {
193  return chromatograms_;
194  }
195 
203  inline void addChromatogram(const ChromatogramType& chromatogram, const String& key)
204  {
205  // store the index where to find the chromatogram, using the key for lookup
206  auto result = chromatogram_map_.emplace(key, int(chromatograms_.size()));
207  if (!result.second) // ouch: key was already used
208  {
209  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
210  }
211  chromatograms_.push_back(chromatogram);
212  }
213 
214  inline bool hasChromatogram(const String& key) const
215  {
216  return chromatogram_map_.find(key) != chromatogram_map_.end();
217  }
218 
220  {
221  OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
222  OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_[key], "Mapping needs to be accurate")
223  return chromatograms_[chromatogram_map_.at(key)];
224  }
225 
226  inline const ChromatogramType& getChromatogram(const String& key) const
227  {
228  OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
229  OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_.at(key), "Mapping needs to be accurate")
230  return chromatograms_[chromatogram_map_.at(key)];
231  }
233 
234 
236 
237  inline std::vector<ChromatogramType>& getPrecursorChromatograms()
238  {
240  }
241 
242  inline const std::vector<ChromatogramType>& getPrecursorChromatograms() const
243  {
245  }
246 
256  inline void addPrecursorChromatogram(const ChromatogramType& chromatogram, const String& key)
257  {
258  // store the index where to find the chromatogram, using the key for lookup
259  auto result = precursor_chromatogram_map_.emplace(key, int(precursor_chromatograms_.size()));
260  if (!result.second) // ouch: key was already used
261  {
262  throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
263  }
264  precursor_chromatograms_.push_back(chromatogram);
265  }
266 
267  inline bool hasPrecursorChromatogram(const String& key) const
268  {
270  }
271 
273  {
274  OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
275  OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
277  }
278 
279  inline const ChromatogramType & getPrecursorChromatogram(const String& key) const
280  {
281  OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
282  OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
284  }
286 
287 
289 
290  inline const std::vector<MRMFeature> & getFeatures() const
291  {
292  return mrm_features_;
293  }
294 
295  inline std::vector<MRMFeature> & getFeaturesMuteable()
296  {
297  return mrm_features_;
298  }
299 
300  inline void addFeature(const MRMFeature & feature)
301  {
302  mrm_features_.push_back(feature);
303  }
304 
305  inline void addFeature(MRMFeature && feature)
306  {
307  mrm_features_.push_back(std::move(feature));
308  }
310 
311 
313 
314 
316  inline bool isInternallyConsistent() const
317  {
318  OPENMS_PRECONDITION(transitions_.size() == chromatograms_.size(), "Same number of transitions as chromatograms are required")
319  OPENMS_PRECONDITION(transition_map_.size() == chromatogram_map_.size(), "Same number of transitions as chromatograms mappings are required")
320  OPENMS_PRECONDITION(isMappingConsistent_(), "Mapping needs to be consistent")
321  return true;
322  }
323 
325  inline bool chromatogramIdsMatch() const
326  {
327  for (std::map<String, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
328  {
329  if (getChromatogram(it->first).getNativeID() != it->first)
330  {
331  return false;
332  }
333  }
334  for (std::map<String, int>::const_iterator it = precursor_chromatogram_map_.begin(); it != precursor_chromatogram_map_.end(); it++)
335  {
336  if (getPrecursorChromatogram(it->first).getNativeID() != it->first)
337  {
338  return false;
339  }
340  }
341  return true;
342  }
343 
344  void getLibraryIntensity(std::vector<double> & result) const
345  {
346  for (typename TransitionsType::const_iterator it = transitions_.begin(); it != transitions_.end(); ++it)
347  {
348  result.push_back(it->getLibraryIntensity());
349  }
350  for (Size i = 0; i < result.size(); i++)
351  {
352  // the library intensity should never be below zero
353  if (result[i] < 0.0)
354  {
355  result[i] = 0.0;
356  }
357  }
358  }
359 
360  MRMTransitionGroup subset(std::vector<std::string> tr_ids) const
361  {
362  MRMTransitionGroup transition_group_subset;
363  transition_group_subset.setTransitionGroupID(tr_gr_id_);
364 
365  for (const auto& tr : transitions_)
366  {
367  if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
368  {
369  if (this->hasTransition(tr.getNativeID()))
370  {
371  transition_group_subset.addTransition(tr, tr.getNativeID());
372  }
373  if (this->hasChromatogram(tr.getNativeID()))
374  {
375  transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr.getNativeID())], tr.getNativeID());
376  }
377  }
378  }
379 
380  for (const auto& pc : precursor_chromatograms_)
381  {
382  // add precursor chromatograms if present
383  transition_group_subset.addPrecursorChromatogram(pc, pc.getNativeID());
384  }
385 
386  for (const auto& tgf : mrm_features_)
387  {
388  MRMFeature mf;
389  mf.setIntensity(tgf.getIntensity());
390  mf.setRT(tgf.getRT());
391  mf.MetaInfoInterface::operator=(tgf);
392 
393  for (const auto& tr : transitions_)
394  {
395  if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
396  {
397  mf.addFeature(tgf.getFeature(tr.getNativeID()),tr.getNativeID());
398  }
399  }
400  std::vector<String> pf_ids;
401  tgf.getPrecursorFeatureIDs(pf_ids);
402  for (const auto& pf_id : pf_ids)
403  {
404  mf.addPrecursorFeature(tgf.getPrecursorFeature(pf_id), pf_id);
405  }
406  transition_group_subset.addFeature(mf);
407  }
408 
409  return transition_group_subset;
410  }
411 
412  MRMTransitionGroup subsetDependent(std::vector<std::string> tr_ids) const
413  {
414  MRMTransitionGroup transition_group_subset;
415  transition_group_subset.setTransitionGroupID(tr_gr_id_);
416 
417  for (typename TransitionsType::const_iterator tr_it = transitions_.begin(); tr_it != transitions_.end(); ++tr_it)
418  {
419  if (std::find(tr_ids.begin(), tr_ids.end(), tr_it->getNativeID()) != tr_ids.end())
420  {
421  transition_group_subset.addTransition(*tr_it, tr_it->getNativeID());
422  transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr_it->getNativeID())], tr_it->getNativeID());
423  }
424  }
425 
426  for (std::vector< MRMFeature >::const_iterator tgf_it = mrm_features_.begin(); tgf_it != mrm_features_.end(); ++tgf_it)
427  {
428  transition_group_subset.addFeature(*tgf_it);
429  }
430 
431  return transition_group_subset;
432  }
434 
442  const MRMFeature& getBestFeature() const
443  {
444  OPENMS_PRECONDITION(!getFeatures().empty(), "Cannot get best feature for empty transition group")
445 
446  // Find the feature with the highest score
447  Size bestf = 0;
448  double highest_score = getFeatures()[0].getOverallQuality();
449  for (Size it = 0; it < getFeatures().size(); it++)
450  {
451  if (getFeatures()[it].getOverallQuality() > highest_score)
452  {
453  bestf = it;
454  highest_score = getFeatures()[it].getOverallQuality();
455  }
456  }
457  return getFeatures()[bestf];
458  }
459 
460 protected:
461 
463  bool isMappingConsistent_() const
464  {
465  if (transition_map_.size() != chromatogram_map_.size())
466  {
467  return false;
468  }
469  for (std::map<String, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
470  {
471  if (!hasTransition(it->first))
472  {
473  return false;
474  }
475  }
476  return true;
477  }
478 
481 
484 
486  std::vector<ChromatogramType> chromatograms_;
487 
489  std::vector<ChromatogramType> precursor_chromatograms_;
490 
493 
494  std::map<String, int> chromatogram_map_;
495  std::map<String, int> precursor_chromatogram_map_;
496  std::map<String, int> transition_map_;
497 
498  };
499 }
500 
A 1-dimensional raw data point or peak for chromatograms.
Definition: ChromatogramPeak.h:54
const String & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software.
Invalid value exception.
Definition: Exception.h:329
A multi-chromatogram MRM feature.
Definition: MRMFeature.h:52
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:68
const ChromatogramType & getChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:226
ChromatogramType::PeakType PeakType
Peak type.
Definition: MRMTransitionGroup.h:79
std::vector< ChromatogramType > & getPrecursorChromatograms()
Definition: MRMTransitionGroup.h:237
void addChromatogram(const ChromatogramType &chromatogram, const String &key)
Definition: MRMTransitionGroup.h:203
virtual ~MRMTransitionGroup()
Destructor.
Definition: MRMTransitionGroup.h:104
std::map< String, int > precursor_chromatogram_map_
Definition: MRMTransitionGroup.h:495
ChromatogramType & getChromatogram(const String &key)
Definition: MRMTransitionGroup.h:219
std::vector< MRMFeature > MRMFeatureListType
Type definitions.
Definition: MRMTransitionGroup.h:75
MRMTransitionGroup(const MRMTransitionGroup &rhs)
Copy Constructor.
Definition: MRMTransitionGroup.h:91
bool hasPrecursorChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:267
const std::vector< ChromatogramType > & getChromatograms() const
Definition: MRMTransitionGroup.h:191
std::vector< ChromatogramType > chromatograms_
chromatogram list
Definition: MRMTransitionGroup.h:486
std::vector< TransitionType > & getTransitionsMuteable()
Definition: MRMTransitionGroup.h:147
void addFeature(MRMFeature &&feature)
Definition: MRMTransitionGroup.h:305
std::vector< MRMFeature > & getFeaturesMuteable()
Definition: MRMTransitionGroup.h:295
MRMTransitionGroup subset(std::vector< std::string > tr_ids) const
Definition: MRMTransitionGroup.h:360
const String & getTransitionGroupID() const
Definition: MRMTransitionGroup.h:130
String tr_gr_id_
transition group id (peak group id)
Definition: MRMTransitionGroup.h:480
bool isInternallyConsistent() const
Check whether internal state is consistent, e.g. same number of chromatograms and transitions are pre...
Definition: MRMTransitionGroup.h:316
void addTransition(const TransitionType &transition, const String &key)
Definition: MRMTransitionGroup.h:159
std::vector< ChromatogramType > precursor_chromatograms_
precursor chromatogram list
Definition: MRMTransitionGroup.h:489
const ChromatogramType & getPrecursorChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:279
bool chromatogramIdsMatch() const
Ensure that chromatogram native ids match their keys in the map.
Definition: MRMTransitionGroup.h:325
void addPrecursorChromatogram(const ChromatogramType &chromatogram, const String &key)
Definition: MRMTransitionGroup.h:256
std::vector< TransitionType > TransitionsType
List of Reaction Monitoring transitions (meta data) type.
Definition: MRMTransitionGroup.h:77
MRMTransitionGroup()
Default constructor.
Definition: MRMTransitionGroup.h:86
bool hasTransition(const String &key) const
Definition: MRMTransitionGroup.h:170
MRMFeatureListType mrm_features_
feature list
Definition: MRMTransitionGroup.h:492
MRMTransitionGroup subsetDependent(std::vector< std::string > tr_ids) const
Definition: MRMTransitionGroup.h:412
TransitionsType transitions_
transition list
Definition: MRMTransitionGroup.h:483
std::map< String, int > chromatogram_map_
Definition: MRMTransitionGroup.h:494
MRMTransitionGroup & operator=(const MRMTransitionGroup &rhs)
Definition: MRMTransitionGroup.h:109
bool isMappingConsistent_() const
Checks that the mapping between chromatograms and transitions is consistent.
Definition: MRMTransitionGroup.h:463
const TransitionType & getTransition(const String &key)
Definition: MRMTransitionGroup.h:175
bool hasChromatogram(const String &key) const
Definition: MRMTransitionGroup.h:214
void addFeature(const MRMFeature &feature)
Definition: MRMTransitionGroup.h:300
const std::vector< ChromatogramType > & getPrecursorChromatograms() const
Definition: MRMTransitionGroup.h:242
std::map< String, int > transition_map_
Definition: MRMTransitionGroup.h:496
Size size() const
Definition: MRMTransitionGroup.h:125
const std::vector< TransitionType > & getTransitions() const
Definition: MRMTransitionGroup.h:142
void setTransitionGroupID(const String &tr_gr_id)
Definition: MRMTransitionGroup.h:135
const std::vector< MRMFeature > & getFeatures() const
Definition: MRMTransitionGroup.h:290
ChromatogramType & getPrecursorChromatogram(const String &key)
Definition: MRMTransitionGroup.h:272
std::vector< ChromatogramType > & getChromatograms()
Definition: MRMTransitionGroup.h:186
void getLibraryIntensity(std::vector< double > &result) const
Definition: MRMTransitionGroup.h:344
const MRMFeature & getBestFeature() const
Returns the best feature by overall quality.
Definition: MRMTransitionGroup.h:442
The representation of a chromatogram.
Definition: MSChromatogram.h:57
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:216
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition: Peak2D.h:174
A more convenient string class.
Definition: String.h:60
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48