Skip to content

Commit d47572e

Browse files
committed
update
1 parent d20e7be commit d47572e

File tree

7 files changed

+198
-27
lines changed

7 files changed

+198
-27
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import(ggExtra)
4141
import(ggalluvial)
4242
import(ggplot2)
4343
import(ggrepel)
44+
import(grDevices)
4445
import(iterators)
4546
import(methods)
4647
import(parallel)

R/methods.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,10 @@ get_lr_path <- function(object, celltype_sender, celltype_receiver, ligand, rece
806806
ggi_tf <- unique(pathways[, c("src", "dest", "src_tf", "dest_tf")])
807807
st_data <- .get_st_data(object)
808808
st_meta <- .get_st_meta(object)
809-
if (!celltype_sender %in% colnames(object@coef)) {
809+
if (!celltype_sender %in% st_meta$celltype) {
810810
stop("Please provide the correct name of celltype_sender!")
811811
}
812-
if (!celltype_receiver %in% colnames(object@coef)) {
812+
if (!celltype_receiver %in% st_meta$celltype) {
813813
stop("Please provide the correct name of celltype_receiver!")
814814
}
815815
if (!ligand %in% rownames(st_data)) {

R/plot.R

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,15 @@ plot_ccdist <- function(object, celltype_sender, celltype_receiver, color = NULL
571571
#' @param type Set 'sig' to plot significant LR pairs or set 'number' to plot the number of spatial LR interactions.
572572
#' @param fontsize_number fontsize of the numbers displayed in cells.
573573
#' @param number_color color of the text.
574-
#' @import ggplot2 pheatmap
574+
#' @param color_low For 'number' type, define the color for the lowest value.
575+
#' @param color_high For 'number' type, define the color for the highest value.
576+
#' @import ggplot2 pheatmap grDevices
575577
#' @importFrom ggpubr get_palette
576578
#' @importFrom reshape2 dcast
577579
#' @export
578580

579-
plot_cci_lrpairs <- function(object, celltype_sender, celltype_receiver, top_lrpairs = 20, color = NULL,
580-
border_color = "black", type = NULL, fontsize_number = 1, number_color = "black") {
581+
plot_cci_lrpairs <- function(object, celltype_sender, celltype_receiver, top_lrpairs = 20, color = NULL, border_color = "black", type = "sig",
582+
fontsize_number = 5, number_color = "black", color_low = NULL, color_high = NULL) {
581583
# check
582584
if (!is(object, "SpaTalk")) {
583585
stop("Invalid class for object: must be 'SpaTalk'!")
@@ -624,25 +626,41 @@ plot_cci_lrpairs <- function(object, celltype_sender, celltype_receiver, top_lrp
624626
}
625627
ligand <- unique(lrpair$ligand)
626628
receptor <- unique(lrpair$receptor)
627-
# get lrpair_real
628-
lrpair_real <- object@lr_path$lrpairs
629-
lrpair_real <- lrpair_real[, c(1, 2)]
630-
lrpair_real$score <- 1
631-
lrpair_mat <- reshape2::dcast(lrpair_real, formula = ligand ~ receptor, fill = 0, value.var = "score")
632-
rownames(lrpair_mat) <- lrpair_mat$ligand
633-
lrpair_mat <- lrpair_mat[, -1]
634-
lrpair_mat <- lrpair_mat[ligand, receptor]
635-
if (!is.data.frame(lrpair_mat)) {
636-
stop("Limited number of ligand-receptor interactions!")
637-
}
638-
plot_res <- matrix("", nrow = length(ligand), ncol = length(receptor))
639-
rownames(plot_res) <- ligand
640-
colnames(plot_res) <- receptor
641-
for (i in 1:nrow(lrpair)) {
642-
plot_res[lrpair$ligand[i], lrpair$receptor[i]] <- "*"
643-
}
644-
pheatmap::pheatmap(lrpair_mat, cluster_cols = F, cluster_rows = F, color = heat_col, border_color = border_color,
645-
legend = F, display_numbers = plot_res, fontsize_number = fontsize_number, number_color = number_color)
629+
if (type == "sig") {
630+
# get lrpair_real
631+
lrpair_real <- object@lr_path$lrpairs
632+
lrpair_real <- lrpair_real[, c(1, 2)]
633+
lrpair_real$score <- 1
634+
lrpair_mat <- reshape2::dcast(lrpair_real, formula = ligand ~ receptor, fill = 0, value.var = "score")
635+
rownames(lrpair_mat) <- lrpair_mat$ligand
636+
lrpair_mat <- lrpair_mat[, -1]
637+
lrpair_mat <- lrpair_mat[ligand, receptor]
638+
if (!is.data.frame(lrpair_mat)) {
639+
stop("Limited number of ligand-receptor interactions!")
640+
}
641+
plot_res <- matrix("", nrow = length(ligand), ncol = length(receptor))
642+
rownames(plot_res) <- ligand
643+
colnames(plot_res) <- receptor
644+
for (i in 1:nrow(lrpair)) {
645+
plot_res[lrpair$ligand[i], lrpair$receptor[i]] <- "*"
646+
}
647+
pheatmap::pheatmap(lrpair_mat, cluster_cols = F, cluster_rows = F, color = heat_col, border_color = border_color, legend = F, display_numbers = plot_res,
648+
fontsize_number = fontsize_number, number_color = number_color, main = "Significantly enriched LRI")
649+
} else {
650+
if (is.null(color_low)) {
651+
color_low <- "orange"
652+
}
653+
if (is.null(color_high)) {
654+
color_high <- "red"
655+
}
656+
lrpair_real <- lrpair[,c("ligand","receptor","lr_co_exp_num")]
657+
lrpair_mat <- reshape2::dcast(lrpair_real, formula = ligand ~ receptor, fill = 0, value.var = "lr_co_exp_num")
658+
rownames(lrpair_mat) <- lrpair_mat$ligand
659+
lrpair_mat <- lrpair_mat[, -1]
660+
heat_color <- grDevices::colorRampPalette(c(color_low, color_high))(max(as.matrix(lrpair_mat))-1)
661+
heat_color <- c("white", heat_color)
662+
pheatmap::pheatmap(lrpair_mat, cluster_cols = F, cluster_rows = F, border_color = border_color, color = heat_color, main = "Number of spatial LRIs")
663+
}
646664
}
647665

648666
#' @title Plot LR pair

man/plot_cci_lrpairs.Rd

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

script/cellchat.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#==============================================
2+
#================== CellChat ==================
3+
#==============================================
4+
library(CellChat)
5+
library(SpaTalk)
6+
library(tidyverse)
7+
library(Seurat)
8+
options(stringsAsFactors = FALSE)
9+
10+
load(paste0(system.file(package = 'SpaTalk'),'/extdata/starmap_data.rda')) # starmap_data
11+
load(paste0(system.file(package = 'SpaTalk'),'/extdata/starmap_meta.rda')) # starmap_meta
12+
13+
14+
ndata <- starmap_data
15+
celltype <- starmap_meta
16+
rownames(celltype) <- celltype$cell
17+
all(colnames(ndata) == celltype$cell)
18+
19+
seuratObj <- CreateSeuratObject(counts = ndata)
20+
seuratObj <- NormalizeData(seuratObj)
21+
Idents(seuratObj) <- celltype$celltype
22+
23+
data.input <- GetAssayData(seuratObj, assay = "RNA", slot = "data") # normalized data matrix
24+
labels <- Idents(seuratObj)
25+
meta <- data.frame(labels = labels, row.names = names(labels)) # create a dataframe of the cell labels
26+
27+
cellchat <- createCellChat(object = data.input, meta = meta, group.by = "labels")
28+
CellChatDB <- CellChat::CellChatDB.mouse
29+
cellchat@DB <- CellChatDB
30+
31+
cellchat <- subsetData(cellchat)
32+
future::plan("multisession", workers = 4) # do parallel
33+
cellchat <- identifyOverExpressedGenes(cellchat)
34+
cellchat <- identifyOverExpressedInteractions(cellchat)
35+
cellchat <- computeCommunProb(cellchat)
36+
cellchat <- filterCommunication(cellchat, min.cells = 10)
37+
df.net <- res_cellchat[[use_db]][['starmap']][['starmap']] <- df.net %>% dplyr::select(1:6)

script/cellphonedb.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#-----------------------------------
2+
#--- R script after
3+
#-----------------------------------
4+
# Run before python
5+
library(SpaTalk)
6+
library(tidyverse)
7+
8+
load(paste0(system.file(package = 'SpaTalk'),'/extdata/starmap_data.rda')) # starmap_data
9+
load(paste0(system.file(package = 'SpaTalk'),'/extdata/starmap_meta.rda')) # starmap_meta
10+
11+
# orthologs gene
12+
ndata <- starmap_data
13+
orthologs_gene <- mapper[rownames(ndata)]
14+
na_gene <- rownames(ndata)[is.na(orthologs_gene)]
15+
ndata <- ndata[!is.na(orthologs_gene),]
16+
rownames(ndata) <- orthologs_gene[!is.na(orthologs_gene)] %>% make.names()
17+
18+
celltype <- starmap_meta
19+
rownames(celltype) <- celltype$cell
20+
all(colnames(ndata) == celltype$cell)
21+
22+
# create Seurat object
23+
24+
seuratObj <- CreateSeuratObject(counts = ndata)
25+
Idents(seuratObj) <- celltype$celltype
26+
seuratObj[['celltype']] <- celltype$celltype
27+
seuratObj <- NormalizeData(seuratObj)
28+
29+
writeMM(seuratObj@assays$RNA@counts, file = 'data/cpdb_input/starmap_counts_mtx/matrix.mtx')
30+
# save gene and cell names
31+
write(x = rownames(seuratObj@assays$RNA@counts), file = "data/cpdb_input/starmap_counts_mtx/features.tsv")
32+
write(x = colnames(seuratObj@assays$RNA@counts), file = "data/cpdb_input/starmap_counts_mtx/barcodes.tsv")
33+
34+
35+
df = [email protected][, c('Cell', 'celltype')]
36+
write.table(df, file ='data/cpdb_input/starmap_meta.tsv', sep = '\t', quote = F, row.names = F)
37+
38+
#===============================================================================================
39+
40+
41+
#-----------------------------------
42+
#--- python script after
43+
#-----------------------------------
44+
45+
conda activate cpdb
46+
cd data/cpdb_input/
47+
cellphonedb method statistical_analysis starmap_meta.tsv starmap_counts_mtx/ --counts-data gene_name
48+
49+
#-----------------------------------
50+
#--- python script before
51+
#-----------------------------------
52+
53+
54+
#=================================================================================
55+
56+
#-----------------------------------
57+
#--- R script after
58+
#-----------------------------------
59+
60+
# standardize cpdb output
61+
res_cpdb_uniondb <- list()
62+
# create mapper to convert gene into mouse gene
63+
mapper <- gene2orthologs$mousegene
64+
names(mapper) <- gene2orthologs$humangene
65+
gene2orthologs <- readRDS("~/待处理/Spatalk_Benchmark/verified_database/cpdb/gene2orthologs_human_mouse.rds")
66+
67+
cpdb2df <- function(data) {
68+
df_data <- data.frame()
69+
Ligand = c()
70+
Receptor = c()
71+
Ligand.Cluster = c()
72+
Receptor.Cluster = c()
73+
isReceptor_fst = c()
74+
isReceptor_scn = c()
75+
MeanLR = c()
76+
77+
for (i in 1:nrow(data)) {
78+
pair <- strsplit(data$`interacting_pair`[i],split = "_")
79+
for (j in 13:ncol(data)) {
80+
c_pair = strsplit(colnames(data)[j],"\\|")
81+
if (data[i,j] != 0.0) {
82+
Ligand <- c(Ligand,pair[[1]][1])
83+
Receptor <- c(Receptor,pair[[1]][2])
84+
Ligand.Cluster <- c(Ligand.Cluster,c_pair[[1]][1])
85+
Receptor.Cluster <- c(Receptor.Cluster,c_pair[[1]][2])
86+
isReceptor_fst <- c(isReceptor_fst,data$`receptor_a`[i])
87+
isReceptor_scn <- c(isReceptor_scn,data$`receptor_b`[i])
88+
MeanLR <- c(MeanLR,data[i,j])
89+
}
90+
91+
}
92+
93+
}
94+
df_data <- data.frame(Ligand = Ligand, Receptor = Receptor,
95+
Ligand.Cluster = Ligand.Cluster,Receptor.Cluster = Receptor.Cluster,
96+
isReceptor_fst = isReceptor_fst,
97+
isReceptor_scn = isReceptor_scn,
98+
MeanLR = MeanLR)
99+
return(df_data)
100+
}
101+
102+
#--- starmap
103+
cpdb_O <- read.table(paste0("cpdb_input/","/out/significant_means.txt"),sep = "\t",header = T,check.names = F,stringsAsFactors = F)
104+
cpdb_O[is.na(cpdb_O)] <- 0
105+
cpdb_O <- cpdb2df(cpdb_O)
106+
107+
cpdb_O <- cpdb_O %>% mutate(Ligand = mapper[Ligand], Receptor = mapper[Receptor])
108+
cpdb_O <- cpdb_O %>% rename(receptor = Ligand,ligand = Receptor, sender = Receptor.Cluster, receiver = Ligand.Cluster)
109+

vignettes/SpaTalk.pdf

851 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)