-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
183 lines (174 loc) · 6.84 KB
/
database.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
import psycopg
class Database:
def __init__(self, database_name, host, port, user, password=None):
self.conn = psycopg.connect(f"user={user} password={password} host={host} port={port} dbname={database_name}")
self.c = self.conn.cursor()
def insert_patient(self, patient_id, birth_date, sex, status, consent):
self.c.execute(
f"INSERT INTO patient (id, birth_date, sex, status, consent) VALUES (%s, %s, %s, %s, %s)",
(
patient_id,
birth_date,
sex,
status,
consent
),
)
self.conn.commit()
def insert_tissue(self, sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no,
material_type_id, diagnosis, ptnm, morphology, cut_time, freeze_time, retrieved):
self.c.execute(
f"INSERT INTO tissue (sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no, material_type_id, diagnosis, ptnm, morphology, cut_time, freeze_time, retrieved) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(
sample_id,
patient_id,
biopsy_id,
predictive_id,
samples_no,
available_samples_no,
material_type_id,
diagnosis,
ptnm,
morphology,
cut_time,
freeze_time,
retrieved
),
)
def insert_serum(self, sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no,
material_type_id, diagnosis, taking_date):
self.c.execute(
f"INSERT INTO serum (sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no, material_type_id, diagnosis,taking_date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
(
sample_id,
patient_id,
biopsy_id,
predictive_id,
samples_no,
available_samples_no,
material_type_id,
diagnosis,
taking_date,
),
)
def insert_genome(self, sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no,
material_type_id, retrieved, taking_date):
self.c.execute(
f"INSERT INTO genome (sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no, material_type_id, retrieved, taking_date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
(
sample_id,
patient_id,
biopsy_id,
predictive_id,
samples_no,
available_samples_no,
material_type_id,
retrieved,
taking_date,
),
)
def insert_cell(self, sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no,
material_type_id):
self.c.execute(
f"INSERT INTO cell (sample_id, patient_id, biopsy_id, predictive_id, samples_no, available_samples_no, material_type_id) VALUES (%s, %s, %s, %s, %s, %s, %s)",
(
sample_id,
patient_id,
biopsy_id,
predictive_id,
samples_no,
available_samples_no,
material_type_id,
),
)
def insert_diagnosis_material(self, sample_id, patient_id, material_type_id, taking_date, diagnosis, retrieved):
self.c.execute(
f"INSERT INTO diagnosis_material (sample_id, patient_id, material_type_id, taking_date, diagnosis, retrieved) VALUES (%s, %s, %s, %s, %s, %s)",
(
sample_id,
patient_id,
material_type_id,
taking_date,
diagnosis,
retrieved,
),
)
def get_samples_with_pred_id(self, predictive_id):
data = []
self.c.execute(
f"SELECT sample_id, patient_id, biopsy_id, predictive_id, material_type_id, diagnosis, cut_time, freeze_time FROM tissue WHERE predictive_id = %s",
(
predictive_id,
),
)
for row in self.c.fetchall():
data.append(
{
"sample_id": row[0],
"patient_id": row[1],
"biopsy_id": row[2],
"predictive_id": row[3],
"material_type_id": row[4],
"diagnosis": row[5],
"cut_time": row[6],
"freeze_time": row[7]
}
)
self.c.execute(
f"SELECT sample_id, patient_id, biopsy_id, predictive_id, material_type_id, diagnosis, taking_date FROM serum WHERE predictive_id = %s",
(
predictive_id,
),
)
for row in self.c.fetchall():
data.append(
{
"sample_id": row[0],
"patient_id": row[1],
"biopsy_id": row[2],
"predictive_id": row[3],
"material_type_id": row[4],
"diagnosis": row[5],
"sampling_timestamp": row[6],
"registration_timestamp": row[6]
}
)
self.c.execute(
f"SELECT sample_id, patient_id, biopsy_id, predictive_id, material_type_id, taking_date FROM genome WHERE predictive_id = %s",
(
predictive_id,
),
)
for row in self.c.fetchall():
data.append(
{
"sample_id": row[0],
"patient_id": row[1],
"biopsy_id": row[2],
"predictive_id": row[3],
"material_type_id": row[4],
"diagnosis": None,
"sampling_timestamp": row[5],
"registration_timestamp": row[5]
}
)
self.c.execute(
f"SELECT sample_id, patient_id, biopsy_id, predictive_id, material_type_id FROM cell WHERE predictive_id = %s",
(
predictive_id,
),
)
for row in self.c.fetchall():
data.append(
{
"sample_id": row[0],
"patient_id": row[1],
"biopsy_id": row[2],
"predictive_id": row[3],
"material_type_id": row[4],
"diagnosis": None,
"sampling_timestamp": None,
"registration_timestamp": None,
}
)
return data