Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def all_data_types():
return qdb.sql_connection.TRN.execute_fetchflatten()

@classmethod
def get_by_status(cls, status):
def get_ids_by_status(cls, status):
"""Returns study id for all Studies with given status

Parameters
Expand All @@ -138,7 +138,7 @@ def get_by_status(cls, status):
JOIN qiita.portal_type USING (portal_type_id)
WHERE visibility = %s AND portal = %s"""
qdb.sql_connection.TRN.add(sql, [status, qiita_config.portal])
studies = set(qdb.sql_connection.TRN.execute_fetchflatten())
sids = set(qdb.sql_connection.TRN.execute_fetchflatten())
# If status is sandbox, all the studies that are not present in the
# study_artifact table are also sandbox
if status == 'sandbox':
Expand All @@ -150,10 +150,26 @@ def get_by_status(cls, status):
SELECT study_id
FROM qiita.study_artifact)"""
qdb.sql_connection.TRN.add(sql, [qiita_config.portal])
studies = studies.union(
sids = sids.union(
qdb.sql_connection.TRN.execute_fetchflatten())

return set(cls(sid) for sid in studies)
return sids

@classmethod
def get_by_status(cls, status):
"""Returns study id for all Studies with given status

Parameters
----------
status : str
Status setting to search for

Returns
-------
set of qiita_db.study.Study
All studies in the database that match the given status
"""
return set(cls(sid) for sid in cls.get_ids_by_status(status))

@classmethod
def get_info(cls, study_ids=None, info_cols=None):
Expand Down
167 changes: 130 additions & 37 deletions qiita_db/test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,11 @@ def test_get_pubmed_ids_from_dois(self):
self.assertEqual(obs, exp)

def test_generate_study_list(self):
USER = qdb.user.User
STUDY = qdb.study.Study
PREP = qdb.metadata_template.prep_template.PrepTemplate
UTIL = qdb.util

# creating a new study to make sure that empty studies are also
# returned
info = {"timeseries_type_id": 1, "metadata_complete": True,
Expand All @@ -868,46 +873,134 @@ def test_generate_study_list(self):
"emp_person_id": qdb.study.StudyPerson(1),
"principal_investigator_id": qdb.study.StudyPerson(1),
"lab_person_id": qdb.study.StudyPerson(1)}
new_study = qdb.study.Study.create(
qdb.user.User('shared@foo.bar'), 'test_study_1', info=info)

exp_info = [
{'status': 'private', 'study_title': (
'Identification of the Microbiomes for Cannabis Soils'),
'metadata_complete': True, 'publication_pid': [
'123456', '7891011'], 'artifact_biom_ids': [4, 5, 6, 7],
'ebi_submission_status': 'submitted', 'study_id': 1,
'ebi_study_accession': 'EBI123456-BB', 'owner': 'Dude',
'shared': [('shared@foo.bar', 'Shared')],
'study_abstract': (
new_study = STUDY.create(
USER('shared@foo.bar'), 'test_study_1', info=info)

sinfo = {
'study_id': 1,
'owner': 'Dude',
'study_alias': 'Cannabis Soils',
'status': 'private',
'study_abstract':
'This is a preliminary study to examine the microbiota '
'associated with the Cannabis plant. Soils samples from '
'the bulk soil, soil associated with the roots, and the '
'rhizosphere were extracted and the DNA sequenced. Roots '
'from three independent plants of different strains were '
'examined. These roots were obtained November 11, 2011 from '
'plants that had been harvested in the summer. Future studies '
'will attempt to analyze the soils and rhizospheres from the '
'same location at different time points in the plant '
'lifecycle.'), 'pi': ('PI_dude@foo.bar', 'PIDude'),
'publication_doi': ['10.100/123456', '10.100/7891011'],
'study_alias': 'Cannabis Soils', 'study_tags': None,
'number_samples_collected': 27},
{'status': 'sandbox', 'study_title': 'test_study_1',
'metadata_complete': True, 'publication_pid': [],
'artifact_biom_ids': None,
'ebi_submission_status': 'not submitted',
'study_id': new_study.id, 'ebi_study_accession': None,
'owner': 'Shared', 'shared': [],
'study_abstract': 'Some abstract goes here',
'pi': ('lab_dude@foo.bar', 'LabDude'), 'publication_doi': [],
'study_alias': 'TST', 'study_tags': None,
'number_samples_collected': 0}]
obs_info = qdb.util.generate_study_list([1, 2, 3, 4])
self.assertEqual(obs_info, exp_info)
'associated with the Cannabis plant. Soils samples '
'from the bulk soil, soil associated with the roots, '
'and the rhizosphere were extracted and the DNA '
'sequenced. Roots from three independent plants of '
'different strains were examined. These roots were '
'obtained November 11, 2011 from plants that had been '
'harvested in the summer. Future studies will attempt '
'to analyze the soils and rhizospheres from the same '
'location at different time points in the plant '
'lifecycle.',
'metadata_complete': True,
'ebi_study_accession': 'EBI123456-BB',
'ebi_submission_status': 'submitted',
'study_title':
'Identification of the Microbiomes for Cannabis Soils',
'number_samples_collected': 27,
'shared': [('shared@foo.bar', 'Shared')],
'publication_doi': ['10.100/123456', '10.100/7891011'],
'publication_pid': ['123456', '7891011'],
'pi': ('PI_dude@foo.bar', 'PIDude'),
'artifact_biom_ids': [4, 5, 6, 7],
'study_tags': None,
}
snew_info = {
'status': 'sandbox', 'study_title': 'test_study_1',
'metadata_complete': True, 'publication_pid': [],
'artifact_biom_ids': None,
'ebi_submission_status': 'not submitted',
'study_id': new_study.id, 'ebi_study_accession': None,
'owner': 'Shared', 'shared': [],
'study_abstract': 'Some abstract goes here',
'pi': ('lab_dude@foo.bar', 'LabDude'), 'publication_doi': [],
'study_alias': 'TST', 'study_tags': None,
'number_samples_collected': 0}
exp1 = [sinfo]
exp2 = [snew_info]
exp_both = [sinfo, snew_info]

# let's make sure that everything is private for stiudy 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stiudy -> study

for a in STUDY(1).artifacts():
a.visibility = 'private'

# owner of study
obs = UTIL.generate_study_list(USER('test@foo.bar'), 'user')
self.assertEqual(obs, exp1)
# shared with
obs = UTIL.generate_study_list(USER('shared@foo.bar'), 'user')
self.assertEqual(obs, exp_both)
# admin
obs = UTIL.generate_study_list(USER('admin@foo.bar'), 'user')
self.assertEqual(obs, exp_both)
# no access/hidden
obs = UTIL.generate_study_list(USER('demo@microbio.me'), 'user')
self.assertEqual(obs, [])
# public - none for everyone
obs = UTIL.generate_study_list(USER('test@foo.bar'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('shared@foo.bar'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('admin@foo.bar'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('demo@microbio.me'), 'public')
self.assertEqual(obs, [])

# deleting the old study
def _avoid_duplicated_tests(all_artifacts=False):
# nothing should shange for owner, shared
obs = UTIL.generate_study_list(USER('test@foo.bar'), 'user')
self.assertEqual(obs, exp1)
obs = UTIL.generate_study_list(USER('shared@foo.bar'), 'user')
self.assertEqual(obs, exp_both)
# for admin it should be shown in public and user cause there are
# 2 preps and only one is public
obs = UTIL.generate_study_list(USER('admin@foo.bar'), 'user')
if not all_artifacts:
self.assertEqual(obs, exp_both)
else:
self.assertEqual(obs, exp2)
obs = UTIL.generate_study_list(USER('demo@microbio.me'), 'user')
self.assertEqual(obs, [])
# for the public query, everything should be same for owner, share
# and admin but demo should now see it as public but with limited
# artifacts
obs = UTIL.generate_study_list(USER('test@foo.bar'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('shared@foo.bar'), 'public')
self.assertEqual(obs, [])
obs = UTIL.generate_study_list(USER('admin@foo.bar'), 'public')
if not all_artifacts:
exp1[0]['artifact_biom_ids'] = [7]
self.assertEqual(obs, exp1)
obs = UTIL.generate_study_list(USER('demo@microbio.me'), 'public')
self.assertEqual(obs, exp1)

# returning artifacts
exp1[0]['artifact_biom_ids'] = [4, 5, 6, 7]

# make artifacts of prep 2 public
PREP(2).artifact.visibility = 'public'
exp1[0]['status'] = 'public'
exp_both[0]['status'] = 'public'
_avoid_duplicated_tests()

# make artifacts of prep 1 awaiting_approval
PREP(1).artifact.visibility = 'awaiting_approval'
_avoid_duplicated_tests()

# making all studies public
PREP(1).artifact.visibility = 'public'
_avoid_duplicated_tests(True)

# deleting the new study study and returning artifact status
qdb.study.Study.delete(new_study.id)
PREP(1).artifact.visibility = 'private'
PREP(2).artifact.visibility = 'private'

def test_generate_study_list_errors(self):
with self.assertRaises(ValueError):
qdb.util.generate_study_list(qdb.user.User('test@foo.bar'), 'bad')

def test_generate_study_list_without_artifacts(self):
# creating a new study to make sure that empty studies are also
Expand Down
Loading