-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart1.py
122 lines (91 loc) · 4.82 KB
/
part1.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
##### Tal Bachar #####
##### Farhan Chowdhury #####
##### CSCI 49371 Project #####
# Commands used in terminal to import files into mongoDB are:
### mongoimport --db projectDB --collection nodes --type tsv <PATH/TO/FILE.tsv> --headerline
### mongoimport --db projectDB --collection edges --type tsv <PATH/TO/FILE.tsv> --headerline
import pymongo
import pprint
import sys
from pymongo import MongoClient
from pprint import pprint
client = MongoClient('localhost', 27017) # sets client to local database
db = client["projectDB"] #db = name of database
edge_collection = db["edges"] #edges = name of collection
node_collection = db["nodes"] #nodes = name of collection
# What is the disease's name, what are drug names that can treat or palliate this disease, --> showDrugs()
# what are the gene names that cause this disease, --> showGenes()
# and where this disease occurs? --> showBodyParts()
###################################################################################
def main_menu():
global disease_name
global disease_id
disease_name = input("Enter Disease Name, or type 'exit' to quit program: ")
if disease_name == "exit":
print("Goodbye!")
sys.exit(0)
result = node_collection.count_documents({ "name": disease_name}) != 0 #check if disease exists in DB
if not result: # if disease doesn't exist, return to start of main menu
print("Could not find a disease called", disease_name, "\n")
main_menu()
print ("Found disease called:", disease_name, "\n")
showDrugs(disease_name)
showGenes(disease_name)
showBodyParts(disease_name)
main_menu()
###################################################################################
def showDrugs(disease_name):
drugList = [] # will hold all drugs that match
global disease_id
for user_input_result in node_collection.find({ "kind": "Disease", "name": disease_name },
{"_id": 0}): # find specific disease
disease_id = user_input_result['id'] # save id of disease as disease_id
for drugs_that_match in edge_collection.find({ "target": disease_id,
"source": {"$regex": "^Comp", "$options": 'i' }}, {"target": 0}):
drugList.append(drugs_that_match) #insert all drugs that match disease into array
print("Drugs that treat of palliate", disease_name, "are:")
print("-------------------------------------------------------")
if not drugList:
print("No data available \n")
else:
pprint(drugList) # print all drugs that match disease
print("\n\n")
###################################################################################
def showGenes(disease_name):
geneList = [] # will hold all genes that cause the disease
global disease_id
for user_input_result in node_collection.find({ "kind": "Disease", "name": disease_name },
{"_id": 0}): # find disease code
disease_id = user_input_result['id'] # save id of disease as disease_id
for genes_that_cause in edge_collection.find({"source": disease_id,
"target": {"$regex": "^Gen", "$options": 'i' }}, {"source": 0}):
geneList.append(genes_that_cause) #insert all genes that cause disease into array
print("Genes that cause", disease_name, "are:")
print("-------------------------------------------------------")
if not geneList:
print("No data available \n")
else:
pprint(geneList) # print all genes that cause disease
print("\n\n")
###################################################################################
def showBodyParts(disease_name):
bodypartList = []
global disease_id
for user_input_result in node_collection.find({ "kind": "Disease", "name": disease_name },
{"_id": 0}): # find disease code
disease_id = user_input_result['id'] # save id of disease as disease_id
for bodypartAffected in edge_collection.find({"source": disease_id,
"target": {"$regex": "^A", "$options": 'i' }} ):
tempStr = bodypartAffected['target']
for named_body_parts in node_collection.find({"id": tempStr},
{"id": 0, "kind": 0}):
bodypartList.append(named_body_parts)
print ("\n\nWhere in the body does this disease occur:")
print("-------------------------------------------------------")
if not bodypartList:
print("No data available \n")
else:
pprint(bodypartList) #prints all bodyparts that are affected
print("\n\n")
###################################################################################
main_menu() #run program