bedapub / besca

BESCA (Beyond Single Cell Analysis) offers python functions for single-cell analysis
https://bedapub.github.io/besca/
GNU General Public License v3.0
49 stars 17 forks source link

KeyError: 'base' when using bc.tl.dge.get_de() or bc.st.additional_labeling() #271

Open llumdi opened 1 year ago

llumdi commented 1 year ago

By using besca25 I get the following error in the celltype annotation notebook:

DEgenes = bc.tl.dge.get_de(adata, clusters, demethod="wilcoxon", topnr=5000, logfc=1, padj=0.05)

 ---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In [17], line 1
----> 1 DEgenes = bc.tl.dge.get_de(adata, clusters, demethod="wilcoxon", topnr=5000, logfc=1, padj=0.05)

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/besca/tl/dge/_dge.py:698, in get_de(adata, mygroup, demethod, topnr, logfc, padj)
    696 mygroups = list(sort(list(set(adata.obs[mygroup]))))
    697 delist = {}
--> 698 rank_genes_groups(
    699     adata,
    700     groupby=mygroup,
    701     use_raw=True,
    702     n_genes=adata.raw.X.shape[1],
    703     method=demethod,
    704 )
    705 for i in mygroups:
    706     df = DataFrame(adata.uns["rank_genes_groups"]["names"]).head(topnr)[i]

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:590, in rank_genes_groups(adata, groupby, use_raw, groups, reference, n_genes, rankby_abs, pts, key_added, copy, method, corr_method, tie_correct, layer, **kwds)
    580 adata.uns[key_added] = {}
    581 adata.uns[key_added]['params'] = dict(
    582     groupby=groupby,
    583     reference=reference,
   (...)
    587     corr_method=corr_method,
    588 )
--> 590 test_obj = _RankGenes(adata, groups_order, groupby, reference, use_raw, layer, pts)
    592 if check_nonnegative_integers(test_obj.X) and method != 'logreg':
    593     logg.warning(
    594         "It seems you use rank_genes_groups on the raw count data. "
    595         "Please logarithmize your data before calling rank_genes_groups."
    596     )

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:93, in _RankGenes.__init__(self, adata, groups, groupby, reference, use_raw, layer, comp_pts)
     82 def __init__(
     83     self,
     84     adata,
   (...)
     90     comp_pts=False,
     91 ):
---> 93     if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
     94         self.expm1_func = lambda x: np.expm1(x * np.log(adata.uns['log1p']['base']))
     95     else:

KeyError: 'base'

This is a blocking error for my analysis. I hope you can help. Best, Llucia

llumdi commented 1 year ago

Same error in function bc.st.additional_labeling()

adata = bc.st.additional_labeling(adata, "celltype1", "celltype1", "Major cell types, short names", annot_author, results_folder)

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In [93], line 2
      1 ### Save labelling celltype1
----> 2 adata = bc.st.additional_labeling(adata, "celltype0", "celltype0", "Broad annotation, short names", annot_author, results_folder)
      3 adata = bc.st.additional_labeling(adata, "celltype1", "celltype1", "Major cell types, short names", annot_author, results_folder)
      4 adata = bc.st.additional_labeling(adata, "celltype2", "celltype2", "Intermediate cell types, short names", annot_author, results_folder)

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/besca/st/_wrapper_funcs.py:706, in additional_labeling(adata, labeling_to_use, labeling_name, labeling_description, labeling_author, results_folder, cluster_method, is_celltype_labeling, filename)
    703 if len(set(adata.obs.get(labeling_to_use))) != 1:
    704     start1 = time()
--> 706     rank_genes_groups(
    707         adata,
    708         labeling_to_use,
    709         method="wilcoxon",
    710         use_raw=True,
    711         n_genes=adata.raw.X.shape[1],
    712     )
    713     print("rank genes per label calculated using method wilcoxon.")
    714     logging.info(
    715         "Marker gene detection performed on a per-label basis using the method wilcoxon."
    716         + "\n\tTime for marker gene detection: "
    717         + str(round(time() - start1, 3))
    718         + "s"
    719     )

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:590, in rank_genes_groups(adata, groupby, use_raw, groups, reference, n_genes, rankby_abs, pts, key_added, copy, method, corr_method, tie_correct, layer, **kwds)
    580 adata.uns[key_added] = {}
    581 adata.uns[key_added]['params'] = dict(
    582     groupby=groupby,
    583     reference=reference,
   (...)
    587     corr_method=corr_method,
    588 )
--> 590 test_obj = _RankGenes(adata, groups_order, groupby, reference, use_raw, layer, pts)
    592 if check_nonnegative_integers(test_obj.X) and method != 'logreg':
    593     logg.warning(
    594         "It seems you use rank_genes_groups on the raw count data. "
    595         "Please logarithmize your data before calling rank_genes_groups."
    596     )

File ~/scratch/conda/envs/besca25_LAS/lib/python3.8/site-packages/scanpy/tools/_rank_genes_groups.py:93, in _RankGenes.__init__(self, adata, groups, groupby, reference, use_raw, layer, comp_pts)
     82 def __init__(
     83     self,
     84     adata,
   (...)
     90     comp_pts=False,
     91 ):
---> 93     if 'log1p' in adata.uns_keys() and adata.uns['log1p']['base'] is not None:
     94         self.expm1_func = lambda x: np.expm1(x * np.log(adata.uns['log1p']['base']))
     95     else:

KeyError: 'base'
kohleman commented 1 year ago

Looks like this scanpy bug: https://github.com/scverse/scanpy/issues/2239 The workaround as described in the above issue would be:

adata.uns['log1p']["base"] = None

Every time after reading a h5ad file.

llumdi commented 1 year ago

Thanks, it works now. Would be good to add it in the celltype notebook until is fixed