From 62f4f24d6a8baac5a54de993fdeea64b1cc402b1 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 18:06:58 +0100 Subject: [PATCH] Refinement: move a model's TLS groups with its placed copies Molecular replacement moves every copy of the model (centred, turned, translated, symmetry image and lattice shift, rigid-body fit) and renames its chains, but carried the model's TLS metadata as read: origins in the model's own frame, T/L/S unturned, selections on the old chain names. The refinement then read U from origins tens of angstroms off, and the model's own groups lost to isotropic B by a wide margin (R-free 0.55 against 0.36 on a three-chain MR case). PlaceTlsGroups rebuilds the groups on the placed structure from the model's: each placed chain is matched to the model chain it is a rigid copy of (best superposition), and each group's origin is moved and T, L, S are turned (R X R^T) by the superposition of its atoms; whole where each of its chains was placed once, one group per copy where a chain was placed several times. Called on the MR candidates after compaction, on the carried placement, and on the validated placed model. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- .../structure_refinement/ModelValidation.cpp | 5 +- .../MolecularReplacement.cpp | 3 + .../structure_refinement/RefinementADP.cpp | 98 +++++++++++++++++++ .../structure_refinement/RefinementADP.h | 7 ++ tests/RefinementTest.cpp | 68 +++++++++++++ 5 files changed, 180 insertions(+), 1 deletion(-) diff --git a/image_analysis/structure_refinement/ModelValidation.cpp b/image_analysis/structure_refinement/ModelValidation.cpp index 202aa67be..01f50636b 100644 --- a/image_analysis/structure_refinement/ModelValidation.cpp +++ b/image_analysis/structure_refinement/ModelValidation.cpp @@ -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 &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(st); logger.Info("Model validation: R-work={:.4f} ({} refl) R-free={:.4f} ({} refl) " "[overall + anisotropic B + bulk solvent, fitted on the working set only]", diff --git a/image_analysis/structure_refinement/MolecularReplacement.cpp b/image_analysis/structure_refinement/MolecularReplacement.cpp index 8d9bf7a72..6abacdba9 100644 --- a/image_analysis/structure_refinement/MolecularReplacement.cpp +++ b/image_analysis/structure_refinement/MolecularReplacement.cpp @@ -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(*c.placed); CompactCopies(*compact, c.copies); + PlaceTlsGroups(model.structure, *compact); c.placed = compact; } return result; @@ -4211,6 +4213,7 @@ std::optional CarryMolecularReplacement(const Molecu } } auto placed = std::make_shared(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){} " diff --git a/image_analysis/structure_refinement/RefinementADP.cpp b/image_analysis/structure_refinement/RefinementADP.cpp index 810400540..09199bd49 100644 --- a/image_analysis/structure_refinement/RefinementADP.cpp +++ b/image_analysis/structure_refinement/RefinementADP.cpp @@ -2,10 +2,13 @@ // SPDX-License-Identifier: GPL-3.0-only #include "RefinementADP.h" +#include "gemmi/qcp.hpp" // superpose_positions #include #include #include +#include +#include #include @@ -317,3 +320,98 @@ std::vector TlsSegments(const std::vector> &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 *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; + const auto key = [](const gemmi::Residue &r, const gemmi::Atom &a) { + return AtomKey(r.seqid.str(), r.name, a.name, a.altloc); + }; + std::map> 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 &p, std::vector &q) { + const std::map &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 source; + for (const gemmi::Chain &ch : placed.models[0].chains) { + double best = INFINITY; + for (const auto &[s, atoms] : model_atoms) { + std::vector 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 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 copies; + std::map 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> bodies; + if (whole) + bodies.push_back(copies); + else + for (const std::string &c : copies) + bodies.push_back({c}); + for (const std::vector &body : bodies) { + gemmi::TlsGroup t = g; + t.selections.clear(); + std::vector 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(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); +} diff --git a/image_analysis/structure_refinement/RefinementADP.h b/image_analysis/structure_refinement/RefinementADP.h index 52eda2c69..2029c48dc 100644 --- a/image_analysis/structure_refinement/RefinementADP.h +++ b/image_analysis/structure_refinement/RefinementADP.h @@ -77,3 +77,10 @@ double EvaluateURestraints(const URestraints &r, const std::vector TlsSegments(const std::vector> &residue_atoms, const std::vector &pos, const std::vector &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); diff --git a/tests/RefinementTest.cpp b/tests/RefinementTest.cpp index 18f80aae3..7328b2912 100644 --- a/tests/RefinementTest.cpp +++ b/tests/RefinementTest.cpp @@ -1535,6 +1535,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 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 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 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 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 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 d(-1.0, 1.0);