def test_rarefy_depth_error(self):
        t = Table(np.array([[0, 1, 3], [1, 1, 2]]),
                  ['O1', 'O2'],
                  ['S1', 'S2', 'S3'])

        with self.assertRaisesRegex(ValueError, 'shallow enough'):
            rarefy(t, 50)
    def test_rarefy_depth_error(self):
        t = Table(np.array([[0, 1, 3], [1, 1, 2]]),
                  ['O1', 'O2'],
                  ['S1', 'S2', 'S3'])

        with self.assertRaisesRegex(ValueError, 'shallow enough'):
            rarefy(t, 50)
Beispiel #3
0
    def test_rarefy_replacement(self):
        t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'],
                  ['S1', 'S2', 'S3'])
        rt = rarefy(t, 3, with_replacement=True)
        self.assertEqual(rt.shape, (2, 3))

        for n_draws in range(5, 100, 5):
            rt = rarefy(t, n_draws, with_replacement=True)
            npt.assert_array_equal(rt.sum('sample'), np.array([n_draws] * 3))
def rarefy_wrapper(state: PipelineState, target_count: int) -> PipelineState:
    table = Artifact.import_data("FeatureTable[Frequency]", state.df) \
        .view(biom.Table)
    table = rarefy(table, target_count)
    df = Artifact.import_data("FeatureTable[Frequency]", table) \
        .view(pd.DataFrame)
    return state.update_df(df)
Beispiel #5
0
 def test_rarefy(self):
     t = Table(np.array([[0, 1, 3], [1, 1, 2]]), ['O1', 'O2'],
               ['S1', 'S2', 'S3'])
     a = rarefy(t, 2)
     self.assertEqual(a.shape, (2, 2))
     self.assertEqual(set(a.ids(axis='sample')), set(['S2', 'S3']))
     self.assertEqual(set(a.ids(axis='observation')), set(['O1', 'O2']))
     npt.assert_array_equal(a.sum(axis='sample'), np.array([2., 2.]))
def _get_multiple_rarefaction(beta_func, metric, iterations, table,
                              sampling_depth):
    distance_matrices = []
    for _ in range(iterations):
        rarefied_table = q2_feature_table.rarefy(table, sampling_depth)
        distance_matrix = beta_func(table=rarefied_table, metric=metric)
        distance_matrices.append(distance_matrix)
    return distance_matrices
def _get_multiple_rarefaction(beta_func, metric, iterations, table,
                              sampling_depth):
    distance_matrices = []
    for _ in range(iterations):
        rarefied_table = q2_feature_table.rarefy(table, sampling_depth)
        distance_matrix = beta_func(table=rarefied_table, metric=metric)
        distance_matrices.append(distance_matrix)
    return distance_matrices
 def test_rarefy(self):
     t = Table(np.array([[0, 1, 3], [1, 1, 2]]),
               ['O1', 'O2'],
               ['S1', 'S2', 'S3'])
     a = rarefy(t, 2)
     self.assertEqual(a.shape, (2, 2))
     self.assertEqual(set(a.ids(axis='sample')), set(['S2', 'S3']))
     self.assertEqual(set(a.ids(axis='observation')), set(['O1', 'O2']))
     npt.assert_array_equal(a.sum(axis='sample'), np.array([2., 2.]))
Beispiel #9
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
    )
Beispiel #10
0
def _compute_rarefaction_data(feature_table, min_depth, max_depth, steps,
                              iterations, phylogeny, metrics):
    depth_range = np.linspace(min_depth, max_depth, num=steps, dtype=int)
    iter_range = range(1, iterations + 1)

    rows = feature_table.ids(axis='sample')
    cols = pd.MultiIndex.from_product(
        [list(depth_range), list(iter_range)], names=['depth', 'iter'])
    data = {k: pd.DataFrame(np.NaN, index=rows, columns=cols) for k in metrics}

    for d, i in itertools.product(depth_range, iter_range):
        rt = rarefy(feature_table, d)
        for m in metrics:
            if m in phylogenetic_metrics():
                vector = alpha_phylogenetic(table=rt,
                                            metric=m,
                                            phylogeny=phylogeny)
            else:
                vector = alpha(table=rt, metric=m)
            data[m][(d, i)] = vector
    return data
Beispiel #11
0
def _compute_rarefaction_data(feature_table, min_depth, max_depth, steps,
                              iterations, phylogeny, metrics):
    depth_range = np.linspace(min_depth, max_depth, num=steps, dtype=int)
    iter_range = range(1, iterations + 1)

    rows = feature_table.ids(axis='sample')
    cols = pd.MultiIndex.from_product([list(depth_range), list(iter_range)],
                                      names=['depth', 'iter'])
    data = {k: pd.DataFrame(np.NaN, index=rows, columns=cols)
            for k in metrics}

    for d, i in itertools.product(depth_range, iter_range):
        rt = rarefy(feature_table, d)
        for m in metrics:
            if m in phylogenetic_metrics():
                vector = alpha_phylogenetic(table=rt, metric=m,
                                            phylogeny=phylogeny)
            else:
                vector = alpha(table=rt, metric=m)
            data[m][(d, i)] = vector
    return data
Beispiel #12
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)