Example #1
0
    def test_alpha_phylogenetic_empty_table(self):
        t = biom.Table(np.array([]), [], [])
        tree = skbio.TreeNode.read(io.StringIO(
            '((O1:0.25):0.25, O3:0.75)root;'))

        with self.assertRaisesRegex(ValueError, "empty"):
            alpha_phylogenetic(table=t, phylogeny=tree, metric='faith_pd')
Example #2
0
 def test_alpha_phylogenetic_unknown_metric(self):
     t = biom.Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     tree = skbio.TreeNode.read(
         io.StringIO('((O1:0.25, O2:0.50):0.25, O3:0.75)root;'))
     with self.assertRaises(ValueError):
         alpha_phylogenetic(table=t, phylogeny=tree, metric='not-a-metric')
Example #3
0
 def test_alpha_phylogenetic_unknown_metric(self):
     t = biom.Table(np.array([[0, 1, 3], [1, 1, 2]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     tree = skbio.TreeNode.read(io.StringIO(
         '((O1:0.25, O2:0.50):0.25, O3:0.75)root;'))
     with self.assertRaises(ValueError):
         alpha_phylogenetic(table=t, phylogeny=tree, metric='not-a-metric')
Example #4
0
 def test_alpha_phylogenetic_skbio_error_rewriting(self):
     t = biom.Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     tree = skbio.TreeNode.read(
         io.StringIO('((O1:0.25):0.25, O3:0.75)root;'))
     # Verify through regex that there is a ``feature_ids`` substring
     # followed by a ``phylogeny``
     with self.assertRaisesRegex(skbio.tree.MissingNodeError,
                                 'feature_ids.*phylogeny'):
         alpha_phylogenetic(table=t, phylogeny=tree, metric='faith_pd')
Example #5
0
 def test_alpha_phylogenetic_skbio_error_rewriting(self):
     t = biom.Table(np.array([[0, 1, 3], [1, 1, 2]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     tree = skbio.TreeNode.read(io.StringIO(
         '((O1:0.25):0.25, O3:0.75)root;'))
     # Verify through regex that there is a ``feature_ids`` substring
     # followed by a ``phylogeny``
     with self.assertRaisesRegex(skbio.tree.MissingNodeError,
                                 'feature_ids.*phylogeny'):
         alpha_phylogenetic(table=t, phylogeny=tree, metric='faith_pd')
Example #6
0
 def test_alpha_phylogenetic(self):
     t = biom.Table(np.array([[0, 1, 3], [1, 1, 2]]),
                    ['O1', 'O2'],
                    ['S1', 'S2', 'S3'])
     tree = skbio.TreeNode.read(io.StringIO(
         '((O1:0.25, O2:0.50):0.25, O3:0.75)root;'))
     actual = alpha_phylogenetic(table=t, phylogeny=tree, metric='faith_pd')
     # expected computed with skbio.diversity.alpha_diversity
     expected = pd.Series({'S1': 0.75, 'S2': 1.0, 'S3': 1.0},
                          name='faith_pd')
     pdt.assert_series_equal(actual, expected)
Example #7
0
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
    )
Example #8
0
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)