Skip to content

Commit

Permalink
Add formula_weight to entity in pdb2cif
Browse files Browse the repository at this point in the history
  • Loading branch information
mhekkel committed Dec 13, 2023
1 parent 95a6b42 commit e1a1c11
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Binary file added examples/.1cbs.cif.gz.swp
Binary file not shown.
41 changes: 41 additions & 0 deletions src/pdb/pdb2cif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>();
float formula_weight = 0;

if (entity["type"] == "polymer")
{
int n = 0;

for (std::string comp_id : getCategory("pdbx_poly_seq_scheme")->find<std::string>(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<std::optional<std::string>>(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)
Expand Down
Binary file removed test/.1juh.cif.gz.swp
Binary file not shown.
Binary file added test/pdb1cbs.ent.gz
Binary file not shown.
19 changes: 19 additions & 0 deletions test/unit-v2-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>(cif::key("id") == 1, "formula_weight");
CHECK(std::abs(fw - 15581.802f) < 0.1f);

fw = a.front()["entity"].find1<float>(cif::key("id") == 2, "formula_weight");
CHECK(fw == 300.435f);

fw = a.front()["entity"].find1<float>(cif::key("id") == 3, "formula_weight");
CHECK(fw == 18.015f);
}

0 comments on commit e1a1c11

Please sign in to comment.