2022-07-12 Seurat复习

基本流程:数据导入,QC去除低质量细胞,归一化,由于矩阵过于稀疏所以选取top1000~2000,对每个基因进行zscore,线性降维和非线性降维,聚类(基于线性降维的结果进行KNN,SNN)

rm( list = ls())

library(dplyr)

library(Seurat)

library(patchwork)

# Load the PBMC dataset

pbmc.data <- Read10X(data.dir = "F:/class/test/filtered_gene_bc_matrices/hg19/")

#### Method1对于不规范数据

dir.10x = 'F:/class/test/filtered_gene_bc_matrices/hg19/'

#首先处理genelist文件,考虑到只需要第二行

genes <- read.table(paste0(dir.10x, 'genes.tsv'), stringsAsFactors=F, header=F)$V2

genes <- make.unique(genes, sep = '.')#将重复的以一个点分开

#barcode是当列矩阵

barcodes <- readLines(paste0(dir.10x, 'barcodes.tsv'))

mtx <- Matrix::readMM(paste0(dir.10x, 'matrix.mtx'))

mtx <- as(mtx, 'dgCMatrix')#格式转换

colnames(mtx) = barcodes#赋予列名

rownames(mtx) = genes#赋予行名

pbmc <- CreateSeuratObject(counts = mtx, project = "pbmc3k",min.cells = 3, min.features = 200)

#### Method2

# Initialize the Seurat object with the raw (non-normalized data).

pbmc <- CreateSeuratObject(counts = pbmc.data, project = "pbmc3k", min.cells = 3, min.features = 200)

pbmc

# Lets examine a few genes in the first thirty cells

pbmc.data[c("CD3D", "TCL1A", "MS4A1"), 1:30]

dense.size <- object.size(as.matrix(pbmc.data))

dense.size

sparse.size <- object.size(pbmc.data)

sparse.size

dense.size/sparse.size

#######QC计算线粒体比例,线粒体比例高是死细胞,过滤掉线粒体高的细胞

# The [[ operator can add columns to object metadata. This is a great place to stash QC stats

pbmc[["percent.mt"]] <- PercentageFeatureSet(pbmc, pattern = "^MT-")

# Visualize QC metrics as a violin plot

#三张图每个细胞检测到的基因数量,每个细胞检测到的count数量,每个细胞检测到的线粒体比例

VlnPlot(pbmc, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)

# FeatureScatter is typically used to visualize feature-feature relationships, but can be used

# for anything calculated by the object, i.e. columns in object metadata, PC scores etc.

#count和线粒体基因的的关系

plot1 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "percent.mt")

#count和基因总数的关系

plot2 <- FeatureScatter(pbmc, feature1 = "nCount_RNA", feature2 = "nFeature_RNA")

plot1 + plot2

#根据数据特点选择适合的过滤方式

pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)

######NormalizeData去除测序深度对数据分析的影响

#将每一个细胞的count先归一化到10000,再对每一个基因表达值log

pbmc <- NormalizeData(pbmc, normalization.method = "LogNormalize", scale.factor = 10000)

#pbmc <- NormalizeData(pbmc)

##########Feature selection 1500,3000

pbmc <- FindVariableFeatures(pbmc, selection.method = "vst", nfeatures = 2000)

# Identify the 10 most highly variable genes

top10 <- head(VariableFeatures(pbmc), 10)

top10

# plot variable features with and without labels

plot1 <- VariableFeaturePlot(pbmc)

plot2 <- LabelPoints(plot = plot1, points = top10, repel=TRUE)

plot2

#FindVariableFeatures选择基因的原理平均表达量,一个细胞的平均表达量在不同的细胞之间的方差有多大,方差越大就是细胞类群分开的依据

plot1 + plot2

all.genes <- rownames(pbmc)

pbmc <- ScaleData(pbmc, features = all.genes)#本质还是zscore,每一个基因在所有细胞中平均细胞表达量为0,方差为1

########降维Reduction

pbmc <- RunPCA(pbmc, features = VariableFeatures(object = pbmc))

# Examine and visualize PCA results a few different ways

DimPlot(pbmc, reduction = "pca")#可视化

# FeaturePlot(pbmc, reduction = 'pca', features = c('CD79A', 'CD14', 'FCGR3A', 'CD4', 'CST3', 'PPBP'))

