pswap commited on
Commit
75503a8
·
1 Parent(s): 710f03c
Files changed (3) hide show
  1. static/style.css +1 -1
  2. templates/index.html +16 -73
  3. utils/synteny.py +17 -10
static/style.css CHANGED
@@ -8,7 +8,7 @@
8
  [hidden] { display: none !important; }
9
  body { margin: 0; background: var(--bg); font-family: var(--sans); color: var(--ink); -webkit-font-smoothing: antialiased; }
10
 
11
- .layout { max-width: 1100px; margin: 24px auto; padding: 0 16px; }
12
  .card { background: var(--card); border: 1px solid #E4E1DB; border-radius: 10px; overflow: hidden; }
13
 
14
  .top { display: flex; align-items: baseline; gap: 14px; padding: 18px 28px; border-bottom: 1px solid var(--line); }
 
8
  [hidden] { display: none !important; }
9
  body { margin: 0; background: var(--bg); font-family: var(--sans); color: var(--ink); -webkit-font-smoothing: antialiased; }
10
 
11
+ .layout { max-width: 1320px; margin: 24px auto; padding: 0 16px; }
12
  .card { background: var(--card); border: 1px solid #E4E1DB; border-radius: 10px; overflow: hidden; }
13
 
14
  .top { display: flex; align-items: baseline; gap: 14px; padding: 18px 28px; border-bottom: 1px solid var(--line); }
templates/index.html CHANGED
@@ -21,7 +21,7 @@
21
 
22
  <section class="searchbar">
23
  <div class="row">
24
- <textarea id="input" placeholder="&gt;my_phage&#10;ATGC... (DNA FASTA) or a short NCBI accession"></textarea>
25
  <button id="run" class="btn dark">Analyze</button>
26
  </div>
27
  <div class="row hint">
@@ -63,32 +63,14 @@
63
 
64
  <section class="section" id="neighbors-section" hidden>
65
  <h2 class="h2">Nearest genomes</h2>
66
- <div class="tabs" id="neighbor-tabs">
67
- <button class="tab on" data-src="all">All</button>
68
- <button class="tab" data-src="IMGVR">IMG/VR</button>
69
- <button class="tab" data-src="INPHARED">INPHARED</button>
70
- </div>
71
  <p id="neighbors-status" class="status"></p>
72
- <p id="neighbors-note" class="status" hidden></p>
73
- <div class="scroll-pane">
74
- <table id="neighbors-table" class="grid" hidden>
75
- <thead><tr><th>#</th><th>genome</th><th>genes</th><th>length (bp)</th><th>cosine</th></tr></thead>
76
- <tbody></tbody>
77
- </table>
78
- </div>
79
  </section>
80
 
81
  <section class="section">
82
  <div id="jobs"></div>
83
  </section>
84
 
85
- <section class="section" id="synteny-section" hidden>
86
- <h2 class="h2">Synteny vs. genome neighbors</h2>
87
- <button id="load-synteny" class="btn small">Load synteny (top 5 neighbors)</button>
88
- <p id="synteny-status" class="status"></p>
89
- <div id="synteny-plot"></div>
90
- </section>
91
-
92
  </main>
93
  </div>
94
 
@@ -178,36 +160,6 @@
178
  document.getElementById("genes-section").hidden = tab !== "genes";
179
  }));
180
 
181
- // IMGVR/INPHARED are filtered by hit.source, which the bucket doesn't
182
- // carry yet (phlam_genomes.parquet has no source column) -- those two
183
- // tabs show an honest "not available yet" state until it's exported;
184
- // "All" always works since it needs no source field.
185
- let neighborList = [];
186
- function renderNeighbors() {
187
- const src = document.querySelector("#neighbor-tabs .tab.on").dataset.src;
188
- const rows = src === "all" ? neighborList : neighborList.filter(h => h.source === src);
189
- const tbody = document.querySelector("#neighbors-table tbody");
190
- const table = document.getElementById("neighbors-table");
191
- const note = document.getElementById("neighbors-note");
192
- if (src !== "all" && !neighborList.some(h => "source" in h)) {
193
- table.hidden = true;
194
- note.hidden = false;
195
- note.textContent = "Per-source genome metadata isn't available yet -- use \"All\" for now.";
196
- return;
197
- }
198
- note.hidden = true;
199
- tbody.innerHTML = rows.map((h, i) => `<tr>
200
- <td class="ink">${i + 1}</td><td class="ink">genome_${h.genome_id}</td>
201
- <td>${h.n_genes}</td><td>${h.length_bp ?? "-"}</td><td>${h.score.toFixed(4)}</td>
202
- </tr>`).join("");
203
- table.hidden = false;
204
- }
205
- document.querySelectorAll("#neighbor-tabs .tab").forEach(btn => btn.addEventListener("click", () => {
206
- document.querySelectorAll("#neighbor-tabs .tab").forEach(b => b.classList.remove("on"));
207
- btn.classList.add("on");
208
- renderNeighbors();
209
- }));
210
-
211
  function jobPanel(key) {
212
  const div = document.createElement("div");
213
  div.className = "box";
@@ -247,13 +199,13 @@
247
  }
