def core_metrics(table: biom.Table, phylogeny: skbio.TreeNode, sampling_depth: int) -> (pd.Series, pd.Series, pd.Series, pd.Series, skbio.DistanceMatrix, skbio.DistanceMatrix, skbio.DistanceMatrix, skbio.DistanceMatrix, skbio.OrdinationResults, skbio.OrdinationResults, skbio.OrdinationResults, skbio.OrdinationResults): rarefied_table = rarefy(table=table, sampling_depth=sampling_depth) faith_pd_vector = alpha_phylogenetic( table=rarefied_table, phylogeny=phylogeny, metric='faith_pd') observed_otus_vector = alpha(table=rarefied_table, metric='observed_otus') shannon_vector = alpha(table=rarefied_table, metric='shannon') evenness_vector = alpha(table=rarefied_table, metric='pielou_e') unweighted_unifrac_distance_matrix = beta_phylogenetic( table=rarefied_table, phylogeny=phylogeny, metric='unweighted_unifrac') weighted_unifrac_distance_matrix = beta_phylogenetic( table=rarefied_table, phylogeny=phylogeny, metric='weighted_unifrac') jaccard_distance_matrix = beta(table=rarefied_table, metric='jaccard') bray_curtis_distance_matrix = beta( table=rarefied_table, metric='braycurtis') unweighted_unifrac_pcoa_results = pcoa( distance_matrix=unweighted_unifrac_distance_matrix) weighted_unifrac_pcoa_results = pcoa( distance_matrix=weighted_unifrac_distance_matrix) jaccard_pcoa_results = pcoa(distance_matrix=jaccard_distance_matrix) bray_curtis_pcoa_results = pcoa( distance_matrix=bray_curtis_distance_matrix) return ( faith_pd_vector, observed_otus_vector, shannon_vector, evenness_vector, unweighted_unifrac_distance_matrix, weighted_unifrac_distance_matrix, jaccard_distance_matrix, bray_curtis_distance_matrix, unweighted_unifrac_pcoa_results, weighted_unifrac_pcoa_results, jaccard_pcoa_results, bray_curtis_pcoa_results )
def test_parallel_beta(self): t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'], ['S1', 'S2', 'S3']) parallel = beta(table=t, metric='braycurtis', n_jobs=-1) single_thread = beta(table=t, metric='braycurtis', n_jobs=1) # expected computed with scipy.spatial.distance.braycurtis expected = skbio.DistanceMatrix([[0.0000000, 0.3333333, 0.6666667], [0.3333333, 0.0000000, 0.4285714], [0.6666667, 0.4285714, 0.0000000]], ids=['S1', 'S2', 'S3']) self.assertEqual(parallel.ids, expected.ids) self.assertEqual(single_thread.ids, expected.ids) for id1 in parallel.ids: for id2 in parallel.ids: npt.assert_almost_equal(parallel[id1, id2], expected[id1, id2]) for id1 in single_thread.ids: for id2 in single_thread.ids: npt.assert_almost_equal(single_thread[id1, id2], expected[id1, id2])
def test_beta_canberra_adkins(self): t = Table(np.array([[0, 0], [0, 1], [1, 2]]), ['O1', 'O2', 'O3'], ['S1', 'S2']) d = (1. / 2.) * sum([abs(0. - 1.) / (0. + 1.), abs(1. - 2.) / (1. + 2.)]) expected = skbio.DistanceMatrix(np.array([[0.0, d], [d, 0.0]]), ids=['S1', 'S2']) actual = beta(table=t, metric='canberra_adkins') self.assertEqual(actual.ids, expected.ids) for id1 in actual.ids: for id2 in actual.ids: npt.assert_almost_equal(actual[id1, id2], expected[id1, id2])
def test_aitchison(self): t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'], ['S1', 'S2', 'S3']) actual = beta(table=t, metric='aitchison') expected = skbio.DistanceMatrix([[0.0000000, 0.4901290, 0.6935510], [0.4901290, 0.0000000, 0.2034219], [0.6935510, 0.2034219, 0.0000000]], ids=['S1', 'S2', 'S3']) self.assertEqual(actual.ids, expected.ids) for id1 in actual.ids: for id2 in actual.ids: npt.assert_almost_equal(actual[id1, id2], expected[id1, id2])
def test_beta(self): t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'], ['S1', 'S2', 'S3']) actual = beta(table=t, metric='braycurtis') # expected computed with scipy.spatial.distance.braycurtis expected = skbio.DistanceMatrix([[0.0000000, 0.3333333, 0.6666667], [0.3333333, 0.0000000, 0.4285714], [0.6666667, 0.4285714, 0.0000000]], ids=['S1', 'S2', 'S3']) self.assertEqual(actual.ids, expected.ids) for id1 in actual.ids: for id2 in actual.ids: npt.assert_almost_equal(actual[id1, id2], expected[id1, id2])
def test_beta_unknown_metric(self): t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'], ['S1', 'S2', 'S3']) with self.assertRaises(ValueError): beta(table=t, metric='not-a-metric')
def test_beta_phylo_metric(self): t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'], ['S1', 'S2', 'S3']) with self.assertRaises(ValueError): beta(table=t, metric='unweighted_unifrac')
def test_beta_empty_table(self): t = Table(np.array([]), [], []) with self.assertRaisesRegex(ValueError, 'empty'): beta(table=t, metric='braycurtis')