web123456

⑤ Single-cell learning-cellchat intergroup communication difference analysis

catalogs

1, data loading

2, create CellChat objects

① LS group data

② NL group data

3, build the cellchat file separately

① Construct LS group cellchat data

② Constructing NL group cellchat data

4, perform cellchat analysis

① Cellchat analysis of the LS group

② Cellchat analysis of NL groups

5, merge and group cellchat objects

①Data loading

②Merge cellchat objects

analyze

1. Overall comparison: differences in the number and intensity of communications

2. Differences in communication at the level of cell subpopulations

2.1 Network diagram of cellular communication differences

2.2 Heatmap of cellular communication differences

3. Communication differences at the level of signaling pathways

3.1 Bar graph of differences in enriched signaling pathways between groups

3.2 Horizontal heat map of outgoing signaling pathways

3.3 Horizontal heat map of afferent signaling pathways

3.4 Heat map of overall signaling pathway levels

4. Differences in horizontal communication between ligand-receptor pairs

4.1 Bubble diagram of probability differences between total paired receptor pairs

4.2 Distinguishing between upper and lower marshaled receptor pairs

5. Visualization of differences at the level of individual/specific signaling pathways

5.1 Network Diagram

5.2 Heat map

④-2 Single Cell Learning - cellchat Single Data Code Supplement (Communication Network) - CSDN Blogs

1, data loading
  1. # Package loading
  2. #CellChatIntergroup Difference Analysis of Cellular Communication#
  3. rm(list=ls())
  4. library(CellChat)
  5. library(patchwork)
  6. library(ggplot2)
  7. library(Seurat)
  8. library(ggalluvial)# Plotting the Sankey map
  9. library(expm)
  10. library(sna)
  11. library(NMF)
  12. library(ComplexHeatmap)
  13. options(stringsAsFactors = FALSE)## input data is not automatically converted to factors (to prevent data formatting errors)

Data loading

  1. #DataLoad ################
  2. load("data_humanSkin.Rdata")# Data loading: here's thecount datadigital
  3. data.input = data_humanSkin$data#Normalized gene expression matrix and cell grouping information files required
  4. #dat <- as.data.frame(data.input)# You can view the expression matrix [1] 17328 7563
  5. meta = data_humanSkin$meta
  6. data.input[1:6,1:3] #Expressioncount
  7. head(meta);table(meta$condition) #with normal(NL) and disequations(LS)
  8. unique(meta$labels) #check cell subpopulation label type

2, create CellChat objects
① LS group data
  1. # LS group data
  2. cell.use1 = rownames(meta)[meta$condition == 'LS'] #Name of the cell from which the LS was extracted
  3. data.input1 = data.input[, cell.use1]# Extract the LS expression matrix
  4. meta1 = meta[cell.use1, ]#Extract LS cell information
  5. identical(rownames(meta1),colnames(data.input1)) #Check that matrix column names and grouped file line names are identical
  6. unique(meta1$labels) #Check cell subpopulation label types
② NL group data
  1. # NL group data
  2. cell.use2 = rownames(meta)[meta$condition == 'NL'] #Name of cell from which NL was extracted
  3. data.input2 = data.input[, cell.use2]# Extract the NL expression matrix
  4. meta2 = meta[cell.use2, ]#Extract NL cell information
  5. identical(rownames(meta2),colnames(data.input2)) #Check that matrix column names and grouped file line names are identical
  6. unique(meta2$labels) #Check cell subpopulation label types
  7. #Data checking: cell line checking in different subgroups
  8. setdiff(unique(meta1$labels),unique(meta2$labels))#character(0) Cell classifications are all consistent
3, build the cellchat file separately
① Construct LS group cellchat data
  1. # Build cellchat files separately
  2. ## Build LS group cellchat data
  3. cellchatLS <- createCellChat(object = data.input1, # Support for normalized expression matrices, Seurat objects, and SingleCellExperiment objects
  4. meta = meta1, #meta file
  5. group.by = 'labels') #cell classification columns in meta
② Constructing NL group cellchat data
  1. ## Build NL group cellchat data
  2. cellchatNL <- createCellChat(object = data.input2, # Support for normalized expression matrices, Seurat objects, and SingleCellExperiment objects
  3. meta = meta2, #meta file
  4. group.by = 'labels') #cell classification columns in meta
  5. save(cellchatLS, cellchatNL, file = "")# Staging grouped cellchat data
