diff --git a/examples/.1cbs.cif.gz.swp b/examples/.1cbs.cif.gz.swp new file mode 100644 index 00000000..fe5c9ae4 Binary files /dev/null and b/examples/.1cbs.cif.gz.swp differ diff --git a/src/pdb/pdb2cif.cpp b/src/pdb/pdb2cif.cpp index f41f9847..b808fc22 100644 --- a/src/pdb/pdb2cif.cpp +++ b/src/pdb/pdb2cif.cpp @@ -4511,6 +4511,47 @@ void PDBFileParser::ConstructEntities() } } } + + // Finish by calculating the formula_weight for each entity + for (auto entity : *getCategory("entity")) + { + auto entity_id = entity["id"].as(); + float formula_weight = 0; + + if (entity["type"] == "polymer") + { + int n = 0; + + for (std::string comp_id : getCategory("pdbx_poly_seq_scheme")->find(cif::key("entity_id") == entity_id, "mon_id")) + { + auto compound = cif::compound_factory::instance().create(comp_id); + assert(compound); + if (not compound) + throw std::runtime_error("missing information for compound " + comp_id); + formula_weight += compound->formula_weight(); + ++n; + } + + formula_weight -= (n - 1) * 18.015; + } + else if (entity["type"] == "water") + formula_weight = 18.015; + else + { + auto comp_id = getCategory("pdbx_nonpoly_scheme")->find_first>(cif::key("entity_id") == entity_id, "mon_id"); + if (comp_id.has_value()) + { + auto compound = cif::compound_factory::instance().create(*comp_id); + assert(compound); + if (not compound) + throw std::runtime_error("missing information for compound " + *comp_id); + formula_weight = compound->formula_weight(); + } + } + + if (formula_weight > 0) + entity["formula_weight"] = formula_weight; + } } void PDBFileParser::ConstructSugarTrees(int &asymNr) diff --git a/test/.1juh.cif.gz.swp b/test/.1juh.cif.gz.swp deleted file mode 100644 index 7cb69acd..00000000 Binary files a/test/.1juh.cif.gz.swp and /dev/null differ diff --git a/test/pdb1cbs.ent.gz b/test/pdb1cbs.ent.gz new file mode 100644 index 00000000..2801ea2a Binary files /dev/null and b/test/pdb1cbs.ent.gz differ diff --git a/test/unit-v2-test.cpp b/test/unit-v2-test.cpp index ea93250e..f00a8270 100644 --- a/test/unit-v2-test.cpp +++ b/test/unit-v2-test.cpp @@ -3468,3 +3468,22 @@ TEST_CASE("compound_not_found_test_1") auto cmp = cif::compound_factory::instance().create("&&&"); REQUIRE(cmp == nullptr); } + +// -------------------------------------------------------------------- +// PDB2CIF tests + +TEST_CASE("pdb2cif_formula_weight") +{ + cif::compound_factory::instance().push_dictionary(gTestDir / "REA.cif"); + + cif::file a = cif::pdb::read(gTestDir / "pdb1cbs.ent.gz"); + + auto fw = a.front()["entity"].find1(cif::key("id") == 1, "formula_weight"); + CHECK(std::abs(fw - 15581.802f) < 0.1f); + + fw = a.front()["entity"].find1(cif::key("id") == 2, "formula_weight"); + CHECK(fw == 300.435f); + + fw = a.front()["entity"].find1(cif::key("id") == 3, "formula_weight"); + CHECK(fw == 18.015f); +} \ No newline at end of file