loupe的空转变成seurat中的对象





sio2_7_exp <- Read10X_h5(filename = "G:/raw_material for omics_chaojie/silicosis for spatial transcription/B26/2.3.h5_files/filtered_feature_bc_matrix.h5" ) #1815
sio2_7_exp_valid=sio2_7_exp
sio2_7=CreateSeuratObject(counts = sio2_7_exp_valid, project = 'SiO2_7', assay = 'Spatial',min.cells=3) #18690*1743
sio2_7$stim <- "sio2_7"
getwd()
#把空转的图片也加入seurat
#sio2_7_img <- Read10X_Image(image.dir="/data/biomath/Spatial_Transcriptome/silicosis/Spatial_transcriptome/B26-SiO2-7d/spatial/")
sio2_7_img<-Read10X_Image("G:/raw_material for omics_chaojie/silicosis for spatial transcription/B26/spatial/")
DefaultAssay(object = sio2_7_img) <- 'Spatial'
sio2_7_img <- sio2_7_img[colnames(sio2_7)]
sio2_7[['image']] <- sio2_7_img
colnames(sio2_7)
length(colnames(sio2_7))
load("G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/sio2_7.rds")






sio2_56_exp <- Read10X_h5(filename = "G:/raw_material for omics_chaojie/silicosis for spatial transcription/B11/2.3.h5_files/filtered_feature_bc_matrix.h5" ) #1887
sio2_56_exp_valid=sio2_56_exp
sio2_56=CreateSeuratObject(counts = sio2_56_exp_valid, project = 'SiO2_56', assay = 'Spatial',min.cells=3) #18056*1752
sio2_56$stim <- "sio2_56"
sio2_56_img <- Read10X_Image(image.dir="G:/raw_material for omics_chaojie/silicosis for spatial transcription/B11/spatial/")
DefaultAssay(object = sio2_56_img) <- 'Spatial'
sio2_56_img <- sio2_56_img[colnames(sio2_56)]
sio2_56[['image']] <- sio2_56_img
SpatialFeaturePlot(sio2_56, features = "nCount_Spatial")
getwd()
#save(sio2_56,file="G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/sio2_56.rds")  #sio2_56
colnames(sio2_56)
load("G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/sio2_56.rds")



NS_56_exp <- Read10X_h5(filename = "G:/raw_material for omics_chaojie/silicosis for spatial transcription/A73/2.3.h5_files/filtered_feature_bc_matrix.h5" ) #1887
NS_56_exp_valid=NS_56_exp
NS_56=CreateSeuratObject(counts = NS_56_exp_valid, project = 'NS_56', assay = 'Spatial',min.cells=3) #18056*1752
NS_56$stim <- "NS_56"
NS_56_img <- Read10X_Image(image.dir="G:/raw_material for omics_chaojie/silicosis for spatial transcription/A73/spatial/")
DefaultAssay(object = NS_56_img) <- 'Spatial'
NS_56_img <- NS_56_img[colnames(NS_56)]
NS_56[['image']] <- NS_56_img
SpatialFeaturePlot(NS_56, features = "nCount_Spatial")
getwd()
#save(NS_56,file="G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/NS_56.rds")  #sio2_56
load("G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/sio2_56.rds")



NS_7_exp <- Read10X_h5(filename = "G:/raw_material for omics_chaojie/silicosis for spatial transcription/A72/2.3.h5_files/filtered_feature_bc_matrix.h5" ) #1887
NS_7_exp_valid=NS_7_exp
NS_7=CreateSeuratObject(counts = NS_7_exp_valid, project = 'NS_7', assay = 'Spatial',min.cells=3) #18056*1752
NS_7$stim <- "NS_7"
NS_7_img <- Read10X_Image(image.dir="G:/raw_material for omics_chaojie/silicosis for spatial transcription/A72/spatial/")
DefaultAssay(object = NS_7_img) <- 'Spatial'
NS_7_img <- NS_7_img[colnames(NS_7)]
NS_7[['image']] <- NS_7_img
SpatialFeaturePlot(NS_7, features = "nCount_Spatial")
getwd()
#save(NS_56,file="G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/NS_7.rds")  #sio2_56
load("G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/sio2_56.rds")



