SAMap icon indicating copy to clipboard operation
SAMap copied to clipboard

KeyError: 'run_args' when sam is run using scanpy.tl.external.sam

Open RobTesf opened this issue 10 months ago • 2 comments

Hello, I've just started using this tool, so sorry if I've overlooked something. I imported my raw .h5ad with scanpy and basically did scanpy.tl.external.sam with default parameters. Then I launched SAMAP like this:

sm = SAMAP({"sp1":sam1,"sp2":sam2},
           f_maps="directory/maps/",
           names={"sp1":f_names,"sp2":s_names}, save_processed=True)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[41], line 1
----> 1 sm = SAMAP({"sp1":sam1,"sp2":sam2},
      2            f_maps="directory/maps/",
      3            names={"sp1":f_names,"sp2":s_names}, save_processed=True)

File ~/miniconda3/envs/g2g_env/lib/python3.8/site-packages/samap/mapping.py:135, in SAMAP.__init__(self, sams, f_maps, names, keys, resolutions, gnnm, save_processed, eval_thr)
    132     sam.leiden_clustering(res=res)
    134 if "PCs_SAMap" not in sam.adata.varm.keys():
--> 135     prepare_SAMap_loadings(sam)  
    137 if save_processed and isinstance(data,str):
    138     sam.save_anndata(data.split('.h5ad')[0]+'_pr.h5ad')

File ~/miniconda3/envs/g2g_env/lib/python3.8/site-packages/samap/mapping.py:1242, in prepare_SAMap_loadings(sam, npcs)
   1230 def prepare_SAMap_loadings(sam, npcs=300):
   1231     """ Prepares SAM object to contain the proper PC loadings associated with its manifold.
   1232     Deposits the loadings in `sam.adata.varm['PCs_SAMap']`.
   1233     
   (...)
   1240     
   1241     """
-> 1242     ra = sam.adata.uns["run_args"]
   1243     preprocessing = ra.get("preprocessing", "StandardScaler")
   1244     weight_PCs = ra.get("weight_PCs", False)

KeyError: 'run_args'

Running sam directly and not with the scanpy wrapper gives sam1.adata.uns["run_args"] scanpy's code source shows that this key is saved in sam1.adata.uns["sam"]["run_args"]. SAMap tries to access it through the first way only though (line 1242) ra = sam.adata.uns["run_args"] Can you tell me if I've missed some steps? Thank you for this great tool by the way and your time.

RobTesf avatar Jan 16 '25 10:01 RobTesf

Hello, did you already find an answer to this problem?

Even I am facing the same issue. Does anyone has answer to this issue? Thanks in advance!

juhi-ku avatar Jan 27 '25 17:01 juhi-ku

Hi, The quick fix I made was: sam1.adata.uns['run_args'] = sam1.adata.uns["sam"]["run_args"] or line 1242 of samap's mapping.py file: ra = sam.adata.uns["run_args"] should be replaced by this:

if 'run_args' in sam.adata.uns_keys():
    ra = sam.adata.uns["run_args"]
else:
    ra = sam.adata.uns["sam"]["run_args"]

RobTesf avatar Jan 27 '25 18:01 RobTesf