scverse / squidpy

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

Comparisons between groups of samples #784

Open grst opened 9 months ago

grst commented 9 months ago

Most functions in squidpy seem to be centered around analyzing a single sample. For me, in the context of clinical trials, it would be important to compare between sample groups. The usual variables of interest are

A few things that came to my mind

Would be great to have dedicated functions for this, and happy about further ideas!

CC @Zethson, because this is basically "spatial pertpy"


Example data

Collection of use-cases and implementation ideas

Differential neighborhood enrichment

Sharing here a very simple approach that estimates for each "niche" (e.g. tumor spots), the average neighborhood (i.e. across all spots of that niche, what's the fraction of neighboring niches). This allows to distinguish, for instance, between immune-infiltrated and immune-excluded tumors.

# adata_vis -> AnnData with visium data, contains multiple samples
# "niche_leiden" -> categorical annotation of niches
res2 = []
for sample in samples:
    tmp_ad = select_slide(adata_vis, sample)
    sq.gr.spatial_neighbors(tmp_ad)

    res = []
    for spot in range(tmp_ad.shape[0]):
        spot_neighbors = tmp_ad.obsp["spatial_distances"][spot, :].todense().A1.astype(bool)
        current_niche = tmp_ad.obs["niche_leiden"][spot]
        res.append(tmp_ad.obs["niche_leiden"][spot_neighbors].value_counts().to_frame().T.assign(current_niche = current_niche))

    res_df = pd.concat(res).groupby("current_niche").agg("mean").assign(sample = sample)
    res2.append(res_df)

neighbors_per_sample = pd.concat(res2).reset_index(drop=False).set_index(["current_niche", "sample"]).pipe(lambda x: x.div(np.sum(x, axis=1), axis=0))

This results in something like this (each column a sample): image

Statistics between groups of samples can be computed using a standard linear model.

Visualization idea: image (but instead color heatmap by enriched/depleted)

Zethson commented 9 months ago

I'd like to CC @AnnaChristina here who's also been working on this AFAIK.

grst commented 9 months ago

@timtreis, here is one for "spatial perturbation analysis" already

Zethson commented 9 months ago

@grst we're discussing this now and might get back with an update here

giovp commented 9 months ago

@Zethson we discussed this before going on holiday, we have a zulip channel for that, I'll add you

grst commented 5 months ago

cellcharter already has a function for differential neighborhood enrichment that does statistics on the sample level: https://github.com/CSOgroup/cellcharter/blob/396c415f706e46ce5d9b82df062d0b6509aa526e/src/cellcharter/gr/_nhood.py#L136