Skip to content

Commit

Permalink
refactor _add_metadata_column
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwoer committed Sep 18, 2024
1 parent e6caf03 commit 0e0ab54
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions alphastats/plots/VolcanoPlot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict
from typing import Dict, List, Union

from alphastats.DataSet_Statistics import Statistics
from alphastats.DataSet_Preprocess import PreprocessingStateKeys
Expand Down Expand Up @@ -44,10 +44,10 @@ def __init__(
index_column: str,
gene_names: str,
preprocessing_info: Dict,
group1,
group2,
column=None,
method=None,
group1: Union[List[str], str],
group2: Union[List[str], str],
column: str = None,
method: str = None,
labels=None,
min_fc=None,
alpha=None,
Expand All @@ -65,9 +65,6 @@ def __init__(
self.gene_names: str = gene_names
self.preprocessing_info: Dict = preprocessing_info

self.group1 = group1
self.group2 = group2
self.column = column
self.method = method
self.labels = labels
self.min_fc = min_fc
Expand All @@ -80,25 +77,36 @@ def __init__(
self.perm = perm
self.color_list = color_list

if isinstance(group1, list) and isinstance(group2, list):
self.metadata, self.column = self._add_metadata_column(
metadata, group1, group2
)
self.group1, self.group2 = "group1", "group2"
else:
self.metadata, self.column = metadata, column
self.group1, self.group2 = group1, group2

self._check_input()

self._statistics = Statistics(
mat=self.mat,
metadata=self.metadata,
sample=self.sample,
index_column=self.index_column,
preprocessing_info=self.preprocessing_info,
)
self._check_input()

if plot:
self._perform_differential_expression_analysis()
self._annotate_result_df()
self._add_hover_data_columns()
self._plot()

# TODO this used to change the actual metadata .. is this intended?
def _add_metadata_column(self, group1_list: list, group2_list: list):
# TODO this used to change the actual metadata .. was this intended?
def _add_metadata_column(
self, metadata: pd.DataFrame, group1_list: list, group2_list: list
):
# create new column in metadata with defined groups
metadata = self.metadata

sample_names = metadata[self.sample].to_list()
misc_samples = list(set(group1_list + group2_list) - set(sample_names))
Expand All @@ -114,24 +122,17 @@ def _add_metadata_column(self, group1_list: list, group2_list: list):
]
choices = ["group1", "group2"]
metadata[column] = np.select(conditons, choices, default=np.nan)
self.metadata = metadata

return column, "group1", "group2"
return metadata, column

def _check_input(self):
"""
check input and add metadata column if samples are given
"""
if isinstance(self.group1, list) and isinstance(self.group2, list):
self.column, self.group1, self.group2 = self._add_metadata_column(
self.group1, self.group2
)

"""Check if self.column is set correctly."""
if self.column is None:
raise ValueError(
"Column containing group1 and group2 needs to be specified"
)

# TODO revisit this
def _update(self, updated_attributes):
"""
update attributes using dict
Expand Down

0 comments on commit 0e0ab54

Please sign in to comment.