Skip to content

Commit

Permalink
palette support for da_neighborhoods (#343)
Browse files Browse the repository at this point in the history
* palette support for da_neighborhoods

Signed-off-by: zethson <[email protected]>

* Remove custom css

Signed-off-by: zethson <[email protected]>

---------

Signed-off-by: zethson <[email protected]>
  • Loading branch information
Zethson authored Aug 17, 2023
1 parent abbdd2c commit a7d8b15
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]
html_css_files = ["css/override.css", "css/sphinx_gallery.css"]
# html_css_files = ["css/override.css", "css/sphinx_gallery.css"]
html_show_sphinx = False


Expand Down
53 changes: 26 additions & 27 deletions pertpy/plot/_milopy.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations

from typing import Sequence

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scanpy as sc
import seaborn as sns
from anndata import AnnData
from mudata import MuData


Expand All @@ -23,7 +24,7 @@ def nhood_graph(
show: bool | None = None,
save: bool | str | None = None,
**kwargs,
):
) -> None:
"""Visualize DA results on abstracted graph (wrapper around sc.pl.embedding)
Args:
Expand Down Expand Up @@ -86,7 +87,7 @@ def nhood(
show: bool | None = None,
save: bool | str | None = None,
**kwargs,
):
) -> None:
"""Visualize cells in a neighbourhood.
Args:
Expand All @@ -109,7 +110,8 @@ def da_beeswarm(
feature_key: str | None = "rna",
anno_col: str = "nhood_annotation",
alpha: float = 0.1,
subset_nhoods: list = None,
subset_nhoods: list[str] = None,
palette: str | Sequence[str] | dict[str, str] | None = None,
):
"""Plot beeswarm plot of logFC against nhood labels
Expand All @@ -118,49 +120,47 @@ def da_beeswarm(
anno_col: Column in adata.uns['nhood_adata'].obs to use as annotation. (default: 'nhood_annotation'.)
alpha: Significance threshold. (default: 0.1)
subset_nhoods: List of nhoods to plot. If None, plot all nhoods. (default: None)
palette: Name of Seaborn color palette for violinplots.
Defaults to pre-defined category colors for violinplots.
"""
try:
nhood_adata = mdata["milo"].T.copy()
except KeyError:
print(
"mdata should be a MuData object with two slots: feature_key and 'milo' - please run milopy.count_nhoods(adata) first"
)
raise RuntimeError(
"mdata should be a MuData object with two slots: feature_key and 'milo'. Run 'milopy.count_nhoods(adata)' first."
) from None

if subset_nhoods is not None:
nhood_adata = nhood_adata[subset_nhoods]

try:
nhood_adata.obs[anno_col]
except KeyError:
print(
'Cannot find {a} in adata.uns["nhood_adata"].obs -- \
please run milopy.utils.annotate_nhoods(adata, anno_col) first'.format(
a=anno_col
)
)
raise RuntimeError(
f"Unable to find {anno_col} in mdata.uns['nhood_adata']. Run 'milopy.utils.annotate_nhoods(adata, anno_col)' first"
) from None

try:
nhood_adata.obs["logFC"]
except KeyError:
print(
'Cannot find `logFC` in adata.uns["nhood_adata"].obs -- \
please run milopy.core.da_nhoods(adata) first'
)
raise RuntimeError(
"Unable to find 'logFC' in mdata.uns['nhood_adata'].obs. Run 'core.da_nhoods(adata)' first."
) from None

sorted_annos = (
nhood_adata.obs[[anno_col, "logFC"]].groupby(anno_col).median().sort_values("logFC", ascending=True).index
)

anno_df = nhood_adata.obs[[anno_col, "logFC", "SpatialFDR"]].copy()
anno_df["is_signif"] = anno_df["SpatialFDR"] < alpha
# anno_df['across_organs'] = ["Significant across organs" if x else "" for x in (keep_nhoods & signif_nhoods)]
anno_df = anno_df[anno_df[anno_col] != "nan"]

try:
obs_col = nhood_adata.uns["annotation_obs"]
anno_palette = dict(
zip(mdata[feature_key].obs[obs_col].cat.categories, mdata[feature_key].uns[f"{obs_col}_colors"])
)
if palette is None:
palette = dict(
zip(mdata[feature_key].obs[obs_col].cat.categories, mdata[feature_key].uns[f"{obs_col}_colors"])
)
sns.violinplot(
data=anno_df,
y=anno_col,
Expand All @@ -169,7 +169,7 @@ def da_beeswarm(
size=190,
inner=None,
orient="h",
palette=anno_palette,
palette=palette,
linewidth=0,
scale="width",
)
Expand All @@ -196,7 +196,7 @@ def da_beeswarm(
orient="h",
alpha=0.5,
)
plt.legend(loc="upper left", title=f"< {int(alpha*100)}% SpatialFDR", bbox_to_anchor=(1, 1), frameon=False)
plt.legend(loc="upper left", title=f"< {int(alpha * 100)}% SpatialFDR", bbox_to_anchor=(1, 1), frameon=False)
plt.axvline(x=0, ymin=0, ymax=1, color="black", linestyle="--")

@staticmethod
Expand All @@ -205,7 +205,6 @@ def nhood_counts_by_cond(
test_var: str,
subset_nhoods: list = None,
log_counts: bool = False,
feature_key: str | None = "rna",
):
"""Plot boxplot of cell numbers vs condition of interest
Expand All @@ -218,9 +217,9 @@ def nhood_counts_by_cond(
try:
nhood_adata = mdata["milo"].T.copy()
except KeyError:
print(
"milo_mdata should be a MuData object with two slots: feature_key and 'milo' - please run milopy.count_nhoods(adata) first"
)
raise RuntimeError(
"mdata should be a MuData object with two slots: feature_key and 'milo'. Run milopy.count_nhoods(mdata) first"
) from None

if subset_nhoods is None:
subset_nhoods = nhood_adata.obs_names
Expand Down

0 comments on commit a7d8b15

Please sign in to comment.