Build Packages / Unit tests (push) Skipped
Build Packages / build:windows:nocuda (push) Successful in 11m6s
Build Packages / build:rpm (rocky8_nocuda) (push) Successful in 10m27s
Build Packages / build:rpm (rocky9_nocuda) (push) Successful in 10m54s
Build Packages / build:rpm (ubuntu2204_nocuda) (push) Successful in 9m25s
Build Packages / build:rpm (ubuntu2404_nocuda) (push) Successful in 10m5s
Build Packages / build:rpm (rocky8_sls9) (push) Successful in 11m33s
Build Packages / build:rpm (rocky9_sls9) (push) Successful in 11m19s
Build Packages / build:rpm (rocky8) (push) Successful in 12m23s
Build Packages / build:rpm (rocky9) (push) Successful in 13m21s
Build Packages / build:rpm (ubuntu2204) (push) Successful in 12m30s
Build Packages / build:rpm (ubuntu2404) (push) Successful in 11m55s
Build Packages / DIALS test (push) Successful in 13m42s
Build Packages / XDS test (durin plugin) (push) Successful in 9m26s
Build Packages / XDS test (JFJoch plugin) (push) Successful in 6m41s
Build Packages / XDS test (neggia plugin) (push) Successful in 6m12s
Build Packages / Generate python client (push) Successful in 19s
Build Packages / Build documentation (push) Successful in 52s
Build Packages / Create release (push) Skipped
Build Packages / build:viewer-tgz:cpu (push) Successful in 5m29s
Build Packages / build:viewer-tgz:cuda (push) Successful in 6m12s
Build Packages / build:windows:cuda (push) Successful in 18m36s
This is an UNSTABLE release. It includes many experimental features, as well as many AI generated fixes. We recommend using rc.152 for production use. * rugnux: Add `--model model.pdb` - score the merged data against an atomic model and compute initial maps. It reports R-work/R-free (scaling the model to the observed amplitudes with an overall scale, an anisotropic B and a flat bulk solvent - the standard few-parameter model, so a batch of maps stays directly comparable) and writes 2Fo-Fc / Fo-Fc electron-density maps (CCP4) plus a map-coefficient MTZ. The structure itself is not refined; the model is only re-fractionalised into the data cell. * rugnux: The merged reflection output now carries French-Wilson amplitudes (|F| and its sigma) next to the intensities - MTZ `F`/`SIGF`, mmCIF `_refln.F_meas_au`, and the text HKL - computed with the correct centric/acentric Wilson prior and epsilon multiplicity, so a downstream program (e.g. phenix.refine) can refine against amplitudes. The intensity columns are unchanged. * rugnux: R-free test-set flags are now assigned deterministically and consistently across symmetry - a Bijvoet pair I(+)/I(-) is never split between the work and free sets, and the assignment is a reproducible per-hkl hash that depends only on the reflection index, so every dataset of one crystal form gets the same ~5% free set (what a multi-dataset campaign such as PanDDA needs). On small data the fraction is floored so the test set stays large enough for a stable R-free (~500 reflections, capped at 10%); it stays flat at 5% on ordinary data. When a reference MTZ carries a `FreeR_flag` column its test set is imported instead, letting a whole campaign inherit one shared free set. * rugnux: A reference MTZ (`--reference-mtz`) can now fix the space group and cell for rotation data too (previously rejected), without being used to scale - the rotation merge stays self-consistent. When the crystal has an indexing (merohedral) ambiguity - a lattice symmetry higher than its Laue symmetry, e.g. P3/P4/P6/C2 - the reference also resolves it: each candidate reindexing (identity plus the twin-law cosets of the metric symmetry) is scored by its intensity correlation against the reference and the data are re-merged in the best-correlating one. This is a metric-preserving relabelling of hkl (the cell is unchanged) and a no-op for a holohedral crystal such as lysozyme. * rugnux: `--model` validation now aligns the data to the model before scoring - the observed reflections are reindexed into the model's enantiomorph when the two differ only by hand (indistinguishable from merged intensities). A merohedral indexing ambiguity is resolved against the reference MTZ when one is given (so a whole campaign shares one indexing convention); only with a model and no reference does validation fall back to fitting each candidate reindexing and keeping the lowest R-free. * rugnux: De-novo symmetry - recover a genuine high-symmetry group whose data are imperfectly scaled. Such a merge's within-orbit chi² lands just past the self-consistency bound (each real symmetry step adds a little systematic scatter), right where a merohedral twin also lands, so the chi² ratio alone cannot separate them. The candidate is now rescued when the extra intensity-proportional systematic error it invokes stays small relative to the confirmed subgroup - a genuine symmetry step gains multiplicity without inflating the merge error model's b, whereas a twin forces non-equivalent reflections together and b balloons. Fixes cubic insulin (I23 instead of I222) with no change to any other crystal in the test battery, including the twins that must stay in their lower symmetry. * Docs: Document the French-Wilson amplitude estimation, R-free flagging, reference-based space-group/ambiguity resolution, and model-based validation/maps in CPU_DATA_ANALYSIS.md. * Frontend: The status-bar pill now shows a progress bar during detector calibration (previously only during measurement), and the calibration state and its button are labelled "Calibration"/"CALIBRATE" (the internal `Pedestal` state name is unchanged for back-compatibility).Reviewed-on: #69 Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
336 lines
11 KiB
C++
336 lines
11 KiB
C++
// Copyright 2017-2021 Global Phasing Ltd.
|
|
//
|
|
// Modify various properties of the model.
|
|
|
|
// For modifications that depend on entities or connectivity see polyheur.hpp.
|
|
|
|
#ifndef GEMMI_MODIFY_HPP_
|
|
#define GEMMI_MODIFY_HPP_
|
|
|
|
#include "model.hpp"
|
|
#include "util.hpp" // for vector_remove_if
|
|
#include <set>
|
|
|
|
namespace gemmi {
|
|
|
|
/// Remove alternative conformations.
|
|
template<class T> void remove_alternative_conformations(T& obj) {
|
|
for (auto& child : obj.children())
|
|
remove_alternative_conformations(child);
|
|
}
|
|
template<> inline void remove_alternative_conformations(Chain& chain) {
|
|
std::set<SeqId> seqids;
|
|
for (size_t i = 0; i < chain.residues.size(); ) {
|
|
if (seqids.insert(chain.residues[i].seqid).second)
|
|
++i;
|
|
else
|
|
chain.residues.erase(chain.residues.begin() + i);
|
|
}
|
|
for (Residue& residue : chain.residues) {
|
|
std::set<std::string> names;
|
|
for (size_t i = 0; i < residue.atoms.size(); ) {
|
|
Atom& atom = residue.atoms[i];
|
|
atom.altloc = '\0';
|
|
if (names.insert(atom.name).second)
|
|
++i;
|
|
else
|
|
residue.atoms.erase(residue.atoms.begin() + i);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Remove hydrogens.
|
|
template<class T> void remove_hydrogens(T& obj) {
|
|
for (auto& child : obj.children())
|
|
remove_hydrogens(child);
|
|
}
|
|
template<> inline void remove_hydrogens(Residue& res) {
|
|
vector_remove_if(res.atoms, [](const Atom& a) {
|
|
return a.element == El::H || a.element == El::D;
|
|
});
|
|
}
|
|
|
|
/// Set isotropic ADP to the range (b_min, b_max). Values smaller than
|
|
/// b_min are changed to b_min, values larger than b_max to b_max.
|
|
/// Anisotropic ADP is left unchanged.
|
|
template<class T> void assign_b_iso(T& obj, float b_min, float b_max) {
|
|
for (auto& child : obj.children())
|
|
assign_b_iso(child, b_min, b_max);
|
|
}
|
|
template<> inline void assign_b_iso(Atom& atom, float b_min, float b_max) {
|
|
atom.b_iso = clamp(atom.b_iso, b_min, b_max);
|
|
}
|
|
|
|
/// Remove anisotropic ADP
|
|
template<class T> void remove_anisou(T& obj) {
|
|
for (auto& child : obj.children())
|
|
remove_anisou(child);
|
|
}
|
|
template<> inline void remove_anisou(Atom& atom) {
|
|
atom.aniso = {0, 0, 0, 0, 0, 0};
|
|
}
|
|
|
|
/// Set absent ANISOU to value from B_iso
|
|
template<class T> void ensure_anisou(T& obj) {
|
|
for (auto& child : obj.children())
|
|
ensure_anisou(child);
|
|
}
|
|
template<> inline void ensure_anisou(Atom& atom) {
|
|
if (!atom.aniso.nonzero()) {
|
|
float u = float(1. / gemmi::u_to_b() * atom.b_iso);
|
|
atom.aniso = {u, u, u, 0.f, 0.f, 0.f};
|
|
}
|
|
}
|
|
|
|
/// apply Transform to both atom's position and ADP
|
|
template<class T> void transform_pos_and_adp(T& obj, const Transform& tr) {
|
|
for (auto& child : obj.children())
|
|
transform_pos_and_adp(child, tr);
|
|
}
|
|
template<> inline void transform_pos_and_adp(Atom& atom, const Transform& tr) {
|
|
atom.pos = Position(tr.apply(atom.pos));
|
|
if (atom.aniso.nonzero())
|
|
atom.aniso = atom.aniso.transformed_by<float>(tr.mat);
|
|
}
|
|
|
|
/// set atom site serial numbers to 1, 2, ..., optionally leaving gaps for TERs
|
|
inline void assign_serial_numbers(Model& model, bool numbered_ter=false) {
|
|
int serial = 0;
|
|
for (Chain& chain : model.chains)
|
|
for (Residue& res : chain.residues) {
|
|
for (Atom& atom : res.atoms)
|
|
atom.serial = ++serial;
|
|
if (numbered_ter && res.entity_type == EntityType::Polymer &&
|
|
(&res == &chain.residues.back() || (&res + 1)->entity_type != EntityType::Polymer))
|
|
++serial;
|
|
}
|
|
}
|
|
inline void assign_serial_numbers(Structure& st, bool numbered_ter=false) {
|
|
for (Model& model : st.models)
|
|
assign_serial_numbers(model, numbered_ter);
|
|
}
|
|
|
|
|
|
/// Helper function for processing (usually: changing) names and numbers
|
|
/// in AtomAddress instances in metadata:
|
|
/// Connection, CisPep, Helix, Sheet::Strand.
|
|
/// Other fields are not updated here, in particular: ModRes, Entity::DbRef,
|
|
/// Entity::full_sequence, TlsGroup::Selection.
|
|
template<typename Func>
|
|
void process_addresses(Structure& st, Func func) {
|
|
for (Connection& con : st.connections) {
|
|
func(con.partner1);
|
|
func(con.partner2);
|
|
}
|
|
for (CisPep& cispep : st.cispeps) {
|
|
func(cispep.partner_c);
|
|
func(cispep.partner_n);
|
|
}
|
|
for (Helix& helix : st.helices) {
|
|
func(helix.start);
|
|
func(helix.end);
|
|
}
|
|
for (Sheet& sheet : st.sheets)
|
|
for (Sheet::Strand& strand : sheet.strands) {
|
|
func(strand.start);
|
|
func(strand.end);
|
|
func(strand.hbond_atom2);
|
|
func(strand.hbond_atom1);
|
|
}
|
|
}
|
|
|
|
/// Takes func(const std::string& chain_name, gemmi::SeqId& seqid).
|
|
/// It doesn't process Entity::DbRef::seq_begin/seq_end (b/c there is no
|
|
/// single corresponding chain name).
|
|
template<typename Func>
|
|
void process_sequence_ids(Structure& st, Func func) {
|
|
process_addresses(st, [&](AtomAddress& aa) { func(aa.chain_name, aa.res_id.seqid); });
|
|
for (ModRes& modres : st.mod_residues)
|
|
func(modres.chain_name, modres.res_id.seqid);
|
|
for (RefinementInfo& ri : st.meta.refinement)
|
|
for (TlsGroup& tls : ri.tls_groups)
|
|
for (TlsGroup::Selection& sel : tls.selections) {
|
|
func(sel.chain, sel.res_begin);
|
|
func(sel.chain, sel.res_end);
|
|
}
|
|
}
|
|
|
|
inline void rename_chain(Structure& st, const std::string& old_name,
|
|
const std::string& new_name) {
|
|
auto update = [&](std::string& name) {
|
|
if (name == old_name)
|
|
name = new_name;
|
|
};
|
|
process_addresses(st, [&](AtomAddress& aa) { update(aa.chain_name); });
|
|
for (ModRes& modres : st.mod_residues)
|
|
update(modres.chain_name);
|
|
for (RefinementInfo& ri : st.meta.refinement)
|
|
for (TlsGroup& tls : ri.tls_groups)
|
|
for (TlsGroup::Selection& sel : tls.selections)
|
|
update(sel.chain);
|
|
for (Model& model : st.models)
|
|
for (Chain& chain : model.chains)
|
|
update(chain.name);
|
|
}
|
|
|
|
inline void rename_residues(Structure& st, const std::string& old_name,
|
|
const std::string& new_name) {
|
|
auto update = [&](ResidueId& rid) {
|
|
if (rid.name == old_name)
|
|
rid.name = new_name;
|
|
};
|
|
process_addresses(st, [&](AtomAddress& aa) { update(aa.res_id); });
|
|
for (ModRes& modres : st.mod_residues)
|
|
update(modres.res_id);
|
|
for (Entity& ent : st.entities)
|
|
for (std::string& mon_ids : ent.full_sequence)
|
|
for (size_t start = 0;;) {
|
|
size_t end = mon_ids.find(',', start);
|
|
if (mon_ids.compare(start, end-start, old_name) == 0) {
|
|
mon_ids.replace(start, end-start, new_name);
|
|
if (end != std::string::npos)
|
|
end = start + new_name.size();
|
|
}
|
|
if (end == std::string::npos)
|
|
break;
|
|
start = end + 1;
|
|
}
|
|
for (Model& model : st.models)
|
|
for (Chain& chain : model.chains)
|
|
for (Residue& res : chain.residues)
|
|
update(res);
|
|
}
|
|
|
|
|
|
inline void rename_atom_names(Structure& st, const std::string& res_name,
|
|
const std::map<std::string, std::string>& old_new) {
|
|
auto update = [&old_new](std::string& name) {
|
|
auto it = old_new.find(name);
|
|
if (it != old_new.end())
|
|
name = it->second;
|
|
};
|
|
process_addresses(st, [&](AtomAddress& aa) {
|
|
if (aa.res_id.name == res_name)
|
|
update(aa.atom_name);
|
|
});
|
|
for (Model& model : st.models)
|
|
for (Chain& chain : model.chains)
|
|
for (Residue& res : chain.residues)
|
|
if (res.name == res_name) {
|
|
for (Atom& atom : res.atoms)
|
|
update(atom.name);
|
|
}
|
|
}
|
|
|
|
|
|
inline void replace_d_fraction_with_altlocs(Residue& res) {
|
|
for (size_t i = res.atoms.size(); i-- != 0; ) {
|
|
Atom& atom = res.atoms[i];
|
|
float d_fraction = atom.fraction;
|
|
if (atom.element == El::H && d_fraction > 0) {
|
|
if (d_fraction >= 1) {
|
|
atom.element = El::D;
|
|
if (atom.name[0] == 'H')
|
|
atom.name[0] = 'D';
|
|
} else {
|
|
int alt_offset = atom.altloc;
|
|
if (alt_offset) {
|
|
alt_offset -= 'A';
|
|
// we don't expect 4+ altlocs - ignore such cases
|
|
if (alt_offset < 0 || alt_offset >= 3)
|
|
continue;
|
|
}
|
|
atom.altloc = 'A' + alt_offset;
|
|
float d_occ = atom.occ * d_fraction;
|
|
atom.occ *= (1 - d_fraction);
|
|
auto deut = res.atoms.insert(res.atoms.begin() + i + 1, atom);
|
|
deut->altloc = 'D' + alt_offset;
|
|
deut->element = El::D;
|
|
deut->occ = d_occ;
|
|
if (deut->name[0] == 'H')
|
|
deut->name[0] = 'D';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
inline bool replace_deuterium_with_fraction(Residue& res) {
|
|
bool found = false;
|
|
for (auto d = res.atoms.end(); d-- != res.atoms.begin(); )
|
|
if (d->element == El::D) {
|
|
found = true;
|
|
auto h = res.atoms.begin();
|
|
for (; h != res.atoms.end(); ++h)
|
|
if (h->element == El::H && h->pos.approx(d->pos, 1e-9))
|
|
break;
|
|
if (h != res.atoms.end()) {
|
|
h->occ += d->occ;
|
|
h->fraction = h->occ > 0.f ? d->occ / h->occ : 0.f;
|
|
if (h->altloc) {
|
|
bool keep_altloc = false;
|
|
for (auto i = res.atoms.begin(); i != res.atoms.end(); ++i)
|
|
if (i != d && i != h && (i->name == h->name || i->name == d->name))
|
|
keep_altloc = true;
|
|
if (!keep_altloc)
|
|
h->altloc = '\0';
|
|
}
|
|
res.atoms.erase(d);
|
|
} else {
|
|
d->element = El::H;
|
|
d->fraction = 1;
|
|
// Atom name is left unchanged. prepare_topology() first calls this
|
|
// function and then conditionally changes the name (Dxx -> Hxx).
|
|
}
|
|
}
|
|
return found;
|
|
}
|
|
|
|
/// Hydrogens modelled as H/D mixture (altlocs H and D with the same position
|
|
/// and ADP, but with refined fraction of D), it can be stored in mmCIF either
|
|
/// as two atoms (H and D) or, using CCP4/Refmac extension, as H atoms with
|
|
/// the ccp4_deuterium_fraction parameter.
|
|
/// This function switches fraction <-> altlocs
|
|
inline void store_deuterium_as_fraction(Structure& st, bool store_fraction) {
|
|
if (st.has_d_fraction == store_fraction)
|
|
return;
|
|
st.has_d_fraction = false;
|
|
for (Model& model : st.models)
|
|
for (Chain& chain : model.chains)
|
|
for (Residue& res : chain.residues)
|
|
if (store_fraction) {
|
|
if (replace_deuterium_with_fraction(res))
|
|
st.has_d_fraction = true;
|
|
} else {
|
|
replace_d_fraction_with_altlocs(res);
|
|
}
|
|
}
|
|
|
|
inline void set_deuterium_fraction_of_hydrogens(Structure& st, float d_fract) {
|
|
st.has_d_fraction = true;
|
|
for (Model& model : st.models)
|
|
for (Chain& chain : model.chains)
|
|
for (Residue& res : chain.residues)
|
|
for (Atom& atom : res.atoms)
|
|
if (atom.is_hydrogen())
|
|
atom.fraction = d_fract;
|
|
}
|
|
|
|
/// Convert coordinates to the standard coordinate system for the unit cell.
|
|
inline void standardize_crystal_frame(Structure& st) {
|
|
if (!st.cell.explicit_matrices || !st.cell.is_crystal())
|
|
return;
|
|
Transform orig_frac = st.cell.frac;
|
|
st.cell.explicit_matrices = false;
|
|
st.cell.calculate_properties();
|
|
Transform tr = st.cell.orth.combine(orig_frac);
|
|
Transform tr_inv = tr.inverse();
|
|
st.has_origx = true;
|
|
st.origx = tr_inv.combine(st.origx);
|
|
for (NcsOp& ncsop : st.ncs)
|
|
ncsop.tr = tr.combine(ncsop.tr).combine(tr_inv);
|
|
transform_pos_and_adp(st, tr);
|
|
}
|
|
|
|
} // namespace gemmi
|
|
#endif
|