Merge rc187-tls-mr

This commit is contained in:
leonarski_f committed 2026-11-15 08:08:12 +01:00
commit 5079abf616
5 files changed
+180 -1

No files matched your search

@@ -46,6 +46,7 @@
#include "RigidBodyRefine.h"
#include "MolecularReplacement.h" // the warm-up of a prediction
#include "SigmaA.h"
#include "RefinementADP.h" // PlaceTlsGroups
#ifdef JFJOCH_USE_CUDA
#include "ModelStructureFactorsGPU.h"
#include "../indexing/CUDAMemHelpers.h" // cuda_trim_allocation_pool
@@ -2024,7 +2025,9 @@ ModelValidationResult Validate(const std::vector<MergedReflection> &merged,
result.ok = true;
result.maps = std::move(maps);
// The placed coordinates, for the caller to write out beside the maps once the frame is settled.
// The placed coordinates, for the caller to write out beside the maps once the frame is settled, with the model's
// TLS groups moved as its chains were.
PlaceTlsGroups(placed != nullptr ? *placed : model.structure, st);
result.placed_model = std::make_shared<gemmi::Structure>(st);
logger.Info("Model validation: R-work={:.4f} ({} refl) R-free={:.4f} ({} refl) "
"[overall + anisotropic B + bulk solvent, fitted on the working set only]",
@@ -39,6 +39,7 @@
#include "ModelScaling.h"
#include "ModelValidation.h"
#include "RigidBodyRefine.h"
#include "RefinementADP.h" // PlaceTlsGroups
#include "MolecularReplacementGPU.h"
#ifdef __APPLE__
#include "MolecularReplacementMetal.h"
@@ -4089,6 +4090,7 @@ MolecularReplacementResult RunMolecularReplacement(const RugnuxModel &model, con
if (c.placed && c.copies >= 1) {
auto compact = std::make_shared<gemmi::Structure>(*c.placed);
CompactCopies(*compact, c.copies);
PlaceTlsGroups(model.structure, *compact);
c.placed = compact;
}
return result;
@@ -4211,6 +4213,7 @@ std::optional<MolecularReplacementResult> CarryMolecularReplacement(const Molecu
}
}
auto placed = std::make_shared<gemmi::Structure>(AssembleCopies(body));
PlaceTlsGroups(*c.placed, *placed);
const PlacementScore score = score_placement(*placed, sg, *merge.merged, d_min, nthreads, &scorers);
const bool holds = score.r_free <= c.r_free + R_FREE_MARGIN;
logger.Info("Molecular replacement: the search on the {} ({}): its placement in {} ({} copies, R-free {:.3f} there){} "
@@ -2,10 +2,13 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "RefinementADP.h"
#include "gemmi/qcp.hpp" // superpose_positions
#include <algorithm>
#include <cmath>
#include <limits>
#include <map>
#include <tuple>
#include <Eigen/Dense>
@@ -317,3 +320,98 @@ std::vector<int> TlsSegments(const std::vector<std::vector<int>> &residue_atoms,
std::reverse(starts.begin(), starts.end());
return starts;
}
void PlaceTlsGroups(const gemmi::Structure &model, gemmi::Structure &placed) {
for (gemmi::RefinementInfo &ref : placed.meta.refinement)
ref.tls_groups.clear();
const std::vector<gemmi::TlsGroup> *groups = model.meta.get_tls_groups();
if (groups == nullptr || model.models.empty() || placed.models.empty())
return;
// The model's atoms by chain, residue and name.
using AtomKey = std::tuple<std::string, std::string, std::string, char>;
const auto key = [](const gemmi::Residue &r, const gemmi::Atom &a) {
return AtomKey(r.seqid.str(), r.name, a.name, a.altloc);
};
std::map<std::string, std::map<AtomKey, gemmi::Position>> model_atoms;
for (const gemmi::Chain &ch : model.models[0].chains)
for (const gemmi::Residue &r : ch.residues)
for (const gemmi::Atom &a : r.atoms)
model_atoms[ch.name][key(r, a)] = a.pos;
// The atoms of placed chain `c` (in the residues of `sel`, where given) and the same atoms of model chain `s`.
const auto pairs = [&](const std::string &c, const std::string &s, const gemmi::TlsGroup::Selection *sel,
std::vector<gemmi::Position> &p, std::vector<gemmi::Position> &q) {
const std::map<AtomKey, gemmi::Position> &m = model_atoms[s];
for (const gemmi::Chain &ch : placed.models[0].chains)
if (ch.name == c)
for (const gemmi::Residue &r : ch.residues)
if (sel == nullptr || (!(r.seqid < sel->res_begin) && !(sel->res_end < r.seqid)))
for (const gemmi::Atom &a : r.atoms)
if (auto it = m.find(key(r, a)); it != m.end()) {
p.push_back(a.pos);
q.push_back(it->second);
}
};
// Each placed chain, the model's chain it is a copy of.
std::map<std::string, std::string> source;
for (const gemmi::Chain &ch : placed.models[0].chains) {
double best = INFINITY;
for (const auto &[s, atoms] : model_atoms) {
std::vector<gemmi::Position> p, q;
pairs(ch.name, s, nullptr, p, q);
if (p.size() < 3)
continue;
const double rmsd = gemmi::calculate_rmsd_of_superposed_positions(p.data(), q.data(), p.size(), nullptr);
if (rmsd < best) {
best = rmsd;
source[ch.name] = s;
}
}
}
std::vector<gemmi::TlsGroup> out;
for (const gemmi::TlsGroup &g : *groups) {
// The copies of the group's chains, together where each chain was placed once, else each by itself.
std::vector<std::string> copies;
std::map<std::string, int> placed_times;
for (const auto &[c, s] : source)
if (std::any_of(g.selections.begin(), g.selections.end(),
[&](const gemmi::TlsGroup::Selection &sel) { return sel.chain == s; })) {
copies.push_back(c);
placed_times[s]++;
}
const bool whole = std::all_of(placed_times.begin(), placed_times.end(), [](const auto &t) { return t.second == 1; });
std::vector<std::vector<std::string>> bodies;
if (whole)
bodies.push_back(copies);
else
for (const std::string &c : copies)
bodies.push_back({c});
for (const std::vector<std::string> &body : bodies) {
gemmi::TlsGroup t = g;
t.selections.clear();
std::vector<gemmi::Position> p, q;
for (const std::string &c : body)
for (const gemmi::TlsGroup::Selection &sel : g.selections)
if (sel.chain == source[c]) {
t.selections.push_back(sel);
t.selections.back().chain = c;
pairs(c, sel.chain, &sel, p, q);
}
if (p.size() < 3)
continue;
// the model's atoms onto the placed ones: x' = R x + v
const gemmi::Transform tr = gemmi::superpose_positions(p.data(), q.data(), p.size(), nullptr).transform;
t.origin = gemmi::Position(tr.apply(g.origin));
t.T = g.T.transformed_by(tr.mat);
t.L = g.L.transformed_by(tr.mat);
t.S = tr.mat.multiply(g.S).multiply(tr.mat.transpose());
out.push_back(t);
}
}
for (size_t i = 0; i < out.size(); i++) {
out[i].num_id = static_cast<short>(i + 1);
out[i].id = std::to_string(i + 1);
}
if (placed.meta.refinement.empty())
placed.meta.refinement.emplace_back();
placed.meta.refinement[0].tls_groups = std::move(out);
}
@@ -77,3 +77,10 @@ double EvaluateURestraints(const URestraints &r, const std::vector<gemmi::Positi
// Following Painter & Merritt (2006) Acta Cryst. D62, 439-450
std::vector<int> TlsSegments(const std::vector<std::vector<int>> &residue_atoms, const std::vector<gemmi::Position> &pos,
const std::vector<double> &b, int k, int min_residues);
// The TLS groups of `model` on `placed`, a structure whose chains are rigid copies of the model's moved elsewhere (by
// molecular replacement, a symmetry operator, a lattice shift, a rigid-body fit), renamed and repeated as the
// placement made them; `placed`'s own groups replaced. Each placed chain is the copy of the model's chain its atoms
// superpose on best; a group goes with the copies of its chains, its origin moved and T, L and S turned (R X R^T) by
// the superposition of its atoms - whole where each of its chains was placed once, one group per copy otherwise.
void PlaceTlsGroups(const gemmi::Structure &model, gemmi::Structure &placed);
+68
View File
@@ -1801,6 +1801,74 @@ TEST_CASE("RefinementADP_tls_gradient", "[Refinement]") {
CHECK(u1.u33 == Catch::Approx(u0.u33).margin(1e-12));
}
TEST_CASE("RefinementADP_place_tls_groups", "[Refinement]") {
std::mt19937 rng(6);
std::uniform_real_distribution<double> d(-1.0, 1.0);
// A model of two chains, three residues of ten atoms each, a TLS group on each chain.
gemmi::Structure model;
model.models.emplace_back(1);
for (const char *name : {"A", "B"}) {
gemmi::Chain chain(name);
for (int r = 1; r <= 3; r++) {
gemmi::Residue res;
res.name = "ALA";
res.seqid = gemmi::SeqId(r, ' ');
for (int i = 0; i < 10; i++) {
gemmi::Atom a;
a.name = "C" + std::to_string(i);
a.pos = gemmi::Position(10 + 8 * d(rng), -3 + 8 * d(rng), 5 + 8 * d(rng));
res.atoms.push_back(a);
}
chain.residues.push_back(res);
}
model.models[0].chains.push_back(chain);
}
std::vector<TlsBody> bodies(2);
for (int b = 0; b < 2; b++) {
bodies[b].id = std::to_string(b + 1);
bodies[b].origin = gemmi::Position(9 + b, -2, 4);
std::array<double, TlsBody::PARAMETERS> p{};
for (double &x : p)
x = 0.01 * d(rng);
bodies[b].Set(p);
for (int i = 0; i < 30; i++)
bodies[b].atoms.push_back(30 * b + i);
}
model.meta.refinement.emplace_back();
model.meta.refinement[0].tls_groups = TlsBodiesToGroups(model.models[0], bodies);
// Placed as molecular replacement places one chain: chain A twice, each copy turned and moved, named A and B.
const gemmi::Mat33 r0 = gemmi::Mat33::from_columns(gemmi::Vec3(0, 1, 0), gemmi::Vec3(-1, 0, 0), gemmi::Vec3(0, 0, 1));
const gemmi::Mat33 r1 = gemmi::Mat33::from_columns(gemmi::Vec3(1, 0, 0), gemmi::Vec3(0, 0, 1), gemmi::Vec3(0, -1, 0));
const gemmi::Transform move[2] = {{r0, gemmi::Vec3(40, 5, -7)}, {r1, gemmi::Vec3(-12, 30, 8)}};
gemmi::Structure placed = model;
placed.models[0].chains.resize(1);
placed.models[0].chains.push_back(placed.models[0].chains[0]);
placed.models[0].chains[1].name = "B";
for (int k = 0; k < 2; k++)
for (gemmi::Residue &r : placed.models[0].chains[k].residues)
for (gemmi::Atom &a : r.atoms)
a.pos = gemmi::Position(move[k].apply(a.pos));
PlaceTlsGroups(model, placed);
const std::vector<TlsBody> moved = TlsBodiesFromModel(placed.models[0], *placed.meta.get_tls_groups());
// the group of chain A once on each copy, that of chain B on none; U turned with its copy
REQUIRE(moved.size() == 2);
for (int k = 0; k < 2; k++) {
REQUIRE(moved[k].atoms.size() == 30);
for (int i = 0; i < 30; i++) {
const gemmi::Position &x = model.models[0].chains[0].residues[i / 10].atoms[i % 10].pos;
const gemmi::Position &y = placed.models[0].chains[k].residues[i / 10].atoms[i % 10].pos;
CHECK(moved[k].atoms[i] == 30 * k + i);
const gemmi::SMat33<double> want = bodies[0].U(x).transformed_by(move[k].mat), got = moved[k].U(y);
CHECK(got.u11 == Catch::Approx(want.u11).margin(1e-9));
CHECK(got.u22 == Catch::Approx(want.u22).margin(1e-9));
CHECK(got.u33 == Catch::Approx(want.u33).margin(1e-9));
CHECK(got.u12 == Catch::Approx(want.u12).margin(1e-9));
CHECK(got.u13 == Catch::Approx(want.u13).margin(1e-9));
CHECK(got.u23 == Catch::Approx(want.u23).margin(1e-9));
}
}
}
TEST_CASE("RefinementADP_u_restraints_gradient", "[Refinement]") {
std::mt19937 rng(5);
std::uniform_real_distribution<double> d(-1.0, 1.0);