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(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
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)