4, perform cellchat analysis
① Cellchat analysis of the LS group
  1. #ReloadData for cellchat analysis #################################################
  2. rm(list=ls())
  3. load("")
  4. Cellchat analysis of #LS groups
  5. cellchat <- cellchatLS
  6. cellchat <- setIdent(cellchat, = 'labels') # Set the labels to the default order of display
  7. levels(cellchat@idents) #View celltype and factor order
  8. table(cellchat@idents) # of cells per celltype
  9. cellchat@DB <- ## Setting up the ligand receptor database (CellChatDB).
  10. cellchat <- subsetData(cellchat)## A subset of the expression matrix of the signaling genes Assignment to cellchat@
  11. cellchat <- identifyOverExpressedGenes(cellchat)## Identify overexpressed signaling genes associated with each cell subpopulation
  12. cellchat <- identifyOverExpressedInteractions(cellchat)## Recognize overexpressed gene ligand-receptor interactions
  13. cellchat <- projectData(cellchat, )#Mapping gene expression data to PPI networks (skippable)
  14. cellchat <- computeCommunProb(cellchat, = TRUE) #Calculating cellular communication probabilities
  15. cellchat <- filterCommunication(cellchat, = 10)## Cellular communication filtering
  16. cellchat <- computeCommunProbPathway(cellchat)# Calculate the probability of communication at the level of the signaling pathway
  17. cellchat <- aggregateNet(cellchat)# Calculate the number and probability strength of inter-cell pair communication
  18. cellchat <- netAnalysis_computeCentrality(cellchat,#Computing network centrality weights: identifying roles/activities of each cell type in signaling pathways
  19. = "netP")
  20. cellchatLS <- cellchat
  21. saveRDS(cellchatLS, "")
② Cellchat analysis of the NL group
  1. Cellchat analysis of #cellchatNL groups
  2. rm(list=ls())
  3. load("")
  4. cellchat <- cellchatNL
  5. cellchat <- setIdent(cellchat, = 'labels') # Set the labels to the default order of display
  6. levels(cellchat@idents) #View celltype and factor order
  7. table(cellchat@idents) # of cells per celltype
  8. cellchat@DB <- ## Setting up the ligand receptor database (CellChatDB).
  9. cellchat <- subsetData(cellchat)## A subset of the expression matrix of the signaling genes Assigned to cellchat@
  10. cellchat <- identifyOverExpressedGenes(cellchat)## Identify overexpressed signaling genes associated with each cell subpopulation
  11. cellchat <- identifyOverExpressedInteractions(cellchat)## Recognize overexpressed gene ligand-receptor interactions
  12. cellchat <- projectData(cellchat, )#Mapping gene expression data to PPI networks (skippable)
  13. cellchat <- computeCommunProb(cellchat, = TRUE) #Calculating cellular communication probabilities
  14. cellchat <- filterCommunication(cellchat, = 10)## Cellular communication filtering
  15. cellchat <- computeCommunProbPathway(cellchat)# Calculate the probability of communication at the level of the signaling pathway
  16. cellchat <- aggregateNet(cellchat)# Calculate the number and probability strength of inter-cell pair communication
  17. cellchat <- netAnalysis_computeCentrality(cellchat,#Computing network centrality weights: identifying roles/activities of each cell type in signaling pathways
  18. = "netP")
  19. cellchatNL <- cellchat
  20. saveRDS(cellchatNL, "")
  21. #######################################################

5, merge and group cellchat objects
①Data loading
  1. # Two datasets of skin samples from patients with atopic dermatitis: non-lesional (NL, normal) and lesional (LS, diseased)
  2. rm(list=ls())
  3. cellchatNL <- readRDS("")
  4. cellchatLS <- readRDS("")
  5. levels(cellchatNL@idents)
  6. levels(cellchatLS@idents)
  7. identical(levels(cellchatNL@idents),levels(cellchatLS@idents))#Data checking for consistency [1] TRUE
②Merge cellchat objects
  1. #Merge cellchat objects.
  2. object.list <- list(NL = cellchatNL,
  3. LS = cellchatLS) # control group (NL) first, comparison group (LS) second, note order
  4. cellchat <- mergeCellChat(object.list, add.names = names(object.list))
  5. cellchat
  6. dplyr::glimpse(cellchat)#data structure view
  7. ## Merged content.'','images','net','netP','meta','idents','','DB',and 'LR'

