コード例 #1
0
ファイル: sample_template.py プロジェクト: antgonza/qiita
def sample_template_checks(study_id, user, check_exists=False):
    """Performs different checks and raises errors if any of the checks fail

    Parameters
    ----------
    study_id : int
        The study id
    user : qiita_db.user.User
        The user trying to access the study
    check_exists : bool, optional
        If true, check if the sample template exists

    Raises
    ------
    HTTPError
        404 if the study does not exist
        403 if the user does not have access to the study
        404 if check_exists == True and the sample template doesn't exist
    """
    try:
        study = Study(int(study_id))
    except QiitaDBUnknownIDError:
        raise HTTPError(404, reason='Study does not exist')
    if not study.has_access(user):
        raise HTTPError(403, reason='User does not have access to study')

    # Check if the sample template exists
    if check_exists and not SampleTemplate.exists(study_id):
        raise HTTPError(404, reason="Study %s doesn't have sample information"
                        % study_id)
コード例 #2
0
def sample_template_checks(study_id, user, check_exists=False):
    """Performs different checks and raises errors if any of the checks fail

    Parameters
    ----------
    study_id : int
        The study id
    user : qiita_db.user.User
        The user trying to access the study
    check_exists : bool, optional
        If true, check if the sample template exists

    Raises
    ------
    HTTPError
        404 if the study does not exist
        403 if the user does not have access to the study
        404 if check_exists == True and the sample template doesn't exist
    """
    try:
        study = Study(int(study_id))
    except QiitaDBUnknownIDError:
        raise HTTPError(404, reason='Study does not exist')
    if not study.has_access(user):
        raise HTTPError(403, reason='User does not have access to study')

    # Check if the sample template exists
    if check_exists and not SampleTemplate.exists(study_id):
        raise HTTPError(404,
                        reason="Study %s doesn't have sample information" %
                        study_id)
コード例 #3
0
ファイル: util.py プロジェクト: ElDeveloper/qiita
def check_access(study_id, user_id):
    """Checks if user given has access to the study given

    Parameters
    ----------
    study_id : int
        ID of the study to check access to
    user_id : str
        ID of the user to check access for

    Returns
    -------
    dict
        Empty dict if access allowed, else a dict in the form
        {'status': 'error',
         'message': reason for error}

    """
    try:
        study = Study(int(study_id))
    except QiitaDBUnknownIDError:
        return {'status': 'error',
                'message': 'Study does not exist'}
    if not study.has_access(User(user_id)):
        return {'status': 'error',
                'message': 'User does not have access to study'}
    return {}
コード例 #4
0
ファイル: util.py プロジェクト: yotohoshi/qiita
def check_access(study_id, user_id):
    """Checks if user given has access to the study given

    Parameters
    ----------
    study_id : int
        ID of the study to check access to
    user_id : str
        ID of the user to check access for

    Returns
    -------
    dict
        Empty dict if access allowed, else a dict in the form
        {'status': 'error',
         'message': reason for error}

    """
    try:
        study = Study(int(study_id))
    except QiitaDBUnknownIDError:
        return {'status': 'error', 'message': 'Study does not exist'}
    if not study.has_access(User(user_id)):
        return {
            'status': 'error',
            'message': 'User does not have access to study'
        }
    return {}
コード例 #5
0
ファイル: study_handlers.py プロジェクト: gustabf/qiita
    def get(self, arguments):
        study_id = int(self.get_argument('study_id'))

        # this block is tricky because you can either pass the sample or the
        # prep template and if none is passed then we will let an exception
        # be raised because template will not be declared for the logic below
        if self.get_argument('prep_template', None):
            template = PrepTemplate(int(self.get_argument('prep_template')))
        if self.get_argument('sample_template', None):
            template = None
            tid = int(self.get_argument('sample_template'))
            try:
                template = SampleTemplate(tid)
            except QiitaDBUnknownIDError:
                raise HTTPError(404, "SampleTemplate %d does not exist" % tid)

        study = Study(template.study_id)

        # check whether or not the user has access to the requested information
        if not study.has_access(User(self.current_user)):
            raise HTTPError(403, "You do not have access to access this "
                                 "information.")

        df = dataframe_from_template(template)
        stats = stats_from_df(df)

        self.render('metadata_summary.html', user=self.current_user,
                    study_title=study.title, stats=stats,
                    study_id=study_id)
