// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #include #include #include #include #include #include #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 PeakMap(const gemmi::UnitCell &cell, const gemmi::SpaceGroup *sg, const gemmi::Fractional &f, std::mt19937 &rng) { gemmi::Grid 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(10.0 * std::exp(-r2 / 2.0)); } grid.symmetrize_max(); std::normal_distribution 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 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 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 difference = PeakMap(cell, sg, f, rng); const gemmi::Grid 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 without = FindDensityBlobs(difference, 1.0, st, 2.0); REQUIRE(without.size() == 1); CHECK(std::isnan(without[0].anomalous_sigma)); const std::vector 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); }