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
6 changes: 4 additions & 2 deletions qiita_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import meta_util
import ontology
import parameters
import portal
import reference
import search
import software
Expand All @@ -32,5 +33,6 @@

__all__ = ["analysis", "artifact", "base", "commands", "environment_manager",
"exceptions", "investigation", "job", "logger", "meta_util",
"ontology", "parameters", "reference", "search", "software",
"sql_connection", "study", "user", "util", "metadata_template"]
"ontology", "parameters", "portal", "reference", "search",
"software", "sql_connection", "study", "user", "util",
"metadata_template"]
31 changes: 18 additions & 13 deletions qiita_db/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,21 @@ def exists(portal):
return True

def get_studies(self):
"""Returns study id for all Studies belonging to the portal
"""Returns all studies belonging to the portal

Returns
-------
set of int
All study ids in the database that match the given portal
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
sql = """SELECT study_id
FROM qiita.study_portal
WHERE portal_type_id = %s"""
qdb.sql_connection.TRN.add(sql, [self._id])
return set(qdb.sql_connection.TRN.execute_fetchflatten())
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:
Expand Down Expand Up @@ -289,8 +292,8 @@ def remove_studies(self, studies):

# Make sure study not used in analysis in portal
sql = """SELECT DISTINCT study_id
FROM qiita.study_processed_data
JOIN qiita.analysis_sample USING (processed_data_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)])
Expand Down Expand Up @@ -322,19 +325,21 @@ def remove_studies(self, studies):
qdb.sql_connection.TRN.execute()

def get_analyses(self):
"""Returns analysis id for all Analyses belonging to a portal
"""Returns all analyses belonging to a portal

Returns
-------
set of int
All analysis ids in the database that match the given portal
set of qiita_db.analysis.Analysis
All analysis belonging to the portal
Copy link
Contributor

Choose a reason for hiding this comment

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

analysis -> analyses

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

"""
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.sql_connection.TRN.execute_fetchflatten())
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:
Expand Down Expand Up @@ -385,8 +390,8 @@ def add_analyses(self, analyses):
# Make sure new portal has access to all studies in analysis
sql = """SELECT DISTINCT analysis_id
FROM qiita.analysis_sample
JOIN qiita.study_processed_data
USING (processed_data_id)
JOIN qiita.study_artifact
USING (artifact_id)
WHERE study_id NOT IN (
SELECT study_id
FROM qiita.study_portal
Expand Down
7 changes: 4 additions & 3 deletions qiita_db/software.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,16 @@ def commands(self):

Returns
-------
list of int
The command identifiers
list of qiita_db.software.Command
The commands attached to this software package
"""
with qdb.sql_connection.TRN:
sql = """SELECT command_id
FROM qiita.software_command
WHERE software_id = %s"""
qdb.sql_connection.TRN.add(sql, [self.id])
return qdb.sql_connection.TRN.execute_fetchflatten()
return [Command(cid)
for cid in qdb.sql_connection.TRN.execute_fetchflatten()]

@property
def publications(self):
Expand Down
97 changes: 52 additions & 45 deletions qiita_db/test/test_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,30 @@
import numpy.testing as npt

from qiita_core.util import qiita_test_checker
from qiita_db.portal import Portal
from qiita_db.study import Study, StudyPerson
from qiita_db.user import User
from qiita_db.analysis import Analysis
from qiita_db.exceptions import (QiitaDBError, QiitaDBDuplicateError,
QiitaDBWarning, QiitaDBLookupError)
from qiita_core.qiita_settings import qiita_config
import qiita_db as qdb


@qiita_test_checker()
class TestPortal(TestCase):
portal = qiita_config.portal

def setUp(self):
self.study = Study(1)
self.analysis = Analysis(1)
self.qiita_portal = Portal('QIITA')
self.emp_portal = Portal('EMP')
self.study = qdb.study.Study(1)
self.analysis = qdb.analysis.Analysis(1)
self.qiita_portal = qdb.portal.Portal('QIITA')
self.emp_portal = qdb.portal.Portal('EMP')