コード例 #6
0
    def get(self, arguments):
        study_id = int(self.get_argument('study_id'))

        # Get the arguments
        prep_template = self.get_argument('prep_template', None)
        sample_template = self.get_argument('sample_template', None)

        if prep_template and sample_template:
            raise HTTPError(
                500, "You should provide either a sample template "
                "or a prep template, but not both")
        elif prep_template:
            # The prep template has been provided
            template = self._get_template(PrepTemplate, prep_template)
            back_button_path = (
                "/study/description/%s?top_tab=raw_data_tab&sub_tab=%s"
                "&prep_tab=%s" % (study_id, template.raw_data, template.id))
        elif sample_template:
            # The sample template has been provided
            template = self._get_template(SampleTemplate, sample_template)
            back_button_path = ("/study/description/%s" % study_id)
        else:
            # Neither a sample template or a prep template has been provided
            # Fail nicely
            raise HTTPError(
                500, "You should provide either a sample template "
                "or a prep template")

        study = Study(template.study_id)

        # check whether or not the user has access to the requested information
        if not study.has_access(self.current_user):
            raise HTTPError(
                403, "You do not have access to access this "
                "information.")

        df = dataframe_from_template(template)
        num_samples = df.shape[0]
        stats = stats_from_df(df)

        self.render('metadata_summary.html',
                    study_title=study.title,
                    stats=stats,
                    num_samples=num_samples,
                    back_button_path=back_button_path)
コード例 #7
0
    def get(self, arguments):
        study_id = int(self.get_argument('study_id'))

        # Get the arguments
        prep_template = self.get_argument('prep_template', None)
        sample_template = self.get_argument('sample_template', None)

        if prep_template and sample_template:
            raise HTTPError(500, "You should provide either a sample template "
                                 "or a prep template, but not both")
        elif prep_template:
            # The prep template has been provided
            template = self._get_template(PrepTemplate, prep_template)
            back_button_path = (
                "/study/description/%s?top_tab=prep_template_tab&sub_tab=%s"
                % (study_id, template.id))
        elif sample_template:
            # The sample template has been provided
            template = self._get_template(SampleTemplate, sample_template)
            back_button_path = (
                "/study/description/%s"
                % study_id)
        else:
            # Neither a sample template or a prep template has been provided
            # Fail nicely
            raise HTTPError(500, "You should provide either a sample template "
                                 "or a prep template")

        study = Study(template.study_id)

        # check whether or not the user has access to the requested information
        if not study.has_access(self.current_user):
            raise HTTPError(403, "You do not have access to access this "
                                 "information.")

        df = template.to_dataframe()
        num_samples = df.shape[0]
        stats = stats_from_df(df)

        self.render('metadata_summary.html',
                    study_title=study.title, stats=stats,
                    num_samples=num_samples, back_button_path=back_button_path)
