squidpy icon indicating copy to clipboard operation
squidpy copied to clipboard

ligrec plot order

Open wangjiawen2013 opened this issue 3 years ago • 9 comments

when I use sq.pl.ligrec to plot the interaction, the axis wasn't ordered by leiden number, but by dict order. I changed the order of the leiden categories by:

adata.obs['leiden'] = pd.Categorical(adata.obs['leiden'],categories=(str(i) for i in np.arange(np.unique(adata.obs['leiden'])))

and plot again:

for leiden in adata.obs['leiden'].cat.categories:
    sq.pl.ligrec(adata,cluster_key="leiden",pvalue_threshold=0.05,source_groups=leiden,target_groups=None,means_range=(1.4, np.inf),alpha=1e-4,swap_axes=True,save=leiden)

This time the axis order is still disordered, so how to set the leiden order ?

wangjiawen2013 avatar May 29 '22 15:05 wangjiawen2013

which axis order do you mean @wangjiawen2013 ?

giovp avatar Jun 07 '22 15:06 giovp

The cluster number is 1,2,3,4,5,6,7,8,9,10,11, but it's displayed as 1, 10, 11, 2,3,4,5,6,7,8,9 on the plot.

wangjiawen2013 avatar Jun 08 '22 01:06 wangjiawen2013

what's the result of

adata.obs['leiden'].cat.categories

?

giovp avatar Jun 08 '22 07:06 giovp

In [5]: adata.obs['leiden'].cat.categories

Out[5]:

Index(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],dtype='object')

wangjiawen2013 avatar Jun 09 '22 05:06 wangjiawen2013

I think I call sorted() on the categories here which causes this.

michalk8 avatar Jun 09 '22 08:06 michalk8

Will you update the code, or we fix it ourselves ?

wangjiawen2013 avatar Jun 11 '22 03:06 wangjiawen2013

Will you update the code, or we fix it ourselves ?

Would be great if you could open a PR.

michalk8 avatar Jun 13 '22 17:06 michalk8

I opened a PR, but I am afraid that the issue isn't resolved, because the plot order didn't change with or without "sorted" on my data.

wangjiawen2013 avatar Jul 03 '22 08:07 wangjiawen2013

thanks @wangjiawen2013 I'll take a look soon, sorry for delay!

giovp avatar Jul 07 '22 19:07 giovp

hi @wangjiawen2013 ,

reposting here from the PR. I actually realized it's possible to just pass the order excpliticly, you can try out with this:

import squidpy as sq
adata = sq.datasets.seqfish()
import pandas as pd

adata.obs["leiden"] = pd.Categorical(
    np.random.randint(0, 15, size=adata.obs.Area.shape)
)
res = sq.gr.ligrec(
    adata,
    n_perms=1,
    cluster_key="leiden",
    copy=True,
    use_raw=False,
    transmitter_params={"categories": "ligand"},
    receiver_params={"categories": "receptor"},
)
sq.pl.ligrec(
    res,
    source_groups="1",
    target_groups=adata.obs.leiden.cat.categories.astype(str),
    alpha=0.005,
)

image

giovp avatar Aug 18 '22 19:08 giovp