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

Find gene symbols exist only in .raw attribute #962

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
46 changes: 31 additions & 15 deletions scanpy/plotting/_anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2985,22 +2985,38 @@ def _prepare_dataframe(
f'Given {groupby}, valid observations: {adata.obs_keys()}'
)

if gene_symbols is not None and gene_symbols in adata.var.columns:
# translate gene_symbols to var_names
# slow method but gives a meaningful error if no gene symbol is found:
translated_var_names = []
for symbol in var_names:
if symbol not in adata.var[gene_symbols].values:
logg.error(
f"Gene symbol {symbol!r} not found in given "
f"gene_symbols column: {gene_symbols!r}"
# translate gene_symbols to var_names
# slow method but gives a meaningful error if no gene symbol is found:
if use_raw:
if gene_symbols is not None and gene_symbols in adata.raw.var.columns:
translated_var_names = []
for symbol in var_names:
if symbol not in adata.raw.var[gene_symbols].values:
logg.error(
f"Gene symbol {symbol!r} not found in given "
f"gene_symbols column: {gene_symbols!r}"
)
return
translated_var_names.append(
adata.raw.var[adata.raw.var[gene_symbols] == symbol].index[0]
)
return
translated_var_names.append(
adata.var[adata.var[gene_symbols] == symbol].index[0]
)
symbols = var_names
var_names = translated_var_names
symbols = var_names
var_names = translated_var_names
else:
if gene_symbols is not None and gene_symbols in adata.var.columns:
translated_var_names = []
for symbol in var_names:
if symbol not in adata.var[gene_symbols].values:
logg.error(
f"Gene symbol {symbol!r} not found in given "
f"gene_symbols column: {gene_symbols!r}"
)
return
translated_var_names.append(
adata.var[adata.var[gene_symbols] == symbol].index[0]
)
symbols = var_names
var_names = translated_var_names
Comment on lines +2988 to +3019
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using sc.get.obs_df for this? I believe it already has the logic you're implementing.

if layer is not None:
if layer not in adata.layers.keys():
raise KeyError(
Expand Down