forked from SuLab/DrugMechDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
302 lines (250 loc) · 11.9 KB
/
parse.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from datetime import date
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import os
import pandas as pd
import difflib
from path_plots import dmdb_plots as dp
import re
import shutil
from yaml import safe_load
from utils.build_indications import get_id_num, read_deprecated_ids
def generate_md_header(output, title, permalink, datatable=False):
output.write("---\n")
output.write("title: \"" + title + "\"\n")
output.write("sidebar: mydoc_sidebar\n")
output.write("permalink: " + permalink + ".html\n")
output.write("toc: false \n")
if datatable:
output.write("datatable: true \n")
output.write("---\n\n")
def generate_out_dirs():
to_make = ['pages/mydoc',
'images',
'_data/sidebars']
for d in to_make:
os.makedirs(d, exist_ok=True)
def generate_home_pages():
with open("pages/CurationGuide.md", 'w') as output:
generate_md_header(output, "Curation Guide", "CurationGuide2")
with open("CurationGuide.md", 'r') as input:
content = input.readlines()
content.pop(0) # title will generate from jekyll
for line in content:
output.write(line)
with open("pages/index.md", 'w') as output:
generate_md_header(
output, "Indication Mechanism of Action Database", "index")
with open("README.md", 'r') as input:
content = input.readlines()
content.pop(0)
for line in content:
output.write(line)
def restart_file(file_name):
if os.path.exists(file_name):
os.remove(file_name)
def restart_sidebar():
restart_file('_data/sidebars/mydoc_sidebar.yml')
shutil.copyfile('_data/sidebars/copy_sidebar.yml',
'_data/sidebars/mydoc_sidebar.yml')
def read_yaml():
stream = open("indication_paths.yaml", 'r')
data = safe_load(stream) # data returned as list of dictionaries
stream.close()
return data
def generate_path_pages():
data = read_yaml()
deprecated_ids = read_deprecated_ids()
# 1. overview table metadata and header
#restart_file("pages/mydoc/overview.md")
with open("pages/mydoc/overview.md", "w") as output:
generate_md_header(output=output, title="Paths Overview",
permalink="overview", datatable=True)
output.write("<div class=\"datatable-begin\"></div>\n\n")
output.write("|--------|------|-------------|--------------|--------" +
"-|-----------------|--------------------|-------------" +
"---------|\n")
output.write("|Path ID | Drug | Drugbank ID | Drug MESH ID | Disease" +
" | Disease MESH ID | Number of Concepts | Num of Relat" +
"ionships |\n")
output.write("|--------|------|-------------|--------------|--------" +
"-|-----------------|--------------------|-------------" +
"---------|\n")
ax = plt.axes([0, 0, 1, 1], frameon=False) # remove border
drug_sidebar_data = {}
disease_sidebar_data = {}
for path in data:
new_path = True
pathid = path['graph']['_id']
pathid_num = get_id_num(pathid)
nodes = []
edge_lookup = {}
for node in path["nodes"]:
nodes.append(node["name"] + "\n" +
node["id"] + "\n" + node["label"])
edge_lookup[node["id"]] = nodes[len(nodes)-1]
# 2. generate pathway pages
outfile_name = pathid
outfile_name = pathid.replace('_', '-').lower()
# Check for deprecation and delete if true
if pathid in deprecated_ids:
# delete the webpage
if os.path.exists("pages/mydoc/" + outfile_name + ".md"):
os.remove("pages/mydoc/" + outfile_name + ".md")
# delete the image
if os.path.exists("images/" + outfile_name + ".png"):
os.remove("images/" + outfile_name + ".png")
# Jump to next iteration
continue
# Check to see if the file already exists
if os.path.exists("pages/mydoc/" + outfile_name + ".md"):
with open("pages/mydoc/" + outfile_name + ".md", 'r') as in_file:
old_file = in_file.readlines()
else:
old_file = []
# Generate the new content
restart_file("pages/mydoc/" + outfile_name + ".md")
with open("pages/mydoc/" + outfile_name + ".md", "w") as output:
path_title = (path["graph"]["drug"].title() + " - " +
path["graph"]["disease"].title())
# Add numbers to title if multiple paths fo the indications
if pathid_num != 1:
path_title += ' - ' + str(pathid_num)
# required header for format
generate_md_header(
output=output, title=path_title, permalink=outfile_name)
output.write("\nPath ID: `" + pathid + "`\n")
output.write('{% include image.html url="images/' + outfile_name + '.png" ' +
'file="' + outfile_name + '.png" alt="' +
outfile_name + '" %}\n\n')
output.write("## Concepts\n\n")
output.write("|------------|------|---------|\n")
output.write("| Identifier | Name | Type |\n")
output.write("|------------|------|---------|\n")
for node in path['nodes']:
output.write("| " + node["id"] + " | " + node["name"] + " | "
+ node["label"] + " |\n")
output.write("|------------|------|---------|\n")
output.write("\n## Relationships\n\n")
output.write("|---------|-----------|---------|\n")
output.write("| Subject | Predicate | Object |\n")
output.write("|---------|-----------|---------|\n")
for edge in path["links"]:
output.write("| " + edge_lookup[edge["source"]].split('\n')[0].title() + " | "
+ edge["key"].upper() + " | "
+ edge_lookup[edge["target"]].split('\n')[0].title() + " |\n")
output.write("|---------|-----------|---------|\n")
if 'comment' in path: # Display Comment if available
if path["comment"] is not None:
comment = add_md_hyperlink(path["comment"])
output.write("\nComment: " + comment + "\n")
# Add references from path info if available
if 'reference' in path:
ref_out = "\n - " + "\n - ".join([add_md_hyperlink(r) for r in path['reference']])
output.write("\nReference: " + ref_out + "\n")
elif 'drugbank' in path["graph"]: # generate drugbank url if no ref present
if path["graph"]['drugbank'] is not None:
url = ("https://go.drugbank.com/drugs/" +
path['graph']['drugbank'].split(':', 1)[1] +
"#mechanism-of-action")
output.write("\nReference:\n - [" + url + "](" + url +
"){:target=\"_blank\"}")
# Compare the new content with the old content
with open("pages/mydoc/" + outfile_name + ".md", 'r') as in_file:
new_file = in_file.readlines()
delta = list(difflib.unified_diff(old_file, new_file))
if delta == []:
new_file = False
# 3. generate and save plots
# Only remake if the files the page is new and/or different
# or if the image is missing
if new_file or not os.path.exists("images/" + outfile_name + ".png"):
print('Building new page: ' + path_title)
if os.path.exists("images/" + outfile_name + ".png"):
os.remove("images/" + outfile_name + ".png")
dp.plot_path(path)
plt.savefig("images/" + outfile_name + ".png")
plt.close(fig=None)
ax.clear()
# 4. save sidebar info for later generation
if path["graph"]["drug"] not in drug_sidebar_data:
drug_sidebar_data[path["graph"]["drug"].title()] = {
path_title: outfile_name}
else:
drug_sidebar_data[path["graph"]["drug"].title()][
path_title] = outfile_name
if path["graph"]["disease"] not in disease_sidebar_data:
disease_sidebar_data[path["graph"]["disease"].title()] = {
path_title: outfile_name}
else:
disease_sidebar_data[path["graph"]["disease"].title()][
path_title] = outfile_name
# 5. add entry to jquery table for overview page
with open("pages/mydoc/overview.md", "a") as output:
output.write("| [" + pathid + "](" + outfile_name + ".html" + ") | "
+ path["graph"]["drug"] + " | "
+ str(path["graph"]["drugbank"]) + " | "
+ str(path["graph"]["drug_mesh"]) + " | "
+ path["graph"]["disease"] + " | "
+ str(path["graph"]["disease_mesh"]) + " | "
+ str(len(path["nodes"])) + " | "
+ str(len(path["links"])) + " |\n")
# 6. generate footer for overview table
with open("pages/mydoc/overview.md", "a") as output:
output.write("|--------|-----|-------------|--------------|---------" +
"|-----------------|--------------------|--------------" +
"--------|\n\n")
output.write("<div class=\"datatable-end\"></div>\n")
return drug_sidebar_data, disease_sidebar_data
def add_md_hyperlink(text):
# Split across spaces to separate words
text_split = text.split(' ')
out_text = []
# check each word to see if it's a hyperlink
# (current logic is very basic, just checks for http)
for word in text_split:
if word.startswith('http'):
out_text.append('['+ word +'](' + word + ')')
else:
out_text.append(word)
return ' '.join(out_text)
def generate_sidebar(drug_sidebar_data, disease_sidebar_data):
restart_sidebar()
with open("_data/sidebars/mydoc_sidebar.yml", 'a') as sidebar:
sidebar.write(" subfolders:\n")
# Sort alpha so easier to find
sorted_drug_keys = sorted(drug_sidebar_data.keys())
for key in sorted_drug_keys:
value = drug_sidebar_data[key]
sidebar.write(" - title: " + key + "\n")
sidebar.write(" output: web\n")
sidebar.write(" subfolderitems:\n\n")
for k, v in value.items():
sidebar.write(" - title: " + k + "\n")
sidebar.write(" url: /" + v + ".html\n")
sidebar.write(" output: web\n\n")
sidebar.write(" - title: Diseases\n")
sidebar.write(" output: web\n")
sidebar.write(" folderitems:\n\n")
sidebar.write(" - title: Paths Overview\n")
sidebar.write(" url: /overview.html\n")
sidebar.write(" output: web, pdf\n\n")
sidebar.write(" subfolders:\n")
# Sort Alpha so easy to find
sorted_disease_keys = sorted(disease_sidebar_data.keys())
for key in sorted_disease_keys:
value = disease_sidebar_data[key]
sidebar.write(" - title: " + key + "\n")
sidebar.write(" output: web\n")
sidebar.write(" subfolderitems:\n\n")
for k, v in value.items():
sidebar.write(" - title: " + k + "\n")
sidebar.write(" url: /" + v + ".html\n")
sidebar.write(" output: web\n\n")
def main():
generate_out_dirs()
generate_home_pages()
drug_sidebar_data, disease_sidebar_data = generate_path_pages()
generate_sidebar(drug_sidebar_data, disease_sidebar_data)
main()