コード例 #8
0
ファイル: studies.py プロジェクト: mestaki/qiita
def study_get_req(study_id, user_id):
    """Returns information available for the given study

    Parameters
    ----------
    study_id : int
        Study id to get prep template info for
    user_id : str
        User requesting the info

    Returns
    -------
    dict
        Data types information in the form
        {'status': status,
         'message': message,
         'info': dict of objects
        status can be success, warning, or error depending on result
        message has the warnings or errors
        info contains study information seperated by data type, in the form
        {col_name: value, ...} with value being a string, int, or list of
        strings or ints
    """
    access_error = check_access(study_id, user_id)
    if access_error:
        return access_error
    # Can only pass ids over API, so need to instantiate object
    study = Study(study_id)
    study_info = study.info
    # Add needed info that is not part of the initial info pull
    study_info['publication_doi'] = []
    study_info['publication_pid'] = []
    for pub, is_doi in study.publications:
        if is_doi:
            study_info['publication_doi'].append(pub)
        else:
            study_info['publication_pid'].append(pub)
    study_info['study_id'] = study.id
    study_info['study_title'] = study.title
    study_info['shared_with'] = [s.id for s in study.shared_with]
    study_info['status'] = study.status
    study_info['ebi_study_accession'] = study.ebi_study_accession
    study_info['ebi_submission_status'] = study.ebi_submission_status
    study_info['public_raw_download'] = study.public_raw_download
    study_info['notes'] = study.notes

    # Clean up StudyPerson objects to string for display
    pi = study_info['principal_investigator']
    study_info['principal_investigator'] = {
        'name': pi.name,
        'email': pi.email,
        'affiliation': pi.affiliation
    }

    lab_person = study_info['lab_person']
    if lab_person:
        study_info['lab_person'] = {
            'name': lab_person.name,
            'email': lab_person.email,
            'affiliation': lab_person.affiliation
        }

    samples = study.sample_template
    study_info['num_samples'] = 0 if samples is None else len(list(samples))
    study_info['owner'] = study.owner.id
    # Study.has_access no_public=True, will return True only if the user_id is
    # the owner of the study or if the study is shared with the user_id; this
    # with study.public_raw_download will define has_access_to_raw_data
    study_info['has_access_to_raw_data'] = study.has_access(
        User(user_id), True) or study.public_raw_download

    study_info['show_biom_download_button'] = 'BIOM' in [
        a.artifact_type for a in study.artifacts()
    ]
    study_info['show_raw_download_button'] = any(
        [True for pt in study.prep_templates() if pt.artifact is not None])

    # getting study processing status from redis
    processing = False
    study_info['level'] = ''
    study_info['message'] = ''
    job_info = r_client.get(STUDY_KEY_FORMAT % study_id)
    if job_info:
        job_info = defaultdict(lambda: '', loads(job_info))
        job_id = job_info['job_id']
        job = ProcessingJob(job_id)
        job_status = job.status
        processing = job_status not in ('success', 'error')
        if processing:
            study_info['level'] = 'info'
            study_info['message'] = 'This study is currently being processed'
        elif job_status == 'error':
            study_info['level'] = 'danger'
            study_info['message'] = job.log.msg.replace('\n', '</br>')
        else:
            study_info['level'] = job_info['alert_type']
            study_info['message'] = job_info['alert_msg'].replace(
                '\n', '</br>')

    return {
        'status': 'success',
        'message': '',
        'study_info': study_info,
        'editable': study.can_edit(User(user_id))
    }
