参考:https://www.jianshu.com/p/9850c4d88a67
| # 1.以pbmc3k为例,加载Seurat数据+设置工作目录----##### |
| setwd("/public/home/chidm/test/Gene相关性") |
| load("/public/home/chidm/test/pbmc3k/pbmc3k_final.RData") |
| pbmc |
| |
| exprSet <- pbmc@assays$RNA@data |
| exprSet<-as.data.frame(t(exprSet))#转置 |
| # 2. 计算某个基因和其它基因的相关性(以S100A8为例)-----##### |
| y <- as.numeric(exprSet[,"S100A8"]) |
| colnames<-colnames(exprSet) |
| cor_data_df<- data.frame(colnames) |
| for(i in 1:length(colnames)){ |
| test<-cor.test(as.numeric(exprSet[,i]),y,type="spearman") #可更换pearson |
| cor_data_df[i,2]<- test$estimate |
| cor_data_df[i,3]<- test$p.value |
| |
| } |
| names(cor_data_df)<-c("symbol","correlation","pvalue") |
| cor_data_df %>% head() |
| # 3. 筛选有意义的正相关和负相关的基因-----#### |
| library(dplyr) |
| library(tidyr) |
| cor_data_sig_pos <- cor_data_df %>% |
| dplyr::filter(pvalue <0.01)%>%dplyr::filter(correlation >0)%>% |
| dplyr::arrange(desc(correlation)) |
| |
| cor_data_sig_neg <- cor_data_df %>% |
| dplyr::filter(pvalue <0.01)%>%dplyr::filter(correlation <0)%>% |
| dplyr::arrange(desc(abs(correlation))) |
| # 4. 随机选取正相关和负相关基因,分别作图验证 |
| #1)S100A9正相关#### |
| # install.packages("ggstatsplot") ##linux安装过程中报错,需先安装mpfr工具 |
| # $conda install anaconda::mpfr |
| library(ggstatsplot) |
| png(paste0("相关性densigram-正相关.png"), width = 6, height = 6, res = 400, units = "in") |
| ggscatterstats(data = exprSet , |
| y = S100A8, |
| x = S100A9, |
| centrality.para = "mean", |
| margins = "both", |
| xfill = "#CC79A7", |
| yfill = "#009E73", |
| marginal.type = "densigram", # #类型可以换成density,boxplot,violin,densigram |
| title = "Relationship between S100A8 and S100A9") |
| dev.off() |
| #2)S100A9负相关#### |
| library(ggstatsplot) |
| png(paste0("相关性densigram-负相关.png"), width = 6, height = 6, res = 400, units = "in") |
| ggscatterstats(data = exprSet, |
| y = MALAT1, |
| x = S100A9, |
| centrality.para = "mean", |
| margins = "both", |
| xfill = "#CC79A7", |
| yfill = "#009E73", |
| marginal.type = "densigram", # #类型可以换成density,boxplot,violin,densigram |
| title = "Relationship between MALAT1 and S100A9") |
| dev.off() |
| |
| # 这两个图的更简单画法(Seurat的FeatureScatter函数) |
| library(Seurat) |
| library(SeuratObject) |
| png(paste0("相关性-FeatureScatter.png"), width = 8, height = 4, res = 400, units = "in") |
| p1=FeatureScatter(pbmc, feature1 = "S100A8", feature2 = "S100A9") |
| p2=FeatureScatter(pbmc, feature1 = "S100A8", feature2 = "MALAT1") |
| p1|p2 |
| dev.off() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了