Files
leonarski_f 63f5f96132
Build Packages / Create release (push) Successful in 9s
Build Packages / build:portable:macos-arm64:nocuda (push) Successful in 5m19s
Build Packages / build:rugnux:linux-aarch64:cuda (push) Successful in 10m26s
Build Packages / build:portable:linux-x86_64:nocuda (push) Successful in 19m33s
Build Packages / build:portable:windows-x86_64:nocuda (push) Successful in 23m17s
Build Packages / build:portable:linux-x86_64:cuda (push) Successful in 23m28s
Build Packages / build:jfjoch:ubuntu2204:nocuda (push) Successful in 24m38s
Build Packages / build:portable:windows-x86_64:cuda (push) Successful in 27m30s
Build Packages / HDF5 consumer tests (DIALS, XDS) (push) Successful in 29m4s
Build Packages / build:jfjoch:rocky8:nocuda (push) Successful in 29m19s
Build Packages / build:jfjoch:rocky9:nocuda (push) Successful in 29m31s
Build Packages / build:jfjoch:ubuntu2404:nocuda (push) Successful in 23m19s
Build Packages / Generate python client (push) Successful in 28s
Build Packages / Build documentation (push) Successful in 1m22s
Build Packages / build:jfjoch:rocky8:cuda-sls9 (push) Successful in 24m39s
Build Packages / build:jfjoch:rocky9:cuda-sls9 (push) Successful in 29m40s
Build Packages / build:jfjoch:rocky8:cuda (push) Successful in 28m31s
Build Packages / build:jfjoch:ubuntu2204:cuda (push) Successful in 24m33s
Build Packages / build:jfjoch:ubuntu2404:cuda (push) Successful in 27m45s
Build Packages / build:jfjoch:rocky9:cuda (push) Successful in 29m25s
Build Packages / Unit tests (push) Successful in 1h35m24s
Build Packages / Validation Battery (push) Successful in 35m45s
v1.0.0-rc.180 (#91)
* Rugnux: A placed model is refined by default - restrained maximum-likelihood refinement on the GPU (CPU without one), writing `<prefix>_refined_model.cif`, `<prefix>_refined_maps.mtz` and the difference-map blobs in `<prefix>_blobs.json`; `--no-refine` skips it.
* Rugnux: `--mode refine` refines a model against merged structure factors from an MTZ or mmCIF file.
* Rugnux: The embedded restraint dictionary covers amino acids, nucleotides including common modified RNA nucleotides, common cofactors, crystallisation additives, waters and ions, with metal sites restrained from the model's LINK records.
* Rugnux: A model or reference file can be given among the inputs in any order, recognised by its contents.
* Rugnux: The geometry refinement algorithm option is now `-r, --geom-refine` (was `--refine`).
* Rugnux: Serial stills resolve the indexing ambiguity from the data when no model or reference is given, reject merge outliers by default, follow the run's measured rocking curve in prediction and partiality (also in `--mode scale`), and index reproducibly on the GPU.
* Rugnux: Reflections touching the beam-stop shadow are not integrated, reflections coarser than the low-resolution limit are merged and written (but kept out of the scale fit), and the polarisation plane is measured more reliably.
* Rugnux: Small-molecule runs report unit-cell e.s.d.s, in a SHELX `.ins` beside the `.hkl` and in the mmCIF.
* jfjoch_broker: Live Rugnux refines the uploaded model after placement; the web frontend's Rugnux live page has a hero panel with resolution, ISa, space group, placement and refined R-work/R-free, and its Mol* viewer shows the refined model and maps.
* Documentation: A new front page with download pages for Rugnux and the viewer, and the technical reference reorganised below it.
* Build: The fast-feedback indexer is part of the source tree instead of being downloaded at build time.

Reviewed-on: #91
Co-authored-by: Filip Leonarski <filip.leonarski@psi.ch>
2026-10-27 13:18:42 +01:00

311 lines
18 KiB
TypeScript

import React, {useCallback, useEffect, useRef, useState} from "react";
import {Alert, Box, Slider, Switch, Typography} from "@mui/material";
import type {PluginContext} from "molstar/lib/mol-plugin/context";
import {authHeaders, noteResponse} from "../auth";
import MolstarCanvas from "./structure/MolstarCanvas";
import {applyModelDisplay, clearScene, getStructureAtoms, getStructureBox, LoadedModel, loadModel, ReprMode}
from "./structure/loadModel";
import {loadMapLayer, MAP_COLORS, MapLayer, setLayerVisible, updateLayerSigma} from "./structure/loadMaps";
import {fullCellFromCcp4, prepareMap} from "./structure/densityCrop";
import {revealCamera} from "./structure/reveal";
import {Blob, BLOB_COLORS, loadBlobs, setBlobsVisible} from "./structure/loadBlobs";
// The model rugnux live placed - refined, where the run refined it, with the unexplained blobs of its difference map as
// markers (off at first) - with its 2mFo-DFc, mFo-DFc and (where the merge kept the Bijvoet pairs) anomalous
// difference maps, in Mol*. Set up as aaredb's structure viewer (gitea.psi.ch/mx/aaredb, pages/StructureView.tsx
// and components/Structure/, the PSI MX group's own code): its plugin, Coot mouse controls, model presets, map
// meshes, crop and carve, panel and defaults - on a black stage. Departures for the live page: every map is carved
// within the same radius of the atoms (aaredb carves 2Fo-Fc only), Fo-Fc starts at +-3.5 sigma, the anomalous map
// is shown at first only where the data carry anomalous signal, and the first model makes an entrance (reveal.ts). Mol* is only loaded with this component
// (React.lazy in RugnuxPage), and every Mol* import lives in ./structure.
type Kind = 'twofofc' | 'fofc' | 'anom';
const hex = (c: number) => '#' + c.toString(16).padStart(6, '0');
const SectionTitle: React.FC<{ children: React.ReactNode }> = ({children}) => (
<Typography sx={{fontSize: '0.6786rem', letterSpacing: '.05em', textTransform: 'uppercase',
color: 'text.secondary', mt: 2, mb: 1}}>{children}</Typography>
);
// one map row: colored dot · name · switch · σ slider + readout (aaredb StructureView.tsx:47-77)
const MapRow: React.FC<{
label: string;
dotColor: string;
on: boolean;
onToggle: (v: boolean) => void;
sigma: number;
min: number;
max: number;
step: number;
onSigma: (v: number) => void;
disabled?: boolean;
}> = ({label, dotColor, on, onToggle, sigma, min, max, step, onSigma, disabled}) => (
<Box sx={{mb: 1}}>
<Box sx={{display: 'flex', alignItems: 'center', gap: 1}}>
<Box sx={{width: 10, height: 10, borderRadius: '3px', bgcolor: dotColor, flex: 'none'}}/>
<Typography sx={{fontSize: '0.8125rem', flex: 1}}>{label}</Typography>
<Typography sx={{fontSize: '0.8125rem', color: 'text.secondary', width: 44, textAlign: 'right'}}>
{sigma.toFixed(1)} σ</Typography>
<Switch size="small" checked={on} disabled={disabled} onChange={(e) => onToggle(e.target.checked)}/>
</Box>
<Slider size="small" value={sigma} min={min} max={max} step={step} disabled={disabled || !on}
onChange={(_, v) => onSigma(v as number)} aria-label={`${label} contour level`}
sx={{mx: 0.5, width: 'calc(100% - 8px)', color: dotColor, '& .MuiSlider-thumb': {width: 12, height: 12}}}/>
</Box>
);
async function fetchBytes(path: string, signal: AbortSignal): Promise<ArrayBuffer | undefined> {
const res = await fetch(path, {headers: authHeaders(), signal});
noteResponse(res);
if (res.status === 404)
return undefined;
if (!res.ok)
throw new Error(`${path}: HTTP ${res.status}`);
return res.arrayBuffer();
}
const REVEAL_MS = 1600;
const reducedMotion = () => window.matchMedia?.('(prefers-reduced-motion: reduce)').matches ?? false;
const nextFrames = () => new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)));
const BLOB_LABELS: [string, string][] = [['LIGAND', 'ligand'], ['WATER', 'water'], ['ION', 'ion'],
['MODEL_ERROR', 'model error'], ['UNASSIGNED', 'other']];
// resultKey: changes when a new result is there to load; anomalous: it has an anomalous difference map;
// anomalousSignal: the data carry usable anomalous signal, so that map is worth showing from the start;
// refined: the broker serves the refined model and its maps; blobs: what the refined model leaves unexplained.
function RugnuxStructureViewer({resultKey, anomalous, anomalousSignal, refined, blobs}:
{ resultKey: string, anomalous: boolean, anomalousSignal: boolean,
refined: boolean, blobs: Blob[] }) {
const pluginRef = useRef<PluginContext | null>(null);
const [pluginReady, setPluginReady] = useState(0);
const [error, setError] = useState<string>();
const [modelLoading, setModelLoading] = useState(true);
const [mapsLoading, setMapsLoading] = useState(false);
const layersRef = useRef<Partial<Record<Kind, MapLayer>>>({});
const modelRefsRef = useRef<LoadedModel['refs']>({});
const sigmaTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
// First load frames the molecule; later results keep the user's camera and clip (aaredb StructureView.tsx:117-121)
const sceneLoadedRef = useRef(false);
const [hasMap, setHasMap] = useState<Record<Kind, boolean>>({twofofc: false, fofc: false, anom: false});
// The stage stays dark until the first model and its maps are in, then fades up as the camera eases in.
const [revealed, setRevealed] = useState(false);
// aaredb factory defaults (state/viewerDefaults.ts:16-23), but Fo-Fc at +-3.5 sigma rather than 3: carved or not,
// a difference map at 3 sigma over a whole molecule shows dozens of noise peaks; anomalous as a difference map,
// +4 sigma
const [show, setShow] = useState<Record<Kind, boolean>>({twofofc: true, fofc: true, anom: anomalousSignal});
const [sigma, setSigma] = useState<Record<Kind, number>>({twofofc: 1.0, fofc: 3.5, anom: 4.0});
const [reprMode, setReprMode] = useState<ReprMode>('cartoon');
const [showWaters, setShowWaters] = useState(false);
// The blobs can be dozens of waters: off until asked for
const [showBlobs, setShowBlobs] = useState(false);
const blobRefsRef = useRef<string[]>([]);
const displayRef = useRef({show, sigma, reprMode, showWaters, showBlobs});
displayRef.current = {show, sigma, reprMode, showWaters, showBlobs};
const onPluginReady = useCallback((plugin: PluginContext) => {
pluginRef.current = plugin;
setPluginReady(n => n + 1);
}, []);
// The model, then its maps (aaredb StructureView.tsx maybeLoad / computeAndAttachMaps, 212-295): each map cropped
// to the model's box + 6 A and carved 4.5 A around the atoms.
useEffect(() => {
const plugin = pluginRef.current;
if (!plugin)
return;
const abort = new AbortController();
(async () => {
setModelLoading(true);
setMapsLoading(false);
setError(undefined);
try {
await clearScene(plugin);
layersRef.current = {};
blobRefsRef.current = [];
setHasMap({twofofc: false, fofc: false, anom: false});
const [cif, map2, map1, mapA] = await Promise.all([
fetchBytes("/rugnux/model.cif", abort.signal),
fetchBytes("/rugnux/map.ccp4?type=2fofc", abort.signal),
fetchBytes("/rugnux/map.ccp4?type=fofc", abort.signal),
anomalous ? fetchBytes("/rugnux/map.ccp4?type=anom", abort.signal) : undefined]);
if (abort.signal.aborted)
return;
if (!cif) {
setError("No placed model");
return;
}
const firstScene = !sceneLoadedRef.current;
const loaded = await loadModel(plugin, cif, 'model', {resetCamera: firstScene, format: 'mmcif'});
sceneLoadedRef.current = true;
modelRefsRef.current = loaded.refs;
const d = displayRef.current;
applyModelDisplay(plugin, loaded.refs, d.reprMode, d.showWaters);
const box = getStructureBox(loaded.structure);
const crop = box ? {...box, marginA: 6} : null;
const atoms = getStructureAtoms(loaded.structure);
if (blobs.length > 0)
blobRefsRef.current = await loadBlobs(plugin, blobs, d.showBlobs);
setModelLoading(false);
setMapsLoading(true);
const maps: [Kind, '2fofc' | 'fofc' | 'anom', ArrayBuffer | undefined][] =
[['twofofc', '2fofc', map2], ['fofc', 'fofc', map1], ['anom', 'anom', mapA]];
for (const [kind, name, bytes] of maps) {
if (!bytes || abort.signal.aborted)
continue;
const computed = prepareMap(name, fullCellFromCcp4(bytes), crop, atoms.length ? atoms : null);
const layer = await loadMapLayer(plugin, computed, name, d.sigma[kind]);
layersRef.current[kind] = layer;
if (!displayRef.current.show[kind])
setLayerVisible(plugin, layer, false);
setHasMap(h => ({...h, [kind]: true}));
}
if (firstScene && !abort.signal.aborted) {
await nextFrames(); // the camera reset loadModel asked for is applied on a draw
if (!reducedMotion())
revealCamera(plugin, REVEAL_MS);
}
} catch (e) {
if (!abort.signal.aborted)
setError(e instanceof Error ? e.message : String(e));
} finally {
if (!abort.signal.aborted) {
setModelLoading(false);
setMapsLoading(false);
setRevealed(true);
}
}
})();
return () => abort.abort();
}, [pluginReady, resultKey, anomalous]);
// σ sliders: debounced param-only updates (re-mesh, no recompute) - aaredb StructureView.tsx:321-330
const applySigma = useCallback((kind: Kind, value: number) => {
setSigma(s => ({...s, [kind]: value}));
if (sigmaTimer.current)
clearTimeout(sigmaTimer.current);
sigmaTimer.current = setTimeout(() => {
const plugin = pluginRef.current;
const layer = layersRef.current[kind];
if (plugin && layer)
void updateLayerSigma(plugin, layer, value);
}, 90);
}, []);
const toggleLayer = useCallback((kind: Kind, visible: boolean) => {
setShow(s => ({...s, [kind]: visible}));
const plugin = pluginRef.current;
const layer = layersRef.current[kind];
if (plugin && layer)
setLayerVisible(plugin, layer, visible);
}, []);
const setDisplay = useCallback((mode: ReprMode, waters: boolean) => {
setReprMode(mode);
setShowWaters(waters);
const plugin = pluginRef.current;
if (plugin)
applyModelDisplay(plugin, modelRefsRef.current, mode, waters);
}, []);
const toggleBlobs = useCallback((visible: boolean) => {
setShowBlobs(visible);
const plugin = pluginRef.current;
if (plugin)
setBlobsVisible(plugin, blobRefsRef.current, visible);
}, []);
const mapsDisabled = mapsLoading;
return (
<Box sx={{display: 'grid', gridTemplateColumns: {xs: '1fr', md: '1fr 300px'}, border: '1px solid',
borderColor: 'divider', borderRadius: 1, overflow: 'hidden'}}>
<Box sx={{position: 'relative', bgcolor: '#000', height: {xs: '55vh', md: 'clamp(520px, 64vh, 820px)'}}}>
<Box sx={{position: 'absolute', inset: 0, opacity: revealed ? 1 : 0,
transition: 'opacity 900ms ease-out', '@media (prefers-reduced-motion: reduce)': {transition: 'none'}}}>
<MolstarCanvas onReady={onPluginReady}/>
</Box>
{(modelLoading || (mapsLoading && !revealed)) && !error && <Box sx={{position: 'absolute', inset: 0, display: 'flex', alignItems: 'center',
justifyContent: 'center', pointerEvents: 'none'}}>
<Typography sx={{fontSize: '0.875rem', color: '#8aa0b5'}}>
{modelLoading ? 'loading model…' : 'computing density maps…'}</Typography>
</Box>}
</Box>
<Box sx={{borderLeft: {md: '1px solid'}, borderColor: {md: 'divider'}, p: 1.75}}>
{error && <Alert severity="warning" sx={{mb: 1}}>{error}</Alert>}
<SectionTitle>{refined ? 'Model · refined' : 'Model'}</SectionTitle>
<Box sx={{display: 'flex', gap: 0.75, flexWrap: 'wrap', alignItems: 'center', mb: 0.75}}>
{(['cartoon', 'sticks', 'both'] as const).map((m) => (
<Typography key={m} role="button" tabIndex={0}
onClick={() => setDisplay(m, showWaters)}
onKeyDown={(e) => { if (e.key === 'Enter') setDisplay(m, showWaters); }}
sx={{fontSize: '0.8125rem', px: 1, py: '3px', borderRadius: '8px',
border: '1px solid', cursor: 'pointer',
color: reprMode === m ? 'primary.main' : 'text.secondary',
borderColor: reprMode === m ? 'primary.main' : 'divider'}}
>{m === 'both' ? 'cartoon + sticks' : m}</Typography>
))}
</Box>
<Box sx={{display: 'flex', alignItems: 'center', justifyContent: 'space-between'}}>
<Typography sx={{fontSize: '0.8125rem'}}>Waters</Typography>
<Switch size="small" checked={showWaters} onChange={(e) => setDisplay(reprMode, e.target.checked)}/>
</Box>
<SectionTitle>Maps</SectionTitle>
{hasMap.twofofc && <MapRow label="2Fo−Fc" dotColor={hex(MAP_COLORS.twofofc)} on={show.twofofc}
onToggle={(v) => toggleLayer('twofofc', v)} sigma={sigma.twofofc}
min={0.5} max={4} step={0.1} onSigma={(v) => applySigma('twofofc', v)}
disabled={mapsDisabled}/>}
{hasMap.fofc && <MapRow label="Fo−Fc (±)" dotColor={hex(MAP_COLORS.fofcPos)} on={show.fofc}
onToggle={(v) => toggleLayer('fofc', v)} sigma={sigma.fofc}
min={1} max={8} step={0.5} onSigma={(v) => applySigma('fofc', v)}
disabled={mapsDisabled}/>}
{hasMap.anom && <MapRow label="Anomalous (+)" dotColor={hex(MAP_COLORS.anom)} on={show.anom}
onToggle={(v) => toggleLayer('anom', v)} sigma={sigma.anom}
min={1} max={8} step={0.5} onSigma={(v) => applySigma('anom', v)}
disabled={mapsDisabled}/>}
{mapsLoading && <Typography sx={{fontSize: '0.8125rem', color: 'text.secondary'}}>
computing density maps…</Typography>}
{!modelLoading && !mapsLoading && !hasMap.twofofc &&
<Typography sx={{fontSize: '0.8125rem', color: 'text.secondary'}}>No maps</Typography>}
{blobs.length > 0 && <>
<SectionTitle>Unexplained blobs</SectionTitle>
<Box sx={{display: 'flex', alignItems: 'center', justifyContent: 'space-between'}}>
<Typography sx={{fontSize: '0.8125rem'}}>{blobs.length} in mFo−DFc</Typography>
<Switch size="small" checked={showBlobs} disabled={modelLoading}
onChange={(e) => toggleBlobs(e.target.checked)}/>
</Box>
<Box sx={{display: 'flex', flexWrap: 'wrap', columnGap: 1.5, rowGap: 0.5, mt: 0.5}}>
{BLOB_LABELS.filter(([k]) => blobs.some(b => b.kind === k)).map(([k, label]) => (
<Box key={k} sx={{display: 'flex', alignItems: 'center', gap: 0.5}}>
<Box sx={{width: 9, height: 9, borderRadius: '50%', bgcolor: hex(BLOB_COLORS[k]),
border: '1px solid', borderColor: 'divider'}}/>
<Typography sx={{fontSize: '0.75rem', color: 'text.secondary'}}>
{blobs.filter(b => b.kind === k).length} {label}</Typography>
</Box>
))}
</Box>
</>}
<SectionTitle>Controls</SectionTitle>
{([
['left drag', 'rotate'],
['right drag', 'zoom'],
['middle drag', 'pan'],
['middle click', 'center on residue'],
['scroll', 'clip'],
] as const).map(([k, v]) => (
<Box key={k} sx={{display: 'flex', justifyContent: 'space-between', py: '1px'}}>
<Typography component="span" sx={{fontSize: '0.75rem', color: 'text.secondary'}}>{k}</Typography>
<Typography component="span" sx={{fontSize: '0.75rem'}}>{v}</Typography>
</Box>
))}
</Box>
</Box>
);
}
export default RugnuxStructureViewer;