A rugnux live run that placed a model keeps it, with a copy of its map coefficients, beside the live status (RugnuxStructure), for as long as /rugnux/status keeps the run. Made in memory on the first request and cached, never written to disk: - GET /rugnux/model.cif: the placed model as mmCIF, labelled as WritePlacedModel labels _model.cif (PlacedModelMmcif, the labelling factored out of WritePlacedModel; the file output is unchanged). - GET /rugnux/map.ccp4?type=2fofc|fofc: the 2mFo-DFc / mFo-DFc map as a CCP4 map, FFT of the FWT/PHWT or DELFWT/PHDELWT coefficients (get_f_phi_on_grid + MapFromFPhi, sampled as ModelValidation samples), cut to the box around the model plus 5 A, with the whole cell's mean and RMS in the header. - 404 without a placed model or maps, 400 for an unknown map type; both routes protected by the dataset bearer token like /rugnux/status. rugnux_live_status gets placed_model. C++ model, python client docs, TypeScript client and redoc regenerated. Tests: a point's coefficients give a map peaking at the atom in a cropped box; the mmCIF; the structure is cleared at the next start; the auth test covers the new routes. End to end (myoglobin 2M replay with an AlphaFold model): the maps equal gemmi's FFT of the written _maps.mtz to 2.5e-6 rms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015CeEGgpKdfkCTJb3YNMJcu (cherry picked from commit 33124c3288ba112def3f51c0d91f5206886f3495)
82 lines
3.3 KiB
C++
82 lines
3.3 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 <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();
|
|
}
|
|
|
|
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)
|
|
return {};
|
|
std::unique_lock ul(m);
|
|
std::optional<std::string> &cached = (type == RugnuxMapType::TwoFoFc) ? map_2fofc : map_fofc;
|
|
if (cached)
|
|
return *cached;
|
|
|
|
const MtzTable &mtz = *coefficients;
|
|
const auto &h = FindColumn(mtz, "H"), &k = FindColumn(mtz, "K"), &l = FindColumn(mtz, "L");
|
|
const auto &f = FindColumn(mtz, type == RugnuxMapType::TwoFoFc ? "FWT" : "DELFWT");
|
|
const auto &phi = FindColumn(mtz, type == RugnuxMapType::TwoFoFc ? "PHWT" : "PHDELWT");
|
|
|
|
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++)
|
|
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;
|
|
map.set_extent(gemmi::calculate_fractional_box(st, MAP_MARGIN_A));
|
|
|
|
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;
|
|
}
|