-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseKegg2.py
187 lines (160 loc) · 4.74 KB
/
parseKegg2.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
#OM
import pickle
def save_obj(obj, name ):
with open('obj/'+ name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name ):
with open('obj/' + name + '.pkl', 'rb') as f:
return pickle.load(f)
def removeJunk(string):
string = string.replace('\n','')
string = string.strip()
return string
strEntry="ENTRY "
strName="NAME "
strPathway="PATHWAY "
strGene="GENE "
strDrug="DRUG "
disease_file="download/Kegg/disease"
f = open(disease_file)
dict_disease={}
skip=0
line = f.readline()
count =0
while line:
# print line
if ( strEntry in line) :
entryLineList= line.split(strEntry)
entryName = entryLineList[1].split(' ')[0]
if(entryName not in dict_disease):
dict_disease[entryName] = {}
dict_disease[entryName]['PATHWAY'] =set()
dict_disease[entryName]['GENE'] =set()
dict_disease[entryName]['DRUG'] =set()
print (entryName)
elif (strPathway in line ):
pathwaySplit = line.split(strPathway)[1]
pathwayId=pathwaySplit.split(' ')[0]
pathwayId=pathwayId.split('(')[0]
line = f.readline()
print ('\t',pathwayId)
dict_disease[entryName]['PATHWAY'].add(removeJunk(pathwayId))
while(' hsa' in line):
pathwaySplit = line.split(' ')[1]
pathwayId=pathwaySplit.split(' ')[0]
pathwayId=pathwayId.split('(')[0]
line = f.readline()
count=count+1
dict_disease[entryName]['PATHWAY'].add(removeJunk(pathwayId) )
print ('\t', ':',pathwayId)
continue
elif (strGene in line):
geneSplit = line.split(strGene)[1]
geneId=geneSplit#geneSplit.split('(')[0]
dict_disease[entryName]['GENE'].add(removeJunk(geneId) )
print ('\t',geneId)
line = f.readline()
while(' ' in line):
geneSplit = line.split(' ')[1]
geneId=geneSplit#geneSplit.split('(')[0]
line = f.readline()
count=count+1
print ('\t',':',geneId)
dict_disease[entryName]['GENE'].add(removeJunk(geneId) )
continue
elif (strDrug in line):
drugValue = line.split(strDrug)[1]
drugId=drugValue.split(' ')[0]
dict_disease[entryName]['DRUG'].add(removeJunk(drugId) )
print ('\t',drugId)
line = f.readline()
while(' ' in line):
drugValue = line.split(' ')[1]
drugId=drugValue.split(' ')[0]
line = f.readline()
count=count+1
print ('\t',':',drugId)
dict_disease[entryName]['DRUG'].add(removeJunk(drugId) )
continue
line = f.readline()
# print (line)
f.close()
drug_file="download/Kegg/drug"
f = open(drug_file)
dict_drugs={}
skip=0
line = f.readline()
count =0
strEntry="ENTRY "
strName="NAME "
strPathway=" PATHWAY "
#strGene="GENE "
strTarget="TARGET "
strDbLink="DBLINKS "
tempEntry=""
while line :
# print line
if ( strEntry in line) :
entryLineList= line.split(strEntry)
entryName = entryLineList[1].split(' ')[0]
print (entryName)
tempEntry=entryName
if(entryName not in dict_drugs):
dict_drugs[entryName] = {}
dict_drugs[entryName]['PATHWAY'] =set()
elif (strPathway in line ):
pathwaySplit = line.split(strPathway)[1]
pathwayId=pathwaySplit.split(' ')[0]
pathwayId=pathwayId.split('(')[0]
line = f.readline()
print ('\t',pathwayId)
dict_drugs[entryName]['PATHWAY'].add(removeJunk(pathwayId))
while(' hsa' in line):
pathwaySplit = line.split(' ')[1]
pathwayId=pathwaySplit.split(' ')[0]
pathwayId=pathwayId.split('(')[0]
line = f.readline()
count=count+1
dict_drugs[entryName]['PATHWAY'].add(removeJunk(pathwayId))
print ('\t', ':',pathwayId)
continue
elif (strTarget in line):
geneSplit = line.split(strTarget)[1]
geneId=geneSplit#geneSplit.split('(')[0]
print ('\t',geneId)
line = f.readline()
while(' ' in line):
geneSplit = line.split(' ')[1]
geneId=geneSplit#geneSplit.split('(')[0]
line = f.readline()
count=count+1
print ('\t',':',geneId)
continue
elif (strDbLink in line):
# print ("ya")
dbLinks = line.split(strDbLink)[1]
dbLinks=dbLinks#geneSplit.split('(')[0d]
if('DrugBank' in dbLinks):
dict_drugs[entryName]['DBID'] =removeJunk(dbLinks)
print ('\t',':',dbLinks)
line = f.readline()
while(' ' in line):
dbLinks = line.split(' ')[1]
dbLinks=dbLinks#geneSplit.split('(')[0]
line = f.readline()
count=count+1
if('DrugBank' in dbLinks):
dict_drugs[entryName]['DBID'] =removeJunk(dbLinks)
print ('\t',':',dbLinks)
continue
line = f.readline()
# print (line)
save_obj(dict_drugs,'kegg_dict_drugs')
save_obj(dict_disease,'kegg_dict_disease')
for dr in dict_disease:
print (dr,dict_disease[dr])
print (len(dict_disease))
print (len(dict_drugs))
#print (dict_drugs)
#print (dict_disease)
f.close()