带有树状图的 scanpy 相关矩阵

发布于 2025-01-11 23:04:36 字数 801 浏览 3 评论 0原文

我尝试使用我自己的 RNAseq 数据集重新创建 scanpy 教程中描述的相关矩阵。

scanpy中的相关函数是: sc.pl.correlation_matrix ,绘图如下所示:

在此处输入图像描述

这里的主要问题是:不同单元格之间的皮尔逊相关性如何类型计算,而每种细胞类型的矩阵大小不同?

例如:我有 1000 个基因作为列,500 个 CD34+ 细胞作为行,只有 200 个 CD19+ B 细胞。那么如何计算两种细胞类型之间的相关性呢?

此外,计算两个矩阵之间的相关性会产生另一个矩阵(并且不是如上图中所示的单个标量)。例如,numpy 的 corrcoef() 函数应用于两个相同大小的矩阵,结果是另一个矩阵,而不是标量......

我尝试对每个细胞的细胞上的基因表达进行平均类型,因此计算包含相同大小的向量,但它仍然与 scanpy 的结果不匹配。

我遇到了这个对话: https://github.com/theislab/scanpy/p​​ull/425 提到当计算层次聚类时,会创建上面的相关矩阵(但没有提供代码)。

我会很高兴收到任何建议、解释和一些可能的 python/代码实现。

I tried to recreate the correlation matrix that is described in scanpy's tutorial, using my own RNAseq dataset.

The relevant function in scanpy is: sc.pl.correlation_matrix and the plot looks like this:

enter image description here

The main question here is: how was this Pearson's correlation between different cell types calculated, while the size of the matrix for each cell type is different?

For example: I have 1000 genes as columns, 500 CD34+ cells as rows and only 200 CD19+ B cells. So how is it possible to calculate a correlation between both cell types?

Additionally, calculating a correlation between two matrices, results in another matrix (and not with a single scalar such as displayed in the plot above). For example, numpy's corrcoef() function that is applied on two matrices of the same size results in another matrix and not with a scalar...

I tried to average the genes expression over the cells for each cell type so the calculation contained equally-sized vectors, and it still didn't match scanpy's results.

I encountered this conversation: https://github.com/theislab/scanpy/pull/425 mentioning that when hierarchical clustering is computed, this correlation matrix above is created (but no code was provided).

I'll be happy with any suggestions, explanations and some possible python / code implementations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月染过的梦 2025-01-18 23:04:36

我设法通过一些反复试验的方法解决了这个问题。

事实上,不可能从两个大小不等的矩阵之间的相关性获得单个标量。

事实上,这个函数中的相关性根本不是在基因上进行的!它是根据数据集的 50 个分量 PCA 结果计算得出的!

进行 PCA 后,您必须对每种细胞类型的每个分量的值进行平均(因为每种细胞类型包含不同数量的细胞)。因此,您将为每种细胞类型获得一个向量 (1*50)。

您最终将得到一个数据框,其中每列都是不同的单元格类型,每行是每种单元格类型的 PCA 的平均分量。

皮尔逊相关性的计算可以通过使用pandas的corr()函数在最终的平均PCA数据帧上简单地实现。

绘图的排列(包括树状图)可以使用seaborn的clustermap()函数来实现。

就是这样 :)

I managed to solve this problem with some trial and error method.

Indeed, it is impossible to obtain a single scalar from a correlation between two unequal-sized matrices.

In fact, the correlation in this function is not conducted on genes at all! It is calculated on the results of a 50-components-PCA of the dataset!

After conducting PCA, you'll have to average the values of every component for each cell type (because each cell type contains different number of cells). Thus, you'll get a single vector (1*50) for each cell type.

You will finally have a dataframe where each column is a different cell type and each row is the average component of the PCA for each cell type.

The calculation of the pearson correlation can be simply achieved using the corr() function of pandas on the final averaged PCA dataframe.

The arrangement of the plot, including dendrogram, can be achieved with seaborn's clustermap() function.

That's it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文