-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_networks.py
470 lines (364 loc) · 17.5 KB
/
make_networks.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import os
import re
import csv
import ast
from time import time
import pickle
import pandas as pd
import networkx as nx
import measures
import requests
import json
class MakeNetworks:
def make_edge_network(self, edges_file, network_out_file):
# make an empty graph and populate it with edges
G = nx.Graph()
# ------------ edge attributes from viruses.string file ----------------
with open(edges_file, 'rb') as f:
# skip first line, save header
header = next(f)
header = header.split()
# read in line by line
for i, line in enumerate(f):
line = line.split()
# load in different edge types
neighborhood = int(line[2])
fusion = int(line[4])
cooccurence = int(line[5])
homology = int(line[6])
coexpression = int(line[7])
experiments = int(line[9])
database = int(line[11])
textmining = int(line[13])
combined_score = int(line[15])
# only keep experimentally verified edges
if experiments == 0 and database == 0:
continue
# get the actual protein ids
p1 = line[0].decode("utf-8")
p2 = line[1].decode("utf-8")
# add edge with attributes
G.add_edge(p1, p2,
neighborhood=neighborhood,
fusion=fusion,
cooccurence=cooccurence,
homology=homology,
coexpression=coexpression,
experiments=experiments,
database=database,
textmining=textmining
)
# pickle these networks
with open(network_out_file, 'wb') as handle:
pickle.dump(G, handle)
def make_node_list_chtc(self, network_in, list_out):
# load in the list of nodes
with open(network_in, 'rb') as f:
G = pickle.load(f)
nodes = list(G.nodes())
# break into smaller lists for size 2000
# 1 sec between https requests = ~30 min each
node_groups = [nodes[i:i + 2000] for i in range(0, len(nodes), 2000)]
# open file in write mode
for i, node_group in enumerate(node_groups):
with open(list_out + str(i) + '.txt', 'w') as fp:
for item in node_group:
fp.write("%s\n" % item)
fp.close()
return None
def split_into_subnetworks(self, network_file_out, networks_file_out):
with open(network_file_out, 'rb') as f:
whole_network = pickle.load(f)
# make list of unique viruses in here # HERE! <<<<<<<<<<<<<<<
names = []
node_data = whole_network.nodes()._nodes
for node in node_data.keys():
try:
name = node_data[node]['uniprot_data']['organism']['taxonId']
names.append(name)
names = list(set(names))
except:
continue
subgraphs = {}
# for each virus, get network of all interacting nodes
for virus in viruses:
# get list of edges that involve those nodes
virus_subgraph_edges = []
# filter list of nodes to ones with virus name
virus_subgraph_nodes = list(filter(lambda x: node_information[x]['organism'] == virus, node_information.keys()))
for node in virus_subgraph_nodes:
# get edges associated with this node
edges = list(G.edges(node))
# add to list of edges
[virus_subgraph_edges.append(e) for e in edges]
# turn list of edges into list of nodes
subgraph_nodes = []
[(subgraph_nodes.append(e[0]), subgraph_nodes.append(e[1])) for e in virus_subgraph_edges]
# make subgraph from list of nodes from edges
virus_subgraph = G.subgraph(list(set(subgraph_nodes)))
# save subgraph to dict
subgraphs[virus] = virus_subgraph
# basic subgraph stats
print(virus)
print({'n_nodes': len(virus_subgraph.nodes()), 'n_edges': len(virus_subgraph.edges())})
# pickle these networks
with open(networks_file_out, 'wb') as handle:
pickle.dump(subgraphs, handle)
return None
def make_graph(self, df, file_out_df, file_out_graph):
'''
Makes whole PPI network and then updates the previously-made df with network values
:param df: Initial df to update
:param file_out_df: File path to save updated df to (can be the same as initial df to save space)
:param file_out_graph: File path to save network to
:return: None, just makes two pickle files
'''
# make new df for the graph, has to be one column per out node
node_id = []
out_node_id = []
for index, row in df.iterrows():
if row['out_edges'] == None:
continue
for out_node in row['out_edges']:
node_id.append(row['string_id'])
out_node_id.append(out_node)
for_df_graph = list(zip(node_id, out_node_id))
df_graph = pd.DataFrame(for_df_graph, columns=['node_id', 'out_node_id'])
graph = nx.from_pandas_edgelist(df_graph, source='node_id', target='out_node_id')
# add in node attributes from the df
attributes = df.set_index('string_id').to_dict('index')
nx.set_node_attributes(graph, attributes)
# BDM vs node degree, color nodes by protein type
degrees = []
graph_degree_dict = dict(graph.degree())
for protein in df['string_id']:
try:
if graph_degree_dict[protein]:
degrees.append(graph.degree(protein))
except:
degrees.append(0)
df['degree'] = degrees
# can add more network features to this
# pickle this graph
with open(file_out_graph, 'wb') as handle:
pickle.dump(graph, handle)
# update that df
with open(file_out_df, 'wb') as handle:
pickle.dump(df, handle)
return None
def filter_networks(self, networks, filtered_experiments_networks_file_out, filtered_textmining_networks_file_out):
'''
From dict of whole networks in this dataset, filter them based on criteria.
:param networks: Dict, {virus_name_str: networkx_graph_object}
:param filtered_experiments_networks_file_out: str, path+filename for resulting pickled dictionary
:param filtered_textmining_networks_file_out: str, path+filename for resulting pickled dictionary
:return: Return networks
'''
# ------------- experiment and database networks first ---------------
# filter on edges
networks_filtered_edges = list(map(lambda x: networks[x].edge_subgraph(
list(filter(lambda y: networks[x][y[0]][y[1]]['experiments'] != 0 or
networks[x][y[0]][y[1]]['database'] != 0,
list(networks[x].edges())))).copy(), networks.keys()))
# remove lone nodes and networks without edges
networks_filtered = {}
for i, network in enumerate(networks_filtered_edges):
network.remove_nodes_from(list(nx.isolates(network)))
if len(network.edges) > 0:
networks_filtered[list(networks.keys())[i]] = network
# get nodes involved in virus-host edges only
interaction_nodes = {}
for network in networks_filtered:
node_list = []
edge_list = list(networks_filtered[network].edges)
for edge in edge_list:
node_a = edge[0]
node_b = edge[1]
node_a_type = networks_filtered[network].nodes[node_a]['type']
node_b_type = networks_filtered[network].nodes[node_b]['type']
# just get the edges between non-matching node types (virus/host edges)
if not node_a_type == node_b_type:
node_list.append(node_a)
node_list.append(node_b)
interaction_nodes[network] = node_list
# get subgraph only involving these nodes
networks_filtered = dict(map(lambda x: (x[0], x[1].subgraph(interaction_nodes[x[0]])), networks_filtered.items()))
# remove empty networks
networks_filtered = dict(filter(lambda x: len(list(x[1].edges())) > 0, networks_filtered.items()))
# sort these networks by size, then do large ones first and small ones last
networks_filtered = list(map(lambda x: [x[0], x[1], x[1].number_of_edges()], networks_filtered.items()))
networks_filtered = sorted(networks_filtered, key=lambda x: x[-1], reverse=False)
networks_filtered_experiments = dict(map(lambda x: (x[0], x[1]), networks_filtered))
# pickle these networks
with open(filtered_experiments_networks_file_out, 'wb') as handle:
pickle.dump(networks_filtered_experiments, handle)
# ------------- then do textmined networks next ---------------
# filter on edges
networks_filtered_edges = list(map(lambda x: networks[x].edge_subgraph(
list(filter(lambda y: networks[x][y[0]][y[1]]['textmining'] != 0,
list(networks[x].edges())))).copy(), networks.keys()))
# remove lone nodes and networks without edges
networks_filtered = {}
for i, network in enumerate(networks_filtered_edges):
network.remove_nodes_from(list(nx.isolates(network)))
if len(network.edges) > 0:
networks_filtered[list(networks.keys())[i]] = network
# get nodes involved in virus-host edges only
interaction_nodes = {}
for network in networks_filtered:
node_list = []
edge_list = list(networks_filtered[network].edges)
for edge in edge_list:
node_a = edge[0]
node_b = edge[1]
node_a_type = networks_filtered[network].nodes[node_a]['type']
node_b_type = networks_filtered[network].nodes[node_b]['type']
# just get the edges between non-matching node types (virus/host edges)
if not node_a_type == node_b_type:
node_list.append(node_a)
node_list.append(node_b)
interaction_nodes[network] = node_list
# get subgraph only involving these nodes
networks_filtered = dict(map(lambda x: (x[0], x[1].subgraph(interaction_nodes[x[0]])), networks_filtered.items()))
# remove empty networks
networks_filtered = dict(filter(lambda x: len(list(x[1].edges())) > 0, networks_filtered.items()))
# sort these networks by size, then do large ones first and small ones last
networks_filtered = list(map(lambda x: [x[0], x[1], x[1].number_of_edges()], networks_filtered.items()))
networks_filtered = sorted(networks_filtered, key=lambda x: x[-1], reverse=False)
networks_filtered_textmining = dict(map(lambda x: (x[0], x[1]), networks_filtered))
# pickle these networks
with open(filtered_textmining_networks_file_out, 'wb') as handle:
pickle.dump(networks_filtered_textmining, handle)
return networks_filtered_experiments, networks_filtered_textmining
def make_df(self, network, filtered_networks_file_out):
t0 = time()
network_id = network
print(network_id)
with open(filtered_networks_file_out, 'rb') as f:
networks_filtered = pickle.load(f)
network = networks_filtered[network_id]
network_measures = measures.Network_Measures()
node_measures = measures.Node_Measures()
header = [
'object_type',
'object_name',
'network_id',
'n_nodes',
'n_edges',
'n_components',
'n_hosts',
'n_viruses',
'average_node_connectivity',
'non_randomness',
#'small_world_omega',
'degree_centrality',
'betweenness_centrality',
'closeness_centrality',
'eigenvector_centrality',
'pagerank',
'katz_centrality',
'load_centrality',
'closeness_vitality',
'clustering_coefficient',
'node_ncbi_id',
'node_organism',
'node_uniprot_id',
'node_uniprot_id_type',
'node_type']
measures_data = pd.DataFrame(columns=header)
# get basic network measures
n_nodes = network.number_of_nodes()
print(n_nodes)
n_edges = network.number_of_edges()
print(n_edges)
n_components = nx.number_connected_components(network)
node_types = list(zip(nx.get_node_attributes(network, "type").values(),
list(nx.get_node_attributes(network, "ncbi_id").values())))
# number of hosts
hosts = list(filter(lambda x: x[0] == 'host', node_types))
n_hosts = len(list(set(list(map(lambda x: x[1], hosts)))))
# number of viruses
viruses = list(filter(lambda x: x[0] == 'virus', node_types))
n_viruses = len(list(set(list(map(lambda x: x[1], viruses)))))
# get whole network measures
try:
average_node_connectivity = network_measures.average_node_connectivity(network)
except:
average_node_connectivity = None
try:
non_randomness = network_measures.non_randomness(network)
except:
non_randomness = None
#try:
# small_world_omega = network_measures.small_world_omega(network)
#except:
# small_world_omega = None
#normalized_network_centrality = network_measures.normalized_network_centrality(network)
#kolmogorov_complexity = network_measures.kolmogorov_complexity(network)
# get node measures
degree_centrality = node_measures.degree_centrality(network)
betweenness_centrality = node_measures.betweenness_centrality(network)
closeness_centrality = node_measures.closeness_centrality(network)
eigenvector_centrality = node_measures.eigenvector_centrality(network)
pagerank = node_measures.pagerank(network)
katz_centrality = node_measures.katz_centrality(network)
load_centrality = node_measures.load_centrality(network)
#percolation_centrality = node_measures.percolation_centrality(network)
closeness_vitality = node_measures.closeness_vitality(network)
clustering_coefficient = node_measures.clustering_coefficient(network)
# for each node, add row to df
for node in network.nodes():
# get other node information
attributes = network.nodes[node]
row = {
'object_type': 'node',
'object_name': node,
'network_id': network_id,
'n_nodes': n_nodes,
'n_edges': n_edges,
'n_components': n_components,
'n_hosts': n_hosts,
'n_viruses': n_viruses,
'average_node_connectivity': average_node_connectivity,
'non_randomness': non_randomness,
#'small_world_omega': small_world_omega,
'degree_centrality': degree_centrality[node],
'betweenness_centrality': betweenness_centrality[node],
'closeness_centrality': closeness_centrality[node],
'eigenvector_centrality': eigenvector_centrality[node],
'pagerank': pagerank[node],
'katz_centrality': katz_centrality[node],
'load_centrality': load_centrality[node],
'closeness_vitality': closeness_vitality[node],
'clustering_coefficient': clustering_coefficient[node],
'node_ncbi_id': attributes['ncbi_id'],
'node_organism': attributes['organism'],
'node_uniprot_id': attributes['uniprot_id'],
'node_uniprot_id_type': attributes['uniprot_id_type'],
'node_type': attributes['type']
}
measures_data = measures_data.append(row, ignore_index=True)
# save measures for this network to csv
measures_data.to_csv(os.path.join('OLD/data_jar', 'measures_' + network_id + '.csv'))
dt = time() - t0
print(dt)
return None
def add_uniprot_node_data(self, edge_only_network_file, whole_network_file):
# load in full network
with open(edge_only_network_file, 'rb') as file:
full_network = pickle.load(file)
# load in uniprot files one by one
node_data_files = os.listdir(os.path.join('networks', 'full', 'uniprot_node_data'))
for file in node_data_files:
with open(os.path.join('networks', 'full', 'uniprot_node_data', file)) as f:
try:
nodes_data = json.load(f) # some malformed json from api response... try again?
except:
continue
for string_id in nodes_data.keys():
string_id = string_id[:-1]
full_network.nodes(string_id)
full_network.nodes[string_id]['uniprot_data'] = nodes_data[string_id + '\n']
# pickle the resulting file
with open(whole_network_file, 'wb') as handle:
pickle.dump(full_network, handle)