Build Packages / Create release (push) Successful in 8s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m11s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 10m32s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 18m26s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 22m11s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 23m32s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 25m36s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 27m28s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 28m10s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 28m10s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 29m24s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 21m45s
Build Packages / Generate python client (push) Successful in 31s
Build Packages / Build documentation (push) Successful in 1m21s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 24m2s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 29m25s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 30m27s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 27m12s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 32m20s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 33m36s
Build Packages / Unit tests (push) Successful in 2h7m18s
Build Packages / Validation Battery (push) Successful in 33m19s
* Rugnux: Stills processing needs only the unit cell and space group beyond what the file records - detector distance, beam centre, tilt, panel placement, saturation level, gain and polarisation plane are found from the images - and is up to twice as fast. * Rugnux: Stills can be indexed without a GPU, with a CPU implementation of the fast-feedback indexer (macOS, computers without a GPU). * Rugnux: Rotation and stills data are integrated in the detector's own units with one gain model, improving data from CCD detectors. * Web frontend: The Rugnux live page shows the placed model with its electron density and anomalous difference map in a Mol* viewer. Reviewed-on: #90 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
104 lines
4.4 KiB
C++
104 lines
4.4 KiB
C++
// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "RugnuxStructure.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <complex>
|
|
#include <cstring>
|
|
|
|
#include "gemmi/asudata.hpp"
|
|
#include "gemmi/calculate.hpp" // calculate_fractional_box
|
|
#include "gemmi/ccp4.hpp"
|
|
#include "gemmi/fourier.hpp" // get_size_for_hkl, get_f_phi_on_grid
|
|
|
|
#include "../image_analysis/structure_refinement/ModelFFT.h"
|
|
#include "../writer/WriteModel.h"
|
|
|
|
namespace {
|
|
const MtzTable::Column &FindColumn(const MtzTable &mtz, const std::string &label) {
|
|
for (const auto &c: mtz.columns)
|
|
if (c.label == label)
|
|
return c;
|
|
throw std::runtime_error("map coefficients have no column " + label);
|
|
}
|
|
}
|
|
|
|
RugnuxStructure::RugnuxStructure(std::shared_ptr<const gemmi::Structure> model, const UnitCell &cell,
|
|
const gemmi::SpaceGroup &space_group, std::optional<MtzTable> coefficients)
|
|
: model(std::move(model)), cell(cell), space_group(space_group), coefficients(std::move(coefficients)) {}
|
|
|
|
bool RugnuxStructure::HasMaps() const {
|
|
return coefficients.has_value();
|
|
}
|
|
|
|
bool RugnuxStructure::HasAnomalousMap() const {
|
|
if (!coefficients)
|
|
return false;
|
|
for (const auto &c: coefficients->columns)
|
|
if (c.label == "ANOM")
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
std::string RugnuxStructure::ModelCif() const {
|
|
std::unique_lock ul(m);
|
|
if (!model_cif)
|
|
model_cif = PlacedModelMmcif(*model, cell, space_group);
|
|
return *model_cif;
|
|
}
|
|
|
|
std::string RugnuxStructure::Map(RugnuxMapType type) const {
|
|
if (!coefficients || (type == RugnuxMapType::Anomalous && !HasAnomalousMap()))
|
|
return {};
|
|
std::unique_lock ul(m);
|
|
std::optional<std::string> &cached = type == RugnuxMapType::TwoFoFc ? map_2fofc
|
|
: type == RugnuxMapType::FoFc ? map_fofc : map_anom;
|
|
if (cached)
|
|
return *cached;
|
|
|
|
const MtzTable &mtz = *coefficients;
|
|
const auto &h = FindColumn(mtz, "H"), &k = FindColumn(mtz, "K"), &l = FindColumn(mtz, "L");
|
|
const char *f_label = type == RugnuxMapType::TwoFoFc ? "FWT" : type == RugnuxMapType::FoFc ? "DELFWT" : "ANOM";
|
|
const char *phi_label = type == RugnuxMapType::TwoFoFc ? "PHWT" : type == RugnuxMapType::FoFc ? "PHDELWT" : "PHANOM";
|
|
const auto &f = FindColumn(mtz, f_label);
|
|
const auto &phi = FindColumn(mtz, phi_label);
|
|
|
|
const gemmi::SpaceGroup *sg = gemmi::find_spacegroup_by_name(mtz.space_group_name);
|
|
if (sg == nullptr)
|
|
throw std::runtime_error("unknown space group '" + mtz.space_group_name + "' of the map coefficients");
|
|
gemmi::AsuData<std::complex<float>> coef;
|
|
coef.unit_cell_ = mtz.unit_cell;
|
|
coef.spacegroup_ = sg;
|
|
for (size_t i = 0; i < h.values.size(); i++)
|
|
if (std::isfinite(f.values[i]) && std::isfinite(phi.values[i])) // missing: no Bijvoet pair (ANOM)
|
|
coef.v.push_back({{static_cast<int>(h.values[i]), static_cast<int>(k.values[i]), static_cast<int>(l.values[i])},
|
|
std::polar(f.values[i], phi.values[i] * static_cast<float>(M_PI / 180.0))});
|
|
coef.ensure_sorted();
|
|
// Sampled as ModelValidation samples its maps.
|
|
const std::array<int, 3> size = gemmi::get_size_for_hkl(coef, {{0, 0, 0}}, 3.0);
|
|
|
|
gemmi::Ccp4<float> map;
|
|
map.grid = MapFromFPhi(gemmi::get_f_phi_on_grid<float>(coef, size, true));
|
|
map.update_ccp4_header(2, true); // the statistics of the whole cell
|
|
|
|
gemmi::Structure st = *model;
|
|
st.cell = mtz.unit_cell;
|
|
const gemmi::Box<gemmi::Fractional> model_box = gemmi::calculate_fractional_box(st, MAP_MARGIN_A);
|
|
gemmi::Box<gemmi::Fractional> box;
|
|
for (int i = 0; i < 3; i++) {
|
|
const double centre = (model_box.minimum.at(i) + model_box.maximum.at(i)) / 2;
|
|
const double half = std::max(0.5, (model_box.maximum.at(i) - model_box.minimum.at(i)) / 2);
|
|
box.minimum.at(i) = centre - half;
|
|
box.maximum.at(i) = centre + half - 1e-9; // one cell is exactly one cell of grid points
|
|
}
|
|
map.set_extent(box);
|
|
|
|
std::string out(4 * map.ccp4_header.size() + sizeof(float) * map.grid.data.size(), '\0');
|
|
memcpy(out.data(), map.ccp4_header.data(), 4 * map.ccp4_header.size());
|
|
memcpy(out.data() + 4 * map.ccp4_header.size(), map.grid.data.data(), sizeof(float) * map.grid.data.size());
|
|
cached = std::move(out);
|
|
return *cached;
|
|
}
|