Ejemplo n.º 1
0
    def setUp(self):
        ConfigTestCase.setUp(self)
        TempfileTestCase.setUp(self)
        self.no_resources_repo = AlphaRepo()
        resource_filename1 = self.create_tempfile(suffix='.qza').name
        resource_filename2 = self.create_tempfile(suffix='.qza').name
        test_series1 = pd.Series({
            'sample1': 7.15,
            'sample2': 9.04
        },
                                 name='chao1')
        test_series2 = pd.Series(
            {
                'sample3': 7.24,
                'sample2': 9.04,
                'sample4': 8.25
            },
            name='faith_pd')

        imported_artifact = Artifact.import_data("SampleData[AlphaDiversity]",
                                                 test_series1)
        imported_artifact.save(resource_filename1)

        imported_artifact = Artifact.import_data("SampleData[AlphaDiversity]",
                                                 test_series2)
        imported_artifact.save(resource_filename2)
        config.resources.update({
            'alpha_resources': {
                'chao1': resource_filename1,
                'faith_pd': resource_filename2,
            }
        })
        resources.update(config.resources)

        self.repo = AlphaRepo()
Ejemplo n.º 2
0
def available_metrics_alpha():
    alpha_repo = AlphaRepo()
    ret_val = {
        'alpha_metrics': alpha_repo.available_metrics(),
    }

    return jsonify(ret_val), 200
Ejemplo n.º 3
0
 def test_load_resource_metric_not_available(self):
     alpha_repo = AlphaRepo()
     with patch.object(AlphaRepo, 'available_metrics') as mock_avail:
         mock_avail.return_value = ['faith_pd', 'chao1']
         with self.assertRaisesRegex(
                 UnknownMetric, "No resource "
                 "available for "
                 "metric="
                 "'fake-test-metric'"):
             alpha_repo._get_resource('fake-test-metric')
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def _get_alpha_info(alpha_metric, matching_ids, percentiles, sample_id):
    alpha_repo = AlphaRepo()
    # retrieve the alpha diversity for each sample
    alpha_series = alpha_repo.get_alpha_diversity(
        matching_ids,
        alpha_metric,
    )
    alpha_ = Alpha(alpha_series, percentiles=percentiles)
    alpha_summary = alpha_.get_group(name='').to_dict()
    if sample_id:
        sample_diversity, = alpha_repo.get_alpha_diversity(
            sample_id, alpha_metric)
    else:
        sample_diversity = None
    return alpha_summary, sample_diversity
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def setUp(self):
        super().setUp()
        faith_pd_values = [1, 2, 3, 4]
        faith_pd_index = ['s01', 's02', 's04', 's05']
        shannon_values = [7.24, 9.05, 8.25]
        shannon_index = ['s01', 's02', 'sOther']
        alpha_resources = AlphaRepo({
            'faith_pd':
            pd.Series(faith_pd_values, index=faith_pd_index),
            'shannon':
            pd.Series(shannon_values, index=shannon_index),
        })
        self.res_patcher = patch(
            'microsetta_public_api.api.plotting._alpha_repo_getter')
        self.mock_resources = self.res_patcher.start()
        self.mock_resources.return_value = alpha_resources

        self.metadata = MetadataRepo(
            pd.DataFrame(
                {
                    'age_cat': ['30s', '40s', '50s', '30s', '30s'],
                    'num_var': [3, 4, 5, 6, 7],
                },
                index=['s01', 's02', 's04', 's05', 'sOther']))
        self.md_patcher = patch(
            'microsetta_public_api.api.plotting._metadata_repo_getter')
        self.mock_metadata = self.md_patcher.start()
        self.mock_metadata.return_value = self.metadata
Ejemplo n.º 8
0
def get_alpha_alt(dataset, sample_id, alpha_metric):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)

    alpha_repo = AlphaRepo(alpha_resource.data)
    alpha_value = _get_alpha(alpha_repo, alpha_metric, sample_id)

    return jsonify(alpha_value), 200
Ejemplo n.º 9
0
def alpha_group(body,
                alpha_metric,
                summary_statistics=True,
                percentiles=None,
                return_raw=False):
    alpha_repo = AlphaRepo()
    alpha_data = _alpha_group(body, alpha_repo, _metadata_repo_getter,
                              alpha_metric, percentiles, return_raw,
                              summary_statistics)

    response = jsonify(alpha_data)
    return response, 200
Ejemplo n.º 10
0
def alpha_group_alt(body,
                    dataset,
                    alpha_metric,
                    summary_statistics=True,
                    percentiles=None,
                    return_raw=False):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)
    alpha_repo = AlphaRepo(alpha_resource.data)
    getter = partial(_metadata_repo_getter_alt, dataset=dataset)
    alpha_data = _alpha_group(body, alpha_repo, getter, alpha_metric,
                              percentiles, return_raw, summary_statistics)

    return jsonify(alpha_data), 200
