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
192 stars 46 forks source link

Seeking help on `order` Parameter Usage and Metadata Color Settings #92

Open KGZaker opened 3 months ago

KGZaker commented 3 months ago

Firstly, thanks so much for developing such a useful tool. It has been immensely helpful in my research. However, I've encountered a couple of challenges that I hope you could assist me with:

  1. I came across the following description in the Data_format.md:

    The sample metadata is a table which contains additional information about each sample, such as timepoint or genotype. Each row should represent a sample and each column should represent a metadata feature, where the first column contains the same sample identifier that was used in the gene expression matrix. The rows should be in the same order as the rows of the gene expression matrix, or the user can specify order=False.

    Based on this: if the rows of the sample metadata and the rows of the gene expression matrix are not in the same order, or if the columns of the gene metadata and the rows of the gene expression matrix are not in the same order, in which function should I specify order=False? Should it be in pyWGCNA.analyseWGCNA() or do I need to specify it in other functions like PyWGCNA.WGCNA, pyWGCNA.preprocess(), or pyWGCNA.findModules()? I tried using pyWGCNA.analyseWGCNA(order=False), but I encountered the following error: TypeError: 'bool' object is not iterable.

  2. It appears that I need to first specify colors using pyWGCNA.setMetadataColor before I can correctly call pyWGCNA.analyseWGCNA(). However, in the step of pyWGCNA.setMetadataColor(), does it only support categorical variables and not continuous variables, which means I need to convert continuous variables into categorical ones?

Thank you sooooo much for your time and assistance.

nargesr commented 3 months ago

Hi, Thank you for your kind comments:)

  1. you need to specify this when you update your metadata. but I just realized I updated it in a way you don’t need to specify it. the only key import thing is that the index should match and it will update it based on the index for more information look at the updateGeneInfo() and updateSampleInfo().

I will update the Data_format in the next week to reflect changes (I will notify you through this issue).

thanks for catching this :)

  1. if you have a continuous value you can pass the color as a color palette using matplotlib.cm.ScalarMappable.
KGZaker commented 3 months ago

Thanks for your quick reply. Sorry, I may still have a naive question😂. Regarding the second solution, I attempted the following codes, but encountered an error: AttributeError: 'ScalarMappable' object has no attribute 'keys'.

import matplotlib as mpl
norm = mpl.colors.Normalize(vmin=min(adata.obs['age']), 
                            vmax=max(adata.obs['age']))
cmap = mpl.colormaps['viridis']
pyWGCNA.setMetadataColor('age', mpl.cm.ScalarMappable(norm, cmap))

pyWGCNA.analyseWGCNA(geneList=adata.var)
nargesr commented 3 months ago

Hi @KGZaker,

I believe I used the same script! lol can you send me the full error?

import matplotlib as mpl

norm = mpl.colors.Normalize(vmin=adata.obs['age'].min(), 
                            vmax=adata.obs['age'].max())
my_palette_age = mpl.cm.ScalarMappable(norm=norm, cmap='viridis')
KGZaker commented 3 months ago

Hi, thanks for helping me figure out what's wrong. Here is the code I used and errors I got:

pyWGCNA.updateSampleInfo(adata.obs[["sex", "age"]])

norm = mpl.colors.Normalize(vmin=adata.obs['age'].min(),  vmax=adata.obs['age'].max())
my_palette_age = mpl.cm.ScalarMappable(norm=norm, cmap='viridis')

pyWGCNA.setMetadataColor('age', my_palette_age)
pyWGCNA.barplotModuleEigenGene(moduleName="white", metadata=["age"])
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[229], line 1
----> 1 pyWGCNA.barplotModuleEigenGene(moduleName="white", metadata=["age"])

File [~/micromamba/envs/macbiom/lib/python3.11/site-packages/PyWGCNA/wgcna.py:3028](http://localhost:8889/~/micromamba/envs/macbiom/lib/python3.11/site-packages/PyWGCNA/wgcna.py#line=3027), in WGCNA.barplotModuleEigenGene(self, moduleName, metadata, combine, colorBar, show)
   3026 height_ratios = []
   3027 for m in metadata:
-> 3028     height_ratios.append(len(list(self.metadataColors[m].keys())))
   3029 height_ratios.reverse()
   3031 modules = np.unique(self.datExpr.var['moduleColors']).tolist()

AttributeError: 'ScalarMappable' object has no attribute 'keys'
nargesr commented 3 months ago

Hi @KGZaker

I just released the new version (2.0.4) which hopefully will solve your problem. Please upgrade the PyWGCNA and let me know if you still have the same problem

KGZaker commented 3 months ago

Hi @nargesr , thanks so much for your time and kind help. Now everything works well in the new version.

realzhipeng commented 1 day ago

Hello @nargesr . I also want to setup this one. But I still meet the error: "'ScalarMappable' object has no attribute 'keys'". And I'm using 2.0.4 the new version. How to get it corrected?

nargesr commented 1 day ago

Hi @realzhipeng

could you please provide me with the script you used and the full error you got?

Thanks

realzhipeng commented 1 day ago

@nargesr , Yes. Actually, I'm using the above code that you shared with KGZaker.

pyWGCNA_datset.setMetadataColor('Sex',` {'F': 'Pink',
                                       'M': 'blue'})
norm = mpl.colors.Normalize(vmin=df_sampleids_expr['Age'].min(),  vmax=df_sampleids_expr['Age'].max())

my_palette_age = mpl.cm.ScalarMappable(norm=norm, cmap='viridis')

pyWGCNA_datset.setMetadataColor("Age",my_palette_age)

Additionally, df_sampleids_expr is: image image

And the error after run pyWGCNA_datset.analyseWGCNA(): image

Let me know if you need the more detail.