diff --git a/image_analysis/scale_merge/TwinningAnalysis.cpp b/image_analysis/scale_merge/TwinningAnalysis.cpp index b8b07525c..01349f9a8 100644 --- a/image_analysis/scale_merge/TwinningAnalysis.cpp +++ b/image_analysis/scale_merge/TwinningAnalysis.cpp @@ -16,6 +16,8 @@ #include +#include "gemmi/twin.hpp" + #include "../../common/ParallelFor.h" namespace { @@ -28,12 +30,6 @@ namespace { return std::isfinite(r.I) && std::isfinite(r.d) && r.d > 0.0; } - // Merohedral twinning needs a twin law - a lattice symmetry operation that is not a symmetry of the - // crystal - which exists only when the Laue class is a proper subgroup of the lattice holohedry. - // The holohedral high-symmetry Laue classes (4/mmm, 6/mmm, m-3m, and -3m on a rhombohedral lattice) - // admit no such operation, so twinning is geometrically impossible and the intensity statistics - // cannot be indicating it. Low-symmetry classes stay eligible because pseudo-merohedral twinning - // through an accidental metric specialisation cannot be excluded from the symmetry alone. // <|L|> of a partial twin with fraction a: I = (1-a) X + a Y for independent Wilson X, Y, and the // expectation of |I1 - I2|/(I1 + I2) over two independent such intensities, integrated in closed // form (0.500 at a = 0, 0.440 at 0.1, 0.408 at 0.2, 0.375 at 0.5; checked by simulation, and @@ -60,26 +56,16 @@ namespace { } return 0.5 * (lo + hi); } +} - bool MerohedralTwinningPossible(const gemmi::SpaceGroup* sg) { - if (!sg) - return true; // P1 / unknown symmetry: cannot rule twinning out - switch (sg->laue_class()) { - case gemmi::Laue::L4mmm: // 4/mmm - tetragonal holohedry - case gemmi::Laue::L6mmm: // 6/mmm - hexagonal holohedry - case gemmi::Laue::Lm3m: // m-3m - cubic holohedry - return false; - case gemmi::Laue::L3m: // -3m is holohedral on a rhombohedral (R) lattice, but a - return sg->hm[0] != 'R'; // hexagonal-P 32/3m crystal can still twin towards 6/mmm - default: - return true; - } - } +bool TwinLawPossible(const gemmi::UnitCell& cell, const gemmi::SpaceGroup& space_group, double max_obliquity_deg) { + return !gemmi::find_twin_laws(cell, &space_group, max_obliquity_deg, /*all_ops=*/false).empty(); } TwinningAnalysisResult AnalyzeTwinning(const std::vector& merged, const gemmi::SpaceGroup* space_group, int resolution_shells, - const std::array* tncs_vector, char lattice_centring) { + const std::array* tncs_vector, char lattice_centring, + const gemmi::UnitCell* cell) { TwinningAnalysisResult result; if (merged.empty()) return result; @@ -326,7 +312,13 @@ TwinningAnalysisResult AnalyzeTwinning(const std::vector& merg // ~490 A axis, whose neighbouring reflections overlap and share counts - averaging of another // kind, which narrows the distribution the same way. That is why this is a warning and never a // veto on the space group. - result.merohedral_twinning_possible = MerohedralTwinningPossible(space_group); + // Whether a twin law can exist is a property of the LATTICE: a rotation of its metric that the + // group lacks (gemmi's find_twin_laws, the same question RfreeFlags and ReindexAmbiguity ask). A + // list of Laue classes cannot answer it - a pseudo-merohedral 2/m or mmm lattice admits a law the + // class names nothing about, and a C-centred orthorhombic lattice with no special metric admits + // none although mmm is not "holohedral" by name. In P1, or without a cell, it cannot be ruled out. + result.merohedral_twinning_possible = + !space_group || !cell || TwinLawPossible(*cell, *space_group); // The two indicators can only move one way under twinning: <|L|> down from 0.500 towards 0.375, // the second moment down from 2.0 towards 1.5. A narrow intensity distribution sitting next to an // <|L|> at or ABOVE its untwinned value therefore has some other cause, and calling it a twin is @@ -356,7 +348,7 @@ TwinningAnalysisResult AnalyzeTwinning(const std::vector& merg if (result.merohedral_twinning_possible) result.twinning_suspected = !l_test_contradicts_twin && (l_test_low || moment_low); else { - // In a holohedral class the alternative is not a partial twin anywhere between 0.5 and 0.375 + // With no twin law possible the alternative is not a partial twin anywhere between 0.5 and 0.375 // but a false operator, which puts <|L|> at the perfect-twin 0.375 itself - so the bar sits // nearer that value. Measured on the corpus (normalised L-test): the weakest genuine // holohedral crystal reads 0.436, the over-promoted twin 0.374. The second moment does not @@ -365,6 +357,9 @@ TwinningAnalysisResult AnalyzeTwinning(const std::vector& merg // law lies OUTSIDE the Laue class, pulls down (1.66 beside an untwinned <|L|>). result.adopted_operators_suspect = l_test_usable && result.mean_abs_l < 0.42; result.twinning_suspected = result.adopted_operators_suspect; + // Above that bar but still in the twin band: not a twin in this group, which the lattice rules + // out, so not "needs a twin law" - the statistics say instead that the group may be too high. + result.twin_like_without_twin_law = !result.adopted_operators_suspect && l_test_low; } // The twin fraction, from the statistic that carries the verdict. Merged under a false operator @@ -411,8 +406,8 @@ std::string TwinningAnalysisToText(const TwinningAnalysisResult& result) { << " [untwinned 2.00, perfect twin 1.50]\n"; const bool l_test_read = result.l_test_pairs > 0 && !result.l_test_contaminated_by_tncs; if (result.adopted_operators_suspect) - os << " => An adopted operator relates UNEQUAL intensities. The Laue class is holohedral, so no\n" - << " twin law exists within it - yet the intensities read like a twin's, and merging under\n" + os << " => An adopted operator relates UNEQUAL intensities. The lattice admits no twin law for this\n" + << " group - yet the intensities read like a twin's, and merging under\n" << " a false operator produces exactly that distribution, whatever the twin fraction.\n" << (result.laue_class_was_chosen_by_promotion ? " This class was chosen by the space-group search: the promotion is suspect.\n" @@ -440,14 +435,20 @@ std::string TwinningAnalysisToText(const TwinningAnalysisResult& result) { : ".\n"); os << " Statistics flag the presence of twinning, not the twin law; the law itself is a\n" << " question for a dedicated twin-law analysis.\n"; - } else if (!result.merohedral_twinning_possible && result.laue_class_was_chosen_by_promotion) - os << " => No twinning indicated. The Laue class is holohedral, so no twin law exists within it,\n" + } else if (result.twin_like_without_twin_law) + os << " => Twin-like statistics, but the lattice admits no twin law for this group (no rotation of\n" + << " its metric that the group lacks), so they cannot be a twin in this group. Either the\n" + << " symmetry is too high - a twin of a subgroup whose law the group absorbed, or non-\n" + << " crystallographic symmetry taken for real - or the statistics are biased by something\n" + << " other than twinning (non-crystallographic symmetry, overlapping reflections).\n"; + else if (!result.merohedral_twinning_possible && result.laue_class_was_chosen_by_promotion) + os << " => No twinning indicated. The lattice admits no twin law for this group,\n" << " and it was chosen by the space-group search - but its operators read as genuine: a\n" << " merge under a false operator has the perfect-twin distribution, not the untwinned one\n" << " measured here. An operator that is only APPROXIMATE (non-crystallographic symmetry\n" << " along it) also passes; the subgroup statistics reported by the search speak to that.\n"; else if (!result.merohedral_twinning_possible) - os << " => No twinning: the Laue class is holohedral, so no merohedral twin law exists, and no\n" + os << " => No twinning: the lattice admits no twin law for this group, and no\n" << " operator of it averages unequal intensities.\n"; else if (result.l_test_contaminated_by_tncs) os << " => No twinning indicated by the second moment. The L-test, which is normally the\n" @@ -473,7 +474,10 @@ std::string TwinningVerdictLine(const TwinningAnalysisResult& result) { os << std::setprecision(2) << ", twin fraction ~" << result.estimated_twin_fraction << " from the " << (result.twin_fraction_source == TwinFractionSource::LTest ? "L-test" : "second moment"); os << ")"; - } else if (l_test_read) + } else if (result.twin_like_without_twin_law) + os << "TWIN-LIKE, NO TWIN LAW (<|L|> " << result.mean_abs_l + << ": the lattice admits no twin law for this group - the symmetry may be too high)"; + else if (l_test_read) os << "no indication (<|L|> " << result.mean_abs_l << ")"; else os << "no indication (second moment " << std::setprecision(2) << result.second_moment diff --git a/image_analysis/scale_merge/TwinningAnalysis.h b/image_analysis/scale_merge/TwinningAnalysis.h index cda213ece..b914d125e 100644 --- a/image_analysis/scale_merge/TwinningAnalysis.h +++ b/image_analysis/scale_merge/TwinningAnalysis.h @@ -22,8 +22,8 @@ // - Second moment /^2 of acentric intensities, normalised per resolution shell so it is // 2.0 for untwinned data and 1.5 for a perfect twin (= 2(1 - a + a^2) for twin fraction a). // -// These flag the *presence* of twinning; they do not by themselves identify the twin law. In a -// holohedral Laue class, where no twin law exists, the same low reading says instead that an operator +// These flag the *presence* of twinning; they do not by themselves identify the twin law. Where the +// lattice admits no twin law for the group, the same low reading says instead that an operator // of the group averages unequal intensities - the group is too high, or a twin law was absorbed into // it (adopted_operators_suspect). @@ -55,11 +55,17 @@ struct TwinningAnalysisResult { // 0.5). Set in every Laue class; what it means depends on the next two fields. bool twinning_suspected = false; - // False when the Laue class is holohedral (4/mmm, 6/mmm, m-3m, rhombohedral -3m): no merohedral - // twin law can exist WITHIN it. + // False when the lattice admits no twin law for the group (TwinLawPossible): no rotation of the + // metric lies outside it, so no merohedral or pseudo-merohedral twin law can exist. bool merohedral_twinning_possible = true; - // twinning_suspected in a holohedral class: an operator of the group averages intensities that + // No twin law is possible, yet <|L|> sits in the twin band (below 0.44) above the bar + // adopted_operators_suspect is read at. Not a twin in this group - the lattice rules that out - so + // a reading that the group may be too high (a subgroup's twin law absorbed, or non-crystallographic + // symmetry taken for real), or that something other than twinning narrowed the distribution. + bool twin_like_without_twin_law = false; + + // twinning_suspected where no twin law is possible: an operator of the group averages intensities that // are not equal, so the group is too high or it has absorbed a twin law. A warning about the // symmetry, not a twin call - and the twin fraction is not measurable on such a merge. bool adopted_operators_suspect = false; @@ -81,10 +87,18 @@ struct TwinningAnalysisResult { bool laue_class_was_chosen_by_promotion = false; }; +// Whether a twin law can exist for `space_group` on this lattice: a rotation of the lattice's own metric +// symmetry - found by Le Page's two-fold search within `max_obliquity_deg`, phenix.xtriage's default of +// 3 degrees, so a pseudo-merohedral lattice counts as its higher metric - that the group lacks. +bool TwinLawPossible(const gemmi::UnitCell& cell, const gemmi::SpaceGroup& space_group, + double max_obliquity_deg = 3.0); + // The space group (when known) is used to drop centric reflections, which follow different // statistics (<|L|>, /^2 = 3 not 2); pass nullptr for P1 data, where none are centric. // `lattice_centring` drops the reflections the lattice centring extinguishes, for a P1 merge of a // centred lattice (whose space_group is nullptr and so says nothing about them). +// `cell` decides whether a twin law is possible at all (TwinLawPossible); without it, or in P1, one is +// assumed possible. // `tncs_vector` is the fractional pseudo-translation of a DETECTED translational pseudo-symmetry on // this same merge (TranslationalNCSVectorForLTest), or nullptr. It changes which partner reflections // the L-test pairs with, so it has to be an argument rather than a flag set on the result afterwards: @@ -93,7 +107,8 @@ TwinningAnalysisResult AnalyzeTwinning(const std::vector& merg const gemmi::SpaceGroup* space_group = nullptr, int resolution_shells = 20, const std::array* tncs_vector = nullptr, - char lattice_centring = 'P'); + char lattice_centring = 'P', + const gemmi::UnitCell* cell = nullptr); std::string TwinningAnalysisToText(const TwinningAnalysisResult& result); diff --git a/rugnux/ResultReport.cpp b/rugnux/ResultReport.cpp index 8c3902113..dcd03bc28 100644 --- a/rugnux/ResultReport.cpp +++ b/rugnux/ResultReport.cpp @@ -1830,12 +1830,15 @@ ReportDocument BuildReportDocument(const std::string &output_prefix, // NOT_READABLE where <|L|> reads outside the range any twin fraction gives: then neither // twinning nor a false operator is read from it. const bool abnormal = TwinStatisticsAbnormal(result); + // NO_TWIN_LAW: twin-like, but the lattice admits no twin law for the group (see + // TwinningAnalysisResult::twin_like_without_twin_law) - a question about the symmetry. Add(s, KeyEnum("TWINNING_VERDICT", abnormal ? "NOT_READABLE" : tw.adopted_operators_suspect ? "SYMMETRY_SUSPECT" : tw.twinning_suspected ? "INDICATED" + : tw.twin_like_without_twin_law ? "NO_TWIN_LAW" : "NO_INDICATION", - {"NO_INDICATION", "INDICATED", "SYMMETRY_SUSPECT", "NOT_READABLE"})); + {"NO_INDICATION", "INDICATED", "SYMMETRY_SUSPECT", "NO_TWIN_LAW", "NOT_READABLE"})); // The fraction comes from the statistic that carries the verdict, and the key beside it says // which. None is quoted where it cannot be measured: merged under a false operator, or from a // second moment a pseudo-translation inflates. @@ -1862,7 +1865,7 @@ ReportDocument BuildReportDocument(const std::string &output_prefix, Add(s, Prose(" Twinning: " + TwinVerdict(result) + ".")); if (!abnormal && tw.adopted_operators_suspect) Add(s, Prose(fmt::format( - " The Laue class is holohedral, so no twin law exists within it - but merging under a\n" + " The lattice admits no twin law for this group - but merging under a\n" " false operator gives exactly this distribution, whatever the twin fraction. {} Either\n" " the crystal has lower symmetry or it is a twin whose law the point group absorbed;\n" " refinement belongs in a subgroup.", @@ -1871,6 +1874,11 @@ ReportDocument BuildReportDocument(const std::string &output_prefix, : "The space group in use is higher than these intensities support."))); else if (!abnormal && tw.twinning_suspected) Add(s, Prose(" Refinement against these data needs a twin law.")); + else if (!abnormal && tw.twin_like_without_twin_law) + Add(s, Prose(" The lattice admits no twin law for this group, so these statistics cannot be a twin in it:\n" + " either the space group is too high - a twin of a subgroup whose law it absorbed, or\n" + " non-crystallographic symmetry taken for real - or something other than twinning\n" + " narrowed the intensity distribution.")); else if (tw.l_test_contaminated_by_tncs) // <|L|> is the number this sentence would otherwise rest on, and on this crystal it is // biased by the pseudo-translation above. @@ -1917,10 +1925,17 @@ ReportDocument BuildReportDocument(const std::string &output_prefix, } if (!abnormal && tw.adopted_operators_suspect) Warn(doc, PathologyCode::TWINNING, fmt::format( - "An adopted symmetry operator relates unequal intensities (<|L|> = {:.3f} in a holohedral " - "Laue class, where a genuine operator reads about 0.5 and a false one 0.375): the " - "space group may be too high, or the crystal is a twin whose law the point group " - "absorbed - refine in a subgroup", + "An adopted symmetry operator relates unequal intensities (<|L|> = {:.3f} where the " + "lattice admits no twin law, so a genuine operator reads about 0.5 and a false one " + "0.375): the space group may be too high, or the crystal is a twin whose law the " + "point group absorbed - refine in a subgroup", + tw.mean_abs_l)); + else if (!abnormal && tw.twin_like_without_twin_law) + Warn(doc, PathologyCode::TWINNING, fmt::format( + "The intensities read twin-like (<|L|> = {:.3f}), but the lattice admits no twin law " + "for this space group, so they cannot be a twin in it: the space group may be too " + "high (a twin of a subgroup whose law it absorbed, or non-crystallographic symmetry " + "taken for real), or something else narrowed the distribution", tw.mean_abs_l)); else if (!abnormal && tw.twinning_suspected) Warn(doc, PathologyCode::TWINNING, fmt::format( diff --git a/rugnux/RugnuxScaleMerge.cpp b/rugnux/RugnuxScaleMerge.cpp index 65d5b7d04..5a4f04d6f 100644 --- a/rugnux/RugnuxScaleMerge.cpp +++ b/rugnux/RugnuxScaleMerge.cpp @@ -2848,8 +2848,11 @@ bool Rugnux::ScaleMergeAndSymmetry(PipelineLocals &p) { const char twin_centring = twin_sg ? twin_sg->centring_type() : end_msg.rotation_lattice_type.has_value() ? end_msg.rotation_lattice_type->centering : 'P'; + const std::optional twin_cell = + result.consensus_cell ? std::optional(*result.consensus_cell) : std::nullopt; result.twinning = AnalyzeTwinning(sm.merged, twin_sg, 20, - TranslationalNCSVectorForLTest(result.tncs), twin_centring); + TranslationalNCSVectorForLTest(result.tncs), twin_centring, + twin_cell ? &*twin_cell : nullptr); // Set on the struct the text below is rendered from. AnalyzeTwinning returns a fresh result // with this flag clear, so setting it only afterwards left the statistics text asserting // "No twinning: the Laue class is holohedral" as a fact while the report, rendering the same diff --git a/rugnux/rugnux_cli.cpp b/rugnux/rugnux_cli.cpp index f370f5be5..6524fb581 100644 --- a/rugnux/rugnux_cli.cpp +++ b/rugnux/rugnux_cli.cpp @@ -2150,9 +2150,13 @@ static int RunRugnux(int argc, char **argv) { // Same as the full pipeline: without the reflections the lattice centring extinguishes. The // merged ASU of a centred group still holds keys in the absent class, and they are noise, not // intensities, in both statistics. + const auto twin_uc = experiment.GetUnitCell(); + const std::optional twin_cell = + twin_uc ? std::optional(*twin_uc) : std::nullopt; const auto twinning = AnalyzeTwinning(merged_reflections, twin_sg, 20, TranslationalNCSVectorForLTest(tncs), - twin_sg ? twin_sg->centring_type() : 'P'); + twin_sg ? twin_sg->centring_type() : 'P', + twin_cell ? &*twin_cell : nullptr); logger.Info("{}", TwinningAnalysisToText(twinning)); // Diffraction anisotropy, exactly as the full pipeline computes it (Rugnux.cpp) - no intensity diff --git a/tests/TwinningAnalysisTest.cpp b/tests/TwinningAnalysisTest.cpp index ed51ded0b..165ac5501 100644 --- a/tests/TwinningAnalysisTest.cpp +++ b/tests/TwinningAnalysisTest.cpp @@ -195,7 +195,8 @@ TEST_CASE("L-test on a P1 merge of a centred lattice", "[twinning]") { // A genuine holohedral crystal: no twin law exists, and its operators read as real. TEST_CASE("No twin is called in a genuine holohedral Laue class", "[twinning]") { const auto merged = MergedIn(Tetragonal("P 4 2 2", 0.0), "P 4 2 2"); - const auto r = AnalyzeTwinning(merged, gemmi::find_spacegroup_by_name("P 4 2 2")); + const gemmi::UnitCell tetragonal(47, 47, 63, 90, 90, 90); + const auto r = AnalyzeTwinning(merged, gemmi::find_spacegroup_by_name("P 4 2 2"), 20, nullptr, 'P', &tetragonal); CHECK_FALSE(r.merohedral_twinning_possible); CHECK(r.mean_abs_l == Catch::Approx(0.5).margin(0.03)); CHECK_FALSE(r.twinning_suspected); @@ -208,8 +209,9 @@ TEST_CASE("No twin is called in a genuine holohedral Laue class", "[twinning]") // law exists". TEST_CASE("A holohedral merge under a false operator is called suspect", "[twinning]") { const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 4 2 2"); + const gemmi::UnitCell tetragonal(47, 47, 63, 90, 90, 90); for (double alpha : {0.0, 0.2, 0.5}) { - const auto r = AnalyzeTwinning(MergedIn(Tetragonal("P 4", alpha), "P 4 2 2"), sg); + const auto r = AnalyzeTwinning(MergedIn(Tetragonal("P 4", alpha), "P 4 2 2"), sg, 20, nullptr, 'P', &tetragonal); CAPTURE(alpha); CHECK(r.mean_abs_l == Catch::Approx(0.375).margin(0.02)); CHECK(r.adopted_operators_suspect); @@ -218,6 +220,53 @@ TEST_CASE("A holohedral merge under a false operator is called suspect", "[twinn } } +// Whether a twin law can exist is read off the lattice, not off a list of Laue classes: a group whose +// lattice has a rotation the group lacks admits one, whatever the class is called. +TEST_CASE("A twin law is possible exactly where the lattice has a rotation the group lacks", "[twinning]") { + const auto possible = [](const char *group, const gemmi::UnitCell &cell) { + return TwinLawPossible(cell, gemmi::get_spacegroup_by_name(group)); + }; + // Merohedral: a tetragonal lattice under a group of lower Laue class than its holohedry. + CHECK(possible("P 4", gemmi::UnitCell(47, 47, 63, 90, 90, 90))); + CHECK_FALSE(possible("P 4 2 2", gemmi::UnitCell(47, 47, 63, 90, 90, 90))); + // Orthorhombic: no law on a general metric - mmm is the lattice's own holohedry - and a law where + // the C-centred cell is pseudo-hexagonal (b = a sqrt(3)). + CHECK_FALSE(possible("C 2 2 21", gemmi::UnitCell(56, 157, 143, 90, 90, 90))); + CHECK(possible("C 2 2 21", gemmi::UnitCell(50, 50 * std::sqrt(3.0), 143, 90, 90, 90))); + // Monoclinic: none on a general metric, one where beta is 90 (pseudo-merohedral). + CHECK_FALSE(possible("P 1 21 1", gemmi::UnitCell(75, 94, 99, 90, 93.1, 90))); + CHECK(possible("P 1 21 1", gemmi::UnitCell(55, 108, 55.3, 90, 90.0, 90))); + // ... and within the obliquity it is read at, a degree off still counts. + CHECK(possible("P 1 21 1", gemmi::UnitCell(54, 142, 84, 90, 91.0, 90))); +} + +// A twin-like <|L|> in a group the lattice admits no twin law for is not a twin in that group: it is +// reported as such, and not as "needs a twin law". Where the lattice does admit one, it is a twin. +TEST_CASE("Twin-like statistics where no twin law is possible", "[twinning]") { + const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 2 2 2"); + const auto merged = MergedIn(Tetragonal("P 2 2 2", 0.12), "P 2 2 2"); + const gemmi::UnitCell orthorhombic(47, 52, 63, 90, 90, 90); + const gemmi::UnitCell tetragonal(47, 47, 63, 90, 90, 90); + + const auto none = AnalyzeTwinning(merged, sg, 20, nullptr, 'P', &orthorhombic); + INFO("<|L|> " << none.mean_abs_l); + REQUIRE(none.mean_abs_l > 0.42); + REQUIRE(none.mean_abs_l < 0.44); + CHECK_FALSE(none.merohedral_twinning_possible); + CHECK_FALSE(none.twinning_suspected); + CHECK_FALSE(none.adopted_operators_suspect); + CHECK(none.twin_like_without_twin_law); + CHECK(TwinningVerdictLine(none).rfind("TWIN-LIKE, NO TWIN LAW", 0) == 0); + + const auto law = AnalyzeTwinning(merged, sg, 20, nullptr, 'P', &tetragonal); + CHECK(law.merohedral_twinning_possible); + CHECK(law.twinning_suspected); + CHECK_FALSE(law.twin_like_without_twin_law); + + // Without a cell nothing can be ruled out. + CHECK(AnalyzeTwinning(merged, sg).merohedral_twinning_possible); +} + namespace { // A P1 merge whose centric reflections are centric: SyntheticMergedReflections draws every // intensity from the acentric distribution, which is all the searches it was written for need, but