-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify.py
124 lines (100 loc) · 3.65 KB
/
modify.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
import psycopg2
from datetime import date
import os
def clear_screen():
os.system('cls' if os.name == 'nt' else 'clear')
def modify_gene(cursor, connection):
cursor.execute("SELECT * FROM gene_test LIMIT 0")
print("Column names:")
print(", ".join([desc[0] for desc in cursor.description]))
gene_id = input("Enter the ID of the gene to modify: ")
if gene_id == "exit":
return
col = input("Choose which column to update: ")
if col == "exit":
return
mod_val = input("Enter new value: ")
if col in ["gene_id", "tax_id"]:
sql = f"UPDATE gene_test SET {col} = %s WHERE gene_id = %s"
else:
print("Column not in dataset")
return
try:
cursor.execute(sql, (mod_val, gene_id))
connection.commit()
cursor.execute("UPDATE gene_test SET mod_date = %s WHERE gene_id = %s", (date.today(), gene_id))
connection.commit()
print(f"Updated gene {gene_id}.")
except psycopg2.Error as e:
print("Query error: check your input or contact developers")
print(e)
def modify_snp(cursor, connection):
cursor.execute("SELECT * FROM snp_test LIMIT 0")
print("Column names:")
print(", ".join([desc[0] for desc in cursor.description]))
snp_id = input("Enter the ID of the SNP to modify: ")
if snp_id == "exit":
return
col = input("Choose which column to update: ")
if col == "exit":
return
mod_val = input("Enter new value: ")
if col in ["snp_id", "gene_id", "snp_position"]:
sql = f"UPDATE snp_test SET {col} = %s WHERE snp_id = %s"
else:
print("Column not in dataset")
return
try:
cursor.execute(sql, (mod_val, snp_id))
connection.commit()
print(f"Updated SNP {snp_id}.")
except psycopg2.Error as e:
print("Query error: check your input or contact developers")
print(e)
def modify_disease(cursor, connection):
cursor.execute("SELECT * FROM new_disease_test LIMIT 0")
print("Column names:")
print(", ".join([desc[0] for desc in cursor.description]))
disease_id = input("Enter the ID of the disease to modify: ")
if disease_id == "exit":
return
col = input("Choose which column to update: ")
if col == "exit":
return
mod_val = input("Enter new value: ")
if col == disease_id:
sql = f"UPDATE new_disease_test SET {col} = %s WHERE disease_id = %s"
else:
print("Column not in dataset")
return
try:
cursor.execute(sql, (mod_val, disease_id))
connection.commit()
print(f"Updated disease {disease_id}.")
except psycopg2.Error as e:
print("Query error: check your input or contact developers")
print(e)
def modify(connection, cursor):
while True:
print("Select which database to modify: ")
print("0: Gene, 1: SNP, 2: Disease")
print("Type 'exit' to exit to the previous menu")
mod_mode = input("Select database: ")
clear_screen()
if mod_mode == "exit":
print("Exiting modification mode...")
clear_screen()
break
try:
if mod_mode == "0":
modify_gene(cursor, connection)
elif mod_mode == "1":
modify_snp(cursor, connection)
elif mod_mode == "2":
modify_disease(cursor, connection)
else:
clear_screen()
print("Invalid input. Please input either '0', '1', '2' or 'exit'.")
except Exception as e:
clear_screen()
print("An error occurred:", e)