Spaces:
Running
Running
File size: 16,435 Bytes
76fb273 2f67ecf 6c461b6 349f22c 2f67ecf 76fb273 973dd4c 76fb273 973dd4c 6c461b6 76fb273 973dd4c 76fb273 973dd4c 76fb273 5e98b5a c976c65 76fb273 150c960 c976c65 76fb273 c976c65 76fb273 bc59cdc 76fb273 c976c65 76fb273 c976c65 76fb273 2a12412 76fb273 2f67ecf 76fb273 c976c65 2f67ecf 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 2f67ecf 76fb273 c976c65 76fb273 c976c65 76fb273 150c960 76fb273 2f67ecf c976c65 2f67ecf c976c65 2f67ecf c976c65 2f67ecf c976c65 2f67ecf 76fb273 2f67ecf 76fb273 2f67ecf 76fb273 2f67ecf 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 5e98b5a 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 c976c65 76fb273 00b6857 c976c65 00b6857 906f65b 00b6857 906f65b 00b6857 76fb273 2f67ecf 76fb273 2f67ecf 76fb273 | 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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | 'use client';
import { useState } from 'react';
import { motion } from 'framer-motion';
import { useRouter } from 'next/navigation';
import { MagnifyingGlass as Search, Dna, CircleNotch as LoaderCircle, CheckCircle, WarningCircle as AlertCircle, BookOpen, ArrowRight, Flask as Beaker } from '@phosphor-icons/react';
import { fadeUp, stagger } from '@/lib/animations';
import { fetchSequence, validateSequence, searchSequences } from '@/lib/api';
import { extractErrorMessage } from '@/lib/errors';
import type { SequenceResult, SequenceValidation, SequenceSearchResult } from '@/types/pipeline';
type InputMode = 'accession' | 'sequence' | 'name';
export function SequenceRetrieval() {
const router = useRouter();
const [mode, setMode] = useState<InputMode>('accession');
const [input, setInput] = useState('');
const [loading, setLoading] = useState(false);
const [result, setResult] = useState<SequenceResult | null>(null);
const [validation, setValidation] = useState<SequenceValidation | null>(null);
const [searchResults, setSearchResults] = useState<SequenceSearchResult[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [copied, setCopied] = useState(false);
const handleSearch = async () => {
if (!input.trim()) return;
setLoading(true);
setError(null);
setResult(null);
setValidation(null);
setSearchResults(null);
try {
if (mode === 'accession') {
const res = await fetchSequence(input.trim());
if (res.error) {
setError(res.error);
} else {
setResult(res);
}
} else if (mode === 'sequence') {
const res = await validateSequence(input);
setValidation(res);
} else if (mode === 'name') {
const res = await searchSequences(input.trim());
if (res.error) {
setError(res.error);
} else {
setSearchResults(res.results);
}
}
} catch (err: unknown) {
setError(extractErrorMessage(err));
} finally {
setLoading(false);
}
};
const handleSelectAccession = async (accession: string) => {
setInput(accession);
setMode('accession');
setLoading(true);
setError(null);
setSearchResults(null);
try {
const res = await fetchSequence(accession);
if (res.error) setError(res.error);
else setResult(res);
} catch (err: unknown) {
setError(extractErrorMessage(err));
} finally {
setLoading(false);
}
};
const copySequence = () => {
if (result?.sequence) {
navigator.clipboard.writeText(result.sequence);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
};
const seqTypeColor = (type?: string) => {
switch (type) {
case 'protein': return 'text-molecule-protein bg-molecule-protein/10';
case 'dna': return 'text-molecule-dna bg-molecule-dna/10';
case 'rna': return 'text-molecule-rna bg-molecule-rna/10';
default: return 'text-text-muted bg-surface-0';
}
};
return (
<motion.div initial={{ y: 24 }} animate="show" variants={stagger} className="space-y-6">
<motion.div variants={fadeUp} className="bg-surface-0 rounded-2xl border border-glass-border p-6">
<h2 className="text-lg font-semibold text-text-primary mb-1">Sequence Retrieval</h2>
<p className="text-sm text-text-muted mb-6">
Look up a sequence by accession number, paste raw sequence data, or search by gene/protein name
</p>
<div className="flex gap-2 mb-6">
{([
{ id: 'accession' as const, label: 'Accession' },
{ id: 'name' as const, label: 'Gene / Protein Name' },
{ id: 'sequence' as const, label: 'Raw Sequence' },
]).map((m) => (
<button
key={m.id}
onClick={() => { setMode(m.id); setResult(null); setValidation(null); setSearchResults(null); setError(null); }}
className={`px-4 py-2 text-sm font-medium rounded-lg transition ${
mode === m.id ? 'bg-accent-cyan text-void' : 'bg-surface-0 text-text-muted hover:bg-surface-2'
}`}
>
{m.label}
</button>
))}
</div>
<div className="flex gap-3">
<input
type="text"
value={input}
onChange={(e) => { setInput(e.target.value); setResult(null); setValidation(null); setSearchResults(null); setError(null); }}
onKeyDown={(e) => e.key === 'Enter' && handleSearch()}
placeholder={
mode === 'accession' ? 'e.g. NP_000509.1, P04637, 1TIM' :
mode === 'name' ? 'e.g. p53, BRCA1, TP53 human' :
'Paste FASTA or raw sequence...'
}
className="flex-1 px-4 py-3 rounded-xl border border-glass-border focus:border-accent-cyan focus:ring-2 focus:ring-accent-cyan/20 outline-none transition text-sm"
/>
<button
onClick={handleSearch}
disabled={loading || !input.trim()}
className="px-6 py-3 bg-accent-cyan text-void font-medium rounded-xl hover:bg-accent-hover transition disabled:opacity-50 flex items-center gap-2"
>
{loading ? <LoaderCircle className="w-4 h-4 animate-spin" /> : <Search className="w-4 h-4" />}
{loading ? 'Searching...' : 'Search'}
</button>
</div>
</motion.div>
{error && (
<motion.div variants={fadeUp} className="bg-error-dim border border-error/20 rounded-2xl p-4 flex items-start gap-3">
<AlertCircle className="w-5 h-5 text-error mt-0.5 shrink-0" />
<p className="text-sm text-error">{error}</p>
</motion.div>
)}
{validation && mode === 'sequence' && (
<motion.div variants={fadeUp} className="bg-surface-0 rounded-2xl border border-glass-border p-6">
<div className="flex items-center gap-3 mb-4">
{validation.valid ? (
<CheckCircle className="w-5 h-5 text-accent-cyan" />
) : (
<AlertCircle className="w-5 h-5 text-error" />
)}
<h3 className="font-semibold text-text-primary">
{validation.valid ? 'Valid Sequence' : 'Invalid Sequence'}
</h3>
</div>
<div className="grid grid-cols-3 gap-4">
<div className="p-3 bg-surface-0 rounded-xl">
<div className="text-xs text-text-muted mb-1">Type</div>
<div className="text-sm font-medium text-text-primary">
<span className={`inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium ${seqTypeColor(validation.sequence_type)}`}>
{validation.sequence_type}
</span>
</div>
</div>
<div className="p-3 bg-surface-0 rounded-xl">
<div className="text-xs text-text-muted mb-1">Length</div>
<div className="text-sm font-medium text-text-primary">{validation.length.toLocaleString()} residues</div>
</div>
<div className="p-3 bg-surface-0 rounded-xl">
<div className="text-xs text-text-muted mb-1">Format</div>
<div className="text-sm font-medium text-text-primary">{validation.format}</div>
</div>
</div>
{validation.issues.length > 0 && (
<div className="mt-4 p-3 bg-error-dim rounded-xl">
<p className="text-xs text-error font-medium mb-1">Issues</p>
{validation.issues.map((issue, i) => (
<p key={i} className="text-sm text-error">{issue}</p>
))}
</div>
)}
</motion.div>
)}
{searchResults && mode === 'name' && (
<motion.div variants={fadeUp} className="bg-surface-0 rounded-2xl border border-glass-border p-6">
<h3 className="font-semibold text-text-primary mb-4">Search Results ({searchResults.length})</h3>
{searchResults.length === 0 ? (
<p className="text-sm text-text-muted">No results found</p>
) : (
<motion.div animate="show" variants={stagger} className="space-y-2">
{searchResults.map((r, i) => (
<motion.div key={i} variants={fadeUp}>
<button
onClick={() => handleSelectAccession(r.accession)}
className="w-full text-left p-4 rounded-xl border border-glass-border hover:border-accent-cyan/30 hover:bg-accent-cyan/10 transition"
>
<div className="flex items-center justify-between">
<div>
<div className="flex items-center gap-2">
<code className="text-sm font-mono text-accent-cyan">{r.accession}</code>
<ArrowRight className="w-3 h-3 text-text-muted" />
</div>
<p className="text-sm text-text-secondary mt-1 line-clamp-1">{r.title}</p>
</div>
<div className="text-right text-xs text-text-muted">
<div>{r.organism}</div>
<div>{r.length} aa</div>
</div>
</div>
</button>
</motion.div>
))}
</motion.div>
)}
</motion.div>
)}
{result && mode === 'accession' && (
<motion.div variants={fadeUp} className="bg-surface-0 rounded-2xl border border-glass-border divide-y divide-glass-border">
<div className="p-6">
<div className="flex items-start justify-between mb-4">
<div>
<div className="flex items-center gap-3 mb-2">
<code className="text-lg font-mono font-bold text-accent-cyan">{result.accession}</code>
<span className={`inline-flex items-center px-2 py-0.5 rounded-full text-xs font-medium ${seqTypeColor(result.sequence_type)}`}>
{result.sequence_type}
</span>
<span className="text-xs bg-surface-0 text-text-muted px-2 py-0.5 rounded-full">{result.db_source}</span>
</div>
<p className="text-sm text-text-secondary">{result.description}</p>
{result.organism && (
<p className="text-xs text-text-muted mt-1">{result.organism}</p>
)}
</div>
<div className="text-right">
<div className="text-2xl font-bold text-text-primary">{result.length.toLocaleString()}</div>
<div className="text-xs text-text-muted">residues</div>
</div>
</div>
<div className="bg-surface-0 rounded-xl p-4">
<div className="flex items-center justify-between mb-2">
<span className="text-xs font-medium text-text-muted">Sequence</span>
<button
onClick={copySequence}
className="text-xs text-accent-cyan hover:text-accent-cyan font-medium"
>
{copied ? 'Copied!' : 'Copy'}
</button>
</div>
<pre className="font-mono text-xs text-text-secondary overflow-auto max-h-32 break-all whitespace-pre-wrap">
{result.sequence}
</pre>
</div>
</div>
{(result.gene_names && result.gene_names.length > 0) && (
<div className="px-6 py-4">
<h4 className="text-xs font-medium text-text-muted uppercase tracking-wider mb-2">Gene Names</h4>
<div className="flex flex-wrap gap-2">
{result.gene_names.map((g, i) => (
<span key={i} className="px-2 py-1 bg-surface-0 text-text-secondary rounded-lg text-xs font-mono">{g}</span>
))}
</div>
</div>
)}
{result.functions && result.functions.length > 0 && (
<div className="px-6 py-4">
<h4 className="text-xs font-medium text-text-muted uppercase tracking-wider mb-2">Functions</h4>
{result.functions.map((f, i) => (
<p key={i} className="text-sm text-text-secondary mb-1">{f}</p>
))}
</div>
)}
{result.pdb_ids && result.pdb_ids.length > 0 && (
<div className="px-6 py-4">
<h4 className="text-xs font-medium text-text-muted uppercase tracking-wider mb-2">PDB Structures</h4>
<div className="flex flex-wrap gap-2">
{result.pdb_ids.map((pdb, i) => (
<a
key={i}
href={`https://www.rcsb.org/structure/${pdb}`}
target="_blank"
rel="noopener noreferrer"
className="px-2 py-1 bg-info/10 text-info rounded-lg text-xs font-mono hover:bg-info/10"
>
{pdb}
</a>
))}
</div>
</div>
)}
{result.features && result.features.length > 0 && (
<div className="px-6 py-4">
<h4 className="text-xs font-medium text-text-muted uppercase tracking-wider mb-2">Features</h4>
<div className="space-y-1">
{result.features.slice(0, 10).map((f, i) => (
<div key={i} className="flex items-center gap-3 text-sm">
<span className="px-2 py-0.5 bg-surface-0 text-text-muted rounded text-xs font-mono">{f.type}</span>
<span className="text-text-secondary">{f.description}</span>
{f.begin && f.end && (
<span className="text-xs text-text-muted ml-auto">{f.begin}-{f.end}</span>
)}
</div>
))}
{result.features.length > 10 && (
<p className="text-xs text-text-muted mt-1">+{result.features.length - 10} more features</p>
)}
</div>
</div>
)}
<div className="px-6 py-4 flex items-center justify-between">
<div className="flex items-center gap-2 text-xs text-text-muted">
<BookOpen className="w-3 h-3" />
<span>Source: {result.db_source} {result.from_cache && '(cached)'}</span>
</div>
<div className="flex items-center gap-2 flex-wrap">
<button
onClick={() => {
sessionStorage.setItem('blast_sequence', `>${result.accession}\n${result.sequence}`);
router.push('/analyze/blast');
}}
className="flex items-center gap-2 px-4 py-2 bg-accent-cyan text-void text-sm font-medium rounded-xl hover:bg-accent-hover transition"
>
<Beaker className="w-4 h-4" />
Analyze with BLAST
</button>
{(result.sequence_type === 'dna' || result.sequence_type === 'rna') && (
<>
<button
onClick={() => {
sessionStorage.setItem('primer_sequence', result.sequence);
router.push('/analyze/primers');
}}
className="flex items-center gap-2 px-4 py-2 border border-accent-purple/40 text-accent-purple text-sm font-medium rounded-xl hover:bg-accent-purple/10 transition"
>
<Dna className="w-4 h-4" />
Design Primers
</button>
<button
onClick={() => {
sessionStorage.setItem('cds_sequence', result.sequence);
router.push('/analyze/tools');
}}
className="flex items-center gap-2 px-4 py-2 border border-accent-amber/40 text-accent-amber text-sm font-medium rounded-xl hover:bg-accent-amber/10 transition"
>
<Dna className="w-4 h-4" />
Translate CDS
</button>
</>
)}
</div>
</div>
</motion.div>
)}
</motion.div>
);
}
|