Gemmi C++ API
Loading...
Searching...
No Matches
to_chemcomp.hpp
Go to the documentation of this file.
1// Copyright 2022 Global Phasing Ltd.
2//
3// Create cif::Block with monomer library _chem_comp* categories
4// from struct ChemComp.
5
6#ifndef GEMMI_TO_CHEMCOMP_HPP_
7#define GEMMI_TO_CHEMCOMP_HPP_
8
9#include "chemcomp.hpp" // for ChemComp
10#include "sprintf.hpp" // for to_str
11
12namespace gemmi {
13
14inline void add_chemcomp_to_block(const ChemComp& cc, cif::Block& block) {
15 {
16 std::vector<std::string> tags =
17 {"comp_id", "atom_id", "type_symbol", "type_energy", "charge"};
18 if (cc.has_coordinates)
19 for (char c = 'x'; c <= 'z'; ++c)
20 tags.emplace_back(1, c);
21 cif::Table tab = block.find_or_add("_chem_comp_atom.", tags);
22 tab.ensure_loop();
23 size_t pos = tab.length();
24 cif::Loop& loop = tab.loop_item->loop;
25 loop.values.resize(loop.values.size() + loop.width() * cc.atoms.size(), ".");
26 for (const ChemComp::Atom& a : cc.atoms) {
27 cif::Table::Row row = tab[pos++];
28 row[0] = cc.name;
29 row[1] = a.id;
30 row[2] = a.el.name();
31 row[3] = cif::quote(a.chem_type);
32 row[4] = std::to_string(iround(a.charge));
33 if (cc.has_coordinates) {
34 row[5] = to_str(a.xyz.x);
35 row[6] = to_str(a.xyz.y);
36 row[7] = to_str(a.xyz.z);
37 }
38 }
39 }
40 {
41 cif::Table tab = block.find_or_add("_chem_comp_bond.",
42 {"comp_id", "atom_id_1", "atom_id_2", "type", "aromatic",
43 "value_dist", "value_dist_esd", "value_dist_nucleus", "value_dist_nucleus_esd"});
44 for (const Restraints::Bond& a : cc.rt.bonds)
45 tab.append_row({cc.name, a.id1.atom, a.id2.atom, bond_type_to_string(a.type),
46 std::string(1, a.aromatic ? 'y' : 'n'),
47 to_str(a.value), to_str(a.esd),
48 to_str(a.value_nucleus), to_str(a.esd_nucleus)});
49 }
50 {
51 cif::Table tab = block.find_or_add("_chem_comp_angle.",
52 {"comp_id", "atom_id_1", "atom_id_2", "atom_id_3",
53 "value_angle", "value_angle_esd"});
54 for (const Restraints::Angle& a : cc.rt.angles)
55 tab.append_row({cc.name, a.id1.atom, a.id2.atom, a.id3.atom,
56 to_str(a.value), to_str(a.esd)});
57 }
58 {
59 cif::Table tab = block.find_or_add("_chem_comp_tor.",
60 {"comp_id", "id", "atom_id_1", "atom_id_2", "atom_id_3", "atom_id_4",
61 "value_angle", "value_angle_esd", "period"});
62 for (const Restraints::Torsion& a : cc.rt.torsions)
63 tab.append_row({cc.name, a.label, a.id1.atom, a.id2.atom, a.id3.atom, a.id4.atom,
64 to_str(a.value), to_str(a.esd), std::to_string(a.period)});
65 }
66 {
67 cif::Table tab = block.find_or_add("_chem_comp_chir.",
68 {"comp_id", "id", "atom_id_centre", "atom_id_1", "atom_id_2", "atom_id_3",
69 "volume_sign"});
70 for (const Restraints::Chirality& a : cc.rt.chirs) {
71 std::string label = "chir_" + std::to_string(tab.length() + 1);
72 tab.append_row({cc.name, label, a.id_ctr.atom, a.id1.atom, a.id2.atom, a.id3.atom,
73 chirality_to_string(a.sign)});
74 }
75 }
76 {
77 cif::Table tab = block.find_or_add("_chem_comp_plane_atom.",
78 {"comp_id", "plane_id", "atom_id", "dist_esd"});
79 for (const Restraints::Plane& p : cc.rt.planes)
80 for (const Restraints::AtomId& atom_id : p.ids)
81 tab.append_row({cc.name, p.label, atom_id.atom, to_str(p.esd)});
82 }
83}
84
85} // namespace gemmi
86#endif
ChemComp - chemical component that represents a monomer from Refmac monomer library,...
std::string quote(std::string v)
Definition cifdoc.hpp:1180
int iround(double d)
Definition math.hpp:44
const char * chirality_to_string(ChiralityType chir_type)
Definition chemcomp.hpp:574
void add_chemcomp_to_block(const ChemComp &cc, cif::Block &block)
const char * bond_type_to_string(BondType btype)
Definition chemcomp.hpp:527
GEMMI_DLL int GEMMI_DLL int std::string to_str(double d)
Definition sprintf.hpp:36
interface to stb_sprintf: snprintf_z, to_str(float|double)
std::string name
Definition chemcomp.hpp:369
Restraints rt
Definition chemcomp.hpp:375
std::vector< Atom > atoms
Definition chemcomp.hpp:373
std::vector< Bond > bonds
Definition chemcomp.hpp:141
Table find_or_add(const std::string &prefix, std::vector< std::string > tags)
Definition cifdoc.hpp:476
size_t width() const
Definition cifdoc.hpp:141
std::vector< std::string > values
Definition cifdoc.hpp:129
void ensure_loop()
if it's pairs, convert it to loop
Definition cifdoc.hpp:796
size_t length() const
Definition cifdoc.hpp:733
void append_row(const T &new_values)
Definition cifdoc.hpp:749