-
Notifications
You must be signed in to change notification settings - Fork 0
/
operate_db.py
439 lines (386 loc) · 15.3 KB
/
operate_db.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
import time
import template_sql as template_sql
import fill
import progressbar
def list_tables(db_connection):
""" list available tables in db"""
cur = db_connection.cursor()
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
return [value[0] for value in cur.fetchall()]
def get_table_size(db_connection, table_name):
""" get number of rows in a given table"""
cur = db_connection.cursor()
query = f'SELECT count(*) FROM {table_name};'
cur.execute(query)
return cur.fetchone()[0]
def reset(db_connection):
tables = list_tables(db_connection)
cur = db_connection.cursor()
# delete all tables
if len(tables) > 0:
querry = 'DROP table '
for table in tables:
querry += table + ", "
querry = querry[:-2] + ";"
cur.execute(querry)
db_connection.commit()
if len(list_tables(db_connection)) == 0:
print("Tables deleted")
# create new tables
cur.execute(open("ddl.sql", "r").read())
db_connection.commit()
# fill db
widgets = [
'\x1b[33mCreation of new tables \x1b[39m',
progressbar.Percentage(),
progressbar.Bar(marker='\x1b[32m#\x1b[39m'),
]
bar = progressbar.ProgressBar(widgets=widgets, max_value=6).start()
counter = 0
start = time.time()
new_file = fill.generate_random_values_columns('data/Homo_sapiens_gene_info.txt', 'popularity')
head1, cont1 = fill.parse_tsv(new_file)
fill.fill_database(db_connection, "gene", head1, cont1)
counter += 1
bar.update(counter)
head2, cont2 = fill.parse_tsv('data/SNP.txt')
fill.fill_database(db_connection, "snp", head2, cont2)
counter += 1
bar.update(counter)
merged = fill.merge('data/disease_OMIM.txt', 'data/gene_OMIM.txt', " ", 'disease_OMIM_ID')
merged.to_csv('data/merged.txt', sep=" ", index=False)
head3, cont3 = fill.parse_tsv('data/merged.txt')
headers_disease = ['DiseaseName', 'DiseaseID', 'AltDiseaseIDs']
cont4 = fill.parse_xml('data/CTD_diseases.xml', headers_disease)
headers_disease_gene = ['DiseaseName', 'DiseaseID', 'AltDiseaseIDs', 'GeneSymb']
disease_gene_content = fill.smart_merge_disease(disease_data=cont4, omim_data=cont3)
fill.fill_database(db_connection, "disease", headers_disease_gene, disease_gene_content)
counter += 1
bar.update(counter)
headers_chem_dis = ['ChemicalName', 'ChemicalID', 'DiseaseName', 'DiseaseID']
cont6 = fill.parseCTO_tsv('data/CTD_chemicals_diseases.tsv')
fill.fill_database(db_connection, "disease_drug", headers_chem_dis, cont6)
counter += 1
bar.update(counter)
head7, cont7 = fill.generate_random_mock_toxicity(db_connection)
fill.fill_database(db_connection, 'toxicity', head7, cont7)
counter += 1
bar.update(counter)
head8, cont8 = fill.generate_random_mock_prevalence(db_connection)
fill.fill_database(db_connection, 'prevalence', head8, cont8)
counter += 1
bar.update(counter)
end = time.time()
bar.finish()
print(f'Insert finished. It took {end - start} sec.')
# make table_name: size
table_size_str = ''
for table in list_tables(db_connection):
table_size_str += f'{table}: {get_table_size(db_connection, table)}' \
f' Rows\n'
print(f'Available tables:\n{table_size_str}')
def cols_info(db_connection, table):
""" get info about column names and
their data types in a given table"""
cur = None
if table.strip() == "" or table is None:
print("!!! No table name provided")
quit()
else:
cur = db_connection.cursor()
cur.execute(f'select column_name, data_type from information_schema.columns '
f'where table_name = \'{table}\';')
res_dict = {}
for tupl in cur.fetchall():
res_dict[tupl[0]] = tupl[1]
return res_dict
def pre_update(db_connection):
""" collecting further data for update """
print("*******************\n"
"Update action...\n"
"*******************")
print(f'Available tables: {list_tables(db_connection)}')
table = input("What table do you want to update in: ")
while True:
print("""What do you want to update?\n
a. Modify a row
b. Add a row
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['a', 'b', 'c', 'q']:
print("Please choose between a, b, c or q")
else:
break
cols_dic = cols_info(db_connection, table)
if answer == 'a':
print(f'Available cols: {list(cols_dic.keys())} ')
condition = input("What do you want to modify? Please provide in SQL format: ")
print("Please provide the new values")
content = ""
for colname, coltype in cols_dic.items():
user_input = input(f'{colname}: ')
if coltype == 'character varying' and \
"\'" not in user_input:
user_input = f'\'{user_input}\''
elif coltype == 'character varying' and \
'\"' in user_input:
user_input = user_input.replace('\"', '')
user_input = f'\'{user_input}\''
content += f'{colname}={user_input}, '
content = content[:-2]
update(db_connection=db_connection, table=table, new_content=content,
condition=condition, type_of_update='mod')
elif answer == 'b':
print(f'Available cols: {list(cols_dic.keys())} ')
print("Please provide the new values")
content = ""
for colname, coltype in cols_dic.items():
user_input = input(f'{colname}: ')
if coltype == 'character varying' and \
"\'" not in user_input:
user_input = f'\'{user_input}\''
elif coltype == 'character varying' and \
'\"' in user_input:
user_input = user_input.replace('\"', '')
user_input = f'\'{user_input}\''
content += f'{user_input}, '
content = content[:-2]
update(db_connection=db_connection, table=table,
new_content=content, type_of_update='add_r')
else:
quit()
def update(db_connection, table, new_content, type_of_update, condition=None):
query = None
if type_of_update == 'mod':
query = f'UPDATE {table} SET {new_content} WHERE {condition}'
print(f'UPDATE {table} SET {new_content} WHERE {condition}')
elif type_of_update == 'add_r':
query = f'INSERT INTO {table} VALUES ({new_content});'
else:
raise ValueError("Wrong type of update")
cur = db_connection.cursor()
cur.execute(query)
db_connection.commit()
print("Table was updated")
def pre_delete(db_connection):
""" collecting further data for deletion """
print("*******************\n"
"Delete action...\n"
"*******************")
while True:
print("""Do you want to delete table or entry in table?\n
a. Entry in table
b. Whole table
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['a', 'b', 'q']:
print("Please choose between a, b or q")
else:
break
if answer == 'a':
print(f'Available tables: {list_tables(db_connection)}')
table = input("What table do you want to delete in: ")
print(f'Available cols: {list(cols_info(db_connection, table).keys())}')
cond = input("What do you want to search? Please provide in SQL format: ")
print(f'Deleting in {table} with condition\n{cond}')
delete(db_connection, table, cond)
elif answer == 'b':
print(f'Available tables: {list_tables(db_connection)}')
table = input("What table do you want to delete: ")
print(f'Deleting table {table}')
delete_table(db_connection, table)
else:
quit()
def delete_table(db_connection, table):
cur = db_connection.cursor()
query = ""
if table.strip() == "" or table is None:
print("!!! No table name provided")
quit()
else:
query = f'DROP TABLE IF EXISTS {table};'
cur.execute(query)
db_connection.commit()
print(f'Table {table} was deleted')
def delete(db_connection, table, condition):
cur = db_connection.cursor()
if (table.strip() == "" or table is None) or \
(condition.strip() == "" or condition is None):
print("!!! No table name provided")
quit()
else:
query = f'DELETE FROM {table} WHERE {condition};'
cur.execute(query)
db_connection.commit()
print("An entry in table was deleted")
def pre_search(db_connection):
""" collecting further data for search """
print("*******************\n"
"Search action...\n"
"*******************")
print(f'Available tables: {list_tables(db_connection)}')
while True:
print("""Pick option?\n
a. Try out the search templates
b. Make own search query
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['a', 'b', 'q']:
print("Please choose between a, b or q")
else:
break
if answer == 'a':
while True:
print("""What template do you want to try out?\n
1. Find all gene information
2. Find all gene symbols located in the chromosome
3. Find all diseases associated with the SNP
4. Find all SNP IDs associated with the disease
5. Drug-Diseases related templates
6. Drug-Genes related templates
7. Diseases-Genes related templates
8. Statistics
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['1', '2', '3', '4', '5', '6', '7', '8', 'q']:
print("Please choose between 1, 2, 3, 4, 5, 6, 7, 8 and q")
else:
break
if answer == '1':
answer2 = input("Please provide a gene symbol: ")
return template_sql.get_gene_info(db_connection, answer2)[0]
elif answer == '2':
answer2 = input("Please provide a chromosome number: ")
res = template_sql.get_genes_on_chromosome(db_connection, answer2)
return [value[0] for value in res]
elif answer == '3':
answer2 = input("Please provide a SNP id: ")
res = template_sql.find_diseases(db_connection, answer2)
return [value[0] for value in res]
elif answer == '4':
answer2 = input("Please provide a disease name: ")
res = template_sql.find_snp(db_connection, answer2)
return [value[0] for value in res]
elif answer == '5':
return drug_disease(db_connection)
elif answer == '6':
return drug_genes(db_connection)
elif answer == '7':
return disease_genes(db_connection)
elif answer == '8':
return db_statistics(db_connection)
else:
quit()
if answer == 'b':
table = input("What table do you want to search in: ")
cols_dic = cols_info(db_connection, table)
print(f'Available cols: {list(cols_dic.keys())}')
cond = input("What do you want to search? Please provide in SQL format: ")
res = search(db_connection, table, cond)
return res
else:
quit()
def db_statistics(db_connection):
while True:
print("""Choose a statistic?\n
1. Statistics: Count number of diseases for each chromosome
2. Statistics: Drugs that can treat the most diseases
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['1', '2', 'q']:
print("Please choose between 1, 2 and q")
else:
break
if answer == '1':
res = template_sql.stats_diseases_on_chr(db_connection)
return res
elif answer == '2':
res = template_sql.stats_universal_drug(db_connection)
return res
else:
quit()
def disease_genes(db_connection):
while True:
print("""What template do you want to try out?\n
1. Given chromosome number find associated diseases
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['1', 'q']:
print("Please choose between 1 and q")
else:
break
if answer == '1':
answer2 = input("Please provide a chromosome number: ")
res = template_sql.get_diseases_from_chr(db_connection, answer2)
return res
elif answer == '2':
res = template_sql.stats_diseases_on_chr(db_connection)
return res
else:
quit()
def drug_genes(db_connection):
while True:
print("""What template do you want to try out?\n
1. Find genes that are affected by given drug
2. Find chromosomes that are affected by given drug
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['1', '2', 'q']:
print("Please choose between 1, 2, and q")
else:
break
if answer == '1':
answer2 = input("Please provide a drug name: ")
res = template_sql.get_genes_from_drug(db_connection, answer2)
return res
elif answer == '2':
answer2 = input("Please provide a drug name: ")
res = template_sql.get_chr_from_drug(db_connection, answer2)
return res
else:
quit()
def drug_disease(db_connection):
while True:
print("""What template do you want to try out?\n
1. Find drug to treat given disease
2. Find diseases that can be treated with your drug
q. Quit
""")
answer = input("Enter your choice: ").strip()
if answer not in ['1', '2', 'q']:
print("Please choose between 1, 2 and q")
else:
break
if answer == '1':
answer2 = input("Please provide a disease name: ")
res = template_sql.get_drugs(db_connection, answer2)
return res
elif answer == '2':
answer2 = input("Please provide a drug name: ")
res = template_sql.get_diseases(db_connection, answer2)
return res
else:
quit()
def search(db_connection, table, condition):
cur = db_connection.cursor()
# get tables columns names
query = ""
if table.strip() == "" or table is None:
print("!!! No table name provided")
quit()
elif condition.strip() == "" or condition is None:
query = f'SELECT * FROM {table}'
else:
query = f'SELECT * FROM {table} WHERE {condition};'
cur.execute(query)
result = [value for value in cur.fetchall()]
db_connection.commit()
return result