broadinstitute / Tangram

Spatial alignment of single cell transcriptomic data.
BSD 3-Clause "New" or "Revised" License
249 stars 50 forks source link

Using Tangram with MERFISH and visualizing #82

Open wdg118 opened 1 year ago

wdg118 commented 1 year ago

Hi,

I'm working on using Tangram with my snRNA-seq data with MERFISH. How do I visualize the cell types in space ? I've tried using tg.plot_cell_annotation_sc but that does not work for MERFISH. I've also attempted to visualize gene patterns using tg.plot_genes_sc but that also does not work with MERFISH. In the paper, specifically Fig.2 there is visualization but this is not addressed in the tutorial or documentation.

Thanks,

Will

gaddamshreya commented 1 year ago

Hi @wdg118, thank you for your interest in Tangram! Can you please elaborate on what you mean by tg.plot_cell_annotation_sc and tg.plot_genes_sc not working? If there's an error, can you provide an error message so I can figure out the best way to help you with this. I'm curious to see the output of the mentioned functions with your data. Also, which sub-figure in Fig.2. are referring to? Please let me know, I'd love to help you solve the issue!

-Shreya

wdg118 commented 1 year ago

Hi @gaddamshreya,

Thank you for your reply.

Here is a screenshot of the error for tg.plot_cell_annotation_sc

image

It seems this function only works for Visium data.