コード例 #9
0
ファイル: test_study.py プロジェクト: MarkBruns/qiita
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']
コード例 #10
0
ファイル: studies.py プロジェクト: josenavas/QiiTa
def study_get_req(study_id, user_id):
    """Returns information available for the given study

    Parameters
    ----------
    study_id : int
        Study id to get prep template info for
    user_id : str
        User requesting the info

    Returns
    -------
    dict
        Data types information in the form
        {'status': status,
         'message': message,
         'info': dict of objects
        status can be success, warning, or error depending on result
        message has the warnings or errors
        info contains study information seperated by data type, in the form
        {col_name: value, ...} with value being a string, int, or list of
        strings or ints
    """
    access_error = check_access(study_id, user_id)
    if access_error:
        return access_error
    # Can only pass ids over API, so need to instantiate object
    study = Study(study_id)
    study_info = study.info
    # Add needed info that is not part of the initial info pull
    study_info['publication_doi'] = []
    study_info['publication_pid'] = []
    for pub, is_doi in study.publications:
        if is_doi:
            study_info['publication_doi'].append(pub)
        else:
            study_info['publication_pid'].append(pub)
    study_info['study_id'] = study.id
    study_info['study_title'] = study.title
    study_info['shared_with'] = [s.id for s in study.shared_with]
    study_info['status'] = study.status
    study_info['ebi_study_accession'] = study.ebi_study_accession
    study_info['ebi_submission_status'] = study.ebi_submission_status

    # Clean up StudyPerson objects to string for display
    pi = study_info['principal_investigator']
    study_info['principal_investigator'] = {
        'name': pi.name,
        'email': pi.email,
        'affiliation': pi.affiliation}

    lab_person = study_info['lab_person']
    if lab_person:
        study_info['lab_person'] = {
            'name': lab_person.name,
            'email': lab_person.email,
            'affiliation': lab_person.affiliation}

    samples = study.sample_template
    study_info['num_samples'] = 0 if samples is None else len(list(samples))
    study_info['owner'] = study.owner.id
    # Study.has_access no_public=True, will return True only if the user_id is
    # the owner of the study or if the study is shared with the user_id
    study_info['has_access_to_raw_data'] = study.has_access(
        User(user_id), True)

    study_info['show_biom_download_button'] = 'BIOM' in [
        a.artifact_type for a in study.artifacts()]
    study_info['show_raw_download_button'] = any([
        True for pt in study.prep_templates() if pt.artifact is not None])

    # getting study processing status from redis
    processing = False
    study_info['level'] = ''
    study_info['message'] = ''
    job_info = r_client.get(STUDY_KEY_FORMAT % study_id)
    if job_info:
        job_info = defaultdict(lambda: '', loads(job_info))
        job_id = job_info['job_id']
        job = ProcessingJob(job_id)
        job_status = job.status
        processing = job_status not in ('success', 'error')
        if processing:
            study_info['level'] = 'info'
            study_info['message'] = 'This study is currently being processed'
        elif job_status == 'error':
            study_info['level'] = 'danger'
            study_info['message'] = job.log.msg.replace('\n', '</br>')
        else:
            study_info['level'] = job_info['alert_type']
            study_info['message'] = job_info['alert_msg'].replace(
                '\n', '</br>')

    return {'status': 'success',
            'message': '',
            'study_info': study_info,
            'editable': study.can_edit(User(user_id))}
コード例 #11
0
ファイル: test_study.py プロジェクト: jwdebelius/qiita
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']
コード例 #12
0
ファイル: studies.py プロジェクト: experimentAccount0/qiita
def study_get_req(study_id, user_id):
    """Returns information available for the given study

    Parameters
    ----------
    study_id : int
        Study id to get prep template info for
    user_id : str
        User requesting the info

    Returns
    -------
    dict
        Data types information in the form
        {'status': status,
         'message': message,
         'info': dict of objects
        status can be success, warning, or error depending on result
        message has the warnings or errors
        info contains study information seperated by data type, in the form
        {col_name: value, ...} with value being a string, int, or list of
        strings or ints
    """
    access_error = check_access(study_id, user_id)
    if access_error:
        return access_error
    # Can only pass ids over API, so need to instantiate object
    study = Study(study_id)
    study_info = study.info
    # Add needed info that is not part of the initial info pull
    study_info['publication_doi'] = []
    study_info['publication_pid'] = []
    for pub, is_doi in study.publications:
        if is_doi:
            study_info['publication_doi'].append(pub)
        else:
            study_info['publication_pid'].append(pub)
    study_info['study_id'] = study.id
    study_info['study_title'] = study.title
    study_info['shared_with'] = [s.id for s in study.shared_with]
    study_info['status'] = study.status
    study_info['ebi_study_accession'] = study.ebi_study_accession
    study_info['ebi_submission_status'] = study.ebi_submission_status

    # Clean up StudyPerson objects to string for display
    pi = study_info['principal_investigator']
    study_info['principal_investigator'] = {
        'name': pi.name,
        'email': pi.email,
        'affiliation': pi.affiliation}

    lab_person = study_info['lab_person']
    if lab_person:
        study_info['lab_person'] = {
            'name': lab_person.name,
            'email': lab_person.email,
            'affiliation': lab_person.affiliation}

    samples = study.sample_template
    study_info['num_samples'] = 0 if samples is None else len(list(samples))
    study_info['owner'] = study.owner.id
    # Study.has_access no_public=True, will return True only if the user_id is
    # the owner of the study or if the study is shared with the user_id
    study_info['has_access_to_raw_data'] = study.has_access(
        User(user_id), True)

    study_info['show_biom_download_button'] = 'BIOM' in [
        a.artifact_type for a in study.artifacts()]
    study_info['show_raw_download_button'] = any([
        True for pt in study.prep_templates() if pt.artifact is not None])

    return {'status': 'success',
            'message': '',
            'study_info': study_info,
            'editable': study.can_edit(User(user_id))}
