diff --git a/image_analysis/structure_refinement/ClashRelief.cpp b/image_analysis/structure_refinement/ClashRelief.cpp index 67979e4f2..2100f886b 100644 --- a/image_analysis/structure_refinement/ClashRelief.cpp +++ b/image_analysis/structure_refinement/ClashRelief.cpp @@ -2,204 +2,17 @@ // SPDX-License-Identifier: GPL-3.0-only #include "ClashRelief.h" +#include "SideChainRotamers.h" #include -#include -#include #include -#include -#include #include #include namespace { -constexpr double PI = 3.14159265358979323846; -constexpr double HBOND_CONTACT = 2.6; // A: between N and O, as the refinement's repulsions have it -constexpr double REACH = 4.0; // A: the farthest pair that can overlap - -using Chi = std::array; - -// The chi angles of each amino acid, by the atoms that define them. -const std::map> &ChiAtoms() { - static const std::map> chis = { - {"ARG", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "NE"}, {"CG", "CD", "NE", "CZ"}}}, - {"ASN", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "OD1"}}}, - {"ASP", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "OD1"}}}, - {"GLN", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "OE1"}}}, - {"GLU", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "OE1"}}}, - {"HIS", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "ND1"}}}, - {"ILE", {{"N", "CA", "CB", "CG1"}, {"CA", "CB", "CG1", "CD1"}}}, - {"LEU", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, - {"LYS", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "CE"}, {"CG", "CD", "CE", "NZ"}}}, - {"MET", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "SD"}, {"CB", "CG", "SD", "CE"}}}, - {"PHE", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, - {"SER", {{"N", "CA", "CB", "OG"}}}, - {"THR", {{"N", "CA", "CB", "OG1"}}}, - {"TRP", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, - {"TYR", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, - {"VAL", {{"N", "CA", "CB", "CG1"}}}, - }; - return chis; -} - -// The last chi turns a planar group (an amide, a carboxylate, a ring, a guanidinium): any angle, not only the -// staggered ones. -bool PlanarEnd(const std::string &res) { - return res == "ASP" || res == "ASN" || res == "HIS" || res == "PHE" || res == "TYR" || res == "TRP" || - res == "GLU" || res == "GLN" || res == "ARG"; -} - -// How far along the side chain an atom is, hydrogens by their parent's letter: CB 1, gamma 2, delta 3, epsilon 4, -// zeta 5, eta 6; the main chain and its hydrogens 0. -int Depth(const std::string &name) { - if (name.size() < 2 || name == "CA" || name == "OXT") - return 0; - switch (name[1]) { - case 'B': return 1; - case 'G': return 2; - case 'D': return 3; - case 'E': return 4; - case 'Z': return 5; - case 'H': return 6; - default: return 0; - } -} - -double VdwRadius(gemmi::El el) { - switch (el) { - case gemmi::El::N: return 1.55; - case gemmi::El::O: return 1.52; - case gemmi::El::S: - case gemmi::El::P: return 1.80; - default: return 1.70; - } -} - -bool Polar(gemmi::El el) { - return el == gemmi::El::N || el == gemmi::El::O; -} - -gemmi::Position Rotate(const gemmi::Position &p, const gemmi::Position &a, const gemmi::Position &b, double angle) { - const gemmi::Vec3 k = (b - a).normalized(); - const gemmi::Vec3 v = p - b; - const double c = std::cos(angle), s = std::sin(angle); - return gemmi::Position(gemmi::Vec3(b) + v * c + k.cross(v) * s + k * (k.dot(v) * (1 - c))); -} - -struct SideChain { - std::vector pos; // the residue's atoms, as given - std::vector depth; - std::vector> chi; // atom indices of each chi - - double Dihedral(const std::vector &p, size_t k) const { - const auto &c = chi[k]; - return gemmi::calculate_dihedral(p[c[0]], p[c[1]], p[c[2]], p[c[3]]) * 180.0 / PI; - } - - // The residue's atoms with the side chain at these chi angles (degrees), turned from the given ones. - std::vector Build(const std::vector &chis) const { - std::vector p = pos; - for (size_t k = 0; k < chi.size(); k++) { - const double angle = (chis[k] - Dihedral(p, k)) * PI / 180.0; - const int pivot = depth[chi[k][2]]; - for (size_t i = 0; i < p.size(); i++) - if (depth[i] > pivot) - p[i] = Rotate(p[i], p[chi[k][1]], p[chi[k][2]], angle); - } - return p; - } -}; - -// The residue's chi atoms, found by name; none where any is missing (an incomplete side chain stays as it is) or -// the residue has alternative conformations (its atoms would be found by name once, for both). -std::optional MakeSideChain(const gemmi::Residue &res) { - auto it = ChiAtoms().find(res.name); - if (it == ChiAtoms().end()) - return std::nullopt; - if (std::any_of(res.atoms.begin(), res.atoms.end(), [](const gemmi::Atom &a) { return a.altloc != '\0'; })) - return std::nullopt; - SideChain sc; - for (const gemmi::Atom &a : res.atoms) { - sc.pos.push_back(a.pos); - sc.depth.push_back(Depth(a.name)); - } - for (const Chi &c : it->second) { - std::array idx{}; - for (int k = 0; k < 4; k++) { - const auto found = std::find_if(res.atoms.begin(), res.atoms.end(), - [&](const gemmi::Atom &a) { return a.name == c[k]; }); - if (found == res.atoms.end()) - return std::nullopt; - idx[k] = static_cast(found - res.atoms.begin()); - } - sc.chi.push_back(idx); - } - return sc; -} - -// The overlaps of a residue's turned atoms (depth 2 and beyond, heavy) with the rest of the model: the sum of -// squared overlaps and the deepest one. Its own atoms, hydrogens, absent atoms (occupancy 0), metals and the -// other conformations of an alternative are no contact; nor is a covalent partner (`linked`: a LINK's, or a -// disulfide's). -struct Overlap { - double sum = 0, deepest = 0; -}; - -Overlap OverlapOf(gemmi::NeighborSearch &ns, const gemmi::Model &model, const gemmi::UnitCell &cell, - const gemmi::Residue &res, int chain_idx, int residue_idx, const SideChain &sc, - const std::vector &p, const std::vector &linked) { - Overlap out; - for (size_t i = 0; i < p.size(); i++) { - const gemmi::Atom &a = res.atoms[i]; - if (sc.depth[i] < 2 || a.is_hydrogen()) - continue; - ns.for_each(p[i], '\0', REACH, [&](const gemmi::NeighborSearch::Mark &m, double) { - if (m.image_idx == 0 && m.chain_idx == chain_idx && m.residue_idx == residue_idx) - return; // its own atoms; its own symmetry copy is another residue to it - const gemmi::Atom &b = model.chains[m.chain_idx].residues[m.residue_idx].atoms[m.atom_idx]; - if (b.is_hydrogen() || !(b.occ > 0) || b.element.is_metal()) - return; - if (std::find(linked.begin(), linked.end(), &b) != linked.end()) - return; - if (a.altloc && b.altloc && a.altloc != b.altloc) - return; - if (a.element == gemmi::El::S && b.element == gemmi::El::S) - return; // a disulfide, where it is one, is no clash - const double d = cell.find_nearest_pbc_image(p[i], b.pos, m.image_idx).dist(); - const double d0 = Polar(a.element.elem) && Polar(b.element.elem) - ? HBOND_CONTACT - : VdwRadius(a.element.elem) + VdwRadius(b.element.elem); - if (d < d0) { - out.sum += (d0 - d) * (d0 - d); - out.deepest = std::max(out.deepest, d0 - d); - } - }); - } - return out; -} - -// The rotamers tried: each chi at the staggered angles, the last one of a planar group every 30 degrees. -std::vector> Rotamers(const std::string &resname, size_t n_chi) { - std::vector> out = {{}}; - for (size_t k = 0; k < n_chi; k++) { - std::vector values = {-60, 60, 180}; - if (k + 1 == n_chi && PlanarEnd(resname)) { - values.clear(); - for (int deg = -150; deg <= 180; deg += 30) - values.push_back(deg); - } - std::vector> next; - for (const std::vector &head : out) - for (double v : values) { - next.push_back(head); - next.back().push_back(v); - } - out = std::move(next); - } - return out; -} +constexpr double REACH = 4.0; // A: the farthest pair that can overlap (OverlapOf) } // namespace @@ -244,17 +57,15 @@ std::vector RelieveSideChainClashes(gemmi::Structure &st) { auto overlap = [&](const std::vector &p) { return OverlapOf(ns, model, cell, res, static_cast(ci), static_cast(ri), *sc, p, linked); }; - const Overlap given = overlap(sc->pos); + const SideChainOverlap given = overlap(sc->pos); const auto earlier = turned_index.find({ci, ri}); if (given.deepest < CLASH_RELIEF_OVERLAP && earlier == turned_index.end()) continue; - std::vector chi; - for (size_t k = 0; k < sc->chi.size(); k++) - chi.push_back(sc->Dihedral(sc->pos, k)); - Overlap best = given; + const std::vector chi = sc->Chis(); + SideChainOverlap best = given; std::vector best_chi = chi; - for (const std::vector &chis : Rotamers(res.name, sc->chi.size())) { - const Overlap o = overlap(sc->Build(chis)); + for (const std::vector &chis : SideChainRotamers(res.name, sc->chi.size())) { + const SideChainOverlap o = overlap(sc->Build(chis)); if (o.sum < best.sum) { best = o; best_chi = chis; diff --git a/image_analysis/structure_refinement/SideChainRotamers.cpp b/image_analysis/structure_refinement/SideChainRotamers.cpp new file mode 100644 index 000000000..9117910cb --- /dev/null +++ b/image_analysis/structure_refinement/SideChainRotamers.cpp @@ -0,0 +1,187 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#include "SideChainRotamers.h" + +#include +#include +#include + +#include + +namespace { + +constexpr double PI = 3.14159265358979323846; +constexpr double HBOND_CONTACT = 2.6; // A: between N and O, as the refinement's repulsions have it +constexpr double REACH = 4.0; // A: the farthest pair that can overlap + +using Chi = std::array; + +// The chi angles of each amino acid, by the atoms that define them. +const std::map> &ChiAtoms() { + static const std::map> chis = { + {"ARG", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "NE"}, {"CG", "CD", "NE", "CZ"}}}, + {"ASN", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "OD1"}}}, + {"ASP", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "OD1"}}}, + {"GLN", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "OE1"}}}, + {"GLU", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "OE1"}}}, + {"HIS", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "ND1"}}}, + {"ILE", {{"N", "CA", "CB", "CG1"}, {"CA", "CB", "CG1", "CD1"}}}, + {"LEU", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, + {"LYS", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD"}, {"CB", "CG", "CD", "CE"}, {"CG", "CD", "CE", "NZ"}}}, + {"MET", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "SD"}, {"CB", "CG", "SD", "CE"}}}, + {"PHE", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, + {"SER", {{"N", "CA", "CB", "OG"}}}, + {"THR", {{"N", "CA", "CB", "OG1"}}}, + {"TRP", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, + {"TYR", {{"N", "CA", "CB", "CG"}, {"CA", "CB", "CG", "CD1"}}}, + {"VAL", {{"N", "CA", "CB", "CG1"}}}, + }; + return chis; +} + +// The last chi turns a planar group (an amide, a carboxylate, a ring, a guanidinium): any angle, not only the +// staggered ones. +bool PlanarEnd(const std::string &res) { + return res == "ASP" || res == "ASN" || res == "HIS" || res == "PHE" || res == "TYR" || res == "TRP" || + res == "GLU" || res == "GLN" || res == "ARG"; +} + +double VdwRadius(gemmi::El el) { + switch (el) { + case gemmi::El::N: return 1.55; + case gemmi::El::O: return 1.52; + case gemmi::El::S: + case gemmi::El::P: return 1.80; + default: return 1.70; + } +} + +bool Polar(gemmi::El el) { + return el == gemmi::El::N || el == gemmi::El::O; +} + +gemmi::Position Rotate(const gemmi::Position &p, const gemmi::Position &a, const gemmi::Position &b, double angle) { + const gemmi::Vec3 k = (b - a).normalized(); + const gemmi::Vec3 v = p - b; + const double c = std::cos(angle), s = std::sin(angle); + return gemmi::Position(gemmi::Vec3(b) + v * c + k.cross(v) * s + k * (k.dot(v) * (1 - c))); +} + +} // namespace + +int SideChainDepth(const std::string &name) { + if (name.size() < 2 || name == "CA" || name == "OXT") + return 0; + switch (name[1]) { + case 'B': return 1; + case 'G': return 2; + case 'D': return 3; + case 'E': return 4; + case 'Z': return 5; + case 'H': return 6; + default: return 0; + } +} + +double SideChain::Dihedral(const std::vector &p, size_t k) const { + const auto &c = chi[k]; + return gemmi::calculate_dihedral(p[c[0]], p[c[1]], p[c[2]], p[c[3]]) * 180.0 / PI; +} + +std::vector SideChain::Build(const std::vector &chis) const { + std::vector p = pos; + for (size_t k = 0; k < chi.size(); k++) { + const double angle = (chis[k] - Dihedral(p, k)) * PI / 180.0; + const int pivot = depth[chi[k][2]]; + for (size_t i = 0; i < p.size(); i++) + if (depth[i] > pivot) + p[i] = Rotate(p[i], p[chi[k][1]], p[chi[k][2]], angle); + } + return p; +} + +std::vector SideChain::Chis() const { + std::vector out; + for (size_t k = 0; k < chi.size(); k++) + out.push_back(Dihedral(pos, k)); + return out; +} + +std::optional MakeSideChain(const gemmi::Residue &res) { + auto it = ChiAtoms().find(res.name); + if (it == ChiAtoms().end()) + return std::nullopt; + if (std::any_of(res.atoms.begin(), res.atoms.end(), [](const gemmi::Atom &a) { return a.altloc != '\0'; })) + return std::nullopt; + SideChain sc; + for (const gemmi::Atom &a : res.atoms) { + sc.pos.push_back(a.pos); + sc.depth.push_back(SideChainDepth(a.name)); + } + for (const Chi &c : it->second) { + std::array idx{}; + for (int k = 0; k < 4; k++) { + const auto found = std::find_if(res.atoms.begin(), res.atoms.end(), + [&](const gemmi::Atom &a) { return a.name == c[k]; }); + if (found == res.atoms.end()) + return std::nullopt; + idx[k] = static_cast(found - res.atoms.begin()); + } + sc.chi.push_back(idx); + } + return sc; +} + +std::vector> SideChainRotamers(const std::string &resname, size_t n_chi) { + std::vector> out = {{}}; + for (size_t k = 0; k < n_chi; k++) { + std::vector values = {-60, 60, 180}; + if (k + 1 == n_chi && PlanarEnd(resname)) { + values.clear(); + for (int deg = -150; deg <= 180; deg += 30) + values.push_back(deg); + } + std::vector> next; + for (const std::vector &head : out) + for (double v : values) { + next.push_back(head); + next.back().push_back(v); + } + out = std::move(next); + } + return out; +} + +SideChainOverlap OverlapOf(gemmi::NeighborSearch &ns, const gemmi::Model &model, const gemmi::UnitCell &cell, + const gemmi::Residue &res, int chain_idx, int residue_idx, const SideChain &sc, + const std::vector &p, const std::vector &linked) { + SideChainOverlap out; + for (size_t i = 0; i < p.size(); i++) { + const gemmi::Atom &a = res.atoms[i]; + if (sc.depth[i] < 2 || a.is_hydrogen()) + continue; + ns.for_each(p[i], '\0', REACH, [&](const gemmi::NeighborSearch::Mark &m, double) { + if (m.image_idx == 0 && m.chain_idx == chain_idx && m.residue_idx == residue_idx) + return; // its own atoms; its own symmetry copy is another residue to it + const gemmi::Atom &b = model.chains[m.chain_idx].residues[m.residue_idx].atoms[m.atom_idx]; + if (b.is_hydrogen() || !(b.occ > 0) || b.element.is_metal()) + return; + if (std::find(linked.begin(), linked.end(), &b) != linked.end()) + return; + if (a.altloc && b.altloc && a.altloc != b.altloc) + return; + if (a.element == gemmi::El::S && b.element == gemmi::El::S) + return; // a disulfide, where it is one, is no clash + const double d = cell.find_nearest_pbc_image(p[i], b.pos, m.image_idx).dist(); + const double d0 = Polar(a.element.elem) && Polar(b.element.elem) + ? HBOND_CONTACT + : VdwRadius(a.element.elem) + VdwRadius(b.element.elem); + if (d < d0) { + out.sum += (d0 - d) * (d0 - d); + out.deepest = std::max(out.deepest, d0 - d); + } + }); + } + return out; +} diff --git a/image_analysis/structure_refinement/SideChainRotamers.h b/image_analysis/structure_refinement/SideChainRotamers.h new file mode 100644 index 000000000..c86490b75 --- /dev/null +++ b/image_analysis/structure_refinement/SideChainRotamers.h @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +// A residue's side chain turned about its chi bonds, and its overlap with the rest of the model: what ClashRelief +// (the side chains turned clear of another residue before refining) and SideChainField (the side chains searched +// against the likelihood) share. + +#include +#include +#include +#include + +#include +#include +#include + +// How far along the side chain an atom is, hydrogens by their parent's letter: CB 1, gamma 2, delta 3, epsilon 4, +// zeta 5, eta 6; the main chain and its hydrogens 0. +int SideChainDepth(const std::string &name); + +struct SideChain { + std::vector pos; // the residue's atoms, as given + std::vector depth; + std::vector> chi; // atom indices of each chi + double Dihedral(const std::vector &p, size_t k) const; + // The residue's atoms with the side chain at these chi angles (degrees), turned from the given ones. + std::vector Build(const std::vector &chis) const; + std::vector Chis() const; +}; + +// The residue's chi atoms, found by name; none for a residue without chi angles (Gly, Ala, Pro, Cys and anything +// but an amino acid), where any is missing (an incomplete side chain stays as it is) or the residue has alternative +// conformations (its atoms would be found by name once, for both). +std::optional MakeSideChain(const gemmi::Residue &res); + +// The rotamers tried: each chi at the staggered angles, the last one of a planar group every 30 degrees. +std::vector> SideChainRotamers(const std::string &resname, size_t n_chi); + +// The overlaps of a residue's turned atoms (depth 2 and beyond, heavy) with the rest of the model: the sum of +// squared overlaps and the deepest one. Its own atoms, hydrogens, absent atoms (occupancy 0), metals and the +// other conformations of an alternative are no contact; nor is a covalent partner (`linked`: a LINK's, or a +// disulfide's). Polar pairs may come to the hydrogen-bond distance, others to the sum of their van der Waals radii. +struct SideChainOverlap { + double sum = 0, deepest = 0; +}; +SideChainOverlap OverlapOf(gemmi::NeighborSearch &ns, const gemmi::Model &model, const gemmi::UnitCell &cell, + const gemmi::Residue &res, int chain_idx, int residue_idx, const SideChain &sc, + const std::vector &p, const std::vector &linked);