diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index cb1e3406e..7e8c81502 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -3332,6 +3332,57 @@ double FinalRWork(const RefinementResult &r) { return r.restrained ? r.r_work : r.r_work_placement; } +// The refinement run again from the model it ended with, while that lowers R-free by more than the tie, up to +// REFINEMENT_ROUNDS in all. One refinement chooses its weights on the B factors as given and its displacement model +// only after them, so where the displacements change much - a placed prediction, whose B factors are its pLDDT - the +// weights were chosen for B factors it no longer has: on a placed prediction of three copies the second round chose +// an eight times lower X-ray weight, R-work and R-free both fell and R-free went 0.418 -> 0.405 (with the solvent +// build 0.427 -> 0.409 -> 0.401), where REFMAC5 reached 0.41-0.43 from the same model. The first round's view of +// the model as given is kept; the solvent build is the first round's. +constexpr int REFINEMENT_ROUNDS = 3; + +RefinementResult RefineRounds(const gemmi::Structure &placed, const std::vector &merged, + const gemmi::UnitCell &cell, const gemmi::SpaceGroup &sg, double wavelength_A, + Logger &logger, const RefinementSettings &settings, + const std::vector &twin_laws = {}, size_t null_first = 0, + CardHeld *held = nullptr) { + RefinementResult result = RefineOnce(placed, merged, cell, sg, wavelength_A, logger, settings, twin_laws, + null_first, held); + RefinementSettings again = settings; + again.solvent = false; + for (int round = 2; round <= REFINEMENT_ROUNDS; round++) { + if (!result.ok || !result.restrained || result.as_given || result.cancelled || settings.score_only || + result.model == nullptr) + break; + logger.Info("Refinement, round {}: from the model the round before ended with (R-free {:.4f})", round, + result.r_free); + RefinementResult next = RefineOnce(*result.model, merged, cell, sg, wavelength_A, logger, again, twin_laws, + null_first); + if (!next.ok || !next.restrained || next.as_given || !(next.r_free < result.r_free - R_FREE_TIE)) { + logger.Info("Refinement, round {}: not kept (R-free {:.4f})", round, FinalRFree(next)); + result.seconds += next.seconds; + break; + } + next.seconds += result.seconds; + next.r_work_start = result.r_work_start; + next.r_free_start = result.r_free_start; + next.score_start = result.score_start; + next.adp_input = result.adp_input; + next.adp_input_aniso_atoms = result.adp_input_aniso_atoms; + next.rigid_bodies = result.rigid_bodies; + next.group_b = result.group_b; + next.r_work_placement = result.r_work_placement; + next.r_free_placement = result.r_free_placement; + next.geometry_start = result.geometry_start; + next.ncs_rmsd_start = result.ncs_rmsd_start; + next.solvent_build = result.solvent_build; + if (next.adp_model == "the model's U held") // the round before's displacement model, refined on + next.adp_model = result.adp_model; + result = std::move(next); + } + return result; +} + // The twin hypotheses beside the untwinned refinement `result`, as a ladder. First one law: the placed model refined // again, solvent build and displacement model included, against the data as twinned by a law of the lattice, and the // twinned refinement taken (into `result`) where the law beats its own null (TwinNull) on the free reflections by @@ -3407,7 +3458,7 @@ void DecideTwin(const gemmi::Structure &placed, const std::vector kept{law}; @@ -3590,7 +3641,7 @@ RefinementResult RefineModel(const gemmi::Structure &placed, const std::vector