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

Refactor analysis v #370

Open
wants to merge 2 commits into
base: refactor_analysis_IV
Choose a base branch
from
Open
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
20 changes: 5 additions & 15 deletions alphastats/gui/pages/04_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
display_plot,
do_analysis,
)
from alphastats.gui.utils.options import (
get_statistic_options,
)
from alphastats.gui.utils.ui_helper import (
StateKeys,
convert_df,
Expand All @@ -35,6 +32,8 @@
"""
st.markdown(styl, unsafe_allow_html=True)

# TODO put everything in the session state for a given parameter set?
# or is caching functionality the way to go here?

if StateKeys.DATASET not in st.session_state:
st.info("Import Data first")
Expand All @@ -55,12 +54,7 @@
+ ["------- plots -------"]
+ plotting_options
+ ["------- statistics -------"]
+ statistic_options
+ [
k
for k in get_statistic_options(st.session_state)
if k not in statistic_options
],
+ statistic_options,
)

if method in plotting_options:
Expand All @@ -69,14 +63,10 @@
)
show_plot = analysis_result is not None

elif (
method
in list((options := get_statistic_options(st.session_state)).keys())
+ statistic_options
):
elif method in statistic_options:
analysis_result, *_ = do_analysis(
method,
options_dict=options,
options_dict=None,
)
show_df = analysis_result is not None

Expand Down
66 changes: 66 additions & 0 deletions alphastats/gui/utils/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class PlottingOptions(metaclass=ConstantsClass):
class StatisticOptions(metaclass=ConstantsClass):
DIFFERENTIAL_EXPRESSION = "Differential Expression Analysis"
TUKEY_TEST = "Tukey-Test"
ANOVA = "ANOVA"
ANCOVA = "ANCOVA"


class Analysis(ABC):
Expand Down Expand Up @@ -384,3 +386,67 @@ def _do_analysis(self):
group=self._parameters["group"],
)
return tukey_test_analysis, None, self._parameters


class AnovaAnalysis(GroupCompareAnalysis):
"""Widget for ANOVA analysis."""

def show_widget(self):
"""Show the widget and gather parameters."""

column = st.selectbox(
"A variable from the metadata to calculate ANOVA",
options=self._dataset.metadata.columns.to_list(),
)
protein_ids = st.selectbox(
"All ProteinIDs/or specific ProteinID to perform ANOVA",
options=["all"] + self._dataset.mat.columns.to_list(),
)

tukey = st.checkbox("Follow-up Tukey")

self._parameters.update(
{"column": column, "protein_ids": protein_ids, "tukey": tukey}
)

def _do_analysis(self):
"""Perform Anova analysis."""
anova_analysis = self._dataset.anova(
column=self._parameters["column"],
protein_ids=self._parameters["protein_ids"],
tukey=self._parameters["tukey"],
)
return anova_analysis, None, self._parameters


class AncovaAnalysis(Analysis):
"""Widget for Ancova analysis."""

def show_widget(self):
"""Show the widget and gather parameters."""

protein_id = st.selectbox(
"ProteinID/ProteinGroup",
options=self._dataset.mat.columns.to_list(),
)
covar = st.selectbox(
"Name(s) of column(s) in metadata with the covariate.",
options=self._dataset.metadata.columns.to_list(),
) # TODO: why plural if only one can be selected?
between = st.selectbox(
"Name of the column in the metadata with the between factor.",
options=self._dataset.metadata.columns.to_list(),
)

self._parameters.update(
{"protein_id": protein_id, "covar": covar, "between": between}
)

def _do_analysis(self):
"""Perform ANCOVA analysis."""
ancova_analysis = self._dataset.ancova(
protein_id=self._parameters["protein_id"],
covar=self._parameters["covar"],
between=self._parameters["between"],
)
return ancova_analysis, None, self._parameters
4 changes: 4 additions & 0 deletions alphastats/gui/utils/analysis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import streamlit as st

from alphastats.gui.utils.analysis import (
AncovaAnalysis,
AnovaAnalysis,
ClustermapAnalysis,
DifferentialExpressionAnalysis,
IntensityPlot,
Expand Down Expand Up @@ -152,6 +154,8 @@ def do_analysis(
# "Dendrogram": dataet.plot_dendrogram}, # TODO this was commented out in the original code?
StatisticOptions.DIFFERENTIAL_EXPRESSION: DifferentialExpressionAnalysis,
StatisticOptions.TUKEY_TEST: TukeyTestAnalysis,
StatisticOptions.ANOVA: AnovaAnalysis,
StatisticOptions.ANCOVA: AncovaAnalysis,
}

if (analysis_class := options.get(method)) is not None:
Expand Down
41 changes: 0 additions & 41 deletions alphastats/gui/utils/options.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,11 @@
from alphastats import SpectronautLoader
from alphastats.gui.utils.ui_helper import StateKeys
from alphastats.loader.AlphaPeptLoader import AlphaPeptLoader
from alphastats.loader.DIANNLoader import DIANNLoader
from alphastats.loader.FragPipeLoader import FragPipeLoader
from alphastats.loader.GenericLoader import GenericLoader
from alphastats.loader.MaxQuantLoader import MaxQuantLoader
from alphastats.loader.mzTabLoader import mzTabLoader


def get_statistic_options(state):
dataset = state[StateKeys.DATASET]
metadata_options = dataset.metadata.columns.to_list()
statistic_options = {
"ANOVA": {
"settings": {
"column": {
"options": metadata_options,
"label": "A variable from the metadata to calculate ANOVA",
},
"protein_ids": {
"options": ["all"] + dataset.mat.columns.to_list(),
"label": "All ProteinIDs/or specific ProteinID to perform ANOVA",
},
"tukey": {"label": "Follow-up Tukey"},
},
"function": dataset.anova,
},
"ANCOVA": {
"settings": {
"protein_id": {
"options": [None] + dataset.mat.columns.to_list(),
"label": "Color according to",
},
"covar": {
"options": metadata_options,
"label": "Name(s) of column(s) in metadata with the covariate.",
},
"between": {
"options": metadata_options,
"label": "Name of the column in the metadata with the between factor.",
},
},
"function": dataset.ancova,
},
}
return statistic_options


SOFTWARE_OPTIONS = {
"MaxQuant": {
"import_file": "Please upload proteinGroups.txt",
Expand Down
Loading