In relation to Fig.2, I was hoping to visualize my MERFISH data just like in a,b,c,d and e. But more specifically I wanted to recreate the output of the tutorial (https://github.com/broadinstitute/Tangram/blob/master/tutorial_tangram_with_squidpy.ipynb) where we see tg.project_cell_annotations(ad_map, adata_st, annotation="cell_subclass") annotation_list = list(pd.unique(adata_sc.obs['cell_subclass'])) tg.plot_cell_annotation_sc(adata_st, annotation_list,perc=0.02) and tg.plot_genes_sc(genes, adata_measured=adata_st, adata_predicted=ad_ge, perc=0.02) to visualize cell types and genes respectively after Tangram has been run.

Thanks,

Will

iS4i4S commented 1 year ago

Hi @wdg118 ,

I am not related to the creation of this very useful program, however I am currently analyzing my MERFISH data and found the same. A work-around I used is:

sq.pl.spatial_scatter(adata, shape=None, color=annotation_list, wspace=0.4, )

If you want to use the 'perc' option you can do:

adata_sp=adata adata_sp.obs.drop(annotation_list, inplace=True, errors="ignore", axis=1) df = adata_sp.obsm["tangram_ct_pred"][annotation_list] tg.construct_obs_plot(df, adata_sp, perc=0.05) sq.pl.spatial_scatter(adata_sp, ax=axs[1], shape=None, color=["Plasma",], wspace=0.4, )

Hope it helps

Best

gaddamshreya commented 1 year ago

Hi @iS4i4S , thank you for the work-around! Hi @wdg118 , firstly apologies for the delay in my reply. I'm currently working with Tangram version 1.0.3 and scanpy version 1.9.1 and I wasn't able to reproduce the error with other non visium data (MERFISH and SlideSeq). It would also be helpful to see the 'uns' dictionary of you spatial data, like the output of this : adata_st.uns.get("spatial", {}).keys() Alternatively, you can use the following plotting functions:

  1. To visualize mapped cell types in space: tg.plot_cell_annotation(ad_map,adata_st,annotation='cell_subclass',nrows=5, ncols=5,robust=True, perc=0.02)
  2. To visualize genes in space: tg.plot_genes(genes, adata_measured=adata_st, adata_predicted=ad_ge, robust=True, perc=0.02)

Thank you for your patience! Let me know if you need more help.

wdg118 commented 1 year ago

Hi Shreya,

Thanks for this. I'm currently in my classes this week and I'm reinstalling all of my conda environments and I will run it again and send you the output of adata_st.uns.get("spatial", {}).keys() when I run it again soon, hopefully sometime next week. Really appreciate your help on this, we are really keen and excited on using Tangram for our purposes and any help you can provide and have already provided is greatly appreciated!

Kind regards,

Will

On Wed, Feb 8, 2023 at 9:31 PM Shreya Gaddam @.***> wrote:

Hi @iS4i4S https://github.com/iS4i4S , thank you for the work-around! Hi @wdg118 https://github.com/wdg118 , firstly apologies for the delay in my reply. I'm currently working with Tangram version 1.0.3 and scanpy version 1.9.1 and I wasn't able to reproduce the error with other non visium data (MERFISH and SlideSeq). It would also be helpful to see the 'uns' dictionary of you spatial data, like the output of this : adata_st.uns.get("spatial", {}).keys() Alternatively, you can use the following plotting functions:

  1. To visualize mapped cell types in space: tg.plot_cell_annotation(ad_map,adata_st,annotation='subclass_label',nrows=5, ncols=5,robust=True, perc=0.02)
  2. To visualize genes in space: tg.plot_genes(genes, adata_measured=adata_st, adata_predicted=ad_ge, robust=True, perc=0.02)

Thank you for your patience! Let me know if you need more help.

— Reply to this email directly, view it on GitHub https://github.com/broadinstitute/Tangram/issues/82#issuecomment-1423202548, or unsubscribe https://github.com/notifications/unsubscribe-auth/APDCNESLTD524TVRVPNJYNTWWP7DHANCNFSM6AAAAAAT2P5N5U . You are receiving this because you were mentioned.Message ID: @.***>

wdg118 commented 1 year ago

Hi Isaïas,

Thanks for this, I will try it. Shreya has just replied and I need to show her the output of the .uns of my spatial data, but nonetheless really appreciate your help with the work around. Perhaps you can show Shreya the .uns of your spatial data as well since we've had similar issues.

Kind regards,

Will

On Wed, Feb 8, 2023 at 10:24 AM Isaïas @.***> wrote:

Hi @wdg118 https://github.com/wdg118 ,

I am not related to the creation of this very useful program, however I am currently analyzing my MERFISH data and found the same. A work-around I used is:

sq.pl.spatial_scatter(adata, shape=None, color=annotation_list, wspace=0.4, )

If you want to use the 'perc' option you can do:

adata_sp=adata adata_sp.obs.drop(annotation_list, inplace=True, errors="ignore", axis=1) df = adata_sp.obsm["tangram_ct_pred"][annotation_list] tg.construct_obs_plot(df, adata_sp, perc=0.05) sq.pl.spatial_scatter(adata_sp, ax=axs[1], shape=None, color=["Plasma",], wspace=0.4, )

Hope it helps

Best

— Reply to this email directly, view it on GitHub https://github.com/broadinstitute/Tangram/issues/82#issuecomment-1422286500, or unsubscribe https://github.com/notifications/unsubscribe-auth/APDCNEVI5O66XMPQWCTXPV3WWNQ3RANCNFSM6AAAAAAT2P5N5U . You are receiving this because you were mentioned.Message ID: @.***>

wdg118 commented 1 year ago

Hi again Shreya,

I just looked at the documentation since I remembered that tg.plot_cell_annotation(ad_map,adata_st,annotation='subclass_label',nrows=5, ncols=5,robust=True, perc=0.02) and tg.plot_genes(genes, adata_measured=adata_st, that the arguments for the function do not seem to match what's described in the documentation ( https://tangram-sc.readthedocs.io/en/latest/classes/tangram.plot_utils.plot_cell_annotation_sc.html#tangram.plot_utils.plot_cell_annotation_sc ).

Kind regards,

Will

On Wed, Feb 8, 2023 at 9:31 PM Shreya Gaddam @.***> wrote:

Hi @iS4i4S https://github.com/iS4i4S , thank you for the work-around! Hi @wdg118 https://github.com/wdg118 , firstly apologies for the delay in my reply. I'm currently working with Tangram version 1.0.3 and scanpy version 1.9.1 and I wasn't able to reproduce the error with other non visium data (MERFISH and SlideSeq). It would also be helpful to see the 'uns' dictionary of you spatial data, like the output of this : adata_st.uns.get("spatial", {}).keys() Alternatively, you can use the following plotting functions:

  1. To visualize mapped cell types in space: tg.plot_cell_annotation(ad_map,adata_st,annotation='subclass_label',nrows=5, ncols=5,robust=True, perc=0.02)
  2. To visualize genes in space: tg.plot_genes(genes, adata_measured=adata_st, adata_predicted=ad_ge, robust=True, perc=0.02)

Thank you for your patience! Let me know if you need more help.

— Reply to this email directly, view it on GitHub https://github.com/broadinstitute/Tangram/issues/82#issuecomment-1423202548, or unsubscribe https://github.com/notifications/unsubscribe-auth/APDCNESLTD524TVRVPNJYNTWWP7DHANCNFSM6AAAAAAT2P5N5U . You are receiving this because you were mentioned.Message ID: @.***>

gaddamshreya commented 1 year ago

Hi Will! Yes, the functions in the documentation are newer versions(adapted to work with scanpy plotting functions) of the functions i've shared with you(as an alternative) since the functions in the documentation are not working in your case. The names are confusing:

wdg118 commented 1 year ago

Hi Shreya,

Oh right. I remember trying these previously and they also did not work. I will try them again once I fix my conda environment issues and get back to you.

Kind regards,

Will

On Wed, Feb 8, 2023 at 10:47 PM Shreya Gaddam @.***> wrote:

Hi Will! Yes, the functions in the documentation are newer versions(adapted to work with scanpy plotting functions) of the functions i've shared with you(as an alternative) since the functions in the documentation are not working in your case. The names are confusing:

  • The functions I shared with you as an alternative : tg.plot_cell_annotation and tg.plot_genes
  • Functions in the documentation which also don't seem to work in your case: _tg.plot_cell_annotationsc and _tg.plot_genessc

— Reply to this email directly, view it on GitHub https://github.com/broadinstitute/Tangram/issues/82#issuecomment-1423282131, or unsubscribe https://github.com/notifications/unsubscribe-auth/APDCNEVNO3BDX3HJZQEU5QTWWQIATANCNFSM6AAAAAAT2P5N5U . You are receiving this because you were mentioned.Message ID: @.***>

gaddamshreya commented 1 year ago

Sure, let me know. I'm happy to help. I would like to see the error message in that case as well.

lewlin commented 1 year ago

Thanks for taking care of this Shreya!

wdg118 commented 1 year ago

Hi Shreya,

Here is the output of adata_st.uns.get("spatial", {}).keys():

[image: image.png]

Here is the error message I get when I run tg.plot_cellannotation(ad map,adata_st,annotation='predicted.subclass_Allen',nrows=5, ncols=5,robust=True, perc=0.02)

INFO:root:spatial prediction dataframe is saved in obsm tangram_ct_pred of the spatial AnnData.

---------------------------------------------------------------------------KeyError Traceback (most recent call last) File ~/.conda/envs/tangram_test_env/lib/python3.10/site-packages/pandas/core/indexes/base.py:3802, in Index.get_loc(self, key, method, tolerance) 3801 try:-> 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err:

File ~/.conda/envs/tangram_test_env/lib/python3.10/site-packages/pandas/_libs/index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()

File ~/.conda/envs/tangram_test_env/lib/python3.10/site-packages/pandas/_libs/index.pyx:165, in pandas._libs.index.IndexEngine.get_loc()

File pandas/_libs/hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item()

File pandas/_libs/hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'x'

The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[55], line 1----> 1 tg.plot_cell_annotation(ad_map,adata_st,annotation='predicted.subclass_Allen',nrows=5, ncols=5,robust=True, perc=0.02)

File ~/.conda/envs/tangram_test_env/lib/python3.10/site-packages/tangram/plot_utils.py:300, in plot_cell_annotation(adata_map, adata_sp, annotation, x, y, nrows, ncols, s, cmap, subtitle_add, robust, perc, invert_y) 297 iterator = zip(df_annotation.columns, range(nrows * ncols)) 298 for ann, index in iterator: 299 xs, ys, preds = ordered_predictions(--> 300 adata_map.var[x], adata_map.var[y], df_annotation[ann] 301 ) 303 if robust: 304 vmin, vmax = q_value(preds, perc=perc)

File ~/.conda/envs/tangram_test_env/lib/python3.10/site-packages/pandas/core/frame.py:3807, in DataFrame.getitem(self, key) 3805 if self.columns.nlevels > 1: 3806 return self._getitem_multilevel(key)-> 3807 indexer = self.columns.get_loc(key) 3808 if is_integer(indexer): 3809 indexer = [indexer]

File ~/.conda/envs/tangram_test_env/lib/python3.10/site-packages/pandas/core/indexes/base.py:3804, in Index.get_loc(self, key, method, tolerance) 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err:-> 3804 raise KeyError(key) from err 3805 except TypeError: 3806 # If we have a listlike key, _check_indexing_error will raise 3807

InvalidIndexError. Otherwise we fall through and re-raise 3808

the TypeError. 3809 self._check_indexing_error(key)

KeyError: 'x'

I have also tried 'cellType' and got the same error.

Kind regards,

Will

[image: image.png]

On Wed, Feb 8, 2023 at 11:15 PM Shreya Gaddam @.***> wrote:

Sure, let me know. I'm happy to help. I would like to see the error message in that case as well.

— Reply to this email directly, view it on GitHub https://github.com/broadinstitute/Tangram/issues/82#issuecomment-1423310849, or unsubscribe https://github.com/notifications/unsubscribe-auth/APDCNEQ5UJUM3EJAYKHBOZDWWQLGZANCNFSM6AAAAAAT2P5N5U . You are receiving this because you were mentioned.Message ID: @.***>

gaddamshreya commented 1 year ago

Hi @wdg118,

wdg118 commented 1 year ago

Hi @gaddamshreya,

Sorry, I replied via email and it didn't show here.

Here is the output from adata_st.uns.get("spatial", {}).keys()

image

My Anndata has 'min_x', 'max_x', 'min_y', and 'max_y', so I wasn't sure how I would input it into the tg.plot_cell_annotation and tg.plot_genes functions (I get the same error with this function)

image

The anndata was made with the sq.read.vizgen function in squidpy ?

Should my anndata look different ?