Run scanpy.pp.neighbors and UMAP on a different layer other than X?

Hi there! I apologize if my question is naive. I am new to not only Python, but also to anndata and spatial analysis in general.

I had an original anndata object with 12 variables - this data pertains to spatially barcoded proteins (there is no transcriptomic data). Since the normalization in this case is a bit different, I had to export raw.X as a dataframe, remove some variables, and independently do the normalization based on the images. In the end, I had a data frame of new X values for 8 variables.

I subsetted the original anndata, such that it now had 8 variables, and now I want to add the normalized X values to the subsetted anndata. Right now, I have added them as a new layer:

adata_pc_subsampled.layers["X_norm"] = df_merge[["var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8"]]

The problem is that I want to run scanpy.pp.neighbors and on this X_norm layer. How can I do this?

Or maybe you could copy your existing adata.X to a layer and then do sth like this :

adata.X = adata.layers[“X_norm”].copy()

Hi @singhbhavya, you can save your new layer to adata.obsm and use the use_rep argument in scanpy.pp.neighbors to compute neighbors on that representation. You can then run on this knn graph.

1 Like