The next step was to start the between-group cellchat analysis


1. Overall comparison: differences in number and intensity of communications
  1. #1. Overall comparison: differences in the number and intensity of communications
  2. p1 <- compareInteractions(cellchat, = F, group = c(1,2))
  3. p2 <- compareInteractions(cellchat, = F, group = c(1,2),
  4. measure = "weight")
  5. p1 + p2

The picture on the left shows the twodata setbetween the number of communications, and the figure on the right illustrates the difference between the strength of communications.

2. Differences in communication at the level of cellular subpopulations
2.1 Network diagram of cellular communication differences
  1. #2. Differences in communication at the cellular subpopulation level
  2. par(mfrow = c(1,2), xpd = TRUE)
  3. netVisual_diffInteraction(cellchat, = T)
  4. netVisual_diffInteraction(cellchat, = T, measure = "weight")

Shows the difference in the number of ligand receptor pairs (left) and the difference in the communication probability (right) in all cell subpopulations between samples; different colors of the outer peripheral circle represent different cell subpopulations, and the size indicates the number of ligand receptor pairs of the subpopulation, and the bigger the circle is, the bigger the ratio of the number of ligand receptor pairs between cells is. The blue line indicates stronger communication in the control group, and the red line indicates stronger communication in the comparison group. The thicker the line, the stronger the degree of variation in communication.

2.2 Heatmap of cellular communication differences
  1. #cellular communication differences heatmap
  2. p3 <- netVisual_heatmap(cellchat)
  3. p4 <- netVisual_heatmap(cellchat, measure = "weight")
  4. p3 + p4
  5. dev.off()

The vertical coordinate is the ligand cell, the horizontal coordinate is the receptor cell, and the heat map color represents the communication probability difference; the darker the color, the stronger the communication. The bars on the top and right are the accumulation of communication probability differences in the vertical and horizontal axes. The left panel shows the difference in the number of ligand-receptor pairs between cells, and the right panel shows the difference in communication probability.

3. Communication differences at the level of signaling pathways
3.1 Bar graph of differences in enriched signaling pathways between groups
  1. #3. Communication differences at the level of signaling pathways
  2. ## Sorting signaling pathways based on information flow or number of interactions
  3. p5 <- rankNet(cellchat, mode = "comparison", stacked = T, = TRUE) #Stacking
  4. p6 <- rankNet(cellchat, mode = "comparison", stacked = F, = TRUE) #No Stacking
  5. p5 + p6

When the ratio of the sum of the probabilities of the pathways in the comparison (LS) group to the control (NL) group is <0.95 and the difference pval <0.05 (rank sum test), the communication strength of the pathway is significantly increased in the control group (vertical coordinates in red); when the ratio of the sum of the probabilities of the pathways in the comparison (LS) group to the control (NL) group is >1.05 and the difference pval <0.05, the communication strength of the pathway in the comparison group is significantly increased (vertical coordinates in blue); vertical coordinates in black indicate that the pathway did not differ between the two groups. Scale plots are shown on the left, and actual value comparison plots are shown on the right.

3.2 Horizontal heat map of outgoing signaling pathways
  1. # Horizontal heat map of efferent signaling pathways
  2. i = 1
  3. <- union(object.list[[i]]@netP$pathways,
  4. object.list[[i+1]]@netP$pathways)
  5. ht1 = netAnalysis_signalingRole_heatmap(object.list[[i]],
  6. pattern = "outgoing", # Outgoing
  7. signaling = ,
  8. title = names(object.list)[i],
  9. width = 5,
  10. height = 6)
  11. ht2 = netAnalysis_signalingRole_heatmap(object.list[[i+1]],
  12. pattern = "outgoing", # Outgoing
  13. signaling = ,
  14. title = names(object.list)[i+1],
  15. width = 5,
  16. height = 6)
  17. draw(ht1 + ht2, ht_gap = unit(0.5, "cm"))

