/
plot_PPI.py
314 lines (278 loc) · 10.4 KB
/
plot_PPI.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
303
304
305
306
307
308
309
310
311
312
313
314
import os
import networkx as nx
import matplotlib
matplotlib.use('Agg')
from matplotlib.offsetbox import AnchoredText
import matplotlib.pyplot as plt
plt.rcParams['image.cmap'] = 'RdBu_r'
def node(item, nodes, center, colors, labels1, labels2):
if item not in nodes:
nodes.append(item)
if len(item) > 5:
labels2[item] = item
labels1[item] = ""
else:
labels1[item] = item
labels2[item] = ""
if (center["locus_tag"] == item) or \
(center["gene_name"] == item):
colors[item] = '#FFFF66'
else:
colors[item] = '#CCFFCC'
def get_largest_compare(tick, score):
same = True
if (score >= 20) and tick >= 20:
pass
else:
if score != tick:
same = False
if score > tick:
if score >= 20:
tick = 20
else:
tick = score
return (tick, same)
def add_edge(G, ppi, style, weight, colorppi):
G.add_edge(ppi["item_a"], ppi["item_b"],
color=float(colorppi), style=style, weight=weight)
def add_node(G, nodes):
G.add_nodes_from(nodes)
def best_assign_attributes(check_na, G, ppi, pre_ppi, first, style):
check_na["best"] = True
if ppi["score"] == 0:
if ppi["below"] >= 20:
weight = 22
else:
weight = ppi["below"] + 1
else:
if ppi["score"] >= 20:
weight = 22
else:
weight = ppi["score"] + ppi["below"] + 1
add_edge(G, ppi, style, weight, ppi["best"])
if not first:
if pre_ppi["best"] != ppi["best"]:
check_na["same_best"] = True
def create_node(ppis, scores, nodes, center, colors, labels1, labels2, edges,
G, cutoff_score, check_na, pre_ppi):
first = True
for ppi in ppis:
scores.append(ppi["score"])
node(ppi["item_a"], nodes, center, colors, labels1, labels2)
node(ppi["item_b"], nodes, center, colors, labels1, labels2)
if ((ppi["item_a"], ppi["item_b"]) not in edges) or \
((ppi["item_b"], ppi["item_a"]) not in edges):
edges.append((ppi["item_a"], ppi["item_b"]))
if ppi["best"] == "NA":
add_edge(G, ppi, 'dashed', 1, -1)
elif float(ppi["best"]) <= cutoff_score:
best_assign_attributes(check_na, G, ppi,
pre_ppi, first, "dashdot")
else:
best_assign_attributes(check_na, G, ppi,
pre_ppi, first, "solid")
pre_ppi = ppi
first = False
add_node(G, nodes)
return pre_ppi
def modify_label(labels2, new_labels):
for key, value in labels2.items():
if "_" in value:
new_labels[key] = value.replace("_", "\n")
else:
new_labels[key] = value
def plot_text(check_na, plt, ppis, ppi, color_edge):
na = False
if check_na["na"]:
na = True
elif check_na["best"]:
if len(ppis) < 2:
na = True
else:
cbar = plt.colorbar(color_edge)
cbar.ax.tick_params(labelsize=16)
return na
def nx_node(G, pos, node_size, colors, color_list):
'''draw the node'''
nx.draw_networkx_nodes(G, pos, node_size=node_size, node_shape='o',
nodelist=colors.keys(), node_color=color_list,
linewidths=1)
def nx_edge(G, pos, edges, colors, styles, weights):
'''draw the edge'''
color_edge = (nx.draw_networkx_edges(G, pos, edges=edges,
edge_color=colors, style=styles, width=weights,
edge_vmin=-1, edge_vmax=1, vmin=-1, vmax=1))
return color_edge
def nx_label(G, pos, labels, size):
'''setup the label of network'''
nx.draw_networkx_labels(G, pos, labels, font_size=size, font_weight='bold')
def nx_color_style(G, edges):
'''setup the color of network'''
colors = []
styles = []
check_na = True
for u, v in edges:
colors.append(G[u][v]['color'])
styles.append(G[u][v]['style'])
if (G[u][v]['style'] == "solid") or (
G[u][v]['style'] == "dashdot"):
check_na = False
return colors, styles, check_na
def print_title(plt, na, center):
if not na:
plt.title("|".join([center["locus_tag"],
" ".join([center["gene_name"],
"(based on the score of best literature)"])]),
fontsize="16")
else:
plt.title("|".join([center["locus_tag"],
" ".join([center["gene_name"],
"(based on the score of best literature)"])]) + \
"\n the numbers of supported literatures in all interactions are 0",
fontsize="16")
def plot(ppis, center, strain, cutoff_score, node_size, out_folder):
nodes = []
edges = []
labels1 = {}
labels2 = {}
colors = {}
check_na = {"number": False, "best": False, "na": False,
"same_number": False, "same_best": False}
pre_ppi = ""
scores = []
weights = []
plt.figure(figsize=(15, 15))
G = nx.Graph()
pre_ppi = create_node(ppis, scores, nodes, center, colors,
labels1, labels2, edges, G,
cutoff_score, check_na, pre_ppi)
pos = nx.spring_layout(G, k=2, scale=3, iterations=20)
color_list = []
for color in colors.values():
color_list.append(color)
nx_node(G, pos, node_size, colors, color_list)
connects = G.edges()
for weight in G.edges(data=True):
if weight[2]["weight"] <= 30:
weights.append(weight[2]["weight"])
else:
weights.append(30)
colors, styles, check_na["na"] = nx_color_style(G, connects)
color_edge = nx_edge(G, pos, connects, colors, styles, weights)
nx_label(G, pos, labels1, 12)
new_labels = {}
modify_label(labels2, new_labels)
nx_label(G, pos, new_labels, 10)
na = plot_text(check_na, plt, ppis, pre_ppi, color_edge)
print_title(plt, na, center)
plt.axis('off')
if strain not in os.listdir(out_folder):
os.mkdir(os.path.join(out_folder, strain))
plt.savefig(os.path.join(out_folder, strain,
"_".join([center["locus_tag"], center["gene_name"] + ".png"])),
bbox_inches="tight")
plt.clf()
plt.close('all')
return check_na
def score_compare(score, scores, cutoff_score, ppi):
'''check the number of literatures which are pass the cutoff'''
if score == "NA":
ppi["score"] = 0
ppi["below"] = 0
elif float(score) >= cutoff_score:
scores["score"] += 1
else:
scores["below"] += 1
def assign_score_below(pre_ppi, scores, ppis):
if "score" not in pre_ppi.keys():
pre_ppi["score"] = scores["score"]
if "below" not in pre_ppi.keys():
pre_ppi["below"] = scores["below"]
ppis.append(pre_ppi)
def get_best(pre_ppi, ppi, row):
'''get the best score of PPI'''
if "best" not in pre_ppi.keys():
ppi["best"] = row[8]
else:
if pre_ppi["best"] == "NA":
ppi["best"] = row[8]
else:
if float(pre_ppi["best"]) < float(row[8]):
ppi["best"] = row[8]
else:
ppi["best"] = pre_ppi["best"]
def interaction(first, pre_ppi, scores, ppis, match, center, cutoff_score,
node_size, out_folder):
'''check the interaction of two proteins'''
if first:
pass
else:
assign_score_below(pre_ppi, scores, ppis)
if match:
plot(ppis, center, pre_ppi["strain"], cutoff_score,
node_size, out_folder)
match = False
else:
print("No interacted partner with {0} | {1}".format(
center["locus_tag"], center["gene_name"]))
scores = {"score": 0, "below": 0}
ppis = []
first = True
return first, scores, match, ppis
def plot_ppi(PPI_file, cutoff_score, out_folder, node_size):
'''plot the network of PPI'''
ppis = []
first = True
pre_ppi = None
scores = {"score": 0, "below": 0}
center = {}
start = False
match = False
with open(PPI_file) as fh:
for line in fh:
line = line.strip()
row = line.split("\t")
start = True
if row[0].startswith("Interaction"):
first, scores, match, ppis = interaction(
first, pre_ppi, scores, ppis, match, center,
cutoff_score, node_size, out_folder)
datas = row[0].split(" | ")
center["locus_tag"] = datas[0].split(" ")[-1]
center["gene_name"] = datas[-1]
print("Plotting {0}".format(center["gene_name"]))
elif row[0] == "Genome":
pass
else:
ppi = {"strain": row[0], "item_a": row[1], "item_b": row[2],
"mode": row[3]}
if (ppi["item_a"] == center["locus_tag"]) or (
ppi["item_a"] == center["gene_name"]) or (
ppi["item_b"] == center["locus_tag"]) or (
ppi["item_b"] == center["gene_name"]):
match = True
if first:
first = False
score_compare(row[8], scores, cutoff_score, ppi)
ppi["best"] = row[8]
else:
if (ppi["strain"] == pre_ppi["strain"]) and (
ppi["item_a"] == pre_ppi["item_a"]) and (
ppi["item_b"] == pre_ppi["item_b"]):
get_best(pre_ppi, ppi, row)
score_compare(row[8], scores, cutoff_score, ppi)
else:
assign_score_below(pre_ppi, scores, ppis)
scores = {"score": 0, "below": 0}
score_compare(row[8], scores, cutoff_score, ppi)
ppi["best"] = row[8]
pre_ppi = ppi
if start and match:
assign_score_below(pre_ppi, scores, ppis)
plot(ppis, center, pre_ppi["strain"],
cutoff_score, node_size, out_folder)
elif not start:
print("No proper result can be retrieved in " + PPI_file)
elif not match:
print("No interacted partner with {0} | {1}".format(
center["locus_tag"], center["gene_name"]))