35 #ifndef OPENMS_ANALYSIS_OPENSWATH_MRMTRANSITIONGROUPPICKER_H
36 #define OPENMS_ANALYSIS_OPENSWATH_MRMTRANSITIONGROUPPICKER_H
117 template <
typename SpectrumT,
typename TransitionT>
120 std::vector<RichPeakChromatogram> picked_chroms_;
132 !transition_group.
getTransition(native_id).isDetectingTransition() )
141 RichPeakChromatogram picked_chrom;
144 picked_chroms_.push_back(picked_chrom);
151 int chr_idx, peak_idx, cnt = 0;
152 std::vector<MRMFeature> features;
155 chr_idx = -1; peak_idx = -1;
156 findLargestPeak(picked_chroms_, chr_idx, peak_idx);
157 if (chr_idx == -1 && peak_idx == -1)
break;
160 MRMFeature mrm_feature = createMRMFeature(transition_group, picked_chroms_, chr_idx, peak_idx);
163 features.push_back(mrm_feature);
167 if ((stop_after_feature_ > 0 && cnt > stop_after_feature_) &&
175 for (
Size i = 0; i < features.size(); i++)
179 for (
Size j = 0; j < i; j++)
181 if ((
double)mrm_feature.
getMetaValue(
"leftWidth") >= (
double)features[j].getMetaValue(
"leftWidth") &&
194 template <
typename SpectrumT,
typename TransitionT>
196 std::vector<SpectrumT>& picked_chroms,
int& chr_idx,
int& peak_idx)
200 double best_left = picked_chroms[chr_idx].getFloatDataArrays()[1][peak_idx];
201 double best_right = picked_chroms[chr_idx].getFloatDataArrays()[2][peak_idx];
202 double peak_apex = picked_chroms[chr_idx][peak_idx].getRT();
203 LOG_DEBUG <<
"**** Creating MRMFeature for peak " << chr_idx <<
" " << peak_idx <<
" " <<
204 picked_chroms[chr_idx][peak_idx] <<
" with borders " << best_left <<
" " <<
205 best_right <<
" (" << best_right - best_left <<
")" << std::endl;
207 if (recalculate_peaks_)
210 recalculatePeakBorders_(picked_chroms, best_left, best_right, recalculate_peaks_max_z_);
211 if (peak_apex < best_left || peak_apex > best_right)
214 peak_apex = (best_left + best_right) / 2.0;
217 picked_chroms[chr_idx][peak_idx].setIntensity(0.0);
222 remove_overlapping_features(picked_chroms, best_left, best_right);
225 if (min_peak_width_ > 0.0 && std::fabs(best_right - best_left) < min_peak_width_) {
return mrmFeature;}
227 if (compute_peak_quality_)
230 double qual = computeQuality_(transition_group, picked_chroms, chr_idx, best_left, best_right, outlier);
231 if (qual < min_qual_) {
return mrmFeature; }
240 SpectrumT master_peak_container;
241 const SpectrumT& ref_chromatogram = transition_group.
getChromatogram(picked_chroms[chr_idx].getNativeID());
242 prepareMasterContainer_(ref_chromatogram, master_peak_container, best_left, best_right);
244 double total_intensity = 0;
double total_peak_apices = 0;
double total_xic = 0;
250 for (
typename SpectrumT::const_iterator it = chromatogram.begin(); it != chromatogram.end(); it++)
252 total_xic += it->getIntensity();
257 const SpectrumT used_chromatogram = resampleChromatogram_(chromatogram, master_peak_container, best_left, best_right);
266 double intensity_sum(0.0), rt_sum(0.0);
267 double peak_apex_int = -1;
268 double peak_apex_dist = std::fabs(used_chromatogram.begin()->getMZ() - peak_apex);
270 for (
typename SpectrumT::const_iterator it = used_chromatogram.begin(); it != used_chromatogram.end(); it++)
272 if (it->getMZ() > best_left && it->getMZ() < best_right)
276 p[1] = it->getIntensity();
277 hull_points.push_back(p);
278 if (std::fabs(it->getMZ() - peak_apex) <= peak_apex_dist)
280 peak_apex_int = p[1];
281 peak_apex_dist = std::fabs(it->getMZ() - peak_apex);
283 rt_sum += it->getMZ();
284 intensity_sum += it->getIntensity();
288 if (background_subtraction_ !=
"none")
290 double background = 0;
291 if (background_subtraction_ ==
"smoothed")
307 else if (background_subtraction_ ==
"original")
309 background = calculateBgEstimation_(used_chromatogram, best_left, best_right);
311 intensity_sum -= background;
312 if (intensity_sum < 0)
314 std::cerr <<
"Warning: Intensity was below 0 after background subtraction: " << intensity_sum <<
". Setting it to 0." << std::endl;
319 f.
setRT(picked_chroms[chr_idx][peak_idx].getMZ());
320 f.
setMZ(chromatogram.getMetaValue(
"product_mz"));
325 f.
setMetaValue(
"MZ", chromatogram.getMetaValue(
"product_mz"));
326 f.
setMetaValue(
"native_id", chromatogram.getNativeID());
333 total_intensity += intensity_sum;
334 total_peak_apices += peak_apex_int;
336 mrmFeature.
addFeature(f, chromatogram.getNativeID());
345 const SpectrumT used_chromatogram = resampleChromatogram_(chromatogram, master_peak_container, best_left, best_right);
353 double intensity_sum(0.0), rt_sum(0.0);
354 double peak_apex_int = -1;
355 double peak_apex_dist = std::fabs(used_chromatogram.begin()->getMZ() - peak_apex);
357 for (
typename SpectrumT::const_iterator it = used_chromatogram.begin(); it != used_chromatogram.end(); it++)
359 if (it->getMZ() > best_left && it->getMZ() < best_right)
363 p[1] = it->getIntensity();
364 hull_points.push_back(p);
365 if (std::fabs(it->getMZ() - peak_apex) <= peak_apex_dist)
367 peak_apex_int = p[1];
368 peak_apex_dist = std::fabs(it->getMZ() - peak_apex);
370 rt_sum += it->getMZ();
371 intensity_sum += it->getIntensity();
375 if (chromatogram.metaValueExists(
"precursor_mz"))
377 f.
setMZ(chromatogram.getMetaValue(
"precursor_mz"));
379 f.
setRT(picked_chroms[chr_idx][peak_idx].getMZ());
384 f.
setMetaValue(
"native_id", chromatogram.getNativeID());
390 mrmFeature.
setRT(peak_apex);
396 mrmFeature.
setMetaValue(
"peak_apices_sum", total_peak_apices);
415 template <
typename SpectrumT>
420 for (
Size k = 0;
k < picked_chroms.size();
k++)
422 for (
Size i = 0; i < picked_chroms[
k].size(); i++)
424 if (picked_chroms[
k][i].getMZ() >= best_left && picked_chroms[
k][i].getMZ() <= best_right)
428 picked_chroms[
k][i].setIntensity(0.0);
434 for (
Size k = 0;
k < picked_chroms.size();
k++)
436 for (
Size i = 0; i < picked_chroms[
k].size(); i++)
438 if (picked_chroms[
k][i].getIntensity() <= 0.0) {
continue; }
440 double left = picked_chroms[
k].getFloatDataArrays()[1][i];
441 double right = picked_chroms[
k].getFloatDataArrays()[2][i];
442 if ((left > best_left && left < best_right)
443 || (right > best_left && right < best_right))
447 picked_chroms[
k][i].setIntensity(0.0);
454 void findLargestPeak(std::vector<RichPeakChromatogram>& picked_chroms,
int& chr_idx,
int& peak_idx);
459 void updateMembers_();
480 template <
typename SpectrumT,
typename TransitionT>
482 std::vector<SpectrumT>& picked_chroms,
const int chr_idx,
483 const double best_left,
const double best_right,
String& outlier)
489 double resample_boundary = 15.0;
490 SpectrumT master_peak_container;
491 const SpectrumT& ref_chromatogram = transition_group.
getChromatogram(picked_chroms[chr_idx].getNativeID());
492 prepareMasterContainer_(ref_chromatogram, master_peak_container, best_left - resample_boundary, best_right + resample_boundary);
493 std::vector<std::vector<double> > all_ints;
494 for (
Size k = 0;
k < picked_chroms.size();
k++)
496 const SpectrumT& chromatogram = transition_group.
getChromatogram(picked_chroms[
k].getNativeID());
497 const SpectrumT used_chromatogram = resampleChromatogram_(chromatogram,
498 master_peak_container, best_left - resample_boundary, best_right + resample_boundary);
500 std::vector<double> int_here;
501 for (
Size i = 0; i < used_chromatogram.size(); i++)
503 int_here.push_back(used_chromatogram[i].getIntensity());
505 all_ints.push_back(int_here);
509 std::vector<double> mean_shapes;
510 std::vector<double> mean_coel;
511 for (
Size k = 0;
k < all_ints.size();
k++)
513 std::vector<double> shapes;
514 std::vector<double> coel;
515 for (
Size i = 0; i < all_ints.size(); i++)
517 if (i ==
k) {
continue;}
519 all_ints[
k], all_ints[i], boost::numeric_cast<int>(all_ints[i].size()), 1);
525 shapes.push_back(res_shape);
526 coel.push_back(res_coelution);
532 msc = std::for_each(shapes.begin(), shapes.end(), msc);
533 double shapes_mean = msc.
mean();
534 msc = std::for_each(coel.begin(), coel.end(), msc);
535 double coel_mean = msc.
mean();
539 mean_shapes.push_back(shapes_mean);
540 mean_coel.push_back(coel_mean);
546 int min_index_shape = std::distance(mean_shapes.begin(), std::min_element(mean_shapes.begin(), mean_shapes.end()));
547 int max_index_coel = std::distance(mean_coel.begin(), std::max_element(mean_coel.begin(), mean_coel.end()));
550 int missing_peaks = 0;
551 int multiple_peaks = 0;
554 std::vector<double> left_borders;
555 std::vector<double> right_borders;
556 for (
Size k = 0;
k < picked_chroms.size();
k++)
565 for (
Size i = 0; i < picked_chroms[
k].size(); i++)
567 if (picked_chroms[
k][i].getMZ() >= best_left && picked_chroms[
k][i].getMZ() <= best_right)
570 if (picked_chroms[
k][i].getIntensity() > max_int)
572 max_int = picked_chroms[
k][i].getIntensity() > max_int;
573 l_tmp = picked_chroms[
k].getFloatDataArrays()[1][i];
574 r_tmp = picked_chroms[
k].getFloatDataArrays()[2][i];
579 if (l_tmp > 0.0) left_borders.push_back(l_tmp);
580 if (r_tmp > 0.0) right_borders.push_back(r_tmp);
582 if (pfound == 0) missing_peaks++;
583 if (pfound > 1) multiple_peaks++;
588 LOG_DEBUG <<
" Overall found missing : " << missing_peaks <<
" and multiple : " << multiple_peaks << std::endl;
594 if (min_index_shape == max_index_coel)
596 LOG_DEBUG <<
" element " << min_index_shape <<
" is a candidate for removal ... " << std::endl;
609 double shape_score = std::accumulate(mean_shapes.begin(), mean_shapes.end(), 0.0) / mean_shapes.size();
610 double coel_score = std::accumulate(mean_coel.begin(), mean_coel.end(), 0.0) / mean_coel.size();
611 coel_score = (coel_score - 1.0) / 2.0;
613 double score = shape_score - coel_score - 1.0 * missing_peaks / picked_chroms.size();
615 LOG_DEBUG <<
" computed score " << score <<
" (from " << shape_score <<
616 " - " << coel_score <<
" - " << 1.0 * missing_peaks / picked_chroms.size() <<
")" << std::endl;
630 template <
typename SpectrumT>
637 std::vector<double> left_borders;
638 std::vector<double> right_borders;
639 for (
Size k = 0;
k < picked_chroms.size();
k++)
644 for (
Size i = 0; i < picked_chroms[
k].size(); i++)
646 if (picked_chroms[
k][i].getMZ() >= best_left && picked_chroms[
k][i].getMZ() <= best_right)
648 if (picked_chroms[
k].getFloatDataArrays()[0][i] > max_int)
650 max_int = picked_chroms[
k].getFloatDataArrays()[0][i];
651 left = picked_chroms[
k].getFloatDataArrays()[1][i];
652 right = picked_chroms[
k].getFloatDataArrays()[2][i];
658 left_borders.push_back(left);
659 right_borders.push_back(right);
660 LOG_DEBUG <<
" * " <<
k <<
" left boundary " << left_borders.back() <<
" with int " << max_int << std::endl;
661 LOG_DEBUG <<
" * " <<
k <<
" right boundary " << right_borders.back() <<
" with int " << max_int << std::endl;
666 if (right_borders.empty())
684 mean = std::accumulate(right_borders.begin(), right_borders.end(), 0.0) / (
double) right_borders.size();
685 stdev = std::sqrt(std::inner_product(right_borders.begin(), right_borders.end(), right_borders.begin(), 0.0)
686 / right_borders.size() - mean *
mean);
687 std::sort(right_borders.begin(), right_borders.end());
689 LOG_DEBUG <<
" - Recalculating right peak boundaries " << mean <<
" mean / best "
690 << best_right <<
" std " << stdev <<
" : " << std::fabs(best_right - mean) / stdev
691 <<
" coefficient of variation" << std::endl;
694 if (std::fabs(best_right - mean) / stdev > max_z)
696 best_right = right_borders[right_borders.size() / 2];
697 LOG_DEBUG <<
" - Setting right boundary to " << best_right << std::endl;
701 mean = std::accumulate(left_borders.begin(), left_borders.end(), 0.0) / (
double) left_borders.size();
702 stdev = std::sqrt(std::inner_product(left_borders.begin(), left_borders.end(), left_borders.begin(), 0.0)
703 / left_borders.size() - mean *
mean);
704 std::sort(left_borders.begin(), left_borders.end());
706 LOG_DEBUG <<
" - Recalculating left peak boundaries " << mean <<
" mean / best "
707 << best_left <<
" std " << stdev <<
" : " << std::fabs(best_left - mean) / stdev
708 <<
" coefficient of variation" << std::endl;
711 if (std::fabs(best_left - mean) / stdev > max_z)
713 best_left = left_borders[left_borders.size() / 2];
714 LOG_DEBUG <<
" - Setting left boundary to " << best_left << std::endl;
721 template <
typename SpectrumT>
724 SpectrumT& master_peak_container,
double best_left,
double best_right)
727 typename SpectrumT::const_iterator begin = ref_chromatogram.begin();
728 while (begin != ref_chromatogram.end() && begin->getMZ() < best_left) {begin++; }
729 if (begin != ref_chromatogram.begin()) {begin--; }
731 typename SpectrumT::const_iterator end = begin;
732 while (end != ref_chromatogram.end() && end->getMZ() < best_right) {end++; }
733 if (end != ref_chromatogram.end()) {end++; }
736 master_peak_container.resize(distance(begin, end));
737 typename SpectrumT::iterator it = master_peak_container.begin();
738 for (
typename SpectrumT::const_iterator chrom_it = begin; chrom_it != end; chrom_it++, it++)
740 it->setMZ(chrom_it->getMZ());
745 template <
typename SpectrumT>
747 SpectrumT& master_peak_container,
double best_left,
double best_right)
752 typename SpectrumT::const_iterator begin = chromatogram.begin();
753 while (begin != chromatogram.end() && begin->getMZ() < best_left) {begin++; }
754 if (begin != chromatogram.begin()) {begin--; }
756 typename SpectrumT::const_iterator end = begin;
757 while (end != chromatogram.end() && end->getMZ() < best_right) {end++; }
758 if (end != chromatogram.end()) {end++; }
760 SpectrumT resampled_peak_container = master_peak_container;
762 lresampler.
raster(begin, end, resampled_peak_container.begin(), resampled_peak_container.end());
764 return resampled_peak_container;
775 double calculateBgEstimation_(
const RichPeakChromatogram& chromatogram,
double best_left,
double best_right);
790 #endif // OPENMS_ANALYSIS_OPENSWATH_MRMTRANSITIONGROUPPICKER_H
void raster(SpecT< PeakType > &spectrum)
Applies the resampling algorithm to an MSSpectrum.
Definition: LinearResamplerAlign.h:75
double min_qual_
Definition: MRMTransitionGroupPicker.h:781
String background_subtraction_
Definition: MRMTransitionGroupPicker.h:778
const std::vector< TransitionType > & getTransitions() const
Definition: MRMTransitionGroup.h:125
double recalculate_peaks_max_z_
Definition: MRMTransitionGroupPicker.h:786
A more convenient string class.
Definition: String.h:57
bool compute_peak_quality_
Definition: MRMTransitionGroupPicker.h:780
void setMZ(CoordinateType coordinate)
Mutable access to the m/z coordinate (index 1)
Definition: Peak2D.h:203
void sortByPosition()
Lexicographically sorts the peaks by their position.
Definition: MSSpectrum.h:419
IntensityType getIntensity() const
Definition: Peak2D.h:167
bool hasTransition(String key)
Definition: MRMTransitionGroup.h:146
OPENSWATHALGO_DLLAPI XCorrArrayType normalizedCrossCorrelation(std::vector< double > &data1, std::vector< double > &data2, int maxdelay, int lag)
const std::vector< SpectrumType > & getChromatograms() const
Definition: MRMTransitionGroup.h:151
SpectrumType & getPrecursorChromatogram(String key)
Definition: MRMTransitionGroup.h:193
std::vector< PointType > PointArrayType
Definition: ConvexHull2D.h:77
bool isSorted() const
Checks if all peaks are sorted with respect to ascending m/z.
Definition: MSSpectrum.h:481
void prepareMasterContainer_(const SpectrumT &ref_chromatogram, SpectrumT &master_peak_container, double best_left, double best_right)
create an empty master peak container that has the correct mz / RT values set
Definition: MRMTransitionGroupPicker.h:723
The PeakPickerMRM finds peaks a single chromatogram.
Definition: PeakPickerMRM.h:63
int stop_after_feature_
Definition: MRMTransitionGroupPicker.h:783
double mean() const
Definition: StatsHelpers.h:208
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
void setParameters(const Param ¶m)
Sets the parameters.
A 2-dimensional hull representation in [counter]clockwise direction - depending on axis labelling...
Definition: ConvexHull2D.h:73
#define LOG_DEBUG
Macro for general debugging information.
Definition: LogStream.h:459
static double mean(IteratorType begin, IteratorType end)
Calculates the mean of a range of values.
Definition: StatisticFunctions.h:131
void setIntensity(IntensityType intensity)
Non-mutable access to the data point intensity (height)
Definition: Peak2D.h:173
void addFeature(Feature &feature, const String &key)
Adds an feature from a single chromatogram into the feature.
double min_peak_width_
Definition: MRMTransitionGroupPicker.h:785
The representation of a 1D spectrum.
Definition: MSSpectrum.h:66
void setRT(CoordinateType coordinate)
Mutable access to the RT coordinate (index 0)
Definition: Peak2D.h:215
The MRMTransitionGroupPicker finds peaks in chromatograms that belong to the same precursors...
Definition: MRMTransitionGroupPicker.h:81
bool recalculate_peaks_
Definition: MRMTransitionGroupPicker.h:779
const std::vector< ConvexHull2D > & getConvexHulls() const
Non-mutable access to the convex hulls.
void sortByIntensity(bool reverse=false)
Lexicographically sorts the peaks by their intensity.
Definition: MSSpectrum.h:342
void setQuality(Size index, QualityType q)
Set the quality in dimension c.
Linear Resampling of raw data with alignment.
Definition: LinearResamplerAlign.h:58
void pickChromatogram(const RichPeakChromatogram &chromatogram, RichPeakChromatogram &picked_chrom)
Finds peaks in a single chromatogram and annotates left/right borders.
void setHullPoints(const PointArrayType &points)
accessor for the outer(!) points (no checking is performed if this is actually a convex hull) ...
double computeQuality_(MRMTransitionGroup< SpectrumT, TransitionT > &transition_group, std::vector< SpectrumT > &picked_chroms, const int chr_idx, const double best_left, const double best_right, String &outlier)
Compute transition group quality (higher score is better)
Definition: MRMTransitionGroupPicker.h:481
An LC-MS feature.
Definition: Feature.h:70
functor to compute the mean and stddev of sequence using the std::foreach algorithm ...
Definition: StatsHelpers.h:170
void addFeature(MRMFeature &feature)
Definition: MRMTransitionGroup.h:213
void addPrecursorFeature(Feature &feature, const String &key)
Adds a precursor feature from a single chromatogram into the feature.
void setOverallQuality(QualityType q)
Set the overall quality.
bool hasPrecursorChromatogram(String key) const
Definition: MRMTransitionGroup.h:198
SpectrumT resampleChromatogram_(const SpectrumT &chromatogram, SpectrumT &master_peak_container, double best_left, double best_right)
use the master container from above to resample a chromatogram at those points stored in the master c...
Definition: MRMTransitionGroupPicker.h:746
SpectrumType & getChromatogram(String key)
Definition: MRMTransitionGroup.h:167
void pickTransitionGroup(MRMTransitionGroup< SpectrumT, TransitionT > &transition_group)
Pick a group of chromatograms belonging to the same peptide.
Definition: MRMTransitionGroupPicker.h:118
A base class for all classes handling default parameters.
Definition: DefaultParamHandler.h:92
MRMFeature createMRMFeature(MRMTransitionGroup< SpectrumT, TransitionT > &transition_group, std::vector< SpectrumT > &picked_chroms, int &chr_idx, int &peak_idx)
Create feature from a vector of chromatograms and a specified peak.
Definition: MRMTransitionGroupPicker.h:195
void remove_overlapping_features(std::vector< SpectrumT > &picked_chroms, double best_left, double best_right)
Remove overlapping features.
Definition: MRMTransitionGroupPicker.h:416
OPENSWATHALGO_DLLAPI XCorrArrayType::iterator xcorrArrayGetMaxPeak(XCorrArrayType &array)
Find best peak in an cross-correlation (highest apex)
double stop_after_intensity_ratio_
Definition: MRMTransitionGroupPicker.h:784
A multi-chromatogram MRM feature.
Definition: MRMFeature.h:50
const String & getNativeID() const
returns the native identifier for the spectrum, used by the acquisition software. ...
void recalculatePeakBorders_(std::vector< SpectrumT > &picked_chroms, double &best_left, double &best_right, double max_z)
Recalculate the borders of the peak.
Definition: MRMTransitionGroupPicker.h:631
MSSpectrum< ChromatogramPeak > RichPeakChromatogram
Definition: MRMTransitionGroupPicker.h:88
const String & getTransitionGroupID() const
Definition: MRMTransitionGroup.h:115
Not implemented exception.
Definition: Exception.h:437
Size ensureUniqueId()
Assigns a valid unique id, but only if the present one is invalid. Returns 1 if the unique id was cha...
Definition: UniqueIdInterface.h:159
const TransitionType & getTransition(String key)
Definition: MRMTransitionGroup.h:141