WINTER4000 commited on
Commit
a7f536d
Β·
verified Β·
1 Parent(s): 8e84e10

CRISPR MVP: Cas9 knockout gRNA designer (sign-in gated, free for accounts)

Browse files
Files changed (1) hide show
  1. dee/static/app.js +192 -1
dee/static/app.js CHANGED
@@ -187,7 +187,7 @@ renderGutter();
187
  // endpoints they depended on (/api/library) were removed in audit fix C2.
188
  // Per-user saved libraries now live on the landing site's /dashboard/
189
  // page, backed by Supabase Storage with RLS-gated reads (task #98).
190
- const ROUTES = ['design', 'docs'];
191
 
192
  function currentRoute() {
193
  const hash = (window.location.hash || '').replace('#', '').toLowerCase();
@@ -3507,3 +3507,194 @@ if (_quitBtn) {
3507
  });
3508
  }
3509
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
187
  // endpoints they depended on (/api/library) were removed in audit fix C2.
188
  // Per-user saved libraries now live on the landing site's /dashboard/
189
  // page, backed by Supabase Storage with RLS-gated reads (task #98).
190
+ const ROUTES = ['design', 'crispr', 'docs'];
191
 
192
  function currentRoute() {
193
  const hash = (window.location.hash || '').replace('#', '').toLowerCase();
 
3507
  });
3508
  }
3509
 
