Merge branch 'rc187-mr-compact' into rc187-molstar-peaks

This commit is contained in:
leonarski_f committed 2026-11-13 08:12:36 +01:00
commit 857e91b05d
3 files changed
+138 -1

No files matched your search

@@ -2168,6 +2168,56 @@ std::vector<gemmi::Structure> SplitCopies(const gemmi::Structure &assembly, int
return body;
}
void CompactCopies(gemmi::Structure &placed, int copies) {
const gemmi::UnitCell &cell = placed.cell;
const std::vector<gemmi::Op> 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<double, 3> 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<gemmi::Op> op(copies, gemmi::Op::identity());
std::vector<gemmi::Fractional> shift(copies);
const std::vector<gemmi::Structure> 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<SubgroupSetting> 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<MRMerge> &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<gemmi::Structure>(*c.placed);
CompactCopies(*compact, c.copies);
c.placed = compact;
}
return result;
}
std::optional<MolecularReplacementResult> CarryMolecularReplacement(const MolecularReplacementResult &early,
@@ -263,6 +263,12 @@ gemmi::Structure AssembleCopies(const std::vector<gemmi::Structure> &copies);
// The copies of an assembly (AssembleCopies), its chains split evenly in order.
std::vector<gemmi::Structure> 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
+72
View File
@@ -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<double, 3> 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<gemmi::Fractional> 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);