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

merge inh/act into feature #144

Merged
merged 5 commits into from
Oct 31, 2023
Merged
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
29 changes: 11 additions & 18 deletions dashboard/data/bmg_plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
from enum import Enum, auto


class Mode(Enum):
ACTIVATION = auto()
INHIBITION = auto()
ALL = auto()


PlateSummary = namedtuple(
"PlateSummary",
[
Expand Down Expand Up @@ -194,7 +188,7 @@ def parse_bmg_files(files: tuple[str, io.StringIO]) -> tuple[pd.DataFrame, np.nd
def calculate_activation_inhibition_zscore(
values: np.ndarray,
stats: dict,
mode: Mode = Mode.ALL,
mode: str,
without_pos: bool = False,
) -> tuple[np.ndarray]:
"""
Expand All @@ -207,8 +201,7 @@ def calculate_activation_inhibition_zscore(
:return: activation, inhibition and z-score values
"""
activation, inhibition = None, None
if mode == Mode.ACTIVATION or mode == Mode.ALL:
# NOTE: for now `without_pos` is not used
if mode == "activation":
if without_pos:
activation = (values - stats["mean_neg"]) / (stats["mean_neg"]) * 100
else:
Expand All @@ -218,25 +211,28 @@ def calculate_activation_inhibition_zscore(
* 100
)

if mode == Mode.INHIBITION or mode == Mode.ALL:
if mode == "inhibition":
inhibition = (
1 - ((values - stats["mean_pos"])) / (stats["mean_neg"] - stats["mean_pos"])
) * 100
(values - stats["mean_neg"]) / (stats["mean_pos"] - stats["mean_neg"]) * 100
)

z_score = (values - stats["mean_cmpd"]) / stats["std_cmpd"]

return activation, inhibition, z_score


def get_activation_inhibition_zscore_dict(
df_stats: pd.DataFrame, plate_values: np.ndarray, modes: dict[Mode]
df_stats: pd.DataFrame,
plate_values: np.ndarray,
mode: str,
without_pos: bool = False,
) -> dict[str, dict[str, float]]:
"""
Calculates activation and inhibition for each compound in the plates.

:param df_stats: dataframe with statistics for each plate
:param plate_values: array with values in the plate
:param mode: list of modes to calculate activation and inhibition
:param mode: mode to calculate activation and inhibition
:return: dictionary with activation and inhibition values for each compound in the plate
"""
stats = {}
Expand All @@ -251,11 +247,8 @@ def get_activation_inhibition_zscore_dict(

act_inh_dict = {}
for (_, row_stats), v in zip(df_stats.iterrows(), plate_values):
mode = (
modes[row_stats["barcode"]] if row_stats["barcode"] in modes else Mode.ALL
)
activation, inhibition, z_score = calculate_activation_inhibition_zscore(
v[0], stats, mode=mode
v[0], stats, mode, without_pos
)
act_inh_dict[row_stats["barcode"]] = {
"activation": activation,
Expand Down
33 changes: 14 additions & 19 deletions dashboard/data/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pandas as pd

from dashboard.data.bmg_plate import Mode, get_activation_inhibition_zscore_dict
from dashboard.data.bmg_plate import get_activation_inhibition_zscore_dict


def values_array_to_column(
Expand Down Expand Up @@ -93,23 +93,24 @@ def combine_bmg_echo_data(
echo_df: pd.DataFrame,
df_stats: pd.DataFrame,
plate_values: np.ndarray,
modes: dict[Mode] = None,
mode: str,
without_pos: bool = False,
) -> pd.DataFrame:
"""
Combine Echo data with activation and inhibition values.

:param echo_df: dataframe with Echo data
:param df_stats: dataframe containing statistics for each plate
:param plate_values: numpy array with activation and inhibition values - shape: (#plates, 2, 16, 24)
:param modes: dictionary with modes for each plate
:param mode: mode to calculate activation and inhibition
:return: dataframe with Echo data and activation and inhibition values
"""
PLATE = "Destination Plate Barcode"
WELL = "Destination Well"

if modes is None:
modes = dict()
act_inh_dict = get_activation_inhibition_zscore_dict(df_stats, plate_values, modes)
act_inh_dict = get_activation_inhibition_zscore_dict(
df_stats, plate_values, mode, without_pos
)
dfs = []
for barcode, values_dict in act_inh_dict.items():
activation_inhibition_df = get_activation_inhibition_zscore_df(
Expand Down Expand Up @@ -150,7 +151,7 @@ def split_compounds_controls(df: pd.DataFrame) -> tuple[pd.DataFrame]:


def aggregate_well_plate_stats(
df: pd.DataFrame, assign_x_coords: bool = False
df: pd.DataFrame, key: str, assign_x_coords: bool = False
) -> tuple[pd.DataFrame]:
"""
Aggregates the statistics (mean and std) per plate needed for the plots.
Expand All @@ -163,33 +164,27 @@ def aggregate_well_plate_stats(
"""

PLATE = "Destination Plate Barcode"
ACTIVATION = "% ACTIVATION"
INHIBITION = "% INHIBITION"
Z_SCORE = "Z-SCORE"

stats_df = (
df.groupby(PLATE)[[ACTIVATION, INHIBITION, Z_SCORE]]
df.groupby(PLATE)[[key, Z_SCORE]]
.agg(["mean", "std", "min", "max"])
.reset_index()
)
stats_df.columns = [
PLATE,
f"{ACTIVATION}_mean",
f"{ACTIVATION}_std",
f"{ACTIVATION}_min",
f"{ACTIVATION}_max",
f"{INHIBITION}_mean",
f"{INHIBITION}_std",
f"{INHIBITION}_min",
f"{INHIBITION}_max",
f"{key}_mean",
f"{key}_std",
f"{key}_min",
f"{key}_max",
f"{Z_SCORE}_mean",
f"{Z_SCORE}_std",
f"{Z_SCORE}_min",
f"{Z_SCORE}_max",
]

if assign_x_coords:
for col in [ACTIVATION, INHIBITION, Z_SCORE]:
for col in [key, Z_SCORE]:
stats_df = stats_df.sort_values(by=f"{col}_mean") # sort by mean
stats_df[f"{col}_x"] = range(len(stats_df)) # get the x coordinates

Expand Down
1 change: 1 addition & 0 deletions dashboard/pages/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
dcc.Store(id="z-slider-value", storage_type="local"),
dcc.Store(id="report-data-correlation-plots", storage_type="local"),
dcc.Store(id="report-data-hit-validation-input", storage_type="local"),
dcc.Store(id="activation-inhibition-screening-options", storage_type="local"),
],
)

Expand Down
Loading