def _present_microbes_taxonomy_table(sample_ids, resource): taxonomy_repo = TaxonomyRepo() error_response = _check_resource_and_missing_ids(taxonomy_repo, sample_ids, resource) if error_response: return error_response taxonomy_ = taxonomy_repo.model(resource) taxonomy_table = taxonomy_.presence_data_table(sample_ids) response = jsonify(taxonomy_table.to_dict()) return response, 200
def _summarize_group(sample_ids, table_name): taxonomy_repo = TaxonomyRepo() error_response = _check_resource_and_missing_ids(taxonomy_repo, sample_ids, table_name) if error_response: return error_response taxonomy_ = taxonomy_repo.model(table_name) taxonomy_data = taxonomy_.get_group(sample_ids, '').to_dict() del taxonomy_data['name'] del taxonomy_data['feature_ranks'] response = jsonify(taxonomy_data) return response, 200
class TestTaxonomyRepoWithResources(TempfileTestCase, ConfigTestCase): def setUp(self): TempfileTestCase.setUp(self) ConfigTestCase.setUp(self) self.no_resources_repo = TaxonomyRepo() self.table1_filename = self.create_tempfile(suffix='.qza').name self.taxonomy1_filename = self.create_tempfile(suffix='.qza').name self.table2_filename = self.create_tempfile(suffix='.qza').name self.taxonomy2_filename = self.create_tempfile(suffix='.qza').name self.table3_filename = self.create_tempfile(suffix='.qza').name self.var_table_filename = self.create_tempfile(suffix='.qza').name self.table_biom = self.create_tempfile(suffix='.biom').name self.table = biom.Table(np.array([[0, 1, 2], [2, 4, 6], [3, 0, 1]]), ['feature-1', 'feature-2', 'feature-3'], ['sample-1', 'sample-2', 'sample-3']) self.taxonomy_df = pd.DataFrame( [['feature-1', 'a; b; c', 0.123], ['feature-2', 'a; b; c; d; e', 0.345], ['feature-3', 'a; f; g; h', 0.678]], columns=['Feature ID', 'Taxon', 'Confidence']) self.taxonomy_df.set_index('Feature ID', inplace=True) self.table2 = biom.Table(np.array([[0, 1, 2], [2, 4, 6], [3, 0, 1]]), ['feature-1', 'feature-X', 'feature-3'], ['sample-1', 'sample-2', 'sample-3']) self.taxonomy2_df = pd.DataFrame( [['feature-1', 'a; b; c', 0.123], ['feature-X', 'a; b; c; d; e', 0.34], ['feature-3', 'a; f; g; h', 0.678]], columns=['Feature ID', 'Taxon', 'Confidence']) self.taxonomy2_df.set_index('Feature ID', inplace=True) self.table3 = biom.Table(np.array([[1, 2], [0, 1]]), ['feature-X', 'feature-3'], ['sample-2', 'sample-3']) imported_artifact = Artifact.import_data("FeatureTable[Frequency]", self.table) imported_artifact.save(self.table1_filename) imported_artifact = Artifact.import_data("FeatureData[Taxonomy]", self.taxonomy_df) imported_artifact.save(self.taxonomy1_filename) imported_artifact = Artifact.import_data("FeatureTable[Frequency]", self.table2) imported_artifact.save(self.table2_filename) imported_artifact = Artifact.import_data("FeatureData[Taxonomy]", self.taxonomy2_df) imported_artifact.save(self.taxonomy2_filename) imported_artifact = Artifact.import_data("FeatureTable[Frequency]", self.table3) imported_artifact.save(self.table3_filename) with biom_open(self.table_biom, 'w') as f: self.table.to_hdf5(f, 'test-table') config.resources.update({ 'table_resources': { 'table1': { 'table': self.table1_filename, }, 'table2': { 'table': self.table1_filename, 'feature-data-taxonomy': self.taxonomy1_filename, 'cache-taxonomy': False, }, 'table3': { 'table': self.table3_filename, 'feature-data-taxonomy': self.taxonomy2_filename, 'cache-taxonomy': False, 'variances': self.table3_filename, }, 'table4': { 'table': self.table_biom, 'feature-data-taxonomy': self.taxonomy1_filename, 'cache-taxonomy': False, 'table-format': 'biom' }, 'table5': { 'table': self.table2_filename, }, 'table6': { 'table': self.table_biom, 'table-format': 'biom', }, 'cached-taxonomy-table': { 'table': self.table1_filename, 'feature-data-taxonomy': self.taxonomy1_filename, 'cache-taxonomy': True, }, } }) resources.update(config.resources) self.repo = TaxonomyRepo() def tearDown(self): TempfileTestCase.tearDown(self) ConfigTestCase.tearDown(self) def test_available_taxonomy_tables(self): exp = ['table2', 'table3', 'table4', 'cached-taxonomy-table'] obs = self.repo.resources() self.assertCountEqual(exp, obs) def test_available_taxonomy_tables_empty_repo(self): exp = [] obs = self.no_resources_repo.resources() self.assertCountEqual(exp, obs) def test_get_table(self): exp = self.table obs = self.repo.table('table2') self.assertEqual(exp, obs) def test_get_table_cached_taxonomy(self): exp = self.table obs = self.repo.table('cached-taxonomy-table') self.assertEqual(exp, obs) def test_get_table_invalid(self): with self.assertRaises(ValueError): self.repo.table('table1') def test_get_taxonomy(self): exp = self.taxonomy_df obs = self.repo.feature_data_taxonomy('table4') obs['Confidence'] = obs['Confidence'].astype('float64') assert_frame_equal(exp, obs) def test_get_taxonomy_model_cached(self): exp = resources['table_resources']['cached-taxonomy-table']['model'] obs = self.repo.model('cached-taxonomy-table') self.assertIs(exp, obs) def test_get_taxonomy_model_non_cached(self): self.assertNotIn('model', resources['table_resources']['table2']) obs = self.repo.model('table2') self.assertIsInstance(obs, TaxonomyModel) def test_get_taxonomy_invalid(self): with self.assertRaises(ValueError): self.repo.table('foo') def test_get_variances(self): exp = self.table3 obs = self.repo.variances('table3') self.assertEqual(exp, obs) def test_get_variances_none(self): exp = None obs = self.repo.variances('table4') self.assertIs(exp, obs) def test_get_variances_invalid(self): with self.assertRaises(ValueError): self.repo.table('table6') def test_exists(self): exp = [False, True, True] obs = self.repo.exists(['sample-1', 'sample-2', 'sample-3'], 'table3') self.assertListEqual(exp, obs) def test_exists_single(self): obs = self.repo.exists('sample-1', 'table2') self.assertTrue(obs)