コード例 #13
0
ファイル: download.py プロジェクト: experimentAccount0/qiita
    def get(self, study_id):
        study_id = int(study_id)
        # Check general access to study
        study_info = study_get_req(study_id, self.current_user.id)
        if study_info['status'] != 'success':
            raise HTTPError(
                405,
                "%s: %s, %s" % (study_info['message'], self.current_user.email,
                                str(study_id)))

        study = Study(study_id)
        user = self.current_user
        # Check "owner" access to the study
        if not study.has_access(user, True):
            raise HTTPError(
                405, "%s: %s, %s" %
                ('No raw data access', self.current_user.email, str(study_id)))

        basedir = get_db_files_base_dir()
        basedir_len = len(basedir) + 1
        # loop over artifacts and retrieve raw data (no parents)
        to_download = []
        for a in study.artifacts():
            if not a.parents:
                for i, (fid, path, data_type) in enumerate(a.filepaths):
                    if data_type == 'directory':
                        # If we have a directory, we actually need to list
                        # all the files from the directory so NGINX can
                        # actually download all of them
                        for dp, _, fps in walk(path):
                            for fname in fps:
                                fullpath = join(dp, fname)
                                spath = fullpath
                                if fullpath.startswith(basedir):
                                    spath = fullpath[basedir_len:]
                                to_download.append((fullpath, spath, spath))
                    elif path.startswith(basedir):
                        spath = path[basedir_len:]
                        to_download.append((path, spath, spath))
                    else:
                        # We are not aware of any case that can trigger this
                        # situation, but we wanted to be overly cautious
                        # There is no test for this line cause we don't know
                        # how to trigger it
                        to_download.append((path, path, path))

                for pt in a.prep_templates:
                    qmf = pt.qiime_map_fp
                    if qmf is not None:
                        sqmf = qmf
                        if qmf.startswith(basedir):
                            sqmf = qmf[basedir_len:]
                        to_download.append(
                            (qmf, sqmf,
                             'mapping_files/%s_mapping_file.txt' % a.id))

        # If we don't have nginx, write a file that indicates this
        # Note that this configuration will automatically create and download
        # ("on the fly") the zip file via the contents in all_files
        all_files = '\n'.join([
            "- %s /protected/%s %s" % (getsize(fp), sfp, n)
            for fp, sfp, n in to_download
        ])
        self.write("%s\n" % all_files)

        zip_fn = 'study_raw_data_%d_%s.zip' % (
            study_id, datetime.now().strftime('%m%d%y-%H%M%S'))

        self.set_header('Content-Description', 'File Transfer')
        self.set_header('Expires', '0')
        self.set_header('Cache-Control', 'no-cache')
        self.set_header('X-Archive-Files', 'zip')
        self.set_header('Content-Disposition',
                        'attachment; filename=%s' % zip_fn)
        self.finish()