From 0287e13945fc5b78f6027a3f137f5d5ad6b696ef Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 13 Nov 2026 08:11:03 +0100 Subject: [PATCH] MR: bring the placed copies together in the cell The search places each further copy wherever its translation function peaks, and every symmetry image of that position (an operator of the group plus a lattice shift) peaks alike, so the copies of a multi-copy solution came out at arbitrary images, often 50-80 A apart and in different cells, although the crystal they build is right. CompactCopies replaces each copy, in order, by the image whose centroid is nearest the mean centroid of the copies before it, and moves the whole by one lattice shift that puts the mean of the centroids in [0, 1). It runs once the decision is taken, on every candidate: within its own group the crystal is unchanged, while the check that carries the chosen copies into another group reads them as they stand, so compacting earlier would change that check's input. On a three-copy P 32 2 1 solution of an in-house cytochrome C set the copies went from 52-78 A apart to 26 A apart, each still matching a reference chain to 0.3-0.4 A C-alpha rms; the search's decisions were unchanged, and the held-out R-free before the validation's rigid body was identical (0.4331). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK --- .../MolecularReplacement.cpp | 61 +++++++++++++++- .../MolecularReplacement.h | 6 ++ tests/MolecularReplacementTest.cpp | 72 +++++++++++++++++++ 3 files changed, 138 insertions(+), 1 deletion(-) diff --git a/image_analysis/structure_refinement/MolecularReplacement.cpp b/image_analysis/structure_refinement/MolecularReplacement.cpp index 6e8b3349e..087b0e94c 100644 --- a/image_analysis/structure_refinement/MolecularReplacement.cpp +++ b/image_analysis/structure_refinement/MolecularReplacement.cpp @@ -2168,6 +2168,56 @@ std::vector SplitCopies(const gemmi::Structure &assembly, int return body; } +void CompactCopies(gemmi::Structure &placed, int copies) { + const gemmi::UnitCell &cell = placed.cell; + const std::vector ops = placed.find_spacegroup()->operations().all_ops_sorted(); + const auto apply = [](const gemmi::Op &op, const gemmi::Fractional &f, const gemmi::Fractional &shift) { + const std::array x = op.apply_to_xyz({{f.x, f.y, f.z}}); + return gemmi::Fractional(x[0] + shift.x, x[1] + shift.y, x[2] + shift.z); + }; + // Each copy in turn: the image of it - an operator and a lattice shift - whose centroid is nearest the mean + // centroid of the copies before it. + std::vector op(copies, gemmi::Op::identity()); + std::vector shift(copies); + const std::vector body = SplitCopies(placed, copies); + gemmi::Fractional sum; + for (int k = 0; k < copies; k++) { + const gemmi::Fractional c = centroid(body[k]); + gemmi::Fractional centre = c; + if (k > 0) { + const gemmi::Fractional target(sum * (1.0 / k)); + double nearest = INFINITY; + for (const gemmi::Op &o : ops) { + const gemmi::Fractional m = apply(o, c, gemmi::Fractional(0, 0, 0)); + for (int dx = -1; dx <= 1; dx++) + for (int dy = -1; dy <= 1; dy++) + for (int dz = -1; dz <= 1; dz++) { + const gemmi::Fractional s(std::round(target.x - m.x) + dx, std::round(target.y - m.y) + dy, + std::round(target.z - m.z) + dz); + const double d = cell.orthogonalize_difference(m + s - target).length(); + if (d < nearest) { + nearest = d; + op[k] = o; + shift[k] = s; + centre = m + s; + } + } + } + } + sum = sum + centre; + } + // The whole assembly then by one lattice shift, the mean of the centroids in the unit cell. + const gemmi::Fractional mean(sum * (1.0 / copies)); + const gemmi::Fractional to_cell(-std::floor(mean.x), -std::floor(mean.y), -std::floor(mean.z)); + const size_t chains = placed.models[0].chains.size(); + for (size_t i = 0; i < chains; i++) { + const size_t k = i * copies / chains; + for (gemmi::Residue &r : placed.models[0].chains[i].residues) + for (gemmi::Atom &a : r.atoms) + a.pos = cell.orthogonalize(apply(op[k], cell.fractionalize(a.pos), shift[k] + to_cell)); + } +} + std::optional SubgroupBasis(const gemmi::UnitCell &g, const gemmi::SpaceGroup &g_sg, const gemmi::UnitCell &h, const gemmi::SpaceGroup &h_sg) { constexpr double TOLERANCE = 0.01; // relative, on lengths, and on the cosines of the angles @@ -4030,7 +4080,16 @@ MolecularReplacementResult run_molecular_replacement(const RugnuxModel &input, c MolecularReplacementResult RunMolecularReplacement(const RugnuxModel &model, const std::vector &merges, Logger &logger, size_t nthreads, size_t gpu_resident_bytes) { WaitForModelWarmUp(); - return run_molecular_replacement(model, merges, logger, nthreads, gpu_resident_bytes); + MolecularReplacementResult result = run_molecular_replacement(model, merges, logger, nthreads, gpu_resident_bytes); + // Each placement's copies brought together once the decision is taken: the same crystal in its own group, but + // the copies carried into another group (carry_copies) are read as they stand, so not before. + for (MRCandidate &c : result.candidates) + if (c.placed && c.copies >= 1) { + auto compact = std::make_shared(*c.placed); + CompactCopies(*compact, c.copies); + c.placed = compact; + } + return result; } std::optional CarryMolecularReplacement(const MolecularReplacementResult &early, diff --git a/image_analysis/structure_refinement/MolecularReplacement.h b/image_analysis/structure_refinement/MolecularReplacement.h index 1ef05b244..f645099af 100644 --- a/image_analysis/structure_refinement/MolecularReplacement.h +++ b/image_analysis/structure_refinement/MolecularReplacement.h @@ -263,6 +263,12 @@ gemmi::Structure AssembleCopies(const std::vector &copies); // The copies of an assembly (AssembleCopies), its chains split evenly in order. std::vector SplitCopies(const gemmi::Structure &assembly, int copies); +// The copies of a placement brought together, the crystal unchanged: the search puts each copy wherever the +// translation function peaks, which is any of its symmetry images alike. Each copy, in order, replaced by the image +// - an operator of the group and a lattice shift - whose centroid lies nearest the mean centroid of the copies +// before it; then the whole by one lattice shift that puts the mean of their centroids in the unit cell [0, 1). +void CompactCopies(gemmi::Structure &placed, int copies); + // The setting of a subgroup's merge (cell `h`, group `h_sg`) in the cell `g` of a group `g_sg`: `basis` has h's // axes as columns, lattice vectors of g (with g's centring) in g's fractional coordinates, right-handed and // matching h's lengths and angles to 1%; `origin` is where g's origin lies in h's fractional coordinates, so diff --git a/tests/MolecularReplacementTest.cpp b/tests/MolecularReplacementTest.cpp index dd92c098d..e51635d5f 100644 --- a/tests/MolecularReplacementTest.cpp +++ b/tests/MolecularReplacementTest.cpp @@ -613,6 +613,78 @@ TEST_CASE("MolecularReplacement_ExpandIntoSubgroup", "[MolecularReplacement]") { } } +// Three copies side by side, two of them then moved to far symmetry images - the crystal unchanged, as the search +// may place them: CompactCopies brings them back side by side, in the unit cell, and |F| stays as it was. +TEST_CASE("MolecularReplacement_CompactCopies", "[MolecularReplacement]") { + const SearchModel model = synthetic_model(); + const gemmi::UnitCell cell(70, 70, 80, 90, 90, 120); + const gemmi::SpaceGroup &sg = gemmi::get_spacegroup_by_name("P 32 2 1"); + const gemmi::Fractional t1(0.30, 0.20, 0.10), t2(0.62, 0.30, 0.12), t3(0.45, 0.45, 0.30); + const gemmi::Structure side_by_side = AssembleCopies({ + PlaceSearchModel(model, axis_angle({1, 2, 3}, 50), t1, cell, sg), + PlaceSearchModel(model, axis_angle({-2, 1, 1}, 110), t2, cell, sg), + PlaceSearchModel(model, axis_angle({0, 1, -1}, 30), t3, cell, sg)}); + + // Copy 2 by -x+y,-x,z+1/3 and (1, 0, -1), copy 3 by x-y,-y,-z+1/3 and (0, -1, 2) + gemmi::Structure scattered = side_by_side; + const auto move = [&](gemmi::Chain &ch, const gemmi::Op &op, const gemmi::Fractional &s) { + for (gemmi::Residue &r : ch.residues) + for (gemmi::Atom &a : r.atoms) { + const gemmi::Fractional f = cell.fractionalize(a.pos); + const std::array x = op.apply_to_xyz({{f.x, f.y, f.z}}); + a.pos = cell.orthogonalize(gemmi::Fractional(x[0] + s.x, x[1] + s.y, x[2] + s.z)); + } + }; + move(scattered.models[0].chains[1], gemmi::parse_triplet("-x+y,-x,z+1/3"), gemmi::Fractional(1, 0, -1)); + move(scattered.models[0].chains[2], gemmi::parse_triplet("x-y,-y,-z+1/3"), gemmi::Fractional(0, -1, 2)); + + const auto centres = [&](const gemmi::Structure &st) { + std::vector out; + for (const gemmi::Chain &ch : st.models[0].chains) { + gemmi::Vec3 c; + for (const gemmi::Atom &a : ch.residues[0].atoms) + c += a.pos; + out.push_back(cell.fractionalize(gemmi::Position(c * (1.0 / ch.residues[0].atoms.size())))); + } + return out; + }; + const auto widest = [&](const gemmi::Structure &st) { + const auto c = centres(st); + double d = 0; + for (size_t i = 0; i < c.size(); i++) + for (size_t j = i + 1; j < c.size(); j++) + d = std::max(d, cell.orthogonalize_difference(c[i] - c[j]).length()); + return d; + }; + REQUIRE(widest(side_by_side) < 30.0); + REQUIRE(widest(scattered) > 60.0); + + gemmi::Structure compact = scattered; + CompactCopies(compact, 3); + CHECK(widest(compact) < 30.0); + gemmi::Fractional mean; + for (const gemmi::Fractional &c : centres(compact)) + mean = mean + c; + for (double m : {mean.x / 3, mean.y / 3, mean.z / 3}) { + CHECK(m >= 0.0); + CHECK(m < 1.0); + } + + // The same crystal: the same amplitudes at every reflection + const auto before = observed(scattered); + const auto after = observed(compact); + REQUIRE(before.size() == after.size()); + double diff = 0, sum = 0; + for (size_t i = 0; i < before.size(); i++) { + REQUIRE(before[i].h == after[i].h); + REQUIRE(before[i].k == after[i].k); + REQUIRE(before[i].l == after[i].l); + diff += std::fabs(before[i].F - after[i].F); + sum += before[i].F; + } + CHECK(diff / sum < 1e-4); +} + TEST_CASE("MolecularReplacement_MatthewsProbabilities", "[MolecularReplacement]") { // Vm 2.5 A^3/Da for one copy: one copy is by far the most probable, four impossible (Vm 0.6). const auto p = MatthewsProbabilities(2.5 * 20000, 20000);