[973924]: / qiita_db / portal.py

Download this file

466 lines (405 with data), 17.9 kB

  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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
import warnings
import qiita_db as qdb
class Portal(qdb.base.QiitaObject):
r"""Portal object to create and maintain portals in the system
Attributes
----------
portal
Methods
-------
get_studies
add_studies
remove_studies
get_analyses
add_analyses
remove_analyses
"""
_table = 'portal_type'
def __init__(self, portal):
with qdb.sql_connection.TRN:
self.portal = portal
portal_id = qdb.util.convert_to_id(portal, 'portal_type', 'portal')
super(Portal, self).__init__(portal_id)
@staticmethod
def list_portals():
"""Returns list of non-default portals available in system
Returns
-------
list of str
List of portal names for the system
Notes
-----
This does not return the QIITA portal in the list, as it is a required
portal that can not be edited.
"""
with qdb.sql_connection.TRN:
sql = """SELECT portal
FROM qiita.portal_type
WHERE portal != 'QIITA'
ORDER BY portal"""
qdb.sql_connection.TRN.add(sql)
return qdb.sql_connection.TRN.execute_fetchflatten()
@classmethod
def create(cls, portal, desc):
"""Creates a new portal and its default analyses on the system
Parameters
----------
portal : str
The name of the portal to add
desc : str
Description of the portal
Raises
------
QiitaDBDuplicateError
Portal name already exists
"""
if cls.exists(portal):
raise qdb.exceptions.QiitaDBDuplicateError("Portal", portal)
# Add portal and default analyses for all users
sql = """DO $do$
DECLARE
pid bigint;
eml varchar;
aid bigint;
BEGIN
INSERT INTO qiita.portal_type (portal, portal_description)
VALUES (%s, %s)
RETURNING portal_type_id INTO pid;
FOR eml IN
SELECT email FROM qiita.qiita_user
LOOP
INSERT INTO qiita.analysis
(email, name, description, dflt)
VALUES (eml, eml || '-dflt', 'dflt', true)
RETURNING analysis_id INTO aid;
INSERT INTO qiita.analysis_portal
(analysis_id, portal_type_id)
VALUES (aid, pid);
END LOOP;
END $do$;"""
qdb.sql_connection.perform_as_transaction(sql, [portal, desc])
return cls(portal)
@staticmethod
def delete(portal):
"""Removes a portal and its default analyses from the system
Parameters
----------
portal : str
The name of the portal to add
Raises
------
QiitaDBError
Portal has analyses or studies attached to it
"""
with qdb.sql_connection.TRN:
# Check if attached to any studies
portal_id = qdb.util.convert_to_id(portal, 'portal_type', 'portal')
sql = """SELECT study_id
FROM qiita.study_portal
WHERE portal_type_id = %s"""
qdb.sql_connection.TRN.add(sql, [portal_id])
studies = qdb.sql_connection.TRN.execute_fetchflatten()
if studies:
raise qdb.exceptions.QiitaDBError(
" Cannot delete portal '%s', studies still attached: %s" %
(portal, ', '.join(map(str, studies))))
# Check if attached to any analyses
sql = """SELECT analysis_id
FROM qiita.analysis_portal
JOIN qiita.analysis USING (analysis_id)
WHERE portal_type_id = %s AND dflt = FALSE"""
qdb.sql_connection.TRN.add(sql, [portal_id])
analyses = qdb.sql_connection.TRN.execute_fetchflatten()
if analyses:
raise qdb.exceptions.QiitaDBError(
" Cannot delete portal '%s', analyses still attached: %s" %
(portal, ', '.join(map(str, analyses))))
# Remove portal and default analyses for all users
sql = """DO $do$
DECLARE
aid bigint;
BEGIN
FOR aid IN
SELECT analysis_id
FROM qiita.analysis_portal
JOIN qiita.analysis USING (analysis_id)
WHERE portal_type_id = %s AND dflt = True
LOOP
DELETE FROM qiita.analysis_portal
WHERE analysis_id = aid;
DELETE FROM qiita.analysis_sample
WHERE analysis_id = aid;
DELETE FROM qiita.analysis
WHERE analysis_id = aid;
END LOOP;
DELETE FROM qiita.portal_type WHERE portal_type_id = %s;
END $do$;"""
qdb.sql_connection.TRN.add(sql, [portal_id] * 2)
qdb.sql_connection.TRN.execute()
@staticmethod
def exists(portal):
"""Returns whether the portal name already exists on the system
Parameters
----------
portal : str
Name of portal to check
Returns
-------
bool
Whether the portal exists or not
"""
try:
qdb.util.convert_to_id(portal, 'portal_type', 'portal')
except qdb.exceptions.QiitaDBLookupError:
return False
else:
return True
def get_studies(self):
"""Returns all studies belonging to the portal
Returns
-------
set of qiita_db.study.Study
All studies attached to the portal
"""
with qdb.sql_connection.TRN:
sql = """SELECT study_id
FROM qiita.study_portal
WHERE portal_type_id = %s"""
qdb.sql_connection.TRN.add(sql, [self._id])
return set(
qdb.study.Study(sid)
for sid in qdb.sql_connection.TRN.execute_fetchflatten())
def _check_studies(self, studies):
with qdb.sql_connection.TRN:
# Check if any study IDs given do not exist.
sql = "SELECT study_id FROM qiita.study WHERE study_id IN %s"
qdb.sql_connection.TRN.add(sql, [tuple(studies)])
existing = qdb.sql_connection.TRN.execute_fetchflatten()
if len(existing) != len(list(studies)):
bad = map(str, set(studies).difference(existing))
raise qdb.exceptions.QiitaDBError(
"The following studies do not exist: %s" % ", ".join(bad))
def add_studies(self, studies):
"""Adds studies to given portal
Parameters
----------
studies : iterable of int
Study ids to attach to portal
Raises
------
QiitaDBError
Some studies given do not exist in the system
QiitaDBWarning
Some studies already exist in the given portal
"""
with qdb.sql_connection.TRN:
self._check_studies(studies)
# Clean list of studies down to ones not associated
# with portal already
sql = """SELECT study_id
FROM qiita.study_portal
WHERE portal_type_id = %s AND study_id IN %s"""
qdb.sql_connection.TRN.add(sql, [self._id, tuple(studies)])
duplicates = qdb.sql_connection.TRN.execute_fetchflatten()
if len(duplicates) > 0:
warnings.warn(
"The following studies are already part of %s: %s"
% (self.portal, ', '.join(map(str, duplicates))),
qdb.exceptions.QiitaDBWarning)
# Add cleaned list to the portal
clean_studies = set(studies).difference(duplicates)
sql = """INSERT INTO qiita.study_portal (study_id, portal_type_id)
VALUES (%s, %s)"""
if len(clean_studies) != 0:
qdb.sql_connection.TRN.add(
sql, [[s, self._id] for s in clean_studies], many=True)
qdb.sql_connection.TRN.execute()
def remove_studies(self, studies):
"""Removes studies from given portal
Parameters
----------
studies : iterable of int
Study ids to remove from portal
Raises
------
ValueError
Trying to delete from QIITA portal
QiitaDBError
Some studies given do not exist in the system
Some studies are already used in an analysis on the portal
QiitaDBWarning
Some studies already do not exist in the given portal
"""
if self.portal == "QIITA":
raise ValueError('Can not remove from main QIITA portal!')
with qdb.sql_connection.TRN:
self._check_studies(studies)
# Make sure study not used in analysis in portal
sql = """SELECT DISTINCT study_id
FROM qiita.study_artifact
JOIN qiita.analysis_sample USING (artifact_id)
JOIN qiita.analysis_portal USING (analysis_id)
WHERE portal_type_id = %s AND study_id IN %s"""
qdb.sql_connection.TRN.add(sql, [self.id, tuple(studies)])
analysed = qdb.sql_connection.TRN.execute_fetchflatten()
if analysed:
raise qdb.exceptions.QiitaDBError(
"The following studies are used in an analysis on portal "
"%s and can't be removed: %s"
% (self.portal, ", ".join(map(str, analysed))))
# Clean list of studies down to ones associated with portal already
sql = """SELECT study_id
FROM qiita.study_portal
WHERE portal_type_id = %s AND study_id IN %s"""
qdb.sql_connection.TRN.add(sql, [self._id, tuple(studies)])
clean_studies = qdb.sql_connection.TRN.execute_fetchflatten()
if len(clean_studies) != len(studies):
rem = map(str, set(studies).difference(clean_studies))
warnings.warn(
"The following studies are not part of %s: %s"
% (self.portal, ', '.join(rem)),
qdb.exceptions.QiitaDBWarning)
sql = """DELETE FROM qiita.study_portal
WHERE study_id IN %s AND portal_type_id = %s"""
if len(clean_studies) != 0:
qdb.sql_connection.TRN.add(sql, [tuple(studies), self._id])
qdb.sql_connection.TRN.execute()
def get_analyses(self):
"""Returns all analyses belonging to a portal
Returns
-------
set of qiita_db.analysis.Analysis
All analyses belonging to the portal
"""
with qdb.sql_connection.TRN:
sql = """SELECT analysis_id
FROM qiita.analysis_portal
WHERE portal_type_id = %s"""
qdb.sql_connection.TRN.add(sql, [self._id])
return set(
qdb.analysis.Analysis(aid)
for aid in qdb.sql_connection.TRN.execute_fetchflatten())
def _check_analyses(self, analyses):
with qdb.sql_connection.TRN:
# Check if any analysis IDs given do not exist.
sql = """SELECT analysis_id
FROM qiita.analysis
WHERE analysis_id IN %s"""
qdb.sql_connection.TRN.add(sql, [tuple(analyses)])
existing = qdb.sql_connection.TRN.execute_fetchflatten()
if len(existing) != len(analyses):
bad = map(str, set(analyses).difference(existing))
raise qdb.exceptions.QiitaDBError(
"The following analyses do not exist: %s" % ", ".join(bad))
# Check if any analyses given are default
sql = """SELECT analysis_id
FROM qiita.analysis
WHERE analysis_id IN %s AND dflt = True"""
qdb.sql_connection.TRN.add(sql, [tuple(analyses)])
default = qdb.sql_connection.TRN.execute_fetchflatten()
if len(default) > 0:
bad = map(str, set(analyses).difference(default))
raise qdb.exceptions.QiitaDBError(
"The following analyses are default and can't be deleted "
"or assigned to another portal: %s" % ", ".join(bad))
def add_analyses(self, analyses):
"""Adds analyses to given portal
Parameters
----------
analyses : iterable of int
Analysis ids to attach to portal
Raises
------
QiitaDBError
Some given analyses do not exist in the system,
or are default analyses
Portal does not contain all studies used in analyses
QiitaDBWarning
Some analyses already exist in the given portal
"""
with qdb.sql_connection.TRN:
self._check_analyses(analyses)
if self.portal != "QIITA":
# Make sure new portal has access to all studies in analysis
sql = """SELECT DISTINCT analysis_id
FROM qiita.analysis_sample
JOIN qiita.study_artifact
USING (artifact_id)
WHERE study_id NOT IN (
SELECT study_id
FROM qiita.study_portal
WHERE portal_type_id = %s)
AND analysis_id IN %s
ORDER BY analysis_id"""
qdb.sql_connection.TRN.add(sql, [self._id, tuple(analyses)])
missing_info = qdb.sql_connection.TRN.execute_fetchflatten()
if missing_info:
raise qdb.exceptions.QiitaDBError(
"Portal %s is mising studies used in the following "
"analyses: %s"
% (self.portal, ", ".join(map(str, missing_info))))
# Clean list of analyses to ones not already associated with portal
sql = """SELECT analysis_id
FROM qiita.analysis_portal
JOIN qiita.analysis USING (analysis_id)
WHERE portal_type_id = %s AND analysis_id IN %s
AND dflt != TRUE"""
qdb.sql_connection.TRN.add(sql, [self._id, tuple(analyses)])
duplicates = qdb.sql_connection.TRN.execute_fetchflatten()
if len(duplicates) > 0:
warnings.warn(
"The following analyses are already part of %s: %s"
% (self.portal, ', '.join(map(str, duplicates))),
qdb.exceptions.QiitaDBWarning)
sql = """INSERT INTO qiita.analysis_portal
(analysis_id, portal_type_id)
VALUES (%s, %s)"""
clean_analyses = set(analyses).difference(duplicates)
if len(clean_analyses) != 0:
qdb.sql_connection.TRN.add(
sql, [[a, self._id] for a in clean_analyses], many=True)
qdb.sql_connection.TRN.execute()
def remove_analyses(self, analyses):
"""Removes analyses from given portal
Parameters
----------
analyses : iterable of int
Analysis ids to remove from portal
Raises
------
ValueError
Trying to delete from QIITA portal
QiitaDBWarning
Some analyses already do not exist in the given portal
"""
with qdb.sql_connection.TRN:
self._check_analyses(analyses)
if self.portal == "QIITA":
raise ValueError('Can not remove from main QIITA portal!')
# Clean list of analyses to ones already associated with portal
sql = """SELECT analysis_id
FROM qiita.analysis_portal
JOIN qiita.analysis USING (analysis_id)
WHERE portal_type_id = %s AND analysis_id IN %s
AND dflt != TRUE"""
qdb.sql_connection.TRN.add(sql, [self._id, tuple(analyses)])
clean_analyses = qdb.sql_connection.TRN.execute_fetchflatten()
if len(clean_analyses) != len(analyses):
rem = map(str, set(analyses).difference(clean_analyses))
warnings.warn(
"The following analyses are not part of %s: %s"
% (self.portal, ', '.join(rem)),
qdb.exceptions.QiitaDBWarning)
sql = """DELETE FROM qiita.analysis_portal
WHERE analysis_id IN %s AND portal_type_id = %s"""
if len(clean_analyses) != 0:
qdb.sql_connection.TRN.add(
sql, [tuple(clean_analyses), self._id])
qdb.sql_connection.TRN.execute()