Refinement: the side-chain rotamer helpers of ClashRelief in a header of their own

MakeSideChain, SideChain::Build, the staggered rotamer list and the overlap against the rest of the model move from
ClashRelief.cpp to SideChainRotamers.{h,cpp}, unchanged, so that the side-chain field can turn side chains and
score their contacts the same way. RelieveSideChainClashes is as it was.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
This commit is contained in:
leonarski_fandClaude Fable 5.1 committed 2026-11-14 16:53:42 +01:00
1 parent 058ba80fc7
commit 2676b22312
3 files changed
+244 -196

No files matched your search

@@ -2,204 +2,17 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "ClashRelief.h"
#include "SideChainRotamers.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <map>
#include <optional>
#include <gemmi/calculate.hpp>
#include <gemmi/modify.hpp>
#include <gemmi/neighbor.hpp>
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<const char *, 4>;
// The chi angles of each amino acid, by the atoms that define them.
const std::map<std::string, std::vector<Chi>> &ChiAtoms() {
static const std::map<std::string, std::vector<Chi>> 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<gemmi::Position> pos; // the residue's atoms, as given
std::vector<int> depth;
std::vector<std::array<int, 4>> chi; // atom indices of each chi
double Dihedral(const std::vector<gemmi::Position> &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<gemmi::Position> Build(const std::vector<double> &chis) const {
std::vector<gemmi::Position> 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<SideChain> 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<int, 4> 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<int>(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<gemmi::Position> &p, const std::vector<const gemmi::Atom *> &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<std::vector<double>> Rotamers(const std::string &resname, size_t n_chi) {
std::vector<std::vector<double>> out = {{}};
for (size_t k = 0; k < n_chi; k++) {
std::vector<double> 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<std::vector<double>> next;
for (const std::vector<double> &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<SideChainTurn> RelieveSideChainClashes(gemmi::Structure &st) {
auto overlap = [&](const std::vector<gemmi::Position> &p) {
return OverlapOf(ns, model, cell, res, static_cast<int>(ci), static_cast<int>(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<double> 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<double> chi = sc->Chis();
SideChainOverlap best = given;
std::vector<double> best_chi = chi;
for (const std::vector<double> &chis : Rotamers(res.name, sc->chi.size())) {
const Overlap o = overlap(sc->Build(chis));
for (const std::vector<double> &chis : SideChainRotamers(res.name, sc->chi.size())) {
const SideChainOverlap o = overlap(sc->Build(chis));
if (o.sum < best.sum) {
best = o;
best_chi = chis;
@@ -0,0 +1,187 @@
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "SideChainRotamers.h"
#include <algorithm>
#include <cmath>
#include <map>
#include <gemmi/calculate.hpp>
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<const char *, 4>;
// The chi angles of each amino acid, by the atoms that define them.
const std::map<std::string, std::vector<Chi>> &ChiAtoms() {
static const std::map<std::string, std::vector<Chi>> 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<gemmi::Position> &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<gemmi::Position> SideChain::Build(const std::vector<double> &chis) const {
std::vector<gemmi::Position> 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<double> SideChain::Chis() const {
std::vector<double> out;
for (size_t k = 0; k < chi.size(); k++)
out.push_back(Dihedral(pos, k));
return out;
}
std::optional<SideChain> 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<int, 4> 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<int>(found - res.atoms.begin());
}
sc.chi.push_back(idx);
}
return sc;
}
std::vector<std::vector<double>> SideChainRotamers(const std::string &resname, size_t n_chi) {
std::vector<std::vector<double>> out = {{}};
for (size_t k = 0; k < n_chi; k++) {
std::vector<double> 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<std::vector<double>> next;
for (const std::vector<double> &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<gemmi::Position> &p, const std::vector<const gemmi::Atom *> &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;
}
@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// 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 <array>
#include <optional>
#include <string>
#include <vector>
#include <gemmi/model.hpp>
#include <gemmi/neighbor.hpp>
#include <gemmi/unitcell.hpp>
// 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<gemmi::Position> pos; // the residue's atoms, as given
std::vector<int> depth;
std::vector<std::array<int, 4>> chi; // atom indices of each chi
double Dihedral(const std::vector<gemmi::Position> &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<gemmi::Position> Build(const std::vector<double> &chis) const;
std::vector<double> 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<SideChain> 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<std::vector<double>> 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<gemmi::Position> &p, const std::vector<const gemmi::Atom *> &linked);