cellchat_xfy_sepsis 標準プロセス調和


.libPaths(c("/home/data/refdir/Rlib/",  "/home/data/t040413/R/x86_64-pc-linux-gnu-library/4.2", "/usr/local/lib/R/library"))

getwd()
dir.create("/home/data/t040413/silicosis/xfy_sepsis")
setwd("/home/data/t040413/silicosis/xfy_sepsis")


load("../sepsis_cluster_merge.rds")

head([email protected])
table(All.merge$cell.type)

subset_data=subset(All.merge,idents=c("Macrophage","Fibroblast"))

#DimPlot(subset_data)


subset_data[["percent.mt"]] <- PercentageFeatureSet(subset_data, pattern = "^mt-")
VlnPlot(subset_data, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)


subset_data = subset_data %>%
  Seurat::NormalizeData(verbose = FALSE) %>%  
  FindVariableFeatures(selection.method = "vst", nfeatures = 2000) %>%
  ScaleData(verbose = FALSE) %>%
  RunPCA(npcs = 50, verbose = FALSE)
ElbowPlot(subset_data, ndims = 50)
VlnPlot(subset_data, features = c("nFeature_RNA", "nCount_RNA", "percent.mt"), ncol = 3)


## 466	510
#[email protected]$stim <- c(rep("Exp", length(grep("1$",colnames(subset_data)))),rep("Con", length(grep("2$",colnames(subset_data)))))
#table(subset_data$stim)

library('harmony')

subset_data <- subset_data %>% RunHarmony("stim", plot_convergence = TRUE)
harmony_embeddings <- Embeddings(subset_data, 'harmony') 
#######################cluster
dims = 1:30
subset_data <- subset_data %>% 
  RunUMAP(reduction = "harmony", dims = dims) %>% 
  RunTSNE(reduction = "harmony", dims = dims) %>% 
  FindNeighbors(reduction = "harmony", dims = dims)

subset_data=FindClusters(subset_data,resolution =0.3)
#DimPlot(subset_data)
#head([email protected])

subset_data=RenameIdents(subset_data,"0"="Macrophage-1","1"="Macrophage-2",
                       "2"='Fibroblasts', '3'='Fibroblasts','4'="Fibroblasts")


save(subset_data,file = "/home/data/t040413/silicosis/xfy_sepsis/subset_Data_for_macrophages_fibroblasts.rds")
#####################3

library(dplyr)
library(cowplot)
library(Seurat)
library(harmony)
library(ggplot2)
require(Matrix)
require(magrittr)
library(openxlsx)
library(CellChat)

path=getwd()

#head([email protected])
#Idents(subset_data)
#table(subset_data$stim)
##	CellChat
dir.create(paste(path, "CellChat", sep = "/"))
setwd(paste(path, "CellChat", sep = "/"))
#getwd()
#setwd("../")