# FeaturePlot(pbmc, reduction = 'pca', features = c('nCount_RNA', 'nFeature_RNA'))

# loadings_sorted = dplyr::arrange(as.data.frame(loadings), desc(PC_1))

# DimHeatmap(pbmc, dims = 1:2, cells = 200, balanced = TRUE)

# print(pbmc[["pca"]], dims = 1:5, nfeatures = 5)

# VizDimLoadings(pbmc, dims = 1:2, reduction = "pca")

# DimHeatmap(pbmc, dims = 1, cells = 500, balanced = TRUE)

# DimHeatmap(pbmc, dims = 1:15, cells = 500, balanced = TRUE)

ElbowPlot(pbmc,ndims = 50)#做了50个pca可视化,纵轴奇异值

# NOTE: This process can take a long time for big datasets, comment out for expediency. More

# approximate techniques such as those implemented in ElbowPlot() can be used to reduce

# computation time

#pbmc <- JackStraw(pbmc, num.replicate = 100)

#pbmc <- ScoreJackStraw(pbmc, dims = 1:20)

#JackStrawPlot(pbmc, dims = 1:15)

#ElbowPlot(pbmc)

# Look at cluster IDs of the first 5 cells

#head(Idents(pbmc), 5)

# If you haven't installed UMAP, you can do so via reticulate::py_install(packages =

# 'umap-learn')

#选取前10的特征进行非线性降维,两种方式umap,tsne,但是umap更好可以更真实的表现群与群的真实距离

pbmc <- RunUMAP(pbmc, dims = 1:10)

FeaturePlot(pbmc, features = c('FCGR3A', 'CD14'), reduction = 'umap')

#DimPlot(pbmc, reduction = "umap")

# note that you can set `label = TRUE` or use the LabelClusters function to help label

#######cluster分群

pbmc <- FindNeighbors(pbmc, dims = 1:10)# louvain cluster, graph based

pbmc <- FindClusters(pbmc, resolution = 0.5)#resolution越大群越多

DimPlot(pbmc, reduction = "umap", group.by = 'seurat_clusters', label=T)

#根据maker对细胞进行细胞注释

FeaturePlot(pbmc, features = c("MS4A1", "TYROBP", "CD14",'FCGR3A', "FCER1A",

                              "CCR7", "IL7R", "PPBP", "CD8A"))

new.cluster.ids <- c("Naive CD4 T", "CD14+ Mono", "Memory CD4 T", "B", "CD8 T", "FCGR3A+ Mono",

                    "NK", "DC", "Platelet")

names(new.cluster.ids) <- levels(pbmc)

pbmc <- RenameIdents(pbmc, new.cluster.ids)

#重新定义的亚群

DimPlot(pbmc, reduction = "umap", label=T)

# ############################################################################################################

# ## DE analysis有很多小群无法判断时

# ############################################################################################################

#利用差异基因的方式,来看所有群表达怎样的marker,来确定每一个群表达什么样的基因,再去推断那个群可能是什么样的细胞亚群

pbmc.markers <- FindAllMarkers(pbmc, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25)#都所有亚群依次求差异基因

pbmc.markers %>% group_by(cluster) %>% slice_max(n = 2, order_by = avg_log2FC)#每个cluster top2的差异基因看

CD4.mem.DEGS <- FindMarkers(pbmc, ident.1 = 'Memory CD4 T', ident.2 = 'Naive CD4 T', min.pct = 0.25)#要计算哪一群的差异基因,要计算的差异基因对照的是什么

#test.use计算方式

# ############################################################################################################

# ## gene signature analysis

# ############################################################################################################

exhaustion_genes = list(c('PDCD1','CD160','FASLG','CD244','LAG3','TNFRSF1B','CCR5','CCL3',

                          'CCL4','CXCL10','CTLA4','LGALS1','LGALS3','PTPN13','RGS16','ISG20',

                          'MX1','IRF4','EOMES','PBX3','NFATC1','NR4A2','CKS2','GAS2','ENTPD1','CA2'))

pbmc = Seurat::AddModuleScore(pbmc, features = exhaustion_genes, name='exhaustion.score')

FeaturePlot(pbmc, features = 'exhaustion.score1', reduction = 'umap')

VlnPlot(pbmc,features = 'exhaustion.score1', group.by = 'seurat_clusters')

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容