挑选富集分析结果 enrichments

2024-01-12 23:36

本文主要是介绍挑选富集分析结果 enrichments,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 


#2.2挑选term---selected_clusterenrich=enrichmets[grepl(pattern = "cilium|matrix|excular|BMP|inflamm|development|muscle|vaso|pulmonary|alveoli",x = enrichmets$Description),]head(selected_clusterenrich) distinct(selected_clusterenrich)# remove duplicate rows based on Description 并且保留其他所有变量
distinct_df <- distinct(enrichmets, Description,.keep_all = TRUE)library(ggplot2)
ggplot( distinct_df %>%dplyr::filter(stringr::str_detect(pattern = "cilium|matrix|excular|BMP|inflamm|development|muscle",Description))  %>%group_by(Description) %>%add_count()  %>%dplyr::arrange(dplyr::desc(n),dplyr::desc(Description)) %>%mutate(Description =forcats:: fct_inorder(Description)), #fibri|matrix|collaaes(Cluster, Description)) +geom_point(aes(fill=p.adjust, size=Count), shape=21)+theme_bw()+theme(axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5),axis.text.y=element_text(size = 12),axis.text = element_text(color = 'black', size = 12))+scale_fill_gradient(low="red",high="blue")+labs(x=NULL,y=NULL)
# coord_flip()head(enrichmets)ggplot( distinct(enrichmets,Description,.keep_all=TRUE)  %>% #  dplyr::mutate(Cluster = factor(Cluster, levels = unique(.$Cluster))) %>%dplyr::mutate(Description = factor(Description, levels = unique(.$Description))) %>%#  dplyr::group_by(Cluster)  %>%dplyr::filter(stringr::str_detect(pattern = "cilium organization|motile cilium|cilium movemen|cilium assembly|cell-matrix adhesion|extracellular matrix organization|regulation of acute inflammatory response to antigenic stimulus|collagen-containing extracellular matrix|negative regulation of BMP signaling pathway|extracellular matrix structural constituent|extracellular matrix binding|fibroblast proliferation|collagen biosynthetic process|collagen trimer|fibrillar collagen trimer|inflammatory response to antigenic stimulus|chemokine activity|chemokine production|cell chemotaxis|chemoattractant activity|NLRP3 inflammasome complex assembly|inflammatory response to wounding|Wnt signaling pathway|response to oxidative stress|regulation of vascular associated smooth muscle cell proliferation|venous blood vessel development|regulation of developmental growth|lung alveolus development|myofibril assembly|blood vessel diameter maintenance|gas transport|cell maturation|regionalization|oxygen carrier activity|oxygen binding|vascular associated smooth muscle cell proliferation",Description))     %>%#  group_by(Description) %>%add_count()  %>%dplyr::arrange(dplyr::desc(n),dplyr::desc(Description))  %>%mutate(Description =forcats:: fct_inorder(Description)), #fibri|matrix|collaaes(Cluster, y = Description)) +  #stringr:: str_wrapgeom_point(aes(fill=p.adjust, size=Count), shape=21)+theme_bw()+theme(axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5),axis.text.y=element_text(size = 12),axis.text = element_text(color = 'black', size = 12))+scale_fill_gradient(low="red",high="blue")+labs(x=NULL,y=NULL)
# coord_flip()
print(getwd())p=ggplot( distinct(enrichmets,Description,.keep_all=TRUE)  %>%dplyr::mutate(Description = factor(Description, levels = unique(.$Description))) %>%  #调整terms显示顺序dplyr::filter(stringr::str_detect(pattern = "cilium organization|motile cilium|cilium movemen|cilium assembly|cell-matrix adhesion|extracellular matrix organization|regulation of acute inflammatory response to antigenic stimulus|collagen-containing extracellular matrix|negative regulation of BMP signaling pathway|extracellular matrix structural constituent|extracellular matrix binding|fibroblast proliferation|collagen biosynthetic process|collagen trimer|fibrillar collagen trimer|inflammatory response to antigenic stimulus|chemokine activity|chemokine production|cell chemotaxis|chemoattractant activity|NLRP3 inflammasome complex assembly|inflammatory response to wounding|Wnt signaling pathway|response to oxidative stress|regulation of vascular associated smooth muscle cell proliferation|venous blood vessel development|regulation of developmental growth|lung alveolus development|myofibril assembly|blood vessel diameter maintenance|gas transport|cell maturation|regionalization|oxygen carrier activity|oxygen binding|vascular associated smooth muscle cell proliferation",Description))  %>%group_by(Description) %>%add_count()  %>%dplyr::arrange(dplyr::desc(n),dplyr::desc(Description)) %>%mutate(Description =forcats:: fct_inorder(Description)), #fibri|matrix|collaaes(Cluster, y = Description)) +  #stringr:: str_wrap#scale_y_discrete(labels = function(x) stringr::str_wrap(x, width = 60)) +  #调整terms长度geom_point(aes(fill=p.adjust, size=Count), shape=21)+theme_bw()+theme(axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5),axis.text.y=element_text(size = 12),axis.text = element_text(color = 'black', size = 12))+scale_fill_gradient(low="red",high="blue")+labs(x=NULL,y=NULL)
# coord_flip()
print(getwd())ggsave(filename ="~/silicosis/spatial/sp_cluster_rigions_after_harmony/enrichents12.pdf",plot = p,width = 10,height = 12,limitsize = FALSE)######展示term内所有基因,用热图展示-------#提取画图的数据p$data#提取图形中的所有基因-----
mygenes=  p$data $geneID %>% stringr::str_split(.,"/",simplify = TRUE)  %>%as.vector()   %>%unique()
frame_for_genes=p$data %>%as.data.frame() %>% dplyr::group_by(Cluster)  #后面使用split的话,必须按照分组排序
head(frame_for_genes)my_genelist=  split(frame_for_genes, frame_for_genes$Cluster, drop = TRUE)  %>%  #注意drop参数的理解lapply(function(x) select(x, geneID));my_genelistmy_genelist=  split(frame_for_genes, frame_for_genes$Cluster, drop = TRUE)  %>%  #注意drop参数的理解lapply(function(x) x$geneID);my_genelistmygenes=my_genelist %>% lapply( function(x)  {stringr::str_split(x,"/",simplify = TRUE)  %>%as.vector()   %>%unique()}   )#准备画热图,加载seurat对象
load("/home/data/t040413/silicosis/spatial_transcriptomics/silicosis_ST_harmony_SCT_r0.5.rds")
{dim(d.all)DefaultAssay(d.all)="Spatial"#visium_slides=SplitObject(object = d.all,split.by = "stim")names(d.all);dim(d.all)d.all@meta.data %>%head()head(colnames(d.all))#1 给d.all 添加meta信息------adata_obs=read.csv("~/silicosis/spatial/adata_obs.csv")head(adata_obs)mymeta=  paste0(d.all@meta.data$orig.ident,"_",colnames(d.all))  %>% gsub("-.*","",.)  # %>%  head()head(mymeta)tail(mymeta)#掉-及其之后内容adata_obs$col= adata_obs$spot_id %>% gsub("-.*","",.)    # %>%  head()head(adata_obs)rownames(adata_obs)=adata_obs$coladata_obs=adata_obs[mymeta,]head(adata_obs)identical(mymeta,adata_obs$col)d.all=AddMetaData(d.all,metadata = adata_obs)head(d.all@meta.data)}##构建画热图对象---
Idents(d.all)=d.all$clusters
a=AverageExpression(d.all,return.seurat = TRUE)
a$orig.ident=rownames(a@meta.data)
head(a@meta.data)
head(markers)rownames(a) %>%head()
head(mygenes)
table(mygenes %in% rownames(a))
DoHeatmap(a,draw.lines = FALSE, slot = 'scale.data', group.by = 'orig.ident',features = mygenes ) + ggplot2:: scale_color_discrete(name = "Identity", labels =  unique(a$orig.ident) %>%sort()  )##doheatmap做出来的图不好调整,换成heatmap自己调整p=DoHeatmap(a,draw.lines = FALSE, slot = 'scale.data', group.by = 'orig.ident',features = mygenes ) + ggplot2:: scale_color_discrete( labels =  unique(a$orig.ident) %>%sort()  ) #name = "Identity",p$data %>%head()##########这种方式容易出现bug,不建议------
if (F) {wide_data <- p$data %>% .[,-4] %>%tidyr:: pivot_wider(names_from = Cell, values_from = Expression)print(wide_data)  mydata=  wide_data %>%dplyr:: select(-Feature) %>%as.matrix()head(mydata)rownames(mydata)=wide_data$Featuremydata=mydata[,c("Bronchial zone", "Fibrogenic zone",   "Interstitial zone",  "Inflammatory zone","Vascular zone"  )]p2=pheatmap::  pheatmap(mydata, fontsize_row = 2, clustering_method = "ward.D2",#     annotation_col = wide_data$Feature,annotation_colors = c("Interstitial zone" = "red", "Bronchial zone" = "blue", "Fibrogenic zone" = "green", "Vascular zone" = "purple") ,cluster_cols = FALSE,column_order = c("Inflammatory zone", "Vascular zone"  ,"Bronchial zone", "Fibrogenic zone"   ))getwd()ggplot2::ggsave(filename = "~/silicosis/spatial/sp_cluster_rigions_after_harmony/heatmap_usingpheatmap.pdf",width = 8,height = 10,limitsize = FALSE,plot = p2)}##########建议如下方式画热图------
a$orig.ident=a@meta.data %>%rownames()
a@meta.data %>%head()
Idents(a)=a$orig.identa@assays$Spatial@scale.data  %>%head()mydata=a@assays$Spatial@scale.data
mydata=mydata[rownames(mydata) %in% (mygenes %>%unlist() %>%unique()) ,]
mydata= mydata[,c( "Fibrogenic zone",  "Inflammatory zone",   "Bronchial zone","Interstitial zone","Vascular zone"  )]
head(mydata)
p3=pheatmap::  pheatmap(mydata, fontsize_row = 2,  clustering_method = "ward.D2",#     annotation_col = wide_data$Feature,annotation_colors = c("Interstitial zone" = "red", "Bronchial zone" = "blue", "Fibrogenic zone" = "green", "Vascular zone" = "purple") ,cluster_cols = FALSE,column_order = c("Inflammatory zone", "Vascular zone"  ,"Bronchial zone", "Fibrogenic zone"   )
)getwd()
ggplot2::ggsave(filename = "~/silicosis/spatial/sp_cluster_rigions_after_harmony/heatmap_usingpheatmap2.pdf",width = 8,height = 10,limitsize = FALSE,plot = p3)#########单独画出炎症区和纤维化区---------
a@assays$Spatial@scale.data  %>%head()mydata=a@assays$Spatial@scale.data
mygenes2= my_genelist[c('Inflammatory zone','Fibrogenic zone')] %>%  unlist() %>% stringr::str_split("/",simplify = TRUE) mydata2=mydata[rownames(mydata) %in% ( mygenes2 %>%unlist() %>%unique()) ,]
mydata2= mydata2[,c( "Fibrogenic zone",  "Inflammatory zone" )]
head(mydata2)p3=pheatmap::  pheatmap(mydata2, fontsize_row = 5,  #scale = 'row',clustering_method = "ward.D2",#     annotation_col = wide_data$Feature,annotation_colors = c("Interstitial zone" = "red", "Bronchial zone" = "blue", "Fibrogenic zone" = "green", "Vascular zone" = "purple") ,cluster_cols = FALSE,column_order = c("Inflammatory zone", "Vascular zone"  ,"Bronchial zone", "Fibrogenic zone"   )
)getwd()
ggplot2::ggsave(filename = "~/silicosis/spatial/sp_cluster_rigions_after_harmony/heatmap_usingpheatmap3.pdf",width = 4,height = 8,limitsize = FALSE,plot = p3)

这篇关于挑选富集分析结果 enrichments的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/599534

相关文章

慢sql提前分析预警和动态sql替换-Mybatis-SQL

《慢sql提前分析预警和动态sql替换-Mybatis-SQL》为防止慢SQL问题而开发的MyBatis组件,该组件能够在开发、测试阶段自动分析SQL语句,并在出现慢SQL问题时通过Ducc配置实现动... 目录背景解决思路开源方案调研设计方案详细设计使用方法1、引入依赖jar包2、配置组件XML3、核心配

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Java字符串操作技巧之语法、示例与应用场景分析

《Java字符串操作技巧之语法、示例与应用场景分析》在Java算法题和日常开发中,字符串处理是必备的核心技能,本文全面梳理Java中字符串的常用操作语法,结合代码示例、应用场景和避坑指南,可快速掌握字... 目录引言1. 基础操作1.1 创建字符串1.2 获取长度1.3 访问字符2. 字符串处理2.1 子字

Python 迭代器和生成器概念及场景分析

《Python迭代器和生成器概念及场景分析》yield是Python中实现惰性计算和协程的核心工具,结合send()、throw()、close()等方法,能够构建高效、灵活的数据流和控制流模型,这... 目录迭代器的介绍自定义迭代器省略的迭代器生产器的介绍yield的普通用法yield的高级用法yidle

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

kotlin中const 和val的区别及使用场景分析

《kotlin中const和val的区别及使用场景分析》在Kotlin中,const和val都是用来声明常量的,但它们的使用场景和功能有所不同,下面给大家介绍kotlin中const和val的区别,... 目录kotlin中const 和val的区别1. val:2. const:二 代码示例1 Java

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

Spring事务中@Transactional注解不生效的原因分析与解决

《Spring事务中@Transactional注解不生效的原因分析与解决》在Spring框架中,@Transactional注解是管理数据库事务的核心方式,本文将深入分析事务自调用的底层原理,解释为... 目录1. 引言2. 事务自调用问题重现2.1 示例代码2.2 问题现象3. 为什么事务自调用会失效3