def _build_study_info(studytype, user=None): """builds list of namedtuples for study listings""" if studytype == "private": studylist = user.user_studies elif studytype == "shared": studylist = user.shared_studies elif studytype == "public": studylist = Study.get_by_status('public') else: raise IncompetentQiitaDeveloperError("Must use private, shared, " "or public!") StudyTuple = namedtuple('StudyInfo', 'id title meta_complete ' 'num_samples_collected shared num_raw_data pi ' 'pmids owner status') infolist = [] for s_id in studylist: study = Study(s_id) status = study.status # Just passing the email address as the name here, since # name is not a required field in qiita.qiita_user owner = study_person_linkifier((study.owner, study.owner)) info = study.info PI = StudyPerson(info['principal_investigator_id']) PI = study_person_linkifier((PI.email, PI.name)) pmids = ", ".join([pubmed_linkifier([pmid]) for pmid in study.pmids]) shared = _get_shared_links_for_study(study) infolist.append(StudyTuple(study.id, study.title, info["metadata_complete"], info["number_samples_collected"], shared, len(study.raw_data()), PI, pmids, owner, status)) return infolist
def _build_study_info(studytype, user=None): """builds list of namedtuples for study listings""" if studytype == "private": studylist = user.user_studies elif studytype == "shared": studylist = user.shared_studies elif studytype == "public": studylist = Study.get_by_status('public') else: raise IncompetentQiitaDeveloperError("Must use private, shared, " "or public!") StudyTuple = namedtuple( 'StudyInfo', 'id title meta_complete ' 'num_samples_collected shared num_raw_data pi ' 'pmids owner status') infolist = [] for s_id in studylist: study = Study(s_id) status = study.status # Just passing the email address as the name here, since # name is not a required field in qiita.qiita_user owner = study_person_linkifier((study.owner, study.owner)) info = study.info PI = StudyPerson(info['principal_investigator_id']) PI = study_person_linkifier((PI.email, PI.name)) pmids = ", ".join([pubmed_linkifier([pmid]) for pmid in study.pmids]) shared = _get_shared_links_for_study(study) infolist.append( StudyTuple(study.id, study.title, info["metadata_complete"], info["number_samples_collected"], shared, len(study.raw_data()), PI, pmids, owner, status)) return infolist
def _get_accessible_raw_data(user): """Retrieves a tuple of raw_data_id and one study title for that raw_data """ d = {} accessible_studies = user.user_studies.union(user.shared_studies) for sid in accessible_studies: study = Study(sid) study_title = study.title for rdid in study.raw_data(): d[int(rdid)] = study_title return d
class TestStudy(TestCase): def setUp(self): self.study = Study(1) self.portal = qiita_config.portal self.info = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "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) } self.infoexp = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "fried chicken", "study_abstract": "Exploring how a high fat diet changes the " "gut microbiome", "emp_person_id": 2, "principal_investigator_id": 3, "lab_person_id": 1 } self.existingexp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'number_samples_promised': 27, 'emp_person_id': StudyPerson(2), 'funding': None, 'vamps_id': None, 'first_contact': datetime(2014, 5, 19, 16, 10), 'principal_investigator_id': StudyPerson(3), 'timeseries_type_id': 1, '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.", 'spatial_series': False, 'study_description': 'Analysis of the Cannabis Plant Microbiome', 'study_alias': 'Cannabis Soils', 'most_recent_contact': '2014-05-19 16:11', 'most_recent_contact': datetime(2014, 5, 19, 16, 11), 'lab_person_id': StudyPerson(1), 'number_samples_collected': 27} def tearDown(self): qiita_config.portal = self.portal def _change_processed_data_status(self, new_status): # Change the status of the studies by changing the status of their # processed data id_status = convert_to_id(new_status, 'processed_data_status') self.conn_handler.execute( "UPDATE qiita.processed_data SET processed_data_status_id = %s", (id_status,)) def test_get_info(self): # Test get all info for single study qiita_config.portal = 'QIITA' obs = Study.get_info([1]) self.assertEqual(len(obs), 1) obs = dict(obs[0]) exp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'timeseries_type': 'None', 'number_samples_promised': 27, 'emp_person_id': 2, 'funding': None, 'vamps_id': None, 'first_contact': datetime(2014, 5, 19, 16, 10), 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'pmid': ['123456', '7891011'], 'study_alias': 'Cannabis Soils', 'spatial_series': False, '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.', 'study_description': 'Analysis of the Cannabis Plant Microbiome', 'intervention_type': 'None', 'email': '*****@*****.**', 'study_id': 1, 'most_recent_contact': datetime(2014, 5, 19, 16, 11), 'lab_person_id': 1, 'study_title': 'Identification of the Microbiomes for Cannabis ' 'Soils', 'number_samples_collected': 27} self.assertItemsEqual(obs, exp) # Test get specific keys for single study exp_keys = ['metadata_complete', 'reprocess', 'timeseries_type', 'pmid', 'study_title'] obs = Study.get_info([1], exp_keys) self.assertEqual(len(obs), 1) obs = dict(obs[0]) exp = { 'metadata_complete': True, 'reprocess': False, 'timeseries_type': 'None', 'pmid': ['123456', '7891011'], 'study_title': 'Identification of the Microbiomes for Cannabis ' 'Soils'} self.assertItemsEqual(obs, exp) # Test get specific keys for all studies info = { 'timeseries_type_id': 1, 'lab_person_id': None, 'principal_investigator_id': 3, 'metadata_complete': False, 'mixs_compliant': True, 'study_description': 'desc', 'study_alias': 'alias', 'study_abstract': 'abstract'} user = User('*****@*****.**') Study.create(user, 'test_study_1', efo=[1], info=info) obs = Study.get_info(info_cols=exp_keys) exp = [[True, ['123456', '7891011'], False, 'Identification of the Microbiomes for Cannabis Soils', 'None'], [False, None, False, 'test_study_1', 'None']] self.assertEqual(obs, exp) # test portal restriction working qiita_config.portal = 'EMP' with self.assertRaises(QiitaDBError): Study.get_info([1]) def test_has_access_public(self): self._change_processed_data_status('public') qiita_config.portal = 'QIITA' self.assertTrue(self.study.has_access(User("*****@*****.**"))) qiita_config.portal = 'EMP' with self.assertRaises(QiitaDBError): Study(1).has_access(User("*****@*****.**")) def test_has_access_no_public(self): self._change_processed_data_status('public') self.assertFalse(self.study.has_access(User("*****@*****.**"), True)) def test_owner(self): self.assertEqual(self.study.owner, "*****@*****.**") def test_share(self): # Clear all sharing associations self._change_processed_data_status('sandbox') self.conn_handler.execute("delete from qiita.study_users") self.assertEqual(self.study.shared_with, []) # Try to share with the owner, which should not work self.study.share(User("*****@*****.**")) self.assertEqual(self.study.shared_with, []) # Then share the study with [email protected] self.study.share(User("*****@*****.**")) self.assertEqual(self.study.shared_with, ["*****@*****.**"]) def test_unshare(self): self._change_processed_data_status('sandbox') self.study.unshare(User("*****@*****.**")) self.assertEqual(self.study.shared_with, []) def test_has_access_shared(self): self._change_processed_data_status('sandbox') self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_private(self): self._change_processed_data_status('sandbox') self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_admin(self): self._change_processed_data_status('sandbox') self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_no_access(self): self._change_processed_data_status('sandbox') self.assertFalse(self.study.has_access(User("*****@*****.**"))) def test_get_by_status(self): obs = Study.get_by_status('sandbox') self.assertEqual(obs, set()) Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) obs = Study.get_by_status('private') self.assertEqual(obs, {1}) obs = Study.get_by_status('sandbox') self.assertEqual(obs, {2}) obs = Study.get_by_status('public') self.assertEqual(obs, set()) obs = Study.get_by_status('awaiting_approval') self.assertEqual(obs, set()) def test_exists(self): self.assertTrue(Study.exists('Identification of the Microbiomes for ' 'Cannabis Soils')) self.assertFalse(Study.exists('Not Cannabis Soils')) def test_create_duplicate(self): with self.assertRaises(QiitaDBDuplicateError): Study.create( User('*****@*****.**'), 'Identification of the Microbiomes for Cannabis Soils', [1], self.info) def test_create_study_min_data(self): """Insert a study into the database""" before = datetime.now() obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) after = datetime.now() self.assertEqual(obs.id, 2) exp = {'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': None, 'vamps_id': None, 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': None, 'study_description': 'Microbiome of people who eat nothing but' ' fried chicken', 'study_alias': 'FCM', 'study_id': 2, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25} obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 2") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) # Check the timestamp separately, since it is set by the database # to the microsecond, and we can't predict it a priori ins_timestamp = obsins.pop('first_contact') self.assertTrue(before < ins_timestamp < after) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly efo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 2") self.assertEqual(efo, [[1]]) def test_create_nonqiita_portal(self): qiita_config.portal = "EMP" Study.create(User('*****@*****.**'), "NEW!", [1], self.info, Investigation(1)) # make sure portal is associated obs = self.conn_handler.execute_fetchall( "SELECT * from qiita.study_portal WHERE study_id = 2") self.assertEqual(obs, [[2, 2], [2, 1]]) def test_create_study_with_investigation(self): """Insert a study into the database with an investigation""" obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info, Investigation(1)) self.assertEqual(obs.id, 2) # check the investigation was assigned obs = self.conn_handler.execute_fetchall( "SELECT * from qiita.investigation_study WHERE study_id = 2") self.assertEqual(obs, [[1, 2]]) def test_create_study_all_data(self): """Insert a study into the database with every info field""" self.info.update({ 'vamps_id': 'MBE_1111111', 'funding': 'FundAgency', 'spatial_series': True, 'metadata_complete': False, 'reprocess': True, 'first_contact': "10/24/2014 12:47PM", 'study_id': 3827 }) obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) self.assertEqual(obs.id, 3827) exp = {'mixs_compliant': True, 'metadata_complete': False, 'reprocess': True, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': 'FundAgency', 'vamps_id': 'MBE_1111111', 'first_contact': datetime(2014, 10, 24, 12, 47), 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': True, 'study_description': 'Microbiome of people who eat nothing ' 'but fried chicken', 'study_alias': 'FCM', 'study_id': 3827, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25} obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 3827") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly obsefo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 3827") self.assertEqual(obsefo, [[1]]) def test_create_missing_required(self): """ Insert a study that is missing a required info key""" self.info.pop("study_alias") with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_empty_efo(self): """ Insert a study that is missing a required info key""" with self.assertRaises(IncompetentQiitaDeveloperError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [], self.info) def test_create_study_with_not_allowed_key(self): """Insert a study with key from _non_info present""" self.info.update({"email": "*****@*****.**"}) with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_unknown_db_col(self): """ Insert a study with an info key not in the database""" self.info["SHOULDNOTBEHERE"] = "BWAHAHAHAHAHA" with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_delete(self): title = "Fried chicken microbiome" # the study is assigned to investigation 1 study = Study.create(User('*****@*****.**'), title, [1], self.info, Investigation(1)) # sharing with other user study.share(User("*****@*****.**")) study.delete(study.id) self.assertFalse(study.exists(title)) with self.assertRaises(QiitaDBError): Study.delete(1) with self.assertRaises(QiitaDBUnknownIDError): Study.delete(41) def test_retrieve_title(self): self.assertEqual(self.study.title, 'Identification of the Microbiomes' ' for Cannabis Soils') def test_set_title(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.title = "Cannabis soils" self.assertEqual(new.title, "Cannabis soils") def test_get_efo(self): self.assertEqual(self.study.efo, [1]) def test_set_efo(self): """Set efo with list efo_id""" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.efo = [3, 4] self.assertEqual(new.efo, [3, 4]) def test_set_efo_empty(self): """Set efo with list efo_id""" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.efo = [] def test_set_efo_public(self): """Set efo on a public study""" with self.assertRaises(QiitaDBStatusError): self.study.efo = 6 def test_portals(self): self.assertEqual(self.study._portals, ['QIITA']) def test_retrieve_info(self): for key, val in viewitems(self.existingexp): if isinstance(val, QiitaObject): self.existingexp[key] = val.id self.assertEqual(self.study.info, self.existingexp) def test_set_info(self): """Set info in a study""" newinfo = { "timeseries_type_id": 2, "metadata_complete": False, "number_samples_collected": 28, "lab_person_id": StudyPerson(2), "vamps_id": 'MBE_111222', } self.info['first_contact'] = "6/11/2014" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.infoexp.update(newinfo) new.info = newinfo # add missing table cols self.infoexp["funding"] = None self.infoexp["spatial_series"] = None self.infoexp["most_recent_contact"] = None self.infoexp["reprocess"] = False self.infoexp["lab_person_id"] = 2 self.infoexp["first_contact"] = datetime(2014, 6, 11) self.assertEqual(new.info, self.infoexp) def test_set_info_public(self): """Tests for fail if editing info of a public study""" self.study.info = {"vamps_id": "12321312"} def test_set_info_public_error(self): """Tests for fail if trying to modify timeseries of a public study""" with self.assertRaises(QiitaDBStatusError): self.study.info = {"timeseries_type_id": 2} def test_set_info_disallowed_keys(self): """Tests for fail if sending non-info keys in info dict""" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(QiitaDBColumnError): new.info = {"email": "*****@*****.**"} def test_info_empty(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.info = {} def test_retrieve_status(self): self.assertEqual(self.study.status, "private") def test_retrieve_shared_with(self): self.assertEqual(self.study.shared_with, ['*****@*****.**']) def test_retrieve_pmids(self): exp = ['123456', '7891011'] self.assertEqual(self.study.pmids, exp) def test_retrieve_pmids_empty(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.pmids, []) def test_pmids_setter(self): exp = ['123456', '7891011'] self.assertEqual(self.study.pmids, exp) new_values = ['654321', '1101987'] self.study.pmids = new_values self.assertEqual(self.study.pmids, new_values) def test_pmids_setter_typeerror(self): with self.assertRaises(TypeError): self.study.pmids = '123456' def test_retrieve_investigation(self): self.assertEqual(self.study.investigation, 1) def test_retrieve_investigation_empty(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.investigation, None) def test_retrieve_sample_template(self): self.assertEqual(self.study.sample_template, 1) def test_retrieve_data_types(self): self.assertEqual(self.study.data_types, ['18S']) def test_retrieve_data_types_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.data_types, []) def test_retrieve_raw_data(self): self.assertEqual(self.study.raw_data(), [1]) def test_retrieve_raw_data_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.raw_data(), []) def test_retrieve_prep_templates(self): self.assertEqual(self.study.prep_templates(), [1]) def test_retrieve_prep_templates_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.prep_templates(), []) def test_retrieve_preprocessed_data(self): self.assertEqual(self.study.preprocessed_data(), [1, 2]) def test_retrieve_preprocessed_data_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.preprocessed_data(), []) def test_retrieve_processed_data(self): self.assertEqual(self.study.processed_data(), [1]) def test_retrieve_processed_data_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.processed_data(), []) def test_add_pmid(self): self._change_processed_data_status('sandbox') self.study.add_pmid('4544444') exp = ['123456', '7891011', '4544444'] self.assertEqual(self.study.pmids, exp) def test_environmental_packages(self): obs = self.study.environmental_packages exp = ['soil', 'plant-associated'] self.assertEqual(sorted(obs), sorted(exp)) def test_environmental_packages_setter(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) obs = new.environmental_packages exp = [] self.assertEqual(obs, exp) new_values = ['air', 'human-oral'] new.environmental_packages = new_values obs = new.environmental_packages self.assertEqual(sorted(obs), sorted(new_values)) def test_environmental_packages_setter_typeerror(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(TypeError): new.environmental_packages = 'air' def test_environmental_packages_setter_valueerror(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(ValueError): new.environmental_packages = ['air', 'not a package'] def test_environmental_packages_sandboxed(self): with self.assertRaises(QiitaDBStatusError): self.study.environmental_packages = ['air']
class TestStudy(TestCase): def setUp(self): self.study = Study(1) self.info = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "portal_type_id": 3, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "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) } self.infoexp = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "portal_type_id": 3, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "fried chicken", "study_abstract": "Exploring how a high fat diet changes the " "gut microbiome", "emp_person_id": 2, "principal_investigator_id": 3, "lab_person_id": 1 } self.existingexp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'number_samples_promised': 27, 'emp_person_id': StudyPerson(2), 'funding': None, 'vamps_id': None, 'first_contact': datetime(2014, 5, 19, 16, 10), 'principal_investigator_id': StudyPerson(3), 'timeseries_type_id': 1, '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.", 'spatial_series': False, 'study_description': 'Analysis of the Cannabis Plant Microbiome', 'portal_type_id': 2, 'study_alias': 'Cannabis Soils', 'most_recent_contact': '2014-05-19 16:11', 'most_recent_contact': datetime(2014, 5, 19, 16, 11), 'lab_person_id': StudyPerson(1), 'number_samples_collected': 27 } def _make_private(self): # make studies private self.conn_handler.execute("UPDATE qiita.study SET study_status_id = 4") def _make_sandbox(self): # make studies private self.conn_handler.execute("UPDATE qiita.study SET study_status_id = 1") def test_has_access_public(self): self.study.status = 'public' self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_no_public(self): self.study.status = 'public' self.assertFalse(self.study.has_access(User("*****@*****.**"), True)) def test_owner(self): self.assertEqual(self.study.owner, "*****@*****.**") def test_share(self): # Clear all sharing associations self._make_private() self.conn_handler.execute("delete from qiita.study_users") self.assertEqual(self.study.shared_with, []) # Try to share with the owner, which should not work self.study.share(User("*****@*****.**")) self.assertEqual(self.study.shared_with, []) # Then share the study with [email protected] self.study.share(User("*****@*****.**")) self.assertEqual(self.study.shared_with, ["*****@*****.**"]) def test_unshare(self): self._make_private() self.study.unshare(User("*****@*****.**")) self.assertEqual(self.study.shared_with, []) def test_has_access_shared(self): self._make_private() self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_private(self): self._make_private() self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_admin(self): self._make_private() self.assertTrue(self.study.has_access(User("*****@*****.**"))) def test_has_access_no_access(self): self._make_private() self.assertFalse(self.study.has_access(User("*****@*****.**"))) def test_get_by_status(self): Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) obs = Study.get_by_status('private') self.assertEqual(obs, [1]) def test_exists(self): self.assertTrue( Study.exists('Identification of the Microbiomes for ' 'Cannabis Soils')) self.assertFalse(Study.exists('Not Cannabis Soils')) def test_create_study_min_data(self): """Insert a study into the database""" before = datetime.now() obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) after = datetime.now() self.assertEqual(obs.id, 2) exp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'study_status_id': 4, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': None, 'vamps_id': None, 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': None, 'study_description': 'Microbiome of people who eat nothing but' ' fried chicken', 'portal_type_id': 3, 'study_alias': 'FCM', 'study_id': 2, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25 } obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 2") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) # Check the timestamp separately, since it is set by the database # to the microsecond, and we can't predict it a priori ins_timestamp = obsins.pop('first_contact') self.assertTrue(before < ins_timestamp < after) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly efo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 2") self.assertEqual(efo, [[1]]) def test_create_study_with_investigation(self): """Insert a study into the database with an investigation""" obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info, Investigation(1)) self.assertEqual(obs.id, 2) # check the investigation was assigned obs = self.conn_handler.execute_fetchall( "SELECT * from qiita.investigation_study WHERE study_id = 2") self.assertEqual(obs, [[1, 2]]) def test_create_study_all_data(self): """Insert a study into the database with every info field""" self.info.update({ 'vamps_id': 'MBE_1111111', 'funding': 'FundAgency', 'spatial_series': True, 'metadata_complete': False, 'reprocess': True, 'first_contact': "10/24/2014 12:47PM", 'study_id': 3827 }) obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) self.assertEqual(obs.id, 3827) exp = { 'mixs_compliant': True, 'metadata_complete': False, 'reprocess': True, 'study_status_id': 4, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': 'FundAgency', 'vamps_id': 'MBE_1111111', 'first_contact': datetime(2014, 10, 24, 12, 47), 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': True, 'study_description': 'Microbiome of people who eat nothing ' 'but fried chicken', 'portal_type_id': 3, 'study_alias': 'FCM', 'study_id': 3827, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25 } obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 3827") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly obsefo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 3827") self.assertEqual(obsefo, [[1]]) def test_create_missing_required(self): """ Insert a study that is missing a required info key""" self.info.pop("study_alias") with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_empty_efo(self): """ Insert a study that is missing a required info key""" with self.assertRaises(IncompetentQiitaDeveloperError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [], self.info) def test_create_study_with_not_allowed_key(self): """Insert a study with key from _non_info present""" self.info.update({"email": "*****@*****.**"}) with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_unknown_db_col(self): """ Insert a study with an info key not in the database""" self.info["SHOULDNOTBEHERE"] = "BWAHAHAHAHAHA" with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_retrieve_title(self): self.assertEqual( self.study.title, 'Identification of the Microbiomes' ' for Cannabis Soils') def test_set_title(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.title = "Cannabis soils" self.assertEqual(new.title, "Cannabis soils") def test_get_efo(self): self.assertEqual(self.study.efo, [1]) def test_set_efo(self): """Set efo with list efo_id""" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.efo = [3, 4] self.assertEqual(new.efo, [3, 4]) def test_set_efo_empty(self): """Set efo with list efo_id""" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.efo = [] def test_set_efo_public(self): """Set efo on a public study""" with self.assertRaises(QiitaDBStatusError): self.study.efo = 6 def test_retrieve_info(self): for key, val in viewitems(self.existingexp): if isinstance(val, QiitaObject): self.existingexp[key] = val.id self.assertEqual(self.study.info, self.existingexp) def test_set_info(self): """Set info in a study""" newinfo = { "timeseries_type_id": 2, "metadata_complete": False, "number_samples_collected": 28, "lab_person_id": StudyPerson(2), "vamps_id": 'MBE_111222', } self.info['first_contact'] = "6/11/2014" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.infoexp.update(newinfo) new.info = newinfo # add missing table cols self.infoexp["funding"] = None self.infoexp["spatial_series"] = None self.infoexp["most_recent_contact"] = None self.infoexp["reprocess"] = False self.infoexp["lab_person_id"] = 2 self.infoexp["first_contact"] = datetime(2014, 6, 11) self.assertEqual(new.info, self.infoexp) def test_set_info_public(self): """Tests for fail if editing info of a public study""" self.study.info = {"vamps_id": "12321312"} def test_set_info_public_error(self): """Tests for fail if trying to modify timeseries of a public study""" with self.assertRaises(QiitaDBStatusError): self.study.info = {"timeseries_type_id": 2} def test_set_info_disallowed_keys(self): """Tests for fail if sending non-info keys in info dict""" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(QiitaDBColumnError): new.info = {"email": "*****@*****.**"} def test_info_empty(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.info = {} def test_retrieve_status(self): self.assertEqual(self.study.status, "private") def test_set_status(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.status = "private" self.assertEqual(new.status, "private") def test_retrieve_shared_with(self): self.assertEqual(self.study.shared_with, ['*****@*****.**']) def test_retrieve_pmids(self): exp = ['123456', '7891011'] self.assertEqual(self.study.pmids, exp) def test_retrieve_pmids_empty(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.pmids, []) def test_pmids_setter(self): exp = ['123456', '7891011'] self.assertEqual(self.study.pmids, exp) new_values = ['654321', '1101987'] self.study.pmids = new_values self.assertEqual(self.study.pmids, new_values) def test_pmids_setter_typeerror(self): with self.assertRaises(TypeError): self.study.pmids = '123456' def test_retrieve_investigation(self): self.assertEqual(self.study.investigation, 1) def test_retrieve_investigation_empty(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.investigation, None) def test_retrieve_sample_template(self): self.assertEqual(self.study.sample_template, 1) def test_retrieve_data_types(self): self.assertEqual(self.study.data_types, ['18S']) def test_retrieve_data_types_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.data_types, []) def test_retrieve_raw_data(self): self.assertEqual(self.study.raw_data(), [1, 2, 3, 4]) def test_retrieve_raw_data_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.raw_data(), []) def test_add_raw_data(self): self._make_sandbox() new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.add_raw_data([RawData(1), RawData(2)]) obs = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study_raw_data WHERE study_id=%s", (new.id, )) self.assertEqual(obs, [[new.id, 1], [new.id, 2]]) def test_add_raw_data_private(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.status = 'private' with self.assertRaises(QiitaDBStatusError): new.add_raw_data([RawData(2)]) def test_retrieve_preprocessed_data(self): self.assertEqual(self.study.preprocessed_data(), [1, 2]) def test_retrieve_preprocessed_data_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.preprocessed_data(), []) def test_retrieve_processed_data(self): self.assertEqual(self.study.processed_data(), [1]) def test_retrieve_processed_data_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.processed_data(), []) def test_add_pmid(self): self._make_private() self.study.add_pmid('4544444') exp = ['123456', '7891011', '4544444'] self.assertEqual(self.study.pmids, exp) def test_environmental_packages(self): obs = self.study.environmental_packages exp = ['soil', 'plant-associated'] self.assertEqual(sorted(obs), sorted(exp)) def test_environmental_packages_setter(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) obs = new.environmental_packages exp = [] self.assertEqual(obs, exp) new_values = ['air', 'human-oral'] new.environmental_packages = new_values obs = new.environmental_packages self.assertEqual(sorted(obs), sorted(new_values)) def test_environmental_packages_setter_typeerror(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(TypeError): new.environmental_packages = 'air' def test_environmental_packages_setter_valueerror(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(ValueError): new.environmental_packages = ['air', 'not a package'] def test_environmental_packages_sandboxed(self): with self.assertRaises(QiitaDBStatusError): self.study.environmental_packages = ['air']
class TestStudy(TestCase): def setUp(self): self.study = Study(1) self.info = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "portal_type_id": 3, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "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) } self.infoexp = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "portal_type_id": 3, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "fried chicken", "study_abstract": "Exploring how a high fat diet changes the " "gut microbiome", "emp_person_id": 2, "principal_investigator_id": 3, "lab_person_id": 1 } self.existingexp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'number_samples_promised': 27, 'emp_person_id': StudyPerson(2), 'funding': None, 'vamps_id': None, 'first_contact': '2014-05-19 16:10', 'principal_investigator_id': StudyPerson(3), 'timeseries_type_id': 1, '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.", 'spatial_series': False, 'study_description': 'Analysis of the Cannabis Plant Microbiome', 'portal_type_id': 2, 'study_alias': 'Cannabis Soils', 'most_recent_contact': '2014-05-19 16:11', 'lab_person_id': StudyPerson(1), 'number_samples_collected': 27} def test_get_public(self): Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) obs = Study.get_public() self.assertEqual(obs, [1]) def test_exists(self): self.assertTrue(Study.exists('Identification of the Microbiomes for ' 'Cannabis Soils')) self.assertFalse(Study.exists('Not Cannabis Soils')) def test_create_study_min_data(self): """Insert a study into the database""" obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) self.assertEqual(obs.id, 2) exp = {'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'study_status_id': 1, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': None, 'vamps_id': None, 'first_contact': date.today().isoformat(), 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': None, 'study_description': 'Microbiome of people who eat nothing but' ' fried chicken', 'portal_type_id': 3, 'study_alias': 'FCM', 'study_id': 2, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25} obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 2") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly efo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 2") self.assertEqual(efo, [[1]]) def test_create_study_with_investigation(self): """Insert a study into the database with an investigation""" obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info, Investigation(1)) self.assertEqual(obs.id, 2) # check the investigation was assigned obs = self.conn_handler.execute_fetchall( "SELECT * from qiita.investigation_study WHERE study_id = 2") self.assertEqual(obs, [[1, 2]]) def test_create_study_all_data(self): """Insert a study into the database with every info field""" self.info.update({ 'vamps_id': 'MBE_1111111', 'funding': 'FundAgency', 'spatial_series': True, 'metadata_complete': False, 'reprocess': True, 'first_contact': "Today", 'study_id': 3827 }) obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) self.assertEqual(obs.id, 3827) exp = {'mixs_compliant': True, 'metadata_complete': False, 'reprocess': True, 'study_status_id': 1, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': 'FundAgency', 'vamps_id': 'MBE_1111111', 'first_contact': "Today", 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': True, 'study_description': 'Microbiome of people who eat nothing ' 'but fried chicken', 'portal_type_id': 3, 'study_alias': 'FCM', 'study_id': 3827, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25} obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 3827") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly obsefo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 3827") self.assertEqual(obsefo, [[1]]) def test_create_missing_required(self): """ Insert a study that is missing a required info key""" self.info.pop("study_alias") with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_empty_efo(self): """ Insert a study that is missing a required info key""" with self.assertRaises(IncompetentQiitaDeveloperError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [], self.info) def test_create_study_with_not_allowed_key(self): """Insert a study with key from _non_info present""" self.info.update({"email": "*****@*****.**"}) with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_unknown_db_col(self): """ Insert a study with an info key not in the database""" self.info["SHOULDNOTBEHERE"] = "BWAHAHAHAHAHA" with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_retrieve_title(self): self.assertEqual(self.study.title, 'Identification of the Microbiomes' ' for Cannabis Soils') def test_set_title(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.title = "Cannabis soils" self.assertEqual(new.title, "Cannabis soils") def test_set_title_public(self): """Tests for fail if editing title of a public study""" with self.assertRaises(QiitaDBStatusError): self.study.title = "FAILBOAT" def test_get_efo(self): self.assertEqual(self.study.efo, [1]) def test_set_efo(self): """Set efo with list efo_id""" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.efo = [3, 4] self.assertEqual(new.efo, [3, 4]) def test_set_efo_empty(self): """Set efo with list efo_id""" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.efo = [] def test_set_efo_public(self): """Set efo on a public study""" with self.assertRaises(QiitaDBStatusError): self.study.efo = 6 def test_retrieve_info(self): for key, val in viewitems(self.existingexp): if isinstance(val, QiitaObject): self.existingexp[key] = val.id self.assertEqual(self.study.info, self.existingexp) def test_set_info(self): """Set info in a study""" newinfo = { "timeseries_type_id": 2, "metadata_complete": False, "number_samples_collected": 28, "lab_person_id": StudyPerson(2), "vamps_id": 'MBE_111222', "first_contact": "June 11, 2014" } new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.infoexp.update(newinfo) new.info = newinfo # add missing table cols self.infoexp["funding"] = None self.infoexp["spatial_series"] = None self.infoexp["most_recent_contact"] = None self.infoexp["reprocess"] = False self.infoexp["lab_person_id"] = 2 self.assertEqual(new.info, self.infoexp) def test_set_info_public(self): """Tests for fail if editing info of a public study""" with self.assertRaises(QiitaDBStatusError): self.study.info = {"vamps_id": "12321312"} def test_set_info_disallowed_keys(self): """Tests for fail if sending non-info keys in info dict""" new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(QiitaDBColumnError): new.info = {"email": "*****@*****.**"} def test_info_empty(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.info = {} def test_retrieve_status(self): self.assertEqual(self.study.status, "public") def test_set_status(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.status = "private" self.assertEqual(new.status, "private") def test_retrieve_shared_with(self): self.assertEqual(self.study.shared_with, ['*****@*****.**', '*****@*****.**']) def test_retrieve_pmids(self): exp = ['123456', '7891011'] self.assertEqual(self.study.pmids, exp) def test_retrieve_pmids_empty(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.pmids, []) def test_retrieve_investigation(self): self.assertEqual(self.study.investigation, 1) def test_retrieve_investigation_empty(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.investigation, None) def test_retrieve_sample_template(self): self.assertEqual(self.study.sample_template, 1) def test_retrieve_data_types(self): self.assertEqual(self.study.data_types, ['18S']) def test_retrieve_data_types_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.data_types, []) def test_retrieve_raw_data(self): self.assertEqual(self.study.raw_data(), [1, 2]) def test_retrieve_raw_data_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.raw_data(), []) def test_retrieve_preprocessed_data(self): self.assertEqual(self.study.preprocessed_data(), [1, 2]) def test_retrieve_preprocessed_data_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.preprocessed_data(), []) def test_retrieve_processed_data(self): self.assertEqual(self.study.processed_data(), [1]) def test_retrieve_processed_data_none(self): new = Study.create(User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.processed_data(), []) def test_add_pmid(self): self.study.add_pmid('4544444') exp = ['123456', '7891011', '4544444'] self.assertEqual(self.study.pmids, exp)
def _build_study_info(user, results=None): """builds list of dicts for studies table, with all html formatted""" # get list of studies for table study_list = user.user_studies.union( Study.get_by_status('public')).union(user.shared_studies) if results is not None: study_list = study_list.intersection(results) if not study_list: # No studies left so no need to continue return [] # get info for the studies cols = ['study_id', 'email', 'principal_investigator_id', 'pmid', 'study_title', 'metadata_complete', 'number_samples_collected', 'study_abstract'] study_info = Study.get_info(study_list, cols) infolist = [] for row, info in enumerate(study_info): study = Study(info['study_id']) status = study.status # Just passing the email address as the name here, since # name is not a required field in qiita.qiita_user PI = StudyPerson(info['principal_investigator_id']) PI = study_person_linkifier((PI.email, PI.name)) if info['pmid'] is not None: pmids = ", ".join([pubmed_linkifier([p]) for p in info['pmid']]) else: pmids = "" if info["number_samples_collected"] is None: info["number_samples_collected"] = "0" shared = _get_shared_links_for_study(study) meta_complete_glyph = "ok" if info["metadata_complete"] else "remove" # build the HTML elements needed for table cell title = ("<a href='#' data-toggle='modal' " "data-target='#study-abstract-modal' " "onclick='fillAbstract(\"studies-table\", {0})'>" "<span class='glyphicon glyphicon-file' " "aria-hidden='true'></span></a> | " "<a href='/study/description/{1}' " "id='study{0}-title'>{2}</a>").format( str(row), str(study.id), info["study_title"]) meta_complete = "<span class='glyphicon glyphicon-%s'></span>" % \ meta_complete_glyph if status == 'public': shared = "Not Available" else: shared = ("<span id='shared_html_{0}'>{1}</span><br/>" "<a class='btn btn-primary btn-xs' data-toggle='modal' " "data-target='#share-study-modal-view' " "onclick='modify_sharing({0});'>Modify</a>".format( study.id, shared)) infolist.append({ "checkbox": "<input type='checkbox' value='%d' />" % study.id, "id": study.id, "title": title, "meta_complete": meta_complete, "num_samples": info["number_samples_collected"], "shared": shared, "num_raw_data": len(study.raw_data()), "pi": PI, "pmid": pmids, "status": status, "abstract": info["study_abstract"] }) return infolist
class TestStudy(TestCase): def setUp(self): self.study = Study(1) self.info = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "portal_type_id": 3, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "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) } self.infoexp = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "portal_type_id": 3, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "fried chicken", "study_abstract": "Exploring how a high fat diet changes the " "gut microbiome", "emp_person_id": 2, "principal_investigator_id": 3, "lab_person_id": 1 } self.existingexp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'number_samples_promised': 27, 'emp_person_id': StudyPerson(2), 'funding': None, 'vamps_id': None, 'first_contact': '2014-05-19 16:10', 'principal_investigator_id': StudyPerson(3), 'timeseries_type_id': 1, '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.", 'spatial_series': False, 'study_description': 'Analysis of the Cannabis Plant Microbiome', 'portal_type_id': 2, 'study_alias': 'Cannabis Soils', 'most_recent_contact': '2014-05-19 16:11', 'lab_person_id': StudyPerson(1), 'number_samples_collected': 27 } def test_get_public(self): Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) obs = Study.get_public() self.assertEqual(obs, [1]) def test_exists(self): self.assertTrue( Study.exists('Identification of the Microbiomes for ' 'Cannabis Soils')) self.assertFalse(Study.exists('Not Cannabis Soils')) def test_create_study_min_data(self): """Insert a study into the database""" obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) self.assertEqual(obs.id, 2) exp = { 'mixs_compliant': True, 'metadata_complete': True, 'reprocess': False, 'study_status_id': 1, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': None, 'vamps_id': None, 'first_contact': date.today().isoformat(), 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': None, 'study_description': 'Microbiome of people who eat nothing but' ' fried chicken', 'portal_type_id': 3, 'study_alias': 'FCM', 'study_id': 2, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25 } obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 2") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly efo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 2") self.assertEqual(efo, [[1]]) def test_create_study_with_investigation(self): """Insert a study into the database with an investigation""" obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info, Investigation(1)) self.assertEqual(obs.id, 2) # check the investigation was assigned obs = self.conn_handler.execute_fetchall( "SELECT * from qiita.investigation_study WHERE study_id = 2") self.assertEqual(obs, [[1, 2]]) def test_create_study_all_data(self): """Insert a study into the database with every info field""" self.info.update({ 'vamps_id': 'MBE_1111111', 'funding': 'FundAgency', 'spatial_series': True, 'metadata_complete': False, 'reprocess': True, 'first_contact': "Today", 'study_id': 3827 }) obs = Study.create(User('*****@*****.**'), "Fried chicken microbiome", [1], self.info) self.assertEqual(obs.id, 3827) exp = { 'mixs_compliant': True, 'metadata_complete': False, 'reprocess': True, 'study_status_id': 1, 'number_samples_promised': 28, 'emp_person_id': 2, 'funding': 'FundAgency', 'vamps_id': 'MBE_1111111', 'first_contact': "Today", 'principal_investigator_id': 3, 'timeseries_type_id': 1, 'study_abstract': 'Exploring how a high fat diet changes the ' 'gut microbiome', 'email': '*****@*****.**', 'spatial_series': True, 'study_description': 'Microbiome of people who eat nothing ' 'but fried chicken', 'portal_type_id': 3, 'study_alias': 'FCM', 'study_id': 3827, 'most_recent_contact': None, 'lab_person_id': 1, 'study_title': 'Fried chicken microbiome', 'number_samples_collected': 25 } obsins = self.conn_handler.execute_fetchall( "SELECT * FROM qiita.study WHERE study_id = 3827") self.assertEqual(len(obsins), 1) obsins = dict(obsins[0]) self.assertEqual(obsins, exp) # make sure EFO went in to table correctly obsefo = self.conn_handler.execute_fetchall( "SELECT efo_id FROM qiita.study_experimental_factor " "WHERE study_id = 3827") self.assertEqual(obsefo, [[1]]) def test_create_missing_required(self): """ Insert a study that is missing a required info key""" self.info.pop("study_alias") with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_empty_efo(self): """ Insert a study that is missing a required info key""" with self.assertRaises(IncompetentQiitaDeveloperError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [], self.info) def test_create_study_with_not_allowed_key(self): """Insert a study with key from _non_info present""" self.info.update({"email": "*****@*****.**"}) with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_create_unknown_db_col(self): """ Insert a study with an info key not in the database""" self.info["SHOULDNOTBEHERE"] = "BWAHAHAHAHAHA" with self.assertRaises(QiitaDBColumnError): Study.create(User('*****@*****.**'), "Fried Chicken Microbiome", [1], self.info) def test_retrieve_title(self): self.assertEqual( self.study.title, 'Identification of the Microbiomes' ' for Cannabis Soils') def test_set_title(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.title = "Cannabis soils" self.assertEqual(new.title, "Cannabis soils") def test_set_title_public(self): """Tests for fail if editing title of a public study""" with self.assertRaises(QiitaDBStatusError): self.study.title = "FAILBOAT" def test_get_efo(self): self.assertEqual(self.study.efo, [1]) def test_set_efo(self): """Set efo with list efo_id""" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.efo = [3, 4] self.assertEqual(new.efo, [3, 4]) def test_set_efo_empty(self): """Set efo with list efo_id""" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.efo = [] def test_set_efo_public(self): """Set efo on a public study""" with self.assertRaises(QiitaDBStatusError): self.study.efo = 6 def test_retrieve_info(self): for key, val in viewitems(self.existingexp): if isinstance(val, QiitaObject): self.existingexp[key] = val.id self.assertEqual(self.study.info, self.existingexp) def test_set_info(self): """Set info in a study""" newinfo = { "timeseries_type_id": 2, "metadata_complete": False, "number_samples_collected": 28, "lab_person_id": StudyPerson(2), "vamps_id": 'MBE_111222', "first_contact": "June 11, 2014" } new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.infoexp.update(newinfo) new.info = newinfo # add missing table cols self.infoexp["funding"] = None self.infoexp["spatial_series"] = None self.infoexp["most_recent_contact"] = None self.infoexp["reprocess"] = False self.infoexp["lab_person_id"] = 2 self.assertEqual(new.info, self.infoexp) def test_set_info_public(self): """Tests for fail if editing info of a public study""" with self.assertRaises(QiitaDBStatusError): self.study.info = {"vamps_id": "12321312"} def test_set_info_disallowed_keys(self): """Tests for fail if sending non-info keys in info dict""" new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(QiitaDBColumnError): new.info = {"email": "*****@*****.**"} def test_info_empty(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) with self.assertRaises(IncompetentQiitaDeveloperError): new.info = {} def test_retrieve_status(self): self.assertEqual(self.study.status, "public") def test_set_status(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) new.status = "private" self.assertEqual(new.status, "private") def test_retrieve_shared_with(self): self.assertEqual(self.study.shared_with, ['*****@*****.**', '*****@*****.**']) def test_retrieve_pmids(self): exp = ['123456', '7891011'] self.assertEqual(self.study.pmids, exp) def test_retrieve_pmids_empty(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.pmids, []) def test_retrieve_investigation(self): self.assertEqual(self.study.investigation, 1) def test_retrieve_investigation_empty(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.investigation, None) def test_retrieve_sample_template(self): self.assertEqual(self.study.sample_template, 1) def test_retrieve_data_types(self): self.assertEqual(self.study.data_types, ['18S']) def test_retrieve_data_types_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.data_types, []) def test_retrieve_raw_data(self): self.assertEqual(self.study.raw_data(), [1, 2]) def test_retrieve_raw_data_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.raw_data(), []) def test_retrieve_preprocessed_data(self): self.assertEqual(self.study.preprocessed_data(), [1, 2]) def test_retrieve_preprocessed_data_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.preprocessed_data(), []) def test_retrieve_processed_data(self): self.assertEqual(self.study.processed_data(), [1]) def test_retrieve_processed_data_none(self): new = Study.create( User('*****@*****.**'), 'NOT Identification of the ' 'Microbiomes for Cannabis Soils', [1], self.info) self.assertEqual(new.processed_data(), []) def test_add_pmid(self): self.study.add_pmid('4544444') exp = ['123456', '7891011', '4544444'] self.assertEqual(self.study.pmids, exp)
class TestUpdateRawDataFromCmd(TestCase): def setUp(self): fd, seqs_fp = mkstemp(suffix='_seqs.fastq') close(fd) fd, barcodes_fp = mkstemp(suffix='_barcodes.fastq') close(fd) self.filepaths = [seqs_fp, barcodes_fp] self.checksums = [] for fp in sorted(self.filepaths): with open(fp, 'w') as f: f.write("%s\n" % fp) self.checksums.append(compute_checksum(fp)) self.filepaths_types = ["raw_forward_seqs", "raw_barcodes"] self._clean_up_files = [seqs_fp, barcodes_fp] info = { "timeseries_type_id": 1, "metadata_complete": True, "mixs_compliant": True, "number_samples_collected": 25, "number_samples_promised": 28, "study_alias": "FCM", "study_description": "Microbiome of people who eat nothing but " "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) } self.new_study = Study.create(User("*****@*****.**"), "Update raw data test", efo=[1], info=info) self.study = Study(1) # The files for the RawData object attached to study 1 does not exist. # Create them so we can actually perform the tests for _, fp, _ in RawData(1).get_filepaths(): with open(fp, 'w') as f: f.write('\n') self._clean_up_files.append(fp) self.uploaded_files = get_files_from_uploads_folders( str(self.study.id)) def tearDown(self): new_uploaded_files = get_files_from_uploads_folders(str(self.study.id)) new_files = set(new_uploaded_files).difference(self.uploaded_files) path_builder = partial(join, get_mountpoint("uploads")[0][1], '1') for _, fp in new_files: self._clean_up_files.append(path_builder(fp)) for f in self._clean_up_files: if exists(f): remove(f) def test_update_raw_data_from_cmd_diff_length(self): with self.assertRaises(ValueError): update_raw_data_from_cmd(self.filepaths[1:], self.filepaths_types, self.study.id) with self.assertRaises(ValueError): update_raw_data_from_cmd(self.filepaths, self.filepaths_types[1:], self.study.id) def test_update_raw_data_from_cmd_no_raw_data(self): with self.assertRaises(ValueError): update_raw_data_from_cmd(self.filepaths, self.filepaths_types, self.new_study.id) def test_update_raw_data_from_cmd_wrong_raw_data_id(self): # Using max(raw_data_ids) + 1 to make sure that the raw data id # passed does not belong to the study with self.assertRaises(ValueError): update_raw_data_from_cmd(self.filepaths, self.filepaths_types, self.study.id, max(self.study.raw_data()) + 1) def test_update_raw_data_from_cmd(self): rd = update_raw_data_from_cmd(self.filepaths, self.filepaths_types, self.study.id) # Make sure that we are cleaning the environment for _, fp, _ in rd.get_filepaths(): self._clean_up_files.append(fp) # The checkums are in filepath order. If we sort the rd.get_filepath() # result by the filepath (itemgetter(1)) we will get them in the same # order, so the checksums will not fail for obs, exp in zip(sorted(rd.get_filepaths(), key=itemgetter(1)), self.checksums): self.assertEqual(compute_checksum(obs[1]), exp) def test_update_raw_data_from_cmd_rd_id(self): rd = update_raw_data_from_cmd(self.filepaths, self.filepaths_types, self.study.id, self.study.raw_data()[0]) # Make sure that we are cleaning the environment for _, fp, _ in rd.get_filepaths(): self._clean_up_files.append(fp) # The checkums are in filepath order. If we sort the rd.get_filepath() # result by the filepath (itemgetter(1)) we will get them in the same # order, so the checksums will not fail for obs, exp in zip(sorted(rd.get_filepaths(), key=itemgetter(1)), self.checksums): self.assertEqual(compute_checksum(obs[1]), exp)