回溯亚群细分效果到初始分群
1 加载R包
2 读取Seurat object
3 提取子集并做亚群分析
4 修改亚群命名,便于区分初始分群序号
5 回溯亚群细分效果到初始分群
1 加载R包
library(Seurat)
library(ggplot2)
2 读取Seurat object
# Remove variables
rm(list = ls())
# Set workdir
workdir <- 'F:/R_Language/R_Practice/scRNA_Seq_column'
setwd(workdir)
# Load seurat object variable
data.filt <- readRDS(file = "data/Raw_data/Cluster_all_again.Rdata")
print(table(Idents(data.filt)))##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 406 399 352 340 332 283 247 208 184 181 159 81 56 47 41 37DT::datatable(data.filt@meta.data)
3 提取子集并做亚群分析
sub_cells <- subset(data.filt, cluster == 1)
DT::datatable(sub_cells@meta.data)
# Determine the K-nearest neighbor graph
sub_cells <- FindNeighbors(sub_cells, reduction="pca", dims = 1:20, verbose = F)
# Determine the clusters for various resolutions
sub_cells <- FindClusters(sub_cells, resolution = 0.5, verbose = F)
# t-SNE and Clustering
sub_cells <- RunTSNE(sub_cells, reduction = "pca", dims = 1:20, verbose = F) # check_duplicates = FALSE, Remove duplicates before running TSNE
sub_cells <- RunUMAP(sub_cells, reduction = "pca", dims = 1:20, verbose = F)
4 修改亚群命名,便于区分初始分群序号
## Rename sub cells
metadata <- sub_cells@meta.data
metadata$cluster <- Idents(sub_cells)
metadata$sub_cluster <- paste0('M', metadata$cluster)
sub_cells@meta.data <- metadata
table(sub_cells$sub_cluster)##
## M0 M1 M2
## 170 151 85
5 回溯亚群细分效果到初始分群
## Mapping to old clusters
metadata <- data.filt@meta.data
metadata$cell_type <- NA
metadata$cell_type <- sub_cells@meta.data[match(rownames(data.filt@meta.data), rownames(sub_cells@meta.data)), 'sub_cluster']
print(table(metadata$cell_type))##
## M0 M1 M2
## 170 151 85DT::datatable(metadata)
celltype_names <- NULL
for(i in 1:dim(metadata)[1]){
# print(i)
sub_data <- metadata[i,]
if(is.na(sub_data$cell_type)){
# print('Change value')
sub_data$cell_type <- sub_data$cluster
celltype_names <- c(celltype_names, sub_data$cell_type)
}else{
celltype_names <- c(celltype_names, sub_data$cell_type)
}
}
print(table(celltype_names))## celltype_names
## 10 11 12 13 14 15 16 2 3 4 5 6 7 8 9 M0 M1 M2
## 181 159 81 56 47 41 37 399 352 340 332 283 247 208 184 170 151 85metadata$cell_type <- celltype_names
metadata$cell_type <- factor(metadata$cell_type)
metadata$cell_type <- factor(metadata$cell_type, levels = c(names(table(sub_cells$sub_cluster)), 2:nlevels(Idents(data.filt))))
print(table(metadata$cell_type))##
## M0 M1 M2 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 170 151 85 399 352 340 332 283 247 208 184 181 159 81 56 47 41 37data.filt@meta.data <- metadata
DimPlot(data.filt, reduction='tsne', label=T, label.size=5, pt.size=1, group.by='cluster') + ggtitle('original celltype')
DimPlot(data.filt, reduction='tsne', label=T, label.size=5, pt.size=1, group.by='cell_type') + ggtitle('new celltype')