-
Notifications
You must be signed in to change notification settings - Fork 5
/
queries.py
executable file
·333 lines (307 loc) · 12.3 KB
/
queries.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
#!/usr/bin/env python
""" Queries with HMDB and DrugBank data indexed with MongoDB """
import argh
from hmdb.index import DOCTYPE_METABOLITE, DOCTYPE_PROTEIN
from nosqlbiosets.dbutils import DBconnection
from nosqlbiosets.graphutils import *
from nosqlbiosets.qryutils import parseinputquery, Query
from nosqlbiosets.uniprot.query import QueryUniProt
db = "MongoDB" # Elasticsearch support has not been implemented
DATABASE = "biosets" # MongoDB database
class QueryDrugBank(Query):
def autocomplete_drugnames(self, qterm, **kwargs):
"""
Given partial drug names return possible names
:param qterm: partial drug name
:return: list of possible names
"""
qc = {"$or": [
{"name": {
"$regex": "^%s" % qterm, "$options": "i"}},
{"abbreviation": {
"$regex": "^%s" % qterm, "$options": "i"}},
{"products.name": {
"$regex": "^%s" % qterm, "$options": "i"}}
]}
cr = self.query(qc, projection=['name'], **kwargs)
return cr
# Target genes and interacted drugs
def get_target_genes_interacted_drugs(self, qc, limit=1600):
aggqc = [
{"$match": qc},
{'$unwind': "$drug-interactions"},
{'$unwind': "$targets"},
{'$unwind': "$targets.polypeptide"},
{'$group': {
'_id': {
"drug": "$name",
'targetid': '$targets.polypeptide.gene-name',
'target': '$targets.polypeptide.name',
"idrug": "$drug-interactions.name"
}}},
{"$limit": limit}
]
cr = self.aggregate_query(aggqc, allowDiskUse=True)
r = []
for i in cr:
assert 'targetid' in i['_id']
gid = i['_id']
row = (gid['drug'], gid['targetid'], gid['target'],
gid['idrug'])
r.append(row)
return r
def kegg_target_id_to_drugbank_entity_id(self, keggtid, etype='targets',
mdbdb=DATABASE,
uniprotcollection='uniprot'):
"""
Get drugbank target ids for given KEGG target ids
The two databases are connected by first making a UniProt query
:param keggtid: KEGG target id
:param etype: Drugbank entity type, 'targets' or 'enzymes'
:param mdbdb: MongoDB database name
:param uniprotcollection: UniProt collection to search
:return: Drugbank target id
"""
qryuniprot = QueryUniProt("MongoDB", mdbdb, uniprotcollection)
qc = {"dbReference.id": keggtid}
key = 'name'
uniprotid = qryuniprot.dbc.mdbi[uniprotcollection].distinct(key,
filter=qc)
assert len(uniprotid) == 1
qc = {
etype+".polypeptide.external-identifiers.identifier": uniprotid[0]}
aggq = [
{"$match": qc},
{'$unwind': "$"+etype},
{'$unwind': "$"+etype+".polypeptide"},
{"$match": qc},
{"$limit": 1},
{"$project": {etype+".id": 1}}
]
r = list(self.aggregate_query(aggq))
assert 1 == len(r)
return uniprotid[0], r[0][etype]["id"]
def kegg_drug_id_to_drugbank_id(self, keggdid):
"""
Given KEGG drug id return Drugbank drug id
:param keggdid: KEGG drug id
:return: Drugbank drug id
"""
project = {"external-identifiers": 1}
qc = {"external-identifiers.identifier": keggdid}
r = list(self.query(qc, projection=project))
assert 1 == len(r)
return r[0]["_id"]
def get_connections(self, qc, connections):
project = {"name": 1, connections + ".name": 1}
r = self.query(qc, projection=project)
interactions = list()
for d in r:
name = d['name']
if connections in d:
for t in d[connections]:
# TO_DO: return more information
interactions.append((name, t['name']))
return interactions
# Gets and saves networks from subsets of DrugBank records
# filtered by query clause, qc. Graph file format is selected
# based on file extension used, detailed in the readme.md file
def get_connections_graph(self, qc, connections, outfile=None):
interactions = self.get_connections(qc, connections)
graph = nx.MultiDiGraph(name=connections, query=json.dumps(qc))
colors = {
"drug": 'yellowgreen',
"targets": 'orchid',
"enzymes": 'sienna',
"transporters": 'coral',
"carriers": 'blue'
}
_type = 'drug' if connections == 'drug-interactions' else connections
for u, v in interactions:
graph.add_node(u, type='drug', viz_color='green')
graph.add_node(v,
type=_type,
viz_color=colors[_type])
graph.add_edge(u, v)
if outfile is not None:
save_graph(graph, outfile)
return graph
def get_allgraphs(self, qc):
connections = ["targets", "enzymes", "transporters", "carriers"]
graphs = []
for connection in connections:
graphs.append(self.get_connections_graph(qc, connection))
r = nx.compose_all(graphs)
return r
class QueryHMDB:
def __init__(self, index=DATABASE, **kwargs):
self.index = index
self.dbc = DBconnection(db, self.index, **kwargs)
self.mdb = self.dbc.mdbi
def metabolites_protein_functions(self, mq):
"""
Functions of associated proteins for selected set of Metabolites
"""
agpl = [
{'$match': mq},
{'$unwind':
{
'path': '$protein_associations.protein'
}},
{'$project': {
"accession": 1,
"protein_associations.protein": 1}},
{'$lookup': {
'from': DOCTYPE_PROTEIN,
'let': {'a': "$protein_associations.protein.protein_accession"},
'as': 'proteins',
'pipeline': [
{'$match': {
"$expr": {"$eq": ["$accession", "$$a"]}
}},
{'$project': {
"general_function": 1}}
]
}},
{'$unwind': "$proteins"},
{'$group': {
"_id": "$proteins.general_function",
"count": {'$sum': 1}}},
{"$sort": {"count": -1}}
]
r = self.mdb[DOCTYPE_METABOLITE].aggregate(agpl)
return r
def getconnectedmetabolites(self, qc, max_associations=-1):
# Return pairs of connected metabolites
# together with associated proteins and their types
graphlookup = True
agpl = [
{'$match': qc},
{"$unwind": {
"path": "$protein_associations.protein",
"preserveNullAndEmptyArrays": False
}},
{'$graphLookup': {
'from': DOCTYPE_PROTEIN,
'startWith': '$protein_associations.protein.protein_accession',
'connectFromField':
'metabolite_associations.metabolite.accession',
'connectToField':
'accession',
'maxDepth': 0,
'depthField': "depth",
'as': 'associated_proteins',
"restrictSearchWithMatch": {
"metabolite_associations.metabolite.%d" % max_associations:
{"$exists": False}
} if max_associations != -1 else {}
}} if graphlookup else
{'$lookup': {
'from': DOCTYPE_PROTEIN,
'foreignField':
'accession',
'localField':
'protein_associations.protein.protein_accession',
'as': 'associated_proteins'
}},
{'$project': {
"name": 1,
"protein_associations.protein.gene_name": 1,
"protein_associations.protein.protein_type": 1,
"associated_proteins.metabolite_associations.metabolite.name": 1
}},
{"$unwind": {
"path": "$associated_proteins",
"preserveNullAndEmptyArrays": False
}},
{'$match': {
'associated_proteins.metabolite_associations.'
'metabolite.%d' % max_associations: {
"$exists": False}} if max_associations != -1 else {}
},
{"$unwind": {
"path": "$associated_proteins."
"metabolite_associations.metabolite",
"preserveNullAndEmptyArrays": False
}},
{"$redact": {
"$cond": [
{"$ne": [
"$name",
"$associated_proteins."
"metabolite_associations.metabolite.name"]},
"$$KEEP",
"$$PRUNE"]
}},
{'$group': {
'_id': {
"m1": "$name",
"gene": "$protein_associations.protein.gene_name",
"type": "$protein_associations.protein.protein_type",
"m2": "$associated_proteins."
"metabolite_associations.metabolite.name"
}
}},
{"$replaceRoot": {"newRoot": "$_id"}}
]
r = self.mdb[DOCTYPE_METABOLITE].aggregate(agpl, allowDiskUse=True)
return r
def get_connections_graph(self, connections, query=None, outfile=None):
graph = nx.DiGraph(query=query)
for i in connections:
u = i['m1']
v = i['m2']
gene = i['gene']
proteintype = i['type']
graph.add_node(u, type='metabolite', viz_color='green')
graph.add_node(v, type='metabolite', viz_color='honeydew')
graph.add_node(gene, type=proteintype, viz_color='lavender')
graph.add_edge(u, gene)
graph.add_edge(gene, v)
if outfile is not None:
save_graph(graph, outfile)
return graph
def savegraph(query, graphfile, connections='targets'):
"""Save DrugBank interactions as graph files
:param query: MongoDB query clause to select subsets of DrugBank entries
ex: \'{"carriers.name": "Serum albumin"}\'
:param graphfile: File name for saving the output graph
' in GraphML, GML, Cytoscape.js or d3js formats,'
' see readme.md for details'
:param connections: "targets", "enzymes", "transporters" or
"carriers
"""
qry = QueryDrugBank(db, DATABASE, 'drugbank')
qc = parseinputquery(query)
g = qry.get_connections_graph(qc, connections, graphfile)
print(nx.info(g))
def cyview(query, dataset='HMDB', connections='targets', name='',
database=DATABASE):
""" See HMDB/DrugBank graphs with Cytoscape runing on your local machine
`
:param query: Query to select HMDB or DrugBank entries
:param dataset: HMDB or DrugBank, not case sensitive
:param connections: Connection type for DrugBank entries
:param name: Name of the graph on Cytoscape, query is used as default value
:param database: Name of the MongoDB database to connect
"""
from py2cytoscape.data.cyrest_client import CyRestClient
from py2cytoscape.data.style import Style
qc = parseinputquery(query)
print(qc) # todo: tests with HMDB
if dataset.upper() == 'HMDB':
qry = QueryHMDB(index=database)
pairs = qry.getconnectedmetabolites(qc)
mn = qry.get_connections_graph(pairs, query)
else: # assume DrugBank
qry = QueryDrugBank(db, database, 'drugbank')
mn = qry.get_connections_graph(qc, connections)
crcl = CyRestClient()
mn.name = json.dumps(qc) if name == '' else name
cyn = crcl.network.create_from_networkx(mn)
crcl.layout.apply('kamada-kawai', network=cyn)
crcl.style.apply(Style('default'), network=cyn)
if __name__ == '__main__':
argh.dispatch_commands([
savegraph, cyview
])