gustaveroussy / sopa

Technology-invariant pipeline for spatial omics analysis (Xenium / MERSCOPE / CosMx / PhenoCycler / MACSima / Hyperion) that scales to millions of cells
https://gustaveroussy.github.io/sopa/
BSD 3-Clause "New" or "Revised" License
114 stars 11 forks source link

[Bug]"Could not find element with name ['cell_circles']" #117

Open Yifan-debug opened 1 week ago

Yifan-debug commented 1 week ago

Dear Developer,

I'm trying to use the sopa to output the xenium file, so I can open it with xenium. but I got the error said no cell circles, I actually have this one in my spatialdata (pelase check the image) Please let me know how to solve it.

image


KeyError Traceback (most recent call last) Cell In[8], line 4 2 query_sdata 3 explorer_path = "./Crop_sdata" ----> 4 sopa.io.write(explorer_path, query_sdata)

File d:\Yifan_Wang\Software\envs\sopa\lib\site-packages\sopa\io\explorer\converter.py:113, in write(path, sdata, image_key, shapes_key, points_key, gene_column, pixel_size, layer, polygon_max_vertices, lazy, ram_threshold_gb, mode, save_h5ad) 110 adata = sdata.tables[SopaKeys.TABLE] 112 shapes_key = adata.uns["spatialdata_attrs"]["region"] --> 113 geo_df = sdata[shapes_key] 115 if _should_save(mode, "c"): 116 write_gene_counts(path, adata, layer=layer)

File d:\Yifan_Wang\Software\envs\sopa\lib\site-packages\spatialdata_core\spatialdata.py:2124, in SpatialData.getitem(self, item) 2111 def getitem(self, item: str) -> SpatialElement: 2112 """ 2113 Return the element with the given name. 2114 (...) 2122 The element. 2123 """ -> 2124 , , element = self._find_element(item) 2125 return element

File d:\Yifan_Wang\Software\envs\sopa\lib\site-packages\spatialdata_core\spatialdata.py:2031, in SpatialData._find_element(self, element_name) 2028 found.append((element_type, elementname, element)) 2030 if len(found) == 0: -> 2031 raise KeyError(f"Could not find element with name {element_name!r}") 2033 if len(found) > 1: 2034 raise ValueError(f"Found multiple elements with name {element_name!r}")

KeyError: "Could not find element with name ['cell_circles']"

quentinblampey commented 1 week ago

Hello @Yifan-debug, can you show me which line of code you used to get this error? My guess is that you provided shapes_key=["cell_circles"] instead of shapes_key="cell_circles". Indeed, the key should be a str corresponding to the name of the spatial element, and we don't handle lists of strings (yet).

Yifan-debug commented 1 week ago

import sopa

Hi @quentinblampey

Here is the line which got this error.
explorer_path = "./Crop_sdata" sopa.io.write(explorer_path, query_sdata) Actually, I tried to read the xenium data with spatial_io.xenium and then write back to xenium with sopa.io.write and got the same error.

quentinblampey commented 1 week ago

I think I understand the error now, but this is due to the fact that your data is not formated as an output of Sopa. Indeed, the sopa.io.write function expects that the SpatialData object has been segmented and aggregated via Sopa, which is not the case here.

Can you try this, though? sopa.io.write(explorer_path, query_sdata, shapes_key="cell_circles")

Yifan-debug commented 1 week ago

image Hi it's the same error code

quentinblampey commented 1 week ago

Okay, thanks for giving a try. I'll try to fix sopa.io.write to make it more general, i.e. handle non-sopa outputs.

What is your actual use case? I have a few questions, it will help me better understand what you want to do:

Yifan-debug commented 1 week ago

Hi @quentinblampey Thank you so much for helping. Please let me know when the package is updated For the questions: 1 I'm not satisfied with the current Xenium segmentation and currently looking for some other segmentation methods. 2 I want to create the visualization files for just one crop, and if it can be 3D visualization would be perfect for me. 3 Yes. I am planning to use spatial data objects for my following analyses.

quentinblampey commented 1 week ago

Thanks for sharing these details! In that case, since you want to use Sopa for segmentation, you should do the following:

  1. Read only the morphology_focus and transcripts elements in your SpatialData object (you don't need the others). The boundaries and the table will be created when using Sopa. Note that, by default, sopa.io.xenium only loads the latter elements.
  2. Then, run Sopa according to the tutorials, using either the API or CLI or Snakemake pipeline.
  3. Then, you'll have boundaries (Cellpose or Baysor) and a new table. These elements have the right format and you'll be able to use sopa.io.write to visualize it.

Hope that helps, let me know! Still, I keep the issue open, as I'll try to fix it to make the writing more general to non-Sopa formats :)