def tearDown(self):
qiita_config.portal = self.portal

def test_list_portals(self):
obs = Portal.list_portals()
obs = qdb.portal.Portal.list_portals()
exp = ['EMP']
self.assertEqual(obs, exp)

def test_add_portal(self):
Portal.create("NEWPORTAL", "SOMEDESC")
qdb.portal.Portal.create("NEWPORTAL", "SOMEDESC")
obs = self.conn_handler.execute_fetchall(
"SELECT * FROM qiita.portal_type")
exp = [[1, 'QIITA', 'QIITA portal. Access to all data stored '
Expand All @@ -46,17 +41,17 @@ def test_add_portal(self):
[9, 2], [10, 2], [11, 4], [12, 4], [13, 4], [14, 4]]
self.assertItemsEqual(obs, exp)

with self.assertRaises(QiitaDBDuplicateError):
Portal.create("EMP", "DOESNTMATTERFORDESC")
with self.assertRaises(qdb.exceptions.QiitaDBDuplicateError):
qdb.portal.Portal.create("EMP", "DOESNTMATTERFORDESC")

def test_remove_portal(self):
Portal.create("NEWPORTAL", "SOMEDESC")
qdb.portal.Portal.create("NEWPORTAL", "SOMEDESC")
# Select some samples on a default analysis
qiita_config.portal = "NEWPORTAL"
a = Analysis(User("test@foo.bar").default_analysis)
a = qdb.user.User("test@foo.bar").default_analysis
a.add_samples({1: ['1.SKB8.640193', '1.SKD5.640186']})

Portal.delete("NEWPORTAL")
qdb.portal.Portal.delete("NEWPORTAL")
obs = self.conn_handler.execute_fetchall(
"SELECT * FROM qiita.portal_type")
exp = [[1, 'QIITA', 'QIITA portal. Access to all data stored '
Expand All @@ -70,18 +65,19 @@ def test_remove_portal(self):
[9, 2], [10, 2]]
self.assertItemsEqual(obs, exp)

with self.assertRaises(QiitaDBLookupError):
Portal.delete("NOEXISTPORTAL")
with self.assertRaises(QiitaDBError):
Portal.delete("QIITA")
with self.assertRaises(qdb.exceptions.QiitaDBLookupError):
qdb.portal.Portal.delete("NOEXISTPORTAL")
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.portal.Portal.delete("QIITA")

Portal.create("NEWPORTAL2", "SOMEDESC")
qdb.portal.Portal.create("NEWPORTAL2", "SOMEDESC")
# Add analysis to this new portal and make sure error raised
qiita_config.portal = "NEWPORTAL2"
Analysis.create(User("test@foo.bar"), "newportal analysis", "desc")
qdb.analysis.Analysis.create(
qdb.user.User("test@foo.bar"), "newportal analysis", "desc")
qiita_config.portal = "QIITA"
with self.assertRaises(QiitaDBError):
Portal.delete("NEWPORTAL2")
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.portal.Portal.delete("NEWPORTAL2")

# Add study to this new portal and make sure error raised
info = {
Expand All @@ -95,43 +91,45 @@ def test_remove_portal(self):
"fried chicken",
"study_abstract": "Exploring how a high fat diet changes the "
"gut microbiome",
"emp_person_id": StudyPerson(2),
"principal_investigator_id": StudyPerson(3),
"lab_person_id": StudyPerson(1)
"emp_person_id": qdb.study.StudyPerson(2),
"principal_investigator_id": qdb.study.StudyPerson(3),
"lab_person_id": qdb.study.StudyPerson(1)
}
Portal.create("NEWPORTAL3", "SOMEDESC")
qdb.portal.Portal.create("NEWPORTAL3", "SOMEDESC")
qiita_config.portal = "NEWPORTAL3"
Study.create(User('test@foo.bar'), "Fried chicken microbiome",
[1], info)
qdb.study.Study.create(
qdb.user.User('test@foo.bar'), "Fried chicken microbiome",
[1], info)
qiita_config.portal = "QIITA"
with self.assertRaises(QiitaDBError):
Portal.delete("NEWPORTAL3")
with self.assertRaises(qdb.exceptions.QiitaDBError):
qdb.portal.Portal.delete("NEWPORTAL3")

def test_check_studies(self):
with self.assertRaises(QiitaDBError):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.qiita_portal._check_studies([2000000000000, 122222222222222])

def test_check_analyses(self):
with self.assertRaises(QiitaDBError):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.qiita_portal._check_analyses([2000000000000, 122222222222222])

with self.assertRaises(QiitaDBError):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.qiita_portal._check_analyses([8, 9])

def test_get_studies_by_portal(self):
obs = self.emp_portal.get_studies()
self.assertEqual(obs, set())

obs = self.qiita_portal.get_studies()
self.assertEqual(obs, {1})
self.assertEqual(obs, {qdb.study.Study(1)})

def test_add_study_portals(self):
self.emp_portal.add_studies([self.study.id])
obs = self.study._portals
self.assertEqual(obs, ['EMP', 'QIITA'])

obs = npt.assert_warns(
QiitaDBWarning, self.emp_portal.add_studies, [self.study.id])
qdb.exceptions.QiitaDBWarning, self.emp_portal.add_studies,
[self.study.id])

def test_remove_study_portals(self):
with self.assertRaises(ValueError):
Expand All @@ -144,7 +142,7 @@ def test_remove_study_portals(self):
self.assertItemsEqual(obs, ['QIITA', 'EMP'])

# Test study removal failure
with self.assertRaises(QiitaDBError):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.emp_portal.remove_studies([self.study.id])
obs = self.study._portals
self.assertItemsEqual(obs, ['QIITA', 'EMP'])
Expand All @@ -156,19 +154,27 @@ def test_remove_study_portals(self):
self.assertEqual(obs, ['QIITA'])

obs = npt.assert_warns(
QiitaDBWarning, self.emp_portal.remove_studies, [self.study.id])
qdb.exceptions.QiitaDBWarning, self.emp_portal.remove_studies,
[self.study.id])

