-
Notifications
You must be signed in to change notification settings - Fork 14
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 i #366
Open
mschwoer
wants to merge
9
commits into
remove_sample_column
Choose a base branch
from
refactor_analysis_I
base: remove_sample_column
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refactor analysis i #366
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
691d4ed
show analysis result next to parameter selection
mschwoer 8d6d69c
add Analysis class and refactor volcano
mschwoer 61d4f78
remove Entrez.email
mschwoer 2a5dd61
remove Volcano Options
mschwoer d9e83d4
add some TODOs
mschwoer 3edd671
remove biopython dependency
mschwoer 9745b0a
refactor show_widget
mschwoer 943e11c
fix widget logic
mschwoer f8bb1dd
remove accidental commit
mschwoer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
"""Module providing frontend widgets for gathering parameters and mapping them to the actual analysis.""" | ||
|
||
from abc import ABC, abstractmethod | ||
from collections import defaultdict | ||
|
||
import streamlit as st | ||
|
||
from alphastats.DataSet import DataSet | ||
from alphastats.keys import Cols | ||
from alphastats.plots.VolcanoPlot import VolcanoPlot | ||
|
||
|
||
class Analysis(ABC): | ||
"""Abstract class for analysis widgets.""" | ||
|
||
def __init__(self, dataset): | ||
self._dataset: DataSet = dataset | ||
self._parameters = defaultdict(lambda: None) | ||
|
||
@abstractmethod | ||
def show_widget(self): | ||
"""Show the widget and gather parameters.""" | ||
pass | ||
|
||
@abstractmethod | ||
def do_analysis(self): | ||
"""Perform the analysis. | ||
|
||
Returns a tuple(figure, analysis_object, parameters) where figure is the plot, | ||
analysis_object is the underlying object, parameters is a dictionary of the parameters used. | ||
""" | ||
pass | ||
|
||
|
||
class GroupCompareAnalysis(Analysis, ABC): | ||
"""Abstract class for group comparison analysis widgets.""" | ||
|
||
def show_widget(self): | ||
"""Gather parameters to compare two group.""" | ||
|
||
metadata = self._dataset.metadata | ||
|
||
default_option = "<select>" | ||
custom_group_option = "Custom group from samples .." | ||
|
||
grouping_variable = st.selectbox( | ||
"Grouping variable", | ||
options=[default_option] | ||
+ metadata.columns.to_list() | ||
+ [custom_group_option], | ||
) | ||
|
||
column = None | ||
if grouping_variable == default_option: | ||
st.stop() # TODO: using stop here is not really great | ||
elif grouping_variable != custom_group_option: | ||
unique_values = metadata[grouping_variable].unique().tolist() | ||
|
||
column = grouping_variable | ||
group1 = st.selectbox("Group 1", options=unique_values) | ||
group2 = st.selectbox("Group 2", options=list(reversed(unique_values))) | ||
|
||
else: | ||
group1 = st.multiselect( | ||
"Group 1 samples:", | ||
options=metadata[Cols.SAMPLE].to_list(), | ||
) | ||
|
||
group2 = st.multiselect( | ||
"Group 2 samples:", | ||
options=list(reversed(metadata[Cols.SAMPLE].to_list())), | ||
) | ||
|
||
intersection_list = list(set(group1).intersection(set(group2))) | ||
if len(intersection_list) > 0: | ||
st.warning( | ||
"Group 1 and Group 2 contain same samples: " | ||
+ str(intersection_list) | ||
) | ||
|
||
if group1 == group2: | ||
st.error( | ||
"Group 1 and Group 2 can not be the same. Please select different groups." | ||
) | ||
st.stop() | ||
|
||
self._parameters.update({"group1": group1, "group2": group2}) | ||
if column is not None: | ||
self._parameters["column"] = column | ||
|
||
|
||
class VolcanoPlotAnalysis(GroupCompareAnalysis): | ||
"""Widget for Volcano Plot analysis.""" | ||
|
||
def show_widget(self): | ||
"""Show the widget and gather parameters.""" | ||
super().show_widget() | ||
|
||
parameters = {} | ||
method = st.selectbox( | ||
"Differential Analysis using:", | ||
options=["ttest", "anova", "wald", "sam", "paired-ttest", "welch-ttest"], | ||
) | ||
parameters["method"] = method | ||
|
||
parameters["labels"] = st.checkbox("Add labels", value=True) | ||
|
||
parameters["draw_line"] = st.checkbox("Draw lines", value=True) | ||
|
||
parameters["alpha"] = st.number_input( | ||
label="alpha", min_value=0.001, max_value=0.050, value=0.050 | ||
) | ||
|
||
parameters["min_fc"] = st.select_slider( | ||
"Foldchange cutoff", range(0, 3), value=1 | ||
) | ||
|
||
if method == "sam": | ||
parameters["perm"] = st.number_input( | ||
label="Number of Permutations", min_value=1, max_value=1000, value=10 | ||
) | ||
parameters["fdr"] = st.number_input( | ||
label="FDR cut off", min_value=0.005, max_value=0.1, value=0.050 | ||
) | ||
|
||
self._parameters.update(parameters) | ||
|
||
def do_analysis(self): | ||
"""Draw Volcano Plot using the VolcanoPlot class. | ||
|
||
Returns a tuple(figure, analysis_object, parameters) where figure is the plot, | ||
analysis_object is the underlying object, parameters is a dictionary of the parameters used. | ||
""" | ||
# TODO currently there's no other way to obtain both the plot and the underlying data | ||
# Should be refactored such that the interface provided by DateSet.plot_volcano() is used | ||
# One option could be to always return the whole analysis object. | ||
|
||
volcano_plot = VolcanoPlot( | ||
mat=self._dataset.mat, | ||
rawinput=self._dataset.rawinput, | ||
metadata=self._dataset.metadata, | ||
preprocessing_info=self._dataset.preprocessing_info, | ||
group1=self._parameters["group1"], | ||
group2=self._parameters["group2"], | ||
column=self._parameters["column"], | ||
method=self._parameters["method"], | ||
labels=self._parameters["labels"], | ||
min_fc=self._parameters["min_fc"], | ||
alpha=self._parameters["alpha"], | ||
draw_line=self._parameters["draw_line"], | ||
perm=self._parameters["perm"], | ||
fdr=self._parameters["fdr"], | ||
color_list=self._parameters["color_list"], | ||
) | ||
return volcano_plot.plot, volcano_plot, self._parameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
anndata==0.9.1 | ||
biopython==1.83 | ||
click==8.0.1 | ||
combat==0.3.3 | ||
data_cache>=0.1.6 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logic is copied from
helper_compare_two_groups