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 if (!tab.loop_item)
24 size_t pos = tab.length();
25 cif::Loop& loop = tab.loop_item->loop;
26 loop.values.resize(loop.values.size() + loop.width() * cc.atoms.size(), ".");
27 for (const ChemComp::Atom& a : cc.atoms) {
28 cif::Table::Row row = tab[pos++];
29 row[0] = cc.name;
30 row[1] = a.id;
31 row[2] = a.el.name();
32 row[3] = cif::quote(a.chem_type);
33 row[4] = std::to_string(iround(a.charge));
34 if (cc.has_coordinates) {
35 row[5] = to_str(a.xyz.x);
36 row[6] = to_str(a.xyz.y);
37 row[7] = to_str(a.xyz.z);
38 }
39 }
40 }
41 {
42 cif::Table tab = block.find_or_add("_chem_comp_bond.",
43 {"comp_id", "atom_id_1", "atom_id_2", "type", "aromatic",
44 "value_dist", "value_dist_esd", "value_dist_nucleus", "value_dist_nucleus_esd"});
45 for (const Restraints::Bond& a : cc.rt.bonds)
46 tab.append_row({cc.name, a.id1.atom, a.id2.atom, bond_type_to_string(a.type),
47 std::string(1, a.aromatic ? 'y' : 'n'),
48 to_str(a.value), to_str(a.esd),
49 to_str(a.value_nucleus), to_str(a.esd_nucleus)});
50 }
51 {
52 cif::Table tab = block.find_or_add("_chem_comp_angle.",
53 {"comp_id", "atom_id_1", "atom_id_2", "atom_id_3",
54 "value_angle", "value_angle_esd"});
55 for (const Restraints::Angle& a : cc.rt.angles)
56 tab.append_row({cc.name, a.id1.atom, a.id2.atom, a.id3.atom,
57 to_str(a.value), to_str(a.esd)});
58 }
59 {
60 cif::Table tab = block.find_or_add("_chem_comp_tor.",
61 {"comp_id", "id", "atom_id_1", "atom_id_2", "atom_id_3", "atom_id_4",
62 "value_angle", "value_angle_esd", "period"});
63 for (const Restraints::Torsion& a : cc.rt.torsions)
64 tab.append_row({cc.name, a.label, a.id1.atom, a.id2.atom, a.id3.atom, a.id4.atom,
65 to_str(a.value), to_str(a.esd), std::to_string(a.period)});
66 }
67 {
68 cif::Table tab = block.find_or_add("_chem_comp_chir.",
69 {"comp_id", "id", "atom_id_centre", "atom_id_1", "atom_id_2", "atom_id_3",
70 "volume_sign"});
71 for (const Restraints::Chirality& a : cc.rt.chirs) {
72 std::string label = "chir_" + std::to_string(tab.length() + 1);
73 tab.append_row({cc.name, label, a.id_ctr.atom, a.id1.atom, a.id2.atom, a.id3.atom,
74 chirality_to_string(a.sign)});
75 }
76 }
77 {
78 cif::Table tab = block.find_or_add("_chem_comp_plane_atom.",
79 {"comp_id", "plane_id", "atom_id", "dist_esd"});
80 for (const Restraints::Plane& p : cc.rt.planes)
81 for (const Restraints::AtomId& atom_id : p.ids)
82 tab.append_row({cc.name, p.label, atom_id.atom, to_str(p.esd)});
83 }
84}
85
86} // namespace gemmi
87#endif
std::string quote(std::string v)
Definition cifdoc.hpp:1162
int iround(double d)
Definition math.hpp:37
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
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:470
size_t width() const
Definition cifdoc.hpp:142
std::vector< std::string > values
Definition cifdoc.hpp:130
void append_row(T new_values)
Definition cifdoc.hpp:742
size_t length() const
Definition cifdoc.hpp:726
void convert_pair_to_loop()
Definition cifdoc.hpp:790