Beispiel #1
0
    def test_get_group_raw(self):
        adiv = Alpha(self.series)
        self.series = pd.Series([0.1, 0.2, 0.8, 0.7, 0.7, 0.6],
                                index=['a', 'b', 'c', 'd', 'e', 'f'],
                                name='shannon')
        exp_all = GroupAlphaRaw(name=None,
                                alpha_metric='shannon',
                                alpha_diversity={
                                    'a': 0.1,
                                    'b': 0.2,
                                    'c': 0.8,
                                    'd': 0.7,
                                    'e': 0.7,
                                    'f': 0.6
                                })
        obs_all = adiv.get_group_raw()
        self.assertEqual(obs_all, exp_all)

        exp_partial = GroupAlphaRaw(name='foo',
                                    alpha_metric='shannon',
                                    alpha_diversity={
                                        'a': 0.1,
                                        'c': 0.8,
                                        'f': 0.6
                                    })
        obs_partial = adiv.get_group_raw(['a', 'c', 'f'], 'foo')
        self.assertEqual(obs_partial, exp_partial)
def _alpha_group(body, alpha_repo, metadata_repo_getter, alpha_metric,
                 percentiles, return_raw, summary_statistics):
    if not (summary_statistics or return_raw):
        # swagger does not account for parameter dependencies, so we should
        #  give a bad request error here
        raise IncompatibleOptions('Either `summary_statistics`, '
                                  '`return_raw`, or both are required to be '
                                  'true.')
    sample_ids = []
    # do the common checks
    available_metrics = alpha_repo.available_metrics()
    type_ = 'metric'
    validate_resource_alt(available_metrics, alpha_metric, type_)
    if 'sample_ids' in body:
        sample_ids = body['sample_ids']

        # figure out if the user asked for a metric we have data on
        # make sure all of the data the samples the user asked for have values
        # for the given metric
        missing_ids = [
            id_ for id_ in sample_ids
            if not alpha_repo.exists(id_, alpha_metric)
        ]
        check_missing_ids_alt(missing_ids, alpha_metric, type_)
    # find sample IDs matching the metadata query
    if 'metadata_query' in body:
        query = body['metadata_query']
        metadata_repo = metadata_repo_getter()
        matching_ids = metadata_repo.sample_id_matches(query)
        matching_ids = [
            id_ for id_ in matching_ids
            if alpha_repo.exists(id_, alpha_metric)
        ]
        if 'sample_ids' not in body:
            sample_ids = matching_ids
        elif body['condition'] == 'OR':
            sample_ids = list(set(sample_ids) | set(matching_ids))
        elif body['condition'] == 'AND':
            sample_ids = list(set(sample_ids) & set(matching_ids))
    # retrieve the alpha diversity for each sample
    alpha_series = alpha_repo.get_alpha_diversity(
        sample_ids,
        alpha_metric,
    )
    alpha_ = Alpha(alpha_series, percentiles=percentiles)
    alpha_data = dict()
    if return_raw:
        # not using name right now, so give it a placeholder name
        alpha_values = alpha_.get_group_raw(name='').to_dict()
        del alpha_values['name']
        alpha_data.update(alpha_values)
    if summary_statistics:
        # not using name right now, so give it a placeholder name
        alpha_summary = alpha_.get_group(name='').to_dict()
        del alpha_summary['name']
        alpha_data.update({'alpha_metric': alpha_summary.pop('alpha_metric')})
        alpha_data.update({'group_summary': alpha_summary})
    return alpha_data
Beispiel #3
0
def alpha_group(body,
                alpha_metric,
                summary_statistics=True,
                percentiles=None,
                return_raw=False):
    if not (summary_statistics or return_raw):
        # swagger does not account for parameter dependencies, so we should
        #  give a bad request error here
        return jsonify(error=400,
                       text='Either `summary_statistics`, `return_raw`, '
                       'or both are required to be true.'), 400

    sample_ids = body['sample_ids']

    alpha_repo = AlphaRepo()

    # figure out if the user asked for a metric we have data on
    available_metrics = alpha_repo.available_metrics()
    type_ = 'metric'
    missing_metric = validate_resource(available_metrics, alpha_metric, type_)
    if missing_metric:
        return missing_metric

    # make sure all of the data the samples the user asked for have values
    # for the given metric
    missing_ids = [
        id_ for id_ in sample_ids if not alpha_repo.exists(id_, alpha_metric)
    ]
    missing_ids_msg = check_missing_ids(missing_ids, alpha_metric, type_)
    if missing_ids_msg:
        return missing_ids_msg

    # retrieve the alpha diversity for each sample
    alpha_series = alpha_repo.get_alpha_diversity(
        sample_ids,
        alpha_metric,
    )
    alpha_ = Alpha(alpha_series, percentiles=percentiles)
    alpha_data = dict()

    if return_raw:
        # not using name right now, so give it a placeholder name
        alpha_values = alpha_.get_group_raw(name='').to_dict()
        del alpha_values['name']
        alpha_data.update(alpha_values)
    if summary_statistics:
        # not using name right now, so give it a placeholder name
        alpha_summary = alpha_.get_group(name='').to_dict()
        del alpha_summary['name']
        alpha_data.update({'alpha_metric': alpha_summary.pop('alpha_metric')})
        alpha_data.update({'group_summary': alpha_summary})

    response = jsonify(alpha_data)
    return response, 200
def _get_alpha(alpha_repo, alpha_metric, sample_id):
    if not all(alpha_repo.exists([sample_id], alpha_metric)):
        raise UnknownID(f"Sample ID not found. Got: {sample_id}")
    alpha_series = alpha_repo.get_alpha_diversity([sample_id], alpha_metric)
    alpha_ = Alpha(alpha_series)
    alpha_data = alpha_.get_group_raw().to_dict()
    ret_val = {
        'sample_id': sample_id,
        'alpha_metric': alpha_data['alpha_metric'],
        'data': alpha_data['alpha_diversity'][sample_id],
    }
    return ret_val
Beispiel #5
0
def get_alpha(sample_id, alpha_metric):
    alpha_repo = AlphaRepo()
    if not all(alpha_repo.exists([sample_id], alpha_metric)):
        return jsonify(error=404, text="Sample ID not found."), \
               404
    alpha_series = alpha_repo.get_alpha_diversity([sample_id], alpha_metric)
    alpha_ = Alpha(alpha_series)
    alpha_data = alpha_.get_group_raw().to_dict()
    ret_val = {
        'sample_id': sample_id,
        'alpha_metric': alpha_data['alpha_metric'],
        'data': alpha_data['alpha_diversity'][sample_id],
    }

    return jsonify(ret_val), 200
Beispiel #6
0
 def test_get_group_raw_noname(self):
     adiv = Alpha(self.series)
     with self.assertRaisesRegex(ValueError, "Name not specified."):
         adiv.get_group_raw(['a', 'c'])
Beispiel #7
0
 def test_get_group_raw_missing(self):
     adiv = Alpha(self.series)
     with self.assertRaisesRegex(UnknownID, "Identifier not found."):
         adiv.get_group_raw(['foo', 'bar'], 'baz')