Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editable pipeline #16

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions q2_boots/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
alpha_average)
from ._version import get_versions
from .beta import (beta, get_medoid, beta_collection, per_cell_average, beta_average)
from .core_metrics import (core_metrics)
from .diversity_metrics import diversity_metrics
from . import _version

__all__ = ['_bootstrap_iteration', 'resample',
'alpha',
'alpha_collection',
'beta',
'core_metrics_alpha_bootstrap',
'core_metrics',
'diversity_metrics_alpha_bootstrap',
'diversity_metrics',
'get_medoid',
'per_cell_average',
'beta_collection',
Expand Down
95 changes: 0 additions & 95 deletions q2_boots/core_metrics.py

This file was deleted.

140 changes: 140 additions & 0 deletions q2_boots/diversity_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# ----------------------------------------------------------------------------
# Copyright (c) 2023, QIIME 2 development team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ----------------------------------------------------------------------------

from q2_diversity._alpha import METRICS as ALPHA_METRICS
from q2_diversity._beta import METRICS as BETA_METRICS


def diversity_metrics(ctx, table, sampling_depth, metadata,
n_jobs=1, phylogeny=None, n=100, alpha_method='median',
beta_method='non-metric-median', with_replacement=True,
random_seed=None, alpha_metrics=["observed_features",
"shannon",
"pielou_e",
"faith_pd"],
beta_metrics=["jaccard", "braycurtis",
"weighted_unifrac", "unweighted_unifrac"]):

bootstrap = ctx.get_action('boots', 'resample')
pcoa = ctx.get_action('diversity', 'pcoa')
emperor_plot = ctx.get_action('emperor', 'plot')
alpha_average = ctx.get_action('boots', 'alpha_average')
beta_average = ctx.get_action('boots', 'beta_average')

bootstrapped_tables, = bootstrap(table=table,
sampling_depth=sampling_depth,
n=n, with_replacement=with_replacement,
random_seed=random_seed)

tables = bootstrapped_tables.values()

alpha_metric_actions = {'PHYLO': [], 'NONPHYLO': []}

for metric in alpha_metrics:
if metric in ALPHA_METRICS["PHYLO"]["IMPL"] or metric in\
ALPHA_METRICS["PHYLO"]["UNIMPL"]:
metric = ALPHA_METRICS['NAME_TRANSLATIONS'][metric]
alpha_metric_actions['PHYLO'].append(ctx.get_action('diversity_lib',
metric))
else:
if metric in ALPHA_METRICS['NONPHYLO']['IMPL']:
metric = ALPHA_METRICS['NAME_TRANSLATIONS'][metric]
alpha_metric_actions['NONPHYLO'].append(ctx.get_action('diversity_lib',
metric))
else:
alpha_metric_actions['NONPHYLO'].append(
ctx.get_action('diversity_lib', 'alpha_passthrough'))

beta_metric_actions = {'PHYLO': [], 'NONPHYLO': []}

for metric in beta_metrics:
if metric in BETA_METRICS["PHYLO"]["IMPL"] or metric in\
BETA_METRICS["PHYLO"]["UNIMPL"]:
if metric in ('unweighted_unifrac', 'weighted_unifrac'):
metric = BETA_METRICS['NAME_TRANSLATIONS'][metric]
beta_metric_actions['PHYLO'].append(ctx.get_action('diversity_lib',
metric))
else:
# handle unimplemented unifracs
beta_metric_actions['PHYLO'].append(
ctx.get_action('diversity_lib', 'beta_phylogenetic_passthrough'))
else:

if metric in BETA_METRICS['NONPHYLO']['IMPL']:
metric = BETA_METRICS['NAME_TRANSLATIONS'][metric]
beta_metric_actions['NONPHYLO'].append(ctx.get_action('diversity_lib',
metric))
else:
beta_metric_actions['NONPHYLO'].append(
ctx.get_action('diversity_lib', 'beta_passthrough'))

if phylogeny is None and (len(alpha_metrics['PHYLO']) > 0 or
len(beta_metrics['PHYLO']) > 0):
print("WARNING: NO PHYLOGENY PROVIDED, PHYLOGENETIC METRICS WILL NOT BE RUN")

alphas = {}
for m in (alpha_metrics['NONPHYLO']):
alpha = alpha_representative(m, tables, alpha_method)
res, = alpha_average(data=alpha, average_method=alpha_method)
alphas[m.__name__] = res
if phylogeny is not None:
for m in (alpha_metrics['PHYLO']):
alpha = alpha_representative(m, tables, alpha_method, phylogeny=phylogeny)
res, = alpha_average(data=alpha, average_method=alpha_method)
alphas[m.__name__] = res

dms = {}
for m in (beta_metrics['NONPHYLO']):
beta_results = beta_representative(m, tables, beta_method)
res, = beta_average(data=beta_results, representative=beta_method)
dms[m.__name__] = res
if phylogeny is not None:
for m in (beta_metrics['PHYLO']):
beta_results = beta_representative(m, tables, beta_method, phylogeny,
n_threads=n_jobs)
beta_results, = beta_average(data=beta_results,
representative=beta_method)
dms[m.__name__] = beta_results

pcoas = {}
for key, dm in dms.items():
pcoa_results, = pcoa(distance_matrix=dm)
pcoas[key] = pcoa_results

visualizations = {}
for key, pcoa in pcoas.items():
visualizations[key] = (emperor_plot(pcoa=pcoa, metadata=metadata)[0])

return bootstrapped_tables, alphas, dms, pcoas, visualizations


def beta_representative(func, tables, method,
phylogeny=None, n_threads=1):
metric = []
if phylogeny is None:
for table in tables:
metric.append(func(table=table)[0])
else:
for table in tables:
metric.append(func(table=table, phylogeny=phylogeny,
threads=n_threads)[0])

return metric


def alpha_representative(func, tables, method, phylogeny=None):

alpha = []
if phylogeny is None:
for table in tables:
alpha.append(func(table=table)[0])
else:
for table in tables:
alpha.append(func(table=table, phylogeny=phylogeny)[0])

return alpha
16 changes: 12 additions & 4 deletions q2_boots/plugin_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# ----------------------------------------------------------------------------

from qiime2.plugin import (Plugin, Int, Range, Collection,
Citations, Str, Choices, Bool, Float,
Citations, Str, Choices, Bool, Float, List,
Metadata, Visualization)

from q2_types.feature_table import (
Expand Down Expand Up @@ -280,7 +280,7 @@
)

plugin.pipelines.register_function(
function=q2_boots.core_metrics,
function=q2_boots.diversity_metrics,
inputs={
'table': FeatureTable[Frequency | RelativeFrequency |
PresenceAbsence],
Expand All @@ -296,7 +296,15 @@
'non-metric-median',
'medoid'),
'with_replacement': Bool,
'random_seed': Int
'random_seed': Int,
'alpha_metrics' : List[Str % Choices(alpha_metrics['NONPHYLO']['IMPL'] |
alpha_metrics['NONPHYLO']['UNIMPL'] |
alpha_metrics['PHYLO']['IMPL'] |
alpha_metrics['PHYLO']['UNIMPL'])],
'beta_metrics': List[Str % Choices(beta_metrics['NONPHYLO']['IMPL'] |
beta_metrics['NONPHYLO']['UNIMPL'] |
beta_metrics['PHYLO']['IMPL'] |
beta_metrics['PHYLO']['UNIMPL'])],
},
outputs=[
('rarefied_table', Collection[FeatureTable[Frequency]]),
Expand All @@ -314,6 +322,6 @@
output_descriptions={

},
name='Core Metrics',
name='Diversity Metrics',
description='Bootstrapped Core Metrics'
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
from io import StringIO


class TestCoreMetrics(TestPluginBase):
class TestDiversityMetrics(TestPluginBase):

package = 'q2_boots'

def setUp(self):
super().setUp()
self.core_metrics = self.plugin.pipelines['core_metrics']
self.diversity_metrics = self.plugin.pipelines['diversity_metrics']

def test_basic(self):
t = Table(data=np.array([[1000, 1920, 3451], [4536, 1552, 6521],
Expand All @@ -35,7 +35,7 @@ def test_basic(self):
columns=['blank'])
metadata.index.name = 'sample-id'
metadata = Metadata(metadata)
output = self.core_metrics(table=t,
output = self.diversity_metrics(table=t,
sampling_depth=500,
metadata=metadata,
n_jobs=1,
Expand Down Expand Up @@ -66,7 +66,7 @@ def test_phylogeny(self):
sample_ids=['S1', 'S2', 'S3'],
observation_ids=['O1', 'O2', 'O3'])
t = Artifact.import_data('FeatureTable[Frequency]', t)
output = self.core_metrics(table=t,
output = self.diversity_metrics(table=t,
sampling_depth=500,
metadata=metadata,
n_jobs=1,
Expand All @@ -79,3 +79,37 @@ def test_phylogeny(self):
for table in output[0].values():
table = Artifact.view(table, pd.DataFrame)
self.assertEqual(table.shape, (3, 3))

def test_custom_pipeline(self):
with StringIO('(O1:0.3, O2:0.2, O3:0.1, O4:0.2)root;') as f:
phylogeny = skbio.read(f, format='newick', into=skbio.TreeNode)

phylogeny = Artifact.import_data(
'Phylogeny[Rooted]',
phylogeny
)
metadata = pd.DataFrame(['not', 'of', 'interest'],
index=['S1', 'S2', 'S3'],
columns=['blank'])
metadata.index.name = 'sample-id'
metadata = Metadata(metadata)
t = Table(data=np.array([[1000, 1920, 3451], [2536, 1552, 1521],
[1634, 1634, 6721]]),
sample_ids=['S1', 'S2', 'S3'],
observation_ids=['O1', 'O2', 'O3'])
t = Artifact.import_data('FeatureTable[Frequency]', t)
output = self.diversity_metrics(table=t,
sampling_depth=500,
metadata=metadata,
n_jobs=1,
n=10,
phylogeny=phylogeny,
with_replacement=True,
alpha_metrics=["pielou_e", "faith_pd"],
beta_metrics=["jaccard", "unweighted_unifrac"]
)
self.assertEqual(len(output[0]), 10)

for table in output[0].values():
table = Artifact.view(table, pd.DataFrame)
self.assertEqual(table.shape, (3, 3))
Loading