Files
Jungfraujoch/tests/DensityBlobsTest.cpp
leonarski_fandClaude Opus 5.5 676979560f Unexplained blobs: the anomalous difference map's peak height at each, in the report and live
Each blob of the refined mFo-DFc map now carries the anomalous difference map's peak height
(|F+| - |F-| on the model's phases less 90 degrees, in its rms): its highest point within 1 A of
the blob's peak, not the value at the peak. The two maps are on grids of their own (up to ~0.7 A
apart at 2 A) and the anomalous scatterer need not be the difference density's highest point;
1 A, half the shortest metal-ligand bond, keeps a neighbouring modelled S or metal from being read
as the blob's own. NaN (null in _blobs.json) where the merge kept no Bijvoet pairs, instead of 0.
The ION rule reads the same value.

The report's blob list shows it only where the SUMMARY's Anomalous row says "signal present"
(AnomalousRow, factored out of the summary so both read one decision); without the signal it is
noise and is left out. --mode refine has no merge to judge on and leaves it out.

Live: rugnux_live_blob.anomalous_sigma in /rugnux/status (C++ model, python docs, frontend client
and Redoc regenerated); the Rugnux page's Details get a blob table with an "anom σ" column and the
structure viewer's peak label an "anom" reading, both only where anomalous_signal is set.

Tests: FindDensityBlobs_AnomalousPeakNearTheBlob (a scatterer 0.7 A off the difference peak read
at its own height; no map, no reading), Refinement_blob_anomalous_peak (a model missing a zinc and
a water, data with the zinc's f'': zinc blob 32 sigma, water -0.1), report and live checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UqXYdJDrM65gxsCn2PrbfK
2026-11-15 12:56:22 +01:00

107 lines
5.1 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include <catch2/catch_all.hpp>
#include <cmath>
#include <random>
#include <spdlog/fmt/fmt.h>
#include <gemmi/blob.hpp>
#include <gemmi/pdb.hpp>
#include "../image_analysis/structure_refinement/DensityBlobs.h"
namespace {
// A P 32 2 1 map in sigma units with one Gaussian peak at `f` and its symmetry mates, the mates equal to within
// 0.1 sigma as in a map from a Fourier transform (the live cytochrome C difference map: up to 0.06 sigma)
gemmi::Grid<float> PeakMap(const gemmi::UnitCell &cell, const gemmi::SpaceGroup *sg, const gemmi::Fractional &f,
std::mt19937 &rng) {
gemmi::Grid<float> grid;
grid.unit_cell = cell;
grid.spacegroup = sg;
grid.set_size(72, 72, 84);
size_t idx = 0;
for (int w = 0; w < grid.nw; w++)
for (int v = 0; v < grid.nv; v++)
for (int u = 0; u < grid.nu; u++, idx++) {
gemmi::Fractional d(double(u) / grid.nu - f.x, double(v) / grid.nv - f.y, double(w) / grid.nw - f.z);
d = gemmi::Fractional(d.x - std::round(d.x), d.y - std::round(d.y), d.z - std::round(d.z));
const double r2 = cell.orthogonalize_difference(d).length_sq();
grid.data[idx] = static_cast<float>(10.0 * std::exp(-r2 / 2.0));
}
grid.symmetrize_max();
std::normal_distribution<float> noise(0.0f, 0.1f);
for (float &v : grid.data)
v += noise(rng);
return grid;
}
}
TEST_CASE("FindDensityBlobs_SymmetryMatesOnce", "[DensityBlobs]") {
const gemmi::UnitCell cell(60, 60, 70, 90, 90, 120);
const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 32 2 1");
REQUIRE(sg != nullptr);
gemmi::BlobCriteria criteria;
criteria.cutoff = 3.0;
criteria.min_volume = 0.0;
criteria.min_score = 0.0;
criteria.min_peak = 4.0;
// Peaks over the whole cell, the asymmetric unit's faces among them: gemmi's flood fill finds some of those
// that cross a face twice - an edge point above the cutoff whose mate is just below it seeds the mate's copy.
std::mt19937 rng(1);
int found_twice = 0;
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
for (int k = 0; k < 8; k++) {
const gemmi::Fractional f(i / 8.0 + 0.01, j / 8.0 + 0.02, k / 8.0 + 0.005);
const gemmi::Grid<float> map = PeakMap(cell, sg, f, rng);
const gemmi::Position p = cell.orthogonalize(f);
const std::string pdb = fmt::format(
"CRYST1 60.000 60.000 70.000 90.00 90.00 120.00 P 32 2 1\n"
"HETATM 1 O HOH A 1 {:8.3f}{:8.3f}{:8.3f} 1.00 20.00 O\nEND\n",
p.x + 2.5, p.y, p.z);
const gemmi::Structure st = gemmi::read_pdb_string(pdb, "one water");
if (gemmi::find_blobs_by_flood_fill(map, criteria).size() > 1)
found_twice++;
const std::vector<DensityBlob> blobs = FindDensityBlobs(map, 1.0, st, 2.0);
INFO(fmt::format("peak at {:.3f} {:.3f} {:.3f}", f.x, f.y, f.z));
CHECK(blobs.size() == 1);
}
CHECK(found_twice > 0);
}
// The anomalous reading of a blob is the anomalous map's peak near the difference map's: an anomalous scatterer
// 0.7 A from the difference peak (the two maps on grids of their own, or the scatterer not the highest point of the
// blob) is read at its own height, not at the lower value its tail has at the difference peak. No map: no reading.
TEST_CASE("FindDensityBlobs_AnomalousPeakNearTheBlob", "[DensityBlobs]") {
const gemmi::UnitCell cell(60, 60, 70, 90, 90, 120);
const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name("P 32 2 1");
REQUIRE(sg != nullptr);
std::mt19937 rng(2);
const gemmi::Fractional f(0.31, 0.22, 0.13);
const gemmi::Position p = cell.orthogonalize(f);
const gemmi::Fractional g = cell.fractionalize(p + gemmi::Position(0.7, 0, 0));
const gemmi::Grid<float> difference = PeakMap(cell, sg, f, rng);
const gemmi::Grid<float> anomalous = PeakMap(cell, sg, g, rng);
const std::string pdb = fmt::format(
"CRYST1 60.000 60.000 70.000 90.00 90.00 120.00 P 32 2 1\n"
"HETATM 1 O HOH A 1 {:8.3f}{:8.3f}{:8.3f} 1.00 20.00 O\nEND\n",
p.x + 3.0, p.y, p.z);
const gemmi::Structure st = gemmi::read_pdb_string(pdb, "one water");
const std::vector<DensityBlob> without = FindDensityBlobs(difference, 1.0, st, 2.0);
REQUIRE(without.size() == 1);
CHECK(std::isnan(without[0].anomalous_sigma));
const std::vector<DensityBlob> with = FindDensityBlobs(difference, 1.0, st, 2.0, &anomalous, 1.0);
REQUIRE(with.size() == 1);
CHECK(anomalous.interpolate_value(p) < 8.5); // the Gaussian's tail at the difference peak: 10 exp(-0.245)
CHECK(with[0].anomalous_sigma > 9.0); // its own height, 10, sampled on the grid
CHECK(with[0].anomalous_sigma < 10.5);
CHECK(with[0].kind == DensityBlob::Kind::Ion);
}