def alpha(table: biom.Table): """ :param table: :return: """ if table.is_empty(): raise ValueError("The provided table object is empty") table = get_biom_table(table) alpha_diversities = [] counts = table.matrix_data.toarray().astype(float).T sample_ids = table.ids(axis='sample') sample_metadata = dict(zip(table.ids(), table.metadata())) for metric in ALPHA_DIVERSITY_METHODS: result = alpha_diversity(metric=metric, counts=counts, ids=sample_ids) result.name = metric alpha_diversities.append(result) aggregated_diversity_results = aggregate_results(alpha_diversities, sample_ids) formatted_diversity_results = _format_alpha_results_to_json( aggregated_diversity_results, sample_metadata) return formatted_diversity_results
def biom_to_table(table: biom.Table): """Convert a BIOM table into table components. Parameters ---------- table : biom.Table BIOM table to convert. Returns ------- tuple of list of list Profile data. list Observation IDs. list Sample IDs. list of dict Observation metadata. """ return (table.matrix_data.toarray().astype(int).tolist(), table.ids('observation').tolist(), table.ids('sample').tolist(), list(map(dict, table.metadata(axis='observation') or ())))
def test_add_metacol(self): obs = prep_table({ 'S1': { 'G1': 4, 'G2': 5, 'G3': 8, 'G4': 0, 'G5': 3 }, 'S2': { 'G1': 1, 'G2': 8, 'G3': 0, 'G4': 7, 'G5': 4 }, 'S3': { 'G1': 0, 'G2': 2, 'G3': 3, 'G4': 5, 'G5': 0 } }) self.assertListEqual(obs[3], [{}] * 5) ob2 = Table(*map(np.array, obs)) # regular table rankdic = {'G1': 'S', 'G2': 'S', 'G3': 'F', 'G4': 'O', 'G5': 'P'} add_metacol(obs, rankdic, 'Rank') exp = [{ 'Rank': 'S' }, { 'Rank': 'S' }, { 'Rank': 'F' }, { 'Rank': 'O' }, { 'Rank': 'P' }] self.assertListEqual(obs[3], exp) # BIOM table add_metacol(ob2, rankdic, 'Rank') self.assertListEqual(list(map(dict, ob2.metadata(axis='observation'))), exp) # unordered, missing value, append namedic = { 'G1': 'Proteo', 'G3': 'Actino', 'G2': 'Firmic', 'G4': 'Bacter' } add_metacol(obs, namedic, 'Name', missing='X') exp = [{ 'Rank': 'S', 'Name': 'Proteo' }, { 'Rank': 'S', 'Name': 'Firmic' }, { 'Rank': 'F', 'Name': 'Actino' }, { 'Rank': 'O', 'Name': 'Bacter' }, { 'Rank': 'P', 'Name': 'X' }] self.assertListEqual(obs[3], exp) add_metacol(ob2, namedic, 'Name', missing='X') self.assertListEqual(list(map(dict, ob2.metadata(axis='observation'))), exp)
def test_biom_add_metacol(self): obs = Table(*map( np.array, prep_table({ 'S1': { 'G1': 4, 'G2': 5, 'G3': 8, 'G4': 0, 'G5': 3 }, 'S2': { 'G1': 1, 'G2': 8, 'G3': 0, 'G4': 7, 'G5': 4 }, 'S3': { 'G1': 0, 'G2': 2, 'G3': 3, 'G4': 5, 'G5': 0 } }))) self.assertIsNone(obs.metadata(axis='observation')) rankdic = {'G1': 'S', 'G2': 'S', 'G3': 'F', 'G4': 'O', 'G5': 'P'} biom_add_metacol(obs, rankdic, 'Rank') exp = [{ 'Rank': 'S' }, { 'Rank': 'S' }, { 'Rank': 'F' }, { 'Rank': 'O' }, { 'Rank': 'P' }] self.assertListEqual(list(map(dict, obs.metadata(axis='observation'))), exp) namedic = { 'G1': 'Proteo', 'G3': 'Actino', 'G2': 'Firmic', 'G4': 'Bacter' } biom_add_metacol(obs, namedic, 'Name', missing='X') exp = [{ 'Rank': 'S', 'Name': 'Proteo' }, { 'Rank': 'S', 'Name': 'Firmic' }, { 'Rank': 'F', 'Name': 'Actino' }, { 'Rank': 'O', 'Name': 'Bacter' }, { 'Rank': 'P', 'Name': 'X' }] self.assertListEqual(list(map(dict, obs.metadata(axis='observation'))), exp)