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

saving masks to png #112

Open
sunshx-bioinfo opened this issue Aug 23, 2024 · 9 comments
Open

saving masks to png #112

sunshx-bioinfo opened this issue Aug 23, 2024 · 9 comments

Comments

@sunshx-bioinfo
Copy link

Hi,

I want to use cellpose cell segmentation results in QuPath, I have found a script to transfer cell masks from one image to another in here. To run this script, a mask file with PNG format , which comes from cellpose.io.save_masks), is needed.

So how do i get this mask figure with PNG format from Sopa output, or do you know any other method to simply transfer Sopa cell segmentation results into QuPath ?

@quentinblampey
Copy link
Collaborator

quentinblampey commented Aug 23, 2024

Hi @sunshx-bioinfo,

It's not straightforward, but it's possible to use spatialdata.rasterize to convert the geometries into labels (i.e., an image), and then save it to PNG using any python library such as PIL.

May I ask why you need to use QuPath? In Sopa, the results are output in the Xenium Explorer, which is the "easy to use software". And since Sopa builds on top of SpatialData, you can also use napari which may be more convenient for developers.

@sunshx-bioinfo
Copy link
Author

Hi, sorry for late reply.

I want to use object classification function in QuPah to train an object classifier for each protein, because it's difficult to give a cutoff for protein staining to decide whether a protein is truly expressing in a cell or not. There are only dozens of proteins in PhoneCycler data, so it's not suitable to cluster cells and then annotate these clusters (i.e., cell type annotation). Instead, using positive marker genes/proteins to annotate cells may be a good chioce.

I know you have made it possible that shows Sopa results in the Xenium Explorer, that's great! But i need a function to help me to automatically determine a threshold for idendifing positive protein staining in a cell.

@quentinblampey
Copy link
Collaborator

Alright, I understand, thanks for the extra details!

Is it something you'd like to see inside Sopa, or will you always prefer the QuPath way to do it?

@wangjiawen2013
Copy link

wangjiawen2013 commented Aug 26, 2024

It would be cool if you can build it into Sopa. We are using QuPath for legacy reasons.

@sunshx-bioinfo
Copy link
Author

I will be very happy to see such function is avaiable in Sopa! Acturally, I plan to use Sopa as my first-choice software to analysis my spatial data and have suffered a lot from there images and data tranfromation. I have to turn to QuPath if there is no this automatelly-determine-threshold function for protein staining in Sopa.

BTW, can we provide more properties of a cell, such as circularity, solidity and so on, besides area? Because we usually have less signatures(genes/proteins) in spatial data, adding extra cell properties can help to cluster cells and find more potential biological difference among real cell types.

Another thing, by now, i'm not familiar with data structure inside spatialData image, can you give me a small example to save geometries to PNG file :)?

@quentinblampey
Copy link
Collaborator

Okay thanks, in that case could you please give me more details (or a link to a documentation/manual) that explains this feature?

Regarding the cell properties, they are indeed not computed by default, but can be obtained via the shapes stored in your SpatialData object after running Sopa. Note that the shapes are Shapely objects, which means all properties you mentioned can be obtained using the Shapely function. If you are not familiar with Shapely, you could also list the properties you're interested in, and I can give you come code showing how to compute them.

To create the image from the geometries, I have made this example below:

import sopa.io
import spatialdata
from PIL import Image

# create a dummy SpatialData object
sdata = sopa.io.uniform()

# turn the geometries into labels (i.e., image with integers indicating the cell IDs)
labels = spatialdata.rasterize(
    sdata["cells"],
    axes=["y", "x"],
    min_coordinate=[0, 0],
    max_coordinate=[2048, 2048], # shape of the image (i.e., sdata["image"])
    target_coordinate_system="global",
    target_unit_to_pixels=1.0,
)
labels = labels.data.compute()[0] # shape: (2048, 2048)

# save the labels using PIL
pil_labels = Image.fromarray(labels.astype(np.uint8))
pil_labels.save("your_file.png", "PNG")

@sunshx-bioinfo
Copy link
Author

sunshx-bioinfo commented Aug 26, 2024

Thanks!

For cell properties, i want to know cell/nucleus's area, perimeter, circularity, solidity, min/max diameter and so on.

For object classification function in QuPah, it enables us to train a artificial neural network model ( a binary or multi-classfication classifier) based on protein staining intensity, backgroud and so on(I am not clear) for each protein/proteins and then use the model to classify cells.

In QuPath,

  1. first, you need to choose several regions of image and stitch them as one train image;
  2. Then you perform cell segmentation on the train image;
  3. QuPath provide an interactive GUI for user to choose protein-positive/negtive cells manually and also provide a Live update button to live results;
  4. If we satisfied with the results of classifier, then save the model;
  5. Come back the raw image, and apply the trained model on it.

I don't see detail method description about this function in QuPath, here is the document about cell classifier I mentioned above, but we use different parameters (below). Hoping this is helpful to you :).
1724662734215

@quentinblampey
Copy link
Collaborator

Thanks for the details, I'll keep that in mind and work on this when possible (probably not before late September or October).

Regarding export to QuPath, @stergioc just told me it's possible to import a GeoJSON file into QuPath. This will be much more convenient and easy for you than re-creating a labels image. For that, you can use this function from GeoPandas, for instance:

gdf = sdata["cellpose_boundaries"] # this is a GeoPandas Dataframe

gdf.to_file(path, driver="GeoJSON")  

Please let me know if it works 🙂

@sunshx-bioinfo
Copy link
Author

Hi, Both of two methods work! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants