【单细胞-第三节 多样本数据分析】

devtools/2025/2/5 16:13:11/

文件在单细胞\5_GC_py\1_single_cell\1.GSE183904.Rmd
GSE183904
数据原文

1.获取临床信息

筛选样本可以参考临床信息

rm(list = ls())
library(tinyarray)
a = geo_download("GSE183904")$pd
head(a)
table(a$Characteristics_ch1) #统计各样本有多少

2.批量读取

学会如何读取特定的样本

if(!file.exists("f.Rdata")){#untar("GSE183904_RAW.tar",exdir = "GSE183904_RAW")fs = dir("GSE183904_RAW/")[c(2,7)] #dir("GSE183904_RAW/"),列出所有文件#为了省点内存只做2个样本,去掉[c(2,7)]即做全部样本f = lapply(paste0("GSE183904_RAW/",fs),read.csv,row.names = 1)#row.names = 1写在lapply的括号里,但是它是read.csv的参数fs = stringr::str_split_i(fs,"_",1)names(f) = fssave(f,file = "f.Rdata")
}
load("f.Rdata")
library(Seurat)
scelist = list()
for(i in 1:length(f)){scelist[[i]] <- CreateSeuratObject(counts = f[[i]], project = names(f)[[i]])print(dim(scelist[[i]]))
}
sce.all = merge(scelist[[1]],scelist[-1])
sce.all = JoinLayers(sce.all)  #连接数据head(sce.all@meta.data)
table(sce.all$orig.ident)

3.质控指标

sce.all[["percent.mt"]] <- PercentageFeatureSet(sce.all, pattern = "^MT-")
sce.all[["percent.rp"]] <- PercentageFeatureSet(sce.all, pattern = "^RP[SL]")
sce.all[["percent.hb"]] <- PercentageFeatureSet(sce.all, pattern = "^HB[^(P)]")head(sce.all@meta.data, 3)VlnPlot(sce.all, features = c("nFeature_RNA","nCount_RNA", "percent.mt","percent.rp","percent.hb"),ncol = 3,pt.size = 0, group.by = "orig.ident")

4.整合降维聚类分群

f = "obj.Rdata"
library(harmony)
if(!file.exists(f)){sce.all = sce.all %>% NormalizeData() %>%  FindVariableFeatures() %>%  ScaleData(features = rownames(.)) %>%  RunPCA(pc.genes = VariableFeatures(.))  %>%RunHarmony("orig.ident") %>% #RunHarmony 包,整合多个样本,处理多样本的必备步骤FindNeighbors(dims = 1:15, reduction = "harmony") %>% FindClusters(resolution = 0.5) %>% RunUMAP(dims = 1:15,reduction = "harmony") %>% #reduction = "harmony"必须写上RunTSNE(dims = 1:15,reduction = "harmony")save(sce.all,file = f)
}
load(f)
ElbowPlot(sce.all)
UMAPPlot(sce.all,label = T)
TSNEPlot(sce.all,label = T)

5.手动注释

markers = read.delim("GCmarker.txt",header = F,sep = ";")
library(tidyr)
markers = separate_rows(markers,V2,sep = ",") #拆分marker
markers = split(markers$V2,markers$V1)
DotPlot(sce.all,features = markers,cols = "RdYlBu")+RotatedAxis()
ggplot2::ggsave("dotplot.png",height = 10,width = 25)
writeLines(paste0(as.character(0:13),","))
names(markers)celltype = read.csv("celltype.csv",header = F) #自己照着DotPlot图填的
celltypenew.cluster.ids <- celltype$V2
names(new.cluster.ids) <- levels(sce.all)
seu.obj <- RenameIdents(sce.all, new.cluster.ids)
save(seu.obj,file = "seu.obj.Rdata")
p1 <- DimPlot(seu.obj, reduction = "tsne", label = TRUE, pt.size = 0.5) + NoLegend()
p1

6.自动注释

SingleR完成自主注释,不同的是scRNA = sce.all

