forked from alxbetz/celltoxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_db.py
executable file
·320 lines (250 loc) · 7.76 KB
/
insert_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
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 14:13:36 2020
@author: Alex
"""
from app.models import Chemical, Medium, Exposure, Cell_line \
, Person, Estimated, Solvent, Endpoint, Nanomaterial
from fileIO import read_estimated
import subprocess
import os
import warnings
import uuid, shutil
from app import app
fields_record = [
'plate_size',
'timepoint',
'endpoint',
'passive_dosing',
'insert',
'dosing', #direct or indirect
'conc_determination',
'solvent',
'rawfile_hash',
'id_string',
'replicates'
]
def calc_ec(fname):
"""
Parameters
----------
fname : string
filename of dose response values, first column must be concentration
Returns
-------
dict
output file names and R call status flag
"""
bname = os.path.splitext(os.path.basename(fname))[0]
print(bname)
outdir = os.path.join("tmp", bname)
try:
os.mkdir(outdir)
except OSError:
print("Directory %s exists" % outdir)
errPIPE = open(os.path.join(outdir, "stderr.txt"), 'w+')
rcall = " ".join(
["Rscript",
os.path.join("R", "fitdr.R"), outdir, fname, "delta"])
sout = subprocess.Popen(
["Rscript",
os.path.join("R", "fitdr.R"), outdir, fname, "delta"],
stdout=subprocess.PIPE,
stderr=errPIPE)
output, errors = sout.communicate()
rbname = os.path.join(outdir, bname)
return {
'rcall': rcall,
'plot_png_base': bname + '.png',
'plot_png_full': rbname + '.png',
'estimated': rbname + '_estimated.csv',
'plot_data': rbname + '_plotdata.csv',
'r_data': rbname + '.RDS',
'status': sout
}
def make_imagename(filename):
""" Generate a unique name for an image"""
return str(uuid.uuid1()) + "_sep_" + filename
def make_estimated(exposure, ec_out):
"""
Parameters
----------
exposure : TYPE
DESCRIPTION.
ec_out : TYPE
DESCRIPTION.
Returns
-------
estimated : TYPE
DESCRIPTION.
"""
if (ec_out['status'].returncode > 0):
return None
est_dict = read_estimated(ec_out['estimated'])
iname = make_imagename(ec_out['plot_png_base'])
fcopy = os.path.join(app.config['IMG_UPLOAD_FOLDER'], iname)
shutil.copy2(ec_out['plot_png_full'], fcopy)
est_dict['plot_png'] = iname
# if no EC50 values could be calculated, the slope CIs dont need to be removed
if 'exceeds_direction' not in est_dict.keys():
binKeys = ['plot_data', 'r_data']
for k in binKeys:
est_dict[k] = open(ec_out[k], 'rb').read()
del est_dict['slope_ci_lower']
del est_dict['slope_ci_upper']
est_dict['exposure_id'] = exposure.id
estimated = Estimated(**est_dict)
return estimated
def check_rawfile(hash_string, se):
hash_query = se.query(Exposure).filter(
Exposure.rawfile_hash == hash_string)
warnings.warn("Rawfile was already added to the database")
if hash_query.count() > 0:
return False
else:
return True
def add_record(rec, se, eng):
"""
Parameters
----------
rec : dict
DESCRIPTION.
se : flask session object
The current database session
eng : database engine object
DESCRIPTION.
Returns
-------
bool
DESCRIPTION.
"""
#check if rawfile has already been added to the database already
file_check = check_rawfile(rec['rawfile_hash'], se)
if not file_check:
return False
cell_line = se.query(Cell_line).filter(
Cell_line.short_name == rec['cell_line']).one_or_none()
medium = se.query(Medium).filter(
Medium.short_name == rec['medium']).one_or_none()
experimenter = se.query(Person).filter_by(
short_name=rec['experimenter']).one()
to_add = {k: rec[k] for k in fields_record}
to_add['solvent'] = se.query(Solvent).filter_by(
short_name=rec['solvent']).one()
to_add['endpoint'] = se.query(Endpoint).filter_by(
short_name=rec['endpoint']).one()
to_add['medium'] = medium
to_add['cell_line'] = cell_line
to_add['experimenter'] = experimenter
chem = se.query(Chemical).filter_by(
cas_number=rec['cas_number']).one_or_none()
if chem is None:
if rec['cas_number'].find("nano") == 0:
nano_id = int(rec['cas_number'].strip("nano"))
nano_rec = se.query(Nanomaterial).filter(
Nanomaterial.id == nano_id).one_or_none()
to_add['nanomaterial'] = nano_rec
else:
return False
else:
to_add['chemical'] = chem
exposure = Exposure(**to_add)
#after the commit exposure.id gets a value
se.add(exposure)
se.commit()
rec['raw_data']['exposure_id'] = exposure.id
rec['raw_data'].to_sql("dose_response",
eng,
if_exists='append',
schema='public',
index=False,
chunksize=500)
ec_out = calc_ec(rec['filename'])
estimated = make_estimated(exposure, ec_out)
if estimated is not None:
se.add(estimated)
se.commit()
else:
warnings.warn("Could not calculate EC50 values")
return True
def create_exposure(rec, se):
"""
Parameters
----------
rec : TYPE
DESCRIPTION.
se : TYPE
DESCRIPTION.
Returns
-------
exposure : TYPE
DESCRIPTION.
"""
for k, v in rec.items():
if isinstance(rec[k], str) and rec[k] == 'None':
rec[k] = None
elif k.endswith("_id"):
rec[k] = int(rec[k])
#get set of fields which are columns in the database
ifields = set(Exposure.__dict__.keys()).intersection(set(rec.keys()))
to_add = {k: rec[k] for k in ifields}
exposure = Exposure(**to_add)
se.add(exposure)
se.commit()
return exposure
def add_record_rawdata(rec, se, eng):
"""
Parameters
----------
rec : dict
DESCRIPTION.
se : flask session object
The current database session
eng : database engine object
DESCRIPTION.
Returns
-------
bool
DESCRIPTION.
"""
#check if rawfile has already been added to the database
file_check = check_rawfile(rec['rawfile_hash'], se)
if not file_check:
return False
exposure = create_exposure(rec, se)
rec['raw_data']['exposure_id'] = exposure.id
rec['raw_data'].to_sql("dose_response",
eng,
if_exists='append',
schema='public',
index=False,
chunksize=500)
ec_out = calc_ec(rec['filename'])
estimated = make_estimated(exposure, ec_out)
if estimated is not None:
se.add(estimated)
se.commit()
else:
warnings.warn("Could not calculate EC50 values")
return exposure
def add_record_norawdata(rec, se):
"""
Parameters
----------
rec : TYPE
DESCRIPTION.
se : TYPE
DESCRIPTION.
Returns
-------
exposure : TYPE
DESCRIPTION.
"""
exposure = create_exposure(rec, se)
rec['exposure_id'] = exposure.id
ifields = set(Estimated.__dict__.keys()).intersection(set(rec.keys()))
estimated_dict = {k: rec[k] for k in ifields}
estimated = Estimated(**estimated_dict)
se.add(estimated)
se.commit()
return exposure