13#include <nlohmann/json.hpp>
29 static std::string
trailer(
const nlohmann::json& scan,
const std::string& label)
31 for (
const auto& entry : scan.at(
"trailer"))
33 if (entry.at(
"label") == label && entry.at(
"value").is_string()) {
return entry.at(
"value").get<std::string>(); }
43 static nlohmann::json
number(
const std::string& value)
45 std::istringstream stream(value);
46 stream.imbue(std::locale::classic());
48 if (! (stream >> result) || ! std::isfinite(result)) {
return nullptr; }
50 return stream.eof() ? nlohmann::json(result) : nlohmann::json(
nullptr);
61 static double selectedIon(
double target,
const nlohmann::json& mono,
double width)
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;
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");
88 state.
native_id = scan.at(
"native_id").get<std::string>();
92 scans_[scan_number] = state;
93 return nlohmann::json::array();
96 const std::string filter = scan.at(
"filter").get<std::string>();
98 static const std::regex filter_pattern(R
"(ms\d+ (.+?) \[)");
99 if (std::regex_search(filter, match, filter_pattern)) { key = match[1]; }
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);
108 if (parent_it !=
scans_.end() && parent_it->second.level == level)
110 parent = parent_it->second.parent;
111 parent_it =
scans_.find(parent);
113 std::size_t index = parent_it ==
scans_.end() ?
lastReaction_(reactions) : parent_it->second.reactions;
114 if (index >= reactions.size() && parent_it !=
scans_.end())
117 parent_it =
scans_.find(parent);
118 index = parent_it ==
scans_.end() ?
lastReaction_(reactions) : parent_it->second.reactions;
121 nlohmann::json result = nlohmann::json::array();
122 if (index < reactions.size())
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;
132 if (charge.is_number() && charge.get<
double>() <= 0) { charge =
nullptr; }
133 nlohmann::json precursor = {{
"target_mz", target},
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}};
148 if (index < reactions.size()) { precursor[
"supplemental"] = reactions[index++]; }
149 state.
own.push_back(precursor);
152 for (std::size_t i = 1; i < masses.size(); ++i)
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));
165 for (
int ancestor = state.
parent; ancestor != 0;)
167 const auto it =
scans_.find(ancestor);
168 if (it ==
scans_.end()) {
break; }
169 for (
const auto& descriptor : it->second.own)
171 result.push_back(descriptor);
173 ancestor = it->second.parent;
176 if (! key.empty()) {
filters_[key] = scan_number; }
177 scans_[scan_number] = std::move(state);
188 nlohmann::json
own = nlohmann::json::array();
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");
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; }
210 std::istringstream stream(key);
211 std::vector<std::string> parts;
212 for (std::string part; stream >> part;)
214 parts.push_back(part);
218 const auto mass = parts.back().substr(0, parts.back().find(
'@'));
219 while (! parts.empty() && parts.back().substr(0, parts.back().find(
'@')) == mass)
224 std::string parent_key;
225 for (
const auto& part : parts)
227 if (! parent_key.empty()) { parent_key +=
' '; }
230 auto it =
filters_.find(parent_key);
231 return it ==
filters_.end() ? 0 : it->second;
233 static std::vector<double>
spsMasses_(
const nlohmann::json& scan)
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"))
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)))
244 std::istringstream values(entry.at(
"value").get<std::string>());
245 for (std::string value; std::getline(values, value,
',');)
247 auto mass =
number(value);
248 if (mass.is_number() && mass.get<
double>() > 0) { result.push_back(mass.get<
double>()); }
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