OpenMS
Loading...
Searching...
No Matches
IonImageExtraction.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: Timo Sachsenberg $
6// $Authors: Timo Sachsenberg, Patrick Boschmann, Aditya Sarna $
7// --------------------------------------------------------------------------
8
9#pragma once
10
17
18#include <cmath>
19#include <vector>
20
21namespace OpenMS
22{
23namespace Internal
24{
25
71template <typename GetSpectrum>
73 double mz, double tolerance_ppm,
74 const std::vector<Size>& pixel_indices,
75 Size n_spectra,
76 GetSpectrum&& get_spectrum)
77{
78 if (!std::isfinite(mz) || !std::isfinite(tolerance_ppm) || mz < 0.0 || tolerance_ppm < 0.0)
79 {
80 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
81 "mz and tolerance_ppm must be finite and non-negative",
82 "mz=" + StringUtils::toStr(mz) + ", tolerance_ppm=" + StringUtils::toStr(tolerance_ppm));
83 }
84
85 const double dm = mz * tolerance_ppm * 1e-6;
86 const double mz_lo = mz - dm;
87 const double mz_hi = mz + dm;
88
89 IonImage image(geom.getWidth(), geom.getHeight());
90 image.setMzRange(RangeMZ(mz_lo, mz_hi));
91
92 const auto& pixels = geom.getPixels();
93 for (Size i : pixel_indices)
94 {
95 const auto& p = pixels[i];
96 if (p.spectrum_index >= n_spectra)
97 {
98 throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
99 "Pixel references missing spectrum",
100 StringUtils::toStr(p.spectrum_index));
101 }
102 // const auto& spec: for a lambda returning const MSSpectrum& (in-memory) this
103 // binds the reference with no copy; for a lambda returning MSSpectrum by value
104 // (on-disc) this lifetime-extends the temporary for the duration of the loop body.
105 const auto& spec = get_spectrum(p.spectrum_index);
106 double sum = 0.0;
107 for (auto it = spec.MZBegin(mz_lo); it != spec.MZEnd(mz_hi); ++it)
108 {
109 sum += static_cast<double>(it->getIntensity());
110 }
111 image.setIntensity(p.x, p.y, sum);
112 }
113 return image;
114}
115
116} // namespace Internal
117} // namespace OpenMS
Invalid value exception.
Definition Exception.h:306
Dense W x H grid of ion intensities with a per-pixel mask.
Definition IonImage.h:31
void setMzRange(const RangeMZ &range)
Records the m/z window the image was extracted from.
void setIntensity(UInt x, UInt y, double intensity)
Stores intensity at (x, y) and marks the cell valid.
Pixel grid metadata and (x, y) -> spectrum_index lookup for MSI data.
Definition MSImagingGeometry.h:33
const std::vector< Pixel > & getPixels() const
Pixels in insertion order.
UInt getWidth() const
Image width.
UInt getHeight() const
Image height.
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
static double sum(IteratorType begin, IteratorType end)
Calculates the sum of a range of values.
Definition StatisticFunctions.h:103
IonImage extractIonImage(const MSImagingGeometry &geom, double mz, double tolerance_ppm, const std::vector< Size > &pixel_indices, Size n_spectra, GetSpectrum &&get_spectrum)
Build an IonImage by summing peak intensities in an m/z window over a pixel subset.
Definition IonImageExtraction.h:72
std::string toStr(int i)
Definition StringUtils.h:101
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Definition RangeManager.h:358