Skip to content

Commit 480890e

Browse files
timtreisclaude
andcommitted
Warn on duplicate gene symbols, cache resolved var_name in auto-detect loop
Avoid calling _resolve_gene_symbols twice for the winning table by caching the result during the filtering loop. Add a warning when a gene symbol maps to multiple var_names. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 19a3fc9 commit 480890e

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

src/spatialdata_plot/pl/utils.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,12 @@ def _resolve_gene_symbols(
27762776
mask = adata.var[gene_symbols] == col_for_color
27772777
if not mask.any():
27782778
raise KeyError(f"'{col_for_color}' not found in `adata.var['{gene_symbols}']`.")
2779+
n_matches = mask.sum()
2780+
if n_matches > 1:
2781+
logger.warning(
2782+
f"Gene symbol '{col_for_color}' maps to {n_matches} var_names in column '{gene_symbols}'. "
2783+
f"Using the first match: '{adata.var.index[mask][0]}'."
2784+
)
27792785
return str(adata.var.index[mask][0])
27802786

27812787

@@ -2814,12 +2820,12 @@ def _validate_col_for_column_table(
28142820
"Please ensure the element is annotated by at least one table."
28152821
)
28162822
# Now check which tables contain the column
2823+
resolved_var_name: str | None = None
28172824
for annotates in tables.copy():
28182825
if col_for_color not in sdata[annotates].obs.columns and col_for_color not in sdata[annotates].var_names:
2819-
# Try gene_symbols fallback before discarding this table
28202826
if gene_symbols is not None:
28212827
try:
2822-
_resolve_gene_symbols(sdata[annotates], col_for_color, gene_symbols)
2828+
resolved_var_name = _resolve_gene_symbols(sdata[annotates], col_for_color, gene_symbols)
28232829
except KeyError:
28242830
tables.remove(annotates)
28252831
else:
@@ -2832,13 +2838,8 @@ def _validate_col_for_column_table(
28322838
table_name = next(iter(tables))
28332839
if len(tables) > 1:
28342840
logger.warning(f"Multiple tables contain column '{col_for_color}', using table '{table_name}'.")
2835-
# Resolve gene symbol to var_name using the selected table
2836-
if (
2837-
gene_symbols is not None
2838-
and col_for_color not in sdata[table_name].obs.columns
2839-
and col_for_color not in sdata[table_name].var_names
2840-
):
2841-
col_for_color = _resolve_gene_symbols(sdata[table_name], col_for_color, gene_symbols)
2841+
if resolved_var_name is not None:
2842+
col_for_color = resolved_var_name
28422843
return col_for_color, table_name
28432844

28442845

0 commit comments

Comments
 (0)