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

attempt to fix missing interactions 1 #62

Merged
merged 7 commits into from
Feb 17, 2024
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
1 change: 1 addition & 0 deletions ktplotspy/plot/plot_cpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
DEFAULT_SPEC_PAT,
DEFAULT_CELLSIGN_ALPHA,
DEFAULT_COLUMNS,
DEFAULT_CPDB_SEP,
)
from ktplotspy.utils.support import (
ensure_categorical,
Expand Down
31 changes: 14 additions & 17 deletions ktplotspy/plot/plot_cpdb_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from matplotlib.colors import ListedColormap
from typing import Optional, Union, Dict, List

from ktplotspy.utils.support import diverging_palette, sub_pattern
from ktplotspy.utils.settings import DEFAULT_V5_COL_START, DEFAULT_COL_START, DEFAULT_CLASS_COL, DEFAULT_SPEC_PAT
from ktplotspy.utils.support import diverging_palette
from ktplotspy.utils.settings import DEFAULT_V5_COL_START, DEFAULT_COL_START, DEFAULT_CLASS_COL, DEFAULT_CPDB_SEP


def plot_cpdb_heatmap(
Expand All @@ -27,7 +27,6 @@ def plot_cpdb_heatmap(
title: str = "",
return_tables: bool = False,
symmetrical: bool = True,
special_character_regex_pattern: Optional[str] = None,
**kwargs
) -> Union[sns.matrix.ClusterGrid, Dict]:
"""Plot cellphonedb results as total counts of interactions.
Expand Down Expand Up @@ -67,8 +66,6 @@ def plot_cpdb_heatmap(
Whether to return the dataframes storing the interaction network.
symmetrical : bool, optional
Whether to return the sum of interactions as symmetrical heatmap.
special_character_regex_pattern : Optional[str], optional
Regular expression pattern to handle special characters from celltype names.
**kwargs
Passed to seaborn.clustermap.

Expand All @@ -78,25 +75,25 @@ def plot_cpdb_heatmap(
Union[sns.matrix.ClusterGrid, Dict]
Either heatmap of cellphonedb interactions or dataframe containing the interaction network.
"""
if special_character_regex_pattern is None:
special_character_regex_pattern = DEFAULT_SPEC_PAT
all_intr = pvals.copy()
intr_pairs = all_intr.interacting_pair
col_start = (
DEFAULT_V5_COL_START if all_intr.columns[DEFAULT_CLASS_COL] == "classification" else DEFAULT_COL_START
) # in v5, there are 12 columns before the values
all_int = all_intr.iloc[:, col_start : all_intr.shape[1]].T
all_int.columns = intr_pairs
if cell_types is not None:
cell_types = [sub_pattern(cell_type=cell_type, pattern=special_character_regex_pattern) for cell_type in cell_types]
cell_types_comb = ["|".join(list(x)) for x in list(product(cell_types, cell_types))]
cell_types_keep = [ct for ct in all_int.index if ct in cell_types_comb]
empty_celltypes = list(set(cell_types_comb) ^ set(cell_types_keep))
all_int = all_int.loc[cell_types_keep]
if len(empty_celltypes) > 0:
tmp_ = np.zeros((len(empty_celltypes), all_int.shape[1]))
tmp_ = pd.DataFrame(tmp_, index=empty_celltypes, columns=all_int.columns)
all_int = pd.concat([all_int, tmp_], axis=0)
if cell_types is None:
cell_types = sorted(list(set([y for z in [x.split(DEFAULT_CPDB_SEP) for x in all_intr.columns[col_start:]] for y in z])))
cell_types_comb = ["|".join(list(x)) for x in list(product(cell_types, cell_types))]
cell_types_keep = [ct for ct in all_int.index if ct in cell_types_comb]
empty_celltypes = list(set(cell_types_comb) ^ set(cell_types_keep))
all_int = all_int.loc[cell_types_keep]
if len(empty_celltypes) > 0:
tmp_ = np.zeros((len(empty_celltypes), all_int.shape[1]))
if not degs_analysis:
tmp_ += 1
tmp_ = pd.DataFrame(tmp_, index=empty_celltypes, columns=all_int.columns)
all_int = pd.concat([all_int, tmp_], axis=0)
all_count = all_int.melt(ignore_index=False).reset_index()
if degs_analysis:
all_count["significant"] = all_count.value == 1
Expand Down
1 change: 1 addition & 0 deletions ktplotspy/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
DEFAULT_V5_COL_START = 13
DEFAULT_COL_START = 11
DEFAULT_CLASS_COL = 12
DEFAULT_CPDB_SEP = "|"
4 changes: 2 additions & 2 deletions ktplotspy/utils/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import Dict, List, Optional


from ktplotspy.utils.settings import DEFAULT_SEP
from ktplotspy.utils.settings import DEFAULT_SEP, DEFAULT_CPDB_SEP


def set_x_stroke(df: pd.DataFrame, isnull: bool, stroke: int):
Expand Down Expand Up @@ -234,7 +234,7 @@ def prep_table(data: pd.DataFrame) -> pd.DataFrame:
"""
dat = data.copy()
dat.index = [x + DEFAULT_SEP * 3 + y for x, y in zip(dat.id_cp_interaction, dat.interacting_pair)]
dat.columns = [re.sub("\\|", DEFAULT_SEP, col) for col in dat.columns]
dat.columns = [re.sub(f"\\{DEFAULT_CPDB_SEP}", DEFAULT_SEP, col) for col in dat.columns]
dat.index = [re.sub("_", "-", row) for row in dat.index]
dat.index = [re.sub("[.]", " ", row) for row in dat.index]

Expand Down
6 changes: 3 additions & 3 deletions tests/test_plot_cpdb_heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def test_plot_cpdb_heatmap_return(adata, pvals):

@patch("matplotlib.pyplot.show")
@pytest.mark.usefixtures("adata", "pvals")
def test_plot_cpdb_heatmap_celltypes(mock_show, adata, pvals):
def test_plot_cpdb_heatmap_celltypes(mock_show, adata, pvals_v5):
g = plot_cpdb_heatmap(
pvals=pvals,
cell_types=["CD4 T cell", "CD8 T cell", "B cell"],
pvals=pvals_v5,
cell_types=["iEVT", "PV MYH11", "PV STEAP4", "EVT_1"],
)
g
Loading