#因为seurat包3.2.2版本做单细胞和空转的映射mapping比较好,所以此处用3.2.2版本
#此处先把3.2.2的seurat版本安装在G盘,再加载
#设置包的路径
.libPaths()
.libPaths(c("G:/R_big_packages/","D:/Win10 System/Documents/R/win-library/4.1","C:/Program Files/R/R-4.1.0/library"))
#.libPaths("G:/R_big_packages/")
.libPaths()
'''
require(devtools)
install_version("Seurat", version = "3.2.2",
repos = "http://cran.us.r-project.org")
'''#首先安装下面的包再安装seurat
#install.packages('https://cran.r-project.org/src/contrib/Archive/spatstat/spatstat_1.64-1.tar.gz', repos=NULL,type="source")
#Older versions of Seurat
#Old versions of Seurat, from Seurat v2.0.1 and up, are hosted in CRAN’s archive. To install an old version of Seurat, run:
# Enter commands in R (or R studio, if installed)
# Install the remotes package
# install.packages('remotes')
# Replace '2.3.0' with your desired version
#remotes::install_version(package = 'Seurat', version = package_version('3.2.2'))
#安装完成
library(Seurat)
session_info()
library(dplyr)
library(cowplot)
library(ggplot2)
library(patchwork)
library(openxlsx)
#load(file="G:/silicosis/sicosis/unknown/ST_mapping_1216/All.merge.SCT.rds")
#制作All.merge.SCT文件
table(All.merge.SCT$cell.type)
file = "G:/silicosis/sicosis/YSQ/2022-8-2_EPITHELIAL-4"## 改成想存放文件的路径
dir.create(file)
setwd(file)
getwd()
path=getwd()
getwd()
load("G:/silicosis/sicosis/silicosis-1122-merge/silicosis_cluster_merge.rds")## 17226 25002
label = as.character(Idents(All.merge))
label
table(label)
#Igha+ AT2 cell
#字体容易出错
label[which(label %in% c("AT2 cell-1", "AT2 cell-2", "Igha+ AT2 cell"))] = "AT2 cell"
table(label)
label[1:100]
label[which(label %in% "AT2 cell" & All.merge$RNA@counts["Gpnmb", ] > 0)] = "Gpnmb+ AT2 cell"
table(label)
label[which(label %in% "AT2 cell" & All.merge$RNA@counts["Gpnmb", ] == 0)] = "Gpnmb- AT2 cell"
levels(label)
table(label)
table(Idents(All.merge))
label
levels(Idents(All.merge))
label = factor(label, levels = c(levels(Idents(All.merge))[1:17], "Gpnmb+ AT2 cell", "Gpnmb- AT2 cell"))
table(label)
Idents(All.merge)=label
table(Idents(All.merge))
All.merge.SCT <- CreateSeuratObject(counts = All.merge@assays$RNA@counts, project = "silicosis") #17226 26758
All.merge.SCT = SCTransform(All.merge.SCT, verbose = FALSE) %>% RunPCA(verbose = FALSE) #默认3000个HVG
#非常重要的步骤!!!#All.merge.SCT$cell.type = label
table(Idents(All.merge))
table(All.merge.SCT)
All.merge.SCT$cell.type=Idents(All.merge)
DotPlot(All.merge,features = "Gpnmb")
DotPlot(All.merge,features = "S100a8")
DotPlot(All.merge.SCT,features = "Gpnmb",group.by = "cell.type")
getwd()
cell.type = All.merge.SCT$cell.type
#制作完成
#load(file="G:/silicosis/sicosis/unknown/ST_mapping_1216/All.merge.SCT.rds")
library(Seurat)
library(dplyr)
library(cowplot)
library(ggplot2)
library(RColorBrewer)
library(patchwork)
SpatialColors <- colorRampPalette(colors = rev(x = brewer.pal(n = 11, name = "Spectral")))
#加载sio2_56_sct
load("G:/silicosis/需求/矽肺-数据分析结果-0119--/矽肺-数据分析结果-0119/sio2_56_sct.rds")
anterset.sio2_56_sct <- FindTransferAnchors(reference = All.merge.SCT,
query = sio2_56_sct,
normalization.method="SCT")
predictions.assay.sio2_56_sct <- TransferData(anchorset = anterset.sio2_56_sct,
refdata = All.merge.SCT$cell.type,
prediction.assay = TRUE,
dims = 1:30)
sio2_56_sct[["predictions"]] <- predictions.assay.sio2_56_sct
DefaultAssay(sio2_56_sct) <- "predictions"
rowSums(sio2_56_sct$predictions@data)
dim(sio2_56_sct$predictions@data)
sio2_56_sct$predictions@data = sio2_56_sct$predictions@data[c(levels(cell.type), "max"), ]
rowSums(sio2_56_sct$predictions@data)
pre.mat = sio2_56_sct$predictions@data[-dim(sio2_56_sct$predictions@data)[1],]
pre.mat.sum = data.frame(cell.type = rownames(pre.mat),score = rowSums(pre.mat))
pdf("SiO2_56_spot_cell_type_prediction_split.pdf")
for(i in levels(cell.type)){
p <- SpatialPlot(sio2_56_sct, features=i, pt.size.factor=1.6)+scale_fill_gradientn(limits=c(0,1),colours=SpatialColors(n=100))+labs(title = "SiO2_56")
print(p)
}
dev.off()
num = nrow(sio2_56_sct$predictions@data)
max_index = apply(sio2_56_sct$predictions@data[-num,],2,which.max)
max_cell_type = rownames(sio2_56_sct$predictions@data)[max_index]
max_cell_type = factor(max_cell_type, levels=levels(cell.type))
table(max_cell_type)
spot.mat = data.frame(table(max_cell_type))
colnames(spot.mat) = c("cell.type", "spot.num")
spot.num.score = merge(spot.mat, pre.mat.sum, by.x="cell.type", sort=F)
write.xlsx(spot.num.score, "SiO2_56_cellType_spotNum_score.xlsx", col.names=T, row.names=F)
sio2_56_sct$spot.cell.type = max_cell_type
pdf("SiO2_56_spot_cell_type_prediction_all.pdf", width = 10)
SpatialPlot(sio2_56_sct, group.by="spot.cell.type")+labs(title = "SiO2_56")+guides(fill = guide_legend(override.aes = list(size = 5)))
dev.off()
pdf("SiO2_56_max_spot_cellType.pdf")
for (i in levels(cell.type)){
idx = which(max_cell_type == i)
cell = colnames(sio2_56_sct)[idx]
if(length(idx)!=0){
p = SpatialPlot(sio2_56_sct, cells.highlight=cell)+theme(legend.position="None")+labs(title=i)
print(p)
}
}
dev.off()
#################sio2_7
#load("/data/home/longmin/code_test/silicosis_scRNAseq/unknown/ST_mapping_1012/sio2_7_sct.rds")
load("G:/silicosis/需求/矽肺-数据分析结果-0119-yll/矽肺-数据分析结果-0119/sio2_7_sct.rds")
anterset.sio2_7_sct <- FindTransferAnchors(reference = All.merge.SCT,query = sio2_7_sct,normalization.method="SCT")
predictions.assay.sio2_7_sct <- TransferData(anchorset = anterset.sio2_7_sct, refdata = All.merge.SCT$cell.type, prediction.assay = TRUE, dims = 1:30)
sio2_7_sct[["predictions"]] <- predictions.assay.sio2_7_sct
DefaultAssay(sio2_7_sct) <- "predictions"
# save(sio2_7_sct,file="sio2_7_sct.prediction.rds")
#load("G:\\silicosis\\sicosis\\YSQ\\2022-8-2_EPITHELIAL\\spatialMapping\\Gpnmb\\sio2_7_sct.prediction.rds")
rowSums(sio2_7_sct$predictions@data)
dim(sio2_7_sct$predictions@data)
sio2_7_sct$predictions@data = sio2_7_sct$predictions@data[c(levels(cell.type), "max"), ]
rowSums(sio2_7_sct$predictions@data)
pre.mat = sio2_7_sct$predictions@data[-dim(sio2_7_sct$predictions@data)[1],]
pre.mat.sum = data.frame(cell.type = rownames(pre.mat),score = rowSums(pre.mat))
pdf("SiO2_7_spot_cell_type_prediction_split.pdf")
for(i in levels(cell.type)){
p <- SpatialPlot(sio2_7_sct, features=i, pt.size.factor=1.6)+scale_fill_gradientn(limits=c(0,1),colours=SpatialColors(n=100))+labs(title = "SiO2_7")
print(p)
}
dev.off()
num = nrow(sio2_7_sct$predictions@data)
max_index = apply(sio2_7_sct$predictions@data[-num,],2,which.max)
max_cell_type = rownames(sio2_7_sct$predictions@data)[max_index]
max_cell_type = factor(max_cell_type, levels=levels(cell.type))
table(max_cell_type)
spot.mat = data.frame(table(max_cell_type))
colnames(spot.mat) = c("cell.type", "spot.num")
spot.num.score = merge(spot.mat, pre.mat.sum, by.x="cell.type", sort=F)
write.xlsx(spot.num.score, "SiO2_7_cellType_spotNum_score.xlsx", col.names=T, row.names=F)
sio2_7_sct$spot.cell.type = max_cell_type
pdf("SiO2_7_spot_cell_type_prediction_all.pdf")
SpatialPlot(sio2_7_sct, group.by="spot.cell.type")+labs(title = "SiO2_7")+guides(fill = guide_legend(override.aes = list(size = 5)))
dev.off()
pdf("SiO2_7_max_spot_cellType.pdf")
for (i in levels(cell.type)){
idx = which(max_cell_type == i)
cell = colnames(sio2_7_sct)[idx]
if(length(idx)!=0){
p = SpatialPlot(sio2_7_sct, cells.highlight=cell)+theme(legend.position="None")+labs(title=i)
print(p)
}
}
dev.off()
#################NS_56
#load("/data/home/longmin/code_test/silicosis_scRNAseq/unknown/ST_mapping_1012/NS_56_sct.rds")
#################NS_56
load("G:/silicosis/需求/矽肺-数据分析结果-0119-yll/矽肺-数据分析结果-0119/NS_56_sct.rds")
anterset.NS_56_sct <- FindTransferAnchors(reference = All.merge.SCT,query = NS_56_sct,normalization.method="SCT")
predictions.assay.NS_56_sct <- TransferData(anchorset = anterset.NS_56_sct, refdata = All.merge.SCT$cell.type, prediction.assay = TRUE, dims = 1:30)
NS_56_sct[["predictions"]] <- predictions.assay.NS_56_sct
DefaultAssay(NS_56_sct) <- "predictions"
#save(NS_56_sct,file="NS_56_sct.prediction.rds")
rowSums(NS_56_sct$predictions@data)
dim(NS_56_sct$predictions@data)
NS_56_sct$predictions@data = NS_56_sct$predictions@data[c(levels(cell.type), "max"), ]
rowSums(NS_56_sct$predictions@data)
pre.mat = NS_56_sct$predictions@data[-dim(NS_56_sct$predictions@data)[1],]
pre.mat.sum = data.frame(cell.type = rownames(pre.mat),score = rowSums(pre.mat))
pdf("NS_56_spot_cell_type_prediction_split.pdf")
for(i in levels(cell.type)){
p <- SpatialPlot(NS_56_sct, features=i, pt.size.factor=1.6)+scale_fill_gradientn(limits=c(0,1),colours=SpatialColors(n=100))+labs(title = "NS_56")
print(p)
}
dev.off()
num = nrow(NS_56_sct$predictions@data)
max_index = apply(NS_56_sct$predictions@data[-num,],2,which.max)
max_cell_type = rownames(NS_56_sct$predictions@data)[max_index]
max_cell_type = factor(max_cell_type, levels=levels(cell.type))
table(max_cell_type)
spot.mat = data.frame(table(max_cell_type))
colnames(spot.mat) = c("cell.type", "spot.num")
spot.num.score = merge(spot.mat, pre.mat.sum, by.x="cell.type", sort=F)
write.xlsx(spot.num.score, "NS_56_cellType_spotNum_score.xlsx", col.names=T, row.names=F)
NS_56_sct$spot.cell.type = max_cell_type
pdf("NS_56_spot_cell_type_prediction_all.pdf")
SpatialPlot(NS_56_sct, group.by="spot.cell.type")+labs(title = "NS_56")+guides(fill = guide_legend(override.aes = list(size = 5)))
dev.off()
pdf("NS_56_max_spot_cellType.pdf")
for (i in levels(cell.type)){
idx = which(max_cell_type == i)
cell = colnames(NS_56_sct)[idx]
if(length(idx)!=0){
p = SpatialPlot(NS_56_sct, cells.highlight=cell)+theme(legend.position="None")+labs(title=i)
print(p)
}
}
dev.off()
#################NS_7
#load("/data/home/longmin/code_test/silicosis_scRNAseq/unknown/ST_mapping_1012/NS_7_sct.rds")
load("G:/silicosis/需求/矽肺-数据分析结果-0119-yll/矽肺-数据分析结果-0119/NS_7_sct.rds")
anterset.NS_7_sct <- FindTransferAnchors(reference = All.merge.SCT,query = NS_7_sct,normalization.method="SCT")
predictions.assay.NS_7_sct <- TransferData(anchorset = anterset.NS_7_sct, refdata = All.merge.SCT$cell.type, prediction.assay = TRUE, dims = 1:30)
NS_7_sct[["predictions"]] <- predictions.assay.NS_7_sct
DefaultAssay(NS_7_sct) <- "predictions"
save(NS_7_sct,file="NS_7_sct.prediction.rds")
rowSums(NS_7_sct$predictions@data)
dim(NS_7_sct$predictions@data)
NS_7_sct$predictions@data = NS_7_sct$predictions@data[c(levels(cell.type), "max"), ]
rowSums(NS_7_sct$predictions@data)
pre.mat = NS_7_sct$predictions@data[-dim(NS_7_sct$predictions@data)[1],]
pre.mat.sum = data.frame(cell.type = rownames(pre.mat),score = rowSums(pre.mat))
pdf("NS_7_spot_cell_type_prediction_split.pdf")
for(i in levels(cell.type)){
p <- SpatialPlot(NS_7_sct, features=i, pt.size.factor=1.6)+scale_fill_gradientn(limits=c(0,1),colours=SpatialColors(n=100))+labs(title = "NS_7")
print(p)
}
dev.off()
num = nrow(NS_7_sct$predictions@data)
max_index = apply(NS_7_sct$predictions@data[-num,],2,which.max)
max_cell_type = rownames(NS_7_sct$predictions@data)[max_index]
max_cell_type = factor(max_cell_type, levels=levels(cell.type))
table(max_cell_type)
spot.mat = data.frame(table(max_cell_type))
colnames(spot.mat) = c("cell.type", "spot.num")
spot.num.score = merge(spot.mat, pre.mat.sum, by.x="cell.type", sort=F)
write.xlsx(spot.num.score, "NS_7_cellType_spotNum_score.xlsx", col.names=T, row.names=F)
NS_7_sct$spot.cell.type = max_cell_type
pdf("NS_7_spot_cell_type_prediction_all.pdf")
SpatialPlot(NS_7_sct, group.by="spot.cell.type")+labs(title = "NS_7")+guides(fill = guide_legend(override.aes = list(size = 5)))
dev.off()
pdf("NS_7_max_spot_cellType.pdf")
for (i in levels(cell.type)){
idx = which(max_cell_type == i)
cell = colnames(NS_7_sct)[idx]
if(length(idx)!=0){
p = SpatialPlot(NS_7_sct, cells.highlight=cell)+theme(legend.position="None")+labs(title=i)
print(p)
}
}
dev.off()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338