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
187 inline bool hasChromatogram(const std::string& key) const
188 {
189 return chromatogram_map_.contains(key);
190 }
191
192 inline ChromatogramType& getChromatogram(const std::string& key)
193 {
194 OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
195 OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_[key], "Mapping needs to be accurate")
196 return chromatograms_[chromatogram_map_.at(key)];
197 }
198
199 inline const ChromatogramType& getChromatogram(const std::string& key) const
200 {
201 OPENMS_PRECONDITION(hasChromatogram(key), "Cannot retrieve chromatogram that does not exist")
202 OPENMS_PRECONDITION(chromatograms_.size() > (size_t)chromatogram_map_.at(key), "Mapping needs to be accurate")
203 return chromatograms_[chromatogram_map_.at(key)];
204 }
206
207
209
210 inline std::vector<ChromatogramType>& getPrecursorChromatograms()
211 {
213 }
214
215 inline const std::vector<ChromatogramType>& getPrecursorChromatograms() const
216 {
218 }
219
230 inline void addPrecursorChromatogram(const ChromatogramType& chromatogram, const std::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 std::string& key) const
242 {
243 return precursor_chromatogram_map_.contains(key);
244 }
245
246 inline ChromatogramType & getPrecursorChromatogram(const std::string& key)
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 std::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<std::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<std::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<std::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
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
434protected:
435
438 {
439 if (transition_map_.size() != chromatogram_map_.size())
440 {
441 return false;
442 }
443 for (std::map<std::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
454 std::string tr_gr_id_;
455
458
460 std::vector<ChromatogramType> chromatograms_;
461
463 std::vector<ChromatogramType> precursor_chromatograms_;
464
467
468 std::map<std::string, int> chromatogram_map_;
469 std::map<std::string, int> precursor_chromatogram_map_;
470 std::map<std::string, int> transition_map_;
471
472 };
473}
474
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:215
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:241
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:460
void addPrecursorChromatogram(const ChromatogramType &chromatogram, const std::string &key)
Definition MRMTransitionGroup.h:230
std::string tr_gr_id_
transition group id (peak group id)
Definition MRMTransitionGroup.h:454
ChromatogramType & getPrecursorChromatogram(const std::string &key)
Definition MRMTransitionGroup.h:246
void addFeature(MRMFeature &&feature)
Definition MRMTransitionGroup.h:279
const TransitionType & getTransition(const std::string &key)
Definition MRMTransitionGroup.h:148
ChromatogramType & getChromatogram(const std::string &key)
Definition MRMTransitionGroup.h:192
const ChromatogramType & getChromatogram(const std::string &key) const
Definition MRMTransitionGroup.h:199
MRMTransitionGroup subset(std::vector< std::string > tr_ids) const
Definition MRMTransitionGroup.h:334
bool isInternallyConsistent() const
Check whether internal state is consistent, e.g. same number of chromatograms and transitions are pre...
Definition MRMTransitionGroup.h:290
std::vector< ChromatogramType > precursor_chromatograms_
precursor chromatogram list
Definition MRMTransitionGroup.h:463
std::map< std::string, int > precursor_chromatogram_map_
Definition MRMTransitionGroup.h:469
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:299
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:470
const MRMFeature & getBestFeature() const
Returns the best feature by overall quality.
Definition MRMTransitionGroup.h:416
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:466
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:386
std::vector< MRMFeature > & getFeaturesMuteable()
Definition MRMTransitionGroup.h:269
TransitionsType transitions_
transition list
Definition MRMTransitionGroup.h:457
bool isMappingConsistent_() const
Checks that the mapping between chromatograms and transitions is consistent.
Definition MRMTransitionGroup.h:437
const std::vector< MRMFeature > & getFeatures() const
Definition MRMTransitionGroup.h:264
void addFeature(const MRMFeature &feature)
Definition MRMTransitionGroup.h:274
std::vector< ChromatogramType > & getPrecursorChromatograms()
Definition MRMTransitionGroup.h:210
const ChromatogramType & getPrecursorChromatogram(const std::string &key) const
Definition MRMTransitionGroup.h:253
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:187
bool hasTransition(const std::string &key) const
Definition MRMTransitionGroup.h:143
std::map< std::string, int > chromatogram_map_
Definition MRMTransitionGroup.h:468
void getLibraryIntensity(std::vector< double > &result) const
Definition MRMTransitionGroup.h:318
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