Skip to content

Commit 49ede05

Browse files
committed
eaf method and visualizer
1 parent 5d2c65e commit 49ede05

File tree

4 files changed

+139
-37
lines changed

4 files changed

+139
-37
lines changed

q2_qsip2/plugin_setup.py

+70-30
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@
88

99
import importlib
1010

11-
from qiime2.plugin import Citations, Int, List, Metadata, Plugin, Str
11+
from qiime2.plugin import Citations, Float, Int, List, Metadata, Plugin, Str
1212
from q2_types.feature_table import FeatureTable, Frequency
1313

1414
from q2_qsip2 import __version__
1515
from q2_qsip2.types import QSIP2Data, Unfiltered, Filtered, EAF
1616
from q2_qsip2.workflow import (
17-
standard_workflow, create_qsip_data, subset_and_filter
17+
standard_workflow, create_qsip_data, subset_and_filter,
18+
resample_and_calculate_EAF
1819
)
1920
from q2_qsip2.visualizers._visualizers import (
2021
plot_weighted_average_densities, plot_sample_curves, plot_density_outliers,
21-
show_comparison_groups, plot_filtered_features
22+
show_comparison_groups, plot_filtered_features, plot_excess_atom_fractions
2223
)
2324

2425

@@ -34,34 +35,9 @@
3435
"data."
3536
),
3637
short_description="Analyze qSIP data.",
37-
# TODO
3838
citations=[citations['Caporaso-Bolyen-2024']]
3939
)
4040

41-
plugin.methods.register_function(
42-
function=standard_workflow,
43-
inputs={
44-
'table': FeatureTable[Frequency],
45-
'qsip_metadata': QSIP2Data[Unfiltered],
46-
},
47-
parameters={},
48-
outputs=[
49-
('output_table', FeatureTable[Frequency])
50-
],
51-
input_descriptions={
52-
'table': 'The feature table.',
53-
'qsip_metadata': 'The qSIP metadata.',
54-
},
55-
parameter_descriptions={},
56-
output_descriptions={
57-
'output_table': 'Placeholder.'
58-
},
59-
name='Run the standard qSIP2 workflow.',
60-
description=(
61-
'Placeholder.'
62-
)
63-
)
64-
6541
plugin.methods.register_function(
6642
function=create_qsip_data,
6743
inputs={
@@ -99,7 +75,8 @@
9975
name='Bundle your qSIP metadata and feature table.',
10076
description=(
10177
'Placeholder.'
102-
)
78+
),
79+
citations=[]
10380
)
10481

10582
plugin.methods.register_function(
@@ -147,7 +124,40 @@
147124
name='Subset sources and filter features to prepare for comparison.',
148125
description=(
149126
'Placeholder.'
150-
)
127+
),
128+
citations=[]
129+
)
130+
131+
plugin.methods.register_function(
132+
function=resample_and_calculate_EAF,
133+
inputs={
134+
'filtered_qsip_data': QSIP2Data[Filtered]
135+
},
136+
parameters={
137+
'resamples': Int,
138+
'random_seed': Int,
139+
},
140+
outputs=[
141+
('eaf_qsip_data', QSIP2Data[EAF])
142+
],
143+
input_descriptions={
144+
'filtered_qsip_data': 'Your filtered qSIP2 data.'
145+
},
146+
parameter_descriptions={
147+
'resamples': 'The number of bootstrap resamplings to perform.',
148+
'random_seed': 'The random seed to use during resampling.',
149+
},
150+
output_descriptions={
151+
'eaf_qsip_data': (
152+
'Your qSIP2 data with excess atom fraction (EAF) values '
153+
'calculated on a per-taxon basis.'
154+
)
155+
},
156+
name='Calculate excess atom fraction (EAF).',
157+
description=(
158+
'Placeholder.'
159+
),
160+
citations=[]
151161
)
152162

153163
plugin.visualizers.register_function(
@@ -249,4 +259,34 @@
249259
citations=[],
250260
)
251261

262+
plugin.visualizers.register_function(
263+
function=plot_excess_atom_fractions,
264+
inputs={
265+
'eaf_qsip_data': QSIP2Data[EAF],
266+
},
267+
parameters={
268+
'num_top': Int,
269+
'confidence_interval': Float
270+
},
271+
input_descriptions={
272+
'eaf_qsip_data': 'Your EAF-calculated qSIP2 data.',
273+
},
274+
parameter_descriptions={
275+
'num_top': (
276+
'The number of taxa displayed, selected in order of decreasing '
277+
'excess atom fraction.'
278+
),
279+
'confidence_interval': (
280+
'The confidence interval to display from the bootstrapped excess '
281+
'atom fractions.'
282+
)
283+
},
284+
name='Visualize per-taxon excess atom fractions.',
285+
description=(
286+
'Plots per-taxon excess atom fractions with bootstrapped confidence '
287+
'intervals.'
288+
),
289+
citations=[]
290+
)
291+
252292
importlib.import_module('q2_qsip2.types._deferred_setup')

