Download source/src/SequenceFold.tsx from CladeTeam/ouroboros: direct link, hf CLI and curl.
- Browser
- Download file 7.56 kB
-
https://huggingface.co/spaces/CladeTeam/ouroboros/resolve/main/source/src/SequenceFold.tsx
- Command line
-
hf download hf://spaces/CladeTeam/ouroboros/source/src/SequenceFold.tsx
-
curl -L -o SequenceFold.tsx https://huggingface.co/spaces/CladeTeam/ouroboros/resolve/main/source/src/SequenceFold.tsx
7.56 kB
| import {useCallback, useEffect, useRef, useState} from 'react'; | |
| import {useMotion} from './Motion'; | |
| import type {ProteinCase} from './types'; | |
| type Point={x:number;y:number;z:number}; | |
| /** Interpolate the actual Cα coordinates for orientation, not simulated folding dynamics. */ | |
| export function SequenceFold({protein}:{protein:ProteinCase}){ | |
| const host=useRef<HTMLDivElement>(null), canvas=useRef<HTMLCanvasElement>(null); | |
| const phase=useRef(0),angle=useRef(.4),drawingSize=useRef({w:800,h:300,dpr:1}); | |
| const [points,setPoints]=useState<Point[]>([]),[visible,setVisible]=useState(false),[load,setLoad]=useState(false),[error,setError]=useState(false); | |
| const {enabled,reduced}=useMotion(); | |
| const [percent,setPercent]=useState(()=>reduced?100:0),[playing,setPlaying]=useState(()=>!reduced); | |
| const drag=useRef<{x:number;angle:number}|null>(null); | |
| useEffect(()=>{ | |
| const observer=new IntersectionObserver(([entry])=>{setVisible(entry.isIntersecting);if(entry.isIntersecting)setLoad(true);},{rootMargin:'0px',threshold:.1}); | |
| if(host.current)observer.observe(host.current); | |
| return()=>observer.disconnect(); | |
| },[]); | |
| useEffect(()=>{ | |
| if(!load)return; | |
| const ac=new AbortController(); | |
| fetch(protein.pdbUrl,{signal:ac.signal}).then(r=>{if(!r.ok)throw new Error('Structure unavailable');return r.text();}).then(text=>{ | |
| const coords=text.split('\n').filter(line=>line.startsWith('ATOM ')&&line.slice(12,16).trim()==='CA').map(line=>({x:Number(line.slice(30,38)),y:Number(line.slice(38,46)),z:Number(line.slice(46,54))})); | |
| if(coords.length!==protein.length||coords.some(p=>!Number.isFinite(p.x+p.y+p.z)))throw new Error('Invalid backbone'); | |
| const center=coords.reduce((a,p)=>({x:a.x+p.x/coords.length,y:a.y+p.y/coords.length,z:a.z+p.z/coords.length}),{x:0,y:0,z:0}); | |
| setPoints(coords.map(p=>({x:p.x-center.x,y:p.y-center.y,z:p.z-center.z}))); | |
| }).catch(()=>{if(!ac.signal.aborted)setError(true);}); | |
| return()=>ac.abort(); | |
| },[load,protein.pdbUrl,protein.length]); | |
| const draw=useCallback(()=>{ | |
| const ctx=canvas.current?.getContext('2d');if(!ctx)return; | |
| const {w,h,dpr}=drawingSize.current; | |
| ctx.setTransform(dpr,0,0,dpr,0,0);ctx.clearRect(0,0,w,h); | |
| if(!points.length)return; | |
| const p=phase.current, ease=p*p*(3-2*p); | |
| const cos=Math.cos(angle.current),sin=Math.sin(angle.current); | |
| const rotated=points.map(v=>{ | |
| const x=v.x*cos-v.z*sin,z=v.x*sin+v.z*cos; | |
| const y=v.y*Math.cos(.45)-z*Math.sin(.45); | |
| return {x:x*Math.cos(.3)-y*Math.sin(.3),y:x*Math.sin(.3)+y*Math.cos(.3),z}; | |
| }); | |
| const minX=Math.min(...rotated.map(v=>v.x)),maxX=Math.max(...rotated.map(v=>v.x)); | |
| const minY=Math.min(...rotated.map(v=>v.y)),maxY=Math.max(...rotated.map(v=>v.y)); | |
| const scale=Math.min((w-65)/(maxX-minX),(h-70)/(maxY-minY)); | |
| const projected=rotated.map((v,i)=>{ | |
| const x=(v.x-(minX+maxX)/2)*scale,y=(v.y-(minY+maxY)/2)*scale; | |
| const lineX=(i/(points.length-1)-.5)*(w-50); | |
| const lineY=Math.sin(i/(points.length-1)*Math.PI*5)*12; | |
| return {x:w/2+lineX*(1-ease)+x*ease,y:h/2+lineY*(1-ease)+y*ease,z:v.z*ease}; | |
| }); | |
| // Subtle coordinate field anchors the molecular silhouette. | |
| ctx.strokeStyle='#aabbcb1a';ctx.lineWidth=1; | |
| for(let x=24;x<w;x+=36){ctx.beginPath();ctx.moveTo(x,20);ctx.lineTo(x,h-20);ctx.stroke();} | |
| for(let y=24;y<h;y+=36){ctx.beginPath();ctx.moveTo(20,y);ctx.lineTo(w-20,y);ctx.stroke();} | |
| for(let i=1;i<projected.length;i++){ | |
| const a=projected[i-1],b=projected[i]; | |
| ctx.strokeStyle=`hsla(${35-i/points.length*29}, 62%, ${62+b.z/18}%, .8)`; | |
| ctx.lineWidth=1.4+ease*.7;ctx.beginPath();ctx.moveTo(a.x,a.y);ctx.lineTo(b.x,b.y);ctx.stroke(); | |
| } | |
| projected.forEach((v,i)=>{ | |
| ctx.fillStyle=`hsl(${35-i/points.length*29}, 66%, ${66+v.z/20}%)`; | |
| ctx.beginPath();ctx.arc(v.x,v.y,(1.3+ease*.8),0,Math.PI*2);ctx.fill(); | |
| }); | |
| const first=projected[0],last=projected.at(-1)!; | |
| ctx.font='11px "DM Sans", sans-serif';ctx.textAlign='center'; | |
| ctx.fillStyle='#f2bc7a';ctx.fillText('N',first.x,first.y-13); | |
| ctx.fillStyle='#f37f68';ctx.fillText('C',last.x,last.y+20); | |
| },[points]); | |
| useEffect(()=>{ | |
| if(!canvas.current)return; | |
| const el=canvas.current; | |
| const observer=new ResizeObserver(()=>{ | |
| const rect=el.getBoundingClientRect(),dpr=Math.min(devicePixelRatio||1,2); | |
| drawingSize.current={w:rect.width,h:rect.height,dpr}; | |
| el.width=Math.round(rect.width*dpr);el.height=Math.round(rect.height*dpr);draw(); | |
| });observer.observe(el);return()=>observer.disconnect(); | |
| },[draw]); | |
| useEffect(()=>{draw();},[draw,percent]); | |
| useEffect(()=>{if(reduced){phase.current=1;setPercent(100);setPlaying(false);draw();}},[reduced,draw]); | |
| useEffect(()=>{ | |
| if(!playing||!enabled||!visible||!points.length)return; | |
| let frame=0,last=0; | |
| const tick=(now:number)=>{ | |
| if(last)phase.current=Math.min(1,phase.current+Math.min(now-last,60)/8000); | |
| last=now;setPercent(Math.round(phase.current*100));draw(); | |
| if(phase.current<1)frame=requestAnimationFrame(tick);else setPlaying(false); | |
| }; | |
| frame=requestAnimationFrame(tick);return()=>cancelAnimationFrame(frame); | |
| },[playing,enabled,visible,points,draw]); | |
| const replay=()=>{if(phase.current>=1){phase.current=0;setPercent(0);}setPlaying(v=>!v);}; | |
| return <div ref={host} className="fold-story" data-fold-state={error?'error':!points.length?'loading':playing&&enabled&&visible?'playing':'paused'}> | |
| <div className="fold-copy"><p className="eyebrow">SEQUENCE → STRUCTURE</p><h3>One sequence.<br/>Another dimension.</h3><p>Follow {protein.length} residues into the predicted backbone of DE2, then explore the design and its measured PET degradation products below.</p><div className="fold-identity"><strong>{protein.id.toUpperCase()}</strong><span>{protein.length} amino acids<br/>ESMFold-v1 prediction</span></div></div> | |
| <div className="fold-experience"><div className="fold-topline"><span className="micro">Cα BACKBONE · N → C</span><span>{percent===100?'PREDICTED STRUCTURE':percent===0?'SEQUENCE ORDER':'SEQUENCE → STRUCTURE'}</span></div> | |
| <div className="fold-canvas-wrap"><canvas ref={canvas} role="img" aria-label={`Backbone illustration of the ${protein.length}-residue DE2 PETase. The slider interpolates sequence order into the predicted coordinates.`} onPointerDown={e=>{drag.current={x:e.clientX,angle:angle.current};e.currentTarget.setPointerCapture(e.pointerId);}} onPointerMove={e=>{if(drag.current){angle.current=drag.current.angle+(e.clientX-drag.current.x)*.01;draw();}}} onPointerUp={()=>{drag.current=null;}} onPointerCancel={()=>{drag.current=null;}}/>{!points.length&&<p className="fold-loading" role="status">{error?'Backbone preview unavailable. The structure explorer below remains available.':'Loading the predicted backbone…'}</p>}</div> | |
| <div className="fold-controls"><button onClick={replay} disabled={!enabled||!points.length} aria-label={playing&&enabled?'Pause structure transformation':percent===100?'Replay structure transformation':'Play structure transformation'}>{playing&&enabled?'Ⅱ Pause':percent===100?'↻ Replay':'▷ Play'}</button><label><span>Sequence order</span><input type="range" min="0" max="100" value={percent} disabled={!points.length} aria-label="Sequence to predicted structure" aria-valuetext={`${percent}% visual interpolation toward the predicted backbone`} onChange={e=>{setPlaying(false);phase.current=Number(e.target.value)/100;setPercent(Number(e.target.value));draw();}}/><span>Predicted fold</span></label></div> | |
| <p className="fold-note">Drag to rotate · Visual interpolation of predicted coordinates, not a simulated folding trajectory.</p> | |
| </div> | |
| </div>; | |
| } | |