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

CentroidSpace AnnData Annotations #455

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Changes from 4 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
15 changes: 15 additions & 0 deletions pertpy/tools/_perturbation_space/_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def compute(
target_col: str = "perturbations",
layer_key: str = None,
embedding_key: str = "X_umap",
keep_obs: bool = True,
) -> AnnData: # type: ignore
"""Computes the centroids of a pre-computed embedding such as UMAP.

Expand All @@ -26,6 +27,8 @@ def compute(
target_col: .obs column that stores the label of the perturbation applied to each cell.
layer_key: If specified pseudobulk computation is done by using the specified layer. Otherwise, computation is done with .X
embedding_key: `obsm` key of the AnnData embedding to use for computation. Defaults to the 'X' matrix otherwise.
keep_obs: .obs columns in the input AnnData to keep in the output pseudobulk AnnData. Only .obs columns with the same value for
Lilly-May marked this conversation as resolved.
Show resolved Hide resolved
each cell of one perturbation can be kept. Defaults to None.

Examples:
Compute the centroids of a UMAP embedding of the papalexi_2021 dataset:
Expand Down Expand Up @@ -84,6 +87,18 @@ def compute(

ps_adata = AnnData(X=X)
ps_adata.obs_names = index
ps_adata.obs[target_col] = index

if embedding_key is not None:
ps_adata.obsm[embedding_key] = X

if keep_obs: # Save the values of the obs columns of interest in the ps_adata object
obs_df = adata.obs
obs_df = obs_df.groupby(target_col).agg(lambda x: np.nan if len(set(x)) != 1 else list(set(x))[0])
for obs_name in obs_df.columns:
if not obs_df[obs_name].isnull().values.any():
mapping = {pert: obs_df.loc[pert][obs_name] for pert in index}
ps_adata.obs[obs_name] = ps_adata.obs[target_col].map(mapping)

return ps_adata

Expand Down
Loading