#####
############单样本SCT,对空间转录组进行单个样本的标准化操作
sio2_7_sct=SCTransform(sio2_7, verbose = FALSE,,assay ="Spatial")
save(sio2_7_sct,file="sio2_7_sct.rds")

sio2_56_sct=SCTransform(sio2_56, verbose = FALSE,,assay ="Spatial")
save(sio2_56_sct,file="sio2_56_sct.rds")

NS_7_sct=SCTransform(NS_7, verbose = FALSE,,assay ="Spatial")
save(NS_7_sct,file="NS_7_sct.rds")

NS_56_sct=SCTransform(NS_56, verbose = FALSE,,assay ="Spatial")
save(NS_56_sct,file="NS_56_sct.rds")



d.all = merge(sio2_7, c(NS_7, sio2_56, NS_56))
colnames(d.all)[1:100]
colnames(d.all)[3000:3009]

d.all = SCTransform(d.all, verbose = FALSE,assay ="Spatial")
d.all = RunPCA(d.all, verbose = FALSE)
d.all = RunHarmony(d.all, group.by.vars="stim", plot_convergence = TRUE, assay.use="SCT")

d.all=FindVariableFeatures(d.all,selection.method = "vst", nfeatures = length(rownames(d.all)))


d.all <- d.all %>% 
  RunUMAP(reduction = "harmony", dims = 1:30) %>% 
  RunTSNE(reduction = "harmony", dims = 1:30) %>% 
  FindNeighbors(reduction = "harmony", dims = 1:30)


grep(pattern = '1_1',colnames(d.all))
colnames(d.all)
colnames(All)
table(grepl(pattern = '1_1',colnames(d.all)))#1815 sio2_7d
table(grepl(pattern = '1_2',colnames(d.all)))#1638  ns7
table(grepl(pattern = '1_3',colnames(d.all)))#1887 sio2_56d
table(grepl(pattern = '1_4',colnames(d.all)))#1438  ns_56d


table(grepl(pattern = '1',colnames(All)))#1638
table(grepl(pattern = '2',colnames(All))) #1438
table(grepl(pattern = '3',colnames(All)))#1887
table(grepl(pattern = '4',colnames(All))) #1815

#改固定模式 把固定模式的字符替换 
gsub(pattern = "1_1",replacement = "4",colnames(d.all))
gsub(pattern = "1_2",replacement = "1",colnames(d.all))
gsub(pattern = "1_3",replacement = "3",colnames(d.all))
gsub(pattern = "1_4",replacement = "2",colnames(d.all))

colnames(d.all)=as.vector(
  ifelse(grepl(pattern = '1_1',colnames(d.all))==TRUE,gsub(pattern = "1_1",replacement = "4",colnames(d.all)),
         ifelse(grepl(pattern = '1_2',colnames(d.all))==TRUE,gsub(pattern = "1_2",replacement = "1",colnames(d.all)),
                ifelse(grepl(pattern = '1_3',colnames(d.all))==TRUE,gsub(pattern = "1_3",replacement = "3",colnames(d.all)),
                       gsub(pattern = "1_4",replacement = "2",colnames(d.all)))))
)

length(ifelse(grepl(pattern = '1_1',colnames(d.all))==TRUE,gsub(pattern = "1_1",replacement = "4",colnames(d.all)),
              ifelse(grepl(pattern = '1_2',colnames(d.all))==TRUE,gsub(pattern = "1_2",replacement = "1",colnames(d.all)),
                     ifelse(grepl(pattern = '1_3',colnames(d.all))==TRUE,gsub(pattern = "1_3",replacement = "3",colnames(d.all)),
                            gsub(pattern = "1_4",replacement = "2",colnames(d.all)))))
)
length(colnames(d.all))

d.all$colnames=ifelse(grepl(pattern = '1_1',colnames(d.all))==TRUE,gsub(pattern = "1_1",replacement = "4",colnames(d.all)),
                      ifelse(grepl(pattern = '1_2',colnames(d.all))==TRUE,gsub(pattern = "1_2",replacement = "1",colnames(d.all)),
                             ifelse(grepl(pattern = '1_3',colnames(d.all))==TRUE,gsub(pattern = "1_3",replacement = "3",colnames(d.all)),
                                    gsub(pattern = "1_4",replacement = "2",colnames(d.all)))))

length(colnames(d.all))
#改名字,重命名细胞id
d.all=RenameCells(d.all,new.names = d.all$colnames)

