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);