diff --git a/image_analysis/structure_refinement/MolecularReplacement.cpp b/image_analysis/structure_refinement/MolecularReplacement.cpp index 087b0e94c..8d9bf7a72 100644 --- a/image_analysis/structure_refinement/MolecularReplacement.cpp +++ b/image_analysis/structure_refinement/MolecularReplacement.cpp @@ -26,6 +26,8 @@ #include // DensityCalculator #include // IT92 x-ray form factors #include // Scaling (bulk solvent + anisotropic B) +#include // find_twin_laws +#include // ReciprocalAsu #include "../../common/JFJochMath.h" // PI #include "../../common/Logger.h" @@ -4092,6 +4094,46 @@ MolecularReplacementResult RunMolecularReplacement(const RugnuxModel &model, con return result; } +// The operator of the lattice's indexing ambiguity (the identity among them) that takes the reflections of `searched` +// onto those of `merge`, by the correlation of their intensities: the two passes of a run index the sweep each on +// its own, and a lattice with an ambiguity can come out in either hand of it. +static gemmi::Op AmbiguityBetween(const MRMerge &searched, const MRMerge &merge, const gemmi::SpaceGroup &sg) { + const gemmi::GroupOps gops = sg.operations(); + const gemmi::ReciprocalAsu asu(&sg); + std::unordered_map intensity; + const auto key = [&](const gemmi::Miller &h) { + const gemmi::Miller a = asu.to_asu(h, gops).first; + return (static_cast(a[0] + 512) << 20) | (static_cast(a[1] + 512) << 10) | (a[2] + 512); + }; + for (const MergedReflection &r : *merge.merged) + if (std::isfinite(r.I)) + intensity[key({{r.h, r.k, r.l}})] = r.I; + gemmi::Op best = gemmi::Op::identity(); + double best_cc = -2.0; + std::vector ops{gemmi::Op::identity()}; + for (const gemmi::Op &op : gemmi::find_twin_laws(gemmi::UnitCell(merge.cell), &sg, 3.0, false)) + ops.push_back(op); + for (const gemmi::Op &op : ops) { + double n = 0, sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0; + for (const MergedReflection &r : *searched.merged) { + if (!std::isfinite(r.I)) + continue; + const auto it = intensity.find(key(op.apply_to_hkl({{r.h, r.k, r.l}}))); + if (it == intensity.end()) + continue; + n += 1; + sx += r.I; sy += it->second; + sxx += r.I * r.I; syy += it->second * it->second; sxy += r.I * it->second; + } + const double cc = n > 1 ? (sxy - sx * sy / n) / std::sqrt((sxx - sx * sx / n) * (syy - sy * sy / n)) : -1.0; + if (cc > best_cc) { + best_cc = cc; + best = op; + } + } + return best; +} + std::optional CarryMolecularReplacement(const MolecularReplacementResult &early, const MRMerge &searched, const MRMerge &merge, Logger &logger, size_t nthreads, @@ -4117,12 +4159,18 @@ std::optional CarryMolecularReplacement(const Molecu if (r.d > 0) data_d_min = std::min(data_d_min, r.d); const double d_min = std::max(SCORE_D_MIN, data_d_min); - // Every copy at its fractional position in this cell. + // Every copy at its fractional position in this cell, turned by the indexing ambiguity between the two merges: + // a reflection h of `searched` is op(h) here, so a position x there is op^-1 x here. + const gemmi::Op op = AmbiguityBetween(searched, merge, sg); + const gemmi::Op to_here = op.inverse(); std::vector body = SplitCopies(*c.placed, c.copies); for (gemmi::Structure &b : body) { std::vector pos = ModelPositions(b.models[0]); - for (gemmi::Position &x : pos) - x = cell.orthogonalize(b.cell.fractionalize(x)); + for (gemmi::Position &x : pos) { + const gemmi::Fractional f = b.cell.fractionalize(x); + const std::array t = to_here.apply_to_xyz({f.x, f.y, f.z}); + x = cell.orthogonalize(gemmi::Fractional(t[0], t[1], t[2])); + } SetModelPositions(b.models[0], pos); b.cell = cell; b.spacegroup_hm = sg.xhm(); @@ -4165,9 +4213,11 @@ std::optional CarryMolecularReplacement(const Molecu auto placed = std::make_shared(AssembleCopies(body)); 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) refined against " - "this merge: R-work {:.3f} R-free {:.3f} ({:.2f} s) - {}", searched.name, - early.decision, sg.xhm(), c.copies, c.r_free, score.r_work, score.r_free, + logger.Info("Molecular replacement: the search on the {} ({}): its placement in {} ({} copies, R-free {:.3f} there){} " + "refined against this merge: R-work {:.3f} R-free {:.3f} ({:.2f} s) - {}", searched.name, + early.decision, sg.xhm(), c.copies, c.r_free, + op == gemmi::Op::identity() ? std::string() : ", indexed there as " + op.as_hkl().triplet('h') + " here,", + score.r_work, score.r_free, seconds_since(t0), holds ? "it holds, not searched again" : "it does not hold, searched again"); if (!holds) return std::nullopt; diff --git a/tests/MolecularReplacementTest.cpp b/tests/MolecularReplacementTest.cpp index e51635d5f..a2deea5e0 100644 --- a/tests/MolecularReplacementTest.cpp +++ b/tests/MolecularReplacementTest.cpp @@ -13,6 +13,7 @@ #include #include +#include #include "../image_analysis/structure_refinement/ModelFFT.h" #include "../image_analysis/structure_refinement/MolecularReplacement.h" @@ -817,6 +818,56 @@ TEST_CASE("MolecularReplacement_CarryToAnotherMerge", "[MolecularReplacement]") CHECK_FALSE(CarryMolecularReplacement(early, searched, {"MERGE", &merged, uc, other}, logger, 4)); } +// The two merges of one crystal in the two hands of its indexing ambiguity (P 41: k,h,-l), as two passes that index +// the sweep each on its own can give: the placement found on the one is turned onto the other (here up to a symmetry operator of the group) and +// holds there. +TEST_CASE("MolecularReplacement_CarryAcrossIndexingAmbiguity", "[MolecularReplacement]") { + const SearchModel model = synthetic_model(); + const gemmi::SpaceGroup &sg = gemmi::get_spacegroup_by_name("P 41"); + const gemmi::UnitCell cell(60, 60, 70, 90, 90, 90); + const gemmi::Structure truth = PlaceSearchModel(model, axis_angle({1, 2, 3}, 50), gemmi::Fractional(0.17, 0.31, 0.09), + cell, sg); + auto merged = observed(truth); + for (size_t i = 0; i < merged.size(); i++) + merged[i].rfree_flag = i % 10 == 0; + // The other hand: every reflection h of this merge is op(h) there, and the model placed there is op^-1 x. + const gemmi::Op op = gemmi::parse_triplet("y,x,-z"); + const gemmi::GroupOps gops = sg.operations(); + const gemmi::ReciprocalAsu asu(&sg); + std::vector other_hand = merged; + for (MergedReflection &r : other_hand) { + const gemmi::Miller h = asu.to_asu(op.apply_to_hkl({{r.h, r.k, r.l}}), gops).first; + r.h = h[0]; r.k = h[1]; r.l = h[2]; + } + gemmi::Structure there = truth; + std::vector pos = ModelPositions(there.models[0]); + for (gemmi::Position &x : pos) { + const gemmi::Fractional f = cell.fractionalize(x); + const std::array t = op.inverse().apply_to_xyz({f.x, f.y, f.z}); + x = cell.orthogonalize(gemmi::Fractional(t[0], t[1], t[2])); + } + SetModelPositions(there.models[0], pos); + + MolecularReplacementResult early; + early.ok = true; + early.chosen = 0; + early.decision = "AGREES"; + MRCandidate c; + c.space_group = sg; + c.copies = 1; + c.r_free = 0.30; + c.placed = std::make_shared(there); + early.candidates.push_back(c); + + const UnitCell uc{60, 60, 70, 90, 90, 90}; + Logger logger("MolecularReplacement_CarryAcrossIndexingAmbiguity"); + const auto carried = CarryMolecularReplacement(early, {"first merge", &other_hand, uc, sg}, + {"MERGE", &merged, uc, sg}, logger, 4); + // the placement as it came, untouched by the ambiguity, fits this merge at R-free 0.5 and more + REQUIRE(carried); + CHECK(carried->candidates[0].r_free < 0.2); +} + #ifdef JFJOCH_USE_CUDA TEST_CASE("MolecularReplacement_GPUMatchesCPU", "[MolecularReplacement]") { if (get_gpu_count() == 0)