12#include <nlohmann/json.hpp>
40 std::string s = j.get<std::string>();
46 else throw std::invalid_argument(
"Unknown ProForma::CvDatabase: " + s);
58 j = nlohmann::json{{
"database", cv.
database}, {
"accession",
static_cast<std::string
>(cv.
accession)}};
64 j.at(
"database").get_to(cv.
database);
65 cv.
accession = j.at(
"accession").get<std::string>();
77 j = nlohmann::json{{
"name",
static_cast<std::string
>(nm.
name)}};
80 j[
"cv_hint"] = nm.
cv_hint.value();
87 nm.
name = j.at(
"name").get<std::string>();
88 if (j.contains(
"cv_hint") && !j.at(
"cv_hint").is_null())
122 std::string s = j.get<std::string>();
130 else throw std::invalid_argument(
"Unknown ProForma::MassDelta::Source: " + s);
145 {
"original_text",
static_cast<std::string
>(md.
original_text)}
152 j.at(
"source").get_to(md.
source);
153 j.at(
"mass").get_to(md.
mass);
166 j = nlohmann::json{{
"formula_string",
static_cast<std::string
>(ft.
formula_string)}};
167 if (ft.
charge.has_value())
169 j[
"charge"] = ft.
charge.value();
177 if (j.contains(
"charge") && !j.at(
"charge").is_null())
179 ft.
charge = j.at(
"charge").get<
int>();
196 std::visit([&j](
auto&& arg) {
197 using T = std::decay_t<
decltype(arg)>;
198 if constexpr (std::is_same_v<T, OpenMS::String>)
200 j = nlohmann::json{{
"type",
"name"}, {
"value",
static_cast<std::string
>(arg)}};
202 else if constexpr (std::is_same_v<T, ProForma::FormulaTag>)
204 j = nlohmann::json{{
"type",
"formula"}, {
"value", arg}};
212 std::string type = j.at(
"type").get<std::string>();
215 mono =
String(j.at(
"value").get<std::string>());
217 else if (type ==
"formula")
223 throw std::invalid_argument(
"Unknown ProForma::GlycanComposition::Monosaccharide type: " + type);
236 j = nlohmann::json::array();
237 for (
const auto& [mono, count] : gc.
components)
239 nlohmann::json component;
240 component[
"monosaccharide"] = mono;
241 component[
"count"] = count;
242 j.push_back(component);
250 for (
const auto& item : j)
253 from_json(item.at(
"monosaccharide"), mono);
254 int count = item.at(
"count").get<
int>();
268 j = nlohmann::json{{
"text",
static_cast<std::string
>(it.
text)}};
274 it.
text = j.at(
"text").get<std::string>();
296 std::string residue_str = j.at(
"residues").get<std::string>();
297 pc.
residues.assign(residue_str.begin(), residue_str.end());
298 if (j.contains(
"n_term")) pc.
n_term = j.at(
"n_term").get<
bool>();
299 if (j.contains(
"c_term")) pc.
c_term = j.at(
"c_term").get<
bool>();
311 std::visit([&j](
auto&& arg) {
312 using T = std::decay_t<
decltype(arg)>;
313 if constexpr (std::is_same_v<T, ProForma::CvAccession>)
315 j = nlohmann::json{{
"type",
"cv_accession"}, {
"value", arg}};
317 else if constexpr (std::is_same_v<T, ProForma::NamedMod>)
319 j = nlohmann::json{{
"type",
"named_mod"}, {
"value", arg}};
321 else if constexpr (std::is_same_v<T, ProForma::MassDelta>)
323 j = nlohmann::json{{
"type",
"mass_delta"}, {
"value", arg}};
325 else if constexpr (std::is_same_v<T, ProForma::FormulaTag>)
327 j = nlohmann::json{{
"type",
"formula"}, {
"value", arg}};
329 else if constexpr (std::is_same_v<T, ProForma::GlycanComposition>)
331 j = nlohmann::json{{
"type",
"glycan"}, {
"value", arg}};
333 else if constexpr (std::is_same_v<T, ProForma::InfoTag>)
335 j = nlohmann::json{{
"type",
"info"}, {
"value", arg}};
337 else if constexpr (std::is_same_v<T, ProForma::PositionConstraint>)
339 j = nlohmann::json{{
"type",
"position"}, {
"value", arg}};
347 std::string type = j.at(
"type").get<std::string>();
348 if (type ==
"cv_accession")
352 else if (type ==
"named_mod")
356 else if (type ==
"mass_delta")
360 else if (type ==
"formula")
364 else if (type ==
"glycan")
368 else if (type ==
"info")
372 else if (type ==
"position")
378 throw std::invalid_argument(
"Unknown ProForma::ModificationTag type: " + type);
402 std::string s = j.get<std::string>();
406 else throw std::invalid_argument(
"Unknown ProForma::Label::Type: " + s);
420 {
"identifier",
static_cast<std::string
>(lbl.
identifier)}
422 if (lbl.
score.has_value())
424 j[
"score"] = lbl.
score.value();
431 j.at(
"type").get_to(lbl.
type);
432 lbl.
identifier = j.at(
"identifier").get<std::string>();
433 if (j.contains(
"score") && !j.at(
"score").is_null())
435 lbl.
score = j.at(
"score").get<
double>();
439 lbl.
score = std::nullopt;
452 j = nlohmann::json::array();
457 if (label.has_value())
459 alt[
"label"] = label.value();
470 for (
const auto& item : j)
473 std::optional<ProForma::Label> label;
474 if (item.contains(
"label") && !item.at(
"label").is_null())
492 {
"amino_acid", std::string(1, se.
amino_acid)},
500 std::string aa = j.at(
"amino_acid").get<std::string>();
503 throw std::invalid_argument(
"amino_acid must be exactly 1 character, got length " + std::to_string(aa.size()));
518 j = nlohmann::json{{
"elements", ar.
elements}};
524 j.at(
"elements").get_to(ar.
elements);
545 j.at(
"elements").get_to(mr.
elements);
558 std::visit([&j](
auto&& arg) {
559 using T = std::decay_t<
decltype(arg)>;
560 if constexpr (std::is_same_v<T, ProForma::SequenceElement>)
562 j = nlohmann::json{{
"type",
"element"}, {
"value", arg}};
564 else if constexpr (std::is_same_v<T, ProForma::AmbiguousRegion>)
566 j = nlohmann::json{{
"type",
"ambiguous_region"}, {
"value", arg}};
568 else if constexpr (std::is_same_v<T, ProForma::ModifiedRange>)
570 j = nlohmann::json{{
"type",
"modified_range"}, {
"value", arg}};
578 std::string type = j.at(
"type").get<std::string>();
579 if (type ==
"element")
583 else if (type ==
"ambiguous_region")
587 else if (type ==
"modified_range")
593 throw std::invalid_argument(
"Unknown ProForma::SequenceSection type: " + type);
617 if (j.contains(
"occurrence") && !j.at(
"occurrence").is_null())
619 um.
occurrence = j.at(
"occurrence").get<
int>();
654 std::vector<std::string> locs;
657 locs.push_back(
static_cast<std::string
>(loc));
670 for (
const auto& loc : j.at(
"locations"))
685 j = nlohmann::json{{
"isotope",
static_cast<std::string
>(ir.
isotope)}};
691 ir.
isotope = j.at(
"isotope").get<std::string>();
703 std::visit([&j](
auto&& arg) {
704 using T = std::decay_t<
decltype(arg)>;
705 if constexpr (std::is_same_v<T, ProForma::IsotopeReplacement>)
707 j = nlohmann::json{{
"type",
"isotope_replacement"}, {
"value", arg}};
709 else if constexpr (std::is_same_v<T, ProForma::GlobalModification>)
711 j = nlohmann::json{{
"type",
"global_modification"}, {
"value", arg}};
719 std::string type = j.at(
"type").get<std::string>();
720 if (type ==
"isotope_replacement")
724 else if (type ==
"global_modification")
730 throw std::invalid_argument(
"Unknown ProForma::GlobalModEntry type: " + type);
744 {
"formula",
static_cast<std::string
>(ai.
formula)},
756 ai.
formula = j.at(
"formula").get<std::string>();
757 j.at(
"charge").get_to(ai.
charge);
758 if (j.contains(
"occurrence") && !j.at(
"occurrence").is_null())
760 ai.
occurrence = j.at(
"occurrence").get<
int>();
777 std::visit([&j](
auto&& arg) {
778 using T = std::decay_t<
decltype(arg)>;
779 if constexpr (std::is_same_v<T, int>)
781 j = nlohmann::json{{
"type",
"simple"}, {
"value", arg}};
783 else if constexpr (std::is_same_v<T, std::vector<ProForma::AdductIon>>)
785 j = nlohmann::json{{
"type",
"adducts"}, {
"value", arg}};
793 std::string type = j.at(
"type").get<std::string>();
794 if (type ==
"simple")
796 cs = j.at(
"value").get<
int>();
798 else if (type ==
"adducts")
800 cs = j.at(
"value").get<std::vector<ProForma::AdductIon>>();
804 throw std::invalid_argument(
"Unknown ProForma::ChargeState type: " + type);
825 if (pf.
name.has_value())
827 j[
"name"] =
static_cast<std::string
>(pf.
name.value());
829 if (pf.
charge.has_value())
831 j[
"charge"] = pf.
charge.value();
838 if (j.contains(
"global_mods"))
849 j.at(
"sequence").get_to(pf.
sequence);
851 if (j.contains(
"name") && !j.at(
"name").is_null())
853 pf.
name =
String(j.at(
"name").get<std::string>());
857 pf.
name = std::nullopt;
859 if (j.contains(
"charge") && !j.at(
"charge").is_null())
879 if (pfi.
name.has_value())
881 j[
"name"] =
static_cast<std::string
>(pfi.
name.value());
883 if (pfi.
charge.has_value())
885 j[
"charge"] = pfi.
charge.value();
892 j.at(
"chains").get_to(pfi.
chains);
893 if (j.contains(
"name") && !j.at(
"name").is_null())
895 pfi.
name =
String(j.at(
"name").get<std::string>());
899 pfi.
name = std::nullopt;
901 if (j.contains(
"charge") && !j.at(
"charge").is_null())
907 pfi.
charge = std::nullopt;
909 if (j.contains(
"is_chimeric"))
928 nlohmann::json sites_json = nlohmann::json::array();
929 for (
const auto& [chain_idx, site_idx] : clg.
sites)
931 sites_json.push_back(nlohmann::json{{
"chain_index", chain_idx}, {
"site_index", site_idx}});
934 {
"label",
static_cast<std::string
>(clg.
label)},
935 {
"sites", sites_json}
942 clg.
label = j.at(
"label").get<std::string>();
944 for (
const auto& site : j.at(
"sites"))
946 size_t chain_idx = site.at(
"chain_index").get<
size_t>();
947 size_t site_idx = site.at(
"site_index").get<
size_t>();
948 clg.
sites.emplace_back(chain_idx, site_idx);
A more convenient string class.
Definition String.h:34
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
void to_json(nlohmann::json &j, const ProForma::CvDatabase &db)
Convert CvDatabase enum to JSON string.
Definition ProFormaDataJson.h:25
void from_json(const nlohmann::json &j, ProForma::CvDatabase &db)
Construct CvDatabase enum from JSON string.
Definition ProFormaDataJson.h:38