-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
193 lines (154 loc) · 6.71 KB
/
stats.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
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 8 14:16:25 2012
@author: proto
"""
import bioservices
import pyparsing as pyp
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
import concurrent.futures
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
port = 9200
server = SimpleXMLRPCServer(("10.253.98.102", port),requestHandler=RequestHandler)
#server = SimpleXMLRPCServer(("127.0.0.1", port), requestHandler=RequestHandler)
server.register_introspection_functions()
#def updateDictionary(tmpArray,key,newInfo):
def resolveAnnotations(annotations):
tmpArray = {}
futures = []
with concurrent.futures.ProcessPoolExecutor(max_workers=8) as executor:
for annotation in annotations:
futures.append(executor.submit(resolveAnnotation, annotation))
for future in concurrent.futures.as_completed(futures):
resolvedAnnotation = future.result()
for element in resolvedAnnotation:
if resolvedAnnotation[element] != annotation:
if element not in tmpArray:
tmpArray[element] = []
if isinstance(resolvedAnnotation[element],list):
tmpArray[element].extend(resolvedAnnotation[element])
else:
tmpArray[element].append(resolvedAnnotation[element])
finalArray = []
#transforming to an [key [result],....] structure since the ouput cant be dictionaries
for element in tmpArray:
finalArray.append([element,tmpArray[element]])
print finalArray
return finalArray
def removeTags(taggedInformation):
taggedInformation2 = taggedInformation.decode('ascii','ignore')
goGrammar = pyp.Suppress('<' + pyp.Word(pyp.alphanums) + '>') + pyp.Word(pyp.alphanums + pyp.alphas8bit + ' ._-') + pyp.Suppress('</' + pyp.Word(pyp.alphanums) + '>')
tmp = goGrammar.parseString(str(taggedInformation2))
return tmp[0]
def resolveAnnotation(annotation):
if not hasattr(resolveAnnotation, 'db'):
resolveAnnotation.db = {}
resolveAnnotation.ch = bioservices.ChEBI(verbose=False)
resolveAnnotation.bio = bioservices.BioModels(verbose=False)
resolveAnnotation.uni = bioservices.UniProt(verbose=False)
resolveAnnotation.k = bioservices.kegg.KeggParser(verbose=False)
resolveAnnotation.s = bioservices.Service('name')
resolveAnnotation.qg = bioservices.QuickGO(verbose=False)
resolveAnnotation.db['http://identifiers.org/uniprot/P62988'] = 'http://identifiers.org/uniprot/P62988'
resolveAnnotation.db['http://identifiers.org/uniprot/P06842'] = 'http://identifiers.org/uniprot/P06842'
resolveAnnotation.db['http://identifiers.org/uniprot/P07006'] = 'http://identifiers.org/uniprot/P06842'
if annotation in resolveAnnotation.db:
return resolveAnnotation.db[annotation]
tAnnotation = annotation.replace('%3A',':')
tAnnotation = annotation.split('/')[-1]
#tAnnotation = re.search(':([^:]+:[^:]+$)',tAnnotation).group(1)
tmpArray = {}
if 'obo.go' in annotation:
res = resolveAnnotation.qg.Term(tAnnotation)
tmp = res.findAll('name')
finalArray = []
goGrammar = pyp.Suppress(pyp.Literal('<name>')) + pyp.Word(pyp.alphanums + ' ') + pyp.Suppress(pyp.Literal('</name>'))
for x in tmp:
finalArray.append(str(goGrammar.parseString(str(x))[0]))
tmp = finalArray
resolveAnnotation.db[annotation] = tmp
tmpArray['tags'] = tmp
elif 'kegg' in annotation:
data = resolveAnnotation.k.get(tAnnotation)
dict_data = resolveAnnotation.k.parse(data)
resolveAnnotation.db[annotation] = dict_data['name']
tmpArray['tags'] = resolveAnnotation.db[annotation]
elif 'uniprot' in annotation:
identifier = annotation.split('/')[-1]
result = resolveAnnotation.uni.quick_search(identifier)
resolveAnnotation.db[annotation] = result[result.keys()[0]]['Protein names']
tmpArray['tags'] = resolveAnnotation.db[annotation]
elif 'chebi' in annotation:
tmp = annotation.split('/')[-1]
entry = resolveAnnotation.ch.getLiteEntity(tmp)
for element in entry:
resolveAnnotation.db[annotation] = element['chebiAsciiName']
tmpArray['tags'] = resolveAnnotation.db[annotation]
elif 'cco' in annotation or 'pirsf' in annotation or 'pubchem' in annotation or 'omim' in annotation:
tmpArray['ignore'] = annotation
elif 'biomodels.db' in annotation:
tmp = annotation.split('/')[-1]
tmpArray = {}
try:
request = resolveAnnotation.bio.getSimpleModelsByIds(tmp)
except ValueError:
tmpArray['ignore'] = annotation
return tmpArray
entry = resolveAnnotation.s.easyXML(request)
tmpArray['modelName'] = []
for tag in entry['modelname']:
tmpArray['modelName'].append(removeTags(str(tag)))
tmpArray['author'] = []
for tag in entry['author']:
tmpArray['author'].append(removeTags(str(tag)))
#elif 'taxonomy' in annotation:
#uniprot stuff for taxonomy
# pass
'''
url = 'http://www.uniprot.org/taxonomy/'
params = {
'from':'ACC',
'to':'P_REFSEQ_AC',
'format':'tab',
'query':'P13368 P20806 Q9UM73 P97793 Q17192'
}
data = urllib.urlencode(params)
request = urllib2.Request(url, data)
contact = "" # Please set your email address here to help us debug in case of problems.
request.add_header('User-Agent', 'Python contact')
response = urllib2.urlopen(request)
page = response.read(200000)
'''
else:
print 'ERRROERROROERRRO',annotation
#assert(False)
tmpArray['ignore'] = annotation
#finally:
return tmpArray
class AnnotationServer:
def __init__(self):
pass
def getSpeciesConventions(self,annotation):
print annotation
#tmp = resolveAnnotation(annotation)
#print '----',tmp
import sys
sys.stdout.flush()
return annotation
def getArray(self,annotation):
print annotation
import sys
sys.stdout.flush()
return ['ab','cd','ef']
def is_even(n):
return n%2 == 0
#server.register_function(is_even, "is_even")
#resolveAnnotations(tmpD)
print "Listening on port {0}...".format(port)
server.register_function(resolveAnnotations,'resolveAnnotations')
# Run the server's main loop
server.serve_forever()