From 59a2f46d150b00ff752746b4725acb3b8ea089af Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Fri, 20 Nov 2026 22:33:04 +0100 Subject: [PATCH] Merged MTZ: multiplicity columns N, N(+) and N(-) The merged MTZ carries the number of observations merged into each value: N (type I) after SIGIMEAN, N(+)/N(-) after SIGI(-) where the anomalous columns are written - the labels and the integer type dials.merge gives them. The merged reflection carries the counts from the half-set accumulators (rotation and on-the-fly merges) and per hand from the anomalous table; the two places that swap the Bijvoet hands on a reindexing swap the counts with them. Centrics, which have no anomalous split, carry N(+) = N(-) = 0. Every existing column keeps its label, type and value. The synthetic merged generator carries the multiplicity it simulates and the writer tests check the new columns; the MTZ column lists in the docs are updated. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01HVeDQrPqZ52Tp6rzzZGxf3 (cherry picked from commit 01a24d7b7b37f6263a950cc56af069448922df48) (cherry picked from commit 441f020a2b8896f957b87bd6b70e1ad1663db64b) --- common/Reflection.h | 4 ++++ docs/CPU_DATA_ANALYSIS_DECISIONS.md | 2 +- docs/RUGNUX_INTEGRATION.md | 7 +++--- docs/RUGNUX_TUTORIAL.md | 4 ++-- image_analysis/WriteReflections.cpp | 24 ++++++++++++------- image_analysis/scale_merge/Merge.cpp | 3 ++- .../scale_merge/ReindexAmbiguity.cpp | 1 + .../scale_merge/RotationScaleMerge.cpp | 6 ++++- .../structure_refinement/Refinement.cpp | 1 + tests/RugnuxResultFilesTest.cpp | 10 ++++---- tests/SyntheticMergedReflections.h | 1 + tests/WriteReflectionsTest.cpp | 9 ++++++- 12 files changed, 49 insertions(+), 23 deletions(-) diff --git a/common/Reflection.h b/common/Reflection.h index 6f2d45b37..7946f2aac 100644 --- a/common/Reflection.h +++ b/common/Reflection.h @@ -112,6 +112,10 @@ struct MergedReflection { // written - the ring contaminates it, it does not make it absent - and this only marks it so // a consumer that must not read the ring as crystal signal can leave it out. bool on_ice_ring = false; + // Observations merged into I, and into each Bijvoet hand of it (the MTZ N, N(+), N(-) columns). + int32_t n_obs = 0; + int32_t n_plus = 0; + int32_t n_minus = 0; bool rfree_flag = false; float F = NAN; // French-Wilson amplitude |F| (filled by ApplyFrenchWilson at end of merge) float sigmaF = NAN; // its sigma diff --git a/docs/CPU_DATA_ANALYSIS_DECISIONS.md b/docs/CPU_DATA_ANALYSIS_DECISIONS.md index 6e72d2f07..cceac4d7b 100644 --- a/docs/CPU_DATA_ANALYSIS_DECISIONS.md +++ b/docs/CPU_DATA_ANALYSIS_DECISIONS.md @@ -124,7 +124,7 @@ Everything lands in `_report.txt` section 4 (`ANISOTROPY_*` keys), in th - **Space-group symmetry** beyond centering absences is not enforced during prediction/integration unless the space group is supplied and used downstream. - **Resolution masking** is controllable, and so is every stage of ice-ring handling (§3.3, §10.10). None of it runs unless the crystal is measured to have ice, because the fixed bands are a fixed cost in unique reflections whether it does or not. - **Rotation vs still modes** differ substantially in prediction and scaling: partiality is angle-driven in rotation data, while stills are predicted within an excitation-error window that follows the run's rocking curve (§8.2) and get their partiality from the default-on per-crystal tilt post-refinement (§10.2) — or unit partiality with `--simple-stills`. -- **Amplitudes and intensities.** The merged output carries both intensities (mmCIF `intensity_meas`, MTZ `IMEAN`/`SIGIMEAN`) and French–Wilson amplitudes (mmCIF `F_meas_au`, MTZ `F`/`SIGF`; §10.8), so a downstream program can refine against either. +- **Amplitudes and intensities.** The merged output carries both intensities (mmCIF `intensity_meas`, MTZ `IMEAN`/`SIGIMEAN`, with the multiplicity in `N`) and French–Wilson amplitudes (mmCIF `F_meas_au`, MTZ `F`/`SIGF`; §10.8), so a downstream program can refine against either. --- diff --git a/docs/RUGNUX_INTEGRATION.md b/docs/RUGNUX_INTEGRATION.md index d07ece54f..11cc30212 100644 --- a/docs/RUGNUX_INTEGRATION.md +++ b/docs/RUGNUX_INTEGRATION.md @@ -65,11 +65,12 @@ the `.hkl` worth knowing before using it: column types CCP4 programs dispatch on: ``` -H K L IMEAN SIGIMEAN I(+) SIGI(+) I(-) SIGI(-) F SIGF F(+) SIGF(+) F(-) SIGF(-) FreeR_flag -H H H J Q K M K M F Q G L G L I +H K L IMEAN SIGIMEAN N I(+) SIGI(+) I(-) SIGI(-) N(+) N(-) F SIGF F(+) SIGF(+) F(-) SIGF(-) FreeR_flag +H H H J Q I K M K M I I F Q G L G L I ``` -`F` is the French–Wilson amplitude. The header carries the determined space group, the refined cell +`F` is the French–Wilson amplitude; `N` is the number of observations merged into `IMEAN`, and +`N(+)`/`N(-)` into each Bijvoet hand (the multiplicity columns `dials.merge` writes). The header carries the determined space group, the refined cell and the wavelength, on a dataset of its own behind the reserved `HKL_base`, which is where the MTZ format puts them. `_unmerged.mtz` reads 1.54187 Å (CCP4's Cu Kα default) under `mtzinfo` and is not wrong: its columns sit on `HKL_base` deliberately, as POINTLESS expects, and the diff --git a/docs/RUGNUX_TUTORIAL.md b/docs/RUGNUX_TUTORIAL.md index ddc052d34..dcda19a53 100644 --- a/docs/RUGNUX_TUTORIAL.md +++ b/docs/RUGNUX_TUTORIAL.md @@ -304,8 +304,8 @@ Absorption is corrected only by the empirical surface a rotation run fits and ke - Merging is **on by default** (`--no-merge` disables it). The merged reflections are written in **three** formats — each has its uses downstream: - `.mtz` — CCP4 MTZ for the CCP4 / phenix reflection tools. The columns are - `H K L IMEAN SIGIMEAN I(+) SIGI(+) I(-) SIGI(-) F SIGF F(+) SIGF(+) F(-) SIGF(-) FreeR_flag` - (`F` is the French–Wilson amplitude); the anomalous columns are present whenever any reflection + `H K L IMEAN SIGIMEAN N I(+) SIGI(+) I(-) SIGI(-) N(+) N(-) F SIGF F(+) SIGF(+) F(-) SIGF(-) FreeR_flag` + (`F` is the French–Wilson amplitude, `N` the number of observations merged into each value); the anomalous columns are present whenever any reflection carries a Bijvoet split — a rotation run always does, a stills run only with `-A`. - `.cif` — mmCIF, for deposition and as the self-describing native format (also carries the merging statistics, ISa, twinning and radiation-damage indicators). diff --git a/image_analysis/WriteReflections.cpp b/image_analysis/WriteReflections.cpp index e36b5dfff..8d0b3f5a7 100644 --- a/image_analysis/WriteReflections.cpp +++ b/image_analysis/WriteReflections.cpp @@ -205,6 +205,7 @@ struct MergedOutRow { int h = 0, k = 0, l = 0; float Imean = NAN, sImean = NAN, Ip = NAN, sIp = NAN, Im = NAN, sIm = NAN; float Fmean = NAN, sFmean = NAN, Fp = NAN, sFp = NAN, Fm = NAN, sFm = NAN; + int n = 0, np = 0, nm = 0; // observations merged into Imean, Ip and Im // The value written into FreeR_flag: 0 for the TEST set, 1 for the working set. That is the CCP4 // convention the column NAME belongs to - CCP4's own freerflag writes 0..19 with 0 as the ~5% // test bin, and REFMAC's default FREE 0 reads it that way, so the opposite (phenix/CNS) numbering @@ -233,7 +234,7 @@ std::vector BuildMergedRows(const std::vector &r for (const auto& r : reflections) out_rows.push_back({r.h, r.k, r.l, r.I, r.sigma, r.I_plus, r.sigma_plus, r.I_minus, r.sigma_minus, r.F, r.sigmaF, r.F_plus, r.sigmaF_plus, r.F_minus, - r.sigmaF_minus, r.rfree_flag ? 0 : 1}); + r.sigmaF_minus, r.n_obs, r.n_plus, r.n_minus, r.rfree_flag ? 0 : 1}); } else { // Anomalous: group the two mates by their (shared) Friedel-merged ASU representative, then form // IMEAN / F as their inverse-variance Friedel mean. A single generator gives both the group key @@ -243,6 +244,7 @@ std::vector BuildMergedRows(const std::vector &r int h = 0, k = 0, l = 0; float Ip = NAN, sIp = NAN, Im = NAN, sIm = NAN; float Fp = NAN, sFp = NAN, Fm = NAN, sFm = NAN; + int np = 0, nm = 0; int rfree = 1; }; std::map, AnomRow> rows; @@ -251,8 +253,8 @@ std::vector BuildMergedRows(const std::vector &r AnomRow& row = rows[{key.h, key.k, key.l}]; row.h = key.h; row.k = key.k; row.l = key.l; row.rfree = r.rfree_flag ? 0 : 1; - if (key.plus) { row.Ip = r.I; row.sIp = r.sigma; row.Fp = r.F; row.sFp = r.sigmaF; } - else { row.Im = r.I; row.sIm = r.sigma; row.Fm = r.F; row.sFm = r.sigmaF; } + if (key.plus) { row.Ip = r.I; row.sIp = r.sigma; row.Fp = r.F; row.sFp = r.sigmaF; row.np = r.n_obs; } + else { row.Im = r.I; row.sIm = r.sigma; row.Fm = r.F; row.sFm = r.sigmaF; row.nm = r.n_obs; } } // Friedel-mean of the two mates by inverse variance (the single mate, if only one was measured). @@ -275,7 +277,8 @@ std::vector BuildMergedRows(const std::vector &r combine(row.Ip, row.sIp, row.Im, row.sIm, i_mean, sig_i_mean); combine(row.Fp, row.sFp, row.Fm, row.sFm, f_mean, sig_f_mean); out_rows.push_back({row.h, row.k, row.l, i_mean, sig_i_mean, row.Ip, row.sIp, row.Im, - row.sIm, f_mean, sig_f_mean, row.Fp, row.sFp, row.Fm, row.sFm, row.rfree}); + row.sIm, f_mean, sig_f_mean, row.Fp, row.sFp, row.Fm, row.sFm, + row.np + row.nm, row.np, row.nm, row.rfree}); } } return out_rows; @@ -665,13 +668,16 @@ MtzTable MergedMtzTable(const std::vector &reflections, {"native", experiment.GetSampleName(), "native", experiment.GetWavelength_A()}}; // One row per reflection in the CCP4 anomalous layout (IMEAN + I(+)/I(-), and the same split for - // the French-Wilson amplitude), which aimless / ctruncate / mtz2sca / ANODE read directly. + // the French-Wilson amplitude), which aimless / ctruncate / mtz2sca / ANODE read directly. N is the + // number of observations merged into IMEAN and N(+)/N(-) into each hand, the labels and the integer + // type dials.merge gives them. bool has_anom = true; const std::vector out_rows = BuildMergedRows(reflections, experiment, has_anom); std::vector> columns{{"H", 'H'}, {"K", 'H'}, {"L", 'H'}, - {"IMEAN", 'J'}, {"SIGIMEAN", 'Q'}}; + {"IMEAN", 'J'}, {"SIGIMEAN", 'Q'}, {"N", 'I'}}; if (has_anom) - columns.insert(columns.end(), {{"I(+)", 'K'}, {"SIGI(+)", 'M'}, {"I(-)", 'K'}, {"SIGI(-)", 'M'}}); + columns.insert(columns.end(), {{"I(+)", 'K'}, {"SIGI(+)", 'M'}, {"I(-)", 'K'}, {"SIGI(-)", 'M'}, + {"N(+)", 'I'}, {"N(-)", 'I'}}); columns.insert(columns.end(), {{"F", 'F'}, {"SIGF", 'Q'}}); // F: the French-Wilson amplitude if (has_anom) columns.insert(columns.end(), {{"F(+)", 'G'}, {"SIGF(+)", 'L'}, {"F(-)", 'G'}, {"SIGF(-)", 'L'}}); @@ -680,9 +686,9 @@ MtzTable MergedMtzTable(const std::vector &reflections, m.columns.push_back({label, type, type == 'H' ? 0 : 1, {}}); for (const auto &row : out_rows) { std::vector v{static_cast(row.h), static_cast(row.k), static_cast(row.l), - row.Imean, row.sImean}; + row.Imean, row.sImean, static_cast(row.n)}; if (has_anom) - v.insert(v.end(), {row.Ip, row.sIp, row.Im, row.sIm}); + v.insert(v.end(), {row.Ip, row.sIp, row.Im, row.sIm, static_cast(row.np), static_cast(row.nm)}); v.insert(v.end(), {row.Fmean, row.sFmean}); if (has_anom) v.insert(v.end(), {row.Fp, row.sFp, row.Fm, row.sFm}); diff --git a/image_analysis/scale_merge/Merge.cpp b/image_analysis/scale_merge/Merge.cpp index 6ee398eb2..cf69a1229 100644 --- a/image_analysis/scale_merge/Merge.cpp +++ b/image_analysis/scale_merge/Merge.cpp @@ -568,7 +568,8 @@ std::vector MergeOnTheFly::ExportIntensities() { .I_half = {NAN, NAN}, .sigma_half = {NAN, NAN}, .d = accum.d, - .on_ice_ring = accum.on_ice_ring + .on_ice_ring = accum.on_ice_ring, + .n_obs = static_cast(accum.n_half[0] + accum.n_half[1]) }; if (accum.exists_half[0] && accum.exists_half[1]) { diff --git a/image_analysis/scale_merge/ReindexAmbiguity.cpp b/image_analysis/scale_merge/ReindexAmbiguity.cpp index 8089346dc..53b1c60e4 100644 --- a/image_analysis/scale_merge/ReindexAmbiguity.cpp +++ b/image_analysis/scale_merge/ReindexAmbiguity.cpp @@ -101,6 +101,7 @@ std::vector ReindexMergedIntoAsu(const std::vector(a.nh[0] + a.nh[1]); if (a.nh[0] + a.nh[1] > 0 && a.swh[0] > 0.0 && a.swh[1] > 0.0) { for (int i = 0; i < 2; ++i) { mr.I_half[i] = static_cast(a.swIh[i] / a.swh[i]); @@ -6662,7 +6663,7 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool // N_uniq. ---- sub("statistics"); const int n_shells = report_shell_count; - struct AnomExport { float Ip = NAN, sIp = NAN, Im = NAN, sIm = NAN; }; + struct AnomExport { float Ip = NAN, sIp = NAN, Im = NAN, sIm = NAN; int32_t np = 0, nm = 0; }; std::unordered_map anom_export; // filled by the run's own table only auto statistics_over = [&](const std::vector &merged, float grid_d_min, float grid_d_max, float d_max_label, double d_floor, @@ -7031,6 +7032,8 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool const double swp = a.sw[0][0] + a.sw[0][1], swm = a.sw[1][0] + a.sw[1][1]; const double swIp = a.swI[0][0] + a.swI[0][1], swIm = a.swI[1][0] + a.swI[1][1]; AnomExport ex; + ex.np = a.nh[0][0] + a.nh[0][1]; + ex.nm = a.nh[1][0] + a.nh[1][1]; if (swp > 0.0) { ex.Ip = static_cast(swIp / swp); ex.sIp = static_cast(1.0 / std::sqrt(swp)); } if (swm > 0.0) { ex.Im = static_cast(swIm / swm); ex.sIm = static_cast(1.0 / std::sqrt(swm)); } if (own_table && (std::isfinite(ex.Ip) || std::isfinite(ex.Im))) anom_export[fkey] = ex; @@ -7275,6 +7278,7 @@ RotationScaleMerge::Result RotationScaleMerge::MergeAndStats(int n_groups, bool if (it == anom_export.end()) continue; r.I_plus = it->second.Ip; r.sigma_plus = it->second.sIp; r.I_minus = it->second.Im; r.sigma_minus = it->second.sIm; + r.n_plus = it->second.np; r.n_minus = it->second.nm; } } diff --git a/image_analysis/structure_refinement/Refinement.cpp b/image_analysis/structure_refinement/Refinement.cpp index ca17fdff9..86024f63c 100644 --- a/image_analysis/structure_refinement/Refinement.cpp +++ b/image_analysis/structure_refinement/Refinement.cpp @@ -4938,6 +4938,7 @@ std::vector ExpandToSubgroup(const std::vector END blocks -> CBOR -> writer, WritePlacedModel(WithJungfraujoch(TestModel(), gemmi::SoftwareItem::Phasing), TEST_CELL, experiment.GetSpaceGroupOrP1(), original, logger); end.placed_model = PlacedModelFromStructure(TestModel(), TEST_CELL, experiment.GetSpaceGroupOrP1().xhm()); - REQUIRE(end.merged->columns.size() == 16); // with the anomalous columns + REQUIRE(end.merged->columns.size() == 19); // with the anomalous columns and the three N CHECK(end.merged->columns[0].values.size() == reflections.size()); CHECK(end.placed_model->atom_name.size() == 2 * 3 * 6 + 2); @@ -195,9 +195,9 @@ TEST_CASE("RugnuxResultFiles: rugnux's outputs -> END blocks -> CBOR -> writer, CHECK(ReadFile(written + "_model.pdb") == ReadFile(original + "_model.pdb")); const gemmi::Mtz mtz = gemmi::read_mtz_file(written + ".mtz"); - CHECK(Labels(mtz) == std::vector{"H", "K", "L", "IMEAN", "SIGIMEAN", "I(+)", "SIGI(+)", "I(-)", - "SIGI(-)", "F", "SIGF", "F(+)", "SIGF(+)", "F(-)", "SIGF(-)", - "FreeR_flag"}); + CHECK(Labels(mtz) == std::vector{"H", "K", "L", "IMEAN", "SIGIMEAN", "N", "I(+)", "SIGI(+)", "I(-)", + "SIGI(-)", "N(+)", "N(-)", "F", "SIGF", "F(+)", "SIGF(+)", "F(-)", + "SIGF(-)", "FreeR_flag"}); CHECK(mtz.nreflections == static_cast(reflections.size())); CHECK(mtz.datasets.back().wavelength == Catch::Approx(experiment.GetWavelength_A()).epsilon(1e-5)); const gemmi::Mtz maps_mtz = gemmi::read_mtz_file(written + "_maps.mtz"); @@ -236,7 +236,7 @@ TEST_CASE("RugnuxResultFiles: an existing file is kept when overwrite is off", " WriteRugnuxResultFiles(end, prefix, true, logger); const gemmi::Mtz mtz = gemmi::read_mtz_file(prefix + ".mtz"); CHECK(mtz.nreflections == 1); - CHECK(Labels(mtz) == std::vector{"H", "K", "L", "IMEAN", "SIGIMEAN", "F", "SIGF", "FreeR_flag"}); + CHECK(Labels(mtz) == std::vector{"H", "K", "L", "IMEAN", "SIGIMEAN", "N", "F", "SIGF", "FreeR_flag"}); std::filesystem::remove_all(dir); } diff --git a/tests/SyntheticMergedReflections.h b/tests/SyntheticMergedReflections.h index 395c487a5..4e8d69477 100644 --- a/tests/SyntheticMergedReflections.h +++ b/tests/SyntheticMergedReflections.h @@ -224,6 +224,7 @@ namespace jfjoch_test { const double i_merged = sum_n_i / n_obs; r.I = static_cast(i_merged); + r.n_obs = n_obs; // The error model on one observation, averaged down by the merge, then thrown off // by the error-model miscalibration. r.sigma = static_cast(std::hypot(sigma_one, p.error_model_b * i_merged) / diff --git a/tests/WriteReflectionsTest.cpp b/tests/WriteReflectionsTest.cpp index c47f310a0..289ec395a 100644 --- a/tests/WriteReflectionsTest.cpp +++ b/tests/WriteReflectionsTest.cpp @@ -63,12 +63,19 @@ TEST_CASE("Merged MTZ: the data dataset is id 1 and carries the wavelength", "[w // The wavelength is read off the dataset the data columns belong to, so they have to be on the // data dataset and not on the base. - for (const char *label : {"IMEAN", "SIGIMEAN", "F", "SIGF", "FreeR_flag"}) { + for (const char *label : {"IMEAN", "SIGIMEAN", "N", "F", "SIGF", "FreeR_flag"}) { const gemmi::Mtz::Column *col = mtz.column_with_label(label); REQUIRE(col != nullptr); CHECK(col->dataset_id == 1); } + // N is the number of observations merged into IMEAN, an integer column as dials.merge writes it. + const gemmi::Mtz::Column *n_col = mtz.column_with_label("N"); + CHECK(n_col->type == 'I'); + for (size_t i = 0; i < reflections.size(); i++) + CHECK(mtz.data[i * mtz.columns.size() + n_col->idx] == static_cast(reflections[i].n_obs)); + CHECK(reflections.front().n_obs == std::max(2, params.multiplicity)); + // Cell and space group travel in the same header. CHECK(mtz.spacegroup != nullptr); CHECK(mtz.spacegroup->number == 96);