Live MR: carry the pre-pass placement across the lattice's indexing ambiguity

In a live run the molecular replacement of a prediction starts on the geometry pre-pass's merge
and its placement is carried to the final merge where it holds there. The two passes index the
sweep each on its own, so on a lattice with an indexing ambiguity the final merge can come out in
the other hand, the carried placement then fits at R-free ~0.6 and the whole search is run again
after the space-group step, on the critical path of the tail.

CarryMolecularReplacement now picks the ambiguity operator (identity included) that takes the
searched merge's reflections onto this merge's by the correlation of their intensities - a pass
over the reflections, milliseconds - and turns the copies by it before the rigid body.

Measured on a P3_221 rotation set with three copies and tNCS (--simulate-live 10 --fast-live,
1800 images): the pre-pass placement, turned by -h,-k,l, holds at R-free 0.417 (the re-search
reached 0.413); MR step 0.91-1.40 s -> 0.18-0.26 s. Refined R-free 0.4175 against 0.4255 from the
re-searched placement (same merge, same refinement). New test
MolecularReplacement_CarryAcrossIndexingAmbiguity (fails on the old code at R-free 0.51).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
This commit is contained in:
leonarski_fandClaude Opus 5.5 committed 2026-11-14 00:03:24 +01:00
1 parent 724430022f
commit a22d8b9b56
2 files changed
+107 -6

No files matched your search

@@ -26,6 +26,8 @@
#include <gemmi/dencalc.hpp> // DensityCalculator
#include <gemmi/it92.hpp> // IT92 x-ray form factors
#include <gemmi/scaling.hpp> // Scaling (bulk solvent + anisotropic B)
#include <gemmi/twin.hpp> // find_twin_laws
#include <gemmi/asudata.hpp> // 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<int64_t, float> intensity;
const auto key = [&](const gemmi::Miller &h) {
const gemmi::Miller a = asu.to_asu(h, gops).first;
return (static_cast<int64_t>(a[0] + 512) << 20) | (static_cast<int64_t>(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<gemmi::Op> 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<MolecularReplacementResult> CarryMolecularReplacement(const MolecularReplacementResult &early,
const MRMerge &searched, const MRMerge &merge,
Logger &logger, size_t nthreads,
@@ -4117,12 +4159,18 @@ std::optional<MolecularReplacementResult> CarryMolecularReplacement(const Molecu
if (r.d > 0)
data_d_min = std::min<double>(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<gemmi::Structure> body = SplitCopies(*c.placed, c.copies);
for (gemmi::Structure &b : body) {
std::vector<gemmi::Position> 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<double, 3> 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<MolecularReplacementResult> CarryMolecularReplacement(const Molecu
auto placed = std::make_shared<gemmi::Structure>(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;
+51
View File
@@ -13,6 +13,7 @@
#include <gemmi/dencalc.hpp>
#include <gemmi/it92.hpp>
#include <gemmi/asudata.hpp>
#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<MergedReflection> 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<gemmi::Position> pos = ModelPositions(there.models[0]);
for (gemmi::Position &x : pos) {
const gemmi::Fractional f = cell.fractionalize(x);
const std::array<double, 3> 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<gemmi::Structure>(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)