-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.js
144 lines (127 loc) · 3.73 KB
/
preprocess.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Call with `node utils/make_id_unique api/openapi.json` to make each `operationId` unique (OVERWRITES FILE)
*/
const fs = require("fs");
const { v4 } = require("uuid");
const BASE_URL = "https://robokop-automat.apps.renci.org";
const AUTOMAT_OPENAPI_URL = `${BASE_URL}/openapi.yml`;
const OPENAPI_PATH = "./api/openapi.json";
async function main() {
let jsonData = await fetchSpec();
await writeSourceMetadata(jsonData);
fs.writeFile(OPENAPI_PATH, JSON.stringify(jsonData, null, 2), (err) => {
if (err) throw err;
});
}
main();
async function fetchSpec() {
return await fetch(AUTOMAT_OPENAPI_URL, {
method: "GET",
headers: { Accept: "application/json" },
}).then((data) => data.json());
}
async function writeSourceMetadata(jsonData) {
const SOURCES = [
"binding-db",
"cam-kp",
"ctd",
"drugcentral",
"genome-alliance",
"gtex",
"gtopdb",
"gwas-catalog",
"hetio",
"hgnc",
"hmdb",
"human-goa",
"icees-kg",
"intact",
"monarch-kg",
"panther",
"pharos",
"reactome",
"robokopkg",
"string-db",
"textminingkp",
"ubergraph",
"viral-proteome",
];
const metadata = new Map();
for (const source of SOURCES) {
const res = await fetch(`${BASE_URL}/${source}/metadata`);
if (!res.ok)
throw new Error(`Something went wrong fetching the ${source} metadata`);
const json = await res.json();
metadata.set(source, json);
}
const traverseJson = (node) => {
for (let key in node) {
if (node.hasOwnProperty(key)) {
if (key === "tags") {
// replace all tags with their display name
const tags = node[key];
// [ 'translator', 'viral-proteome' ]
if (Array.isArray(tags) && tags.every((t) => typeof t === "string")) {
for (let i = 0; i < tags.length; ++i) {
if (metadata.has(tags[i]) && metadata.get(tags[i])?.graph_name) {
tags[i] = metadata.get(tags[i]).graph_name;
}
}
}
// [{ name: "blah", description: "blahblahblah" }]
else if (
Array.isArray(tags) &&
tags.every((t) => typeof t === "object")
) {
for (const tag of tags) {
if (
!tag?.name ||
!metadata.has(tag.name) ||
!metadata.get(tag.name).graph_name ||
!tag?.description
) {
console.log(`${tag.name} does not have a name/description`);
continue;
}
const tagMd = metadata.get(tag.name);
tag.name = tagMd.graph_name;
const {
graph_version,
graph_description,
graph_url,
neo4j_dump,
final_node_count,
final_edge_count,
} = tagMd;
// split the neo4j_dump url on '/' to get the containing directory and link to that instead
const fileDirLink = neo4j_dump
?.split("/")
?.slice(0, -1)
?.join("/");
tag.description = `
${graph_description ?? ""}\n
${graph_version ? `**Version:** ${graph_version}` : ""}\n
${graph_url ? `**URL:** [${graph_url}](${graph_url})` : ""}\n
${fileDirLink ? `**Files:** [${fileDirLink}](${fileDirLink})` : ""}\n
${
final_node_count
? `**Nodes:** ${parseInt(final_node_count).toLocaleString()}`
: ""
}\n
${
final_edge_count
? `**Edges:** ${parseInt(final_edge_count).toLocaleString()}`
: ""
}\n
`;
}
}
} else if (typeof node[key] === "object") {
node[key] = traverseJson(node[key]);
}
}
}
return node;
};
traverseJson(jsonData);
}