|
1 | 1 | <script> |
2 | | - import cytoscape from 'cytoscape'; |
| 2 | + import { Network } from 'vis-network/esnext'; |
| 3 | + import { DataSet } from 'vis-data/peer' |
3 | 4 |
|
4 | | - const cytoscapeContainer = document.querySelector<HTMLDivElement>("#cytoscapeVisualizer")!; |
5 | | - const cytoscapeSizeWarning = document.querySelector<HTMLParagraphElement>("#cytoscapeSizeWarning")! |
| 5 | + const visjsContainer = document.querySelector<HTMLDivElement>("#visjsVisualizer")!; |
| 6 | + const visjsSizeWarning = document.querySelector<HTMLParagraphElement>("#visjsSizeWarning")! |
6 | 7 |
|
7 | | - const interactome = cytoscapeContainer.dataset.interactome as string; |
8 | | - const interactomeLines: string[] = interactome.split("\n").filter(line => line.trim() !== ''); |
9 | | - const edges = interactomeLines.map(line => line.split("\t")) |
10 | | - |
11 | | - function produceCytoscape() { |
| 8 | + function produceCytoscape(edges: string[][]) { |
12 | 9 | const nodes = [...new Set(edges.map(edge => [edge[0], edge[1]]).flat())] |
13 | 10 |
|
14 | | - cytoscape({ |
15 | | - container: cytoscapeContainer, |
16 | | - elements: { |
17 | | - nodes: nodes.map(node => ({ data: { id: node }})), |
18 | | - edges: edges.map(([source, target, rank, dir]) => ({ data: { id: `${source}-${target}`, source, target }})) |
19 | | - }, |
20 | | - layout: { |
21 | | - name: 'grid', |
22 | | - rows: 5 |
23 | | - } |
24 | | - }); |
25 | | - } |
| 11 | + // TODO: not the best thing to run on the client |
| 12 | + const e = edges.map(([a, b]) => ({ from: nodes.findIndex(n => n == a), to: nodes.findIndex(n => n == b) })) |
26 | 13 |
|
27 | | - if (edges.length > 200) { |
28 | | - cytoscapeSizeWarning.hidden = false; |
| 14 | + new Network(visjsContainer, { |
| 15 | + nodes: new DataSet(nodes.map((node, i) => ({ id: i + 1, label: node }))), |
| 16 | + edges: new DataSet(e as any[]) // TODO: badly typed? |
| 17 | + }, {}); |
| 18 | + } |
| 19 | + |
| 20 | + const interactome = visjsContainer.dataset.interactome as string; |
| 21 | + if (interactome.trim() == 'BIG') { |
| 22 | + visjsSizeWarning.hidden = false; |
29 | 23 | } else { |
30 | | - produceCytoscape(); |
| 24 | + const interactomeLines: string[] = interactome.split("\n").filter(line => line.trim() !== ''); |
| 25 | + const edges = interactomeLines.map(line => line.split("\t")) |
| 26 | + |
| 27 | + if (edges.length > 200) { |
| 28 | + visjsSizeWarning.hidden = false; |
| 29 | + } else { |
| 30 | + produceCytoscape(edges); |
| 31 | + } |
31 | 32 | } |
32 | 33 | </script> |
0 commit comments