for(stim in c("Con", "Exp")){
  dir.create(paste(path, "CellChat", stim, sep = "/"))
  setwd(paste(path, "CellChat", stim, sep = "/"))
  data.input = subset_data$RNA@data[, subset_data$stim==stim]
  meta = data.frame(labels = Idents(subset_data)[subset_data$stim==stim], row.names = colnames(subset_data)[subset_data$stim==stim])
  cellchat <- createCellChat(object = data.input, meta = meta, group.by = "labels")
  CellChatDB <- CellChatDB.mouse
  cellchat@DB <- CellChatDB
  cellchat <- subsetData(cellchat)
 # future::plan("multiprocess", workers = 4) 
  cellchat <- identifyOverExpressedGenes(cellchat)
  cellchat <- identifyOverExpressedInteractions(cellchat)
  cellchat <- projectData(cellchat, PPI.mouse)
  cellchat <- computeCommunProb(cellchat)
  cellchat <- filterCommunication(cellchat, min.cells = 10)
  cellchat <- computeCommunProbPathway(cellchat)
  cellchat <- aggregateNet(cellchat)
  df.net <- subsetCommunication(cellchat)
  write.xlsx(df.net,file='0.Cell-Cell_Communications_At_L-R.xlsx', rowNames=F, colNames=T)
  df.net <- subsetCommunication(cellchat, slot.name = "netP")
  write.xlsx(df.net,file='0.Cell-Cell_Communications_At_Pathway.xlsx', rowNames=F, colNames=T)
  groupSize <- as.numeric(table(cellchat@idents))
  mat <- cellchat@net$count
  write.xlsx(mat, file='1.NumberOfInteractions.xlsx', rowNames=T, colNames=T)
  pdf("1.NumberOfInteractions.pdf")
  netVisual_circle(mat, vertex.weight = groupSize, weight.scale = T, label.edge= F, title.name = "Number of interactions")
  dev.off()
  pdf("1.NumberOfInteractions_Split.pdf")
  for (i in 1:nrow(mat))  {
    mat2 <- matrix(0, nrow = nrow(mat), ncol = ncol(mat), dimnames = dimnames(mat))
    mat2[i, ] <- mat[i, ]
    p = netVisual_circle(mat2, vertex.weight = groupSize, weight.scale = T, edge.weight.max = max(mat), title.name = rownames(mat)[i])
    print(p)
  }
  dev.off()
  mat <- cellchat@net$weight
  write.xlsx(mat, file='2.InteractionWeights.xlsx', rowNames=T, colNames=T)
  pdf("2.InteractionWeights.pdf")
  netVisual_circle(mat, vertex.weight = groupSize, weight.scale = T, label.edge= F, title.name = "Interaction weights/strength")
  dev.off()
  pdf("2.InteractionWeights_Split.pdf")
  for (i in 1:nrow(mat)) {
    mat2 <- matrix(0, nrow = nrow(mat), ncol = ncol(mat), dimnames = dimnames(mat))
    mat2[i, ] <- mat[i, ]
    p = netVisual_circle(mat2, vertex.weight = groupSize, weight.scale = T, edge.weight.max = max(mat), title.name = rownames(mat)[i])
    print(p)
  }
  dev.off()
  pathways = cellchat@netP$pathways
  vertex.receiver = seq(1,3)
  pdf("3.Sig_Pathway_Hierarchy_Plot.pdf")
  for(i in pathways){
    print(i)
    p = netVisual_aggregate(cellchat, signaling = i,vertex.receiver = vertex.receiver, vertex.label.cex = 0.4)
    print(p)
  }
  dev.off()
  pdf("3.Sig_Pathway_Circle_Plot.pdf")
  for(i in pathways){
    print(i)
    p = netVisual_aggregate(cellchat, signaling = i,vertex.receiver = vertex.receiver, layout = "circle")
    print(p)
  }
  dev.off()
  pdf("4.Sig_Pathway_L-R_pair_Contribution.pdf")
  for(i in pathways){
    print(i)
    p = netAnalysis_contribution(cellchat, signaling = i, title = paste0(i, " signaling pathway", " Contribution of each L-R pair"))
    print(p)
  }
  dev.off()
  pdf("4.Sig_Pathway_L-R_pair.pdf", width=10, height=14)
  netVisual_bubble(cellchat, sources.use = 1:5, targets.use = 1:5, remove.isolate = FALSE)
  dev.off()
  cellchat <- netAnalysis_computeCentrality(cellchat, slot.name = "netP")
  pdf("5.Signaling_Roles_Of_Cell_Groups_Heatmap.pdf")
  for(i in pathways){
    print(i)
    p = netAnalysis_signalingRole_network(cellchat, signaling = i, width = 8, height = 2.5, font.size = 10)
    print(p)
  }
  dev.off()
  pdf("5.Signaling_Roles_Of_Cell_Groups_2D.pdf")
  p = netAnalysis_signalingRole_scatter(cellchat)
  print(p)
  dev.off()
  pdf("5.signals_Contribution_Of_Cell_Groups_Heatmap.pdf", width=10)
  ht1 <- netAnalysis_signalingRole_heatmap(cellchat, pattern = "outgoing", font.size = 5)
  ht2 <- netAnalysis_signalingRole_heatmap(cellchat, pattern = "incoming", font.size = 5)
  print(ht1 + ht2)
  dev.off()
}



おすすめ

転載: blog.csdn.net/qq_52813185/article/details/129971722