mortazavilab / PyWGCNA

PyWGCNA is a Python package designed to do Weighted Gene Correlation Network analysis (WGCNA)
https://academic.oup.com/bioinformatics/advance-article/doi/10.1093/bioinformatics/btad415/7218311
MIT License
214 stars 51 forks source link

Error in Functional Enrichment Analysis: Cannot Set New Category on Categorical Variable #125

Open victorsanchezarevalo opened 1 week ago

victorsanchezarevalo commented 1 week ago

Error in Functional Enrichment Analysis: Cannot Set New Category on Categorical Variable

Description:

While running functional enrichment analysis for the "silver" module using wgcna.functional_enrichment_analysis() with Gene Ontology (GO) sets, I encountered the following error:

TypeError: Cannot setitem on a Categorical with a new category (), set the categories first

This error occurs when the function tries to handle a categorical variable without ensuring that the category exists beforehand. Specifically, it seems related to attempting to assign a new category to the moduleColors column.

Code Snippet:

wgcna.figureType = 'png'
gene_set_library = ["GO_Biological_Process_2021", "GO_Cellular_Component_2021", "GO_Molecular_Function_2021"]

# Run the functional enrichment analysis for the 'silver' module
wgcna.functional_enrichment_analysis(type='GO',
                                     moduleName='silver',
                                     sets=gene_set_library,
                                     p_value=0.05,
                                     file_name='GO_silver')

Full Traceback:

Traceback (most recent call last):
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/PyWGCNA/wgcna.py", line 3246, in functional_enrichment_analysis
    geneModule = self.datExpr.var.gene_name[self.datExpr.var.moduleColors == moduleName]
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/generic.py", line 7349, in fillna
    new_data = self._mgr.fillna(value=value, limit=limit, inplace=inplace, downcast=downcast)
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/internals/base.py", line 186, in fillna
    return self.apply_with_block("fillna", value=value, limit=limit, inplace=inplace, downcast=downcast)
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/internals/managers.py", line 363, in apply
    applied = b.apply(f, **kwargs)
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/internals/blocks.py", line 2334, in fillna
    new_values = self.values.fillna(value=value, method=None, limit=limit)
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/arrays/_mixins.py", line 372, in fillna
    new_values[mask] = value
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/arrays/_mixins.py", line 261, in __setitem__
    value = self._validate_setitem_value(value)
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/arrays/categorical.py", line 1589, in _validate_setitem_value
    return self._validate_scalar(value)
  File "~/miniconda3/envs/pywgcna/lib/python3.10/site-packages/pandas/core/arrays/categorical.py", line 1614, in _validate_scalar
    raise TypeError(
TypeError: Cannot setitem on a Categorical with a new category (), set the categories first

Steps to Reproduce:

  1. Load an AnnData object with expression data, sample metadata, and gene information.
  2. Run PyWGCNA's functional_enrichment_analysis using a gene set library such as GO with a specific module name (e.g., "silver").
  3. The error occurs when attempting to process the categorical moduleColors variable.
nargesr commented 1 day ago

hi @victorsanchezarevalo

Thank you for your detailed issue.

Can you tell me if you ever did any preprocessing on your WGCNA object, besides running the Quick_start notebook??

I do not think I ever converted the type of self.datExpr.var.moduleColors to Categorical data when it should have been 'object', which might explain why I wasn't able to reproduce the error.

Also if you can share the whole script you used for making the object, I can look at it more deeply

victorsanchezarevalo commented 2 hours ago

Hi,

I have sent yoy by e-mail the script.

Thanks a lot!

Vic