Skip to content

Commit

Permalink
celltype signatures of anndata layers
Browse files Browse the repository at this point in the history
  • Loading branch information
Müller-Bötticher committed Oct 30, 2024
1 parent 1dec225 commit b7abeac
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sainsc/utils/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ def celltype_signatures(
adata: ad.AnnData,
*,
celltype_col: str = "leiden",
layer: str | None = None,
agg_method: str | Callable = "mean",
) -> pd.DataFrame:
"""
Calculate gene expression signatures per 'celltype'.
Note, that this will make a dense copy of `adata.X` therefore potentially leading
to large memory usage.
Note, that this will make a dense copy of `adata.X` or the selected `layer`,
therefore potentially leading to large memory usage.
Parameters
----------
adata : anndata.AnnData
celltype_col : str, optional
Name of column in :py:attr:`anndata.AnnData.obs` containing cell-type
information.
layer : str, optional
Which layer to use for aggregation. If `None`, `adata.X` is used.
agg_method : str or collections.abc.Callable, optional
Function to aggregate gene expression per cluster used by
:py:meth:`pandas.DataFrame.agg`.
Expand All @@ -32,7 +35,7 @@ def celltype_signatures(
:py:class:`pandas.DataFrame` of gene expression aggregated per 'celltype'.
"""
signatures = (
adata.to_df()
adata.to_df(layer=layer)
.merge(adata.obs[celltype_col], left_index=True, right_index=True)
.groupby(celltype_col, observed=True, sort=False)
.agg(agg_method)
Expand Down

0 comments on commit b7abeac

Please sign in to comment.