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

news/2025/1/31 20:29:31 标签: 数据分析, 数据挖掘

文件在单细胞\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) = fs
  save(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图填的
celltype


new.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.niftyadmin.cn/n/5838860.html

相关文章

在 WSL2 中重启 Ubuntu 实例

在 WSL2 中重启 Ubuntu 实例&#xff0c;可以按照以下步骤操作&#xff1a; 方法 1: 使用 wsl 命令 关闭 Ubuntu 实例: 打开 PowerShell 或命令提示符&#xff0c;运行以下命令&#xff1a; wsl --shutdown这会关闭所有 WSL2 实例。 重新启动 Ubuntu: 再次打开 Ubuntu&#x…

一文了解视觉Transformer解析 !!

文章目录 前言 模型解析 图像Token化 Token Processing 编码块 神经网络模块 预测处理 完整代码 前言 自2017年“注意力就是一切”的理念问世以来&#xff0c;Transformer模型便迅速再自然语言处理&#xff08;NLP&#xff09;领域展露头角&#xff0c;确立了其领先地位。到了…

智能汽车网络安全威胁报告

近年来随着智能汽车技术的快速发展&#xff0c;针对智能汽车的攻击也逐渐从传统的针对单一车辆控制器的攻击转变为针对整车智能化服务的攻击&#xff0c;包括但不限于对远程控制应用程序的操控、云服务的渗透、智能座舱系统的破解以及对第三方应用和智能服务的攻击。随着WP.29 …

Python中的函数(下)

函数返回值 返回单个值 函数可以通过 return 语句返回一个值。一旦执行到 return 语句&#xff0c;函数就会停止执行&#xff0c;并将指定的值返回给调用者。例如&#xff1a; 返回多个值 实际上&#xff0c;Python函数只能返回一个值&#xff0c;但可以通过返回一个元组来模…

Vue3的el-table-column增加跳转其他页面

效果图 既不影响显示内容&#xff0c;也不影响页面跳转 el-table-column写法 <el-table-columnlabel"系统单号"align"center"prop"systematicReceipt"width"180" ><template #default"scope"><el-link t…

将pandas.core.series.Series类型的小数转化成百分数

大年初二&#xff0c;大家过年好&#xff0c;蛇年行大运&#xff01; 今天在编写一个代码的时候&#xff0c;使用 import pandas as pd产生了pandas.core.series.Series类型的数据&#xff0c;里面有小数&#xff0c;样式如下&#xff1a; 目的&#xff1a;将这些小数转化为百…

Linux多路转接poll

Linux多路转接poll 1. poll() poll() 结构包含了要监视的 event 和发生的 event &#xff0c;接口使用比 select() 更方便。且 poll 并没有最大数量限制&#xff08;但是数量过大后性能也是会下降&#xff09;。 2. poll() 的工作原理 poll() 不再需要像 select() 那样自行…

大一计算机的自学总结:位运算实现加减乘除

前言 位运算当然可以用来实现加减乘除。 一、加法 #include<bits/stdc.h> using namespace std;int add(int a,int b) {int ansa;while(b!0){ansa^b;b(a&b)<<1;aans;}return ans; }int main() {int a,b;cout<<"a,b:"<<endl;cin>&g…