Repository navigation
Expand file tree
/
Copy pathnetworkConstructor.py
More file actions
360 lines (325 loc) · 12.2 KB
/
Copy pathnetworkConstructor.py
File metadata and controls
360 lines (325 loc) · 12.2 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
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
#import external code
import operator
import networkx as nx
import re
import urllib2
import csv
import itertools as it
import sys
from bs4 import BeautifulSoup
from random import randint
# import our code
import simulation as sim
import utils as utils
#definitions from BioPAX level 3 reference manual (http://www.biopax.org/mediawiki/index.php?title=Specification)
#these biopax classes are iteracted over in the biopax methods
edge_classes = ['Interaction', 'GeneticInteraction', 'MolecularInteraction', 'TemplateReaction', 'Control', 'Catalysis', 'TemplateReactionRegulation', 'Modulation', 'Conversion', 'ComplexAssembly', 'BiochemicalReaction', 'Degradation', 'Transport', 'TransportWithBiochemicalReaction']
node_classes = ['PhysicalEntity', 'Complex', 'Dna', 'DnaRegion', 'Protein', 'Rna', 'RnaRegion', 'SmallMolecule', 'Gene']
graph_classes = ['Pathway']
#Upload KEGG codes modified for human pathways
def uploadKEGGcodes_hsa(codelist, graph, hsaDict, KEGGdict):
#queries the KEGG for the pathways with the given codes then uploads to graph. Need to provide the KEGGdict so that we can name the nodes with gene names rather than KO numbers
for code in codelist:
try:
url=urllib2.urlopen('http://rest.kegg.jp/get/'+code+'/kgml')
except:
print('could not read code: ' + code )
continue
text=url.readlines()
readKEGGhsa(text, graph, hsaDict, KEGGdict)
#print(code)
#print(graph.nodes())
def readKEGGhsa(lines, graph, hsaDict, KEGGdict):
#read all lines into a bs4 object using libXML parser
soup = BeautifulSoup(''.join(lines), 'xml')
groups = {} # store group IDs and list of sub-ids
id_to_name = {} # map id numbers to names
for entry in soup.find_all('entry'):
entry_split= entry['name'].split(':')
if len(entry_split)>2:
if entry_split[0]=='hsa' or entry_split[0]=='ko':
if entry_split[0]=='hsa':
useDict=hsaDict
else:
useDict=KEGGdict
nameList=[]
entry_name=''
namer=entry_split.pop(0)
namer=entry_split.pop(0)
namer=namer.split()[0]
entry_name=entry_name+useDict[namer] if namer in useDict.keys() else entry_name+namer
for i in range(len(entry_split)):
nameList.append(entry_split[i].split()[0])
for namer in nameList:
entry_name=entry_name+'-'+useDict[namer] if namer in useDict.keys() else entry_name+'-'+namer
entry_type = entry['type']
else:
entry_name=entry['name']
entry_type = entry['type']
else:
if entry_split[0]=='hsa':
entry_name=entry_split[1]
entry_type = entry['type']
entry_name = hsaDict[entry_name] if entry_name in hsaDict.keys() else entry_name
elif entry_split[0]=='ko':
entry_name=entry_split[1]
entry_type = entry['type']
entry_name = KEGGdict[entry_name] if entry_name in KEGGdict.keys() else entry_name
elif entry_split[0]=='path':
entry_name=entry['name']
entry_type='path'
else:
entry_name=entry['name']
entry_type = entry['type']
entry_id = entry['id']
id_to_name[entry_id] = entry_name
if entry_type == 'group':
group_ids = []
for component in entry.find_all('component'):
group_ids.append(component['id'])
groups[entry_id] = group_ids
else:
graph.add_node(entry_name, {'name': entry_name, 'type': entry_type})
for relation in soup.find_all('relation'):
(color, signal) = ('black', 'a')
relation_entry1 = relation['entry1']
relation_entry2 = relation['entry2']
relation_type = relation['type']
subtypes = []
for subtype in relation.find_all('subtype'):
subtypes.append(subtype['name'])
if ('activation' in subtypes) or ('expression' in subtypes):
color='green'
signal='a'
elif 'inhibition' in subtypes:
color='red'
signal='i'
elif ('binding/association' in subtypes) or('compound' in subtypes):
color='purple'
signal='a'
elif 'phosphorylation' in subtypes:
color='orange'
signal='a'
elif 'dephosphorylation' in subtypes:
color='pink'
signal='i'
elif 'indirect effect' in subtypes:
color='cyan'
signal='a'
elif 'dissociation' in subtypes:
color='yellow'
signal='i'
elif 'ubiquitination' in subtypes:
color='cyan'
signal='i'
else:
print('color not detected. Signal assigned to activation arbitrarily')
print(subtypes)
signal='a'
#given: a node ID that may be a group
#returns: a list that contains all group IDs deconvoluted
def expand_groups(node_id):
node_list = []
if node_id in groups.keys():
for component_id in groups[node_id]:
node_list.extend(expand_groups(component_id))
else:
node_list.extend([node_id])
return node_list
entry1_list = expand_groups(relation_entry1)
entry2_list = expand_groups(relation_entry2)
for (entry1, entry2) in it.product(entry1_list, entry2_list):
node1 = id_to_name[entry1]
node2 = id_to_name[entry2]
graph.add_edge(node1,node2, color=color, subtype='/'.join(subtypes), type=relation_type, signal=signal)
for node in graph.nodes():
if graph.degree(node)==0:
graph.remove_node(node)
def parseKEGGdict(filename):
#makes a dictionary to convert ko numbers from KEGG into real gene names
#this is all file formatting. it reads a line, parses the string into the gene name and ko # then adds to a dict that identifies the two.
dict={}
inputfile = open(filename, 'r')
lines = inputfile.readlines()
for line in lines:
if line[0]=='D':
kline=line.split(' ')
kstring=kline[1]
kline=kstring.split(' ')
k=kline[0]
nameline=line.replace('D ', 'D')
nameline=nameline.split(' ')
namestring=nameline[1]
nameline=namestring.split(';')
name=nameline[0]
dict[k]=name
return dict
def parseKEGGdicthsa(filename, aliasDict, dict1):
#reads KEGG dictionary of identifiers between orthology numbers and actual protein names and saves it to a python dictionary
#extends above method to also keep track of gene names that are identical so we can recover those from input data as well
#again, read in lines from the file, save ko number and gene name, identify them in the dictionary.
inputfile = open(filename, 'r')
lines = inputfile.readlines()
for line in lines:
if line[0]=='D':
kline=line.split(' ')
kstring=kline[1]
kline=kstring.split(' ')
k=kline[0]
nameline=kline[1]
nameline=nameline.split(';')
name=nameline[0]
if ',' in name:
nameline=name.split(',')
name=nameline[0]
for entry in range(1,len(nameline)):
aliasDict[nameline[entry].strip()]=name
dict1[k]=name
return dict1
def parseKEGGdict(filename, aliasDict, dict1):
#reads KEGG dictionary of identifiers between orthology numbers and actual protein names and saves it to a python dictionary
#extends above method to also keep track of gene names that are identical so we can recover those from input data as well
#again, read in lines from the file, save ko number and gene name, identify them in the dictionary.
inputfile = open(filename, 'r')
lines = inputfile.readlines()
for line in lines:
if line[0]=='D':
kline=line.split(' ')
kstring=kline[1]
kline=kstring.split(' ')
k=kline[0]
nameline=line.replace('D ', 'D')
nameline=nameline.split(' ')
namestring=nameline[1]
nameline=namestring.split(';')
name=nameline[0]
if ',' in name:
nameline=name.split(',')
name=nameline[0]
for entry in range(1,len(nameline)):
aliasDict[nameline[entry].strip()]=name
dict1[k]=name
return dict1
def readKEGG(lines, graph, KEGGdict):
#read all lines into a bs4 object using libXML parser
soup = BeautifulSoup(''.join(lines), 'xml')
groups = {} # store group IDs and list of sub-ids
id_to_name = {} # map id numbers to names
for entry in soup.find_all('entry'):
entry_name = entry['name'].split(':')[1] if ':' in entry['name'] else entry['name']
entry_name = KEGGdict[entry_name] if entry_name in KEGGdict.keys() else entry_name
entry_type = entry['type']
entry_id = entry['id']
id_to_name[entry_id] = entry_name
if entry_type == 'group':
group_ids = []
for component in entry.find_all('component'):
group_ids.append(component['id'])
groups[entry_id] = group_ids
else:
graph.add_node(entry_name, {'name': entry_name, 'type': entry_type})
for relation in soup.find_all('relation'):
(color, signal) = ('black', 'a')
relation_entry1 = relation['entry1']
relation_entry2 = relation['entry2']
relation_type = relation['type']
subtypes = []
for subtype in relation.find_all('subtype'):
subtypes.append(subtype['name'])
if ('activation' in subtypes) or ('expression' in subtypes):
color='green'
signal='a'
elif 'inhibition' in subtypes:
color='red'
signal='i'
elif ('binding/association' in subtypes) or('compound' in subtypes):
color='purple'
signal='a'
elif 'phosphorylation' in subtypes:
color='orange'
signal='a'
elif 'dephosphorylation' in subtypes:
color='pink'
signal='i'
elif 'indirect effect' in subtypes:
color='cyan'
signal='a'
elif 'dissociation' in subtypes:
color='yellow'
signal='i'
elif 'ubiquitination' in subtypes:
color='cyan'
signal='i'
else:
print('color not detected. Signal assigned to activation arbitrarily')
print(subtypes)
signal='a'
#given: a node ID that may be a group
#returns: a list that contains all group IDs deconvoluted
def expand_groups(node_id):
node_list = []
if node_id in groups.keys():
for component_id in groups[node_id]:
node_list.extend(expand_groups(component_id))
else:
node_list.extend([node_id])
return node_list
entry1_list = expand_groups(relation_entry1)
entry2_list = expand_groups(relation_entry2)
for (entry1, entry2) in it.product(entry1_list, entry2_list):
node1 = id_to_name[entry1]
node2 = id_to_name[entry2]
graph.add_edge(node1,node2, color=color, subtype='/'.join(subtypes), type=relation_type, signal=signal)
def uploadKEGGfiles(filelist, graph, foldername, KEGGdict):
#upload KEGG files from a particular folder.
#just provide the folder, file names as a list, and the graph you want to put things in.
#iteratively calls the readKEGG method
for file in filelist:
inputfile = open(foldername+'/'+file, 'r')
lines = inputfile.readlines()
readKEGG(lines, graph, KEGGdict)
def uploadKEGGfolder(foldername, graph, KEGGdict):
#uploads an entire folder of KEGG files given by foldername to the netx graph
#just picks up names from the folder and passes them to the uploadKEGGfiles method
#be sure not to pass in a folder that has files other than the KEGG files in it
filelist= [ f for f in listdir(foldername+'/') if isfile(join(foldername,join('/',f))) ]
uploadKEGGfiles(filelist, graph, foldername, KEGGdict)
def uploadKEGGcodes(codelist, graph, KEGGdict):
#queries the KEGG for the pathways with the given codes then uploads to graph. Need to provide the KEGGdict so that we can name the nodes with gene names rather than KO numbers
for code in codelist:
try:
url=urllib2.urlopen('http://rest.kegg.jp/get/'+code+'/kgml')
except:
print('could not read code: ' + code )
continue
text=url.readlines()
readKEGG(text, graph, KEGGdict)
# creates graphs as gpickles for experiments using IFNG networks
def ifngStimTestSetup():
aliasDict={}
dict1={}
parseKEGGdicthsa('inputData/hsa00001.keg',aliasDict,dict1)
dict2={}
parseKEGGdict('inputData/ko00001.keg',aliasDict,dict2)
# read in list of codes then load them into network
inputfile = open('inputData/ID_filtered_IFNGpathways.txt', 'r')
lines = inputfile.readlines()
# iterate through and create gpickle of graph for all IFNG networks
for code in lines:
graph=nx.DiGraph()
coder=str('ko'+code[:-1])
uploadKEGGcodes([coder], graph, dict2)
coder=str('hsa'+code[:-1])
uploadKEGGcodes_hsa([coder], graph,dict1, dict2)
# if the code has an interesting logic rule to find, run the algorithm...
print(coder)
checker=False
for x in graph.in_degree():
if x>1:
checker=True
if(checker):
nx.write_graphml(graph,coder+'.graphml')
nx.write_gpickle(graph,coder+'.gpickle')
print(coder)
# if __name__ == '__main__':
# ifngStimTestSetup()