Files
Jungfraujoch/writer/WriteModel.cpp
T
leonarski_fandFilip Leonarski 19b135186d
Build Packages / Create release (push) Successful in 7s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 4m56s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 9m10s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 13m51s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 16m56s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 20m59s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 21m25s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 21m17s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 14m14s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 25m59s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 25m48s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 15m52s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 19m33s
Build Packages / Generate python client (push) Successful in 29s
Build Packages / Build documentation (push) Successful in 1m19s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 20m49s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 25m31s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 25m24s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 26m5s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 22m19s
Build Packages / Unit tests (push) Failing after 2h28m24s
Build Packages / Validation Battery (push) Successful in 40m33s
v1.0.0-rc.175 (#85)
* Rugnux: Processes a collection live while jfjoch_broker writes it (`rugnux <master.h5> <broker URL>`).
* Rugnux: Processing starts on the first images and confirms its choices once all are in, so analysis can begin during collection.
* Rugnux: Faster processing, especially on servers with several GPUs and CPU sockets and on computers without a GPU (`--no-gpu`, macOS).
* Rugnux: Uses much less GPU memory, so large data sets stay on the GPU of smaller cards, and a data set too large for the card is processed on the CPU instead.
* Rugnux: On macOS, Bragg integration runs on the Apple GPU.
* Rugnux: Better handling of twinning and pseudo-symmetry, with the twin hypothesis written as `<prefix>_twin_<SG>.mtz`, and a wrong `--model` no longer confirms the data or decides the hand.
* Rugnux: `--model` accepts predicted (AlphaFold) models and falls back to molecular replacement when the model is not placed as a rigid body (experimental `--mr` forces it), settling space group, hand and twinning and writing a different answer as `<prefix>_mr.mtz`.
* Rugnux: Molecular replacement is faster and settles the space group in more cases with several copies in the asymmetric unit.
* Rugnux: Deposition-ready mmCIF, a richer report, and numbered steps on the terminal with the full log in `<prefix>_log.txt`; `_P1.mtz`, `--estimate-beam-center` and `--beam-center-search` are removed.
* Rugnux: The beam-stop shadow is found correctly on tilted detectors.
* Rugnux: Stills files that record a stationary goniometer axis get the default stills geometry refinement.
* jfjoch_viewer: "Analyze dataset" uses the same defaults as a bare `rugnux <file>`, and shows the results of processed CBF, marCCD and SMV sweeps.
* jfjoch_viewer: The Files panel refreshes on F5 and every few seconds, and the plot type and other selections stay put during live collection.
* jfjoch_writer: Memory stays bounded and old data files are closed efficiently on long runs, any failure ends the run in an error state the broker is told about, and the master file appears only after its data files; the ZeroMQ pusher logs when a stalled writer makes it drop images.
* Fixes: The TCP image pusher no longer blocks the broker's receiver, GPU out-of-memory failures stop with a clear message, and the XDS plugin masks module and chip edges.

---------

Co-authored-by: Filip Leonarski <jungfrau@filips-mac-mini.home>
Reviewed-on: #85
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-10-16 15:34:43 +02:00

103 lines
5.6 KiB
C++

// SPDX-FileCopyrightText: 2026 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
// SPDX-License-Identifier: GPL-3.0-only
#include "WriteModel.h"
#include <algorithm>
#include <fstream>
#include <gemmi/to_mmcif.hpp> // make_mmcif_document
#include <gemmi/to_cif.hpp> // write_cif_to_stream
#include <gemmi/to_pdb.hpp> // write_pdb
#include <gemmi/polyheur.hpp> // setup_entities
#include "../common/Logger.h"
namespace {
// Do the coordinates of a model built in `coordinates_group` obey `label` as well? They do when every
// operation of `label` is one of theirs, i.e. when `label` is a subgroup: the file then holds more
// than one asymmetric unit of it, which is allowed and is what a model always does in P1. They do not
// when `label` has an operation of its own, and then the label is a lie a reader acts on - it
// generates atoms from that operation, and the crystal it builds is not the one in the file.
bool coordinates_obey(const gemmi::SpaceGroup &coordinates_group, const gemmi::SpaceGroup &label) {
const std::vector<gemmi::Op> coordinate_ops = coordinates_group.operations().all_ops_sorted();
const std::vector<gemmi::Op> label_ops = label.operations().all_ops_sorted();
return std::includes(coordinate_ops.begin(), coordinate_ops.end(),
label_ops.begin(), label_ops.end());
}
} // namespace
void WritePlacedModel(const gemmi::Structure &placed,
const UnitCell &cell,
const gemmi::SpaceGroup &space_group,
const std::string &output_prefix,
Logger &logger) {
const std::string cif_path = output_prefix + "_model.cif";
// PDB as well as mmCIF, because the fragment-screening tools this file exists for take a PDB:
// PanDDA's per-dataset input is <name>.pdb beside <name>.mtz, and dimple produces the same pair.
// Same coordinates, same frame, both formats - a PDB cannot hold every cell, so it is written
// when it can be and skipped, with a line saying so, when it cannot.
const std::string pdb_path = output_prefix + "_model.pdb";
gemmi::Structure st = placed;
// The cell is the data's, taken from the same value the reflection files are written from: the
// coordinates were re-fractionalized into it and genuinely sit there.
//
// The group is the data's too wherever the coordinates obey it, which is the ordinary case and the
// one that matters - the label then matches the .mtz beside it, the enantiomorph in particular,
// since --model can adopt the model's and that is neither the data's original label nor,
// necessarily, the one the input model arrived with. Where they do not obey it the model's own
// group is written instead. Data merged in a supergroup of the model's - a run that over-merged
// across a pseudo-symmetry twofold, or a hand the model's fit did not earn - would otherwise put
// operations on the file that its contents contradict, and a refinement program expands them: the
// structure it refines is not the model, and it says so nowhere. A pair whose two files disagree
// is worth a warning; a coordinate file that is quietly wrong on its own is not worth writing.
const gemmi::SpaceGroup *model_group = st.find_spacegroup();
const bool takes_model_group = model_group != nullptr && !coordinates_obey(*model_group, space_group);
const gemmi::SpaceGroup &label = takes_model_group ? *model_group : space_group;
st.cell = cell;
st.spacegroup_hm = label.xhm();
st.setup_cell_images();
// Fills in entity types and label_asym_id for a model read from a PDB, which carries neither.
// Both are no-ops where the input already had them, i.e. for an mmCIF input.
// setup_entities and make_mmcif_document can both throw, and this runs BEFORE the reflection files
// are written: a convenience deliverable must not be able to take the run's actual output with it.
// The map writer does the same.
try {
gemmi::setup_entities(st);
std::ofstream os(cif_path);
gemmi::cif::write_cif_to_stream(os, gemmi::make_mmcif_document(st));
if (!os) {
logger.Error("Model validation: cannot write the placed model to {}", cif_path);
return;
}
} catch (const std::exception &e) {
logger.Warning("Model validation: could not write the placed model to {} ({})",
cif_path, e.what());
return;
}
bool pdb_written = false;
try {
std::ofstream os(pdb_path);
gemmi::write_pdb(st, os);
pdb_written = static_cast<bool>(os);
} catch (const std::exception &e) {
logger.Warning("Model validation: the placed model could not also be written as PDB ({}); "
"{} has it", e.what(), cif_path);
}
if (!takes_model_group) {
logger.Info("Model validation: the model as placed against these data written to {}{} "
"(cell and space group {} as in the reflection files)",
cif_path, pdb_written ? " and " + pdb_path : "", space_group.short_name());
} else {
logger.Warning("Model validation: the model as placed written to {}{} in ITS OWN space group "
"{}, not the {} the reflection files carry - its coordinates do not obey {}, so "
"the two files describe the same crystal in different symmetry and cannot be "
"refined against each other as they stand",
cif_path, pdb_written ? " and " + pdb_path : "",
label.short_name(), space_group.short_name(), space_group.short_name());
}
}