From 058ba80fc73860f2ef28c0aee0d00cd613e85500 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 14 Nov 2026 16:28:08 +0100 Subject: [PATCH] Refinement: B ceiling from the data's Wilson B, weights balanced at the refined level Two start-conditioned defects of the first round, found on a flat-B start of the crystal model and on a placed prediction (pLDDT-derived B of ~6 A^2), both taken from the B column as given: - The ceiling of B was 2 x b_max of the model, i.e. 100 A^2 for a flat or pLDDT B column, and it bit through the TLS tensors (BoundedB scales them down to it) long before atoms reached it: on the flat start one round gave R-free 0.4040 at ceiling 100, 0.3989 at 140, 0.3870 at 200, 0.3848 at 300, while the refined max B was 123. The ceiling is now set from at least 2.5 x the data's Wilson B as well (WilsonB, fitted against the model's own atoms from 4 A), which leaves a deposited model's ceiling where it was. - The ADP weight was balanced before the overall B of the scale was moved into the atoms, which every trial does: the B similarity's gradient goes as 1/B, so a column at 6 A^2 balanced a weight ~100 x too stiff (2350 vs ~20 once refined). The move is now made once after the placement, before the weights are balanced. Together these give in round 1 most of what the second and third rounds were adding: flat start 0.4040 -> 0.3822 (base with rounds 0.3798 at 3 x the time), placed prediction 0.4162 -> 0.4038 (0.3982 with rounds), crystal model with its own B unchanged (0.3790). 13 AlphaFold starts from images with the rounds as they are: six lower by 0.005-0.021, none higher beyond 0.0025, median -0.002, refinement time unchanged. Measured and not adopted: elastic-network and packing-density B starts (within noise once the ceiling is right), and a coarse-to-fine B solve along chain / network-mode / residue shifts before the per-atom one (same optimum to 0.0002 at +25 % evaluations). Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK --- .../structure_refinement/Refinement.cpp | 64 ++++++++++++++++++- .../structure_refinement/Refinement.h | 1 + 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index 8997505b9..bef8159eb 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -53,6 +53,8 @@ namespace { constexpr double B_FLOOR = 2.0; // A^2: no atom's B goes below it // nor above twice the model's largest B, within these bounds (the GPU's gather is reserved for it) constexpr double B_CEILING_MIN = 100.0, B_CEILING_MAX = 300.0; +// The B the ceiling is set from is at least this times the data's Wilson B (WilsonB). +constexpr double B_CEILING_WILSON = 2.5; // The ceiling of B for a model whose highest B is `b_max`: twice it, within the bounds - the bounds lifted for a // model that comes with B above them (a structure refined at 4-5 A, B 400-600 A^2), which the ceiling would // otherwise flatten (the gather is reserved for it). @@ -2288,6 +2290,54 @@ double RefinementDMin(double max_inv_d2) { return 0.999 / std::sqrt(max_inv_d2); } +// The Wilson B of the data against the model's atoms: the slope of ln(<|F|^2> / sum_j f_j^2) in s^2 / 2 over shells +// from 4 A to the finest amplitude; 0 where the data do not reach 4 A. The B factors refine around it whatever the +// model's say, so the ceiling of B is set from it as well as from them (BCeiling): a model may come with B factors +// that say nothing of where they will refine - a flat B, a prediction's pLDDT. +// Following Wilson (1949) Acta Cryst. 2, 318-321 +double WilsonB(const std::vector &merged, const std::vector &atoms) { + constexpr int SHELLS = 10; + constexpr double S2_MIN = 1.0 / 16.0; // 4 A + const double s2_max = MaxInvD2(merged); + if (s2_max <= S2_MIN) + return 0; + std::map, int> composition; + for (const gemmi::Atom *a : atoms) + if (!a->is_hydrogen()) + composition[{static_cast(a->element.elem), a->charge}]++; + std::vector f2_sum(SHELLS, 0.0), s2_sum(SHELLS, 0.0); + std::vector n(SHELLS, 0); + for (const MergedReflection &r : merged) { + if (!(std::isfinite(r.F) && r.d > 0)) + continue; + const double s2 = 1.0 / (double(r.d) * r.d); + if (s2 < S2_MIN) + continue; + const int k = std::min(SHELLS - 1, static_cast((s2 - S2_MIN) / (s2_max - S2_MIN) * SHELLS)); + f2_sum[k] += double(r.F) * r.F; + s2_sum[k] += s2; + n[k]++; + } + // the line y = ln k - B x through the shells (x = s^2 / 2), each weighted by its reflections + double sw = 0, sx = 0, sy = 0, sxx = 0, sxy = 0; + for (int k = 0; k < SHELLS; k++) { + if (n[k] < 20) + continue; + const double s2 = s2_sum[k] / n[k]; + double f2 = 0; + for (const auto &[ion, count] : composition) + f2 += count * std::pow(IonFormFactor(static_cast(ion.first), ion.second).calculate_sf(s2 / 4.0), 2); + const double x = s2 / 2.0, y = std::log(f2_sum[k] / n[k] / f2), w = n[k]; + sw += w; + sx += w * x; + sy += w * y; + sxx += w * x * x; + sxy += w * x * y; + } + const double det = sw * sxx - sx * sx; + return det > 0 ? std::max(0.0, -(sw * sxy - sx * sy) / det) : 0.0; +} + // One refinement of a model, with the solvent build in it where settings.solvent asks for it. // `twin_laws`: against the data as twinned by them (one domain each beside the model's own), empty for untwinned; // `null_first`: the first of them the null (TwinNull) replaces the mates of. @@ -2326,7 +2376,8 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vectorb_iso + gemmi::u_to_b() * body.U(ctx.atoms[i]->pos).trace() / 3.0); - ctx.b_ceiling = BCeiling(b_max); + result.wilson_b = WilsonB(merged, ctx.atoms); + ctx.b_ceiling = BCeiling(std::max(b_max, B_CEILING_WILSON * result.wilson_b)); if (ctx.atoms.empty()) { result.failure_reason = "the model has no atoms"; return result; @@ -2432,14 +2483,15 @@ RefinementResult RefineOnce(const gemmi::Structure &placed, const std::vector> solvent; // the solvent audit (settings.solvent) + double wilson_b = 0; // the data's Wilson B against the model's atoms (A^2), 0 where the data do not reach 4 A double b_ceiling = 0; // the highest B the refinement allowed SolventBuildResult solvent_build; // the solvent build's rounds (settings.solvent)