Is there a way to keep multiple umaps with different parameters?

I’d like to run a sweep over parameters of umap / tsne to see the effect.

And just as for neighbors, I believe there can be something like '‘key added’ thing that can keep multiple projections in the same file, so I could use cellxgene later.

Is there any advice how I can achieve this? Thanks!

I think this is what you are after. Loop through a list of neighbors and add a key for each neighbors and later pass the neighbors key to umap. scanpy will automatically set umap embedding to .obsm[‘X_umap’] so you have to manually change the name if you wish to hold more than one.

Just be mindful with any downstream analysis using neighbors (scVelo, Louvain clustering, etc) to pass the neighbors_key you would like the function to use. Same with umap be sure to pass the basis name for the embedding you would like to use, since the default .obsm[‘X_umap’] will be the last run you completed. Or once you have settled on the parameters you plan to use, you can re-run neightbors and umap functions without passing and keys and they will then be your defaults for any downstream analysis.

# list of neighbors to try
n_neighbors = [10,15,20]
for nn_itr in n_neighbors:
    # key for each neighbors try
    key_itr = f"nn{nn_itr}"
    # run neighbors and set unique key
    sc.pp.neighbors( adata, n_neighbors=nn_itr, key_added=key_itr)
    # run umap while pointing to neighbors key
    sc.tl.umap( adata, neighbors_key=key_itr)
    # change name of umap to match nn used, remember X_umap will be whatever parameters you ran last
    adata.obsm[f"umap_{key_itr}"] = adata.obsm['X_umap']
    
# to plot each embedding reference obsm embedding of interest
sc.pl.embedding( adata, basis="umap_nn20", color=['n_counts_all'])

Hope this helps.
Chuck

1 Like

Thanks for suggestion, Chuck!

Apologies, forgot to add details about cellxgene. I have only briefly used cellxgene but if I remember correctly you can add embeddings by updating their configure.csv (sorry cant remember actual name, so name of file may not be configure.csv). If you have ran PCA there will be an entry for it, I just copied the format and changed names to match my embedding of choice. There could also be an issue with cellxgene recognizing an embedding in obsm if it doesn’t start with “X_”, so you may have to add it to your embedding names.

I’ve tried with just copying obsm elements and that seems to work with cellxgene

1 Like