Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (47 loc) · 1.96 KB
/
Copy pathapp.py
File metadata and controls
61 lines (47 loc) · 1.96 KB
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
from flask import Flask, jsonify
from flask_restful import Api, Resource, reqparse, request
from neo4j import GraphDatabase
from pymongo import MongoClient
app = Flask(__name__)
api = Api(app)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "testingtesting"))
client = MongoClient("localhost:27017")
db = client['DrugID_NameMap']
collection = db['drug_map']
# https://www.biostars.org/p/271718/
def getNeighbors( node_name ):
with driver.session() as session:
result = session.run("MATCH (sourceNode {Name: $name})-[:INTERACTS_WITH]->(neighborNode) RETURN neighborNode", name=node_name)
nodes = [record['neighborNode']['Name'] for record in result]
return nodes
class NodeResource(Resource):
def get(self):
with driver.session() as session:
result = session.run("MATCH (n) RETURN n")
nodes = [record['n']['Name'] for record in result]
return {'nodes': nodes}, 200
class Neighbors(Resource):
def post(self):
parser = reqparse.RequestParser()
parser.add_argument('Name', required=True)
args = parser.parse_args()
node_name = args['Name']
neighbors = getNeighbors(node_name)
return {'neighbors': neighbors}, 201
class ValidMedication(Resource):
def post(self):
names = request.get_json()["Names"]
neighbors = set()
for n in names:
query = { "synonym": n }
result = collection.find_one(query)
if not result:
return {'error' : 'Invalid input. Please provide both name and age.'}, 400
neighbors |= set(getNeighbors(result["drug_id"]))
intersection = [*(set(names) & neighbors)]
return {'valid': not intersection, 'intersection' : intersection}, 201
api.add_resource(NodeResource, '/nodes')
api.add_resource(Neighbors, '/neighbors')
api.add_resource(ValidMedication, '/valid')
if __name__ == '__main__':
app.run(debug=True, port=8000)