OpenMS
Loading...
Searching...
No Matches
ThermoRawFileMetadata.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 $
7// --------------------------------------------------------------------------
8#pragma once
9
10#include <cmath>
11#include <locale>
12#include <map>
13#include <nlohmann/json.hpp>
14#include <regex>
15#include <sstream>
16#include <string>
17#include <vector>
18
19namespace OpenMS::Internal
20{
26{
27public:
29 static std::string trailer(const nlohmann::json& scan, const std::string& label)
30 {
31 for (const auto& entry : scan.at("trailer"))
32 {
33 if (entry.at("label") == label && entry.at("value").is_string()) { return entry.at("value").get<std::string>(); }
34 }
35 return "";
36 }
37
43 static nlohmann::json number(const std::string& value)
44 {
45 std::istringstream stream(value);
46 stream.imbue(std::locale::classic());
47 double result;
48 if (! (stream >> result) || ! std::isfinite(result)) { return nullptr; }
49 stream >> std::ws;
50 return stream.eof() ? nlohmann::json(result) : nlohmann::json(nullptr);
51 }
52
61 static double selectedIon(double target, const nlohmann::json& mono, double width)
62 {
63 if (! mono.is_number() || mono.get<double>() <= 1e-8) { return target; }
64 double mz = mono.get<double>();
65 if (std::abs(mz - target) <= 0.0001) { return target; }
66 double half = width / 2;
67 if (half <= 2.0) { return mz >= target - 3.0 && mz <= target + 2.5 ? mz : target; }
68 return mz >= target - half && mz <= target + half ? mz : target;
69 }
70
81 nlohmann::json precursors(const nlohmann::json& scan)
82 {
83 const int scan_number = scan.at("scan_number").get<int>();
84 const int level = scan.at("ms_level").get<int>();
85 const auto& reactions = scan.at("reactions");
86 State state;
87 state.level = level;
88 state.native_id = scan.at("native_id").get<std::string>();
89 if (level == 1)
90 {
91 filters_[""] = scan_number;
92 scans_[scan_number] = state;
93 return nlohmann::json::array();
94 }
95 std::smatch match;
96 const std::string filter = scan.at("filter").get<std::string>();
97 std::string key;
98 static const std::regex filter_pattern(R"(ms\d+ (.+?) \[)");
99 if (std::regex_search(filter, match, filter_pattern)) { key = match[1]; }
100 int fallback = parentFromFilter_(key);
101 auto master = number(trailer(scan, "Master Scan Number:"));
102 int parent
103 = master.is_number() && master.get<double>() > 0 && master.get<double>() < scan_number ? static_cast<int>(master.get<double>()) : fallback;
104 auto parent_it = scans_.find(parent);
105 // A master scan of the same MS order is a sibling (Tribrid decision trees, or several
106 // activations of one isolation such as HCD / ETD / EThcD scans of the same precursor).
107 // Its reactions do not apply here; descend from the scan the sibling was triggered by.
108 if (parent_it != scans_.end() && parent_it->second.level == level)
109 {
110 parent = parent_it->second.parent;
111 parent_it = scans_.find(parent);
112 }
113 std::size_t index = parent_it == scans_.end() ? lastReaction_(reactions) : parent_it->second.reactions;
114 if (index >= reactions.size() && parent_it != scans_.end())
115 {
116 parent = fallback;
117 parent_it = scans_.find(parent);
118 index = parent_it == scans_.end() ? lastReaction_(reactions) : parent_it->second.reactions;
119 }
120 state.parent = parent_it == scans_.end() ? 0 : parent;
121 nlohmann::json result = nlohmann::json::array();
122 if (index < reactions.size())
123 {
124 const auto& reaction = reactions[index];
125 auto width = number(trailer(scan, "MS" + std::to_string(level) + " Isolation Width:"));
126 if (width.is_null()) { width = reaction.at("isolation_width"); }
127 if (width.is_number() && width.get<double>() < 0) { width = nullptr; }
128 const double target = reaction.at("precursor_mass").get<double>();
129 const double w = width.is_number() ? width.get<double>() : 0.0;
130 const double offset = reaction.at("isolation_offset").is_number() ? reaction.at("isolation_offset").get<double>() : 0.0;
131 auto charge = number(trailer(scan, "Charge State:"));
132 if (charge.is_number() && charge.get<double>() <= 0) { charge = nullptr; }
133 nlohmann::json precursor = {{"target_mz", target},
134 {"selected_mz", selectedIon(target, number(trailer(scan, "Monoisotopic M/Z:")), w)},
135 {"charge", charge},
136 {"width", width},
137 {"lower_offset", w / 2 - offset},
138 {"upper_offset", w / 2 + offset},
139 {"parent_scan", parent_it == scans_.end() ? 0 : parent},
140 {"estimate_intensity", true},
141 {"spectrum_ref", parent_it == scans_.end() ? "" : parent_it->second.native_id},
142 {"activation", reaction},
143 {"supplemental", nullptr}};
144 ++index;
145 // The parent's consumed reactions already account for earlier MS levels.
146 // Like TRFP, retain the remaining reaction as supplemental activation even
147 // when the vendor's activation flag or type is unexpected.
148 if (index < reactions.size()) { precursor["supplemental"] = reactions[index++]; }
149 state.own.push_back(precursor);
150 auto masses = spsMasses_(scan);
151 // First SPS selection is represented by the primary reaction.
152 for (std::size_t i = 1; i < masses.size(); ++i)
153 {
154 auto sps = precursor;
155 sps["target_mz"] = masses[i];
156 sps["selected_mz"] = masses[i];
157 sps["charge"] = nullptr;
158 sps["estimate_intensity"] = false;
159 state.own.push_back(std::move(sps));
160 }
161 state.reactions = index;
162 result = state.own;
163 // Ancestors are resolved through the parent chain (MS3 -> MS2 -> MS1) instead of being
164 // copied into every scan, so retained metadata stays one descriptor per MSn scan.
165 for (int ancestor = state.parent; ancestor != 0;)
166 {
167 const auto it = scans_.find(ancestor);
168 if (it == scans_.end()) { break; }
169 for (const auto& descriptor : it->second.own)
170 {
171 result.push_back(descriptor);
172 }
173 ancestor = it->second.parent;
174 }
175 }
176 if (! key.empty()) { filters_[key] = scan_number; }
177 scans_[scan_number] = std::move(state);
178 return result;
179 }
180
181private:
182 struct State
183 {
184 int level = 1;
185 int parent = 0;
186 std::size_t reactions = 0;
187 std::string native_id;
188 nlohmann::json own = nlohmann::json::array();
189 };
190 std::map<int, State> scans_;
191 std::map<std::string, int> filters_;
192
193 static bool supplemental_(const nlohmann::json& first, const nlohmann::json& second)
194 {
195 const std::string a = first.at("activation").get<std::string>(), b = second.at("activation").get<std::string>();
196 return first.at("precursor_mass").is_number() && second.at("precursor_mass").is_number()
197 && std::abs(first.at("precursor_mass").get<double>() - second.at("precursor_mass").get<double>()) < 0.0001
198 && (a == "ElectronTransferDissociation" || a == "ElectronCaptureDissociation")
199 && (b == "HigherEnergyCollisionalDissociation" || b == "CollisionInducedDissociation");
200 }
201 static std::size_t lastReaction_(const nlohmann::json& reactions)
202 {
203 if (reactions.empty()) { return 0; }
204 std::size_t index = reactions.size() - 1;
205 if (index > 0 && supplemental_(reactions[index - 1], reactions[index])) { --index; }
206 return index;
207 }
208 int parentFromFilter_(const std::string& key) const
209 {
210 std::istringstream stream(key);
211 std::vector<std::string> parts;
212 for (std::string part; stream >> part;)
213 {
214 parts.push_back(part);
215 }
216 if (! parts.empty())
217 {
218 const auto mass = parts.back().substr(0, parts.back().find('@'));
219 while (! parts.empty() && parts.back().substr(0, parts.back().find('@')) == mass)
220 {
221 parts.pop_back();
222 }
223 }
224 std::string parent_key;
225 for (const auto& part : parts)
226 {
227 if (! parent_key.empty()) { parent_key += ' '; }
228 parent_key += part;
229 }
230 auto it = filters_.find(parent_key);
231 return it == filters_.end() ? 0 : it->second;
232 }
233 static std::vector<double> spsMasses_(const nlohmann::json& scan)
234 {
235 std::vector<double> result;
236 const bool modern = ! trailer(scan, "SPS Masses:").empty();
237 static const std::regex legacy_pattern(R"(SPS Mass\s+\d+:)");
238 static const std::regex modern_pattern(R"(SPS Masses(?:\s+Continued)?:)");
239 for (const auto& entry : scan.at("trailer"))
240 {
241 const std::string label = entry.at("label").get<std::string>();
242 if ((! modern && std::regex_match(label, legacy_pattern)) || (modern && std::regex_match(label, modern_pattern)))
243 {
244 std::istringstream values(entry.at("value").get<std::string>());
245 for (std::string value; std::getline(values, value, ',');)
246 {
247 auto mass = number(value);
248 if (mass.is_number() && mass.get<double>() > 0) { result.push_back(mass.get<double>()); }
249 }
250 }
251 }
252 return result;
253 }
254};
255} // namespace OpenMS::Internal
Resolve the precursor hierarchy from bridge metadata without fetching peaks. Keeps isolation targets ...
Definition ThermoRawFileMetadata.h:26
static std::string trailer(const nlohmann::json &scan, const std::string &label)
Value of the trailer-extra entry with exactly this label, or "" if absent / not a string.
Definition ThermoRawFileMetadata.h:29
static nlohmann::json number(const std::string &value)
Parse a trailer value as a number.
Definition ThermoRawFileMetadata.h:43
nlohmann::json own
this scan's own precursor descriptors
Definition ThermoRawFileMetadata.h:188
std::map< int, State > scans_
Definition ThermoRawFileMetadata.h:190
int parentFromFilter_(const std::string &key) const
Definition ThermoRawFileMetadata.h:208
int parent
scan this one descends from (0 if unknown)
Definition ThermoRawFileMetadata.h:185
std::size_t reactions
reactions consumed up to and including this scan
Definition ThermoRawFileMetadata.h:186
static bool supplemental_(const nlohmann::json &first, const nlohmann::json &second)
Definition ThermoRawFileMetadata.h:193
nlohmann::json precursors(const nlohmann::json &scan)
Precursor descriptors of one scan; scans must be passed in acquisition order.
Definition ThermoRawFileMetadata.h:81
std::map< std::string, int > filters_
Definition ThermoRawFileMetadata.h:191
std::string native_id
Definition ThermoRawFileMetadata.h:187
static std::vector< double > spsMasses_(const nlohmann::json &scan)
Definition ThermoRawFileMetadata.h:233
static double selectedIon(double target, const nlohmann::json &mono, double width)
Selected ion m/z of a precursor (ThermoRawFileParser rule).
Definition ThermoRawFileMetadata.h:61
int level
Definition ThermoRawFileMetadata.h:184
static std::size_t lastReaction_(const nlohmann::json &reactions)
Definition ThermoRawFileMetadata.h:201
Definition ThermoRawFileMetadata.h:183
OpenMS looks in the< code > OPENMS_THERMO_MANAGED_DIR</code > environment variable first
Definition common-cmake-parameters.doxygen:171
Namespace used to hide implementation details from users.
Definition BayesianProteinInferenceAlgorithm.h:26