Ejemplo n.º 11
0
def _alpha_repo_getter():
    return AlphaRepo()
Ejemplo n.º 12
0
def available_metrics_alpha():
    alpha_repo = AlphaRepo()
    ret_val = _available_metrics(alpha_repo)

    return jsonify(ret_val), 200
Ejemplo n.º 13
0
def available_metrics_alpha_alt(dataset):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)
    alpha_repo = AlphaRepo(alpha_resource.data)
    return jsonify(_available_metrics(alpha_repo)), 200
Ejemplo n.º 14
0
class TestAlphaRepoWithResources(TempfileTestCase, ConfigTestCase):
    def setUp(self):
        ConfigTestCase.setUp(self)
        TempfileTestCase.setUp(self)
        self.no_resources_repo = AlphaRepo()
        resource_filename1 = self.create_tempfile(suffix='.qza').name
        resource_filename2 = self.create_tempfile(suffix='.qza').name
        test_series1 = pd.Series({
            'sample1': 7.15,
            'sample2': 9.04
        },
                                 name='chao1')
        test_series2 = pd.Series(
            {
                'sample3': 7.24,
                'sample2': 9.04,
                'sample4': 8.25
            },
            name='faith_pd')

        imported_artifact = Artifact.import_data("SampleData[AlphaDiversity]",
                                                 test_series1)
        imported_artifact.save(resource_filename1)

        imported_artifact = Artifact.import_data("SampleData[AlphaDiversity]",
                                                 test_series2)
        imported_artifact.save(resource_filename2)
        config.resources.update({
            'alpha_resources': {
                'chao1': resource_filename1,
                'faith_pd': resource_filename2,
            }
        })
        resources.update(config.resources)

        self.repo = AlphaRepo()

    def tearDown(self):
        TempfileTestCase.tearDown(self)
        ConfigTestCase.tearDown(self)

    def test_available_metrics(self):
        exp = ['chao1', 'faith_pd']
        obs = self.repo.available_metrics()
        self.assertCountEqual(exp, obs)

    def test_get_alpha_diversity(self):
        # group tests
        obs = self.repo.get_alpha_diversity(['sample2', 'sample1'], 'chao1')
        exp_series = pd.Series([9.04, 7.15],
                               index=['sample2', 'sample1'],
                               name='chao1')
        assert_series_equal(obs, exp_series)

    def test_get_alpha_diversity_single_sample(self):
        # single sample tests
        obs = self.repo.get_alpha_diversity('sample2', 'chao1')
        exp_series = pd.Series([9.04], index=['sample2'], name='chao1')
        assert_series_equal(obs, exp_series)

    def test_exists(self):
        # group tests
        sample_list = ['sample2', 'sample1', 'sample2', 'blah', 'sample4']
        obs = self.repo.exists(sample_list, 'chao1')
        exp = [True, True, True, False, False]
        self.assertListEqual(obs, exp)
        obs = self.repo.exists(sample_list, 'faith_pd')
        exp = [True, False, True, False, True]
        self.assertListEqual(obs, exp)

    def test_exists_single_sample(self):
        # single sample tests
        obs = self.repo.exists('sample1', 'chao1')
        self.assertTrue(obs)
        obs = self.repo.exists('sample1', 'faith_pd')
        self.assertFalse(obs)
Ejemplo n.º 15
0
def exists_single_alt(dataset, alpha_metric, sample_id):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)
    alpha_repo = AlphaRepo(alpha_resource.data)
    return _exists(alpha_repo, alpha_metric, sample_id)
Ejemplo n.º 16
0
def exists_single(alpha_metric, sample_id):
    alpha_repo = AlphaRepo()
    return _exists(alpha_repo, alpha_metric, sample_id)
Ejemplo n.º 17
0
def exists_group_alt(body, dataset, alpha_metric):
    alpha_resource = _validate_dataset_alpha(dataset, get_resources)
    alpha_repo = AlphaRepo(alpha_resource.data)
    return _exists(alpha_repo, alpha_metric, body)
Ejemplo n.º 18
0
def exists_group(body, alpha_metric):
    alpha_repo = AlphaRepo()
    return _exists(alpha_repo, alpha_metric, body)
Ejemplo n.º 19
0
def get_alpha(sample_id, alpha_metric):
    alpha_repo = AlphaRepo()
    ret_val = _get_alpha(alpha_repo, alpha_metric, sample_id)

    return jsonify(ret_val), 200
Ejemplo n.º 20
0
 def _getter():
     alpha_resource = _validate_dataset_alpha(dataset, get_resources)
     return AlphaRepo(alpha_resource.data)