3.3 Horizontal heat map of afferent signaling pathways
  1. #Heatmap of afferent signaling pathway levels
  2. ht3 = netAnalysis_signalingRole_heatmap(object.list[[i]],
  3. pattern = "incoming", # incoming
  4. signaling = ,
  5. title = names(object.list)[i],
  6. width = 5, height = 6,
  7. = "GnBu")
  8. ht4 = netAnalysis_signalingRole_heatmap(object.list[[i+1]],
  9. pattern = "incoming", # incoming
  10. signaling = ,
  11. title = names(object.list)[i+1],
  12. width = 5, height = 6,
  13. = "GnBu")
  14. draw(ht3 + ht4, ht_gap = unit(0.5, "cm"))

3.4 Heat map of overall signaling pathway levels
  1. # Overall signaling pathway level heat map
  2. ht5 = netAnalysis_signalingRole_heatmap(object.list[[i]],
  3. pattern = "all", # Overall
  4. signaling = ,
  5. title = names(object.list)[i],
  6. width = 5, height = 6,
  7. = "OrRd")
  8. ht6 = netAnalysis_signalingRole_heatmap(object.list[[i+1]],
  9. pattern = "all", # Overall
  10. signaling = ,
  11. title = names(object.list)[i+1],
  12. width = 5, height = 6,
  13. = "OrRd")
  14. draw(ht5 + ht6, ht_gap = unit(0.5, "cm"))

The vertical coordinate is the signaling pathway, the horizontal coordinate is the cell subpopulation, and the heatmap color represents the signal intensity; the darker the color, the stronger the communication. The bars on the top and right are the accumulation of the intensity of the vertical and horizontal axes. NL group on the left and LS group on the right.

4. Differences in horizontal communication between ligand-receptor pairs
4.1 Bubble diagram of probability difference between total paired receptor pairs
  1. levels(cellchat@idents$joint) #View cell subpopulations
  2. netVisual_bubble(cellchat,
  3. sources.use = 4,
  4. targets.use = c(5:12),
  5. comparison = c(1, 2),
  6. = 45)

4.2 Distinguishing between upper and lower deployed receptor pairs
  1. p7 <- netVisual_bubble(cellchat,
  2. sources.use = 4,
  3. targets.use = c(5:12),
  4. comparison = c(1, 2),
  5. = 2,
  6. = "Increased signaling in LS",
  7. = 45,
  8. = T) #Increased is the ligand-receptor pair information with stronger probability of communication for the comparator group
  9. p8 <- netVisual_bubble(cellchat,
  10. sources.use = 4,
  11. targets.use = c(5:12),
  12. comparison = c(1, 2),
  13. = 1,
  14. = "Decreased signaling in LS",
  15. = 45,
  16. = T) #Decreased for ligand-receptor pair information with stronger probability of communication for controls
  17. p7 + p8

In the bubble diagram, the horizontal coordinates are cell pairs with color distinguishing samples; the vertical coordinates are ligand receptors. The size of the bubble indicates the p-value; the smaller the p-value the larger the bubble. The color indicates the magnitude of the communication probability.

5. Visualization of differences at the level of individual/specific signaling pathways
5.1network diagram
  1. #Use network diagrams:
  2. <- c("CXCL") #Select target signaling pathways
  3. <- getMaxWeight(object.list,
  4. = c("netP"),
  5. attribute = ) #Control edge weights for different datasets
  6. par(mfrow = c(1,2), xpd = TRUE)
  7. for (i in 1:length(object.list)) {
  8. netVisual_aggregate(object.list[[i]],
  9. signaling = ,
  10. layout = "circle",
  11. = [1],
  12. = 10,
  13. = paste(, names(object.list)[i]))
  14. }

5.2 Heat map
  1. <- c("CXCL")
  2. par(mfrow = c(1,2), xpd = TRUE)
  3. ht <- list()
  4. for (i in 1:length(object.list)) {
  5. ht[[i]] <- netVisual_heatmap(object.list[[i]],
  6. signaling = ,
  7. = "Reds",
  8. = paste(, "signaling ",names(object.list)[i]))
  9. }
  10. ComplexHeatmap::draw(ht[[1]] + ht[[2]], ht_gap = unit(0.5, "cm"))


Reference:

1:Single Cell Analysis of Cellular Interactions-3: CellChat - simple book ()

2:CellChat Cell Communication Analysis (Next) - Hands-on Code - Comparative Analysis of Multiple Data Sets - Knowledge ()

3:CellChat Intergroup Difference Analysis ()

4:focuslyj/CellChat ()