Skip to content

Commit cd1dbeb

Browse files
authored
Some general cleaning and getting things in place to fix #3091 (#3165)
* add has_sample_info to generate_study_list * EXIST -> EXISTS * adding some more code
1 parent 7aa8a18 commit cd1dbeb

File tree

2 files changed

+97
-66
lines changed

2 files changed

+97
-66
lines changed

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,10 @@ class MetadataTemplate(qdb.base.QiitaObject):
453453
# sub-classes.
454454
_forbidden_words = {}
455455

456-
def _check_id(self, id_):
456+
@classmethod
457+
def _check_id(cls, id_):
457458
r"""Checks that the MetadataTemplate id_ exists on the database"""
458-
with qdb.sql_connection.TRN:
459-
sql = "SELECT EXISTS(SELECT * FROM qiita.{0} WHERE {1}=%s)".format(
460-
self._table, self._id_column)
461-
qdb.sql_connection.TRN.add(sql, [id_])
462-
return qdb.sql_connection.TRN.execute_fetchlast()
459+
return qdb.util.exists_table(f'{cls._table_prefix}{id_}')
463460

464461
@classmethod
465462
def _table_name(cls, obj_id):

qiita_db/util.py

Lines changed: 94 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ def exists_table(table):
369369
"""
370370
with qdb.sql_connection.TRN:
371371
sql = """SELECT exists(
372-
SELECT * FROM information_schema.tables
372+
SELECT table_name FROM information_schema.tables
373373
WHERE table_name=%s)"""
374374
qdb.sql_connection.TRN.add(sql, [table])
375375
return qdb.sql_connection.TRN.execute_fetchlast()
@@ -1464,6 +1464,10 @@ def generate_study_list(user, visibility):
14641464
(SELECT COUNT(sample_id) FROM qiita.study_sample
14651465
WHERE study_id=qiita.study.study_id)
14661466
AS number_samples_collected,
1467+
(SELECT EXISTS(
1468+
SELECT 1 FROM qiita.study_sample
1469+
WHERE study_id = qiita.study.study_id LIMIT 1))
1470+
AS has_sample_info,
14671471
(SELECT array_agg(row_to_json((prep_template_id, data_type,
14681472
artifact_id, artifact_type, deprecated,
14691473
qiita.bioms_from_preparation_artifacts(prep_template_id)),
@@ -1500,65 +1504,95 @@ def generate_study_list(user, visibility):
15001504
if sids:
15011505
with qdb.sql_connection.TRN:
15021506
qdb.sql_connection.TRN.add(sql, [tuple(sids)])
1503-
for info in qdb.sql_connection.TRN.execute_fetchindex():
1504-
info = dict(info)
1505-
1506-
# cleaning owners name
1507-
if info['owner'] in (None, ''):
1508-
info['owner'] = info['owner_email']
1509-
del info['owner_email']
1510-
1511-
preparation_data_types = []
1512-
artifact_biom_ids = []
1513-
if info['preparation_information'] is not None:
1514-
for pinfo in info['preparation_information']:
1515-
# 'f1': prep_template_id, 'f2': data_type,
1516-
# 'f3': artifact_id, 'f4': artifact_type,
1517-
# 'f5':deprecated, 'f6': biom artifacts
1518-
if pinfo['f5']:
1519-
continue
1520-
preparation_data_types.append(pinfo['f2'])
1521-
if pinfo['f4'] == 'BIOM':
1522-
artifact_biom_ids.append(pinfo['f3'])
1523-
if pinfo['f6'] is not None:
1524-
artifact_biom_ids.extend(
1525-
map(int, pinfo['f6'].split(',')))
1526-
del info['preparation_information']
1527-
info['artifact_biom_ids'] = list(set(artifact_biom_ids))
1528-
info['preparation_data_types'] = list(set(
1529-
preparation_data_types))
1530-
1531-
# publication info
1532-
info['publication_doi'] = []
1533-
info['publication_pid'] = []
1534-
if info['publications'] is not None:
1535-
for p in info['publications']:
1536-
# f1-2 are the default names given by pgsql
1537-
pub = p['f1']
1538-
is_doi = p['f2']
1539-
if is_doi:
1540-
info['publication_doi'].append(pub)
1541-
else:
1542-
info['publication_pid'].append(pub)
1543-
del info['publications']
1544-
1545-
# pi info
1546-
info["pi"] = (info['pi_email'], info['pi_name'])
1547-
del info["pi_email"]
1548-
del info["pi_name"]
1549-
1550-
# shared with
1551-
info['shared'] = []
1552-
if info['shared_with_name'] and info['shared_with_email']:
1553-
for name, email in zip(info['shared_with_name'],
1554-
info['shared_with_email']):
1555-
if not name:
1556-
name = email
1557-
info['shared'].append((email, name))
1558-
del info["shared_with_name"]
1559-
del info["shared_with_email"]
1560-
1561-
infolist.append(info)
1507+
results = qdb.sql_connection.TRN.execute_fetchindex()
1508+
1509+
for info in results:
1510+
info = dict(info)
1511+
1512+
# cleaning owners name
1513+
if info['owner'] in (None, ''):
1514+
info['owner'] = info['owner_email']
1515+
del info['owner_email']
1516+
1517+
preparation_data_types = []
1518+
artifact_biom_ids = []
1519+
if info['preparation_information'] is not None:
1520+
for pinfo in info['preparation_information']:
1521+
# 'f1': prep_template_id, 'f2': data_type,
1522+
# 'f3': artifact_id, 'f4': artifact_type,
1523+
# 'f5':deprecated, 'f6': biom artifacts
1524+
if pinfo['f5']:
1525+
continue
1526+
preparation_data_types.append(pinfo['f2'])
1527+
if pinfo['f4'] == 'BIOM':
1528+
artifact_biom_ids.append(pinfo['f3'])
1529+
if pinfo['f6'] is not None:
1530+
artifact_biom_ids.extend(
1531+
map(int, pinfo['f6'].split(',')))
1532+
del info['preparation_information']
1533+
info['artifact_biom_ids'] = list(set(artifact_biom_ids))
1534+
info['preparation_data_types'] = list(set(
1535+
preparation_data_types))
1536+
1537+
# publication info
1538+
info['publication_doi'] = []
1539+
info['publication_pid'] = []
1540+
if info['publications'] is not None:
1541+
for p in info['publications']:
1542+
# f1-2 are the default names given by pgsql
1543+
pub = p['f1']
1544+
is_doi = p['f2']
1545+
if is_doi:
1546+
info['publication_doi'].append(pub)
1547+
else:
1548+
info['publication_pid'].append(pub)
1549+
del info['publications']
1550+
1551+
# pi info
1552+
info["pi"] = (info['pi_email'], info['pi_name'])
1553+
del info["pi_email"]
1554+
del info["pi_name"]
1555+
1556+
# shared with
1557+
info['shared'] = []
1558+
if info['shared_with_name'] and info['shared_with_email']:
1559+
for name, email in zip(info['shared_with_name'],
1560+
info['shared_with_email']):
1561+
if not name:
1562+
name = email
1563+
info['shared'].append((email, name))
1564+
del info["shared_with_name"]
1565+
del info["shared_with_email"]
1566+
1567+
# # add extra info about sample information file
1568+
# if info['has_sample_info']:
1569+
# # the fix for #3091 should go here; please reference that
1570+
# # issue for more information of why it hasn't been closed
1571+
# with qdb.sql_connection.TRN:
1572+
# # check if host_scientific_name is part of the metadata
1573+
# BMT = qdb.metadata_template.base_metadata_template
1574+
# QCN = BMT.QIITA_COLUMN_NAME
1575+
# sql = """SELECT POSITION('host_scientific_name' IN
1576+
# sample_values->>'columns')
1577+
# FROM qiita.sample_%d
1578+
# WHERE sample_id = '%s'""" % (
1579+
# info['study_id'], QCN)
1580+
# qdb.sql_connection.TRN.add(sql)
1581+
# has_hsn = qdb.sql_connection.TRN.execute_fetchflatten()
1582+
# # if it has that column, we can retrieve the information
1583+
# if has_hsn[0] != 0:
1584+
# sql = """SELECT array_agg(
1585+
# DISTINCT
1586+
# sample_values->>'host_scientific_name')
1587+
# FROM qiita.sample_%d
1588+
# WHERE sample_id != '%s'""" % (
1589+
# info['study_id'], QCN))
1590+
# qdb.sql_connection.TRN.add(sql)
1591+
# hsn = qdb.sql_connection.TRN.execute_fetchflatten()
1592+
# info['host_scientific_name'] = hsn
1593+
del info['has_sample_info']
1594+
1595+
infolist.append(info)
15621596
return infolist
15631597

15641598

0 commit comments

Comments
 (0)