def test_get_analyses_by_portal(self):
qiita_config.portal = 'EMP'
exp = {qdb.analysis.Analysis(7), qdb.analysis.Analysis(8),
qdb.analysis.Analysis(9), qdb.analysis.Analysis(10)}
obs = self.emp_portal.get_analyses()
self.assertEqual(obs, {7, 8, 9, 10})
self.assertEqual(obs, exp)

qiita_config.portal = 'QIITA'
exp = {qdb.analysis.Analysis(1), qdb.analysis.Analysis(2),
qdb.analysis.Analysis(3), qdb.analysis.Analysis(4),
qdb.analysis.Analysis(5), qdb.analysis.Analysis(6)}
obs = self.qiita_portal.get_analyses()
self.assertEqual(obs, {1, 2, 3, 4, 5, 6})
self.assertEqual(obs, exp)

def test_add_analysis_portals(self):
obs = self.analysis._portals
self.assertEqual(obs, ['QIITA'])
with self.assertRaises(QiitaDBError):
with self.assertRaises(qdb.exceptions.QiitaDBError):
self.emp_portal.add_analyses([self.analysis.id])
obs = self.analysis._portals
self.assertEqual(obs, ['QIITA'])
Expand All @@ -179,7 +185,8 @@ def test_add_analysis_portals(self):
self.assertEqual(obs, ['EMP', 'QIITA'])

obs = npt.assert_warns(
QiitaDBWarning, self.emp_portal.add_analyses, [self.analysis.id])
qdb.exceptions.QiitaDBWarning, self.emp_portal.add_analyses,
[self.analysis.id])

def test_remove_analysis_portals(self):
with self.assertRaises(ValueError):
Expand All @@ -196,7 +203,7 @@ def test_remove_analysis_portals(self):
self.assertEqual(obs, ['QIITA'])

obs = npt.assert_warns(
QiitaDBWarning, self.emp_portal.remove_analyses,
qdb.exceptions.QiitaDBWarning, self.emp_portal.remove_analyses,
[self.analysis.id])


Expand Down
Loading