scverse / squidpy

Spatial Single Cell Analysis in Python
https://squidpy.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
440 stars 79 forks source link

How can one plot only specific interactions? #464

Closed rgranit closed 2 years ago

rgranit commented 2 years ago

Does the sq.pl.ligrec function support plotting just some of the interactions?

Sometimes there are many results and I would like to make a figure with just selected interactions.

michalk8 commented 2 years ago

Hi @rgranit ,

currently, there isn't any convenient way of directly specifying a subset of interactions to plot (source_groups and target_groups are the closest, which control the source/target clusters, respectively). You could try something along the lines:

import squidpy as sq

adata = sq.datasets.visium_fluo_adata()

sq.gr.ligrec(adata, "leiden")

ligrec = adata.uns['leiden_ligrec']
mask = ligrec['means'].index[:10]
ligrec_to_plot = {k: df.loc[mask] for k, df in ligrec.items()}

sq.pl.ligrec(ligrec_to_plot)

since sq.pl.ligrec also accepts the output of sq.gr.ligrec (a dictionary of dataframes).

rgranit commented 2 years ago

Thanks @michalk8 it works well, any way to also lifter the source_groups and target_groups in the process? I could not really figure out how to accomplish this using the dict of DFs

michalk8 commented 2 years ago

Thanks @michalk8 it works well, any way to also lifter the source_groups and target_groups in the process? I could not really figure out how to accomplish this using the dict of DFs

You can run e.g. sq.pl.ligrec(ligrec_to_plot, source_groups=["0"], target_groups=["1"]) from the example above. Internally, it's done by running

df = ...  # pandas.DataFrame
source_groups = ["0"]
target_groups = ["1"]
subset = df.loc[:, (source_groups, target_groups)]
rgranit commented 2 years ago

Amazing, thanks again @michalk8 !