-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdp_network.py
More file actions
169 lines (142 loc) · 4.35 KB
/
cdp_network.py
File metadata and controls
169 lines (142 loc) · 4.35 KB
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
# =========================
# Country–Disease–Drug–PMC Network
# =========================
import pandas as pd
import re, json
import networkx as nx
from collections import defaultdict
import argparse
# =========================
# Node color mapping ← ADD IT HERE
# =========================
color_map = {
"PMC": "#1f77b4", # blue
"COUNTRY": "#2ca02c", # green
"DISEASE": "#d62728", # red
"DRUG": "#9467bd", # purple
"CONTINENT": "#8c564b", # brown
"UNKNOWN": "#cccccc" # grey fallback
}
# -------- CLI arguments --------
parser = argparse.ArgumentParser()
parser.add_argument("--country", required=True, help="Country CSV")
parser.add_argument("--disease", required=True, help="Disease CSV")
parser.add_argument("--drug", required=True, help="Drug CSV")
parser.add_argument("--graphml", required=True)
parser.add_argument("--html", required=True)
args = parser.parse_args()
# -------- Load CSVs --------
df_country = pd.read_csv(args.country)
df_disease = pd.read_csv(args.disease)
df_drug = pd.read_csv(args.drug)
# -------- Helpers --------
def wiki(title):
return f"https://en.wikipedia.org/wiki/{title.replace(' ', '_')}"
def pmc_url(pmc):
return f"https://www.ncbi.nlm.nih.gov/pmc/articles/{pmc}/"
def extract_pmc(path):
m = re.search(r"(PMC\d+)", str(path))
return m.group(1) if m else None
# -------- Graph --------
G = nx.Graph()
edge_w = defaultdict(int)
# ========================
# COUNTRY → PMC
# ========================
for _, r in df_country.iterrows():
pmc = extract_pmc(r["file_path"])
if not pmc: continue
countries = {c.strip() for c in str(r["0"]).split(",") if c.strip()}
G.add_node(pmc, type="PMC", url=pmc_url(pmc))
for c in countries:
G.add_node(c, type="COUNTRY", url=wiki(c))
edge_w[(pmc, c)] += 1
G.add_edge(pmc, c)
# ========================
# DISEASE → PMC
# ========================
for _, r in df_disease.iterrows():
pmc = extract_pmc(r["file_path"])
if not pmc: continue
diseases = {d.strip().lower() for d in str(r["0"]).split(",") if d.strip()}
for d in diseases:
d = d.capitalize()
G.add_node(d, type="DISEASE", url=wiki(d))
edge_w[(pmc, d)] += 1
G.add_edge(pmc, d)
# ========================
# DRUG → PMC + DISEASE
# ========================
for _, r in df_drug.iterrows():
pmc = extract_pmc(r["file_path"])
if not pmc: continue
drugs = {d.strip().lower() for d in str(r["0"]).split(",") if d.strip()}
for drug in drugs:
drug = drug.capitalize()
G.add_node(drug, type="DRUG", url=wiki(drug))
edge_w[(pmc, drug)] += 1
G.add_edge(pmc, drug)
# disease–drug association (if disease column exists)
if "disease" in r:
disease = r["disease"].capitalize()
G.add_node(disease, type="DISEASE", url=wiki(disease))
edge_w[(disease, drug)] += 1
G.add_edge(disease, drug)
# -------- Assign weights --------
for (s, t), w in edge_w.items():
if G.has_edge(s, t):
G[s][t]["weight"] = w
# -------- Export GraphML --------
nx.write_graphml(G, args.graphml)
print("✔ GraphML exported")
# -------- Cytoscape HTML --------
elements = []
for n, d in G.nodes(data=True):
node_type = d.get("type", "UNKNOWN")
elements.append({
"data": {
"id": n,
"label": n,
"type": node_type,
"color": color_map.get(node_type, "#cccccc"),
"url": d.get("url")
}
})
for s, t, d in G.edges(data=True):
elements.append({
"data": {
"source": s,
"target": t,
"weight": d.get("weight", 1)
}
})
with open(args.html, "w") as f:
f.write(f"""
<html>
<head>
<script src="https://unpkg.com/cytoscape@3.21.2/dist/cytoscape.min.js"></script>
</head>
<body>
<div id="cy" style="width:100%;height:800px;"></div>
<script>
cytoscape({{
container: document.getElementById('cy'),
elements: {json.dumps(elements)},
style: [
{{ selector: 'node', style: {{
'label':'data(label)',
'background-color':'data(color)',
'font-size':'10px'
}} }},
{{ selector:'edge', style:{{
'width':'mapData(weight,1,10,1,6)'
}} }}
],
layout: {{ name:'cose' }}
}})
.on('tap','node',e=>window.open(e.target.data('url'),'_blank'));
</script>
</body>
</html>
""")
print("✔ Interactive HTML saved")