How to concatenate spatial AnnData objects

What’s the recommended way of merging multiple visium samples into a single AnnData object?

Let’s assume I have a dict with the individual samples:

adatas_vis = {
  "sample1":, library_id="sample1",
  "sample2":, library_id="sample2",

When using

adata_vis_all = anndata.concat(adatas_vis, index_unique="_", merge="same", uns_merge="same")

uns doesn’t get merged and the images are lost.

I am now using

def concat_spatial(adatas):
    spatial_dict = {}
    for ad in adatas:
    return spatial_dict

adata_vis_all.uns["spatial"] = concat_spatial(adatas_vis.values())

to re-add them, but I feel there must be a better way?

1 Like

Have you considered integration after merging? I know that this is an important step to reduce batch effects. scvi does this and might have included methods on merging too

super late on this but if you just set uns_merge='unique' should do the job to handle the concatenation of unique library ids, rest is standard anndata. HTH

Awesome @giovp, this works. The different merge strategies are still getting me confused :see_no_evil:


I am trying to merge multiple CosMX nanostring samples in a single AnnData object using this approach with uns_merge = “unique”.
adata_cosmx_all = anndata.concat(adata_cosmx, index_unique = "_", merge="same", uns_merge = "unique")

However, I get an error message ‘ValueError: Must pass 2-d input. shape=(4480, 4480, 3)’. Eliminating the uns_merge parameter results in the concatenated cell x gene matrix.

I am trying to use the z-stacked image containers, which is also failing.
Each nanostring sample has different number of FOVs, how should consider setting the ‘library_id’ parameter in this case. Ref - [Use z-stacks with ImageContainer — squidpy documentation]

I would highly appreciate any guidance on ways to merge multiple nanostring cosmx objects.