-
Notifications
You must be signed in to change notification settings - Fork 21
/
Neo4jConnection.py
661 lines (557 loc) · 22.4 KB
/
Neo4jConnection.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
''' This module defines the class Neo4jConnection. Neo4jConnection class is designed
to connect to Neo4j database and perform operations on a graphic model object. (e.g.,
retrieve node and update node) The available methods include:
get_xxx_nodes : query all xxx nodes
update_xxx_nodes : update xxx nodes by an array 'nodes', which contain two properties 'node_id'
and 'extended_info_json' for each node
get_xxx_node : query xxx node by ID
xxx is the type of nodes. (e.g., anatomy, phenotype, microRNA, pathway, protein, disease)
'''
__author__ = 'Deqing Qu'
__copyright__ = 'Oregon State University'
__credits__ = ['Deqing Qu', 'Stephen Ramsey']
__license__ = 'MIT'
__version__ = '0.1.0'
__maintainer__ = ''
__email__ = ''
__status__ = 'Prototype'
from neo4j.v1 import GraphDatabase
class Neo4jConnection:
def __init__(self, uri, user, password):
self._driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self._driver.close()
def get_anatomy_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_anatomy_nodes)
def get_phenotype_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_phenotype_nodes)
def get_microRNA_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_microRNA_nodes)
def get_pathway_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_pathway_nodes)
def get_protein_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_protein_nodes)
def get_disease_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_disease_nodes)
def get_chemical_substance_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_chemical_substance_nodes)
def get_bio_process_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_bio_process_nodes)
def get_cellular_component_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_cellular_component_nodes)
def get_molecular_function_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self._get_molecular_function_nodes)
def get_metabolite_nodes(self):
with self._driver.session() as session:
return session.read_transaction(self._get_metabolite_nodes)
def update_anatomy_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_anatomy_nodes, nodes)
def update_phenotype_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_phenotype_nodes, nodes)
def update_microRNA_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_microRNA_nodes, nodes)
def update_pathway_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_pathway_nodes, nodes)
def update_protein_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_protein_nodes, nodes)
def update_disease_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_disease_nodes, nodes)
def update_chemical_substance_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_chemical_substance_nodes, nodes)
def update_bio_process_nodes(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_bio_process_nodes, nodes)
def get_anatomy_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_anatomy_node, id)
def get_phenotype_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_phenotype_node, id)
def get_microRNA_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_microRNA_node, id)
def get_pathway_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_pathway_node, id)
def get_protein_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_protein_node, id)
def get_disease_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_disease_node, id)
def get_chemical_substance_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_chemical_substance_node, id)
def get_bio_process_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_bio_process_node, id)
def get_node(self, id):
with self._driver.session() as session:
return session.write_transaction(self._get_node, id)
def update_anatomy_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_anatomy_nodes_desc, nodes)
def update_phenotype_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_phenotype_nodes_desc, nodes)
def update_microRNA_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_microRNA_nodes_desc, nodes)
def update_pathway_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_pathway_nodes_desc, nodes)
def update_protein_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_protein_nodes_desc, nodes)
def update_disease_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_disease_nodes_desc, nodes)
def update_chemical_substance_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_chemical_substance_nodes_desc, nodes)
def update_bio_process_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_bio_process_nodes_desc, nodes)
def update_cellular_component_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_cellular_component_desc, nodes)
def update_molecular_function_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_molecular_function_desc, nodes)
def update_protein_nodes_name(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_protein_nodes_name, nodes)
def update_metabolite_nodes_desc(self, nodes):
with self._driver.session() as session:
return session.write_transaction(self._update_metabolite_desc, nodes)
def get_node_names(self, type):
with self._driver.session() as session:
return session.write_transaction(self._get_node_names, type)
def create_disease_has_phenotype(self, array):
with self._driver.session() as session:
return session.write_transaction(self.__create_disease_has_phenotype, array)
def remove_duplicate_has_phenotype_relations(self):
with self._driver.session() as session:
return session.write_transaction(self.__remove_duplicate_has_phenotype_relations)
def count_has_phenotype_relation(self, relation):
"""
:param relation: {"d_id": "DOID:xxxx", "p_id": "HP:xxxx"}
:return: count of relations between d_id and p_id
"""
with self._driver.session() as session:
return session.write_transaction(self.__count_has_phenotype_relation, relation)
def remove_duplicated_react_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self.__remove_duplicated_react_nodes)
def count_duplicated_nodes(self):
with self._driver.session() as session:
return session.write_transaction(self.__count_duplicated_nodes)
def get_relationship(self, r_type, s_id, t_id):
with self._driver.session() as session:
return session.write_transaction(self._get_relationship, r_type, s_id, t_id)
@staticmethod
def _get_anatomy_nodes(tx):
result = tx.run("MATCH (n:anatomical_entity) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_phenotype_nodes(tx):
result = tx.run("MATCH (n:phenotypic_feature) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_microRNA_nodes(tx):
result = tx.run("MATCH (n:microRNA) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_pathway_nodes(tx):
result = tx.run("MATCH (n:pathway) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_protein_nodes(tx):
result = tx.run("MATCH (n:protein) RETURN n.id")
return [record["n.id"] for record in result]
@staticmethod
def _get_disease_nodes(tx):
result = tx.run("MATCH (n:disease) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_chemical_substance_nodes(tx):
result = tx.run("MATCH (n:chemical_substance) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_bio_process_nodes(tx):
result = tx.run("MATCH (n:biological_process) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_cellular_component_nodes(tx):
result = tx.run("MATCH (n:cellular_component) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_molecular_function_nodes(tx):
result = tx.run("MATCH (n:molecular_function) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _get_metabolite_nodes(tx):
result = tx.run("MATCH (n:metabolite) RETURN n.rtx_name")
return [record["n.rtx_name"] for record in result]
@staticmethod
def _update_anatomy_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:anatomical_entity{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_phenotype_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:phenotypic_feature{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_microRNA_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:microRNA{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_pathway_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:pathway{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_protein_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:protein{id:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_disease_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:disease{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_chemical_substance_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:chemical_substance{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _update_bio_process_nodes(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.extended_info_json AS extended_info_json
MATCH (n:biological_process{rtx_name:node_id})
SET n.extended_info_json=extended_info_json
""",
nodes=nodes,
)
return result
@staticmethod
def _get_anatomy_node(tx, id):
result = tx.run("MATCH (n:anatomical_entity{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_phenotype_node(tx, id):
result = tx.run("MATCH (n:phenotypic_feature{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_microRNA_node(tx, id):
result = tx.run("MATCH (n:microRNA{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_pathway_node(tx, id):
result = tx.run("MATCH (n:pathway{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_protein_node(tx, id):
result = tx.run("MATCH (n:protein{id:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_disease_node(tx, id):
result = tx.run("MATCH (n:disease{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_chemical_substance_node(tx, id):
result = tx.run("MATCH (n:chemical_substance{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_bio_process_node(tx, id):
result = tx.run("MATCH (n:biological_process{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _get_node(tx, id):
result = tx.run("MATCH (n{rtx_name:'%s'}) RETURN n" % id)
return result.single()
@staticmethod
def _update_anatomy_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:anatomical_entity{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_phenotype_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:phenotypic_feature{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_microRNA_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:microRNA{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_disease_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:disease{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_pathway_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:pathway{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_protein_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:protein{id:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_chemical_substance_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:chemical_substance{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_bio_process_nodes_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:biological_process{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_cellular_component_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:cellular_component{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_molecular_function_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:molecular_function{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _update_protein_nodes_name(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.name AS name
MATCH (n:protein{id:node_id})
SET n.name=name
""",
nodes=nodes,
)
return result
@staticmethod
def _update_metabolite_desc(tx, nodes):
result = tx.run(
"""
UNWIND {nodes} AS row
WITH row.node_id AS node_id, row.desc AS description
MATCH (n:metabolite{rtx_name:node_id})
SET n.description=description
""",
nodes=nodes,
)
return result
@staticmethod
def _get_node_names(tx, type):
result = tx.run("MATCH (n:%s) RETURN n.name" % type)
return [record["n.name"] for record in result]
@staticmethod
def __create_disease_has_phenotype(tx, array):
result = tx.run(
"""
UNWIND {array} AS row
WITH row.d_id AS d_id, row.p_id AS p_id
MATCH (d:disease {rtx_name:d_id}), (p:phenotypic_feature {rtx_name:p_id})
CREATE (d)-[:has_phenotype {
source_node_uuid: d.UUID,
target_node_uuid: p.UUID,
is_defined_by: \'RTX\',
provided_by: \'BioLink\',
predicate: \'has_phenotype\',
seed_node_uuid: d.seed_node_uuid,
relation: \'has_phenotype\'
}]->(p)
""",
array=array
)
return result
@staticmethod
def __remove_duplicate_has_phenotype_relations(tx):
result = tx.run(
"""
MATCH (a)-[r:has_phenotype]->(b)
WITH a, b, TAIL (COLLECT (r)) as rr
WHERE size(rr)>0
FOREACH (r IN rr | DELETE r)
"""
)
return result
@staticmethod
def __count_has_phenotype_relation(tx, relation):
result = tx.run(
"""
MATCH p = (a {rtx_name:$relation.d_id})-[r:has_phenotype]->(b {rtx_name:$relation.p_id})
RETURN count(p)
""",
relation=relation
)
return result.single()['count(p)']
@staticmethod
def __remove_duplicated_react_nodes(tx):
result = tx.run(
"""
MATCH (n), (m)
WHERE n<>m AND n.id=m.id AND split(n.rtx_name, ':')[0] = 'REACT'
DELETE n
"""
)
return result
@staticmethod
def __count_duplicated_nodes(tx):
result = tx.run(
"""
MATCH (n), (m)
WHERE n<>m AND n.id=m.id return count(*)
""",
)
return result.single()['count(*)']
@staticmethod
def _get_relationship(tx, r_type, s_id, t_id):
result = tx.run("MATCH p=()-[r:%s]->() where r.source_node_uuid= '%s' and r.target_node_uuid='%s' RETURN r" %
(r_type, s_id, t_id))
return result.single()