From 60b7fb86fa952244884b49e0f7021732f2a7f8dd Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sun, 4 Oct 2026 21:42:39 +0200 Subject: [PATCH] SearchSpaceGroup: glide groups without a centre of symmetry; the centre decided by absences, a default, or the intensity distribution Non-Sohncke candidates were enumerated only when their PROPER rotations equalled the measured point group's, which only centrosymmetric groups satisfy. Groups without a centre (Pc, Pna2_1, I-42d, I4_1md, Fdd2, P-42_1c, I-43d) were never candidates: where a centrosymmetric group shares their absences rugnux wrote it silently (Pc -> P2/c), and where none does the dead glide zone was dropped and a Sohncke subgroup written (KDP-type I-42d data -> I4_122). - Enumerate non-Sohncke groups by Laue class. The Sohncke-signature dedup, CellHostsRotations and the glide-evidence bar are unchanged. Groups with identical absences in the same Laue class are scored once and carried as `same_absences`; a selected candidate brings them into `alternatives`. - Convention for which twin is written: the centrosymmetric one where it exists (missed centres are the common error, Baur & Kassner 1992), otherwise the lowest-numbered (I4_1md over I-42d). - The centrosymmetric default gives way only when, on the general reflections of the Laue class of the final merge, <|L|>, <|E^2-1|> and N(0.1), each calibrated against acentric/centric intensities simulated with the reflections' own sigmas (fixed-seed mt19937_64, own deviate transforms), read acentric (L f <= 0.3, others <= 0.5), every f lies in [-0.3, 1.3], the always-centric control reads centric (f >= 0.5), and the lattice excludes twinning (gemmi Le Page metric admits no rotation beyond the Laue class, no TWIN_DOMAIN leftover lattice). Then the group is switched and re-merged. - Report: SPACE_GROUP_CENTRE (IMPLIED_BY_ABSENCES / ABSENT_BY_ABSENCES / NOT_DETERMINED / ABSENT_BY_STATISTICS), CENTRE_TWINNING_EXCLUDED, CENTRE_STATISTICS_* on every searched run, and prose for the absence-equivalent alternatives. Battery (targeted): all 14 small-molecule sets vs 58a4bf (smt-all, kdp-all): every group unchanged except kdp_x10sa_20keV I 41 2 2 -> I 41 m d with I -4 2 d as the alternative; dnba stays C 1 2/c 1, now NOT_DETERMINED with C 1 c 1 listed (statistics read centric, f 0.96-1.24). The P2_1/c, Pbca and Ia-3d sets read IMPLIED_BY_ABSENCES with no alternative. Protein panel (19 sets, twinned panel + controls, run glide-prot): every space group equals the rc174-all base (3r6o re-run with the base binary: I 41 2 2 both); no SPACE_GROUP_CENTRE on any. Twinned proteins read the centre statistics NOT_APPLICABLE (L f -0.3 to -1.7) or ACENTRIC. Private subset (8 sets): 8/8 groups unchanged vs 58a4bf. The glide-zone evidence scan over battery 3 (every non-Sohncke group of each run's Laue class, centrosymmetric or not) peaks at 0.69 nats/reflection on proteins against the 2.0 bar. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01K5K8jvPPbmCrbqnWkddTuB --- docs/ACKNOWLEDGEMENT.md | 16 ++ docs/CHANGELOG.md | 1 + docs/CPU_DATA_ANALYSIS_DECISIONS.md | 2 +- docs/RUGNUX_REPORT.md | 26 ++ image_analysis/scale_merge/CMakeLists.txt | 2 + .../scale_merge/CentreOfSymmetry.cpp | 259 ++++++++++++++++++ image_analysis/scale_merge/CentreOfSymmetry.h | 54 ++++ .../scale_merge/SearchSpaceGroup.cpp | 126 ++++++--- image_analysis/scale_merge/SearchSpaceGroup.h | 16 +- rugnux/ResultReport.cpp | 60 ++++ rugnux/Rugnux.cpp | 70 ++++- rugnux/Rugnux.h | 11 + tests/SearchSpaceGroupTest.cpp | 126 +++++++++ 13 files changed, 727 insertions(+), 42 deletions(-) create mode 100644 image_analysis/scale_merge/CentreOfSymmetry.cpp create mode 100644 image_analysis/scale_merge/CentreOfSymmetry.h diff --git a/docs/ACKNOWLEDGEMENT.md b/docs/ACKNOWLEDGEMENT.md index cfe998a05..1a76522e7 100644 --- a/docs/ACKNOWLEDGEMENT.md +++ b/docs/ACKNOWLEDGEMENT.md @@ -145,6 +145,22 @@ J. Agirre, M. Atanasova, H. Bagdonas et al., "The CCP4 suite: integrative softwa crystallography" (2023), Acta Cryst. D79, 449-461 [doi:10.1107/S2059798323003595](https://doi.org/10.1107/S2059798323003595). +**Baur and Kassner** — the convention for which of two absence-equivalent glide groups the space-group +search writes (P2/c over Pc, C2/c over Cc): the centrosymmetric one, because a missed inversion centre is +the common error among published small-molecule space-group assignments. W. H. Baur and D. Kassner, +"The perils of Cc: comparing the frequencies of falsely assigned space groups with their general +population" (1992), Acta Cryst. B48, 356-369 +[doi:10.1107/S0108768191014726](https://doi.org/10.1107/S0108768191014726). + +**The centre-of-symmetry statistics** are Wilson's and Howells, Phillips & Rogers's: the intensity +distributions of acentric and centric structures and the cumulative N(z) test built on them, read here +on the general reflections of the Laue class beside <|E^2-1|> and Padilla and Yeates's L test (taken at +its centric value, 2/pi). A. J. C. Wilson, "The probability distribution of X-ray intensities" (1949), +Acta Cryst. 2, 318-321 [doi:10.1107/S0365110X49000813](https://doi.org/10.1107/S0365110X49000813); +E. R. Howells, D. C. Phillips and D. Rogers, "The probability distribution of X-ray intensities. II. +Experimental investigation and the X-ray detection of centres of symmetry" (1950), Acta Cryst. 3, +210-214 [doi:10.1107/S0365110X50000513](https://doi.org/10.1107/S0365110X50000513). + **The twinning L test** is Padilla and Yeates's: pairing each acentric reflection with a symmetry-independent neighbour and reading the first and second moments of L = (I1−I2)/(I1+I2) against their untwinned and perfect-twin values. J. E. Padilla and diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a0229bed3..89da0dd74 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,6 +9,7 @@ * Rugnux: French-Wilson amplitudes (`F`/`SIGF`) use an anisotropic Wilson prior from the fitted anisotropy tensor, as ctruncate does; intensities are unchanged. * Rugnux claims a screw axis from a short axial row of a few weak reflections, so such crystals (e.g. P2_1 with a ~30 A unique axis) are no longer written without the screw. * Rugnux keeps the screw axes it found when a higher point group is adopted after the twin-law check, instead of writing the group without screws (e.g. P 4 2 2 for P4_2 2_1 2). +* Rugnux names glide planes in groups without a centre of symmetry (e.g. I-42d, Fdd2), reports a group with the same absences (C2/c and Cc) as an alternative, and states how the centre was decided (`SPACE_GROUP_CENTRE`, `CENTRE_STATISTICS_*`). ### 1.0.0-rc.173 diff --git a/docs/CPU_DATA_ANALYSIS_DECISIONS.md b/docs/CPU_DATA_ANALYSIS_DECISIONS.md index 3bafab0d2..418fdcad3 100644 --- a/docs/CPU_DATA_ANALYSIS_DECISIONS.md +++ b/docs/CPU_DATA_ANALYSIS_DECISIONS.md @@ -32,7 +32,7 @@ The candidates are enumerated in **every setting the refined cell can host**, no Several space groups may be indistinguishable on the measured data. Where they are, the search scores them identically and **all of them are named** in the result rather than one being reported as the answer: some are enantiomorph pairs, which merged intensities cannot distinguish in principle, others differ only by a screw condition that the centering condition already implies, so the screw has no observable signature at all, and others differ only on a zone the sweep never measured. Equality is keyed on the measured absence evidence, not on a count of predicted absences — two settings of one point group routinely predict a different *number* of absences on an unmeasured zone, which argues nothing either way. Among the tied candidates, where at least one axial zone *was* judged, the representative is the one claiming a screw on the rows this merge holds no control class for — a stated prior, not a measurement: a crystal that has already shown one screw is the group carrying the rest an order of magnitude more often than not, and a sweep short of half a turn routinely records one axial row and not the next. Centring-driven axial conditions do not count towards the claim, so a centred group and its screw partner still tie. What remains tied is represented by the lowest space-group number, which is a convention and not a measurement. -**Glide planes** are detected the same way screws are, from the zone they extinguish: a glide removes half of a whole zone ($h0l$ with $h+l$ odd for an $n$ glide perpendicular to $\mathbf{b}$, and so on), and the extinguished class is scored against the rest of its own plane, as POINTLESS scores zonal absences. The statistic is per reflection rather than the zone's sum — a plane holds hundreds to thousands of reflections where an axial row holds tens, so a summed statistic reaches enormous values on a class that is merely a few times weak, where per reflection the false and the genuine zones separate cleanly — and a zone that cannot be measured refuses its candidate rather than abstaining, because a glide is an extra claim on top of a group that already fits without it. Candidates whose glides share a plane and differ only in the translation (the $c$ and the $n$ of the same plane) are one zone, keyed by the rotation part of the improper operator, not two scored twice. A Sohncke group has no improper operator at all, so on chiral data the zone loop never runs and every Sohncke candidate scores exactly what it scored before. Both readings are reported on every searched run — `SPACE_GROUP_NAME=` carries the glide where one was found, and `SOHNCKE_SPACE_GROUP=` the best group without — because a crystal of chiral molecules cannot have a glide plane, and a reader who knows the sample is a protein must be able to read the Sohncke answer without processing the images again. A **centre of symmetry** is never claimed: a candidate is enumerated only where its absences differ from a Sohncke candidate's, and an inversion centre predicts exactly what its Sohncke subgroup predicts — it cannot be claimed from intensities, and this does not claim it. +**Glide planes** are detected the same way screws are, from the zone they extinguish: a glide removes half of a whole zone ($h0l$ with $h+l$ odd for an $n$ glide perpendicular to $\mathbf{b}$, and so on), and the extinguished class is scored against the rest of its own plane, as POINTLESS scores zonal absences. The statistic is per reflection rather than the zone's sum — a plane holds hundreds to thousands of reflections where an axial row holds tens, so a summed statistic reaches enormous values on a class that is merely a few times weak, where per reflection the false and the genuine zones separate cleanly — and a zone that cannot be measured refuses its candidate rather than abstaining, because a glide is an extra claim on top of a group that already fits without it. Candidates whose glides share a plane and differ only in the translation (the $c$ and the $n$ of the same plane) are one zone, keyed by the rotation part of the improper operator, not two scored twice. A Sohncke group has no improper operator at all, so on chiral data the zone loop never runs and every Sohncke candidate scores exactly what it scored before. Both readings are reported on every searched run — `SPACE_GROUP_NAME=` carries the glide where one was found, and `SOHNCKE_SPACE_GROUP=` the best group without — because a crystal of chiral molecules cannot have a glide plane, and a reader who knows the sample is a protein must be able to read the Sohncke answer without processing the images again. Non-Sohncke candidates are enumerated by **Laue class**, so the groups without a centre of symmetry (Pc, Pna2_1, I-42d, Fdd2) are candidates as well as the centrosymmetric ones; a candidate is enumerated only where its absences differ from a Sohncke candidate's, because an inversion centre or a mirror alone predicts exactly what the Sohncke subgroup predicts. A **centre of symmetry** is then settled by the absences where they can settle it: no group without a centre predicts the absences of P2_1/c, Pbca or Ia-3d, and no centrosymmetric group those of I-42d or Fdd2. Where two non-Sohncke groups predict the same absences (C2/c and Cc, Pnma and Pna2_1, I4_1md and I-42d) they are reported as alternatives these data cannot separate (`SPACE_GROUP_CENTRE=NOT_DETERMINED`), and the centrosymmetric one is written by default — a missed centre is the common error in small-molecule space-group assignment (Baur & Kassner 1992). That default gives way only when the intensity distribution of the general reflections reads acentric on <|L|>, <|E²-1|> and N(0.1), each calibrated against acentric and centric intensities simulated with the reflections' own sigmas, every reading lies in the range a Wilson population can produce, the reflections centric in both groups read centric, and the lattice excludes twinning (the metric admits no rotation beyond the Laue class and no twin-domain lattice was found), because a twinned centrosymmetric crystal reads exactly like an untwinned acentric one. Between two groups without a centre, the lowest-numbered is written. The Lorentz factor $\zeta$ (§8.3) governs how well a reflection can be measured, so when the spindle lies in a plane of the lattice, an operator permuting the two in-plane axes samples a different mixture of measurement qualities than one that only flips signs. The search is therefore run a second time on a merge of only the well-measured observations (`--search-min-zeta`, rotation default 0.85), both answers are reported, and **where they disagree the merge of all the observations decides**. The filter discards 40–80 % of the observations, which can starve an operator correlation the full merge confirms and can equally leave an operator confirmed that the full merge refuses, so the decision — the point group as well as the absences, which live in the weak reflections the filter removes — rests on the arm with every observation behind it. A tie (same order, different symmetry) is reported with both candidates named, for trying in molecular replacement. diff --git a/docs/RUGNUX_REPORT.md b/docs/RUGNUX_REPORT.md index 1ef530cad..84596d33b 100644 --- a/docs/RUGNUX_REPORT.md +++ b/docs/RUGNUX_REPORT.md @@ -250,6 +250,32 @@ inversion centre, so a reader who knows their sample is a protein reads this key run. Where no glide was found the two keys read the same, deliberately: greppability is the point, and a key that appears only sometimes has to be tested for before it can be read. +`SPACE_GROUP_CENTRE=` says how the **centre of symmetry** of a glide group was settled; it is written +only where a non-Sohncke group was adopted. `IMPLIED_BY_ABSENCES` - the group is centrosymmetric and no +group without a centre predicts its absences (P2_1/c, Pbca, Ia-3d), so the screws and glides found +imply the centre. `ABSENT_BY_ABSENCES` - the reverse (I-42d, Fdd2): no centrosymmetric group predicts +these absences. `NOT_DETERMINED` - a group with exactly the same absences and the other answer exists +(C2/c and Cc, Pnma and Pna2_1), and it is in `SPACE_GROUP_ALTERNATIVES`. Friedel's law hides the centre +from the symmetry of merged intensities, so the centrosymmetric group is written by default: a missed +centre is the common error in small-molecule space-group assignment. `ABSENT_BY_STATISTICS` - the +same situation, but the intensity distribution read acentric and the lattice ruled twinning out, so the +group without a centre was written; `CENTRE_TWINNING_EXCLUDED=` says, for these two, whether the +lattice ruled twinning out. Two non-centrosymmetric groups can also share absences (I-42d and +I4_1md); the lowest-numbered is written and the other listed as the alternative. + +`CENTRE_STATISTICS_*` are the intensity statistics behind that, read on every searched run on the +general reflections - those no operator of the Laue class but the identity maps to themselves, which +are centric exactly when the crystal is centrosymmetric. `CENTRE_STATISTICS_L=` is <|L|> (Padilla & +Yeates; 0.5 acentric, 2/pi centric), `CENTRE_STATISTICS_E2_MINUS_1=` <|E^2-1|> (0.736 / 0.968) and +`CENTRE_STATISTICS_NZ01=` N(0.1), the fraction with E^2 < 0.1 (0.095 / 0.248). Each `_F` places its +statistic between the values the same reflections read when simulated acentric (0) and centric (1) +with their own shell means and sigmas; `CENTRE_STATISTICS_CONTROL_F=` does the same for the +reflections centric in every candidate, which must read centric. `CENTRE_STATISTICS_VERDICT=` is +`ACENTRIC` when <|L|> reads `_F` <= 0.3 and the other two <= 0.5, `NOT_APPLICABLE` when any reading +falls outside -0.3..1.3 (a structure with a few heavy atoms on special positions reads beyond centric) +or the control is not centric, and `INCONCLUSIVE` otherwise. A twinned crystal reads acentric whether +or not it has a centre, which is why the verdict decides only with twinning excluded by the lattice. + `SPACE_GROUP_ENANTIOMORPH=` in section 2 reads **`ASSUMED_FROM_MODEL`** when the hand written in the files is the model's. Assumed, not determined: merged intensities cannot see the hand at all — |F| is invariant under the change of hand — so an accepted model asserts it out of prior chemical knowledge. diff --git a/image_analysis/scale_merge/CMakeLists.txt b/image_analysis/scale_merge/CMakeLists.txt index 3bda5656f..be7c72f30 100644 --- a/image_analysis/scale_merge/CMakeLists.txt +++ b/image_analysis/scale_merge/CMakeLists.txt @@ -3,6 +3,8 @@ ADD_LIBRARY(JFJochScaleMerge SearchSpaceGroup.h TwinningAnalysis.cpp TwinningAnalysis.h + CentreOfSymmetry.cpp + CentreOfSymmetry.h AnisotropyAnalysis.cpp AnisotropyAnalysis.h Merge.cpp diff --git a/image_analysis/scale_merge/CentreOfSymmetry.cpp b/image_analysis/scale_merge/CentreOfSymmetry.cpp new file mode 100644 index 000000000..5a77ae794 --- /dev/null +++ b/image_analysis/scale_merge/CentreOfSymmetry.cpp @@ -0,0 +1,259 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#include "CentreOfSymmetry.h" + +#include +#include +#include +#include +#include +#include + +namespace { + using Hkl = std::array; + + struct Refl { + Hkl hkl; + double I, sigma, d; + }; + + // Bounds of the decision, from the readings of untwinned small-molecule data processed by this + // program: centric sets read f = 0.74-1.03 on <|L|>, acentric ones -0.22-0.27; the other two + // statistics separate less well (one acentric set reads <|E^2-1|> fully centric on outliers), so + // they only have to agree, not decide. + constexpr double MAX_F_L_ACENTRIC = 0.3; + constexpr double MAX_F_OTHER_ACENTRIC = 0.5; + // The range a Wilson population can produce, with room for noise; outside it the statistic does not + // describe the structure. A perfect twin of acentric data reads f ~ -1 on <|L|>, which is the twin + // detection's business, not this; a hypercentric structure reads well above 1. + constexpr double MIN_F_PHYSICAL = -0.3; + constexpr double MAX_F_PHYSICAL = 1.3; + constexpr double MIN_F_CONTROL_CENTRIC = 0.5; + constexpr int MIN_REFLECTIONS = 50; + constexpr int REFLECTIONS_PER_SHELL = 200; + constexpr int SIMULATION_REPEATS = 10; + + struct Stats { + double e2m1 = 0.0, nz01 = 0.0; + }; + + // E^2 = I / in equal-count resolution shells, then <|E^2-1|> and N(0.1). + // `order` lists the class's reflections by resolution, so the shells are the same for every call. + Stats ClassStats(const std::vector& I, const std::vector& order) { + const size_t n = order.size(); + const size_t n_shells = std::max(1, n / REFLECTIONS_PER_SHELL); + double sum_e2m1 = 0.0; + size_t below = 0; + for (size_t s = 0; s < n_shells; ++s) { + const size_t lo = s * n / n_shells, hi = (s + 1) * n / n_shells; + double mean = 0.0; + for (size_t j = lo; j < hi; ++j) + mean += I[order[j]]; + mean /= static_cast(hi - lo); + for (size_t j = lo; j < hi; ++j) { + const double e2 = mean > 0.0 ? I[order[j]] / mean : 0.0; + sum_e2m1 += std::fabs(e2 - 1.0); + if (e2 < 0.1) + ++below; + } + } + return {sum_e2m1 / static_cast(n), static_cast(below) / static_cast(n)}; + } + + double MeanAbsL(const std::vector& I, const std::vector>& pairs) { + double sum = 0.0; + size_t n = 0; + for (const auto& [a, b] : pairs) { + const double s = I[a] + I[b]; + if (s > 0.0) { + sum += std::min(1.0, std::fabs((I[a] - I[b]) / s)); + ++n; + } + } + return n > 0 ? sum / static_cast(n) : 0.0; + } + + // Uniform, exponential and normal deviates written out rather than taken from 's + // distributions, whose output the standard leaves to the implementation: the decision this feeds + // must be the same on every platform. mt19937_64 itself is fully specified. + struct Deviates { + std::mt19937_64 engine{20261004}; + double Uniform() { return (static_cast(engine() >> 11) + 0.5) * 0x1.0p-53; } + double Exponential() { return -std::log(Uniform()); } + double Normal() { // Box-Muller, one of the pair + return std::sqrt(-2.0 * std::log(Uniform())) * std::cos(2.0 * 3.14159265358979323846 * Uniform()); + } + }; + + CentreStatistic Calibrated(double measured, double acentric, double centric) { + return {measured, acentric, centric, + centric != acentric ? (measured - acentric) / (centric - acentric) : 0.0}; + } +} + +// Following Howells, Phillips & Rogers (1950) Acta Cryst. 3, 210-214 (N(z)), and Padilla & Yeates +// (2003) Acta Cryst. D59, 1124-1130 (the L-test, read here against its centric value 2/pi). +CentreOfSymmetryResult AnalyzeCentreOfSymmetry(const std::vector& merged, + const gemmi::SpaceGroup& space_group) { + CentreOfSymmetryResult out; + const gemmi::GroupOps gops = space_group.operations(); + + // The Laue class: every proper rotation of the group, with and without the inversion. + std::vector proper, laue; + for (const auto& op : gops.derive_symmorphic().sym_ops) { + const gemmi::Op r = op.det_rot() > 0 ? gemmi::Op{op.rot, {0, 0, 0}} + : gemmi::Op{op.negated_rot(), {0, 0, 0}}; + if (std::none_of(proper.begin(), proper.end(), [&](const gemmi::Op& p) { return p.rot == r.rot; })) + proper.push_back(r); + } + for (const auto& r : proper) { + laue.push_back(r); + laue.push_back(gemmi::Op{r.negated_rot(), {0, 0, 0}}); + } + + // Fold onto the Laue class: one weighted mean per orbit, keyed by the orbit's largest member. + struct Acc { double w = 0.0, wI = 0.0, d = 0.0; }; + std::map folded; + for (const auto& r : merged) { + if (!std::isfinite(r.I) || !std::isfinite(r.sigma) || !(r.sigma > 0.0f) || !(r.d > 0.0f)) + continue; + const gemmi::Op::Miller hkl{{r.h, r.k, r.l}}; + if (gops.is_systematically_absent(hkl)) + continue; + Hkl key{r.h, r.k, r.l}; + for (const auto& op : laue) + key = std::max(key, Hkl(op.apply_to_hkl(hkl))); + auto& a = folded[key]; + const double w = 1.0 / (static_cast(r.sigma) * r.sigma); + a.w += w; + a.wI += w * r.I; + a.d = r.d; + } + std::vector refl; + for (const auto& [hkl, a] : folded) + refl.push_back({hkl, a.wI / a.w, 1.0 / std::sqrt(a.w), a.d}); + if (refl.size() < static_cast(MIN_REFLECTIONS)) + return out; + + // Low resolution first, and the high-resolution end cut where 20 equal-count shells first fall + // below 3: past that the noise, not the structure, sets the distribution. + std::sort(refl.begin(), refl.end(), [](const Refl& a, const Refl& b) { + return a.d != b.d ? a.d > b.d : a.hkl < b.hkl; + }); + size_t keep = refl.size(); + for (size_t s = 0; s < 20; ++s) { + const size_t lo = s * refl.size() / 20, hi = (s + 1) * refl.size() / 20; + double sum = 0.0; + for (size_t j = lo; j < hi; ++j) + sum += refl[j].I / refl[j].sigma; + if (hi > lo && sum / static_cast(hi - lo) < 3.0) { + keep = lo + 1; + break; + } + } + refl.resize(keep); + out.d_min_A = refl.back().d; + + // The two classes, in resolution order. + std::vector general, control; + std::map general_index; + for (size_t i = 0; i < refl.size(); ++i) { + const gemmi::Op::Miller hkl{{refl[i].hkl[0], refl[i].hkl[1], refl[i].hkl[2]}}; + const gemmi::Op::Miller minus{{-hkl[0], -hkl[1], -hkl[2]}}; + int fixed_by = 0; + for (const auto& op : laue) + if (op.apply_to_hkl(hkl) == hkl) + ++fixed_by; + bool to_friedel_mate = false; + for (const auto& op : proper) + if (op.apply_to_hkl(hkl) == minus) + to_friedel_mate = true; + if (fixed_by == 1) { + general_index[refl[i].hkl] = general.size(); + general.push_back(i); + } else if (to_friedel_mate) { + control.push_back(i); + } + } + // L-test pairs: general reflections a step of 0 or 2 apart in each index, the even steps keeping a + // half-integer pseudo-translation from correlating the pair. + std::vector> pairs; + for (size_t g : general) + for (int a = -2; a <= 2; a += 2) + for (int b = -2; b <= 2; b += 2) + for (int c = -2; c <= 2; c += 2) { + if (Hkl{a, b, c} <= Hkl{0, 0, 0}) + continue; + const Hkl& h = refl[g].hkl; + const auto it = general_index.find(Hkl{h[0] + a, h[1] + b, h[2] + c}); + if (it != general_index.end()) + pairs.emplace_back(g, general[it->second]); + } + out.n_general = static_cast(general.size()); + out.n_control = static_cast(control.size()); + out.l_pairs = static_cast(pairs.size()); + if (out.n_general < MIN_REFLECTIONS || out.n_control < MIN_REFLECTIONS || out.l_pairs < MIN_REFLECTIONS) + return out; + + std::vector I(refl.size()), sigma(refl.size()); + for (size_t i = 0; i < refl.size(); ++i) { + I[i] = refl[i].I; + sigma[i] = refl[i].sigma; + } + // The true mean each simulated intensity is drawn about: the measured shell mean of its class. + std::vector mean_true(refl.size(), 0.0); + for (const auto* cls : {&general, &control}) { + const size_t n = cls->size(); + const size_t n_shells = std::max(1, n / REFLECTIONS_PER_SHELL); + for (size_t s = 0; s < n_shells; ++s) { + const size_t lo = s * n / n_shells, hi = (s + 1) * n / n_shells; + double mean = 0.0; + for (size_t j = lo; j < hi; ++j) + mean += I[(*cls)[j]]; + mean = std::max(mean / static_cast(hi - lo), 1e-9); + for (size_t j = lo; j < hi; ++j) + mean_true[(*cls)[j]] = mean; + } + } + + const Stats g = ClassStats(I, general), c = ClassStats(I, control); + const double l = MeanAbsL(I, pairs); + + Deviates dev; + Stats sim_g[2], sim_c[2]; + double sim_l[2] = {0.0, 0.0}; + std::vector I_sim(refl.size()); + for (int centric = 0; centric < 2; ++centric) + for (int rep = 0; rep < SIMULATION_REPEATS; ++rep) { + for (size_t i = 0; i < refl.size(); ++i) { + double x; + if (centric) { + const double z = dev.Normal(); + x = z * z; + } else { + x = dev.Exponential(); + } + I_sim[i] = mean_true[i] * x + dev.Normal() * sigma[i]; + } + const Stats sg = ClassStats(I_sim, general), sc = ClassStats(I_sim, control); + sim_g[centric].e2m1 += sg.e2m1 / SIMULATION_REPEATS; + sim_g[centric].nz01 += sg.nz01 / SIMULATION_REPEATS; + sim_c[centric].e2m1 += sc.e2m1 / SIMULATION_REPEATS; + sim_l[centric] += MeanAbsL(I_sim, pairs) / SIMULATION_REPEATS; + } + + out.mean_abs_e2_minus_1 = Calibrated(g.e2m1, sim_g[0].e2m1, sim_g[1].e2m1); + out.n_z_01 = Calibrated(g.nz01, sim_g[0].nz01, sim_g[1].nz01); + out.mean_abs_l = Calibrated(l, sim_l[0], sim_l[1]); + out.control_mean_abs_e2_minus_1 = Calibrated(c.e2m1, sim_c[0].e2m1, sim_c[1].e2m1); + + const auto physical = [](const CentreStatistic& s) { return s.f >= MIN_F_PHYSICAL && s.f <= MAX_F_PHYSICAL; }; + out.applicable = physical(out.mean_abs_e2_minus_1) && physical(out.n_z_01) && physical(out.mean_abs_l) + && out.control_mean_abs_e2_minus_1.f >= MIN_F_CONTROL_CENTRIC; + out.reads_acentric = out.applicable + && out.mean_abs_l.f <= MAX_F_L_ACENTRIC + && out.mean_abs_e2_minus_1.f <= MAX_F_OTHER_ACENTRIC + && out.n_z_01.f <= MAX_F_OTHER_ACENTRIC; + return out; +} diff --git a/image_analysis/scale_merge/CentreOfSymmetry.h b/image_analysis/scale_merge/CentreOfSymmetry.h new file mode 100644 index 000000000..19c794248 --- /dev/null +++ b/image_analysis/scale_merge/CentreOfSymmetry.h @@ -0,0 +1,54 @@ +// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute +// SPDX-License-Identifier: GPL-3.0-only + +#pragma once + +#include + +#include "../../common/Reflection.h" +#include "gemmi/symmetry.hpp" + +// Whether the intensity distribution says the crystal has a centre of symmetry. Asked only where the +// absences cannot say it: two groups predicting exactly the same absences in the same Laue class, one +// centrosymmetric and one not (C2/c and Cc, Pnma and Pna2_1). Friedel's law hides the centre from the +// merged intensities' SYMMETRY, but not from their DISTRIBUTION: a centric structure factor is real, +// so its intensity follows a different law from an acentric one's. +// +// Read on the GENERAL reflections - those no operator of the Laue class other than the identity maps +// to themselves. They are centric exactly when the crystal is centrosymmetric, whichever of the two +// groups is right. The CONTROL is the reflections a proper rotation maps to their Friedel mate: those +// are centric in both groups, so they have to read centric here, or the statistic is not working. +// +// Each statistic is placed between what the same reflections would read if they were acentric and if +// they were centric, simulated with the shell means and the measured sigmas of these very reflections: +// f = 0 reads acentric, f = 1 centric. That calibration is what makes the statistics usable on noisy +// data, where the noise pulls every statistic away from its textbook value. +struct CentreStatistic { + double measured = 0.0; + double acentric = 0.0; // the same statistic on simulated acentric intensities with these sigmas + double centric = 0.0; // ...and on simulated centric ones + double f = 0.0; // (measured - acentric) / (centric - acentric) +}; + +struct CentreOfSymmetryResult { + int n_general = 0; // general reflections read (after the resolution cut) + int n_control = 0; // control reflections read + int l_pairs = 0; // L-test pairs among the general reflections + double d_min_A = 0.0; // where first fell below 3 + CentreStatistic mean_abs_e2_minus_1; // <|E^2-1|>: 0.736 acentric, 0.968 centric + CentreStatistic n_z_01; // N(0.1), the fraction with E^2 < 0.1: 0.095 acentric, 0.248 centric + CentreStatistic mean_abs_l; // <|L|>: 0.5 acentric, 2/pi centric + CentreStatistic control_mean_abs_e2_minus_1; + // The statistics can be read at all: enough reflections and pairs, every reading inside the range a + // Wilson population can produce, and the control centric. A reading beyond centric is not "very + // centric" - it is a structure the statistics do not describe (a few heavy atoms on special + // positions read hypercentric), and it says nothing either way. + bool applicable = false; + // ...and they read acentric, all three of them. + bool reads_acentric = false; +}; + +// `merged` is a merge in any group of the Laue class of `space_group` (P1 included); it is folded onto +// that Laue class here. Reflections `space_group` extinguishes are left out. +CentreOfSymmetryResult AnalyzeCentreOfSymmetry(const std::vector& merged, + const gemmi::SpaceGroup& space_group); diff --git a/image_analysis/scale_merge/SearchSpaceGroup.cpp b/image_analysis/scale_merge/SearchSpaceGroup.cpp index d03fed6e3..e9c0ef21d 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.cpp +++ b/image_analysis/scale_merge/SearchSpaceGroup.cpp @@ -83,11 +83,15 @@ namespace { // The two are the same test on different sets, so they share ScrewZoneEvidence and differ only in // what the control class is: the rest of the ROW for a screw, the rest of the PLANE for a glide. // - // NOT DETERMINABLE, AND NOT ATTEMPTED: the INVERSION CENTRE. Friedel's law makes the diffraction - // pattern centrosymmetric whether or not the crystal is, so P2 and P2/m predict exactly the same + // NOT MEASURABLE ON ITS OWN: the INVERSION CENTRE. Friedel's law makes the diffraction pattern + // centrosymmetric whether or not the crystal is, so P2 and P2/m predict exactly the same // intensities and exactly the same absences and no measurement on this merge can separate them. // The enumeration below therefore refuses any non-Sohncke group whose absence signature a Sohncke // candidate already carries, which is precisely the set that differs by an inversion centre alone. + // A centre can still be IMPLIED: no non-centrosymmetric group predicts the absences of P2_1/c, + // Pbca or Ia-3d, so a crystal showing them is centrosymmetric by its absences. Where a + // non-centrosymmetric group does share them (Pc and P2/c), one is written by convention and the + // other reported as an alternative, see the enumeration. // The one exception is not available here: with measurable anomalous signal Friedel's law breaks, // which is how the ENANTIOMORPH is decided elsewhere in this program - but that decides a hand, // not the presence of a centre, and it needs the unmerged Bijvoet pairs, not this merge. @@ -136,6 +140,7 @@ namespace { static const std::pair named[] = { {"0kl", {{0, 1, 2}}}, {"h0l", {{1, 0, 2}}}, {"hk0", {{1, 2, 0}}}, {"hhl", {{1, 1, 2}}}, {"hkh", {{1, 2, 1}}}, {"hkk", {{2, 1, 1}}}, + {"h-hl", {{1, -1, 2}}}, }; for (const auto& [label, hkl] : named) if (op.apply_to_hkl(hkl) == hkl) @@ -602,17 +607,19 @@ RowModulation FindRowModulation(const gemmi::UnitCell& cell, const std::array 0) - out.push_back(RotKey(op)); + out.push_back(RotKey(op.det_rot() > 0 ? op : gemmi::Op{op.negated_rot(), {0, 0, 0}})); std::sort(out.begin(), out.end()); + out.erase(std::unique(out.begin(), out.end()), out.end()); return out; } @@ -1990,36 +1997,64 @@ SearchSpaceGroupResult SearchSpaceGroup( sg_cands.push_back(&sg); } } - // The NON-SOHNCKE groups built on the same proper rotations - the small-molecule answer, the one - // that can name a GLIDE plane. A protein crystal is built from L-amino acids and is therefore - // chiral, so its group is Sohncke and holds only rotations and screws: no glide, no mirror, no - // inversion centre. Nothing here can be right for a protein, which is why these candidates have - // to earn their place against a bar measured on protein data rather than a bar chosen for them + // The NON-SOHNCKE groups of the same Laue class - the small-molecule answer, the one that can + // name a GLIDE plane. A protein crystal is built from L-amino acids and is therefore chiral, so + // its group is Sohncke and holds only rotations and screws: no glide, no mirror, no inversion + // centre. Nothing here can be right for a protein, which is why these candidates have to earn + // their place against a bar measured on protein data rather than a bar chosen for them // (min_glide_evidence_per_reflection, measured 0/140 false positives). // - // A candidate whose absences a Sohncke candidate already predicts is NOT offered. That single - // line is what keeps the inversion centre out: P2/m predicts exactly what P2 predicts, P-1 - // exactly what P1 predicts, and Friedel's law says no measurement on merged intensities can - // separate them. So only a group that predicts something MORE - a glide's zonal absences - is - // ever a candidate, and a centre of symmetry is never claimed. + // Keyed on the Laue class, not on the proper rotations: a non-centrosymmetric group (Pc, Pna2_1, + // I-42d) holds only half the rotations of the Laue class it merges in, and keyed on its own + // rotations it was never a candidate at all. Where its absences have no centrosymmetric carrier + // (I-42d, I4_1md, Fdd2, P-42_1c, I-43d) its glide was then never named, however dead the zone. + // + // A candidate whose absences a Sohncke candidate already predicts is NOT offered. P2/m and Pm + // predict exactly what P2 predicts and no measurement on merged intensities can separate them, + // so only a group that predicts something MORE - a glide's zonal absences - is ever a candidate. + // + // Two non-Sohncke groups can predict the same absences too - Pc and P2/c, Pna2_1 and Pnma, I-42d + // and I4_1md - and they are as inseparable as P2 and P2/m, for the same reason: what tells them + // apart (an inversion centre, a mirror where the other has a 2-fold) extinguishes nothing. One of + // them is scored and the others are carried beside it, to be reported as alternatives when it is + // selected. Which one is written is a CONVENTION, not a measurement: the CENTROSYMMETRIC group + // where there is one, otherwise the lowest-numbered. The centrosymmetric choice is a prior - it + // is what a structure is refined in first, and an inversion centre wrongly left out is the common + // error in deposited small-molecule structures, not the reverse (Baur & Kassner (1992) Acta Cryst. + // B48, 356-369, on Cc reported for C2/c) - and it is not decided from intensity statistics, which + // read a twin as a centre. + std::map> same_absences; if (opt.enumerate_non_sohncke && best_pg != nullptr) { std::vector> signatures; for (const auto* c : sg_cands) signatures.push_back(AbsenceSignature(*c)); - for (const auto& sg : gemmi::spacegroup_tables::main) { - if (sg.is_sohncke() || ProperRotationSetOf(sg) != best_pg->rotation_set) - continue; - // Same precondition as a non-reference Sohncke setting, and for the same reason: a - // setting names the mirror by AXIS, so P 1 2_1/c 1 and P 1 2_1/a 1 are different claims - // about the same crystal and a cell that cannot host one must not be offered it. - if (!opt.cell.has_value() || !CellHostsRotations(*opt.cell, sg.operations())) - continue; - auto sig = AbsenceSignature(sg); - if (std::find(signatures.begin(), signatures.end(), sig) != signatures.end()) - continue; - signatures.push_back(std::move(sig)); - sg_cands.push_back(&sg); - } + const size_t n_sohncke = sg_cands.size(); + for (int pass = 0; pass < 2; ++pass) // the centrosymmetric groups first, see above + for (const auto& sg : gemmi::spacegroup_tables::main) { + if (sg.is_sohncke() || sg.is_centrosymmetric() != (pass == 0) + || LaueRotationSetOf(sg) != best_pg->rotation_set) + continue; + // Same precondition as a non-reference Sohncke setting, and for the same reason: a + // setting names the mirror by AXIS, so P 1 2_1/c 1 and P 1 2_1/a 1 are different + // claims about the same crystal and a cell that cannot host one must not be offered it. + if (!opt.cell.has_value() || !CellHostsRotations(*opt.cell, sg.operations())) + continue; + auto sig = AbsenceSignature(sg); + const auto it = std::find(signatures.begin(), signatures.end(), sig); + if (it == signatures.end()) { + signatures.push_back(std::move(sig)); + sg_cands.push_back(&sg); + continue; + } + // Another setting of the same group (an alternative origin) is the same hypothesis + // written twice, not an alternative. + const auto* carrier = sg_cands[it - signatures.begin()]; + auto& twins = same_absences[carrier]; + if (static_cast(it - signatures.begin()) >= n_sohncke && sg.number != carrier->number + && std::none_of(twins.begin(), twins.end(), + [&](const gemmi::SpaceGroup& t) { return t.number == sg.number; })) + twins.push_back(sg); + } } std::vector sg_scored(sg_cands.size()); ParallelFor(static_cast(sg_cands.size()), @@ -2460,8 +2495,11 @@ SearchSpaceGroupResult SearchSpaceGroup( s.consistent = centering_ok && screw_ok; sg_scored[ci] = std::move(s); }); - for (auto& s : sg_scored) - result.candidates.push_back(std::move(s)); + for (size_t ci = 0; ci < sg_scored.size(); ++ci) { + if (const auto it = same_absences.find(sg_cands[ci]); it != same_absences.end()) + sg_scored[ci].same_absences = it->second; + result.candidates.push_back(std::move(sg_scored[ci])); + } // A candidate is eligible when its absences are confirmed and there are enough of them to // trust (the symmorphic group, with no absences, is always eligible as the fallback). Rank @@ -2582,6 +2620,10 @@ SearchSpaceGroupResult SearchSpaceGroup( result.best_space_group = s.space_group; // representative (lowest number) else result.alternatives.push_back(s.space_group); + // ...and the groups it stands for, which predict its absences exactly (see the + // enumeration): selected with it, because nothing scored could have separated them. + result.alternatives.insert(result.alternatives.end(), s.same_absences.begin(), + s.same_absences.end()); } } @@ -2930,6 +2972,18 @@ std::string SearchSpaceGroupResultToText(const SearchSpaceGroupResult& result, if (result.sohncke_space_group.has_value()) os << " Best Sohncke group for the same data: " << SettingName(*result.sohncke_space_group) << ".\n"; + for (const auto& c : result.candidates) { + if (!c.selected || c.same_absences.empty()) + continue; + os << " " << SettingName(c.space_group); + for (const auto& t : c.same_absences) + os << ", " << SettingName(t); + os << " predict exactly the same absences in the same Laue class, and\n" + " these data cannot separate them: what differs (an inversion centre, a mirror in\n" + " place of a 2-fold) extinguishes nothing, and Friedel's law hides it from the\n" + " intensities. The " << (c.space_group.is_centrosymmetric() ? "centrosymmetric" : "lowest-numbered") + << " one is written - a convention, not a measurement.\n"; + } } // The axes the data left open, said in words. The zone table above is printed for the SELECTED diff --git a/image_analysis/scale_merge/SearchSpaceGroup.h b/image_analysis/scale_merge/SearchSpaceGroup.h index cb8b0e15f..58f087b9b 100644 --- a/image_analysis/scale_merge/SearchSpaceGroup.h +++ b/image_analysis/scale_merge/SearchSpaceGroup.h @@ -124,6 +124,11 @@ struct SpaceGroupCandidateScore { // absence in a group that otherwise fits; a glide is an extra claim on top of a group that // already fits without it, and an extra claim with no evidence behind it is not a tie, it is a no. bool glide_unmeasurable = false; + // Other non-Sohncke groups of the same Laue class predicting exactly this candidate's absences - + // P2/c's Pc, Pnma's Pna2_1, I4_1md's I-42d. They differ from it only by elements that extinguish + // nothing (an inversion centre, a mirror in place of a 2-fold), so they are not scored separately; + // a selected candidate brings them into `alternatives`. Empty for every Sohncke candidate. + std::vector same_absences; double absent_mean_i_over_sigma = 0.0; double present_mean_i_over_sigma = 0.0; @@ -654,8 +659,8 @@ struct SearchSpaceGroupOptions { double min_screw_absence_evidence = 8.0; // ---- Glide planes (small-molecule space groups) ------------------------------------------- - // Offer the NON-SOHNCKE space groups of the same proper-rotation set as candidates, so a glide - // plane can be named. Protein crystals are built from L-amino acids and are therefore chiral: + // Offer the NON-SOHNCKE space groups of the same Laue class as candidates, so a glide plane can + // be named. Protein crystals are built from L-amino acids and are therefore chiral: // their groups are Sohncke and contain rotations and screws only, so this can never change a // protein answer except through a false positive - which is why it is measured rather than // assumed (see min_glide_evidence_per_reflection). Small molecules are where it earns its place: @@ -664,8 +669,11 @@ struct SearchSpaceGroupOptions { // // Only a group whose ABSENCES differ from a Sohncke candidate's is offered. That is not an // optimisation, it is the honest limit of the measurement: P2/m predicts exactly what P2 - // predicts, so the inversion centre buys no candidate and is never claimed. See the note on - // Friedel's law at the top of SearchSpaceGroup.cpp. + // predicts, so the inversion centre buys no candidate. Where a glide IS found, the groups that + // carry it with and without an inversion centre (P2/c and Pc) predict the same absences; the + // centrosymmetric one is written, by convention, and the other is reported beside it as an + // alternative these data cannot separate. See the note on Friedel's law at the top of + // SearchSpaceGroup.cpp. bool enumerate_non_sohncke = true; // How dead a glide zone must be, per reflection, in nats, before the glide may be claimed. diff --git a/rugnux/ResultReport.cpp b/rugnux/ResultReport.cpp index 14e9848f4..473925fd2 100644 --- a/rugnux/ResultReport.cpp +++ b/rugnux/ResultReport.cpp @@ -654,6 +654,66 @@ ReportDocument BuildReportDocument(const std::string &output_prefix, alts += (alts.empty() ? "" : " | ") + alt.xhm(); Add(s, KeyText("SPACE_GROUP_ALTERNATIVES", alts.empty() ? "NONE" : alts)); + // The centre of symmetry of a glide group, and the intensity statistics read for it. Where the + // absences settle it this says so; where a group with the same absences and the other answer + // exists (C2/c and Cc), it says which one was written and on what grounds, and the other is in + // SPACE_GROUP_ALTERNATIVES. + if (!result.space_group_centre.empty()) + Add(s, KeyEnum("SPACE_GROUP_CENTRE", result.space_group_centre, + {"IMPLIED_BY_ABSENCES", "ABSENT_BY_ABSENCES", "NOT_DETERMINED", "ABSENT_BY_STATISTICS"})); + if (result.space_group_centre == "NOT_DETERMINED" || result.space_group_centre == "ABSENT_BY_STATISTICS") + Add(s, KeyBool("CENTRE_TWINNING_EXCLUDED", result.centre_twinning_excluded)); + if (result.centre_statistics.has_value() && result.centre_statistics->n_general > 0) { + const auto &c = *result.centre_statistics; + Add(s, KeyInt("CENTRE_STATISTICS_REFLECTIONS", c.n_general)); + if (c.l_pairs > 0 && c.n_control > 0 && (c.mean_abs_l.centric != c.mean_abs_l.acentric)) { + Add(s, KeyReal("CENTRE_STATISTICS_L", c.mean_abs_l.measured, "{:.3f}")); + Add(s, KeyReal("CENTRE_STATISTICS_L_F", c.mean_abs_l.f, "{:.2f}")); + Add(s, KeyReal("CENTRE_STATISTICS_E2_MINUS_1", c.mean_abs_e2_minus_1.measured, "{:.3f}")); + Add(s, KeyReal("CENTRE_STATISTICS_E2_MINUS_1_F", c.mean_abs_e2_minus_1.f, "{:.2f}")); + Add(s, KeyReal("CENTRE_STATISTICS_NZ01", c.n_z_01.measured, "{:.3f}")); + Add(s, KeyReal("CENTRE_STATISTICS_NZ01_F", c.n_z_01.f, "{:.2f}")); + Add(s, KeyReal("CENTRE_STATISTICS_CONTROL_F", c.control_mean_abs_e2_minus_1.f, "{:.2f}")); + } + Add(s, KeyEnum("CENTRE_STATISTICS_VERDICT", + !c.applicable ? "NOT_APPLICABLE" : c.reads_acentric ? "ACENTRIC" : "INCONCLUSIVE", + {"ACENTRIC", "INCONCLUSIVE", "NOT_APPLICABLE"})); + Add(s, Prose( + " CENTRE_STATISTICS_* read whether the intensity distribution has a centre of symmetry,\n" + " on the general reflections (no operator of the Laue class but the identity fixes them).\n" + " Each _F places the statistic between what these reflections would read acentric (0) and\n" + " centric (1), simulated with their own shell means and sigmas: <|L|> (Padilla & Yeates),\n" + " <|E^2-1|> and N(0.1), the fraction with E^2 < 0.1. CONTROL_F is the reflections centric in\n" + " every candidate, and must read centric. NOT_APPLICABLE where a reading falls outside\n" + " -0.3..1.3 - few heavy atoms on special positions read beyond centric - or the control\n" + " does not read centric; ACENTRIC where all three read acentric. A twinned crystal reads\n" + " acentric whether it has a centre or not, so this decides nothing on its own.")); + } + if (result.space_group_centre == "NOT_DETERMINED") + Add(s, Prose( + " SPACE_GROUP_CENTRE is NOT_DETERMINED: SPACE_GROUP_ALTERNATIVES holds a group with\n" + " exactly the absences of SPACE_GROUP_NAME and no centre of symmetry. Friedel's law hides\n" + " the centre from the symmetry of the merged intensities, so these data cannot separate\n" + " the two; the centrosymmetric one is what the .mtz/.cif/.hkl carry, because a missed\n" + " centre is the common error in small-molecule work. The intensity distribution would\n" + " override that only when it read ACENTRIC with twinning ruled out by the lattice.\n" + " Structure solution and refinement decide it.")); + else if (result.space_group_centre == "ABSENT_BY_ABSENCES" && result.space_group_search.has_value() + && std::ranges::any_of(result.space_group_search->alternatives, + [](const gemmi::SpaceGroup &a) { return !a.is_sohncke(); })) + Add(s, Prose( + " SPACE_GROUP_ALTERNATIVES holds a group with exactly the absences of SPACE_GROUP_NAME,\n" + " and no centre of symmetry either (I-42d and I4_1md): what separates them - a mirror in\n" + " place of a 2-fold - extinguishes nothing, so these data cannot separate them, and the\n" + " centre statistics do not apply. The lowest-numbered is what the .mtz/.cif/.hkl carry: a\n" + " convention, not a measurement. Structure solution and refinement decide it.")); + else if (result.space_group_centre == "ABSENT_BY_STATISTICS") + Add(s, Prose( + " SPACE_GROUP_CENTRE is ABSENT_BY_STATISTICS: a centrosymmetric group with the same\n" + " absences exists (in SPACE_GROUP_ALTERNATIVES), but the intensity distribution reads\n" + " acentric on all three statistics and the lattice rules twinning out - the metric admits\n" + " no rotation beyond the Laue class and no twin-domain lattice was found.")); + // ...and WHICH axis is open, which the list of names above does not say. A screw the data // could not test is not a near miss between two groups - it is a question nobody asked, and // the group that gets written is the one claiming no screw, by convention. Say the axis, say diff --git a/rugnux/Rugnux.cpp b/rugnux/Rugnux.cpp index 596ee0b0e..b4a68b0e6 100644 --- a/rugnux/Rugnux.cpp +++ b/rugnux/Rugnux.cpp @@ -8055,6 +8055,47 @@ ProcessResult Rugnux::RunPipeline(RugnuxObserver *observer, bool write_output, b } } + // The centre of symmetry of a glide group. Where the absences settle it - no group of the + // other kind predicts them (P2_1/c, Pbca; I-42d) - it is settled. Where a group with the same + // absences and the other answer exists (C2/c and Cc), the centrosymmetric one is written by + // default - a missed centre is the common error in small-molecule space-group assignment, not + // a spurious one - and gives way after the merge below only when the intensity distribution + // reads acentric on all three statistics AND the lattice rules twinning out, because a twinned + // centrosymmetric crystal reads exactly like an untwinned acentric one. + std::optional centre_partner; + if (sg_search.best_space_group.has_value() && !sg_search.best_space_group->is_sohncke()) { + const gemmi::SpaceGroup written = *sg_search.best_space_group; + for (const auto &c : sg_search.candidates) + if (c.selected && c.space_group.xhm() == written.xhm()) + for (const auto &t : c.same_absences) + if (!centre_partner && t.is_centrosymmetric() != written.is_centrosymmetric()) + centre_partner = t; + if (census_future.valid()) + result.leftover_lattices = census_future.get(); + if (result.consensus_cell.has_value() && result.leftover_lattices.has_value()) { + const auto &uc = *result.consensus_cell; + const gemmi::UnitCell gc(uc.a, uc.b, uc.c, uc.alpha, uc.beta, uc.gamma); + size_t laue_rotations = 0; + for (const auto &op : written.operations().sym_ops) + if (op.det_rot() > 0) + ++laue_rotations; + if (!written.is_centrosymmetric()) + laue_rotations *= 2; // a group without a centre holds half its Laue class's rotations + result.centre_twinning_excluded = + gemmi::find_lattice_symmetry(gc, written.centring_type(), LATTICE_MAX_OBLIQUITY_DEG) + .sym_ops.size() == laue_rotations + && std::none_of(result.leftover_lattices->lattices.begin(), + result.leftover_lattices->lattices.end(), [](const LeftoverLattice &l) { + return l.kind == LeftoverLatticeKind::TwinDomain; + }); + } + if (!centre_partner) + result.space_group_centre = written.is_centrosymmetric() ? "IMPLIED_BY_ABSENCES" + : "ABSENT_BY_ABSENCES"; + else + result.space_group_centre = "NOT_DETERMINED"; + } + // Adopt the determined space group and re-scale/merge in it, so scaling uses symmetry // equivalents and the statistics come out in the right symmetry. P1 stands when nothing // is determined - and it has to stand as a MERGE of its own, not as whatever `sm` happens @@ -8072,7 +8113,7 @@ ProcessResult Rugnux::RunPipeline(RugnuxObserver *observer, bool write_output, b // promise above is kept HERE, rather than resting on eligibility gates in another file that // a later change could tighten without anyone noticing what else was leaning on them. { - const gemmi::SpaceGroup sg = sg_search.best_space_group.value_or(gemmi::get_spacegroup_p1()); + gemmi::SpaceGroup sg = sg_search.best_space_group.value_or(gemmi::get_spacegroup_p1()); // xhm(), not short_name(): the search can now return a non-reference setting, and // short_name() cannot tell P 1 2 1 from P 1 1 2. logger.Info("Adopting space group {} (number {})", sg.xhm(), sg.number); @@ -8265,6 +8306,33 @@ ProcessResult Rugnux::RunPipeline(RugnuxObserver *observer, bool write_output, b join_pre_promotion(); sm = std::move(adopted); + // The intensity statistics for the centre, on this merge - the full one, resolution-cut and + // corrected, in the adopted group. Read on every run and reported, Sohncke groups included, + // so a chiral small molecule's reader sees them too; they decide only between a glide + // group and its partner with the same absences (see above). + result.centre_statistics = AnalyzeCentreOfSymmetry(sm.merged, sg); + if (centre_partner && sg.is_centrosymmetric() && result.centre_statistics->reads_acentric + && result.centre_twinning_excluded) { + logger.Info("Centre of symmetry: {} and {} predict the same absences; the intensity " + "distribution reads acentric (<|L|> f={:.2f}, <|E^2-1|> f={:.2f}, N(0.1) " + "f={:.2f}) and the lattice rules twinning out, so {} is written", + sg.xhm(), centre_partner->xhm(), result.centre_statistics->mean_abs_l.f, + result.centre_statistics->mean_abs_e2_minus_1.f, + result.centre_statistics->n_z_01.f, centre_partner->xhm()); + result.space_group_centre = "ABSENT_BY_STATISTICS"; + std::replace_if(sg_search.alternatives.begin(), sg_search.alternatives.end(), + [&](const gemmi::SpaceGroup &a) { return a.number == centre_partner->number; }, + sg); + sg = *centre_partner; + sg_search.best_space_group = sg; + sg_search.glide_space_group = sg; + experiment_.SetSpaceGroup(sg); + end_msg.space_group_name = sg.xhm(); + end_msg.space_group_number = static_cast(sg.number); + result.space_group = sg; + sm = scale_and_merge(sg.short_name(), false); + } + // A promotion the operator correlations CONFIRMED and Stage A then refused, put to // the merge. Every gate that can refuse one is a ratio against another operator or // against the noise floor, and both references are properties of how the crystal was diff --git a/rugnux/Rugnux.h b/rugnux/Rugnux.h index 638b7502c..ff147a76c 100644 --- a/rugnux/Rugnux.h +++ b/rugnux/Rugnux.h @@ -27,6 +27,7 @@ #include "../image_analysis/spot_finding/SpotUtils.h" // PowderRings #include "../image_analysis/scale_merge/Merge.h" // MergeStatistics #include "../image_analysis/scale_merge/TwinningAnalysis.h" // TwinningAnalysisResult +#include "../image_analysis/scale_merge/CentreOfSymmetry.h" // CentreOfSymmetryResult #include "../image_analysis/scale_merge/TranslationalNCS.h" // TranslationalNCSResult #include "../image_analysis/indexing/HarmonicContamination.h" // HarmonicContaminationResult #include "ModelValidation.h" // ModelValidationResult @@ -482,6 +483,16 @@ struct ProcessResult { // populated only when a search ran (no space group was fixed). The library no longer renders it to // text - the CLI formats it for stdout, the viewer draws it as a table. std::optional space_group_search; + // How the written group's centre of symmetry was settled, where a glide group was adopted (empty for + // a Sohncke group): IMPLIED_BY_ABSENCES (centrosymmetric, and no group without a centre predicts its + // absences), ABSENT_BY_ABSENCES (the reverse), NOT_DETERMINED (a group with the same absences and the + // other answer exists; the centrosymmetric one is written by default) or ABSENT_BY_STATISTICS (the + // intensity distribution read acentric with twinning excluded, so the one without is written). + std::string space_group_centre; + // The intensity statistics read for it, and whether the lattice excluded twinning (the metric admits + // no rotation beyond the Laue class, and no twin-domain lattice was found among the leftover spots). + std::optional centre_statistics; + bool centre_twinning_excluded = false; // When the two search arms (all observations / Lorentz-filtered) disagreed and the higher point // group was taken from one of them: what the other arm said - which gate refused the promotion, // or that it never confirmed the operators. Empty when the arms agreed. diff --git a/tests/SearchSpaceGroupTest.cpp b/tests/SearchSpaceGroupTest.cpp index 846e410cc..40a96990e 100644 --- a/tests/SearchSpaceGroupTest.cpp +++ b/tests/SearchSpaceGroupTest.cpp @@ -1,11 +1,13 @@ #include #include "../image_analysis/scale_merge/SearchSpaceGroup.h" +#include "../image_analysis/scale_merge/CentreOfSymmetry.h" #include "gemmi/symmetry.hpp" #include #include #include +#include #include #include #include @@ -815,3 +817,127 @@ TEST_CASE("SearchSpaceGroup never claims an inversion centre") { CHECK(result.best_space_group->is_sohncke()); CHECK_FALSE(result.glide_space_group.has_value()); } + +// A NON-centrosymmetric glide group holds only half the rotations of its Laue class, so it has to be +// enumerated by Laue class to be a candidate at all. Where its absences have no centrosymmetric +// carrier (I-42d, Fdd2, P-42_1c, I-43d) it is the only group naming the glide; I-42d and I4_1md +// predict the same absences, so the one not written is reported beside the one that is. +TEST_CASE("SearchSpaceGroup names a glide with no centrosymmetric carrier") { + struct Case { + std::string input_name; + std::vector accepted; // the written group and its same-absence twins + gemmi::UnitCell cell; + }; + const std::vector cases = { + {"I -4 2 d", {"I 41 m d", "I -4 2 d"}, gemmi::UnitCell(7.4, 7.4, 6.9, 90, 90, 90)}, + {"P -4 21 c", {"P -4 21 c"}, gemmi::UnitCell(7.4, 7.4, 6.9, 90, 90, 90)}, + {"F d d 2", {"F d d 2"}, gemmi::UnitCell(11.0, 13.0, 17.0, 90, 90, 90)}, + {"I -4 3 d", {"I -4 3 d"}, gemmi::UnitCell(9.0, 9.0, 9.0, 90, 90, 90)}, + }; + for (const auto& tc : cases) { + DYNAMIC_SECTION(tc.input_name) { + const gemmi::SpaceGroup& sg = gemmi::get_spacegroup_by_name(tc.input_name); + const auto merged = GenerateMergedReflectionsForSpaceGroup(sg, 12); + + SearchSpaceGroupOptions opt; + opt.merge_friedel = true; + opt.cell = tc.cell; + opt.enumerate_all_settings = true; + + const auto result = SearchSpaceGroup(merged, opt); + INFO(SearchSpaceGroupResultToText(result)); + REQUIRE(result.best_space_group.has_value()); + std::vector named{result.best_space_group->xhm()}; + for (const auto& alt : result.alternatives) + named.push_back(alt.xhm()); + CHECK_THAT(named, Catch::Matchers::UnorderedEquals(tc.accepted)); + CHECK(result.glide_space_group.has_value()); + REQUIRE(result.sohncke_space_group.has_value()); + CHECK(result.sohncke_space_group->is_sohncke()); + } + } +} + +// Pc and P2/c (Pna2_1 and Pnma) predict the same absences and differ by an inversion centre, which +// Friedel's law hides. By convention the CENTROSYMMETRIC one is written, whichever the data came +// from, and the other is reported as an alternative - never dropped, so the run does not claim a +// centre nothing measured. +TEST_CASE("SearchSpaceGroup writes the centrosymmetric group and reports its non-centrosymmetric twin") { + struct Case { + std::string input_name; + std::string written; + std::string alternative; + gemmi::UnitCell cell; + }; + const gemmi::UnitCell monoclinic(11.0, 13.0, 17.0, 90.0, 101.0, 90.0); + const gemmi::UnitCell orthorhombic(11.0, 13.0, 17.0, 90.0, 90.0, 90.0); + const std::vector cases = { + {"P 1 c 1", "P 1 2/c 1", "P 1 c 1", monoclinic}, + {"P 1 2/c 1", "P 1 2/c 1", "P 1 c 1", monoclinic}, + {"C 1 c 1", "C 1 2/c 1", "C 1 c 1", monoclinic}, + {"P n a 21", "P n a m", "P n a 21", orthorhombic}, + }; + for (const auto& tc : cases) { + DYNAMIC_SECTION(tc.input_name) { + const gemmi::SpaceGroup& sg = gemmi::get_spacegroup_by_name(tc.input_name); + const auto merged = GenerateMergedReflectionsForSpaceGroup(sg, 10); + + SearchSpaceGroupOptions opt; + opt.merge_friedel = true; + opt.cell = tc.cell; + opt.enumerate_all_settings = true; + + const auto result = SearchSpaceGroup(merged, opt); + INFO(SearchSpaceGroupResultToText(result)); + REQUIRE(result.best_space_group.has_value()); + CHECK(result.best_space_group->xhm() == tc.written); + CHECK(std::any_of(result.alternatives.begin(), result.alternatives.end(), + [&](const gemmi::SpaceGroup& a) { return a.xhm() == tc.alternative; })); + } + } +} + +// The centre-of-symmetry statistics on synthetic Wilson data: acentric and centric intensities on the +// general reflections of a 2/m Laue class, with the reflections a 2-fold maps to their Friedel mate +// (h0l) centric in both. The calibrated readings must land at their ends, and only the acentric set +// may read ACENTRIC. +TEST_CASE("AnalyzeCentreOfSymmetry separates acentric from centric Wilson data") { + const gemmi::UnitCell cell(11.0, 13.0, 17.0, 90.0, 101.0, 90.0); + const gemmi::SpaceGroup& sg = gemmi::get_spacegroup_by_name("P 1 2/c 1"); + for (const bool centric : {false, true}) { + DYNAMIC_SECTION((centric ? "centric" : "acentric")) { + std::mt19937 rng(7); + std::exponential_distribution expo(1.0); + std::normal_distribution gauss(0.0, 1.0); + std::vector merged; + const gemmi::GroupOps gops = sg.operations(); + const gemmi::ReciprocalAsu asu(&sg); + for (int h = -10; h <= 10; ++h) + for (int k = 0; k <= 10; ++k) + for (int l = -12; l <= 12; ++l) { + const gemmi::Op::Miller hkl{{h, k, l}}; + if ((h == 0 && k == 0 && l == 0) || !asu.is_in(hkl) || gops.is_systematically_absent(hkl)) + continue; + const double d = cell.calculate_d(hkl); + if (d < 0.8) + continue; + // h0l is centric under the 2-fold whatever the crystal is. + const bool c = centric || k == 0; + const double z = gauss(rng); + const double I = 1000.0 * std::exp(-2.0 / (d * d)) * (c ? z * z : expo(rng)); + merged.push_back(MergedReflection{.h = h, .k = k, .l = l, + .I = static_cast(I), .sigma = 1.0f, + .d = static_cast(d)}); + } + const auto r = AnalyzeCentreOfSymmetry(merged, sg); + INFO("n " << r.n_general << " pairs " << r.l_pairs << " L f " << r.mean_abs_l.f << " E f " + << r.mean_abs_e2_minus_1.f << " N f " << r.n_z_01.f << " control f " + << r.control_mean_abs_e2_minus_1.f); + REQUIRE(r.n_general > 1000); + CHECK(r.applicable); + CHECK(r.mean_abs_l.f == Catch::Approx(centric ? 1.0 : 0.0).margin(0.2)); + CHECK(r.mean_abs_e2_minus_1.f == Catch::Approx(centric ? 1.0 : 0.0).margin(0.2)); + CHECK(r.reads_acentric == !centric); + } + } +}