table(grepl(pattern = '1',colnames(d.all)))
table(grepl(pattern = '2',colnames(d.all)))
table(grepl(pattern = '3',colnames(d.all)))

#selected genes
if(1==1){
  markers_from_loupe=read.csv("G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap/all-merged-Graph-Based Genes.csv")
  head(markers_from_loupe)
  input=markers_from_loupe
  n = (ncol(input)-2)/3 #获取每个csv文件中cluster的个数
  n
  head(input)
  library(org.Mm.eg.db)
  k <- keys(org.Mm.eg.db, keytype = "ENTREZID")  #获取小鼠的entrezid 和gene symbol的对应关系,注释包
  gene.list <- select(org.Mm.eg.db, keys = k, columns = c("SYMBOL", "ENSEMBL"), keytype = "ENTREZID")## 73306     3
  head(gene.list)
  input$FeatureName.entrez = gene.list[match(input$FeatureID, gene.list$ENSEMBL),"ENTREZID"]
  #在csv文件的基础上添加symbol与entrezid相对应的列FeatureName.entrez
  print(head(input,15))
  head(input[,grepl(pattern = 'Average',colnames(input))])
  
  
  stat = data.frame()
  stat
  mygene=c()
  for(cluster in 1:n){
    #cluster=1
    print(cluster)
    #提取input中的"FeatureName.entrez"列和 cluster.1.log2.Fold.Change列
    input.sub = na.omit(input[,c("FeatureName",paste0("Cluster.",cluster,".Log2.Fold.Change"))])
    head(input.sub)
    #write.table(input.sub, paste0(unlist(strsplit(i, " "))[1], "_Cluster_", cluster, "_log2FC.txt"), quote = FALSE, sep = "\t", row.names = FALSE, col.names = FALSE)
    input.sub = na.omit(input[,c("FeatureName",paste0("Cluster.",cluster,".Log2.Fold.Change"),paste0("Cluster.",cluster,".P.Value"))])
    colnames(input.sub) = c("gene", "log2FC", "PValue")
    head(input.sub)
    
    subset_data = head(subset(input.sub, log2FC>0.21&PValue<0.5),n=20) ###可以自己定义的地方  这里选择上调变化大于2倍,且p-value小于0.01的基因列表
    subset_data
    stat[paste0(unlist(strsplit('i', " "))[1], "_Cluster_", cluster), "up"] = nrow(subset_data)
    head(stat)
    mygene=c(mygene,subset_data$gene)
    
  }
  mygene
}



#clustering
getwd()
setwd("G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12")
fs=list.files(pattern = '.cluster')
fs
clustering_spot=read.csv(fs)
head(clustering_spot)
clustering_spot=clustering_spot[,1:2]
head(clustering_spot)
rownames(clustering_spot)=clustering_spot$Barcode           
head(clustering_spot)


a=Read10X_h5(filename = 'G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap/raw_feature_bc_matrix.h5')
a[1:4,1:4] 
length(colnames(a))
colnames(a)[grepl(pattern = '2',colnames(a))]
match(c(1,3,4,0),  c(3,4,5,1))
match(colnames(a),rownames(clustering_spot))
match(rownames(clustering_spot),colnames(a))

b=a[,match(rownames(clustering_spot),colnames(a))]
length(colnames(b))

length(colnames(d.all))

myindex=match(colnames(d.all),rownames(clustering_spot))
graphclustering=clustering_spot$graphclust[c(myindex)]

d.all$clustering_spot_from_loupe=graphclustering
colnames(d.all)[1:10]
Idents(d.all)[1:10]
Idents(d.all)=d.all$clustering_spot_from_loupe
clustering_spot[rownames(clustering_spot)=='AAACATTTCCCGGATT-1',]

DimPlot(d.all)
DoHeatmap(d.all,features = mygene,group.by ='clustering_spot_from_loupe' )

jpeg("heatmap_mygene_20_for_loupe.jpg",width = 13,height = 15,units = 'in', res=400)
p=DoHeatmap(All, features =mygene,size = 2) 
print(p)
dev.off()


getwd()
save(d.all,file = "G:/silicosis/sicosis/silicosis_ST/yll/0113-3_2022_8-19_doheatmap_sct_from_10.12/d.all.rds")


你可能感兴趣的:(笔记,算法,python,开发语言)