Spaces:
Sleeping
Sleeping
WT row + manual DNA edit + new favicon (tight viewBox)
Browse files- dee/static/app.js +223 -19
dee/static/app.js
CHANGED
|
@@ -659,8 +659,10 @@ function renderResults(data) {
|
|
| 659 |
const desktopMsg = data.desktop_path
|
| 660 |
? `Saved to Desktop · <code>${escapeHtml(basename(data.desktop_path))}</code>`
|
| 661 |
: `Saved to <code>~/.dee/output/</code> · Desktop write blocked by macOS`;
|
|
|
|
|
|
|
| 662 |
$('#resultSummary').innerHTML =
|
| 663 |
-
`<strong>${
|
| 664 |
|
| 665 |
renderStatsStrip(data);
|
| 666 |
renderMutationMap(data);
|
|
@@ -677,7 +679,7 @@ function renderResults(data) {
|
|
| 677 |
renderVariantRows();
|
| 678 |
|
| 679 |
const navLib = document.getElementById('navLibCount');
|
| 680 |
-
if (navLib) navLib.textContent = data.variants.length;
|
| 681 |
|
| 682 |
wireDownloadMenu();
|
| 683 |
|
|
@@ -772,27 +774,38 @@ function renderVariantRows() {
|
|
| 772 |
}
|
| 773 |
});
|
| 774 |
|
|
|
|
| 775 |
const filtered = tableState.variants.filter(v => variantMatchesFilter(v, tableState.filter));
|
| 776 |
const sorter = COL_SORT[tableState.sortKey] || COL_SORT.rank;
|
| 777 |
const dir = tableState.sortDir === 'asc' ? 1 : -1;
|
| 778 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 779 |
const av = sorter(a), bv = sorter(b);
|
| 780 |
if (typeof av === 'string') return av.localeCompare(bv) * dir;
|
| 781 |
return (av - bv) * dir;
|
| 782 |
});
|
|
|
|
| 783 |
|
| 784 |
-
// Counter readout.
|
|
|
|
| 785 |
const counter = document.getElementById('tableCount');
|
| 786 |
if (counter) {
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
|
|
|
|
|
|
|
|
|
|
| 790 |
}
|
| 791 |
|
| 792 |
tbody.innerHTML = '';
|
| 793 |
sorted.forEach((v, idx) => {
|
| 794 |
const tr = document.createElement('tr');
|
| 795 |
-
|
|
|
|
| 796 |
const gc = Number(v.GC_Percent);
|
| 797 |
const gcOutOfRange = !Number.isNaN(gc) && (gc < 40 || gc > 60);
|
| 798 |
const minTm = Math.min(
|
|
@@ -800,11 +813,17 @@ function renderVariantRows() {
|
|
| 800 |
Number(v.Primer_Rev_Tm_C) || Infinity,
|
| 801 |
);
|
| 802 |
const tmDisplay = Number.isFinite(minTm) ? minTm.toFixed(1) : '—';
|
| 803 |
-
const mutHtml =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
tr.innerHTML = `
|
| 805 |
-
<td class="rank">${
|
| 806 |
<td class="mutations">${mutHtml}</td>
|
| 807 |
-
<td class="num">${
|
| 808 |
<td class="num">${Number.isNaN(gc) ? '—' : gc.toFixed(1)}${gcOutOfRange ? '<span class="gc-warn">!</span>' : ''}</td>
|
| 809 |
<td class="num">${tmDisplay}</td>
|
| 810 |
<td class="num">${escapeHtml(String(v.Length_bp ?? '—'))}</td>
|
|
@@ -909,6 +928,7 @@ function toggleDetailRow(idx, headRow, detailRow, v) {
|
|
| 909 |
detailRow.querySelector('td').innerHTML = renderDetailPanel(v);
|
| 910 |
wireCopyButtons(detailRow);
|
| 911 |
wireCloningControls(detailRow);
|
|
|
|
| 912 |
detailRow.dataset.populated = '1';
|
| 913 |
}
|
| 914 |
detailRow.hidden = false;
|
|
@@ -1829,7 +1849,9 @@ function wireDownloadMenu() {
|
|
| 1829 |
function renderStatsStrip(data) {
|
| 1830 |
const strip = document.getElementById('statsStrip');
|
| 1831 |
if (!strip) return;
|
| 1832 |
-
|
|
|
|
|
|
|
| 1833 |
if (!variants.length) { strip.innerHTML = ''; return; }
|
| 1834 |
|
| 1835 |
const fitness = variants.map(v => Number(v.Predicted_Fitness_Score)).filter(Number.isFinite);
|
|
@@ -2330,6 +2352,7 @@ function showToast(message, level = 'info') {
|
|
| 2330 |
}
|
| 2331 |
|
| 2332 |
function renderDetailPanel(v) {
|
|
|
|
| 2333 |
const gc = Number(v.GC_Percent);
|
| 2334 |
const gcWarn = !Number.isNaN(gc) && (gc < 40 || gc > 60)
|
| 2335 |
? `<span class="gc-warn">${gc < 40 ? 'low GC' : 'high GC'}</span>`
|
|
@@ -2340,7 +2363,7 @@ function renderDetailPanel(v) {
|
|
| 2340 |
const fwdGc = v.Primer_Fwd_GC_Percent != null ? `${v.Primer_Fwd_GC_Percent}%` : '—';
|
| 2341 |
const revGc = v.Primer_Rev_GC_Percent != null ? `${v.Primer_Rev_GC_Percent}%` : '—';
|
| 2342 |
// 1-indexed AA positions mutated in this variant — drives the highlight
|
| 2343 |
-
// overlays in the mutant-protein and DNA blocks below.
|
| 2344 |
const mutPositionList = [];
|
| 2345 |
const mutPositionSet = new Set();
|
| 2346 |
(v.Mutations_AA || '').split(',').forEach((m) => {
|
|
@@ -2351,16 +2374,25 @@ function renderDetailPanel(v) {
|
|
| 2351 |
mutPositionSet.add(pos);
|
| 2352 |
}
|
| 2353 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2354 |
|
| 2355 |
return `
|
| 2356 |
-
<div class="detail-panel">
|
| 2357 |
<div class="detail-section">
|
| 2358 |
<h4>PCR setup</h4>
|
| 2359 |
<div class="pcr-summary">
|
| 2360 |
<div><span class="pcr-label">Amplicon length</span><span class="pcr-val">${v.Length_bp} bp</span></div>
|
| 2361 |
<div><span class="pcr-label">GC content</span><span class="pcr-val">${Number(v.GC_Percent).toFixed(1)}% ${gcWarn}</span></div>
|
| 2362 |
<div><span class="pcr-label">Annealing temp</span><span class="pcr-val">${annealing}</span></div>
|
| 2363 |
-
<div><span class="pcr-label">Mutations</span><span class="pcr-val">${
|
| 2364 |
</div>
|
| 2365 |
</div>
|
| 2366 |
|
|
@@ -2405,13 +2437,32 @@ function renderDetailPanel(v) {
|
|
| 2405 |
</div>
|
| 2406 |
|
| 2407 |
<div class="detail-section">
|
| 2408 |
-
<h4>
|
| 2409 |
<div class="sequence-block">${formatProteinDiff(v.Mutant_AA_Seq || '', tableState.wtProtein || '', mutPositionSet)}</div>
|
| 2410 |
</div>
|
| 2411 |
|
| 2412 |
-
<
|
| 2413 |
-
|
| 2414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2415 |
</div>
|
| 2416 |
|
| 2417 |
<div class="detail-section cloning-section" data-variant-id="${escapeHtml(v.Variant_ID)}">
|
|
@@ -2586,6 +2637,159 @@ function wireCopyButtons(scope) {
|
|
| 2586 |
});
|
| 2587 |
}
|
| 2588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2589 |
// ============================================================== UTIL
|
| 2590 |
|
| 2591 |
function showError(msg, detail) {
|
|
|
|
| 659 |
const desktopMsg = data.desktop_path
|
| 660 |
? `Saved to Desktop · <code>${escapeHtml(basename(data.desktop_path))}</code>`
|
| 661 |
: `Saved to <code>~/.dee/output/</code> · Desktop write blocked by macOS`;
|
| 662 |
+
const evolvedCount = (data.variants || []).filter(v => v.Variant_ID !== 'WT').length;
|
| 663 |
+
const hasWt = (data.variants || []).some(v => v.Variant_ID === 'WT');
|
| 664 |
$('#resultSummary').innerHTML =
|
| 665 |
+
`<strong>${evolvedCount}</strong> variants${hasWt ? ' + wild type' : ''} of <strong>${escapeHtml(data.wt_identifier)}</strong> · ${data.wt_protein.length} aa · ${desktopMsg}`;
|
| 666 |
|
| 667 |
renderStatsStrip(data);
|
| 668 |
renderMutationMap(data);
|
|
|
|
| 679 |
renderVariantRows();
|
| 680 |
|
| 681 |
const navLib = document.getElementById('navLibCount');
|
| 682 |
+
if (navLib) navLib.textContent = (data.variants || []).filter(v => v.Variant_ID !== 'WT').length;
|
| 683 |
|
| 684 |
wireDownloadMenu();
|
| 685 |
|
|
|
|
| 774 |
}
|
| 775 |
});
|
| 776 |
|
| 777 |
+
const isWt = (v) => v.Variant_ID === 'WT';
|
| 778 |
const filtered = tableState.variants.filter(v => variantMatchesFilter(v, tableState.filter));
|
| 779 |
const sorter = COL_SORT[tableState.sortKey] || COL_SORT.rank;
|
| 780 |
const dir = tableState.sortDir === 'asc' ? 1 : -1;
|
| 781 |
+
// WT is always pinned to row 0, regardless of the chosen sort column. The
|
| 782 |
+
// user-driven sort applies only to the evolved variants below it.
|
| 783 |
+
const wtRow = filtered.find(isWt);
|
| 784 |
+
const evolved = filtered.filter(v => !isWt(v));
|
| 785 |
+
evolved.sort((a, b) => {
|
| 786 |
const av = sorter(a), bv = sorter(b);
|
| 787 |
if (typeof av === 'string') return av.localeCompare(bv) * dir;
|
| 788 |
return (av - bv) * dir;
|
| 789 |
});
|
| 790 |
+
const sorted = wtRow ? [wtRow, ...evolved] : evolved;
|
| 791 |
|
| 792 |
+
// Counter readout. WT is shown separately so the user sees how many
|
| 793 |
+
// evolved variants the library contains.
|
| 794 |
const counter = document.getElementById('tableCount');
|
| 795 |
if (counter) {
|
| 796 |
+
const evolvedTotal = tableState.variants.filter(v => !isWt(v)).length;
|
| 797 |
+
const evolvedShown = sorted.filter(v => !isWt(v)).length;
|
| 798 |
+
const wtSuffix = wtRow ? ' + WT' : '';
|
| 799 |
+
counter.textContent = evolvedShown === evolvedTotal
|
| 800 |
+
? `${evolvedTotal} variants${wtSuffix}`
|
| 801 |
+
: `${evolvedShown} of ${evolvedTotal} variants${wtSuffix}`;
|
| 802 |
}
|
| 803 |
|
| 804 |
tbody.innerHTML = '';
|
| 805 |
sorted.forEach((v, idx) => {
|
| 806 |
const tr = document.createElement('tr');
|
| 807 |
+
const wt = isWt(v);
|
| 808 |
+
tr.className = wt ? 'expandable variant-wt-row' : 'expandable';
|
| 809 |
const gc = Number(v.GC_Percent);
|
| 810 |
const gcOutOfRange = !Number.isNaN(gc) && (gc < 40 || gc > 60);
|
| 811 |
const minTm = Math.min(
|
|
|
|
| 813 |
Number(v.Primer_Rev_Tm_C) || Infinity,
|
| 814 |
);
|
| 815 |
const tmDisplay = Number.isFinite(minTm) ? minTm.toFixed(1) : '—';
|
| 816 |
+
const mutHtml = wt
|
| 817 |
+
? '<span class="wt-badge" title="Unmutated wild-type sequence — order this to baseline alongside the evolved library.">wild type</span>'
|
| 818 |
+
: renderMutationTokens(v.Mutations_AA || '');
|
| 819 |
+
const fitnessCell = wt ? '<span class="muted">—</span>' : Number(v.Predicted_Fitness_Score).toFixed(3);
|
| 820 |
+
const idCell = wt
|
| 821 |
+
? `<span class="rank-wt">WT</span>`
|
| 822 |
+
: escapeHtml(v.Variant_ID);
|
| 823 |
tr.innerHTML = `
|
| 824 |
+
<td class="rank">${idCell}</td>
|
| 825 |
<td class="mutations">${mutHtml}</td>
|
| 826 |
+
<td class="num">${fitnessCell}</td>
|
| 827 |
<td class="num">${Number.isNaN(gc) ? '—' : gc.toFixed(1)}${gcOutOfRange ? '<span class="gc-warn">!</span>' : ''}</td>
|
| 828 |
<td class="num">${tmDisplay}</td>
|
| 829 |
<td class="num">${escapeHtml(String(v.Length_bp ?? '—'))}</td>
|
|
|
|
| 928 |
detailRow.querySelector('td').innerHTML = renderDetailPanel(v);
|
| 929 |
wireCopyButtons(detailRow);
|
| 930 |
wireCloningControls(detailRow);
|
| 931 |
+
wireDnaEdit(detailRow, v);
|
| 932 |
detailRow.dataset.populated = '1';
|
| 933 |
}
|
| 934 |
detailRow.hidden = false;
|
|
|
|
| 1849 |
function renderStatsStrip(data) {
|
| 1850 |
const strip = document.getElementById('statsStrip');
|
| 1851 |
if (!strip) return;
|
| 1852 |
+
// Stats describe the evolved library only — the WT pseudo-row would skew
|
| 1853 |
+
// every aggregate (zero mutations, zero fitness, etc.).
|
| 1854 |
+
const variants = (data.variants || []).filter(v => v.Variant_ID !== 'WT');
|
| 1855 |
if (!variants.length) { strip.innerHTML = ''; return; }
|
| 1856 |
|
| 1857 |
const fitness = variants.map(v => Number(v.Predicted_Fitness_Score)).filter(Number.isFinite);
|
|
|
|
| 2352 |
}
|
| 2353 |
|
| 2354 |
function renderDetailPanel(v) {
|
| 2355 |
+
const isWt = v.Variant_ID === 'WT';
|
| 2356 |
const gc = Number(v.GC_Percent);
|
| 2357 |
const gcWarn = !Number.isNaN(gc) && (gc < 40 || gc > 60)
|
| 2358 |
? `<span class="gc-warn">${gc < 40 ? 'low GC' : 'high GC'}</span>`
|
|
|
|
| 2363 |
const fwdGc = v.Primer_Fwd_GC_Percent != null ? `${v.Primer_Fwd_GC_Percent}%` : '—';
|
| 2364 |
const revGc = v.Primer_Rev_GC_Percent != null ? `${v.Primer_Rev_GC_Percent}%` : '—';
|
| 2365 |
// 1-indexed AA positions mutated in this variant — drives the highlight
|
| 2366 |
+
// overlays in the mutant-protein and DNA blocks below. Empty for WT.
|
| 2367 |
const mutPositionList = [];
|
| 2368 |
const mutPositionSet = new Set();
|
| 2369 |
(v.Mutations_AA || '').split(',').forEach((m) => {
|
|
|
|
| 2374 |
mutPositionSet.add(pos);
|
| 2375 |
}
|
| 2376 |
});
|
| 2377 |
+
const mutationsDisplay = isWt
|
| 2378 |
+
? '<span class="muted">— (unmutated)</span>'
|
| 2379 |
+
: escapeHtml(v.Mutations_AA || '');
|
| 2380 |
+
const proteinHeader = isWt
|
| 2381 |
+
? `Wild-type protein (${(v.Mutant_AA_Seq || '').length} aa)`
|
| 2382 |
+
: `Mutant protein (${(v.Mutant_AA_Seq || '').length} aa) · mutations highlighted in brand color`;
|
| 2383 |
+
const dnaHeader = isWt
|
| 2384 |
+
? `Codon-optimized wild-type DNA (${v.Length_bp} bp)`
|
| 2385 |
+
: `Optimized DNA sequence (${v.Length_bp} bp) · mutated codons highlighted`;
|
| 2386 |
|
| 2387 |
return `
|
| 2388 |
+
<div class="detail-panel${isWt ? ' detail-panel-wt' : ''}">
|
| 2389 |
<div class="detail-section">
|
| 2390 |
<h4>PCR setup</h4>
|
| 2391 |
<div class="pcr-summary">
|
| 2392 |
<div><span class="pcr-label">Amplicon length</span><span class="pcr-val">${v.Length_bp} bp</span></div>
|
| 2393 |
<div><span class="pcr-label">GC content</span><span class="pcr-val">${Number(v.GC_Percent).toFixed(1)}% ${gcWarn}</span></div>
|
| 2394 |
<div><span class="pcr-label">Annealing temp</span><span class="pcr-val">${annealing}</span></div>
|
| 2395 |
+
<div><span class="pcr-label">Mutations</span><span class="pcr-val">${mutationsDisplay}</span></div>
|
| 2396 |
</div>
|
| 2397 |
</div>
|
| 2398 |
|
|
|
|
| 2437 |
</div>
|
| 2438 |
|
| 2439 |
<div class="detail-section">
|
| 2440 |
+
<h4>${proteinHeader}</h4>
|
| 2441 |
<div class="sequence-block">${formatProteinDiff(v.Mutant_AA_Seq || '', tableState.wtProtein || '', mutPositionSet)}</div>
|
| 2442 |
</div>
|
| 2443 |
|
| 2444 |
+
<!--
|
| 2445 |
+
Editable codon-optimized DNA. The textarea is hidden by default
|
| 2446 |
+
(read-only view shows the highlighted formatted block); clicking
|
| 2447 |
+
"Edit DNA" reveals it, and onInput it validates + persists back
|
| 2448 |
+
to tableState.variants[i].Optimized_DNA_Seq so the Cloning
|
| 2449 |
+
Designer + Synthesize buttons all see the edited sequence.
|
| 2450 |
+
-->
|
| 2451 |
+
<div class="detail-section dna-edit-section" data-variant-id="${escapeHtml(v.Variant_ID)}">
|
| 2452 |
+
<div class="dna-edit-head">
|
| 2453 |
+
<h4>${dnaHeader}</h4>
|
| 2454 |
+
<div class="dna-edit-actions">
|
| 2455 |
+
<button class="ghost-btn dna-edit-toggle" data-variant="${escapeHtml(v.Variant_ID)}" type="button">Edit DNA</button>
|
| 2456 |
+
<button class="ghost-btn dna-edit-reset" data-variant="${escapeHtml(v.Variant_ID)}" type="button" hidden>Revert</button>
|
| 2457 |
+
</div>
|
| 2458 |
+
</div>
|
| 2459 |
+
<div class="dna-view" data-variant="${escapeHtml(v.Variant_ID)}">
|
| 2460 |
+
<div class="sequence-block">${formatDna(v.Optimized_DNA_Seq, mutPositionList)}</div>
|
| 2461 |
+
</div>
|
| 2462 |
+
<div class="dna-edit-wrap" data-variant="${escapeHtml(v.Variant_ID)}" hidden>
|
| 2463 |
+
<textarea class="dna-edit-textarea" data-variant="${escapeHtml(v.Variant_ID)}" spellcheck="false" rows="8">${escapeHtml(v.Optimized_DNA_Seq || '')}</textarea>
|
| 2464 |
+
<div class="dna-edit-stats" data-variant="${escapeHtml(v.Variant_ID)}"></div>
|
| 2465 |
+
</div>
|
| 2466 |
</div>
|
| 2467 |
|
| 2468 |
<div class="detail-section cloning-section" data-variant-id="${escapeHtml(v.Variant_ID)}">
|
|
|
|
| 2637 |
});
|
| 2638 |
}
|
| 2639 |
|
| 2640 |
+
// ============================================================== DNA EDIT
|
| 2641 |
+
// In-place editing of any variant's codon-optimized DNA. Edits persist into
|
| 2642 |
+
// tableState.variants[i].Optimized_DNA_Seq so the Cloning Designer (which
|
| 2643 |
+
// reads the CDS via defaultDesignerState(v.Optimized_DNA_Seq) when first
|
| 2644 |
+
// expanded) and the Synthesize-via-X buttons (which fall back to the same
|
| 2645 |
+
// field) both see whatever the user typed.
|
| 2646 |
+
|
| 2647 |
+
// Snapshot of the server-supplied DNA per variant — used by the Revert button
|
| 2648 |
+
// to roll back without re-fetching from the API.
|
| 2649 |
+
const originalDnaSnapshots = new Map();
|
| 2650 |
+
|
| 2651 |
+
function wireDnaEdit(scope, variant) {
|
| 2652 |
+
const variantId = variant.Variant_ID;
|
| 2653 |
+
const toggle = scope.querySelector(`.dna-edit-toggle[data-variant="${cssEscape(variantId)}"]`);
|
| 2654 |
+
const reset = scope.querySelector(`.dna-edit-reset[data-variant="${cssEscape(variantId)}"]`);
|
| 2655 |
+
const view = scope.querySelector(`.dna-view[data-variant="${cssEscape(variantId)}"]`);
|
| 2656 |
+
const wrap = scope.querySelector(`.dna-edit-wrap[data-variant="${cssEscape(variantId)}"]`);
|
| 2657 |
+
const ta = scope.querySelector(`.dna-edit-textarea[data-variant="${cssEscape(variantId)}"]`);
|
| 2658 |
+
const stats = scope.querySelector(`.dna-edit-stats[data-variant="${cssEscape(variantId)}"]`);
|
| 2659 |
+
if (!toggle || !ta || !wrap || !view) return;
|
| 2660 |
+
|
| 2661 |
+
// Snapshot the original so Revert always works, even after many edits.
|
| 2662 |
+
if (!originalDnaSnapshots.has(variantId)) {
|
| 2663 |
+
originalDnaSnapshots.set(variantId, variant.Optimized_DNA_Seq || '');
|
| 2664 |
+
}
|
| 2665 |
+
|
| 2666 |
+
// Strict ACGT sanitizer + validator. Returns { clean, stats, warnings }.
|
| 2667 |
+
const STOP_CODONS = new Set(['TAA', 'TAG', 'TGA']);
|
| 2668 |
+
function validateDna(raw) {
|
| 2669 |
+
// Strip whitespace, uppercase, keep only A/C/G/T (drop anything else
|
| 2670 |
+
// silently and report it as a warning).
|
| 2671 |
+
const upper = (raw || '').toUpperCase();
|
| 2672 |
+
const dropped = upper.replace(/\s/g, '').match(/[^ACGT]/g) || [];
|
| 2673 |
+
const clean = upper.replace(/[^ACGT]/g, '');
|
| 2674 |
+
const warnings = [];
|
| 2675 |
+
|
| 2676 |
+
if (dropped.length) {
|
| 2677 |
+
const uniq = [...new Set(dropped)].join('');
|
| 2678 |
+
warnings.push({ severity: 'low', text: `Removed ${dropped.length} non-ACGT character${dropped.length > 1 ? 's' : ''} (${uniq}).` });
|
| 2679 |
+
}
|
| 2680 |
+
if (clean.length === 0) {
|
| 2681 |
+
warnings.push({ severity: 'high', text: 'Sequence is empty.' });
|
| 2682 |
+
}
|
| 2683 |
+
if (clean.length && clean.length % 3 !== 0) {
|
| 2684 |
+
warnings.push({ severity: 'high', text: `Length ${clean.length} bp is not a multiple of 3 — frame will be broken.` });
|
| 2685 |
+
}
|
| 2686 |
+
// Premature stop check: scan codons up to (but not including) the
|
| 2687 |
+
// final codon (which is allowed to be a stop).
|
| 2688 |
+
let firstPrematureStop = -1;
|
| 2689 |
+
for (let i = 0; i + 3 <= clean.length - 3; i += 3) {
|
| 2690 |
+
const codon = clean.slice(i, i + 3);
|
| 2691 |
+
if (STOP_CODONS.has(codon)) { firstPrematureStop = i; break; }
|
| 2692 |
+
}
|
| 2693 |
+
if (firstPrematureStop >= 0) {
|
| 2694 |
+
warnings.push({ severity: 'high', text: `Premature stop codon at nt ${firstPrematureStop + 1} (${clean.slice(firstPrematureStop, firstPrematureStop + 3)}). Synthesis vendors will reject this.` });
|
| 2695 |
+
}
|
| 2696 |
+
const gc = clean.length
|
| 2697 |
+
? ((clean.match(/[GC]/g) || []).length / clean.length) * 100
|
| 2698 |
+
: 0;
|
| 2699 |
+
if (clean.length && (gc < 35 || gc > 65)) {
|
| 2700 |
+
warnings.push({ severity: 'medium', text: `GC ${gc.toFixed(1)}% is outside the 35–65% sweet spot.` });
|
| 2701 |
+
}
|
| 2702 |
+
return { clean, gc, len: clean.length, warnings };
|
| 2703 |
+
}
|
| 2704 |
+
|
| 2705 |
+
function renderStats(result) {
|
| 2706 |
+
const sevClass = (s) => `warn-${s}`;
|
| 2707 |
+
const summary = `
|
| 2708 |
+
<span class="dna-stat-pill"><strong>${result.len}</strong> bp</span>
|
| 2709 |
+
<span class="dna-stat-pill">GC <strong>${result.gc.toFixed(1)}%</strong></span>
|
| 2710 |
+
<span class="dna-stat-pill">${result.len % 3 === 0 ? 'in-frame ✓' : '<span class="warn-high">frame broken</span>'}</span>
|
| 2711 |
+
`;
|
| 2712 |
+
const warningsHtml = result.warnings.length
|
| 2713 |
+
? `<ul class="dna-edit-warnings">${result.warnings.map(w => `<li class="${sevClass(w.severity)}">${escapeHtml(w.text)}</li>`).join('')}</ul>`
|
| 2714 |
+
: '<div class="dna-edit-ok">Looks good for synthesis. ✓</div>';
|
| 2715 |
+
stats.innerHTML = summary + warningsHtml;
|
| 2716 |
+
}
|
| 2717 |
+
|
| 2718 |
+
// Initial stats render so user sees current GC/length even before editing.
|
| 2719 |
+
renderStats(validateDna(ta.value));
|
| 2720 |
+
|
| 2721 |
+
// Toggle: hide read-only view, show textarea.
|
| 2722 |
+
toggle.addEventListener('click', (e) => {
|
| 2723 |
+
e.stopPropagation();
|
| 2724 |
+
const editing = !wrap.hidden;
|
| 2725 |
+
if (editing) {
|
| 2726 |
+
// Switching back to view mode — commit the latest textarea value.
|
| 2727 |
+
const result = validateDna(ta.value);
|
| 2728 |
+
ta.value = result.clean; // canonicalize
|
| 2729 |
+
commitEditedDna(variantId, result.clean);
|
| 2730 |
+
wrap.hidden = true;
|
| 2731 |
+
view.hidden = false;
|
| 2732 |
+
toggle.textContent = 'Edit DNA';
|
| 2733 |
+
reset.hidden = true;
|
| 2734 |
+
} else {
|
| 2735 |
+
view.hidden = true;
|
| 2736 |
+
wrap.hidden = false;
|
| 2737 |
+
toggle.textContent = 'Done editing';
|
| 2738 |
+
reset.hidden = false;
|
| 2739 |
+
ta.focus();
|
| 2740 |
+
// Move caret to end so user can append without arrow-keying.
|
| 2741 |
+
const v = ta.value;
|
| 2742 |
+
ta.setSelectionRange(v.length, v.length);
|
| 2743 |
+
}
|
| 2744 |
+
});
|
| 2745 |
+
|
| 2746 |
+
// Revert to server-supplied sequence.
|
| 2747 |
+
reset.addEventListener('click', (e) => {
|
| 2748 |
+
e.stopPropagation();
|
| 2749 |
+
const original = originalDnaSnapshots.get(variantId) || '';
|
| 2750 |
+
ta.value = original;
|
| 2751 |
+
const result = validateDna(original);
|
| 2752 |
+
renderStats(result);
|
| 2753 |
+
commitEditedDna(variantId, original);
|
| 2754 |
+
});
|
| 2755 |
+
|
| 2756 |
+
// Live validation + state update.
|
| 2757 |
+
ta.addEventListener('input', () => {
|
| 2758 |
+
const result = validateDna(ta.value);
|
| 2759 |
+
renderStats(result);
|
| 2760 |
+
// Live-update the underlying variant so the Designer + synth buttons
|
| 2761 |
+
// see the in-progress edit (no need to click "Done editing" first).
|
| 2762 |
+
commitEditedDna(variantId, result.clean);
|
| 2763 |
+
});
|
| 2764 |
+
}
|
| 2765 |
+
|
| 2766 |
+
/** Commit edited DNA back into tableState + refresh anything that consumed it. */
|
| 2767 |
+
function commitEditedDna(variantId, newDna) {
|
| 2768 |
+
const variant = (tableState.variants || []).find(v => v.Variant_ID === variantId);
|
| 2769 |
+
if (!variant) return;
|
| 2770 |
+
variant.Optimized_DNA_Seq = newDna;
|
| 2771 |
+
variant.Length_bp = newDna.length;
|
| 2772 |
+
// Recompute GC to keep the row's GC cell honest if the user closes &
|
| 2773 |
+
// reopens the detail panel.
|
| 2774 |
+
if (newDna.length) {
|
| 2775 |
+
const gc = ((newDna.match(/[GC]/g) || []).length / newDna.length) * 100;
|
| 2776 |
+
variant.GC_Percent = +gc.toFixed(2);
|
| 2777 |
+
}
|
| 2778 |
+
// Reset the cloning state for this variant so the Designer rebuilds from
|
| 2779 |
+
// the new CDS on next render. Then re-render if it's currently visible.
|
| 2780 |
+
if (typeof cloningState !== 'undefined') {
|
| 2781 |
+
cloningState.delete(variantId);
|
| 2782 |
+
}
|
| 2783 |
+
const designerEl = document.querySelector(`.cloning-designer[data-variant="${cssEscape(variantId)}"]`);
|
| 2784 |
+
if (designerEl && typeof renderDesigner === 'function') {
|
| 2785 |
+
// Seed and re-render the designer.
|
| 2786 |
+
if (typeof defaultDesignerState === 'function' && typeof cloningState !== 'undefined') {
|
| 2787 |
+
cloningState.set(variantId, defaultDesignerState(newDna));
|
| 2788 |
+
}
|
| 2789 |
+
renderDesigner(variantId);
|
| 2790 |
+
}
|
| 2791 |
+
}
|
| 2792 |
+
|
| 2793 |
// ============================================================== UTIL
|
| 2794 |
|
| 2795 |
function showError(msg, detail) {
|