`IndexError` in `spg.find_meta_gene`
See original GitHub issueHello,
I’m following along the tutorial but using my own AnnData and histology image. When calling spg.find_meta_gene as in the tutorial, I get the following error:
Trying to set attribute `.obs` of view, copying.
WARNING: It seems you use rank_genes_groups on the raw count data. Please logarithmize your data before calling rank_genes_groups.
Add gene: ENSG00000197971
Minus gene: ENSG00000111341
Absolute mean change: 4.6624002
Number of non-target spots reduced to: 77
===========================================================================
Meta gene is: ENSG00000131095+ENSG00000197971-ENSG00000111341
===========================================================================
Trying to set attribute `.obs` of view, copying.
WARNING: It seems you use rank_genes_groups on the raw count data. Please logarithmize your data before calling rank_genes_groups.
Traceback (most recent call last):
File "<stdin>", line 8, in <module>
File "/jhpce/shared/jhpce/libd/spagcn/1.2.0/spagcn_venv/lib/python3.7/site-packages/SpaGCN/util.py", line 304, in find_meta_gene
add_g=tmp.uns['rank_genes_groups']["names"][0][1]
File "/jhpce/shared/jhpce/libd/spagcn/1.2.0/spagcn_venv/lib/python3.7/site-packages/numpy/core/records.py", line 292, in __getitem__
obj = nt.void.__getitem__(self, indx)
IndexError: invalid index (1)
To be more precise, I ran the following for my AnnData:
# Read in raw data and subset to current sample (my AnnData has several samples)
raw = sc.read_h5ad(input_adata_path)
raw.var_names_make_unique()
raw = raw[raw.obs.sample_id == sample_name, :]
# adata.obs["pred"] was computed earlier using code provided in your tutorial
raw.obs["pred"] = adata.obs["pred"].astype('category')
# Set names to match tutorial, given names present in my AnnData
raw.obs["x_array"]=raw.obs["array_row"]
raw.obs["y_array"]=raw.obs["array_col"]
raw.obs["x_pixel"]= raw.obs["pxl_col_in_fullres"]
raw.obs["y_pixel"]= raw.obs["pxl_row_in_fullres"]
#Convert sparse matrix to non-sparse
raw.X=(raw.X.A if issparse(raw.X) else raw.X)
raw.raw=raw
sc.pp.log1p(raw)
# In my case 8 spatial domains were found, and my AnnData contains gene IDs
# as Ensembl IDs rather than gene symbols. The start gene is still GFAP, and I've verified
# the gene is present in my AnnData
meta_name, meta_exp=spg.find_meta_gene(input_adata=raw,
pred=raw.obs["pred"].tolist(),
target_domain=7,
start_gene="ENSG00000131095",
mean_diff=0,
early_stop=True,
max_iter=3,
use_raw=False)
As the error message suggests, this line triggers the error. I manually ran code from spg.find_meta_gene line-by-line, discovering that the error occurred on i=1 here, and the value of tmp.uns['rank_genes_groups']["names"][0] is ('ENSG00000268674',), which of course has no [1] index, leading to the IndexError I see.
I apologize for not providing a small directly reproducible example, as I couldn’t produce the error with your example data, and I didn’t think it was practical to share my large AnnData file directly. Please let me know how I could provide more useful information.
Thanks in advance for any help.
Best,
-Nick
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)

Top Related StackOverflow Question
Yes., that makes sense. You can use the previous round results as the meta gene, that is, “ENSG00000131095+ENSG00000197971-ENSG00000111341”. I’ll modify the function to make it stop when the non-target set is empty.
Thanks!
Fixed, thank you!