load("~/silicosis/spatial/cellchat2_spatial/2024-01-09 18_51_33.849761figures/trims_0.8cellchat.rds")
#setwd("~/silicosis/spatial/cellchat2_spatial/2024-01-09 18_51_33.849761figures/")
# DefaultAssay(d.all)="SCT"
# [email protected] %>%head()
# Idents(d.all)
# brain=d.all
# brain <- RunPCA(brain, assay = "SCT", verbose = FALSE)
# brain <- FindNeighbors(brain, reduction = "pca", dims = 1:30)
# brain <- FindClusters(brain, verbose = FALSE)
# brain <- RunUMAP(brain, reduction = "pca", dims = 1:30)
# SpatialDimPlot(brain, label = TRUE, label.size = 3)
getwd()
# setwd("./cellchat2_spatial/2024-01-09 18_51_33.849761figures/")
#输出单细胞类图表------
library(CellChat)
{
print(getwd())
df.net <- subsetCommunication(cellchat)
head(df.net)
openxlsx:: write.xlsx(df.net,file='0.Cell-Cell_Communications_At_L-R.xlsx', rowNames=F, colNames=T)
df.net <- subsetCommunication(cellchat, slot.name = "netP")
openxlsx:: write.xlsx(df.net,file='0.Cell-Cell_Communications_At_Pathway.xlsx', rowNames=F, colNames=T)
groupSize <- as.numeric(table(cellchat@idents))
## NumberOfInteractions
mat <- cellchat@net$count
openxlsx:: 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()
## InteractionWeights
mat <- cellchat@net$weight
openxlsx:: 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()
## cellchat@netP$pathways: the signaling pathways showing significant communications
pathways = cellchat@netP$pathways
## the left portion shows autocrine and paracrine signaling to certain cell groups of interest (i.e, the defined vertex.receiver)
## the right portion shows autocrine and paracrine signaling to the remaining cell groups in the dataset
vertex.receiver = seq(1,2)
pdf("3.Sig_Pathway_Hierarchy_Plot.pdf")
for(i in pathways){
print(i)
p = netVisual_aggregate(cellchat, signaling = i,vertex.receiver = vertex.receiver,
layout='hierarchy' #, vertex.label.cex = 0.4
)
title(main = paste0(i,' signaling'))
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")
title(main = paste0(i,' signaling'))
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_bubbleplot.pdf", width=25, height=20)
p = netVisual_bubble(cellchat, remove.isolate = FALSE)
print(p)
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("4.Sig_Pathway_L-R_pair_bubbleplot.pdf", width=25, height=20)
p = netVisual_bubble(cellchat, remove.isolate = FALSE)
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()
save(cellchat, file = paste0("cellchat_","_.RData"))
pdf('6_heatmap_counts_weight.pdf')
p1=netVisual_heatmap(cellchat, measure = "count", color.heatmap = 'Blues')
p2=netVisual_heatmap(cellchat, measure = "weight", color.heatmap = "Blues")
print(p1)
print(p2)
dev.off()
head(cellchat@netP)
pdf('7_chord_signalling_cellgroups.pdf')
for (i in cellchat@netP$pathways) {
p= netVisual_chord_cell(cellchat,signaling = i)
print(p)
}
dev.off()
pdf('8_chord_LR_cellgroups.pdf')
for (i in cellchat@netP$pathways) {
p= netVisual_chord_gene(cellchat,sources.use = 1,targets.use = (3:4),slot.name = "net" )
print(p)
}
for (i in cellchat@netP$pathways) {
p= netVisual_chord_gene(cellchat,sources.use = 1,targets.use = (3:4),slot.name = "netP" )
print(p)
}
dev.off()
library(NMF)
library(ggalluvial)
library(CellChat)
selectK(cellchat,pattern = "outgoing")
.libPaths()
npatterns=3
pdf('9_patterns_outcoming3.pdf' )
par(mfrow=c(1,2))
cellchat=identifyCommunicationPatterns(cellchat,pattern = "outgoing",k=npatterns,
heatmap.show = TRUE,height = 10)
dev.off()
pdf("9_river_outgoning_pattern3.pdf",width = 9)
p=netAnalysis_river(cellchat,pattern = "outgoing")
print(p)
dev.off()
pdf("9_dotplot_outgoing_pattern3.pdf" )
p=netAnalysis_dot(cellchat,pattern = 'outgoing')
print(p)
dev.off()
getwd()
#selectK(cellchat,pattern = "incoming")
pdf('9_patterns_incoming 3.pdf' )
par(mfrow=c(1,2))
# cellchat=identifyCommunicationPatterns(cellchat,pattern = "incoming",k=npatterns,height = 9)
cellchat=identifyCommunicationPatterns(cellchat,pattern = "incoming",k=3,height = 15)
dev.off()
pdf('9_patterns_incoming 3.pdf' )
par(mfrow=c(1,2))
# cellchat=identifyCommunicationPatterns(cellchat,pattern = "incoming",k=npatterns,height = 9)
cellchat=identifyCommunicationPatterns(cellchat,pattern = "incoming",k=3,height = 20)
dev.off()
pdf("9_river_incoming_pattern3.pdf",width = 9)
p=netAnalysis_river(cellchat,pattern = "incoming")
print(p)
dev.off()
pdf("9_dotplot_incoming_pattern3.pdf" )
p=netAnalysis_dot(cellchat,pattern = 'incoming')
print(p)
dev.off()
npatterns=2
pdf('patterns_outcoming2.pdf' )
par(mfrow=c(1,2))
cellchat=identifyCommunicationPatterns(cellchat,pattern = "outgoing",k=npatterns,
heatmap.show = TRUE,height = 9)
dev.off()
pdf("9_river_outgoning_pattern2.pdf",width = 9)
p=netAnalysis_river(cellchat,pattern = "outgoing")
print(p)
dev.off()
pdf("9_dotplot_outgoing_pattern2.pdf" )
p=netAnalysis_dot(cellchat,pattern = 'outgoing')
print(p)
dev.off()
getwd()
#selectK(cellchat,pattern = "incoming")
pdf('patterns_incoming2.pdf' )
par(mfrow=c(1,2))
cellchat=identifyCommunicationPatterns(cellchat,pattern = "incoming",k=npatterns,height = 9)
cellchat=identifyCommunicationPatterns(cellchat,pattern = "incoming",k=3,height = 9)
dev.off()
pdf("9_river_incoming_pattern2.pdf",width = 9)
p=netAnalysis_river(cellchat,pattern = "incoming")
print(p)
dev.off()
pdf("9_dotplot_incoming_pattern2.pdf" )
p=netAnalysis_dot(cellchat,pattern = 'incoming')
print(p)
dev.off()
}
13#可视化---
ptm = Sys.time()
groupSize <- as.numeric(table(cellchat@idents))
par(mfrow = c(1,2), xpd=TRUE)
netVisual_circle(cellchat@net$count,
vertex.weight = rowSums(cellchat@net$count),
weight.scale = T, label.edge= F,
title.name = "Number of interactions")
netVisual_circle(cellchat@net$weight,
vertex.weight = rowSums(cellchat@net$weight),
weight.scale = T, label.edge= F,
title.name = "Interaction weights/strength")
color.heatmap <- colorRampPalette(c("lightpink", "red", "darkred"))(9)
netVisual_heatmap(cellchat, measure = "count", color.heatmap = c('white','blue'))
netVisual_heatmap(cellchat, measure = "count", color.heatmap = 'Blues')
netVisual_heatmap(cellchat, measure = "weight", color.heatmap = "Blues")
cellchat@netP$pathways
pathways.show <- c("SPP1")
# Circle plot
par(mfrow=c(1,1))
14#空间可视化
# Spatial plot
library(ggplot2) # Assuming ggplot2 is used for plotting
# Define the slice names to iterate over
slice.names <- c("SiO2_7", "SiO2_56", "NS_56", "NS_7")
# Initiate a PDF file for output
pdf("cellchat_aggregate_spatial_plots.pdf")
# Loop through each slice name and generate the plot
for (slice.name in slice.names) {
p <- netVisual_aggregate(cellchat, signaling = pathways.show, slice.use = slice.name,
layout = "spatial", edge.width.max = 2, vertex.size.max = 1,
alpha.image = 0.2, vertex.label.cex = 0)
print(p) # Print the plot to the PDF
}
# Close the PDF file
dev.off()
# Setting `vertex.label.cex = 0` to hide the labels on the spatial plot
execution.time = Sys.time() - ptm
print(as.numeric(execution.time, units = "secs"))
#> [1] 1.171192
# Compute the network centrality scores
cellchat <- netAnalysis_computeCentrality(cellchat, slot.name = "netP") # the slot 'netP' means the inferred intercellular communication network of signaling pathways
# Visualize the computed centrality scores using heatmap, allowing ready identification of major signaling roles of cell groups
par(mfrow=c(1,1))
netAnalysis_signalingRole_network(cellchat, signaling = pathways.show, width = 8, height = 2.5, font.size = 10)
par(mfrow=c(1,1))
netVisual_aggregate(cellchat, signaling = pathways.show,
slice.use = c("SiO2_56"), layout = "spatial", edge.width.max = 2, alpha.image = 0.2,
vertex.weight = "incoming",
vertex.size.max = 6, vertex.label.cex = 0)
netAnalysis_contribution(cellchat, signaling = pathways.show)
# Take an input of a few genes
spatialFeaturePlot(cellchat, features = c("Tgfb1"), slice.use = "SiO2_7",
point.size = 0.8, color.heatmap = "Reds", direction = 1)
print(getwd())
#request 2
.libPaths(c( "/home/data/t040413/R/x86_64-pc-linux-gnu-library/4.2",
"/home/data/t040413/R/yll/usr/local/lib/R/site-library",
"/home/data/refdir/Rlib/", "/usr/local/lib/R/library"))
# SiO2_7, NS_7, SiO2_56, NS_56
setwd('/home/data/t040413/silicosis/spatial/')
dir.create("~/silicosis/spatial/cellchat2_spatial/am1_am3_inmt_grem1")
setwd("~/silicosis/spatial/cellchat2_spatial/am1_am3_inmt_grem1");print(getwd())
library(Seurat)
library(dplyr)
library(CellChat)
load("~/silicosis/spatial/cellchat2_spatial/2024-01-09 18_51_33.849761figures/trims_0.8cellchat.rds")
table(cellchat@idents)
lrs=cellchat@LR$LRsig
# Take an input of a ligand-receptor pair
spatialFeaturePlot(cellchat, pairLR.use = "TGFB1_TGFBR1_TGFBR2",
slice.use = "SiO2_7", point.size = 0.5, do.binary = FALSE,
cutoff = 0.05, enriched.only = F, color.heatmap = c('blue','white','red'), direction = 1)
# Take an input of a ligand-receptor pair and show expression in binary
spatialFeaturePlot(cellchat, pairLR.use = "BMP2_BMPR1A_ACVR2A",
slice.use = "SiO2_7", point.size = 1.5,
do.binary = TRUE, cutoff = 0.05, enriched.only = F,
color.heatmap = "Reds", direction = 1)
# Take an input of a ligand-receptor pair and show expression in binary
spatialFeaturePlot(cellchat, pairLR.use = "BMP2_BMPR1A_ACVR2A", slice.use = "SiO2_7",
point.size = 1.5, do.binary = TRUE, cutoff = 0.05,
enriched.only = F, color.heatmap = "Reds", direction = 1)