Commit a1ab67e6 authored by Davide Lagoa's avatar Davide Lagoa
Browse files

python neo4j driver updated to match neo4j v4

parent 77b4c658
from transyt_request import get_reaction_data
import FilesUtilities
CONFIGURATIONS = FilesUtilities.read_conf_file('/configs/docker_config.conf')
def result_parser(id):
url = CONFIGURATIONS["database"]
user = CONFIGURATIONS["dbusername"]
password = CONFIGURATIONS["dbpassword"]
databases, tc_systems, reactionID, metaID, reaction, metabolites = get_reaction_data(id, url, user, password)
if not metabolites:
return None
tc_systems2 = {}
for tc in tc_systems:
tc_elements = tc.split('@')
if tc_elements[0] in tc_systems2:
tc_systems2[tc_elements[0]].append(tc_elements[1])
else:
tc_systems2[tc_elements[0]] = [tc_elements[1]]
res_dict = {'databases': databases,
'tc_systems': tc_systems2,
'reactionID': reactionID,
'metaID': metaID,
'reaction': reaction,
'metabolites': metabolites}
return res_dict
print(result_parser("TR0000067"))
print()
print()
print()
print("*************This script should only be used to check if the docker is able to comunicate with the database!****************")
print()
print()
print()
......@@ -9,7 +9,7 @@ import logging
import re
import time
import requests
from transyt_request import database_retriever_by_id
from transyt_request import get_reaction_data
CONFIGURATIONS = FilesUtilities.read_conf_file('/configs/docker_config.conf')
......@@ -385,7 +385,7 @@ def result_parser(id):
user = CONFIGURATIONS["dbusername"]
password = CONFIGURATIONS["dbpassword"]
databases, tc_systems, reactionID, metaID, reaction, metabolites = database_retriever_by_id(id, url, user, password)
databases, tc_systems, reactionID, metaID, reaction, metabolites = get_reaction_data(id, url, user, password)
if not metabolites:
return None
......
from neo4j.v1 import GraphDatabase
class neo4jPythonDriver:
###### CONSTRUCTOR ######
def __init__(self, uri, user, password):
self.__uri = uri
self.__user = user
self.__password = password
self.__driver = self.__initDriver()
def __initDriver(self):
return GraphDatabase.driver(self.__getUri(), auth=(self.__getUser(), self.__getPassword()))
def initSession(self):
return self.__driver.session()
def __getUri(self):
return self.__uri
def __getUser(self):
return self.__user
def __getPassword(self):
return self.__password
def __getDriver(self):
return self.__driver
def __setUri(self, value):
self.__uri = value
def __setUser(self, value):
self.__user = value
def __setPassword(self, value):
self.__password = value
def __setDriver(self, value):
self.__driver = value
from neo4jPythonDriver import neo4jPythonDriver
from neo4j import GraphDatabase
def database_retriever_by_id(id, url, user, password):
def get_reaction_data(id, url, user, password):
driver = neo4jPythonDriver(url, user, password)
session = driver.initSession()
driver = GraphDatabase.driver(url, auth=(user, password))
query = "MATCH (r:Reaction)<-[:has_reaction]-(tc:TC_Number)<-[:has_tc]-(ua:Uniprot_Accession) WHERE r.ReactionID = \'" + id + "\' " \
"MATCH (r)-[rm]-(y) WHERE EXISTS((y)-[:has_name|:has_molecular_formula]-()) MATCH (y)-[]->(z) RETURN tc.TC_Number, ua, r, rm, y, z"
res_dict = None
result = session.run(query)
session.close()
with driver.session() as session:
databases, tc_systems, reactionID, metaID, reaction, metabolites = session.read_transaction(database_retriever_by_id, id)
if metabolites:
tc_systems2 = {}
for tc in tc_systems:
tc_elements = tc.split('@')
if tc_elements[0] in tc_systems2:
tc_systems2[tc_elements[0]].append(tc_elements[1])
else:
tc_systems2[tc_elements[0]] = [tc_elements[1]]
res_dict = {'databases': databases,
'tc_systems': tc_systems2,
'reactionID': reactionID,
'metaID': metaID,
'reaction': reaction,
'metabolites': metabolites}
driver.close()
return res_dict
def database_retriever_by_id(tx, id):
records = tx.run(
"MATCH (r:Reaction)<-[:has_reaction]-(tc:TC_Number)<-[:has_tc]-(ua:Uniprot_Accession) "
"WHERE r.ReactionID = $name MATCH (r)-[rm]-(y) "
"WHERE EXISTS((y)-[:has_name|:has_molecular_formula]-()) "
"MATCH (y)-[]->(z) "
"RETURN tc.TC_Number, ua, r, rm, y, z",
name=id)
LABELS = ['MetaCyc', 'KEGG', 'ModelSEED', 'BiGG']
......@@ -25,9 +56,7 @@ def database_retriever_by_id(id, url, user, password):
metaID = None
reaction = None
output = {}
for r in result:
for r in records:
entry = r[0] + '@' + r[1]['Accession_Number']
if entry not in tc_systems:
......@@ -66,7 +95,6 @@ def database_retriever_by_id(id, url, user, password):
metaboliteId = r[4]['MetaboliteID']
metabolite['name'] = r[4]['Name']
#metabolite['id'] = r[4]['MetaboliteID']
metabolite['stoichiometry'] = r[3]['Stoichiometry']
metabolite['aliases'] = []
metabolite['formula'] = None
......@@ -75,11 +103,6 @@ def database_retriever_by_id(id, url, user, password):
if key not in metabolites:
metabolites[key] = {}
#print("coiso" + str(metabolites[key]))
#for m in metabolites[key]:
# print(m)
if metaboliteId not in metabolites[key].keys():
metabolites[key][metaboliteId] = metabolite
......@@ -90,18 +113,3 @@ def database_retriever_by_id(id, url, user, password):
metabolites[key][metaboliteId]['formula'] = formula
return databases, tc_systems, reactionID, metaID, reaction, metabolites
# id = 'TR3000069' #small case
# #id = 'TR0000067' #extreme case
# print()
# print("########################")
# print()
# print(databases)
# print(tc_systems)
# print(reactionID)
# print(metaID)
# print(reaction)
# print(metabolites)
# for key in metabolites:
# print(key + " " + str(metabolites[key]))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment