-
Notifications
You must be signed in to change notification settings - Fork 1
/
showPathbankData.js
269 lines (244 loc) · 7.03 KB
/
showPathbankData.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
// Get the last row clicked
let data = get("rowClicked").resurrect();
// Create the tables
let table = {
main: tableDataMain(data),
pathways: tableDataPathways(data)
};
// Find the sink divs
let sink = {
main: document.querySelector("#tableMain"),
pathways: document.querySelector("#tablePathways")
};
// Empty the sinks and append the corresponding tables
for (let g in sink) {
sink[g].innerHTML = '';
sink[g].append(table[g]);
};
/** -- Function definitions -- **/
// Apply a set of attributes to a DOM node
function applyAttributes(element, attributes) {
for (let attr in attributes) {
if (attr === "style") {
for (let s in attributes.style) element.style[s] = attributes.style[s];
} else if (attr === "innerHTML") {
element.innerHTML = attributes.innerHTML;
} else {
element.setAttribute(attr, attributes[attr]);
};
};
return element;
};
// Helper function to create a div table
function divTable() {
let attrs = {
class: "table",
style: {
display: "table"
}
};
return applyAttributes(document.createElement("div"), attrs);
};
// Helper function to create a div table row
function divTableRow() {
let attrs = {
class: "table-row",
style: {
display: "table-row"
}
};
return applyAttributes(document.createElement("div"), attrs);
};
// Helper function to create a div table cell
function divTableCell() {
let attrs = {
class: "table-cell",
style: {
display: "table-cell"
}
};
return applyAttributes(document.createElement("div"), attrs);
};
// Function for creating the table of the main data
function tableDataMain(data) {
let tableData = new Array();
let keys = Object.keys(data.main).sort();
let table = divTable();
let headerRow = divTableRow();
let headerCellAttr = {
style: {
borderBottom: "2px solid #000",
fontWeight: "bold"
}
};
["Attribute", "Value"].forEach(v => {
headerCellAttr.innerHTML = v;
headerRow.append(applyAttributes(divTableCell(), headerCellAttr));
});
table.append(headerRow);
keys.forEach(k => {
let send = data.main[k];
if (typeof(send) === "string") send = [send];
if (send === undefined || send === null) send = [''];
tableData.push({lead: k, data: send.map(s => switchDataLink(k, s))});
});
return addRowsLeader(table, tableData);
};
// Helper function for adding a series of "leader" rows to a table
function addRowsLeader(table, data) {
data.forEach(d => tableRowsLeader(d).forEach(r => table.append(r)));
return table;
};
// Function for creating a "leader"-type set of rows
function tableRowsLeader(content) {
let output = new Array();
let leaderAttrs = {
innerHTML: content.lead,
style: {
borderRight: "1px solid #000",
fontWeight: "bold",
width: "96px"
}
};
let data = content.data;
if (typeof(data) === "string") data = [data];
if (data === undefined || data === null) data = [''];
data = data.sort();
let leaderRow = divTableRow();
leaderRow.append(applyAttributes(divTableCell(), leaderAttrs));
leaderRow.append(applyAttributes(divTableCell(), {innerHTML: data[0]}));
output.push(leaderRow);
leaderAttrs.innerHTML = '';
data = data.slice(1);
while (data.length > 0) {
let row = divTableRow();
row.append(applyAttributes(divTableCell(), leaderAttrs));
row.append(applyAttributes(divTableCell(), {innerHTML: data[0]}));
output.push(row);
data = data.slice(1);
};
return output;
};
// Function for creating the table of pathways data
function tableDataPathways(data) {
let keys = Object.keys(data.pathways[0]).sort();
let table = divTable();
let headerRow = divTableRow();
let headerCellAttr = {
style: {
borderBottom: "2px solid #000",
fontWeight: "bold"
}
};
keys.forEach(k => {
headerCellAttr.innerHTML = k;
headerRow.append(applyAttributes(divTableCell(), headerCellAttr));
});
table.append(headerRow);
sortJsonKey(data.pathways, "PathBank ID").forEach(p => {
table.append(pathwayRow(p));
});
return table;
};
// Helper function for adding a row for the pathway data table
function pathwayRow(data) {
let row = divTableRow();
row.append(linkCellPathwaySvg(data["PathBank ID"]));
["Pathway Name", "Pathway Subject"].forEach(k => {
row.append(applyAttributes(divTableCell(), {innerHTML: data[k]}));
});
return row;
};
// Helper function for creating a table cell with an anchor as innerHTML
function linkCell(attr) {
let aAttrs = {
href: attr.href,
innerHTML: attr.innerHTML,
rel: "noopener noreferrer",
target: "_blank"
};
let a = applyAttributes(document.createElement("a"), aAttrs);
return applyAttributes(divTableCell(), {innerHTML: a.outerHTML});
};
function linkCellPathwaySvg(id) {
return linkCell({
href: `https://pathbank.org/view/${id}`,
innerHTML: id
});
};
// Inefficient helper function for sorting an Object by a particular attribute
function sortJsonKey(obj, key) {
let output = new Array();
let values = obj.map(o => o[key]).sort();
let data = values.map(v => obj.filter(o => o[key] === v));
data.forEach(dt => dt.forEach(d => output.push(d)));
return output;
};
// Get anchor element outerHTML, given input spec (href/innerHTML)
function aOhtml(i) {
let aAttr = {
href: i.href,
innerHTML: i.innerHTML,
rel: "noopener noreferrer",
target: "_blank"
};
return applyAttributes(document.createElement("a"), aAttr).outerHTML;
};
// Helper function for getting KEGG ID anchor outerHTML
function aKegg(id) {
return aOhtml({
href: `https://www.kegg.jp/dbget-bin/www_bget?cpd:${id}`,
innerHTML: id
});
};
// Helper function for getting ChEBI ID anchor outerHTML
function aChebi(id) {
return aOhtml({
href: `https://www.ebi.ac.uk/chebi/searchId.do?chebiId=CHEBI:${id}`,
innerHTML: id
});
};
// Helper function for getting HMDB ID anchor outerHTML
function aHmdb(id) {
return aOhtml({
href: `https://hmdb.ca/metabolites/${id}`,
innerHTML: id
});
};
// Helper function for getting CAS Registry ID anchor outerHTML
function aCas(id) {
return aOhtml({
href: `https://webbook.nist.gov/cgi/cbook.cgi?ID=${id}&Units=SI`,
innerHTML: id
});
};
// Helper function for getting chemical formula anchor outerHTML
function aFormula(id) {
return aOhtml({
href: `https://webbook.nist.gov/cgi/cbook.cgi?Formula=${id}&Units=SI`,
innerHTML: id
});
};
// Helper function for getting InChI ID or Key anchor outerHTML
function aInchi(id) {
// NIST requires "InChI=" before keys as well
let idHref = (/^InChI\=/.test(id)) ? id : `InChI=${id}`;
return aOhtml({
href: `https://webbook.nist.gov/cgi/cbook.cgi?${idHref}&Units=SI`,
innerHTML: id
});
};
// Switch to get the aOhtml of the corresponding key
function switchDataLink(key, id) {
if (id === '') return id;
switch (key) {
case "CAS": return aCas(id);
case "ChEBI ID": return aChebi(id);
case "Formula": return aFormula(id);
case "HMDB ID": return aHmdb(id);
case "InChI":
case "InChI Key": return aInchi(id);
case "KEGG ID": return aKegg(id);
default: return id;
};
};