From 0d86d0a28a71f8a0a5a1bf4560ed6277b5b36c44 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Tue, 27 Oct 2026 23:31:06 +0100 Subject: [PATCH] RESULT-CHANGING: --mode refine expands strict-NCS operators, as model validation does A deposition can hold part of the asymmetric unit with the strict-NCS operators that generate the rest (mmCIF _struct_ncs_oper code 'generate', PDB MTRIX without the given flag). Model validation and the model reference intensities expanded them; the refine-only path passed the structure as read, so it refined and scored a fraction of the crystal. On 4nwv (an icosahedral particle depositing 5 of its 75 chains) refine-only reported R-free 0.567 as given; with the expansion 0.269 as given, 0.257 after placement (phenix.model_vs_data on the same two files: R-work 0.2575, R-free 0.2590) and 0.267 refined. Models without generating operators are unchanged. expand_strict_ncs becomes the public ExpandStrictNcs (ModelValidation.h); RunRefineOnly calls it and logs the chain count it went from and to. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQqN9qRD57vnShzTC37 --- .../structure_refinement/ModelValidation.cpp | 18 +++++------- .../structure_refinement/ModelValidation.h | 7 +++++ rugnux/RugnuxRefine.cpp | 8 +++++- tests/RefineOnlyTest.cpp | 28 ++++++++++++++++++- 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/image_analysis/structure_refinement/ModelValidation.cpp b/image_analysis/structure_refinement/ModelValidation.cpp index 546b998ad..01ca73758 100644 --- a/image_analysis/structure_refinement/ModelValidation.cpp +++ b/image_analysis/structure_refinement/ModelValidation.cpp @@ -55,6 +55,11 @@ class ModelStructureFactorsGPU; #endif +void ExpandStrictNcs(gemmi::Structure &st) { + if (std::any_of(st.ncs.begin(), st.ncs.end(), [](const gemmi::NcsOp &op) { return !op.given; })) + gemmi::expand_ncs(st, gemmi::HowToNameCopiedChain::Short); +} + namespace { using Table = gemmi::IT92; @@ -218,15 +223,6 @@ void reorient_about_centroid(gemmi::Model &model, const gemmi::Mat33 &rot) { SetModelPositions(model, pos); } -// A deposition can hold only part of the asymmetric unit, with the strict-NCS operators that generate -// the rest (mmCIF _struct_ncs_oper code 'generate', PDB MTRIX without the given flag): an icosahedral -// virus deposits a few chains of the dozens in the crystal's ASU. Read as it is, the model is that -// fraction of the crystal, and every R and map made from it describes the fraction. -void expand_strict_ncs(gemmi::Structure &st) { - if (std::any_of(st.ncs.begin(), st.ncs.end(), [](const gemmi::NcsOp &op) { return !op.given; })) - gemmi::expand_ncs(st, gemmi::HowToNameCopiedChain::Short); -} - // Move a model into another cell by keeping its fractional coordinates. Correct only when the two // cells describe the same axes in the same order, which is what the probe below is there to arrange. void refractionalize_into(gemmi::Structure &st, const gemmi::UnitCell &target) { @@ -544,7 +540,7 @@ ModelValidationResult Validate(const std::vector &merged, gemmi::Structure &st = mdl.st; // the real model, the one the maps and the report describe try { st = placed != nullptr ? *placed : model.structure; - expand_strict_ncs(st); + ExpandStrictNcs(st); } catch (const std::exception &e) { result.failure_reason = fmt::format("cannot read model {}: {}", model.name, e.what()); logger.Error("Model validation: {}", result.failure_reason); @@ -2249,7 +2245,7 @@ std::vector ModelReferenceIntensities(const RugnuxModel &model gemmi::Structure st = model.structure; try { - expand_strict_ncs(st); + ExpandStrictNcs(st); } catch (const std::exception &e) { logger.Error("Model reference: cannot read model {}: {}", model.name, e.what()); return out; diff --git a/image_analysis/structure_refinement/ModelValidation.h b/image_analysis/structure_refinement/ModelValidation.h index 9551ff0f9..5bb6d3043 100644 --- a/image_analysis/structure_refinement/ModelValidation.h +++ b/image_analysis/structure_refinement/ModelValidation.h @@ -409,6 +409,13 @@ std::vector ModelReferenceIntensities(const RugnuxModel &model double d_min, Logger &logger); +// A deposition can hold only part of the asymmetric unit, with the strict-NCS operators that generate +// the rest (mmCIF _struct_ncs_oper code 'generate', PDB MTRIX without the given flag): an icosahedral +// virus deposits a few chains of the dozens in the crystal's ASU. Read as it is, the model is that +// fraction of the crystal, and every R and map made from it describes the fraction. This adds the +// generated copies; a model without such operators is left as it is. +void ExpandStrictNcs(gemmi::Structure &st); + // Run one validation of `model` against its own calculated intensities (to the model's stated // resolution, 2 A where it states none) on a background thread, and throw it away. A process's first // validation pays for what the GPU loads on first use - kernels, cuFFT plans and their sizes, on every diff --git a/rugnux/RugnuxRefine.cpp b/rugnux/RugnuxRefine.cpp index 8d897eab6..d232a8074 100644 --- a/rugnux/RugnuxRefine.cpp +++ b/rugnux/RugnuxRefine.cpp @@ -50,7 +50,13 @@ RefineOnlyResult RunRefineOnly(RugnuxReflections data, const gemmi::Structure &m if (const gemmi::SpaceGroup *msg = model.find_spacegroup(); msg != nullptr && msg->number != sg->number) logger.Warning("The model names space group {}, the data {}: refined in the data's", msg->xhm(), sg->xhm()); try { - out.refinement = RefineModel(model, refl, data.cell, *sg, out.wavelength_A, logger, settings); + gemmi::Structure st = model; + const size_t chains = st.models.empty() ? 0 : st.models[0].chains.size(); + ExpandStrictNcs(st); + if (!st.models.empty() && st.models[0].chains.size() != chains) + logger.Info("The model's strict-NCS operators generate the rest of the asymmetric unit: {} chains -> {}", + chains, st.models[0].chains.size()); + out.refinement = RefineModel(st, refl, data.cell, *sg, out.wavelength_A, logger, settings); } catch (const std::exception &e) { out.refinement = RefinementResult(); out.refinement.failure_reason = std::string("refinement failed: ") + e.what(); diff --git a/tests/RefineOnlyTest.cpp b/tests/RefineOnlyTest.cpp index c98c6e0ba..37a62d6a9 100644 --- a/tests/RefineOnlyTest.cpp +++ b/tests/RefineOnlyTest.cpp @@ -46,13 +46,16 @@ gemmi::Mtz MakeMtz(const std::vector> &columns, bool InTestSet(int h, int k, int l) { return (h * 7 + k * 13 + l * 17) % 10 == 0; } // Waters scattered over the test cell, and their own |F|^2 as the data: the model as given scores well. -void SyntheticRefineInput(gemmi::Structure &st, RugnuxReflections &data) { +// With `strict_ncs` the second half of the waters is the first half moved by a translation, and the model +// keeps only the first half with a strict-NCS operator (not given) that generates the rest. +void SyntheticRefineInput(gemmi::Structure &st, RugnuxReflections &data, bool strict_ncs = false) { const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 21 21 21"); st.cell = test_cell; st.spacegroup_hm = sg->xhm(); st.setup_cell_images(); gemmi::Model model(1); gemmi::Chain chain("A"); + const gemmi::Position ncs_shift(5.3, 7.1, 2.9); std::mt19937 rng(7); std::uniform_real_distribution frac(0.0, 1.0); for (int i = 0; i < 120; i++) { @@ -64,6 +67,8 @@ void SyntheticRefineInput(gemmi::Structure &st, RugnuxReflections &data) { a.name = "O"; a.element = gemmi::El::O; a.pos = test_cell.orthogonalize(gemmi::Fractional(frac(rng), frac(rng), frac(rng))); + if (strict_ncs && i >= 60) + a.pos = chain.residues[i - 60].atoms[0].pos + ncs_shift; a.b_iso = 15.0f; a.occ = 1.0f; res.atoms.push_back(a); @@ -99,6 +104,13 @@ void SyntheticRefineInput(gemmi::Structure &st, RugnuxReflections &data) { m.sigma = static_cast(0.02 * fc * fc + 1.0); data.reflections.push_back(m); } + if (strict_ncs) { + st.models[0].chains[0].residues.resize(60); + gemmi::Transform shift; + shift.vec = ncs_shift; + st.ncs.push_back({"1", true, gemmi::Transform()}); + st.ncs.push_back({"2", false, shift}); + } } } // namespace @@ -209,6 +221,20 @@ TEST_CASE("RunRefineOnly scores a model against intensities, with amplitudes and CHECK(text.find("REFINE_FRENCH_WILSON= TRUE") != std::string::npos); } +TEST_CASE("RunRefineOnly scores a model with strict-NCS operators as the expanded model", "[refine_only]") { + gemmi::Structure st; + RugnuxReflections data; + SyntheticRefineInput(st, data, true); + RefinementSettings settings; + settings.score_only = true; + settings.gpu = false; + settings.nthreads = 2; + Logger logger("RefineOnlyTest"); + const RefineOnlyResult r = RunRefineOnly(data, st, 2.5, settings, logger); + REQUIRE(r.refinement.ok); + CHECK(r.refinement.r_work_start < 0.15); +} + TEST_CASE("RunRefineOnly reports a refinement that throws as not performed, with the reason", "[refine_only]") { // The data's cell, a hundred metres on a side, makes the bulk-solvent mask's grid too large to allocate. gemmi::Structure st;