Skip to content

Commit 37e9d84

Browse files
committed
feat: use visjs
1 parent a16ab7c commit 37e9d84

File tree

4 files changed

+112
-37
lines changed

4 files changed

+112
-37
lines changed

web/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
"dependencies": {
1313
"@fontsource-variable/noto-sans": "^5.2.10",
1414
"astro": "^5.16.6",
15-
"cytoscape": "^3.33.1",
1615
"dayjs": "^1.11.19",
1716
"medium-zoom": "^1.1.0",
1817
"sass": "^1.97.1"
18+
},
19+
"devDependencies": {
20+
"vis-data": "^8.0.3",
21+
"vis-network": "^10.0.2"
1922
}
2023
}

web/pnpm-lock.yaml

Lines changed: 79 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/src/components/Visualization.astro

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ interface Props {
77
88
const { interactome } = Astro.props;
99
10-
const noHeaderInteractome = interactome.trim().split("\n").slice(1).join("\n")
10+
const noHeaderInteractomeArray = interactome.trim().split("\n").slice(1)
11+
const noHeaderInteractome = (noHeaderInteractomeArray.length > 200 ? "BIG" : noHeaderInteractomeArray.join("\n"))
1112
---
1213

1314
<style>
14-
#cytoscapeVisualizer {
15+
#visjsVisualizer {
1516
width: 100%;
1617
border: 1px solid gray;
1718
margin-bottom: 1rem;
@@ -27,10 +28,10 @@ const noHeaderInteractome = interactome.trim().split("\n").slice(1).join("\n")
2728
}
2829
</style>
2930

30-
<div data-interactome={noHeaderInteractome} id="cytoscapeVisualizer">
31+
<div data-interactome={noHeaderInteractome} id="visjsVisualizer">
3132
{
3233
noHeaderInteractome === '' ? <p>There is nothing to visualize.</p> :
33-
<p id="cytoscapeSizeWarning" hidden>
34+
<p id="visjsSizeWarning" hidden>
3435
There are over 200 edges in this graph. Visualizing it may lag your machine, and
3536
may also not be visually meaningful. If you do want to see this
3637
data visualized, using the local Cytoscape analyzer SPRAS has may
Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
<script>
2-
import cytoscape from 'cytoscape';
2+
import { Network } from 'vis-network/esnext';
3+
import { DataSet } from 'vis-data/peer'
34

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")!
67

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[][]) {
129
const nodes = [...new Set(edges.map(edge => [edge[0], edge[1]]).flat())]
1310

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) }))
2613

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;
2923
} 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+
}
3132
}
3233
</script>

0 commit comments

Comments
 (0)