q2_qsip2/types/_deferred_setup/_transformers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _4(ff: QSIP2DataFilteredFormat) -> RS4:
5454

5555
@plugin.register_transformer
5656
def _5(qsip_object: RS4) -> QSIP2DataEAFFormat:
57-
ff = QSIP2DataFilteredFormat()
57+
ff = QSIP2DataEAFFormat()
5858
return _qsip_object_to_format(qsip_object, ff)
5959

6060

q2_qsip2/visualizers/_visualizers.py

+31
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,34 @@ def plot_filtered_features(output_dir: str, filtered_qsip_data: RS4) -> None:
124124
_ggplot2_object_to_visualization(
125125
plot, Path(output_dir), width=10, height=10
126126
)
127+
128+
129+
def plot_excess_atom_fractions(
130+
output_dir: str,
131+
eaf_qsip_data: RS4,
132+
num_top: int,
133+
confidence_interval: float = 0.9
134+
) -> None:
135+
'''
136+
Plots per-taxon excess atom fraction values.
137+
138+
Parameters
139+
----------
140+
output_dir : str
141+
The root directory of the visualization loaded into the browser.
142+
qsip_data : RS4
143+
The "qsip_data" object.
144+
num_top : int
145+
The number of taxa displayed taken in order of decreasing excess
146+
atom fraction.
147+
confidence_interval : float
148+
The confidence interval to display from the bootstrapped excess atom
149+
fraction values.
150+
'''
151+
plot = qsip2.plot_EAF_values(
152+
eaf_qsip_data, top=num_top, confidence=confidence_interval, error='bar'
153+
)
154+
155+
_ggplot2_object_to_visualization(
156+
plot, Path(output_dir), width=10, height=10
157+
)

q2_qsip2/workflow.py

+37-6
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,10 @@ def subset_and_filter(
127127
qsip_data: RS4,
128128
unlabeled_sources: list[str],
129129
labeled_sources: list[str],
130-
min_unlabeled_sources: int,
131-
min_labeled_sources: int,
132-
min_unlabeled_fractions: int,
133-
min_labeled_fractions: int
130+
min_unlabeled_sources: int = 1,
131+
min_labeled_sources: int = 1,
132+
min_unlabeled_fractions: int = 1,
133+
min_labeled_fractions: int = 1
134134
) -> RS4:
135135
'''
136136
Subsets the qsip data object to include only those sources listed in
@@ -158,14 +158,45 @@ def subset_and_filter(
158158
The minimum number of fractions a feature must be present in
159159
to be considered present in a labeled source.
160160
'''
161+
unlabeled_sources_vector = ro.vectors.StrVector(unlabeled_sources)
162+
labeled_sources_vector = ro.vectors.StrVector(labeled_sources)
163+
161164
filtered_qsip_data = qsip2.run_feature_filter(
162165
qsip_data,
163-
unlabeled_source_mat_ids=unlabeled_sources,
164-
labeled_source_mat_ids=labeled_sources,
166+
unlabeled_source_mat_ids=unlabeled_sources_vector,
167+
labeled_source_mat_ids=labeled_sources_vector,
165168
min_unlabeled_sources=min_unlabeled_sources,
166169
min_labeled_sources=min_labeled_sources,
167170
min_unlabeled_fractions=min_unlabeled_fractions,
168171
min_labeled_fractions=min_labeled_fractions
169172
)
170173

171174
return filtered_qsip_data
175+
176+
177+
def resample_and_calculate_EAF(
178+
filtered_qsip_data: RS4,
179+
resamples: int = 1000,
180+
random_seed: int = 1,
181+
) -> RS4:
182+
'''
183+
Reseample and calculate excess atom fraction (EAF) for each feature.
184+
185+
Parameters
186+
----------
187+
filtered_qsip_data : RS4
188+
The filtered "qsip_data" object.
189+
resamples : int
190+
The number of bootstrap resamplings to perform.
191+
random_seed : int
192+
The random seed to use during resampling. Exposed for reproducibility.
193+
'''
194+
resampled_qsip_data = qsip2.run_resampling(
195+
filtered_qsip_data,
196+
resamples=resamples,
197+
with_seed=random_seed
198+
)
199+
200+
eaf_qsip_data = qsip2.run_EAF_calculations(resampled_qsip_data)
201+
202+
return eaf_qsip_data

0 commit comments

Comments
 (0)