-
Notifications
You must be signed in to change notification settings - Fork 19
/
sample_repo.py
445 lines (387 loc) · 17.8 KB
/
sample_repo.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
440
441
442
443
444
445
import werkzeug
from werkzeug.exceptions import NotFound
from microsetta_private_api.repo.account_repo import AccountRepo
from microsetta_private_api.repo.base_repo import BaseRepo
from microsetta_private_api.model.sample import Sample, SampleInfo
from microsetta_private_api.exceptions import RepoException
from microsetta_private_api.repo.source_repo import SourceRepo
class SampleRepo(BaseRepo):
PARTIAL_SQL = """SELECT
ag_kit_barcodes.ag_kit_barcode_id,
ag.ag_kit_barcodes.sample_date,
ag.ag_kit_barcodes.sample_time,
ag.ag_kit_barcodes.site_sampled,
ag.ag_kit_barcodes.notes,
ag.ag_kit_barcodes.barcode,
latest_scan.scan_timestamp,
ag.source.id,
ag.source.account_id
FROM ag.ag_kit_barcodes
LEFT JOIN (
SELECT barcode, max(scan_timestamp) AS scan_timestamp
FROM barcodes.barcode_scans
GROUP BY barcode
) latest_scan
ON ag.ag_kit_barcodes.barcode = latest_scan.barcode
LEFT JOIN ag.source
ON ag.ag_kit_barcodes.source_id = ag.source.id"""
# the nested query requires a full scan of the barcode_scans
# table, which is not necessary if limited to a specific sample.
# this query is approximately 200x faster than the above
# Note the WHERE in the nested query
PARTIAL_SQL_BARCODE_SPECIFIC = """SELECT
ag_kit_barcodes.ag_kit_barcode_id,
ag.ag_kit_barcodes.sample_date,
ag.ag_kit_barcodes.sample_time,
ag.ag_kit_barcodes.site_sampled,
ag.ag_kit_barcodes.notes,
ag.ag_kit_barcodes.barcode,
latest_scan.scan_timestamp,
ag.source.id,
ag.source.account_id
FROM ag.ag_kit_barcodes
LEFT JOIN (
SELECT barcode, max(scan_timestamp) AS scan_timestamp
FROM barcodes.barcode_scans
WHERE barcode = %s
GROUP BY barcode
) latest_scan
ON ag.ag_kit_barcodes.barcode = latest_scan.barcode
LEFT JOIN ag.source
ON ag.ag_kit_barcodes.source_id = ag.source.id"""
def __init__(self, transaction):
super().__init__(transaction)
def _retrieve_projects(self, sample_barcode):
with self._transaction.cursor() as cur:
# If there is a sample, we can look for the projects associated
# with it. We do this as a secondary query:
cur.execute("SELECT barcodes.project.project FROM "
"barcodes.barcode "
"LEFT JOIN "
"barcodes.project_barcode "
"ON "
"barcodes.barcode.barcode = "
"barcodes.project_barcode.barcode "
"LEFT JOIN barcodes.project "
"ON "
"barcodes.project_barcode.project_id = "
"barcodes.project.project_id "
"WHERE "
"barcodes.barcode.barcode = %s",
(sample_barcode,))
project_rows = cur.fetchall()
sample_projects = [project[0] for project in project_rows]
return sample_projects
def _create_sample_obj(self, sample_row):
if sample_row is None:
return None
sample_barcode = sample_row[5]
scan_timestamp = sample_row[6]
sample_projects = self._retrieve_projects(sample_barcode)
sample_status = self.get_sample_status(sample_barcode, scan_timestamp)
return Sample.from_db(*sample_row, sample_projects, sample_status)
# TODO: I'm still not entirely happy with the linking between samples and
# sources. The new source_id is direct (and required for environmental
# samples, which have no surveys) but samples can also link to
# surveys which then may link to sources.
# Having multiple pathways to link in the db is a recipe for badness.
# Should something be done with the source_barcodes_surveys table? (which
# itself is required for linking samples to surveys!)
def _update_sample_association(self, sample_id, source_id,
override_locked=False):
with self._transaction.cursor() as cur:
existing_sample = self._get_sample_by_id(sample_id)
if existing_sample.remove_locked and not override_locked:
raise RepoException(
"Sample association locked: Sample already received")
barcode = existing_sample.barcode
# collect old survey associations if they exist
cur.execute("""SELECT survey_id
FROM ag.ag_login_surveys
JOIN ag.ag_kit_barcodes USING (source_id)
WHERE ag_kit_barcode_id=%s""",
(sample_id, ))
survey_ids = tuple([r[0] for r in cur.fetchall()])
if len(survey_ids) > 0:
# if this was a previously assigned sample, then remove any
# stale relations
cur.execute("""DELETE FROM ag.source_barcodes_surveys
WHERE barcode=%s
AND survey_id IN %s""",
(barcode, survey_ids))
# collect new survey associations if they exist
cur.execute("""SELECT survey_id
FROM ag.ag_login_surveys
WHERE source_id=%s""",
(source_id, ))
survey_ids = [r[0] for r in cur.fetchall()]
# set the barcode source association
cur.execute("""UPDATE ag_kit_barcodes
SET source_id = %s
WHERE ag_kit_barcode_id = %s""",
(source_id, sample_id))
# set barcode survey associations
updates = [(barcode, sid) for sid in survey_ids]
cur.executemany("""INSERT INTO ag.source_barcodes_surveys
(barcode, survey_id)
VALUES (%s, %s)""",
updates)
# update any vioscreen associations to reflect the source
# and the account
if source_id is not None:
cur.execute("""SELECT account_id
FROM ag.source
WHERE id=%s""",
(source_id, ))
account_id = cur.fetchone()
cur.execute("""UPDATE ag.vioscreen_registry
SET source_id=%s, account_id=%s
WHERE sample_id=%s""",
(source_id, account_id, sample_id))
def migrate_sample(self, sample_id, source_id_src, source_id_dst,
areyousure=False):
"""Migrate a sample among sources
WARNING !!!
This is NOT intended for general use. This is an
administrative method for correcting unusual circumstances, and the
person issuing this function call knows what they are doing.
WARNING !!!
"""
if not areyousure:
raise RepoException("You aren't sure you want to do this")
if source_id_src == source_id_dst:
# nothing to do
return
with self._transaction.cursor() as cur:
# verify the destination source exists
cur.execute("SELECT id FROM ag.source where id=%s",
(source_id_dst, ))
res = cur.fetchall()
if len(res) != 1:
raise RepoException(f"source ({source_id_dst}) does not exist")
# verify the sample is currently associated with source_id_src
cur.execute("""SELECT ag_kit_barcode_id
FROM ag.ag_kit_barcodes
WHERE source_id=%s
AND ag_kit_barcode_id=%s""",
(source_id_src, sample_id))
res = cur.fetchall()
if len(res) != 1:
raise RepoException(f"{len(res)} entries associated with "
f"source ({source_id_src}) and sample "
f"({sample_id})")
self._update_sample_association(sample_id, source_id_dst,
override_locked=True)
def _get_sample_barcode_from_id(self, sample_id):
"""Obtain a barcode from a ag.ag_kit_barcode_id"""
sql = """SELECT barcode
FROM ag.ag_kit_barcodes
WHERE ag_kit_barcode_id = %s"""
with self._transaction.cursor() as cur:
cur.execute(sql, (sample_id,))
sample_row = cur.fetchone()
if sample_row is None:
return None
else:
return sample_row[0]
def _get_sample_by_id(self, sample_id):
""" Do not use from api layer, you must validate account and source."""
sql = "{0}{1}".format(
self.PARTIAL_SQL_BARCODE_SPECIFIC,
" WHERE"
" ag_kit_barcodes.ag_kit_barcode_id = %s")
with self._transaction.cursor() as cur:
barcode = self._get_sample_barcode_from_id(sample_id)
cur.execute(sql, (barcode, sample_id,))
sample_row = cur.fetchone()
return self._create_sample_obj(sample_row)
def get_samples_by_source(self, account_id, source_id,
allow_revoked=False):
sql = "{0}{1}".format(
self.PARTIAL_SQL,
" WHERE"
" source.account_id = %s"
" AND source.id = %s"
" ORDER BY ag_kit_barcodes.barcode asc")
with self._transaction.cursor() as cur:
acct_repo = AccountRepo(self._transaction)
if acct_repo.get_account(account_id) is None:
raise NotFound("No such account")
source_repo = SourceRepo(self._transaction)
if source_repo.get_source(account_id, source_id,
allow_revoked=allow_revoked) is None:
raise NotFound("No such source")
cur.execute(sql, (account_id, source_id))
samples = []
for sample_row in cur.fetchall():
sample = self._create_sample_obj(sample_row)
sample.kit_id = self._get_supplied_kit_id_by_sample(
sample.barcode
)
samples.append(sample)
return samples
def get_sample(self, account_id, source_id, sample_id):
sql = "{0}{1}".format(
self.PARTIAL_SQL_BARCODE_SPECIFIC,
" WHERE"
" source.account_id = %s"
" AND source.id = %s"
" AND ag_kit_barcodes.ag_kit_barcode_id = %s ")
with self._transaction.cursor() as cur:
barcode = self._get_sample_barcode_from_id(sample_id)
cur.execute(sql, (barcode, account_id, source_id, sample_id))
sample_row = cur.fetchone()
return self._create_sample_obj(sample_row)
def update_info(self, account_id, source_id, sample_info,
override_locked=False):
"""
Updates end user writable information about a sample that is assigned
to a source.
"""
existing_sample = self.get_sample(account_id, source_id,
sample_info.id)
if existing_sample is None:
raise werkzeug.exceptions.NotFound("No sample ID: %s" %
sample_info.id)
if existing_sample.edit_locked and not override_locked:
raise RepoException("Sample edits locked: Sample already evaluated"
" for processing")
sample_date = None
sample_time = None
if sample_info.datetime_collected is not None:
sample_date = sample_info.datetime_collected.date()
sample_time = sample_info.datetime_collected.time()
# TODO: Need to get the exact policy on which fields user is allowed
# to set. For starters: I think: datetime_collected, site, notes
with self._transaction.cursor() as cur:
cur.execute("UPDATE "
"ag_kit_barcodes "
"SET "
"sample_date = %s, "
"sample_time = %s, "
"site_sampled = %s, "
"notes = %s "
"WHERE "
"ag_kit_barcode_id = %s",
(
sample_date,
sample_time,
sample_info.site,
sample_info.notes,
sample_info.id
))
def associate_sample(self, account_id, source_id, sample_id,
override_locked=False):
with self._transaction.cursor() as cur:
cur.execute("SELECT "
"ag_kit_barcode_id, "
"source.account_id, "
"source.id "
"FROM "
"ag_kit_barcodes "
"LEFT OUTER JOIN source "
"ON ag_kit_barcodes.source_id = source.id "
"WHERE "
"ag_kit_barcode_id = %s",
(sample_id,))
row = cur.fetchone()
if row is None:
raise werkzeug.exceptions.NotFound("No sample ID: %s" %
sample_id)
if row[2] is not None:
if row[1] != account_id:
# This is the case where the sample is already assigned in
# another account
raise RepoException("Sample is already assigned")
else:
# This is the case where the sample is already assigned in
# the same account. We will make them dissociate first.
raise RepoException("Sample is already assigned")
else:
# This is the case where the sample is not yet assigned
self._update_sample_association(sample_id, source_id,
override_locked=override_locked) # noqa
def dissociate_sample(self, account_id, source_id, sample_id,
override_locked=False):
existing_sample = self.get_sample(account_id, source_id, sample_id)
if existing_sample is None:
raise werkzeug.exceptions.NotFound("No sample ID: %s" %
sample_id)
if existing_sample.edit_locked and not override_locked:
raise RepoException(
"Sample information locked: Sample already evaluated for "
"processing")
# Wipe any user entered fields from the sample:
self.update_info(account_id, source_id,
SampleInfo(sample_id, None, None, None),
override_locked=override_locked)
if existing_sample.remove_locked and not override_locked:
raise RepoException(
"Sample association locked: Sample already received")
# And detach the sample from the source
self._update_sample_association(sample_id, None,
override_locked=override_locked)
def get_sample_status(self, sample_barcode, scan_timestamp):
if scan_timestamp is None:
return None
with self._transaction.cursor() as cur:
cur.execute(
"SELECT "
"sample_status "
"FROM barcodes.barcode_scans "
"WHERE barcode=%s AND scan_timestamp = %s "
"LIMIT 1",
(sample_barcode, scan_timestamp)
)
row = cur.fetchone()
if row is None:
return None
return row[0]
def _get_supplied_kit_id_by_sample(self, sample_barcode):
with self._transaction.cursor() as cur:
cur.execute(
"SELECT kit_id "
"FROM barcodes.barcode "
"WHERE barcode = %s",
(sample_barcode, )
)
row = cur.fetchone()
return row[0]
def scrub(self, account_id, source_id, sample_id):
"""Wipe out free text information for a sample
Parameters
----------
account_id : str, uuid
The associated account ID to scrub
source_id : str, uuid
The associated source ID to scrub
sample_id : str, uuid
The associated sample ID to scrub
Raises
------
RepoException
If the account / source is relation is bad
If the source / sample relation is bad
If the update fails for any reason
Returns
-------
True if the sample was scrubbed, will raise otherwise
"""
notes = "scrubbed"
with self._transaction.cursor() as cur:
# verify our account / source relation is reasonable
cur.execute("""SELECT id
FROM ag.source
WHERE id=%s AND account_id=%s""",
(source_id, account_id))
res = cur.fetchone()
if res is None:
raise RepoException("Invalid account / source relation")
cur.execute("""UPDATE ag_kit_barcodes
SET notes=%s
WHERE source_id=%s AND ag_kit_barcode_id=%s""",
(notes, source_id, sample_id))
if cur.rowcount != 1:
raise RepoException("Invalid source / sample relation")
else:
return True