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)