File size: 8,442 Bytes
dd079af 047712c dd079af 047712c dd079af cb1600f c77380d cb1600f 047712c cb1600f 047712c cb1600f 047712c cb1600f 047712c cb1600f c77380d cb1600f 047712c cb1600f 047712c cb1600f 047712c cb1600f 047712c cb1600f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | import { useEffect, useRef, useState } from 'react';
import { Box } from 'lucide-react';
/* PDBe Mol* is ~6 MB, so it is loaded from the CDN only when a viewer is opened. */
const MOLSTAR_VERSION = '3.12.0';
const BASE = `https://cdn.jsdelivr.net/npm/pdbe-molstar@${MOLSTAR_VERSION}/build`;
interface MolstarInstance {
render: (el: HTMLElement, options: Record<string, unknown>) => Promise<void> | void;
visual: {
select: (params: Record<string, unknown>) => Promise<void> | void;
tooltips?: (params: Record<string, unknown>) => Promise<void> | void;
};
events: { loadComplete: { subscribe: (cb: (ok: boolean) => void) => { unsubscribe?: () => void } } };
plugin?: {
dispose?: () => void;
canvas3d?: { setProps: (props: Record<string, unknown>) => void };
};
}
declare global {
interface Window {
PDBeMolstarPlugin?: new () => MolstarInstance;
}
}
let loader: Promise<void> | null = null;
function loadMolstar(): Promise<void> {
if (window.PDBeMolstarPlugin) return Promise.resolve();
if (!loader) {
loader = new Promise<void>((resolve, reject) => {
const css = document.createElement('link');
css.rel = 'stylesheet';
css.href = `${BASE}/pdbe-molstar-light.css`;
document.head.appendChild(css);
const s = document.createElement('script');
s.src = `${BASE}/pdbe-molstar-plugin.js`;
s.async = true;
s.onload = () => resolve();
s.onerror = () => {
loader = null;
reject(new Error('Could not load the Mol* viewer'));
};
document.head.appendChild(s);
});
}
return loader;
}
function hexToRgb(hex: string) {
const n = parseInt(hex.replace('#', ''), 16);
return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 };
}
function isDark() {
const t = document.documentElement.dataset.theme;
if (t) return t === 'dark';
return window.matchMedia?.('(prefers-color-scheme: dark)').matches ?? false;
}
export function StructureViewer({
pdbId,
chain,
color = '#2a78d6',
height = 380,
label,
}: {
pdbId: string;
chain: string;
color?: string;
height?: number;
label?: string;
}) {
const ref = useRef<HTMLDivElement>(null);
const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading');
useEffect(() => {
let alive = true;
let sub: { unsubscribe?: () => void } | undefined;
let instance: MolstarInstance | undefined;
const el = ref.current;
setStatus('loading');
// Mol* reports nothing when WebGL is unavailable; don't spin forever
const timeout = setTimeout(() => alive && setStatus((st) => (st === 'loading' ? 'error' : st)), 90000);
loadMolstar()
.then(() => {
if (!alive || !el || !window.PDBeMolstarPlugin) return;
instance = new window.PDBeMolstarPlugin();
sub = instance.events.loadComplete.subscribe((ok) => {
if (!alive) return;
if (ok === false) {
setStatus('error');
return;
}
setStatus('ready');
instance?.visual.select({
data: [{ auth_asym_id: chain, color: hexToRgb(color), focus: true }],
nonSelectedColor: isDark() ? { r: 70, g: 78, b: 92 } : { r: 214, g: 219, b: 226 },
});
});
const bg = isDark() ? { r: 18, g: 24, b: 33 } : { r: 255, g: 255, b: 255 };
instance.render(el, {
moleculeId: pdbId.toLowerCase(),
bgColor: bg,
hideControls: true,
sequencePanel: false,
landscape: true,
visualStyle: 'cartoon',
hideCanvasControls: ['expand', 'selection', 'animation', 'controlToggle', 'controlInfo'],
loadingOverlay: true,
});
})
.catch(() => alive && setStatus('error'));
return () => {
alive = false;
clearTimeout(timeout);
sub?.unsubscribe?.();
instance?.plugin?.dispose?.();
if (el) el.innerHTML = '';
};
}, [pdbId, chain, color]);
return (
<figure className="viewer" style={{ height }}>
<div ref={ref} className="viewer__canvas" />
{status !== 'ready' && (
<div className="viewer__overlay">
{status === 'loading' ? (
<>
<span className="spinner" aria-hidden /> Loading {pdbId.toUpperCase()}…
</>
) : (
<>
<Box aria-hidden /> 3D view unavailable for {pdbId.toUpperCase()}
</>
)}
</div>
)}
{label && <figcaption className="viewer__label">{label}</figcaption>}
</figure>
);
}
export interface ResidueColorRun {
chain: string;
start: number;
end: number;
color: string;
/** 0–1; below 1 draws the run semi-transparent */
opacity?: number;
}
export interface ResidueTooltip {
chain: string;
pos: number;
text: string;
}
/**
* Mol* view of a custom coordinate file (e.g. two superposed chains), coloured by
* residue runs. Residue numbers are the file's auth_seq_id.
*/
export function CustomStructureViewer({
url,
runs,
height = 460,
label,
fallbackColor = '#c3c9d2',
tooltips = [],
}: {
url: string;
runs: ResidueColorRun[];
height?: number;
label?: string;
fallbackColor?: string;
/** per-residue hover text, shown in place of the generic Mol* label */
tooltips?: ResidueTooltip[];
}) {
const ref = useRef<HTMLDivElement>(null);
const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading');
const runsKey = JSON.stringify(runs);
const tipsKey = JSON.stringify(tooltips);
useEffect(() => {
let alive = true;
let sub: { unsubscribe?: () => void } | undefined;
let instance: MolstarInstance | undefined;
const el = ref.current;
setStatus('loading');
const timeout = setTimeout(() => alive && setStatus((st) => (st === 'loading' ? 'error' : st)), 90000);
const data = (JSON.parse(runsKey) as ResidueColorRun[]).map((r) => ({
auth_asym_id: r.chain,
beg_auth_seq_id: r.start,
end_auth_seq_id: r.end,
color: hexToRgb(r.color),
...(r.opacity != null && r.opacity < 1 ? { opacity: r.opacity } : {}),
}));
const tips = (JSON.parse(tipsKey) as ResidueTooltip[]).map((t) => ({
auth_asym_id: t.chain,
beg_auth_seq_id: t.pos,
end_auth_seq_id: t.pos,
tooltip: t.text,
}));
loadMolstar()
.then(() => {
if (!alive || !el || !window.PDBeMolstarPlugin) return;
instance = new window.PDBeMolstarPlugin();
sub = instance.events.loadComplete.subscribe((ok) => {
if (!alive) return;
if (ok === false) {
setStatus('error');
return;
}
setStatus('ready');
// semi-transparent runs stay hoverable: Mol* only picks geometry at or
// above this opacity (default 0.5)
instance?.plugin?.canvas3d?.setProps({ renderer: { pickingAlphaThreshold: 0.05 } });
instance?.visual.select({ data, nonSelectedColor: hexToRgb(fallbackColor) });
if (tips.length) instance?.visual.tooltips?.({ data: tips });
});
const bg = isDark() ? { r: 18, g: 24, b: 33 } : { r: 255, g: 255, b: 255 };
instance.render(el, {
customData: { url: new URL(url, window.location.href).toString(), format: 'pdb', binary: false },
bgColor: bg,
hideControls: true,
sequencePanel: false,
landscape: true,
visualStyle: 'cartoon',
hideCanvasControls: ['expand', 'selection', 'animation', 'controlToggle', 'controlInfo'],
loadingOverlay: true,
});
})
.catch(() => alive && setStatus('error'));
return () => {
alive = false;
clearTimeout(timeout);
sub?.unsubscribe?.();
instance?.plugin?.dispose?.();
if (el) el.innerHTML = '';
};
}, [url, runsKey, tipsKey, fallbackColor]);
return (
<figure className="viewer" style={{ height }}>
<div ref={ref} className="viewer__canvas" />
{status !== 'ready' && (
<div className="viewer__overlay">
{status === 'loading' ? (
<>
<span className="spinner" aria-hidden /> Loading structures…
</>
) : (
<>
<Box aria-hidden /> 3D view unavailable
</>
)}
</div>
)}
{label && <figcaption className="viewer__label">{label}</figcaption>}
</figure>
);
}
|