11#include <OpenMS/config.h>
111 double apex_pos = 0.0;
146 double width_at_5 = 0.0;
150 double width_at_10 = 0.0;
154 double width_at_50 = 0.0;
158 double start_position_at_5 = 0.0;
162 double start_position_at_10 = 0.0;
166 double start_position_at_50 = 0.0;
170 double end_position_at_5 = 0.0;
174 double end_position_at_10 = 0.0;
178 double end_position_at_50 = 0.0;
182 double total_width = 0.0;
196 double tailing_factor = 0.0;
206 double asymmetry_factor = 0.0;
211 double slope_of_baseline = 0.0;
216 double baseline_delta_2_height = 0.0;
220 Int points_across_baseline = 0;
224 Int points_across_half_height = 0;
235 static constexpr const char* INTEGRATION_TYPE_INTENSITYSUM =
"intensity_sum";
237 static constexpr const char* INTEGRATION_TYPE_TRAPEZOID =
"trapezoid";
239 static constexpr const char* INTEGRATION_TYPE_SIMPSON =
"simpson";
241 static constexpr const char* BASELINE_TYPE_BASETOBASE =
"base_to_base";
243 static constexpr const char* BASELINE_TYPE_VERTICALDIVISION =
"vertical_division";
245 static constexpr const char* BASELINE_TYPE_VERTICALDIVISION_MIN =
"vertical_division_min";
247 static constexpr const char* BASELINE_TYPE_VERTICALDIVISION_MAX =
"vertical_division_max";
269 const MSChromatogram& chromatogram,
const double left,
const double right
313 const MSSpectrum& spectrum,
const double left,
const double right
363 const MSChromatogram& chromatogram,
const double left,
const double right,
364 const double peak_apex_pos
393 const double peak_apex_pos
421 const MSSpectrum& spectrum,
const double left,
const double right,
422 const double peak_apex_pos
451 const double peak_apex_pos
474 const MSChromatogram& chromatogram,
const double left,
const double right,
475 const double peak_height,
const double peak_apex_pos
499 const double peak_height,
const double peak_apex_pos
522 const MSSpectrum& spectrum,
const double left,
const double right,
523 const double peak_height,
const double peak_apex_pos
547 const double peak_height,
const double peak_apex_pos
555 template <
typename PeakContainerT>
558 OPENMS_PRECONDITION(left <= right,
"Left peak boundary must be smaller than right boundary!")
559 PeakContainerT emg_pc;
560 const PeakContainerT& p = EMGPreProcess_(pc, emg_pc, left, right);
562 std::function<double(
const double,
const double)>
563 compute_peak_area_trapezoid = [&p](
const double left,
const double right)
565 double peak_area { 0.0 };
566 for (
typename PeakContainerT::ConstIterator it = p.PosBegin(left); it != p.PosEnd(right) - 1; ++it)
568 peak_area += ((it + 1)->getPos() - it->getPos()) * ((it->getIntensity() + (it + 1)->getIntensity()) / 2.0);
573 std::function<double(
const double,
const double)>
574 compute_peak_area_intensity_sum = [&p](
const double left,
const double right)
577 double peak_area { 0.0 };
578 for (
typename PeakContainerT::ConstIterator it = p.PosBegin(left); it != p.PosEnd(right); ++it)
580 peak_area += it->getIntensity();
587 UInt n_points = std::distance(p.PosBegin(left), p.PosEnd(right));
588 for (
auto it = p.PosBegin(left); it != p.PosEnd(right); ++it)
591 if (pa.
height < it->getIntensity())
593 pa.
height = it->getIntensity();
598 if (integration_type_ == INTEGRATION_TYPE_TRAPEZOID)
602 pa.
area = compute_peak_area_trapezoid(left, right);
605 else if (integration_type_ == INTEGRATION_TYPE_SIMPSON)
610 "number of points is 2, falling back to `trapezoid`." << std::endl;
611 pa.
area = compute_peak_area_trapezoid(left, right);
613 else if (n_points > 2)
617 pa.
area = simpson_(p.PosBegin(left), p.PosEnd(right));
621 double areas[4] = {-1.0, -1.0, -1.0, -1.0};
622 areas[0] = simpson_(p.PosBegin(left), p.PosEnd(right) - 1);
623 areas[1] = simpson_(p.PosBegin(left) + 1, p.PosEnd(right));
624 if (p.begin() <= p.PosBegin(left) - 1)
626 areas[2] = simpson_(p.PosBegin(left) - 1, p.PosEnd(right));
628 if (p.PosEnd(right) < p.end())
630 areas[3] = simpson_(p.PosBegin(left), p.PosEnd(right) + 1);
633 for (
const auto& area : areas)
645 else if (integration_type_ == INTEGRATION_TYPE_INTENSITYSUM)
647 pa.
area = compute_peak_area_intensity_sum(left, right);
651 throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Please set a valid value for the parameter \"integration_type\".");
659 template <
typename PeakContainerT>
661 const PeakContainerT& pc,
double left,
double right,
662 const double peak_apex_pos
665 PeakContainerT emg_pc;
666 const PeakContainerT& p = EMGPreProcess_(pc, emg_pc, left, right);
668 const double int_l = p.PosBegin(left)->getIntensity();
669 const double int_r = (p.PosEnd(right) - 1)->getIntensity();
670 const double delta_int = int_r - int_l;
671 const double delta_pos = (p.PosEnd(right) - 1)->getPos() - p.PosBegin(left)->getPos();
672 const double min_int_pos = int_r <= int_l ? (p.PosEnd(right) - 1)->getPos() : p.PosBegin(left)->getPos();
673 const double delta_int_apex = std::fabs(delta_int) * std::fabs(min_int_pos - peak_apex_pos) / delta_pos;
676 if (baseline_type_ == BASELINE_TYPE_BASETOBASE)
678 height = std::min(int_r, int_l) + delta_int_apex;
679 if (integration_type_ == INTEGRATION_TYPE_TRAPEZOID || integration_type_ == INTEGRATION_TYPE_SIMPSON)
683 area = delta_pos * (std::min(int_r, int_l) + 0.5 * std::fabs(delta_int));
685 else if (integration_type_ == INTEGRATION_TYPE_INTENSITYSUM)
692 double pos_sum = 0.0;
693 for (
auto it = p.PosBegin(left); it != p.PosEnd(right); ++it)
695 pos_sum += it->getPos();
697 UInt n_points = std::distance(p.PosBegin(left), p.PosEnd(right));
702 const double rectangle_area = n_points * int_l;
703 const double slope = delta_int / delta_pos;
704 const double triangle_area = (pos_sum - n_points * p.PosBegin(left)->getPos()) * slope;
705 area = triangle_area + rectangle_area;
708 else if (baseline_type_ == BASELINE_TYPE_VERTICALDIVISION || baseline_type_ == BASELINE_TYPE_VERTICALDIVISION_MIN)
710 height = std::min(int_r, int_l);
711 if (integration_type_ == INTEGRATION_TYPE_TRAPEZOID || integration_type_ == INTEGRATION_TYPE_SIMPSON)
713 area = delta_pos * std::min(int_r, int_l);
715 else if (integration_type_ == INTEGRATION_TYPE_INTENSITYSUM)
717 area = std::min(int_r, int_l) * std::distance(p.PosBegin(left), p.PosEnd(right));;
720 else if (baseline_type_ == BASELINE_TYPE_VERTICALDIVISION_MAX)
722 height = std::max(int_r, int_l);
723 if (integration_type_ == INTEGRATION_TYPE_TRAPEZOID || integration_type_ == INTEGRATION_TYPE_SIMPSON)
725 area = delta_pos * std::max(int_r, int_l);
727 else if (integration_type_ == INTEGRATION_TYPE_INTENSITYSUM)
729 area = std::max(int_r, int_l) * std::distance(p.PosBegin(left), p.PosEnd(right));
734 throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
"Please set a valid value for the parameter \"baseline_type\".");
756 template <
typename PeakContainerConstIteratorT>
757 double simpson_(PeakContainerConstIteratorT it_begin, PeakContainerConstIteratorT it_end)
const
759 double integral = 0.0;
760 for (
auto it = it_begin + 1; it < it_end - 1; it = it + 2)
762 const double h = it->getPos() - (it - 1)->getPos();
763 const double k = (it + 1)->getPos() - it->getPos();
764 const double y_h = (it - 1)->getIntensity();
765 const double y_0 = it->getIntensity();
766 const double y_k = (it + 1)->getIntensity();
767 integral += (1.0 / 6.0) * (h + k) * ((2.0 - k / h) * y_h + (pow(h + k, 2) / (h * k)) * y_0 + (2.0 - h / k) * y_k);
773 template <
typename PeakContainerT>
775 const PeakContainerT& pc,
double left,
double right,
776 const double peak_height,
const double peak_apex_pos
781 if (pc.empty())
return psm;
784 if (!(left <= peak_apex_pos && peak_apex_pos <= right))
throw Exception::InvalidRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION);
786 PeakContainerT emg_pc;
787 const PeakContainerT& p = EMGPreProcess_(pc, emg_pc, left, right);
789 typename PeakContainerT::ConstIterator it_PosBegin_l = p.PosBegin(left);
790 typename PeakContainerT::ConstIterator it_PosEnd_apex = p.PosBegin(peak_apex_pos);
791 typename PeakContainerT::ConstIterator it_PosEnd_r = p.PosEnd(right);
792 for (
auto it = it_PosBegin_l; it != it_PosEnd_r; ++it)
796 if (it->getIntensity() >= 0.5 * peak_height)
802 psm.
start_position_at_5 = findPosAtPeakHeightPercent_(it_PosBegin_l, it_PosEnd_apex, p.end(), peak_height, 0.05,
true);
803 psm.
start_position_at_10 = findPosAtPeakHeightPercent_(it_PosBegin_l, it_PosEnd_apex, p.end(), peak_height, 0.1,
true);
804 psm.
start_position_at_50 = findPosAtPeakHeightPercent_(it_PosBegin_l, it_PosEnd_apex, p.end(), peak_height, 0.5,
true);
805 psm.
end_position_at_5 = findPosAtPeakHeightPercent_(it_PosEnd_apex, it_PosEnd_r, p.end(), peak_height, 0.05,
false);
806 psm.
end_position_at_10 = findPosAtPeakHeightPercent_(it_PosEnd_apex, it_PosEnd_r, p.end(), peak_height, 0.1,
false);
807 psm.
end_position_at_50 = findPosAtPeakHeightPercent_(it_PosEnd_apex, it_PosEnd_r, p.end(), peak_height, 0.5,
false);
812 psm.
total_width = (p.PosEnd(right) - 1)->getPos() - p.PosBegin(left)->getPos();
813 psm.
slope_of_baseline = (p.PosEnd(right) - 1)->getIntensity() - p.PosBegin(left)->getIntensity();
814 if (peak_height != 0.0)
855 template <
typename PeakContainerConstIteratorT>
857 PeakContainerConstIteratorT it_left,
858 PeakContainerConstIteratorT it_right,
859 PeakContainerConstIteratorT it_end,
860 const double peak_height,
861 const double percent,
862 const bool is_left_half)
const
868 if (it_left == it_right)
return it_left->getPos();
870 const double percent_intensity = peak_height * percent;
871 PeakContainerConstIteratorT closest;
876 PeakContainerConstIteratorT it = it_left;
877 it < it_right && it->getIntensity() <= percent_intensity;
883 closest = it_right - 1;
885 PeakContainerConstIteratorT it = it_right - 1;
886 it >= it_left && it->getIntensity() <= percent_intensity;
890 return closest->getPos();
903 String integration_type_ = INTEGRATION_TYPE_INTENSITYSUM;
908 String baseline_type_ = BASELINE_TYPE_BASETOBASE;
929 template <
typename PeakContainerT>
931 const PeakContainerT& pc,
932 PeakContainerT& emg_pc,
940 left = emg_pc.front().getPos();
941 right = emg_pc.back().getPos();
#define OPENMS_LOG_WARN
Macro if a warning, a piece of information which should be read by the user, should be logged.
Definition LogStream.h:447
std::vector< PointType > PointArrayType
Definition ConvexHull2D.h:52
Representation of a coordinate in D-dimensional space.
Definition DPosition.h:32
A base class for all classes handling default parameters.
Definition DefaultParamHandler.h:66
Fit peaks to an Exponentially Modified Gaussian (EMG) model using gradient descent.
Definition EmgGradientDescent.h:66
void fitEMGPeakModel(const PeakContainerT &input_peak, PeakContainerT &output_peak, const double left_pos=0.0, const double right_pos=0.0) const
Fit the given peak (either MSChromatogram or MSSpectrum) to the EMG peak model.
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition Exception.h:317
Invalid range exception.
Definition Exception.h:257
The representation of a chromatogram.
Definition MSChromatogram.h:30
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition MSChromatogram.h:66
The representation of a 1D spectrum.
Definition MSSpectrum.h:44
ContainerType::const_iterator ConstIterator
Non-mutable iterator.
Definition MSSpectrum.h:120
Management and storage of parameters / INI files.
Definition Param.h:46
Compute the area, background and shape metrics of a peak.
Definition PeakIntegrator.h:87
Int points_across_half_height
Definition PeakIntegrator.h:224
bool fit_EMG_
Enable/disable EMG peak model fitting.
Definition PeakIntegrator.h:912
PeakArea integratePeak(const MSChromatogram &chromatogram, const double left, const double right) const
Compute the area of a peak contained in a MSChromatogram.
double apex_pos
Definition PeakIntegrator.h:111
ConvexHull2D::PointArrayType hull_points
Definition PeakIntegrator.h:115
PeakBackground estimateBackground_(const PeakContainerT &pc, double left, double right, const double peak_apex_pos) const
Definition PeakIntegrator.h:660
PeakArea integratePeak(const MSChromatogram &chromatogram, MSChromatogram::ConstIterator &left, MSChromatogram::ConstIterator &right) const
Compute the area of a peak contained in a MSChromatogram.
double width_at_5
Definition PeakIntegrator.h:146
Int points_across_baseline
Definition PeakIntegrator.h:220
PeakShapeMetrics calculatePeakShapeMetrics(const MSSpectrum &spectrum, MSSpectrum::ConstIterator &left, MSSpectrum::ConstIterator &right, const double peak_height, const double peak_apex_pos) const
Calculate peak's shape metrics.
double width_at_50
Definition PeakIntegrator.h:154
double end_position_at_50
Definition PeakIntegrator.h:178
const PeakContainerT & EMGPreProcess_(const PeakContainerT &pc, PeakContainerT &emg_pc, double &left, double &right) const
Fit the peak to the EMG model.
Definition PeakIntegrator.h:930
double findPosAtPeakHeightPercent_(PeakContainerConstIteratorT it_left, PeakContainerConstIteratorT it_right, PeakContainerConstIteratorT it_end, const double peak_height, const double percent, const bool is_left_half) const
Find the position (RT/MZ) at a given percentage of peak's height.
Definition PeakIntegrator.h:856
double baseline_delta_2_height
Definition PeakIntegrator.h:216
EmgGradientDescent emg_
Definition PeakIntegrator.h:913
double height
Definition PeakIntegrator.h:107
double end_position_at_10
Definition PeakIntegrator.h:174
double simpson_(PeakContainerConstIteratorT it_begin, PeakContainerConstIteratorT it_end) const
Simpson's rule algorithm.
Definition PeakIntegrator.h:757
~PeakIntegrator() override
Destructor.
PeakShapeMetrics calculatePeakShapeMetrics(const MSChromatogram &chromatogram, MSChromatogram::ConstIterator &left, MSChromatogram::ConstIterator &right, const double peak_height, const double peak_apex_pos) const
Calculate peak's shape metrics.
PeakShapeMetrics calculatePeakShapeMetrics_(const PeakContainerT &pc, double left, double right, const double peak_height, const double peak_apex_pos) const
Definition PeakIntegrator.h:774
PeakBackground estimateBackground(const MSSpectrum &spectrum, MSSpectrum::ConstIterator &left, MSSpectrum::ConstIterator &right, const double peak_apex_pos) const
Estimate the background of a peak contained in a MSSpectrum.
double width_at_10
Definition PeakIntegrator.h:150
PeakArea integratePeak(const MSSpectrum &spectrum, MSSpectrum::ConstIterator &left, MSSpectrum::ConstIterator &right) const
Compute the area of a peak contained in a MSSpectrum.
double total_width
Definition PeakIntegrator.h:182
PeakShapeMetrics calculatePeakShapeMetrics(const MSChromatogram &chromatogram, const double left, const double right, const double peak_height, const double peak_apex_pos) const
Calculate peak's shape metrics.
double end_position_at_5
Definition PeakIntegrator.h:170
void updateMembers_() override
This method is used to update extra member variables at the end of the setParameters() method.
double start_position_at_50
Definition PeakIntegrator.h:166
void getDefaultParameters(Param ¶ms)
double tailing_factor
Definition PeakIntegrator.h:196
PeakShapeMetrics calculatePeakShapeMetrics(const MSSpectrum &spectrum, const double left, const double right, const double peak_height, const double peak_apex_pos) const
Calculate peak's shape metrics.
double slope_of_baseline
Definition PeakIntegrator.h:211
PeakBackground estimateBackground(const MSChromatogram &chromatogram, const double left, const double right, const double peak_apex_pos) const
Estimate the background of a peak contained in a MSChromatogram.
double start_position_at_10
Definition PeakIntegrator.h:162
PeakArea integratePeak(const MSSpectrum &spectrum, const double left, const double right) const
Compute the area of a peak contained in a MSSpectrum.
double asymmetry_factor
Definition PeakIntegrator.h:206
double start_position_at_5
Definition PeakIntegrator.h:158
double area
Definition PeakIntegrator.h:103
PeakBackground estimateBackground(const MSSpectrum &spectrum, const double left, const double right, const double peak_apex_pos) const
Estimate the background of a peak contained in a MSSpectrum.
PeakBackground estimateBackground(const MSChromatogram &chromatogram, MSChromatogram::ConstIterator &left, MSChromatogram::ConstIterator &right, const double peak_apex_pos) const
Estimate the background of a peak contained in a MSChromatogram.
PeakArea integratePeak_(const PeakContainerT &pc, double left, double right) const
Definition PeakIntegrator.h:556
PeakIntegrator()
Constructor.
Definition PeakIntegrator.h:99
Definition PeakIntegrator.h:124
Definition PeakIntegrator.h:142
A more convenient string class.
Definition String.h:34
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
#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