3510
+
3511
+
3512
+ // ════════════════════════════════════════════════════════════════════════
3513
+ // CRISPR view controller β€” Cas9 knockout gRNA designer.
3514
+ // ────────────────────────────────────────────────────────────────────────
3515
+ // Mirrors the directed-evolution view's shape: paste β†’ submit β†’ table.
3516
+ // Sign-in gated server-side; UI shows a sign-in notice when anonymous.
3517
+ // ════════════════════════════════════════════════════════════════════════
3518
+
3519
+ (function crisprController() {
3520
+ const ta = document.getElementById('crisprInput');
3521
+ const stats = document.getElementById('crisprInputStats');
3522
+ const btn = document.getElementById('crisprDesignBtn');
3523
+ const notice = document.getElementById('crisprSigninNotice');
3524
+ const dlBtn = document.getElementById('crisprDownloadBtn');
3525
+ const resCard = document.getElementById('crisprResultsCard');
3526
+ const resSub = document.getElementById('crisprResultsSub');
3527
+ const tbody = document.getElementById('crisprTableBody');
3528
+ const errBox = document.getElementById('crisprError');
3529
+
3530
+ if (!ta || !btn) return; // CRISPR view not in DOM (older cached build)
3531
+
3532
+ let lastSubmittedSequence = null; // for CSV download
3533
+
3534
+ function _cleanedLength(s) {
3535
+ return (s || '').replace(/[^ACGTNacgtn]/g, '').length;
3536
+ }
3537
+
3538
+ function _updateStats() {
3539
+ const nt = _cleanedLength(ta.value);
3540
+ stats.textContent = nt ? `${nt.toLocaleString()} nt` : '0 nt';
3541
+
3542
+ const signedIn = (window.TuringAuth && window.TuringAuth.isSignedIn && window.TuringAuth.isSignedIn());
3543
+ // Show the sign-in notice if user is anonymous AND has typed
3544
+ // enough to design β€” surfacing the gate proactively rather than
3545
+ // after they hit submit + got 403.
3546
+ if (notice) notice.hidden = signedIn || nt < 23;
3547
+
3548
+ // Enable submit only with enough nt AND signed in.
3549
+ const canSubmit = nt >= 23 && !!signedIn;
3550
+ btn.disabled = !canSubmit;
3551
+ const sub = btn.querySelector('.primary-sub');
3552
+ if (sub) {
3553
+ sub.textContent = !signedIn
3554
+ ? 'Sign in to enable'
3555
+ : (nt < 23 ? 'Paste at least 23 nt' : `${nt.toLocaleString()} nt ready`);
3556
+ }
3557
+ }
3558
+
3559
+ function _clearError() {
3560
+ if (!errBox) return;
3561
+ errBox.hidden = true;
3562
+ errBox.textContent = '';
3563
+ }
3564
+
3565
+ function _showError(msg) {
3566
+ if (!errBox) return;
3567
+ errBox.hidden = false;
3568
+ errBox.textContent = msg;
3569
+ errBox.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
3570
+ }
3571
+
3572
+ ta.addEventListener('input', _updateStats);
3573
+
3574
+ btn.addEventListener('click', async () => {
3575
+ _clearError();
3576
+ const seq = ta.value;
3577
+ if (!seq.trim()) return;
3578
+ btn.disabled = true;
3579
+ const labelEl = btn.querySelector('.primary-label');
3580
+ const origLabel = labelEl ? labelEl.textContent : '';
3581
+ if (labelEl) labelEl.textContent = 'Designing…';
3582
+
3583
+ try {
3584
+ const res = await fetch('/api/crispr/design', {
3585
+ method: 'POST',
3586
+ headers: { 'Content-Type': 'application/json' },
3587
+ body: JSON.stringify({ sequence: seq, max_results: 50 }),
3588
+ });
3589
+ const data = await res.json();
3590
+ if (!res.ok) {
3591
+ // signin_required gets handled by auth.js (it redirects).
3592
+ // For other errors, show inline.
3593
+ if (data && data.kind === 'signin_required') {
3594
+ // auth.js will redirect to /signin via its 402/403
3595
+ // handler β€” but in case it doesn't fire (response
3596
+ // wasn't intercepted), show the notice here too.
3597
+ if (notice) notice.hidden = false;
3598
+ _showError('Sign in to design CRISPR guides.');
3599
+ return;
3600
+ }
3601
+ throw new Error(data.error || 'CRISPR design failed.');
3602
+ }
3603
+ lastSubmittedSequence = seq;
3604
+ _renderResults(data);
3605
+ } catch (err) {
3606
+ _showError(err.message || String(err));
3607
+ } finally {
3608
+ btn.disabled = false;
3609
+ if (labelEl) labelEl.textContent = origLabel;
3610
+ _updateStats();
3611
+ }
3612
+ });
3613
+
3614
+ function _renderResults(data) {
3615
+ resCard.hidden = false;
3616
+ resSub.textContent =
3617
+ `${data.n_guides} guides found in ${data.input_length.toLocaleString()} nt Β· ranked by on-target score`;
3618
+ const rows = (data.guides || []).map((g) => {
3619
+ const flags = [];
3620
+ if (g.flag_high_gc) flags.push('high&nbsp;GC');
3621
+ if (g.flag_low_gc) flags.push('low&nbsp;GC');
3622
+ if (g.flag_polyT) flags.push('poly-T');
3623
+ const flagsHtml = flags.length
3624
+ ? flags.map(f => `<span class="crispr-flag">${f}</span>`).join(' ')
3625
+ : '<span class="muted">β€”</span>';
3626
+ const scoreClass = g.on_target_score >= 0.65
3627
+ ? 'crispr-score-good'
3628
+ : (g.on_target_score >= 0.40 ? 'crispr-score-ok' : 'crispr-score-weak');
3629
+ return `
3630
+ <tr>
3631
+ <td class="rank">${g.rank}</td>
3632
+ <td>${g.strand === '+' ? 'sense' : 'antisense'}</td>
3633
+ <td class="num">${g.position.toLocaleString()}</td>
3634
+ <td class="crispr-spacer">
3635
+ <code>${escapeHtml(g.spacer)}</code>
3636
+ <button class="crispr-copy ghost" type="button" data-copy="${escapeHtml(g.spacer)}">Copy</button>
3637
+ </td>
3638
+ <td><code>${escapeHtml(g.pam)}</code></td>
3639
+ <td class="num ${scoreClass}">${g.on_target_score.toFixed(3)}</td>
3640
+ <td class="num">${g.gc_pct.toFixed(0)}%</td>
3641
+ <td>${flagsHtml}</td>
3642
+ </tr>
3643
+ `;
3644
+ });
3645
+ tbody.innerHTML = rows.join('') ||
3646
+ '<tr><td colspan="8" class="muted" style="text-align:center;padding:18px">No guides above the score threshold.</td></tr>';
3647
+
3648
+ // Wire copy buttons.
3649
+ tbody.querySelectorAll('[data-copy]').forEach((b) => {
3650
+ b.addEventListener('click', async () => {
3651
+ const text = b.getAttribute('data-copy') || '';
3652
+ try {
3653
+ await navigator.clipboard.writeText(text);
3654
+ const orig = b.textContent;
3655
+ b.textContent = 'Copied';
3656
+ setTimeout(() => { b.textContent = orig; }, 1200);
3657
+ } catch (_) {
3658
+ _showError('Copy blocked by browser. Select the spacer text manually.');
3659
+ }
3660
+ });
3661
+ });
3662
+
3663
+ // Scroll the results into view.
3664
+ resCard.scrollIntoView({ behavior: 'smooth', block: 'start' });
3665
+ }
3666
+
3667
+ if (dlBtn) {
3668
+ dlBtn.addEventListener('click', async () => {
3669
+ if (!lastSubmittedSequence) {
3670
+ _showError('Design guides first, then download the CSV.');
3671
+ return;
3672
+ }
3673
+ try {
3674
+ const res = await fetch('/api/crispr/download', {
3675
+ method: 'POST',
3676
+ headers: { 'Content-Type': 'application/json' },
3677
+ body: JSON.stringify({ sequence: lastSubmittedSequence, max_results: 50 }),
3678
+ });
3679
+ if (!res.ok) {
3680
+ throw new Error('CSV download failed.');
3681
+ }
3682
+ const blob = await res.blob();
3683
+ const url = URL.createObjectURL(blob);
3684
+ const a = document.createElement('a');
3685
+ a.href = url;
3686
+ a.download = 'turingdna_crispr_guides.csv';
3687
+ document.body.appendChild(a);
3688
+ a.click();
3689
+ document.body.removeChild(a);
3690
+ URL.revokeObjectURL(url);
3691
+ } catch (err) {
3692
+ _showError(err.message || String(err));
3693
+ }
3694
+ });
3695
+ }
3696
+
3697
+ // Initial state β€” depends on auth being ready. auth.js loads
3698
+ // before app.js so window.TuringAuth should exist by now.
3699
+ _updateStats();
3700
+ })();