OpenMS
Loading...
Searching...
No Matches
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
13
14namespace OpenMS
15{
16
39 template <typename ChromatogramType, typename TransitionType>
41 {
42
43public:
44
46
47
48 typedef std::vector<MRMFeature> MRMFeatureListType;
50 typedef std::vector<TransitionType> TransitionsType;
54
60 {
61 }
62
75
78 {
79 }
81
97
98 inline Size size() const
99 {
100 return chromatograms_.size();
101 }
102
103 inline const std::string & getTransitionGroupID() const
104 {
105 return tr_gr_id_;
106 }
107
108 inline void setTransitionGroupID(const std::string & tr_gr_id)
109 {
110 tr_gr_id_ = tr_gr_id;
111 }
112
114
115 inline const std::vector<TransitionType> & getTransitions() const
116 {
117 return transitions_;
118 }
119
120 inline std::vector<TransitionType> & getTransitionsMuteable()
121 {
122 return transitions_;
123 }
124
132 inline void addTransition(const TransitionType& transition, const std::string& key)
133 {
134 // store the index where to find the transition, using the key for lookup
135 auto result = transition_map_.emplace(key, int(transitions_.size()));
136 if (!result.second) // ouch: key was already used
137 {
138 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Transition with nativeID was already present!", key);
139 }
140 transitions_.push_back(transition);
141 }
142
143 inline bool hasTransition(const std::string& key) const
144 {
145 return transition_map_.contains(key);
146 }
147
148 inline const TransitionType& getTransition(const std::string& key)
149 {
150 OPENMS_PRECONDITION(hasTransition(key), "Cannot retrieve transitions that does not exist")
151 OPENMS_PRECONDITION(transitions_.size() > (size_t)transition_map_[key], "Mapping needs to be accurate")
152 return transitions_[transition_map_[key]];
153 }
155
156
158
159 inline std::vector<ChromatogramType>& getChromatograms()
160 {
161 return chromatograms_;
162 }
163
164 inline const std::vector<ChromatogramType>& getChromatograms() const
165 {
166 return chromatograms_;
167 }
168
176 inline void addChromatogram(const ChromatogramType& chromatogram, const std::string& key)
177 {
178 // store the index where to find the chromatogram, using the key for lookup
179 auto result = chromatogram_map_.emplace(key, int(chromatograms_.size()));
180 if (!result.second) // ouch: key was already used
181 {
182 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
183 }
184 chromatograms_.push_back(chromatogram);
185 }
186
188 inline void addChromatogram(ChromatogramType&& chromatogram, const std::string& key)
189 {
190 // store the index where to find the chromatogram, using the key for lookup
191 auto result = chromatogram_map_.emplace(key, int(chromatograms_.size()));
192 if (!result.second) // ouch: key was already used
193 {
194 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
195 }
196 chromatograms_.push_back(std::move(chromatogram));
197 }
198
199 inline bool hasChromatogram(const std::string& key) const
200 {
201 return chromatogram_map_.contains(key);
202 }
203
204 inline ChromatogramType& getChromatogram(const std::string& key)
205 {
206 OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
207 OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_[key], "Mapping needs to be accurate")
208 return chromatograms_[chromatogram_map_.at(key)];
209 }
210
211 inline const ChromatogramType& getChromatogram(const std::string& key) const
212 {
213 OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
214 OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_.at(key), "Mapping needs to be accurate")
215 return chromatograms_[chromatogram_map_.at(key)];
216 }
218
219
221
222 inline std::vector<ChromatogramType>& getPrecursorChromatograms()
223 {
225 }
226
227 inline const std::vector<ChromatogramType>& getPrecursorChromatograms() const
228 {
230 }
231
242 inline void addPrecursorChromatogram(const ChromatogramType& chromatogram, const std::string& key)
243 {
244 // store the index where to find the chromatogram, using the key for lookup
245 auto result = precursor_chromatogram_map_.emplace(key, int(precursor_chromatograms_.size()));
246 if (!result.second) // ouch: key was already used
247 {
248 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Internal error: Chromatogram with nativeID was already present!", key);
249 }
250 precursor_chromatograms_.push_back(chromatogram);
251 }
252
253 inline bool hasPrecursorChromatogram(const std::string& key) const
254 {
255 return precursor_chromatogram_map_.contains(key);
256 }
257
258 inline ChromatogramType & getPrecursorChromatogram(const std::string& key)
259 {
260 OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
261 OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
263 }
264
265 inline const ChromatogramType & getPrecursorChromatogram(const std::string& key) const
266 {
267 OPENMS_PRECONDITION(hasPrecursorChromatogram(key), "Cannot retrieve precursor chromatogram that does not exist")
268 OPENMS_PRECONDITION(precursor_chromatograms_.size() > (size_t)precursor_chromatogram_map_.at(key), "Mapping needs to be accurate")
270 }
272
273
275
276 inline const std::vector<MRMFeature> & getFeatures() const
277 {
278 return mrm_features_;
279 }
280
281 inline std::vector<MRMFeature> & getFeaturesMuteable()
282 {
283 return mrm_features_;
284 }
285
286 inline void addFeature(const MRMFeature & feature)
287 {
288 mrm_features_.push_back(feature);
289 }
290
291 inline void addFeature(MRMFeature && feature)
292 {
293 mrm_features_.push_back(std::move(feature));
294 }
296
297
299
300
302 inline bool isInternallyConsistent() const
303 {
304 OPENMS_PRECONDITION(transitions_.size() == chromatograms_.size(), "Same number of transitions as chromatograms are required")
305 OPENMS_PRECONDITION(transition_map_.size() == chromatogram_map_.size(), "Same number of transitions as chromatograms mappings are required")
306 OPENMS_PRECONDITION(isMappingConsistent_(), "Mapping needs to be consistent")
307 return true;
308 }
309
311 inline bool chromatogramIdsMatch() const
312 {
313 for (std::map<std::string, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
314 {
315 if (getChromatogram(it->first).getNativeID() != it->first)
316 {
317 return false;
318 }
319 }
320 for (std::map<std::string, int>::const_iterator it = precursor_chromatogram_map_.begin(); it != precursor_chromatogram_map_.end(); it++)
321 {
322 if (getPrecursorChromatogram(it->first).getNativeID() != it->first)
323 {
324 return false;
325 }
326 }
327 return true;
328 }
329
330 void getLibraryIntensity(std::vector<double> & result) const
331 {
332 for (typename TransitionsType::const_iterator it = transitions_.begin(); it != transitions_.end(); ++it)
333 {
334 result.push_back(it->getLibraryIntensity());
335 }
336 for (Size i = 0; i < result.size(); i++)
337 {
338 // the library intensity should never be below zero
339 if (result[i] < 0.0)
340 {
341 result[i] = 0.0;
342 }
343 }
344 }
345
346 MRMTransitionGroup subset(std::vector<std::string> tr_ids) const
347 {
348 MRMTransitionGroup transition_group_subset;
349 transition_group_subset.setTransitionGroupID(tr_gr_id_);
350
351 for (const auto& tr : transitions_)
352 {
353 if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
354 {
355 if (this->hasTransition(tr.getNativeID()))
356 {
357 transition_group_subset.addTransition(tr, tr.getNativeID());
358 }
359 if (this->hasChromatogram(tr.getNativeID()))
360 {
361 transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr.getNativeID())], tr.getNativeID());
362 }
363 }
364 }
365
366 for (const auto& pc : precursor_chromatograms_)
367 {
368 // add precursor chromatograms if present
369 transition_group_subset.addPrecursorChromatogram(pc, pc.getNativeID());
370 }
371
372 for (const auto& tgf : mrm_features_)
373 {
374 MRMFeature mf;
375 mf.setIntensity(tgf.getIntensity());
376 mf.setRT(tgf.getRT());
377 mf.MetaInfoInterface::operator=(tgf);
378
379 for (const auto& tr : transitions_)
380 {
381 if (std::find(tr_ids.begin(), tr_ids.end(), tr.getNativeID()) != tr_ids.end())
382 {
383 mf.addFeature(tgf.getFeature(tr.getNativeID()),tr.getNativeID());
384 }
385 }
386 std::vector<std::string> pf_ids;
387 tgf.getPrecursorFeatureIDs(pf_ids);
388 for (const auto& pf_id : pf_ids)
389 {
390 mf.addPrecursorFeature(tgf.getPrecursorFeature(pf_id), pf_id);
391 }
392 transition_group_subset.addFeature(mf);
393 }
394
395 return transition_group_subset;
396 }
397
398 MRMTransitionGroup subsetDependent(std::vector<std::string> tr_ids) const
399 {
400 MRMTransitionGroup transition_group_subset;
401 transition_group_subset.setTransitionGroupID(tr_gr_id_);
402
403 for (typename TransitionsType::const_iterator tr_it = transitions_.begin(); tr_it != transitions_.end(); ++tr_it)
404 {
405 if (std::find(tr_ids.begin(), tr_ids.end(), tr_it->getNativeID()) != tr_ids.end())
406 {
407 transition_group_subset.addTransition(*tr_it, tr_it->getNativeID());
408 transition_group_subset.addChromatogram(chromatograms_[chromatogram_map_.at(tr_it->getNativeID())], tr_it->getNativeID());
409 }
410 }
411
412 for (std::vector< MRMFeature >::const_iterator tgf_it = mrm_features_.begin(); tgf_it != mrm_features_.end(); ++tgf_it)
413 {
414 transition_group_subset.addFeature(*tgf_it);
415 }
416
417 return transition_group_subset;
418 }
420
429 {
430 OPENMS_PRECONDITION(!getFeatures().empty(), "Cannot get best feature for empty transition group")
431
432 // Find the feature with the highest score
433 Size bestf = 0;
434 double highest_score = getFeatures()[0].getOverallQuality();
435 for (Size it = 0; it < getFeatures().size(); it++)
436 {
437 if (getFeatures()[it].getOverallQuality() > highest_score)
438 {
439 bestf = it;
440 highest_score = getFeatures()[it].getOverallQuality();
441 }
442 }
443 return getFeatures()[bestf];
444 }
445
446protected:
447
450 {
451 if (transition_map_.size() != chromatogram_map_.size())
452 {
453 return false;
454 }
455 for (std::map<std::string, int>::const_iterator it = chromatogram_map_.begin(); it != chromatogram_map_.end(); it++)
456 {
457 if (!hasTransition(it->first))
458 {
459 return false;
460 }
461 }
462 return true;
463 }
464
466 std::string tr_gr_id_;
467
470
472 std::vector<ChromatogramType> chromatograms_;
473
475 std::vector<ChromatogramType> precursor_chromatograms_;
476
479
480 std::map<std::string, int> chromatogram_map_;
481 std::map<std::string, int> precursor_chromatogram_map_;
482 std::map<std::string, int> transition_map_;
483
484 };
485}
486
A 1-dimensional raw data point or peak for chromatograms.
Definition ChromatogramPeak.h:29
const std::string & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software.
Invalid value exception.
Definition Exception.h:306
QualityType getOverallQuality() const
Non-mutable access to the overall quality.
A multi-chromatogram MRM feature.
Definition MRMFeature.h:26
void addFeature(const Feature &feature, const std::string &key)
Adds an feature from a single chromatogram into the feature.
void addPrecursorFeature(const Feature &feature, const std::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:41
void addChromatogram(const ChromatogramType &chromatogram, const std::string &key)
Definition MRMTransitionGroup.h:176
const std::vector< ChromatogramType > & getPrecursorChromatograms() const
Definition MRMTransitionGroup.h:227
ChromatogramType::PeakType PeakType
Peak type.
Definition MRMTransitionGroup.h:52
virtual ~MRMTransitionGroup()
Destructor.
Definition MRMTransitionGroup.h:77
bool hasPrecursorChromatogram(const std::string &key) const
Definition MRMTransitionGroup.h:253
const std::vector< TransitionType > & getTransitions() const
Definition MRMTransitionGroup.h:115
std::vector< MRMFeature > MRMFeatureListType
Type definitions.
Definition MRMTransitionGroup.h:48
MRMTransitionGroup(const MRMTransitionGroup &rhs)
Copy Constructor.
Definition MRMTransitionGroup.h:64
std::vector< ChromatogramType > chromatograms_
chromatogram list
Definition MRMTransitionGroup.h:472
void addPrecursorChromatogram(const ChromatogramType &chromatogram, const std::string &key)
Definition MRMTransitionGroup.h:242
std::string tr_gr_id_
transition group id (peak group id)
Definition MRMTransitionGroup.h:466
ChromatogramType & getPrecursorChromatogram(const std::string &key)
Definition MRMTransitionGroup.h:258
void addFeature(MRMFeature &&feature)
Definition MRMTransitionGroup.h:291
const TransitionType & getTransition(const std::string &key)
Definition MRMTransitionGroup.h:148
ChromatogramType & getChromatogram(const std::string &key)
Definition MRMTransitionGroup.h:204
const ChromatogramType & getChromatogram(const std::string &key) const
Definition MRMTransitionGroup.h:211
MRMTransitionGroup subset(std::vector< std::string > tr_ids) const
Definition MRMTransitionGroup.h:346
bool isInternallyConsistent() const
Check whether internal state is consistent, e.g. same number of chromatograms and transitions are pre...
Definition MRMTransitionGroup.h:302
std::vector< ChromatogramType > precursor_chromatograms_
precursor chromatogram list
Definition MRMTransitionGroup.h:475
std::map< std::string, int > precursor_chromatogram_map_
Definition MRMTransitionGroup.h:481
std::vector< TransitionType > & getTransitionsMuteable()
Definition MRMTransitionGroup.h:120
bool chromatogramIdsMatch() const
Ensure that chromatogram native ids match their keys in the map.
Definition MRMTransitionGroup.h:311
void setTransitionGroupID(const std::string &tr_gr_id)
Definition MRMTransitionGroup.h:108
std::vector< TransitionType > TransitionsType
List of Reaction Monitoring transitions (meta data) type.
Definition MRMTransitionGroup.h:50
std::map< std::string, int > transition_map_
Definition MRMTransitionGroup.h:482
const MRMFeature & getBestFeature() const
Returns the best feature by overall quality.
Definition MRMTransitionGroup.h:428
MRMTransitionGroup & operator=(const MRMTransitionGroup &rhs)
Definition MRMTransitionGroup.h:82
const std::vector< ChromatogramType > & getChromatograms() const
Definition MRMTransitionGroup.h:164
MRMTransitionGroup()
Default constructor.
Definition MRMTransitionGroup.h:59
const std::string & getTransitionGroupID() const
Definition MRMTransitionGroup.h:103
MRMFeatureListType mrm_features_
feature list
Definition MRMTransitionGroup.h:478
void addTransition(const TransitionType &transition, const std::string &key)
Definition MRMTransitionGroup.h:132
MRMTransitionGroup subsetDependent(std::vector< std::string > tr_ids) const
Definition MRMTransitionGroup.h:398
std::vector< MRMFeature > & getFeaturesMuteable()
Definition MRMTransitionGroup.h:281
TransitionsType transitions_
transition list
Definition MRMTransitionGroup.h:469
bool isMappingConsistent_() const
Checks that the mapping between chromatograms and transitions is consistent.
Definition MRMTransitionGroup.h:449
void addChromatogram(ChromatogramType &&chromatogram, const std::string &key)
Add a chromatogram (see above) by moving it into the transition group.
Definition MRMTransitionGroup.h:188
const std::vector< MRMFeature > & getFeatures() const
Definition MRMTransitionGroup.h:276
void addFeature(const MRMFeature &feature)
Definition MRMTransitionGroup.h:286
std::vector< ChromatogramType > & getPrecursorChromatograms()
Definition MRMTransitionGroup.h:222
const ChromatogramType & getPrecursorChromatogram(const std::string &key) const
Definition MRMTransitionGroup.h:265
Size size() const
Definition MRMTransitionGroup.h:98
std::vector< ChromatogramType > & getChromatograms()
Definition MRMTransitionGroup.h:159
bool hasChromatogram(const std::string &key) const
Definition MRMTransitionGroup.h:199
bool hasTransition(const std::string &key) const
Definition MRMTransitionGroup.h:143
std::map< std::string, int > chromatogram_map_
Definition MRMTransitionGroup.h:480
void getLibraryIntensity(std::vector< double > &result) const
Definition MRMTransitionGroup.h:330
The representation of a chromatogram.
Definition MSChromatogram.h:33
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition Peak2D.h:191
void setIntensity(IntensityType intensity)
Sets data point intensity (height)
Definition Peak2D.h:149
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:91
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19