library(celldex)
library(SingleR)
ls("package:celldex")
f = "ref_BlueprintEncode.RData"
if(!file.exists(f)){ref <- celldex::BlueprintEncodeData()save(ref,file = f)
}
ref <- get(load(f))
library(BiocParallel)
scRNA = sce.all
test = scRNA@assays$RNA@layers$data
rownames(test) = Features(scRNA)
colnames(test) = Cells(scRNA)
pred.scRNA <- SingleR(test = test, ref = ref,labels = ref$label.main, clusters = scRNA@active.ident)
pred.scRNA$pruned.labels
#查看注释准确性 
plotScoreHeatmap(pred.scRNA, clusters=pred.scRNA@rownames, fontsize.row = 9,show_colnames = T)
new.cluster.ids <- pred.scRNA$pruned.labels
names(new.cluster.ids) <- levels(scRNA)
levels(scRNA)
scRNA <- RenameIdents(scRNA,new.cluster.ids)
levels(scRNA)
p2 <- DimPlot(scRNA, reduction = "tsne",label = T,pt.size = 0.5) + NoLegend()
p1+p2

7.marker基因

找不同细胞类型间的差异基因

f = "markers.Rdata"
if(!file.exists(f)){allmarkers <- FindAllMarkers(seu.obj, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25)save(allmarkers,file = f)
}
load(f)
head(allmarkers)

如果想自行修改orig.ident:使用下边的代码:

sce.all@meta.data$orig.ident=rep(c("a","b"),times= c(ncol(scelist[[1]]),
ncol(scelistl[[2]])))

http://www.ppmy.cn/devtools/156317.html

相关文章

python学opencv|读取图像(四十九)原理探究:使用cv2.bitwise()系列函数实现图像按位运算

【0】基础定义 按位与运算&#xff1a;两个等长度二进制数上下对齐&#xff0c;全1取1&#xff0c;其余取0。 按位或运算&#xff1a;两个等长度二进制数上下对齐&#xff0c;有1取1&#xff0c;其余取0。 按位异或运算&#xff1a; 两个等长度二进制数上下对齐&#xff0c;相…

计算机网络部分知识点(王道考研笔记)

计算机网络体系结构&#xff08;概念、框架&#xff09;&#xff08;选择填空题&#xff09; 什么是计算机网络&#xff1f; 计算机网络的概念&#xff1a;计算机网络是一个将众多分散的、自治的计算机系统&#xff0c;通过通信设备与线路连接起来&#xff0c;由功能完善的软…

【零基础到精通】小白如何自学网络安全

小白人群想学网安但是不知道从哪入手&#xff1f;一篇文章告诉你如何在4个月内吃透网安课程&#xff0c;掌握网安技术 一、基础阶段 1.了解网安相关基础知识 了解中华人民共和国网络安全法、熟知网络安全的相关概念&#xff1a;包括信息安全、风险管理、网络攻防原理、认证与…

Docker 部署 ClickHouse 教程

Docker 部署 ClickHouse 教程 背景 ClickHouse 是一个开源的列式数据库管理系统&#xff08;DBMS&#xff09;&#xff0c;主要用于在线分析处理&#xff08;OLAP&#xff09;。它专为大数据的实时分析设计&#xff0c;支持高速的查询性能和高吞吐量。ClickHouse 以其高效的数…

寒假(五)

请使用read 和 write 实现链表保存到文件&#xff0c;以及从文件加载数据到链表中的功能 link.h #ifndef __link__ #define __link__#include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include <sys/types.h>…

kubernetes 高可用集群搭建

在生产环境中部署 Kubernetes 集群时&#xff0c;确保其高可用性&#xff08;High Availability, HA&#xff09;是至关重要的。高可用性不仅意味着减少服务中断时间&#xff0c;还能提高系统的稳定性和可靠性。本文将详细介绍如何搭建一个高可用的 Kubernetes 集群&#xff0c…

计算机基础知识(第二篇)

计算机基础知识&#xff08;第二篇&#xff09; 嵌入式技术 嵌入式技术 特点&#xff1a;专用性、实时性、低成本、可靠性、体积小 应用&#xff1a;汽车电子、家用电器、通信设备、安防监控、工业自动化、医疗设备 体系结构&#xff1a; 冯诺依曼结构&#xff1a;传统计…

【Python深入浅出】解锁Python3模块:从入门到实战的进阶指南

目录 一、Python3 模块初相识二、模块的类型大揭秘2.1 内置标准模块2.2 第三方开源模块2.3 自定义模块 三、模块导入全攻略3.1 import 语句基础3.2 from...import 语句详解3.3 import as 和 from...import as 的妙用3.4 导入路径与搜索机制 四、常用标准模块深度剖析4.1 os 模块…