R语言:单细胞:挑选PC分群聚类

复制代码
> library(dplyr)
> library(patchwork)
> library(ggplot2)
> library(SingleR)
> library(randomcoloR)
> library(clustree)

#生成随机颜色

> randomColor <- function() {

paste0("#",paste0(sample(c(0:9, letters[1:6]), 6, replace = TRUE),collapse = ""))

}

生成100个随机颜色

> randomColors <- replicate(100,randomColor())

> seurat=readRDS("去批次后seurat.rds")#读取数据

> collist=c(ggsci::pal_nejm()(8))

> names(collist)=names(table(seurat$Type))

#热图可视化前20个PC

> pdf(file = "前20个PC热图.pdf",width =7.5,height = 9)

> DimHeatmap(seurat, dims = 1:20, cells = 1000, balanced = TRUE)

> dev.off()

##确定使用PC个数

> seurat <- JackStraw(seurat, num.replicate = 100)

> seurat <- ScoreJackStraw(seurat, dims = 1:20)

> pdf(file = "jackstrawplot.pdf",width =7.5,height = 5.5)

> JackStrawPlot(seurat, dims = 1:20)

> dev.off()

> pdf(file = "ElbowPlot.pdf",width =5,height = 4)

> ElbowPlot(seurat,ndims = 30)

> dev.off()

#选择PC数

> seuratPC=9

##对细胞聚类

> seurat=FindNeighbors(seurat, dims = 1:seuratPC, reduction = "harmony")

#挑选分辨率

> for (res in c(0.01,0.05,0.1,1,1.5,2,2.5,3,3.5,4)) {

seurat=FindClusters(seurat, graph.name = "RNA_snn", resolution = res, algorithm = 1)}

apply([email protected][,grep("RNA_snn_res",colnames([email protected]))],2,table)

> p2_tree=clustree([email protected], prefix = "RNA_snn_res.")

> pdf(file = "挑选分辨率.pdf",width =12,height =10)

> p2_tree

> dev.off()

> seurat=FindNeighbors(seurat, dims = 1:seuratPC, reduction = "harmony")

#选择分辨率进行降维

> px=1

> seurat <- FindClusters(seurat, resolution = px)

only.pos:只保留上调差异表达的基因

> seurat.markers <- FindAllMarkers(seurat, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25)

> write.csv(seurat.markers,file = "每个聚类的marker基因.csv")

> head(seurat.markers)

#选择每个聚类前5各基因绘制热图

> top5seurat.markers <- seurat.markers %>%

group_by(cluster) %>%

top_n(n = 5, wt = avg_log2FC)

> col <- c(ggsci::pal_npg()(9),ggsci::pal_jco()(9),ggsci::pal_jama()(7),ggsci::pal_nejm()(8))

> pdf(file = "聚类热图.pdf",width =22,height = 16)

> DoHeatmap(seurat,features = top5seurat.markers$gene,

group.colors = col) +

ggsci::scale_colour_npg() +

scale_fill_gradient2(low = '#0099CC',mid = 'white',high = '#CC0033',

name = 'Z-score')

> dev.off()

将细胞在低维空间可视化UMAP/tSNE

> seurat <- RunUMAP(seurat, dims = 1:seuratPC, reduction = "harmony")

> seurat <- RunTSNE(seurat, dims = 1:seuratPC, reduction = "harmony")

可视化UMAP/tSNE3

> pdf(file = "聚类后UMAP.pdf",width =6.5,height = 5.5)

> DimPlot(seurat, reduction = "umap", label = T, label.size = 3.5,pt.size = 2)+theme_classic()+theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"),legend.position = "right")

> dev.off()

> pdf(file = "聚类后TSEN.pdf",width =6.5,height = 5.5)

> DimPlot(seurat, reduction = "tsne", label = T, label.size = 3.5,pt.size = 2)+theme_classic()+theme(panel.border = element_rect(fill=NA,color="black", size=0.5, linetype="solid"),legend.position = "right")

> dev.off()

学习交流

相关推荐
R语言学堂9 小时前
R语言空间数据处理入门教程
arcgis·r语言·空间数据处理
lisw053 天前
R语言的专业网站top5推荐
开发语言·r语言
清同趣科研3 天前
扩增子分析|R分析之微生物生态网络稳定性评估之节点和连接的恒常性、节点持久性以及组成稳定性指数计算
开发语言·r语言
zm-v-159304339865 天前
解锁生命周期评价密码:OpenLCA、GREET 与 R 语言的融合应用
r语言·生命周期
生信大杂烩5 天前
R语言绘图 | 渐变火山图
数据分析·r语言
KY_chenzhao7 天前
用R语言+随机森林玩转遥感空间预测-基于R语言机器学习遥感数据处理与模型空间预测技术及实际项目案例分析
随机森林·机器学习·r语言·生态·遥感·空间预测
没有梦想的咸鱼185-1037-16637 天前
【生命周期分析(Life Cycle Assessment: LCA)】基于OpenLCA、GREET、R语言的生命周期评价方法、模型构建及典型案例应用
数据分析·r语言
没有梦想的咸鱼185-1037-16638 天前
全球森林数据如何分析?基于R语言森林生态系统结构、功能与稳定性分析与可视化
开发语言·随机森林·数据分析·r语言
Tiger Z8 天前
R 语言科研绘图 --- 桑基图-汇总
开发语言·r语言·贴图
Expecto010 天前
因子分析——数学原理及R语言代码
算法·r语言·统计学·多元统计分析