248
 
249
  // tracks/links come back as phagevault-style positional tuples; synteny-view.js
250
- // wants {id, start, end, strand} genes and {a, b, scores} links (ids unique over all tracks).
251
  let syntenyView = null;
252
  function renderSynteny(syn) {
253
  const tracks = syn.tracks.map((t, ti) => ({
254
  id: String(ti), name: t.name, length: t.length_bp,
255
- genes: t.genes.map(([start, end, strand], gi) => ({
256
- id: `${ti}:${gi}`, start, end, strand: strand === "-" ? -1 : 1,
257
  })),
258
  }));
259
  const links = syn.links.map(([a, i, b, j, esm, phlam]) => ({
@@ -270,30 +222,27 @@
270
  syntenyView.setData({ tracks, links }, { reset: true });
271
  }
272
 
273
- document.getElementById("load-synteny").addEventListener("click", async () => {
274
- const statusEl = document.getElementById("synteny-status");
275
- statusEl.textContent = "Running...";
276
  const res = await fetch("/synteny", {
277
  method: "POST",
278
  headers: { "Content-Type": "application/json" },
279
- body: JSON.stringify({
280
- analysis_id: window.PHLAM_ANALYSIS_ID,
281
- genome_ids: window.PHLAM_NEIGHBOR_IDS.slice(0, 5),
282
- }),
283
  });
284
  const data = await res.json();
285
  if (!res.ok) { statusEl.textContent = "Error: " + data.error; return; }
286
  const final = await pollJob(data.job);
287
  if (final.status !== "done") { statusEl.textContent = "Error: " + (final.error || final.status); return; }
288
- statusEl.textContent = `${final.result.links.length} links across ${final.result.tracks.length} tracks.`;
289
  renderSynteny(final.result);
290
- });
291
 
292
  async function analyze() {
293
  const input = document.getElementById("input").value;
294
  const statusEl = document.getElementById("status");
295
  const jobsEl = document.getElementById("jobs");
296
- document.getElementById("synteny-section").hidden = true;
297
  jobsEl.innerHTML = "";
298
  statusEl.className = "status";
299
  statusEl.textContent = "Running...";
@@ -322,28 +271,22 @@
322
  document.querySelectorAll("#view-tabs .tab").forEach(b => b.classList.toggle("on", b.dataset.tab === "circular"));
323
  document.getElementById("circular-section").hidden = false;
324
  document.getElementById("genes-section").hidden = true;
325
- window.PHLAM_ANALYSIS_ID = data.analysis_id;
326
 
327
  document.getElementById("neighbors-section").hidden = false;
328
  document.getElementById("neighbors-status").textContent = "pending...";
329
- document.getElementById("neighbors-table").hidden = true;
330
 
331
  // Poll every job concurrently -- jobs.jobs isn't in a meaningful
332
  // order (jsonify sorts keys), and a slow job must never block a
333
- // fast one's panel (or the synteny section) from updating.
334
  for (const [key, jobId] of Object.entries(data.jobs || {})) {
335
  if (key === "genome_neighbors") {
336
  pollJob(jobId, { onDone: finalData => {
337
- const statusEl = document.getElementById("neighbors-status");
338
  if (finalData.status !== "done") {
339
- statusEl.textContent = finalData.error || finalData.status;
340
  return;
341
  }
342
- statusEl.textContent = "";
343
- neighborList = finalData.result;
344
- renderNeighbors();
345
- window.PHLAM_NEIGHBOR_IDS = finalData.result.map(h => h.genome_id);
346
- document.getElementById("synteny-section").hidden = false;
347
  }});
348
  continue;
349
  }
 
21
 
22
  <section class="searchbar">
23
  <div class="row">
24
+ <textarea id="input" placeholder="NCBI accession (e.g. KT232076) or paste a DNA FASTA (&gt;my_phage &hellip;)"></textarea>
25
  <button id="run" class="btn dark">Analyze</button>
26
  </div>
27
  <div class="row hint">
 
63
 
64
  <section class="section" id="neighbors-section" hidden>
65
  <h2 class="h2">Nearest genomes</h2>
 
 
 
 
 
66
  <p id="neighbors-status" class="status"></p>
67
+ <div id="synteny-plot"></div>
 
 
 
 
 
 
68
  </section>
69
 
70
  <section class="section">
71
  <div id="jobs"></div>
72
  </section>
73
 
 
 
 
 
 
 
 
74
  </main>
75
  </div>
76
 
 
160
  document.getElementById("genes-section").hidden = tab !== "genes";
161
  }));
162
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  function jobPanel(key) {
164
  const div = document.createElement("div");
165
  div.className = "box";
 
199
  }
200
 
201
  // tracks/links come back as phagevault-style positional tuples; synteny-view.js
202
+ // wants {id, start, end, strand, label} genes and {a, b, scores} links (ids unique over all tracks).
203
  let syntenyView = null;
204
  function renderSynteny(syn) {
205
  const tracks = syn.tracks.map((t, ti) => ({
206
  id: String(ti), name: t.name, length: t.length_bp,
207
+ genes: t.genes.map(([start, end, strand, geneId, label], gi) => ({
208
+ id: `${ti}:${gi}`, start, end, strand: strand === "-" ? -1 : 1, label,
209
  })),
210
  }));
211
  const links = syn.links.map(([a, i, b, j, esm, phlam]) => ({
 
222
  syntenyView.setData({ tracks, links }, { reset: true });
223
  }
224
 
225
+ async function loadSynteny(analysisId, neighborIds) {
226
+ const statusEl = document.getElementById("neighbors-status");
227
+ statusEl.textContent = "Loading synteny...";
228
  const res = await fetch("/synteny", {
229
  method: "POST",
230
  headers: { "Content-Type": "application/json" },
231
+ body: JSON.stringify({ analysis_id: analysisId, genome_ids: neighborIds.slice(0, 5) }),
 
 
 
232
  });
233
  const data = await res.json();
234
  if (!res.ok) { statusEl.textContent = "Error: " + data.error; return; }
235
  const final = await pollJob(data.job);
236
  if (final.status !== "done") { statusEl.textContent = "Error: " + (final.error || final.status); return; }
237
+ statusEl.textContent = `${final.result.links.length} links across ${final.result.tracks.length} genomes.`;
238
  renderSynteny(final.result);
239
+ }
240
 
241
  async function analyze() {
242
  const input = document.getElementById("input").value;
243
  const statusEl = document.getElementById("status");
244
  const jobsEl = document.getElementById("jobs");
245
+ document.getElementById("neighbors-section").hidden = true;
246
  jobsEl.innerHTML = "";
247
  statusEl.className = "status";
248
  statusEl.textContent = "Running...";
 
271
  document.querySelectorAll("#view-tabs .tab").forEach(b => b.classList.toggle("on", b.dataset.tab === "circular"));
272
  document.getElementById("circular-section").hidden = false;
273
  document.getElementById("genes-section").hidden = true;
 
274
 
275
  document.getElementById("neighbors-section").hidden = false;
276
  document.getElementById("neighbors-status").textContent = "pending...";
 
277
 
278
  // Poll every job concurrently -- jobs.jobs isn't in a meaningful
279
  // order (jsonify sorts keys), and a slow job must never block a
280
+ // fast one's panel from updating.
281
  for (const [key, jobId] of Object.entries(data.jobs || {})) {
282
  if (key === "genome_neighbors") {
283
  pollJob(jobId, { onDone: finalData => {
 
284
  if (finalData.status !== "done") {
285
+ document.getElementById("neighbors-status").textContent = finalData.error || finalData.status;
286
  return;
287
  }
288
+ const neighborIds = finalData.result.map(h => h.genome_id);
289
+ loadSynteny(data.analysis_id, neighborIds);
 
 
 
290
  }});
291
  continue;
292
  }
utils/synteny.py CHANGED
@@ -91,29 +91,35 @@ def neighbor_track(genome_id):
91
  genes = [dict(zip(cols, r)) for r in rows]
92
 
93
  genomes_path = os.path.join(IMGVR_DIR, "phlam_genomes.parquet")
94
- length_bp = retry(
95
  lambda: genes_store._con.cursor().execute(
96
- "SELECT length_bp FROM read_parquet(?) WHERE genome_id = ?", [genomes_path, genome_id]).fetchone(),
97
- label=f"neighbor_track length {genome_id}")
98
- length_bp = length_bp[0] if length_bp else (genes[-1]["end_pos"] if genes else 0)
 
 
 
 
 
99
 
100
  phl = retry(lambda: np.stack([genes_store.index.reconstruct(int(g["gene_id"])) for g in genes])
101
  if genes else np.zeros((0, 480), np.float32), label=f"neighbor_track phlam vecs {genome_id}")
102
  esm = _embed_proteins([g["protein_id"] for g in genes])
103
 
104
  return {
105
- "genome_id": genome_id, "name": f"genome_{genome_id}", "length_bp": length_bp,
106
  "genes": genes, "esm": _normalize(esm), "phlam": _normalize(phl),
107
  }
108
 
109
 
110
  def query_track(genome_name, genome_length, genes, esm_embeds, phlam_protein):
111
- """genes: pipeline.py's gene_records (start/end/strand keys), same order
112
- as esm_embeds/phlam_protein."""
 
113
  return {
114
  "genome_id": None, "name": genome_name, "length_bp": genome_length,
115
- "genes": [{"gene_id": None, "begin_pos": g["start"], "end_pos": g["end"], "strand": g["strand"]}
116
- for g in genes],
117
  "esm": _normalize(np.asarray(esm_embeds, dtype=np.float32)),
118
  "phlam": _normalize(np.asarray(phlam_protein, dtype=np.float32)),
119
  }
@@ -137,6 +143,7 @@ def build_synteny(tracks):
137
 
138
  out_tracks = [{
139
  "genome_id": t.get("genome_id"), "name": t.get("name"), "length_bp": t.get("length_bp"),
140
- "genes": [[g["begin_pos"], g["end_pos"], g["strand"], g.get("gene_id")] for g in t["genes"]],
 
141
  } for t in tracks]
142
  return {"tracks": out_tracks, "esm_floor": ESM_LINK_FLOOR, "phlam_floor": PHLAM_LINK_FLOOR, "links": links}
 
91
  genes = [dict(zip(cols, r)) for r in rows]
92
 
93
  genomes_path = os.path.join(IMGVR_DIR, "phlam_genomes.parquet")
94
+ meta = retry(
95
  lambda: genes_store._con.cursor().execute(
96
+ "SELECT length_bp, contig_id FROM read_parquet(?) WHERE genome_id = ?",
97
+ [genomes_path, genome_id]).fetchone(),
98
+ label=f"neighbor_track meta {genome_id}")
99
+ length_bp, contig_id = meta if meta else (None, None)
100
+ length_bp = length_bp if length_bp is not None else (genes[-1]["end_pos"] if genes else 0)
101
+ name = contig_id or f"genome_{genome_id}"
102
+ for g in genes:
103
+ g["label"] = f"{name}_{g['gene_order']}"
104
 
105
  phl = retry(lambda: np.stack([genes_store.index.reconstruct(int(g["gene_id"])) for g in genes])
106
  if genes else np.zeros((0, 480), np.float32), label=f"neighbor_track phlam vecs {genome_id}")
107
  esm = _embed_proteins([g["protein_id"] for g in genes])
108
 
109
  return {
110
+ "genome_id": genome_id, "name": name, "length_bp": length_bp,
111
  "genes": genes, "esm": _normalize(esm), "phlam": _normalize(phl),
112
  }
113
 
114
 
115
  def query_track(genome_name, genome_length, genes, esm_embeds, phlam_protein):
116
+ """genes: pipeline.py's gene_records (start/end/strand/id keys), same
117
+ order as esm_embeds/phlam_protein. g["id"] is already a readable label
118
+ (e.g. "my_phage_1", see utils/gene_caller.py)."""
119
  return {
120
  "genome_id": None, "name": genome_name, "length_bp": genome_length,
121
+ "genes": [{"gene_id": None, "begin_pos": g["start"], "end_pos": g["end"], "strand": g["strand"],
122
+ "label": g["id"]} for g in genes],
123
  "esm": _normalize(np.asarray(esm_embeds, dtype=np.float32)),
124
  "phlam": _normalize(np.asarray(phlam_protein, dtype=np.float32)),
125
  }
 
143
 
144
  out_tracks = [{
145
  "genome_id": t.get("genome_id"), "name": t.get("name"), "length_bp": t.get("length_bp"),
146
+ "genes": [[g["begin_pos"], g["end_pos"], g["strand"], g.get("gene_id"), g.get("label")]
147
+ for g in t["genes"]],
148
  } for t in tracks]
149
  return {"tracks": out_tracks, "esm_floor": ESM_LINK_FLOOR, "phlam_floor": PHLAM_LINK_FLOOR, "links": links}