Spaces:
Running
Running
AlphaFold 3D viewer via 3Dmol.js — pLDDT confidence coloring, style selector, spin toggle
Browse files- Self-contained AlphaFoldViewer component (client-only, global namespace)
- Loads 3Dmol-min.js on demand via <script>
- Colors structure by per-residue pLDDT (B-factor column) using AF DB standard scheme
- 4 display modes: cartoon·pLDDT, cartoon·spectrum, surface·pLDDT, stick·pLDDT
- pLDDT confidence legend in footer
- Wired into /results/[jobId] and /shared/[token]
bioai-platform/frontend/src/app/(dashboard)/results/[jobId]/page.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import type { JobStatus } from '@/types/pipeline';
|
|
| 8 |
import AIInterpretation from '@/components/results/AIInterpretation';
|
| 9 |
import BlastPanel from '@/components/results/BlastPanel';
|
| 10 |
import UniprotPanel from '@/components/results/UniprotPanel';
|
| 11 |
-
import
|
| 12 |
|
| 13 |
export default function ResultsPage() {
|
| 14 |
const params = useParams();
|
|
@@ -136,8 +136,11 @@ export default function ResultsPage() {
|
|
| 136 |
)}
|
| 137 |
</div>
|
| 138 |
|
| 139 |
-
{context.alphafold && (
|
| 140 |
-
<
|
|
|
|
|
|
|
|
|
|
| 141 |
)}
|
| 142 |
</div>
|
| 143 |
);
|
|
|
|
| 8 |
import AIInterpretation from '@/components/results/AIInterpretation';
|
| 9 |
import BlastPanel from '@/components/results/BlastPanel';
|
| 10 |
import UniprotPanel from '@/components/results/UniprotPanel';
|
| 11 |
+
import AlphaFoldViewer from '@/components/AlphaFoldViewer';
|
| 12 |
|
| 13 |
export default function ResultsPage() {
|
| 14 |
const params = useParams();
|
|
|
|
| 136 |
)}
|
| 137 |
</div>
|
| 138 |
|
| 139 |
+
{context.alphafold && context.alphafold.structure_available && (
|
| 140 |
+
<AlphaFoldViewer
|
| 141 |
+
pdbUrl={context.alphafold.pdb_url}
|
| 142 |
+
uniprotId={context.alphafold.uniprot_accession}
|
| 143 |
+
/>
|
| 144 |
)}
|
| 145 |
</div>
|
| 146 |
);
|
bioai-platform/frontend/src/app/shared/[token]/page.tsx
CHANGED
|
@@ -8,7 +8,7 @@ import { getSharedResult } from '@/lib/api';
|
|
| 8 |
import AIInterpretation from '@/components/results/AIInterpretation';
|
| 9 |
import BlastPanel from '@/components/results/BlastPanel';
|
| 10 |
import UniprotPanel from '@/components/results/UniprotPanel';
|
| 11 |
-
import
|
| 12 |
|
| 13 |
export default function SharedResultPage() {
|
| 14 |
const params = useParams();
|
|
@@ -76,7 +76,9 @@ export default function SharedResultPage() {
|
|
| 76 |
{context.uniprot && <UniprotPanel data={context.uniprot} />}
|
| 77 |
</div>
|
| 78 |
|
| 79 |
-
{context.alphafold &&
|
|
|
|
|
|
|
| 80 |
</div>
|
| 81 |
)}
|
| 82 |
</div>
|
|
|
|
| 8 |
import AIInterpretation from '@/components/results/AIInterpretation';
|
| 9 |
import BlastPanel from '@/components/results/BlastPanel';
|
| 10 |
import UniprotPanel from '@/components/results/UniprotPanel';
|
| 11 |
+
import AlphaFoldViewer from '@/components/AlphaFoldViewer';
|
| 12 |
|
| 13 |
export default function SharedResultPage() {
|
| 14 |
const params = useParams();
|
|
|
|
| 76 |
{context.uniprot && <UniprotPanel data={context.uniprot} />}
|
| 77 |
</div>
|
| 78 |
|
| 79 |
+
{context.alphafold?.structure_available && (
|
| 80 |
+
<AlphaFoldViewer pdbUrl={context.alphafold.pdb_url} uniprotId={context.alphafold.uniprot_accession} />
|
| 81 |
+
)}
|
| 82 |
</div>
|
| 83 |
)}
|
| 84 |
</div>
|
bioai-platform/frontend/src/components/AlphaFoldViewer.tsx
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'use client';
|
| 2 |
+
|
| 3 |
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
| 4 |
+
|
| 5 |
+
declare global {
|
| 6 |
+
interface Window {
|
| 7 |
+
$3Dmol: any;
|
| 8 |
+
}
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
const SCRIPT_URL = 'https://3Dmol.org/build/3Dmol-min.js';
|
| 12 |
+
|
| 13 |
+
const CONFIDENCE_BANDS = [
|
| 14 |
+
{ label: 'Very high (pLDDT > 90)', color: '#0053D6' },
|
| 15 |
+
{ label: 'Confident (70–90)', color: '#65CBF3' },
|
| 16 |
+
{ label: 'Low (50–70)', color: '#FFDB13' },
|
| 17 |
+
{ label: 'Very low (< 50)', color: '#FF7D45' },
|
| 18 |
+
] as const;
|
| 19 |
+
|
| 20 |
+
function plddtColor(b: number): string {
|
| 21 |
+
if (b > 90) return CONFIDENCE_BANDS[0].color;
|
| 22 |
+
if (b > 70) return CONFIDENCE_BANDS[1].color;
|
| 23 |
+
if (b > 50) return CONFIDENCE_BANDS[2].color;
|
| 24 |
+
return CONFIDENCE_BANDS[3].color;
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
let scriptPromise: Promise<void> | null = null;
|
| 28 |
+
|
| 29 |
+
function load3Dmol(): Promise<void> {
|
| 30 |
+
if (typeof window === 'undefined') {
|
| 31 |
+
return Promise.reject(new Error('3Dmol can only run in the browser'));
|
| 32 |
+
}
|
| 33 |
+
if (window.$3Dmol) return Promise.resolve();
|
| 34 |
+
if (scriptPromise) return scriptPromise;
|
| 35 |
+
|
| 36 |
+
scriptPromise = new Promise((resolve, reject) => {
|
| 37 |
+
const existing = document.querySelector<HTMLScriptElement>(`script[src="${SCRIPT_URL}"]`);
|
| 38 |
+
if (existing) {
|
| 39 |
+
existing.addEventListener('load', () => resolve());
|
| 40 |
+
existing.addEventListener('error', () => reject(new Error('Failed to load 3Dmol.js')));
|
| 41 |
+
return;
|
| 42 |
+
}
|
| 43 |
+
const script = document.createElement('script');
|
| 44 |
+
script.src = SCRIPT_URL;
|
| 45 |
+
script.async = true;
|
| 46 |
+
script.onload = () => resolve();
|
| 47 |
+
script.onerror = () => reject(new Error('Failed to load 3Dmol.js'));
|
| 48 |
+
document.head.appendChild(script);
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
+
return scriptPromise;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
type StyleMode = 'confidence' | 'spectrum' | 'surface' | 'stick';
|
| 55 |
+
|
| 56 |
+
interface AlphaFoldViewerProps {
|
| 57 |
+
pdbUrl?: string | null;
|
| 58 |
+
uniprotId?: string;
|
| 59 |
+
height?: number | string;
|
| 60 |
+
backgroundColor?: string;
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
export default function AlphaFoldViewer({
|
| 64 |
+
pdbUrl,
|
| 65 |
+
uniprotId,
|
| 66 |
+
height = 420,
|
| 67 |
+
backgroundColor = '#0d1117',
|
| 68 |
+
}: AlphaFoldViewerProps) {
|
| 69 |
+
const containerRef = useRef<HTMLDivElement>(null);
|
| 70 |
+
const viewerRef = useRef<any>(null);
|
| 71 |
+
|
| 72 |
+
const [status, setStatus] = useState<'loading' | 'ready' | 'error'>('loading');
|
| 73 |
+
const [error, setError] = useState<string | null>(null);
|
| 74 |
+
const [styleMode, setStyleMode] = useState<StyleMode>('confidence');
|
| 75 |
+
const [spinning, setSpinning] = useState(false);
|
| 76 |
+
|
| 77 |
+
const applyStyle = useCallback((mode: StyleMode) => {
|
| 78 |
+
const viewer = viewerRef.current;
|
| 79 |
+
const $3Dmol = window.$3Dmol;
|
| 80 |
+
if (!viewer || !$3Dmol) return;
|
| 81 |
+
|
| 82 |
+
viewer.removeAllSurfaces();
|
| 83 |
+
viewer.setStyle({}, {});
|
| 84 |
+
|
| 85 |
+
switch (mode) {
|
| 86 |
+
case 'confidence':
|
| 87 |
+
viewer.setStyle({}, { cartoon: { colorfunc: (atom: any) => plddtColor(atom.b) } });
|
| 88 |
+
break;
|
| 89 |
+
case 'spectrum':
|
| 90 |
+
viewer.setStyle({}, { cartoon: { color: 'spectrum' } });
|
| 91 |
+
break;
|
| 92 |
+
case 'surface':
|
| 93 |
+
viewer.setStyle({}, { cartoon: { colorfunc: (atom: any) => plddtColor(atom.b) } });
|
| 94 |
+
viewer.addSurface($3Dmol.SurfaceType.VDW, {
|
| 95 |
+
opacity: 0.85,
|
| 96 |
+
colorfunc: (atom: any) => plddtColor(atom.b),
|
| 97 |
+
});
|
| 98 |
+
break;
|
| 99 |
+
case 'stick':
|
| 100 |
+
viewer.setStyle({}, { stick: { colorfunc: (atom: any) => plddtColor(atom.b) } });
|
| 101 |
+
break;
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
viewer.render();
|
| 105 |
+
}, []);
|
| 106 |
+
|
| 107 |
+
useEffect(() => {
|
| 108 |
+
if (!pdbUrl) {
|
| 109 |
+
setStatus('error');
|
| 110 |
+
setError('No structure URL provided yet');
|
| 111 |
+
return;
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
let cancelled = false;
|
| 115 |
+
setStatus('loading');
|
| 116 |
+
setError(null);
|
| 117 |
+
|
| 118 |
+
load3Dmol()
|
| 119 |
+
.then(() => fetch(pdbUrl))
|
| 120 |
+
.then((res) => {
|
| 121 |
+
if (!res.ok) throw new Error(`Failed to fetch structure (HTTP ${res.status})`);
|
| 122 |
+
return res.text();
|
| 123 |
+
})
|
| 124 |
+
.then((pdbData) => {
|
| 125 |
+
if (cancelled || !containerRef.current) return;
|
| 126 |
+
const $3Dmol = window.$3Dmol;
|
| 127 |
+
|
| 128 |
+
if (viewerRef.current) {
|
| 129 |
+
viewerRef.current.clear();
|
| 130 |
+
} else {
|
| 131 |
+
viewerRef.current = $3Dmol.createViewer(containerRef.current, { backgroundColor });
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
viewerRef.current.addModel(pdbData, 'pdb');
|
| 135 |
+
applyStyle(styleMode);
|
| 136 |
+
viewerRef.current.zoomTo();
|
| 137 |
+
viewerRef.current.render();
|
| 138 |
+
setStatus('ready');
|
| 139 |
+
})
|
| 140 |
+
.catch((err: Error) => {
|
| 141 |
+
if (cancelled) return;
|
| 142 |
+
setError(
|
| 143 |
+
err.message === 'Failed to fetch'
|
| 144 |
+
? 'Could not fetch the structure file (likely a CORS issue — see notes below)'
|
| 145 |
+
: err.message
|
| 146 |
+
);
|
| 147 |
+
setStatus('error');
|
| 148 |
+
});
|
| 149 |
+
|
| 150 |
+
return () => {
|
| 151 |
+
cancelled = true;
|
| 152 |
+
};
|
| 153 |
+
}, [pdbUrl, backgroundColor, applyStyle, styleMode]);
|
| 154 |
+
|
| 155 |
+
useEffect(() => {
|
| 156 |
+
if (status === 'ready') applyStyle(styleMode);
|
| 157 |
+
}, [styleMode, status, applyStyle]);
|
| 158 |
+
|
| 159 |
+
useEffect(() => {
|
| 160 |
+
const viewer = viewerRef.current;
|
| 161 |
+
if (!viewer) return;
|
| 162 |
+
if (spinning) viewer.spin('y', 1);
|
| 163 |
+
else viewer.spin(false);
|
| 164 |
+
}, [spinning, status]);
|
| 165 |
+
|
| 166 |
+
useEffect(() => {
|
| 167 |
+
const viewer = viewerRef.current;
|
| 168 |
+
const container = containerRef.current;
|
| 169 |
+
if (!viewer || !container) return;
|
| 170 |
+
|
| 171 |
+
const observer = new ResizeObserver(() => viewer.resize());
|
| 172 |
+
observer.observe(container);
|
| 173 |
+
return () => observer.disconnect();
|
| 174 |
+
}, [status]);
|
| 175 |
+
|
| 176 |
+
return (
|
| 177 |
+
<div className="w-full overflow-hidden rounded-lg border border-white/10 bg-[#0d1117]">
|
| 178 |
+
<div className="flex flex-wrap items-center justify-between gap-2 border-b border-white/10 px-3 py-2">
|
| 179 |
+
<span className="font-mono text-xs text-white/60">
|
| 180 |
+
{uniprotId ? `AlphaFold model — ${uniprotId}` : 'AlphaFold model'}
|
| 181 |
+
</span>
|
| 182 |
+
|
| 183 |
+
<div className="flex items-center gap-2">
|
| 184 |
+
<select
|
| 185 |
+
value={styleMode}
|
| 186 |
+
onChange={(e) => setStyleMode(e.target.value as StyleMode)}
|
| 187 |
+
disabled={status !== 'ready'}
|
| 188 |
+
className="rounded border border-white/10 bg-black/30 px-2 py-1 text-xs text-white/80 disabled:opacity-40"
|
| 189 |
+
>
|
| 190 |
+
<option value="confidence">Cartoon · pLDDT</option>
|
| 191 |
+
<option value="spectrum">Cartoon · spectrum</option>
|
| 192 |
+
<option value="surface">Surface · pLDDT</option>
|
| 193 |
+
<option value="stick">Stick · pLDDT</option>
|
| 194 |
+
</select>
|
| 195 |
+
<button
|
| 196 |
+
type="button"
|
| 197 |
+
onClick={() => setSpinning((s) => !s)}
|
| 198 |
+
disabled={status !== 'ready'}
|
| 199 |
+
className="rounded border border-white/10 bg-black/30 px-2 py-1 text-xs text-white/80 hover:bg-black/50 disabled:opacity-40"
|
| 200 |
+
>
|
| 201 |
+
{spinning ? 'Stop spin' : 'Spin'}
|
| 202 |
+
</button>
|
| 203 |
+
</div>
|
| 204 |
+
</div>
|
| 205 |
+
|
| 206 |
+
<div className="relative" style={{ height }}>
|
| 207 |
+
<div ref={containerRef} className="absolute inset-0" />
|
| 208 |
+
|
| 209 |
+
{status === 'loading' && (
|
| 210 |
+
<div className="absolute inset-0 flex items-center justify-center bg-[#0d1117]/80 text-sm text-white/60">
|
| 211 |
+
Loading structure...
|
| 212 |
+
</div>
|
| 213 |
+
)}
|
| 214 |
+
|
| 215 |
+
{status === 'error' && (
|
| 216 |
+
<div className="absolute inset-0 flex items-center justify-center bg-[#0d1117]/90 px-6 text-center text-sm text-red-400">
|
| 217 |
+
{error}
|
| 218 |
+
</div>
|
| 219 |
+
)}
|
| 220 |
+
</div>
|
| 221 |
+
|
| 222 |
+
{status === 'ready' && styleMode !== 'spectrum' && (
|
| 223 |
+
<div className="flex flex-wrap items-center gap-3 border-t border-white/10 px-3 py-2 text-[11px] text-white/60">
|
| 224 |
+
<span className="font-mono uppercase tracking-wide text-white/40">pLDDT confidence</span>
|
| 225 |
+
{CONFIDENCE_BANDS.map((band) => (
|
| 226 |
+
<span key={band.label} className="flex items-center gap-1">
|
| 227 |
+
<span className="inline-block h-2.5 w-2.5 rounded-sm" style={{ backgroundColor: band.color }} />
|
| 228 |
+
{band.label}
|
| 229 |
+
</span>
|
| 230 |
+
))}
|
| 231 |
+
</div>
|
| 232 |
+
)}
|
| 233 |
+
</div>
|
| 234 |
+
);
|
| 235 |
+
}
|