BayraktarLab / cell2location

Comprehensive mapping of tissue cell architecture via integrated single cell and spatial transcriptomics (cell2location model)
https://cell2location.readthedocs.io/en/latest/
Apache License 2.0
315 stars 57 forks source link

InvalidIndexError: Reindexing only valid with uniquely valued Index objects #359

Open xiang11bei opened 6 months ago

xiang11bei commented 6 months ago

Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Minimal code sample (that we can run without your data, using public data)

When a scRNA-seq integrate with spatial transcriptome using common HVGs, Anadata containing only intersecting genes cannot be retrieved by idling data my ST data is accquired from the public dataset (https://www.10xgenomics.com/datasets/visium-hd-cytassist-gene-expression-libraries-of-human-crc)

# Your code here
# I made the gene names unique here
adata = sc.read_visium("/data1/zhangshuomin/CRC_spatial_ref/Raw_data/square_008um")
adata.var_names_make_unique()
adata

intersect = np.intersect1d(adata_vis.var_names, inf_aver.index)
intersect

adata_vis = adata_vis[:, intersect].copy()
InvalidIndexError                         Traceback (most recent call last)
Cell In[20], line 2
      1 # 
----> 2 adata_vis = adata_vis[:, intersect].copy()

File ~/miniconda3/lib/python3.12/site-packages/anndata/_core/anndata.py:1085, in AnnData.__getitem__(self, index)
   1083 def __getitem__(self, index: Index) -> AnnData:
   1084     """Returns a sliced view of the object."""
-> 1085     oidx, vidx = self._normalize_indices(index)
   1086     return AnnData(self, oidx=oidx, vidx=vidx, asview=True)

File ~/miniconda3/lib/python3.12/site-packages/anndata/_core/anndata.py:1066, in AnnData._normalize_indices(self, index)
   1065 def _normalize_indices(self, index: Index | None) -> tuple[slice, slice]:
-> 1066     return _normalize_indices(index, self.obs_names, self.var_names)

File ~/miniconda3/lib/python3.12/site-packages/anndata/_core/index.py:36, in _normalize_indices(index, names0, names1)
     34 ax0, ax1 = unpack_index(index)
     35 ax0 = _normalize_index(ax0, names0)
---> 36 ax1 = _normalize_index(ax1, names1)
     37 return ax0, ax1

File ~/miniconda3/lib/python3.12/site-packages/anndata/_core/index.py:97, in _normalize_index(indexer, index)
     95     return indexer
     96 else:  # indexer should be string array
---> 97     positions = index.get_indexer(indexer)
     98     if np.any(positions < 0):
     99         not_found = indexer[positions < 0]

File ~/miniconda3/lib/python3.12/site-packages/pandas/core/indexes/base.py:3885, in Index.get_indexer(self, target, method, limit, tolerance)
   3882 self._check_indexing_method(method, limit, tolerance)
   3884 if not self._index_as_unique:
-> 3885     raise InvalidIndexError(self._requires_unique_msg)
   3887 if len(target) == 0:
   3888     return np.array([], dtype=np.intp)

InvalidIndexError: Reindexing only valid with uniquely valued Index objects
vitkl commented 6 months ago

This is an issue with the incorrect use of Scanpy and pandas. You need to make sure that variable and observation names are unique and match correctly between objects (eg adata_vis and inf_aver). This could also be an issue with package version mismatch - but this is not specific to cell2location in any way.